Chromium Code Reviews| 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 | 145 |
| 146 InstantController::InstantController(chrome::BrowserInstantController* browser, | 146 InstantController::InstantController(chrome::BrowserInstantController* browser, |
| 147 bool extended_enabled) | 147 bool extended_enabled) |
| 148 : browser_(browser), | 148 : browser_(browser), |
| 149 extended_enabled_(extended_enabled), | 149 extended_enabled_(extended_enabled), |
| 150 instant_enabled_(false), | 150 instant_enabled_(false), |
| 151 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 151 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 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 } | 158 } |
| 157 | 159 |
| 158 InstantController::~InstantController() { | 160 InstantController::~InstantController() { |
| 159 } | 161 } |
| 160 | 162 |
| 161 bool InstantController::Update(const AutocompleteMatch& match, | 163 bool InstantController::Update(const AutocompleteMatch& match, |
| 162 const string16& user_text, | 164 const string16& user_text, |
| 163 const string16& full_text, | 165 const string16& full_text, |
| 164 const bool verbatim, | 166 const bool verbatim, |
| 165 const bool user_input_in_progress, | 167 const bool user_input_in_progress, |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 browser_->SetInstantSuggestion(last_suggestion_); | 310 browser_->SetInstantSuggestion(last_suggestion_); |
| 309 | 311 |
| 310 // Though we may have handled a URL match above, we return false here, so that | 312 // Though we may have handled a URL match above, we return false here, so that |
| 311 // omnibox prerendering can kick in. TODO(sreeram): Remove this (and always | 313 // omnibox prerendering can kick in. TODO(sreeram): Remove this (and always |
| 312 // return true) once we are able to commit URLs as well. | 314 // return true) once we are able to commit URLs as well. |
| 313 return match_is_search; | 315 return match_is_search; |
| 314 } | 316 } |
| 315 | 317 |
| 316 // TODO(tonyg): This method only fires when the omnibox bounds change. It also | 318 // TODO(tonyg): This method only fires when the omnibox bounds change. It also |
| 317 // needs to fire when the preview bounds change (e.g.: open/close info bar). | 319 // needs to fire when the preview bounds change (e.g.: open/close info bar). |
| 318 void InstantController::SetOmniboxBounds(const gfx::Rect& bounds) { | 320 void InstantController::SetPopupBounds(const gfx::Rect& bounds) { |
| 319 if (!extended_enabled_ && !instant_enabled_) | 321 if (!extended_enabled_ && !instant_enabled_) |
| 320 return; | 322 return; |
| 321 | 323 |
| 322 if (omnibox_bounds_ == bounds) | 324 if (popup_bounds_ == bounds) |
| 323 return; | 325 return; |
| 324 | 326 |
| 325 omnibox_bounds_ = bounds; | 327 popup_bounds_ = bounds; |
| 326 if (omnibox_bounds_.height() > last_omnibox_bounds_.height()) { | 328 if (popup_bounds_.height() > last_popup_bounds_.height()) { |
| 327 update_bounds_timer_.Stop(); | 329 update_bounds_timer_.Stop(); |
| 328 SendBoundsToPage(); | 330 SendBoundsToPage(); |
| 329 } else if (!update_bounds_timer_.IsRunning()) { | 331 } else if (!update_bounds_timer_.IsRunning()) { |
| 330 update_bounds_timer_.Start(FROM_HERE, | 332 update_bounds_timer_.Start(FROM_HERE, |
| 331 base::TimeDelta::FromMilliseconds(kUpdateBoundsDelayMS), this, | 333 base::TimeDelta::FromMilliseconds(kUpdateBoundsDelayMS), this, |
| 332 &InstantController::SendBoundsToPage); | 334 &InstantController::SendBoundsToPage); |
| 333 } | 335 } |
| 334 } | 336 } |
| 335 | 337 |
| 338 void InstantController::SetMarginSize(int start, int end) { | |
| 339 if ((start_margin_ == start && end_margin_ == end) || | |
| 340 !GetPreviewContents() || loader_->IsPointerDownFromActivate()) | |
| 341 return; | |
| 342 | |
| 343 start_margin_ = start; | |
| 344 end_margin_ = end; | |
| 345 loader_->SetMarginSize(start_margin_, end_margin_); | |
| 346 } | |
| 347 | |
| 336 void InstantController::HandleAutocompleteResults( | 348 void InstantController::HandleAutocompleteResults( |
| 337 const std::vector<AutocompleteProvider*>& providers) { | 349 const std::vector<AutocompleteProvider*>& providers) { |
| 338 if (!extended_enabled_) | 350 if (!extended_enabled_) |
| 339 return; | 351 return; |
| 340 | 352 |
| 341 if (!GetPreviewContents()) | 353 if (!GetPreviewContents()) |
| 342 return; | 354 return; |
| 343 | 355 |
| 344 DVLOG(1) << "AutocompleteResults:"; | 356 DVLOG(1) << "AutocompleteResults:"; |
| 345 std::vector<InstantAutocompleteResult> results; | 357 std::vector<InstantAutocompleteResult> results; |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 695 if (!GetInstantURL(template_url, &instant_url)) | 707 if (!GetInstantURL(template_url, &instant_url)) |
| 696 return false; | 708 return false; |
| 697 | 709 |
| 698 if (GetPreviewContents() && loader_->instant_url() != instant_url) | 710 if (GetPreviewContents() && loader_->instant_url() != instant_url) |
| 699 DeleteLoader(); | 711 DeleteLoader(); |
| 700 | 712 |
| 701 if (!GetPreviewContents()) { | 713 if (!GetPreviewContents()) { |
| 702 loader_.reset(new InstantLoader(this, instant_url, active_tab)); | 714 loader_.reset(new InstantLoader(this, instant_url, active_tab)); |
| 703 loader_->Init(); | 715 loader_->Init(); |
| 704 | 716 |
| 705 // Ensure the searchbox API has the correct theme-related info and context. | 717 // Ensure the searchbox API has the correct theme-related info and context. |
|
samarth
2012/11/28 03:51:19
Update comment please
melevin
2012/11/28 20:28:19
Done.
| |
| 706 if (extended_enabled_) { | 718 if (extended_enabled_) { |
| 707 browser_->UpdateThemeInfoForPreview(); | 719 browser_->UpdateThemeInfoForPreview(); |
| 708 loader_->SearchModeChanged(search_mode_); | 720 loader_->SearchModeChanged(search_mode_); |
| 721 loader_->SetMarginSize(start_margin_, end_margin_); | |
| 709 } | 722 } |
| 710 | 723 |
| 711 // Reset the loader timer. | 724 // Reset the loader timer. |
| 712 stale_loader_timer_.Start( | 725 stale_loader_timer_.Start( |
| 713 FROM_HERE, | 726 FROM_HERE, |
| 714 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, | 727 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, |
| 715 &InstantController::OnStaleLoader); | 728 &InstantController::OnStaleLoader); |
| 716 } | 729 } |
| 717 | 730 |
| 718 return true; | 731 return true; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 744 | 757 |
| 745 void InstantController::DeleteLoader() { | 758 void InstantController::DeleteLoader() { |
| 746 // Clear all state, except |last_transition_type_| as it's used during commit. | 759 // Clear all state, except |last_transition_type_| as it's used during commit. |
| 747 last_user_text_.clear(); | 760 last_user_text_.clear(); |
| 748 last_full_text_.clear(); | 761 last_full_text_.clear(); |
| 749 last_verbatim_ = false; | 762 last_verbatim_ = false; |
| 750 last_suggestion_ = InstantSuggestion(); | 763 last_suggestion_ = InstantSuggestion(); |
| 751 last_match_was_search_ = false; | 764 last_match_was_search_ = false; |
| 752 if (!extended_enabled_) | 765 if (!extended_enabled_) |
| 753 search_mode_.mode = chrome::search::Mode::MODE_DEFAULT; | 766 search_mode_.mode = chrome::search::Mode::MODE_DEFAULT; |
| 754 omnibox_bounds_ = gfx::Rect(); | 767 popup_bounds_ = gfx::Rect(); |
| 755 last_omnibox_bounds_ = gfx::Rect(); | 768 last_popup_bounds_ = gfx::Rect(); |
| 756 update_bounds_timer_.Stop(); | 769 update_bounds_timer_.Stop(); |
| 757 stale_loader_timer_.Stop(); | 770 stale_loader_timer_.Stop(); |
| 758 url_for_history_ = GURL(); | 771 url_for_history_ = GURL(); |
| 759 first_interaction_time_ = base::Time(); | 772 first_interaction_time_ = base::Time(); |
| 760 if (GetPreviewContents()) { | 773 if (GetPreviewContents()) { |
| 761 model_.SetPreviewState(chrome::search::Mode(), 0, INSTANT_SIZE_PERCENT); | 774 model_.SetPreviewState(chrome::search::Mode(), 0, INSTANT_SIZE_PERCENT); |
| 762 loader_->CleanupPreviewContents(); | 775 loader_->CleanupPreviewContents(); |
| 763 } | 776 } |
| 764 | 777 |
| 765 // Schedule the deletion for later, since we may have gotten here from a call | 778 // Schedule the deletion for later, since we may have gotten here from a call |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 812 // arrive for this query editing session, record a histogram value. | 825 // arrive for this query editing session, record a histogram value. |
| 813 if (!first_interaction_time_.is_null() && model_.mode().is_default()) { | 826 if (!first_interaction_time_.is_null() && model_.mode().is_default()) { |
| 814 base::TimeDelta delta = base::Time::Now() - first_interaction_time_; | 827 base::TimeDelta delta = base::Time::Now() - first_interaction_time_; |
| 815 UMA_HISTOGRAM_TIMES("Instant.TimeToFirstShow", delta); | 828 UMA_HISTOGRAM_TIMES("Instant.TimeToFirstShow", delta); |
| 816 } | 829 } |
| 817 | 830 |
| 818 model_.SetPreviewState(search_mode_, height, units); | 831 model_.SetPreviewState(search_mode_, height, units); |
| 819 } | 832 } |
| 820 | 833 |
| 821 void InstantController::SendBoundsToPage() { | 834 void InstantController::SendBoundsToPage() { |
| 822 if (last_omnibox_bounds_ == omnibox_bounds_ || | 835 if (last_popup_bounds_ == popup_bounds_ || |
| 823 !GetPreviewContents() || loader_->IsPointerDownFromActivate()) | 836 !GetPreviewContents() || loader_->IsPointerDownFromActivate()) |
| 824 return; | 837 return; |
| 825 | 838 |
| 826 last_omnibox_bounds_ = omnibox_bounds_; | 839 last_popup_bounds_ = popup_bounds_; |
| 827 gfx::Rect preview_bounds = browser_->GetInstantBounds(); | 840 gfx::Rect preview_bounds = browser_->GetInstantBounds(); |
| 828 gfx::Rect intersection = gfx::IntersectRects(omnibox_bounds_, preview_bounds); | 841 gfx::Rect intersection = gfx::IntersectRects(popup_bounds_, preview_bounds); |
| 829 | 842 |
| 830 // Translate into window coordinates. | 843 // Translate into window coordinates. |
| 831 if (!intersection.IsEmpty()) { | 844 if (!intersection.IsEmpty()) { |
| 832 intersection.Offset(-preview_bounds.origin().x(), | 845 intersection.Offset(-preview_bounds.origin().x(), |
| 833 -preview_bounds.origin().y()); | 846 -preview_bounds.origin().y()); |
| 834 } | 847 } |
| 835 | 848 |
| 836 // In the current Chrome UI, these must always be true so they sanity check | 849 // In the current Chrome UI, these must always be true so they sanity check |
| 837 // the above operations. In a future UI, these may be removed or adjusted. | 850 // the above operations. In a future UI, these may be removed or adjusted. |
| 838 // There is no point in sanity-checking |intersection.y()| because the omnibox | 851 // There is no point in sanity-checking |intersection.y()| because the omnibox |
| 839 // can be placed anywhere vertically relative to the preview (for example, in | 852 // can be placed anywhere vertically relative to the preview (for example, in |
| 840 // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds). | 853 // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds). |
| 841 DCHECK_LE(0, intersection.x()); | 854 DCHECK_LE(0, intersection.x()); |
| 842 DCHECK_LE(0, intersection.width()); | 855 DCHECK_LE(0, intersection.width()); |
| 843 DCHECK_LE(0, intersection.height()); | 856 DCHECK_LE(0, intersection.height()); |
| 844 | 857 |
| 845 loader_->SetOmniboxBounds(intersection); | 858 loader_->SetPopupBounds(intersection); |
| 846 } | 859 } |
| 847 | 860 |
| 848 bool InstantController::GetInstantURL(const TemplateURL* template_url, | 861 bool InstantController::GetInstantURL(const TemplateURL* template_url, |
| 849 std::string* instant_url) const { | 862 std::string* instant_url) const { |
| 850 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 863 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 851 if (command_line->HasSwitch(switches::kInstantURL)) { | 864 if (command_line->HasSwitch(switches::kInstantURL)) { |
| 852 *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL); | 865 *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL); |
| 853 return template_url != NULL; | 866 return template_url != NULL; |
| 854 } | 867 } |
| 855 | 868 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 889 } | 902 } |
| 890 | 903 |
| 891 std::map<std::string, int>::const_iterator iter = | 904 std::map<std::string, int>::const_iterator iter = |
| 892 blacklisted_urls_.find(*instant_url); | 905 blacklisted_urls_.find(*instant_url); |
| 893 if (iter != blacklisted_urls_.end() && | 906 if (iter != blacklisted_urls_.end() && |
| 894 iter->second > kMaxInstantSupportFailures) | 907 iter->second > kMaxInstantSupportFailures) |
| 895 return false; | 908 return false; |
| 896 | 909 |
| 897 return true; | 910 return true; |
| 898 } | 911 } |
| OLD | NEW |