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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 | 139 |
| 140 return false; | 140 return false; |
| 141 } | 141 } |
| 142 | 142 |
| 143 bool IsFullHeight(const InstantModel& model) { | 143 bool IsFullHeight(const InstantModel& model) { |
| 144 return model.height() == 100 && model.height_units() == INSTANT_SIZE_PERCENT; | 144 return model.height() == 100 && model.height_units() == INSTANT_SIZE_PERCENT; |
| 145 } | 145 } |
| 146 | 146 |
| 147 } // namespace | 147 } // namespace |
| 148 | 148 |
| 149 const char* InstantController::kLocalOmniboxPopupURL = | |
| 150 "chrome://local-omnibox-popup/local-omnibox-popup.html"; | |
| 151 | |
| 149 InstantController::InstantController(chrome::BrowserInstantController* browser, | 152 InstantController::InstantController(chrome::BrowserInstantController* browser, |
| 150 bool extended_enabled) | 153 bool extended_enabled, |
| 154 bool incognito_mode) | |
| 151 : browser_(browser), | 155 : browser_(browser), |
| 152 extended_enabled_(extended_enabled), | 156 extended_enabled_(extended_enabled), |
| 153 instant_enabled_(false), | 157 instant_enabled_(false), |
| 158 incognito_mode_(incognito_mode), | |
| 154 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 159 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 155 last_omnibox_text_has_inline_autocompletion_(false), | 160 last_omnibox_text_has_inline_autocompletion_(false), |
| 156 last_verbatim_(false), | 161 last_verbatim_(false), |
| 157 last_transition_type_(content::PAGE_TRANSITION_LINK), | 162 last_transition_type_(content::PAGE_TRANSITION_LINK), |
| 158 last_match_was_search_(false), | 163 last_match_was_search_(false), |
| 159 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), | 164 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), |
| 160 allow_preview_to_show_search_suggestions_(false) { | 165 allow_preview_to_show_search_suggestions_(false) { |
| 161 } | 166 } |
| 162 | 167 |
| 163 InstantController::~InstantController() { | 168 InstantController::~InstantController() { |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 instant_tab_->Submit(last_omnibox_text_); | 445 instant_tab_->Submit(last_omnibox_text_); |
| 441 instant_tab_->contents()->Focus(); | 446 instant_tab_->contents()->Focus(); |
| 442 return true; | 447 return true; |
| 443 } | 448 } |
| 444 return false; | 449 return false; |
| 445 } | 450 } |
| 446 | 451 |
| 447 if (!IsPreviewingSearchResults() && type != INSTANT_COMMIT_NAVIGATED) | 452 if (!IsPreviewingSearchResults() && type != INSTANT_COMMIT_NAVIGATED) |
| 448 return false; | 453 return false; |
| 449 | 454 |
| 455 // Never commit the local omnibox. | |
| 456 if (loader_->IsLoadingLocalOmniboxPopupURL()) | |
| 457 return false; | |
| 458 | |
| 450 if (type == INSTANT_COMMIT_FOCUS_LOST) | 459 if (type == INSTANT_COMMIT_FOCUS_LOST) |
| 451 loader_->Cancel(last_omnibox_text_); | 460 loader_->Cancel(last_omnibox_text_); |
| 452 else if (type != INSTANT_COMMIT_NAVIGATED) | 461 else if (type != INSTANT_COMMIT_NAVIGATED) |
| 453 loader_->Submit(last_omnibox_text_); | 462 loader_->Submit(last_omnibox_text_); |
| 454 | 463 |
| 455 content::WebContents* preview = loader_->ReleaseContents(); | 464 content::WebContents* preview = loader_->ReleaseContents(); |
| 456 | 465 |
| 457 if (extended_enabled_) { | 466 if (extended_enabled_) { |
| 458 // Consider what's happening: | 467 // Consider what's happening: |
| 459 // 1. The user has typed a query in the omnibox and committed it (either | 468 // 1. The user has typed a query in the omnibox and committed it (either |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 829 #else | 838 #else |
| 830 if (IsViewInContents(GetViewGainingFocus(view_gaining_focus), | 839 if (IsViewInContents(GetViewGainingFocus(view_gaining_focus), |
| 831 loader_->contents())) | 840 loader_->contents())) |
| 832 CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); | 841 CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); |
| 833 else | 842 else |
| 834 HideLoader(); | 843 HideLoader(); |
| 835 #endif | 844 #endif |
| 836 } | 845 } |
| 837 | 846 |
| 838 bool InstantController::ResetLoader(const TemplateURL* template_url, | 847 bool InstantController::ResetLoader(const TemplateURL* template_url, |
| 839 const content::WebContents* active_tab) { | 848 const content::WebContents* active_tab, |
| 849 bool use_fallback) { | |
| 840 std::string instant_url; | 850 std::string instant_url; |
| 841 if (!GetInstantURL(template_url, &instant_url)) | 851 if (!GetInstantURL(template_url, &instant_url)) { |
|
dhollowa
2012/12/13 01:20:15
This seems complicated. If there is just one |use
Shishir
2012/12/13 22:57:58
The use fallback is required for a different reaso
| |
| 842 return false; | 852 if (!use_fallback || !extended_enabled_) |
| 853 return false; | |
| 854 | |
| 855 // If we are in extended mode, fallback to the local popup. | |
| 856 instant_url = kLocalOmniboxPopupURL; | |
| 857 } | |
| 843 | 858 |
| 844 if (loader_ && loader_->instant_url() == instant_url) | 859 if (loader_ && loader_->instant_url() == instant_url) |
| 845 return true; | 860 return true; |
| 846 | 861 |
| 847 HideInternal(); | 862 HideInternal(); |
| 848 loader_.reset(new InstantLoader(this, instant_url)); | 863 loader_.reset(new InstantLoader(this, instant_url)); |
| 849 loader_->InitContents(active_tab); | 864 loader_->InitContents(active_tab); |
| 850 | 865 |
| 851 // Ensure the searchbox API has the correct initial state. | 866 // Ensure the searchbox API has the correct initial state. |
| 852 if (extended_enabled_) { | 867 if (extended_enabled_) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 867 bool InstantController::CreateDefaultLoader() { | 882 bool InstantController::CreateDefaultLoader() { |
| 868 // If there's no active tab, the browser is closing. | 883 // If there's no active tab, the browser is closing. |
| 869 const content::WebContents* active_tab = browser_->GetActiveWebContents(); | 884 const content::WebContents* active_tab = browser_->GetActiveWebContents(); |
| 870 if (!active_tab) | 885 if (!active_tab) |
| 871 return false; | 886 return false; |
| 872 | 887 |
| 873 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( | 888 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( |
| 874 Profile::FromBrowserContext(active_tab->GetBrowserContext()))-> | 889 Profile::FromBrowserContext(active_tab->GetBrowserContext()))-> |
| 875 GetDefaultSearchProvider(); | 890 GetDefaultSearchProvider(); |
| 876 | 891 |
| 877 return ResetLoader(template_url, active_tab); | 892 return ResetLoader(template_url, active_tab, true); |
| 878 } | 893 } |
| 879 | 894 |
| 880 void InstantController::OnStaleLoader() { | 895 void InstantController::OnStaleLoader() { |
| 896 // The local popup is never stale. | |
| 897 if (loader_ && loader_->IsLoadingLocalOmniboxPopupURL()) | |
| 898 return; | |
| 899 | |
| 881 // If the preview is showing or the omnibox has focus, don't delete the | 900 // If the preview is showing or the omnibox has focus, don't delete the |
| 882 // loader. It will get refreshed the next time the preview is hidden or the | 901 // loader. It will get refreshed the next time the preview is hidden or the |
| 883 // omnibox loses focus. | 902 // omnibox loses focus. |
| 884 if (!stale_loader_timer_.IsRunning() && | 903 if (!stale_loader_timer_.IsRunning() && |
| 885 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && | 904 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && |
| 886 model_.mode().is_default()) { | 905 model_.mode().is_default()) { |
| 887 loader_.reset(); | 906 loader_.reset(); |
| 888 CreateDefaultLoader(); | 907 CreateDefaultLoader(); |
| 889 } | 908 } |
| 890 } | 909 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 913 return true; | 932 return true; |
| 914 | 933 |
| 915 // If there's no active tab, the browser is closing. | 934 // If there's no active tab, the browser is closing. |
| 916 const content::WebContents* active_tab = browser_->GetActiveWebContents(); | 935 const content::WebContents* active_tab = browser_->GetActiveWebContents(); |
| 917 if (!active_tab) | 936 if (!active_tab) |
| 918 return false; | 937 return false; |
| 919 | 938 |
| 920 // Try to create a loader for the instant_url in the TemplateURL of |match|. | 939 // Try to create a loader for the instant_url in the TemplateURL of |match|. |
| 921 const TemplateURL* template_url = match.GetTemplateURL( | 940 const TemplateURL* template_url = match.GetTemplateURL( |
| 922 Profile::FromBrowserContext(active_tab->GetBrowserContext()), false); | 941 Profile::FromBrowserContext(active_tab->GetBrowserContext()), false); |
| 923 if (ResetLoader(template_url, active_tab)) | 942 if (ResetLoader(template_url, active_tab, false)) |
|
samarth
2012/12/13 00:58:21
Please explain why we don't want to use fallback i
Shishir
2012/12/13 22:57:58
Done.
| |
| 924 return true; | 943 return true; |
| 925 | 944 |
| 926 // In non-extended mode, stop if we couldn't get a loader for the |match|. | 945 // In non-extended mode, stop if we couldn't get a loader for the |match|. |
| 927 if (!extended_enabled_) | 946 if (!extended_enabled_) |
| 928 return false; | 947 return false; |
| 929 | 948 |
| 930 // If the match is a query, it is for a non-Instant search engine; stop. | 949 // If the match is a query, it is for a non-Instant search engine; stop. |
| 931 if (last_match_was_search_) | 950 if (last_match_was_search_) |
| 932 return false; | 951 return false; |
| 933 | 952 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 986 } | 1005 } |
| 987 | 1006 |
| 988 // If the preview is being shown for the first time since the user started | 1007 // If the preview is being shown for the first time since the user started |
| 989 // typing, record a histogram value. | 1008 // typing, record a histogram value. |
| 990 if (!first_interaction_time_.is_null() && model_.mode().is_default()) { | 1009 if (!first_interaction_time_.is_null() && model_.mode().is_default()) { |
| 991 base::TimeDelta delta = base::Time::Now() - first_interaction_time_; | 1010 base::TimeDelta delta = base::Time::Now() - first_interaction_time_; |
| 992 UMA_HISTOGRAM_TIMES("Instant.TimeToFirstShow", delta); | 1011 UMA_HISTOGRAM_TIMES("Instant.TimeToFirstShow", delta); |
| 993 } | 1012 } |
| 994 | 1013 |
| 995 // Show at 100% height except in the following cases: | 1014 // Show at 100% height except in the following cases: |
| 1015 // - The local omnibox popup is being loaded. | |
| 996 // - Instant is disabled. The page needs to be able to show only a dropdown. | 1016 // - Instant is disabled. The page needs to be able to show only a dropdown. |
| 997 // - The page wants to show custom NTP content. | 1017 // - The page wants to show custom NTP content. |
| 998 // - The page is over a website other than search or an NTP, and is not | 1018 // - The page is over a website other than search or an NTP, and is not |
| 999 // already showing at 100% height. | 1019 // already showing at 100% height. |
| 1000 if (!instant_enabled_ || reason == INSTANT_SHOWN_CUSTOM_NTP_CONTENT || | 1020 if (loader_->IsLoadingLocalOmniboxPopupURL() || !instant_enabled_ || |
| 1021 reason == INSTANT_SHOWN_CUSTOM_NTP_CONTENT || | |
| 1001 (search_mode_.is_origin_default() && !IsFullHeight(model_))) | 1022 (search_mode_.is_origin_default() && !IsFullHeight(model_))) |
| 1002 model_.SetPreviewState(search_mode_, height, units); | 1023 model_.SetPreviewState(search_mode_, height, units); |
| 1003 else | 1024 else |
| 1004 model_.SetPreviewState(search_mode_, 100, INSTANT_SIZE_PERCENT); | 1025 model_.SetPreviewState(search_mode_, 100, INSTANT_SIZE_PERCENT); |
| 1005 } | 1026 } |
| 1006 | 1027 |
| 1007 void InstantController::SendBoundsToPage() { | 1028 void InstantController::SendBoundsToPage() { |
| 1008 if (last_omnibox_bounds_ == omnibox_bounds_ || !loader_ || | 1029 if (last_omnibox_bounds_ == omnibox_bounds_ || !loader_ || |
| 1009 loader_->is_pointer_down_from_activate()) | 1030 loader_->is_pointer_down_from_activate()) |
| 1010 return; | 1031 return; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 1026 // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds). | 1047 // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds). |
| 1027 DCHECK_LE(0, intersection.x()); | 1048 DCHECK_LE(0, intersection.x()); |
| 1028 DCHECK_LE(0, intersection.width()); | 1049 DCHECK_LE(0, intersection.width()); |
| 1029 DCHECK_LE(0, intersection.height()); | 1050 DCHECK_LE(0, intersection.height()); |
| 1030 | 1051 |
| 1031 loader_->SetOmniboxBounds(intersection); | 1052 loader_->SetOmniboxBounds(intersection); |
| 1032 } | 1053 } |
| 1033 | 1054 |
| 1034 bool InstantController::GetInstantURL(const TemplateURL* template_url, | 1055 bool InstantController::GetInstantURL(const TemplateURL* template_url, |
| 1035 std::string* instant_url) const { | 1056 std::string* instant_url) const { |
| 1057 if (incognito_mode_) { | |
| 1058 *instant_url = kLocalOmniboxPopupURL; | |
| 1059 return true; | |
| 1060 } | |
| 1061 | |
| 1036 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 1062 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1037 if (command_line->HasSwitch(switches::kInstantURL)) { | 1063 if (command_line->HasSwitch(switches::kInstantURL)) { |
| 1038 *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL); | 1064 *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL); |
| 1039 return template_url != NULL; | 1065 return template_url != NULL; |
| 1040 } | 1066 } |
| 1041 | 1067 |
| 1042 if (!template_url) | 1068 if (!template_url) |
| 1043 return false; | 1069 return false; |
| 1044 | 1070 |
| 1045 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); | 1071 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 1075 } | 1101 } |
| 1076 | 1102 |
| 1077 std::map<std::string, int>::const_iterator iter = | 1103 std::map<std::string, int>::const_iterator iter = |
| 1078 blacklisted_urls_.find(*instant_url); | 1104 blacklisted_urls_.find(*instant_url); |
| 1079 if (iter != blacklisted_urls_.end() && | 1105 if (iter != blacklisted_urls_.end() && |
| 1080 iter->second > kMaxInstantSupportFailures) | 1106 iter->second > kMaxInstantSupportFailures) |
| 1081 return false; | 1107 return false; |
| 1082 | 1108 |
| 1083 return true; | 1109 return true; |
| 1084 } | 1110 } |
| OLD | NEW |