| 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 // static |
| 150 const char* InstantController::kLocalOmniboxPopupURL = |
| 151 "chrome://local-omnibox-popup/local-omnibox-popup.html"; |
| 152 |
| 149 InstantController::InstantController(chrome::BrowserInstantController* browser, | 153 InstantController::InstantController(chrome::BrowserInstantController* browser, |
| 150 bool extended_enabled) | 154 bool extended_enabled, |
| 155 bool use_local_preview_only) |
| 151 : browser_(browser), | 156 : browser_(browser), |
| 152 extended_enabled_(extended_enabled), | 157 extended_enabled_(extended_enabled), |
| 153 instant_enabled_(false), | 158 instant_enabled_(false), |
| 159 use_local_preview_only_(use_local_preview_only), |
| 154 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 160 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 155 last_omnibox_text_has_inline_autocompletion_(false), | 161 last_omnibox_text_has_inline_autocompletion_(false), |
| 156 last_verbatim_(false), | 162 last_verbatim_(false), |
| 157 last_transition_type_(content::PAGE_TRANSITION_LINK), | 163 last_transition_type_(content::PAGE_TRANSITION_LINK), |
| 158 last_match_was_search_(false), | 164 last_match_was_search_(false), |
| 159 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), | 165 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), |
| 160 allow_preview_to_show_search_suggestions_(false) { | 166 allow_preview_to_show_search_suggestions_(false) { |
| 161 } | 167 } |
| 162 | 168 |
| 163 InstantController::~InstantController() { | 169 InstantController::~InstantController() { |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 instant_tab_->Submit(last_omnibox_text_); | 451 instant_tab_->Submit(last_omnibox_text_); |
| 446 instant_tab_->contents()->Focus(); | 452 instant_tab_->contents()->Focus(); |
| 447 return true; | 453 return true; |
| 448 } | 454 } |
| 449 return false; | 455 return false; |
| 450 } | 456 } |
| 451 | 457 |
| 452 if (!IsPreviewingSearchResults() && type != INSTANT_COMMIT_NAVIGATED) | 458 if (!IsPreviewingSearchResults() && type != INSTANT_COMMIT_NAVIGATED) |
| 453 return false; | 459 return false; |
| 454 | 460 |
| 461 // Never commit the local omnibox. |
| 462 if (loader_->IsUsingLocalPreview()) |
| 463 return false; |
| 464 |
| 455 if (type == INSTANT_COMMIT_FOCUS_LOST) | 465 if (type == INSTANT_COMMIT_FOCUS_LOST) |
| 456 loader_->Cancel(last_omnibox_text_); | 466 loader_->Cancel(last_omnibox_text_); |
| 457 else if (type != INSTANT_COMMIT_NAVIGATED && | 467 else if (type != INSTANT_COMMIT_NAVIGATED && |
| 458 type != INSTANT_COMMIT_CLICKED_QUERY_SUGGESTION) | 468 type != INSTANT_COMMIT_CLICKED_QUERY_SUGGESTION) |
| 459 loader_->Submit(last_omnibox_text_); | 469 loader_->Submit(last_omnibox_text_); |
| 460 | 470 |
| 461 content::WebContents* preview = loader_->ReleaseContents(); | 471 content::WebContents* preview = loader_->ReleaseContents(); |
| 462 | 472 |
| 463 if (extended_enabled_) { | 473 if (extended_enabled_) { |
| 464 // Consider what's happening: | 474 // Consider what's happening: |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 844 void InstantController::NavigateToURL(const GURL& url, | 854 void InstantController::NavigateToURL(const GURL& url, |
| 845 content::PageTransition transition) { | 855 content::PageTransition transition) { |
| 846 if (!extended_enabled_) | 856 if (!extended_enabled_) |
| 847 return; | 857 return; |
| 848 if (loader_) | 858 if (loader_) |
| 849 HideLoader(); | 859 HideLoader(); |
| 850 browser_->OpenURLInCurrentTab(url, transition); | 860 browser_->OpenURLInCurrentTab(url, transition); |
| 851 } | 861 } |
| 852 | 862 |
| 853 bool InstantController::ResetLoader(const TemplateURL* template_url, | 863 bool InstantController::ResetLoader(const TemplateURL* template_url, |
| 854 const content::WebContents* active_tab) { | 864 const content::WebContents* active_tab, |
| 865 bool fallback_to_local) { |
| 855 std::string instant_url; | 866 std::string instant_url; |
| 856 if (!GetInstantURL(template_url, &instant_url)) | 867 if (!GetInstantURL(template_url, &instant_url)) { |
| 857 return false; | 868 if (!fallback_to_local || !extended_enabled_) |
| 869 return false; |
| 870 |
| 871 // If we are in extended mode, fallback to the local popup. |
| 872 instant_url = kLocalOmniboxPopupURL; |
| 873 } |
| 858 | 874 |
| 859 if (loader_ && loader_->instant_url() == instant_url) | 875 if (loader_ && loader_->instant_url() == instant_url) |
| 860 return true; | 876 return true; |
| 861 | 877 |
| 862 HideInternal(); | 878 HideInternal(); |
| 863 loader_.reset(new InstantLoader(this, instant_url)); | 879 loader_.reset(new InstantLoader(this, instant_url)); |
| 864 loader_->InitContents(active_tab); | 880 loader_->InitContents(active_tab); |
| 865 | 881 |
| 866 // Ensure the searchbox API has the correct initial state. | 882 // Ensure the searchbox API has the correct initial state. |
| 867 if (extended_enabled_) { | 883 if (extended_enabled_) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 882 bool InstantController::CreateDefaultLoader() { | 898 bool InstantController::CreateDefaultLoader() { |
| 883 // If there's no active tab, the browser is closing. | 899 // If there's no active tab, the browser is closing. |
| 884 const content::WebContents* active_tab = browser_->GetActiveWebContents(); | 900 const content::WebContents* active_tab = browser_->GetActiveWebContents(); |
| 885 if (!active_tab) | 901 if (!active_tab) |
| 886 return false; | 902 return false; |
| 887 | 903 |
| 888 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( | 904 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( |
| 889 Profile::FromBrowserContext(active_tab->GetBrowserContext()))-> | 905 Profile::FromBrowserContext(active_tab->GetBrowserContext()))-> |
| 890 GetDefaultSearchProvider(); | 906 GetDefaultSearchProvider(); |
| 891 | 907 |
| 892 return ResetLoader(template_url, active_tab); | 908 return ResetLoader(template_url, active_tab, true); |
| 893 } | 909 } |
| 894 | 910 |
| 895 void InstantController::OnStaleLoader() { | 911 void InstantController::OnStaleLoader() { |
| 912 // The local popup is never stale. |
| 913 if (loader_ && loader_->IsUsingLocalPreview()) |
| 914 return; |
| 915 |
| 896 // If the preview is showing or the omnibox has focus, don't delete the | 916 // If the preview is showing or the omnibox has focus, don't delete the |
| 897 // loader. It will get refreshed the next time the preview is hidden or the | 917 // loader. It will get refreshed the next time the preview is hidden or the |
| 898 // omnibox loses focus. | 918 // omnibox loses focus. |
| 899 if (!stale_loader_timer_.IsRunning() && | 919 if (!stale_loader_timer_.IsRunning() && |
| 900 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && | 920 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && |
| 901 model_.mode().is_default()) { | 921 model_.mode().is_default()) { |
| 902 loader_.reset(); | 922 loader_.reset(); |
| 903 CreateDefaultLoader(); | 923 CreateDefaultLoader(); |
| 904 } | 924 } |
| 905 } | 925 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 926 // of |match|, we shouldn't use the committed tab. | 946 // of |match|, we shouldn't use the committed tab. |
| 927 if (instant_tab_) | 947 if (instant_tab_) |
| 928 return true; | 948 return true; |
| 929 | 949 |
| 930 // If there's no active tab, the browser is closing. | 950 // If there's no active tab, the browser is closing. |
| 931 const content::WebContents* active_tab = browser_->GetActiveWebContents(); | 951 const content::WebContents* active_tab = browser_->GetActiveWebContents(); |
| 932 if (!active_tab) | 952 if (!active_tab) |
| 933 return false; | 953 return false; |
| 934 | 954 |
| 935 // Try to create a loader for the instant_url in the TemplateURL of |match|. | 955 // Try to create a loader for the instant_url in the TemplateURL of |match|. |
| 956 // Do not fallback to the local preview because if the keyword specific |
| 957 // instant URL fails, we want to first try the default instant URL which |
| 958 // happens in the CreateDefaultLoader call below. |
| 936 const TemplateURL* template_url = match.GetTemplateURL( | 959 const TemplateURL* template_url = match.GetTemplateURL( |
| 937 Profile::FromBrowserContext(active_tab->GetBrowserContext()), false); | 960 Profile::FromBrowserContext(active_tab->GetBrowserContext()), false); |
| 938 if (ResetLoader(template_url, active_tab)) | 961 if (ResetLoader(template_url, active_tab, false)) |
| 939 return true; | 962 return true; |
| 940 | 963 |
| 941 // In non-extended mode, stop if we couldn't get a loader for the |match|. | 964 // In non-extended mode, stop if we couldn't get a loader for the |match|. |
| 942 if (!extended_enabled_) | 965 if (!extended_enabled_) |
| 943 return false; | 966 return false; |
| 944 | 967 |
| 945 // If the match is a query, it is for a non-Instant search engine; stop. | 968 // If the match is a query, it is for a non-Instant search engine; stop. |
| 946 if (last_match_was_search_) | 969 if (last_match_was_search_) |
| 947 return false; | 970 return false; |
| 948 | 971 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1002 } | 1025 } |
| 1003 | 1026 |
| 1004 // If the preview is being shown for the first time since the user started | 1027 // If the preview is being shown for the first time since the user started |
| 1005 // typing, record a histogram value. | 1028 // typing, record a histogram value. |
| 1006 if (!first_interaction_time_.is_null() && model_.mode().is_default()) { | 1029 if (!first_interaction_time_.is_null() && model_.mode().is_default()) { |
| 1007 base::TimeDelta delta = base::Time::Now() - first_interaction_time_; | 1030 base::TimeDelta delta = base::Time::Now() - first_interaction_time_; |
| 1008 UMA_HISTOGRAM_TIMES("Instant.TimeToFirstShow", delta); | 1031 UMA_HISTOGRAM_TIMES("Instant.TimeToFirstShow", delta); |
| 1009 } | 1032 } |
| 1010 | 1033 |
| 1011 // Show at 100% height except in the following cases: | 1034 // Show at 100% height except in the following cases: |
| 1035 // - The local omnibox popup is being loaded. |
| 1012 // - Instant is disabled. The page needs to be able to show only a dropdown. | 1036 // - Instant is disabled. The page needs to be able to show only a dropdown. |
| 1013 // - The page wants to show custom NTP content. | 1037 // - The page wants to show custom NTP content. |
| 1014 // - The page is over a website other than search or an NTP, and is not | 1038 // - The page is over a website other than search or an NTP, and is not |
| 1015 // already showing at 100% height. | 1039 // already showing at 100% height. |
| 1016 if (!instant_enabled_ || reason == INSTANT_SHOWN_CUSTOM_NTP_CONTENT || | 1040 if (loader_->IsUsingLocalPreview() || !instant_enabled_ || |
| 1041 reason == INSTANT_SHOWN_CUSTOM_NTP_CONTENT || |
| 1017 (search_mode_.is_origin_default() && !IsFullHeight(model_))) | 1042 (search_mode_.is_origin_default() && !IsFullHeight(model_))) |
| 1018 model_.SetPreviewState(search_mode_, height, units); | 1043 model_.SetPreviewState(search_mode_, height, units); |
| 1019 else | 1044 else |
| 1020 model_.SetPreviewState(search_mode_, 100, INSTANT_SIZE_PERCENT); | 1045 model_.SetPreviewState(search_mode_, 100, INSTANT_SIZE_PERCENT); |
| 1021 | 1046 |
| 1022 // If the user clicked on a query suggestion, also go ahead and commit the | 1047 // If the user clicked on a query suggestion, also go ahead and commit the |
| 1023 // overlay. This is necessary because if the overlay was partially visible | 1048 // overlay. This is necessary because if the overlay was partially visible |
| 1024 // when the suggestion was clicked, the click itself would not commit the | 1049 // when the suggestion was clicked, the click itself would not commit the |
| 1025 // overlay (because we're not full height). | 1050 // overlay (because we're not full height). |
| 1026 if (reason == INSTANT_SHOWN_CLICKED_QUERY_SUGGESTION) | 1051 if (reason == INSTANT_SHOWN_CLICKED_QUERY_SUGGESTION) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1049 // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds). | 1074 // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds). |
| 1050 DCHECK_LE(0, intersection.x()); | 1075 DCHECK_LE(0, intersection.x()); |
| 1051 DCHECK_LE(0, intersection.width()); | 1076 DCHECK_LE(0, intersection.width()); |
| 1052 DCHECK_LE(0, intersection.height()); | 1077 DCHECK_LE(0, intersection.height()); |
| 1053 | 1078 |
| 1054 loader_->SetOmniboxBounds(intersection); | 1079 loader_->SetOmniboxBounds(intersection); |
| 1055 } | 1080 } |
| 1056 | 1081 |
| 1057 bool InstantController::GetInstantURL(const TemplateURL* template_url, | 1082 bool InstantController::GetInstantURL(const TemplateURL* template_url, |
| 1058 std::string* instant_url) const { | 1083 std::string* instant_url) const { |
| 1084 if (extended_enabled_ && use_local_preview_only_) { |
| 1085 *instant_url = kLocalOmniboxPopupURL; |
| 1086 return true; |
| 1087 } |
| 1088 |
| 1059 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 1089 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1060 if (command_line->HasSwitch(switches::kInstantURL)) { | 1090 if (command_line->HasSwitch(switches::kInstantURL)) { |
| 1061 *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL); | 1091 *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL); |
| 1062 return template_url != NULL; | 1092 return template_url != NULL; |
| 1063 } | 1093 } |
| 1064 | 1094 |
| 1065 if (!template_url) | 1095 if (!template_url) |
| 1066 return false; | 1096 return false; |
| 1067 | 1097 |
| 1068 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); | 1098 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1098 } | 1128 } |
| 1099 | 1129 |
| 1100 std::map<std::string, int>::const_iterator iter = | 1130 std::map<std::string, int>::const_iterator iter = |
| 1101 blacklisted_urls_.find(*instant_url); | 1131 blacklisted_urls_.find(*instant_url); |
| 1102 if (iter != blacklisted_urls_.end() && | 1132 if (iter != blacklisted_urls_.end() && |
| 1103 iter->second > kMaxInstantSupportFailures) | 1133 iter->second > kMaxInstantSupportFailures) |
| 1104 return false; | 1134 return false; |
| 1105 | 1135 |
| 1106 return true; | 1136 return true; |
| 1107 } | 1137 } |
| OLD | NEW |