| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/instant/instant_controller.h" | 5 #include "chrome/browser/instant/instant_controller.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 bool extended_enabled) | 146 bool extended_enabled) |
| 147 : browser_(browser), | 147 : browser_(browser), |
| 148 extended_enabled_(extended_enabled), | 148 extended_enabled_(extended_enabled), |
| 149 instant_enabled_(false), | 149 instant_enabled_(false), |
| 150 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 150 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 151 last_omnibox_text_has_inline_autocompletion_(false), | 151 last_omnibox_text_has_inline_autocompletion_(false), |
| 152 last_verbatim_(false), | 152 last_verbatim_(false), |
| 153 last_transition_type_(content::PAGE_TRANSITION_LINK), | 153 last_transition_type_(content::PAGE_TRANSITION_LINK), |
| 154 last_match_was_search_(false), | 154 last_match_was_search_(false), |
| 155 is_omnibox_focused_(false), | 155 is_omnibox_focused_(false), |
| 156 start_margin_(0), |
| 157 end_margin_(0), |
| 156 allow_preview_to_show_search_suggestions_(false) { | 158 allow_preview_to_show_search_suggestions_(false) { |
| 157 } | 159 } |
| 158 | 160 |
| 159 InstantController::~InstantController() { | 161 InstantController::~InstantController() { |
| 160 } | 162 } |
| 161 | 163 |
| 162 bool InstantController::Update(const AutocompleteMatch& match, | 164 bool InstantController::Update(const AutocompleteMatch& match, |
| 163 const string16& user_text, | 165 const string16& user_text, |
| 164 const string16& full_text, | 166 const string16& full_text, |
| 165 size_t selection_start, | 167 size_t selection_start, |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 browser_->SetInstantSuggestion(last_suggestion_); | 339 browser_->SetInstantSuggestion(last_suggestion_); |
| 338 | 340 |
| 339 // Though we may have handled a URL match above, we return false here, so that | 341 // Though we may have handled a URL match above, we return false here, so that |
| 340 // omnibox prerendering can kick in. TODO(sreeram): Remove this (and always | 342 // omnibox prerendering can kick in. TODO(sreeram): Remove this (and always |
| 341 // return true) once we are able to commit URLs as well. | 343 // return true) once we are able to commit URLs as well. |
| 342 return last_match_was_search_; | 344 return last_match_was_search_; |
| 343 } | 345 } |
| 344 | 346 |
| 345 // TODO(tonyg): This method only fires when the omnibox bounds change. It also | 347 // TODO(tonyg): This method only fires when the omnibox bounds change. It also |
| 346 // needs to fire when the preview bounds change (e.g.: open/close info bar). | 348 // needs to fire when the preview bounds change (e.g.: open/close info bar). |
| 347 void InstantController::SetOmniboxBounds(const gfx::Rect& bounds) { | 349 void InstantController::SetPopupBounds(const gfx::Rect& bounds) { |
| 348 if (!extended_enabled_ && !instant_enabled_) | 350 if (!extended_enabled_ && !instant_enabled_) |
| 349 return; | 351 return; |
| 350 | 352 |
| 351 if (omnibox_bounds_ == bounds) | 353 if (popup_bounds_ == bounds) |
| 352 return; | 354 return; |
| 353 | 355 |
| 354 omnibox_bounds_ = bounds; | 356 popup_bounds_ = bounds; |
| 355 if (omnibox_bounds_.height() > last_omnibox_bounds_.height()) { | 357 if (popup_bounds_.height() > last_popup_bounds_.height()) { |
| 356 update_bounds_timer_.Stop(); | 358 update_bounds_timer_.Stop(); |
| 357 SendBoundsToPage(); | 359 SendPopupBoundsToPage(); |
| 358 } else if (!update_bounds_timer_.IsRunning()) { | 360 } else if (!update_bounds_timer_.IsRunning()) { |
| 359 update_bounds_timer_.Start(FROM_HERE, | 361 update_bounds_timer_.Start(FROM_HERE, |
| 360 base::TimeDelta::FromMilliseconds(kUpdateBoundsDelayMS), this, | 362 base::TimeDelta::FromMilliseconds(kUpdateBoundsDelayMS), this, |
| 361 &InstantController::SendBoundsToPage); | 363 &InstantController::SendPopupBoundsToPage); |
| 362 } | 364 } |
| 363 } | 365 } |
| 364 | 366 |
| 367 void InstantController::SetMarginSize(int start, int end) { |
| 368 if (!extended_enabled_ || (start_margin_ == start && end_margin_ == end)) |
| 369 return; |
| 370 |
| 371 start_margin_ = start; |
| 372 end_margin_ = end; |
| 373 if (loader_) |
| 374 loader_->SetMarginSize(start_margin_, end_margin_); |
| 375 if (instant_tab_) |
| 376 instant_tab_->SetMarginSize(start_margin_, end_margin_); |
| 377 } |
| 378 |
| 365 void InstantController::HandleAutocompleteResults( | 379 void InstantController::HandleAutocompleteResults( |
| 366 const std::vector<AutocompleteProvider*>& providers) { | 380 const std::vector<AutocompleteProvider*>& providers) { |
| 367 if (!extended_enabled_) | 381 if (!extended_enabled_) |
| 368 return; | 382 return; |
| 369 | 383 |
| 370 if (!instant_tab_ && !loader_) | 384 if (!instant_tab_ && !loader_) |
| 371 return; | 385 return; |
| 372 | 386 |
| 373 DVLOG(1) << "AutocompleteResults:"; | 387 DVLOG(1) << "AutocompleteResults:"; |
| 374 std::vector<InstantAutocompleteResult> results; | 388 std::vector<InstantAutocompleteResult> results; |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 | 786 |
| 773 HideInternal(); | 787 HideInternal(); |
| 774 loader_.reset(new InstantLoader(this, instant_url)); | 788 loader_.reset(new InstantLoader(this, instant_url)); |
| 775 loader_->InitContents(active_tab); | 789 loader_->InitContents(active_tab); |
| 776 | 790 |
| 777 // Ensure the searchbox API has the correct initial state. | 791 // Ensure the searchbox API has the correct initial state. |
| 778 if (extended_enabled_) { | 792 if (extended_enabled_) { |
| 779 browser_->UpdateThemeInfoForPreview(); | 793 browser_->UpdateThemeInfoForPreview(); |
| 780 loader_->SetDisplayInstantResults(instant_enabled_); | 794 loader_->SetDisplayInstantResults(instant_enabled_); |
| 781 loader_->SearchModeChanged(search_mode_); | 795 loader_->SearchModeChanged(search_mode_); |
| 796 loader_->SetMarginSize(start_margin_, end_margin_); |
| 782 } | 797 } |
| 783 | 798 |
| 784 // Restart the stale loader timer. | 799 // Restart the stale loader timer. |
| 785 stale_loader_timer_.Start(FROM_HERE, | 800 stale_loader_timer_.Start(FROM_HERE, |
| 786 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, | 801 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, |
| 787 &InstantController::OnStaleLoader); | 802 &InstantController::OnStaleLoader); |
| 788 | 803 |
| 789 return true; | 804 return true; |
| 790 } | 805 } |
| 791 | 806 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 812 CreateDefaultLoader(); | 827 CreateDefaultLoader(); |
| 813 } | 828 } |
| 814 } | 829 } |
| 815 | 830 |
| 816 void InstantController::ResetInstantTab() { | 831 void InstantController::ResetInstantTab() { |
| 817 if (search_mode_.is_origin_search()) { | 832 if (search_mode_.is_origin_search()) { |
| 818 content::WebContents* active_tab = browser_->GetActiveWebContents(); | 833 content::WebContents* active_tab = browser_->GetActiveWebContents(); |
| 819 if (!instant_tab_ || active_tab != instant_tab_->contents()) { | 834 if (!instant_tab_ || active_tab != instant_tab_->contents()) { |
| 820 instant_tab_.reset(new InstantTab(this, active_tab)); | 835 instant_tab_.reset(new InstantTab(this, active_tab)); |
| 821 instant_tab_->Init(); | 836 instant_tab_->Init(); |
| 837 loader_->SetMarginSize(start_margin_, end_margin_); |
| 822 } | 838 } |
| 823 | 839 |
| 824 // Hide the |loader_| since we are now using |instant_tab_| instead. | 840 // Hide the |loader_| since we are now using |instant_tab_| instead. |
| 825 HideLoader(); | 841 HideLoader(); |
| 826 } else { | 842 } else { |
| 827 instant_tab_.reset(); | 843 instant_tab_.reset(); |
| 828 } | 844 } |
| 829 } | 845 } |
| 830 | 846 |
| 831 bool InstantController::ResetLoaderForMatch(const AutocompleteMatch& match) { | 847 bool InstantController::ResetLoaderForMatch(const AutocompleteMatch& match) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 922 // already showing at 100% height. | 938 // already showing at 100% height. |
| 923 bool is_full_height = model_.height() == 100 && | 939 bool is_full_height = model_.height() == 100 && |
| 924 model_.height_units() == INSTANT_SIZE_PERCENT; | 940 model_.height_units() == INSTANT_SIZE_PERCENT; |
| 925 if (!instant_enabled_ || reason == INSTANT_SHOWN_CUSTOM_NTP_CONTENT || | 941 if (!instant_enabled_ || reason == INSTANT_SHOWN_CUSTOM_NTP_CONTENT || |
| 926 (search_mode_.is_origin_default() && !is_full_height)) | 942 (search_mode_.is_origin_default() && !is_full_height)) |
| 927 model_.SetPreviewState(search_mode_, height, units); | 943 model_.SetPreviewState(search_mode_, height, units); |
| 928 else | 944 else |
| 929 model_.SetPreviewState(search_mode_, 100, INSTANT_SIZE_PERCENT); | 945 model_.SetPreviewState(search_mode_, 100, INSTANT_SIZE_PERCENT); |
| 930 } | 946 } |
| 931 | 947 |
| 932 void InstantController::SendBoundsToPage() { | 948 void InstantController::SendPopupBoundsToPage() { |
| 933 if (last_omnibox_bounds_ == omnibox_bounds_ || !loader_ || | 949 if (last_popup_bounds_ == popup_bounds_ || !loader_ || |
| 934 loader_->is_pointer_down_from_activate()) | 950 loader_->is_pointer_down_from_activate()) |
| 935 return; | 951 return; |
| 936 | 952 |
| 937 last_omnibox_bounds_ = omnibox_bounds_; | 953 last_popup_bounds_ = popup_bounds_; |
| 938 gfx::Rect preview_bounds = browser_->GetInstantBounds(); | 954 gfx::Rect preview_bounds = browser_->GetInstantBounds(); |
| 939 gfx::Rect intersection = gfx::IntersectRects(omnibox_bounds_, preview_bounds); | 955 gfx::Rect intersection = gfx::IntersectRects(popup_bounds_, preview_bounds); |
| 940 | 956 |
| 941 // Translate into window coordinates. | 957 // Translate into window coordinates. |
| 942 if (!intersection.IsEmpty()) { | 958 if (!intersection.IsEmpty()) { |
| 943 intersection.Offset(-preview_bounds.origin().x(), | 959 intersection.Offset(-preview_bounds.origin().x(), |
| 944 -preview_bounds.origin().y()); | 960 -preview_bounds.origin().y()); |
| 945 } | 961 } |
| 946 | 962 |
| 947 // In the current Chrome UI, these must always be true so they sanity check | 963 // In the current Chrome UI, these must always be true so they sanity check |
| 948 // the above operations. In a future UI, these may be removed or adjusted. | 964 // the above operations. In a future UI, these may be removed or adjusted. |
| 949 // There is no point in sanity-checking |intersection.y()| because the omnibox | 965 // There is no point in sanity-checking |intersection.y()| because the omnibox |
| 950 // can be placed anywhere vertically relative to the preview (for example, in | 966 // can be placed anywhere vertically relative to the preview (for example, in |
| 951 // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds). | 967 // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds). |
| 952 DCHECK_LE(0, intersection.x()); | 968 DCHECK_LE(0, intersection.x()); |
| 953 DCHECK_LE(0, intersection.width()); | 969 DCHECK_LE(0, intersection.width()); |
| 954 DCHECK_LE(0, intersection.height()); | 970 DCHECK_LE(0, intersection.height()); |
| 955 | 971 |
| 956 loader_->SetOmniboxBounds(intersection); | 972 loader_->SetPopupBounds(intersection); |
| 957 } | 973 } |
| 958 | 974 |
| 959 bool InstantController::GetInstantURL(const TemplateURL* template_url, | 975 bool InstantController::GetInstantURL(const TemplateURL* template_url, |
| 960 std::string* instant_url) const { | 976 std::string* instant_url) const { |
| 961 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 977 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 962 if (command_line->HasSwitch(switches::kInstantURL)) { | 978 if (command_line->HasSwitch(switches::kInstantURL)) { |
| 963 *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL); | 979 *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL); |
| 964 return template_url != NULL; | 980 return template_url != NULL; |
| 965 } | 981 } |
| 966 | 982 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1000 } | 1016 } |
| 1001 | 1017 |
| 1002 std::map<std::string, int>::const_iterator iter = | 1018 std::map<std::string, int>::const_iterator iter = |
| 1003 blacklisted_urls_.find(*instant_url); | 1019 blacklisted_urls_.find(*instant_url); |
| 1004 if (iter != blacklisted_urls_.end() && | 1020 if (iter != blacklisted_urls_.end() && |
| 1005 iter->second > kMaxInstantSupportFailures) | 1021 iter->second > kMaxInstantSupportFailures) |
| 1006 return false; | 1022 return false; |
| 1007 | 1023 |
| 1008 return true; | 1024 return true; |
| 1009 } | 1025 } |
| OLD | NEW |