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/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 !user_text.empty(); | 254 !user_text.empty(); |
255 | 255 |
256 // In non extended mode, Instant is disabled for URLs and keyword mode. | 256 // In non extended mode, Instant is disabled for URLs and keyword mode. |
257 if (!extended_enabled_ && | 257 if (!extended_enabled_ && |
258 (!last_match_was_search_ || | 258 (!last_match_was_search_ || |
259 match.type == AutocompleteMatch::SEARCH_OTHER_ENGINE)) { | 259 match.type == AutocompleteMatch::SEARCH_OTHER_ENGINE)) { |
260 HideLoader(); | 260 HideLoader(); |
261 return false; | 261 return false; |
262 } | 262 } |
263 | 263 |
264 // If we have an |instant_tab_| use it, else ensure we have a loader. | 264 // If we have an |instant_tab_| use it, else ensure we have a loader that is |
265 if (!instant_tab_ && !EnsureLoaderIsCurrent()) { | 265 // current or is using local preview. |
| 266 if (!instant_tab_ && !(loader_ && loader_->IsUsingLocalPreview()) && |
| 267 !EnsureLoaderIsCurrent()) { |
266 HideLoader(); | 268 HideLoader(); |
267 return false; | 269 return false; |
268 } | 270 } |
269 | 271 |
270 if (extended_enabled_) { | 272 if (extended_enabled_) { |
271 if (!omnibox_popup_is_open) { | 273 if (!omnibox_popup_is_open) { |
272 if (!user_input_in_progress) { | 274 if (!user_input_in_progress) { |
273 // If the user isn't typing and the omnibox popup is closed, it means a | 275 // If the user isn't typing and the omnibox popup is closed, it means a |
274 // regular navigation, tab-switch or the user hitting Escape. | 276 // regular navigation, tab-switch or the user hitting Escape. |
275 if (instant_tab_) { | 277 if (instant_tab_) { |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 // this, but it's not called in non-extended mode, so fake it. | 378 // this, but it's not called in non-extended mode, so fake it. |
377 if (!extended_enabled_) | 379 if (!extended_enabled_) |
378 search_mode_.mode = chrome::search::Mode::MODE_SEARCH_SUGGESTIONS; | 380 search_mode_.mode = chrome::search::Mode::MODE_SEARCH_SUGGESTIONS; |
379 | 381 |
380 if (instant_tab_) { | 382 if (instant_tab_) { |
381 instant_tab_->Update(user_text, selection_start, selection_end, verbatim); | 383 instant_tab_->Update(user_text, selection_start, selection_end, verbatim); |
382 } else { | 384 } else { |
383 if (first_interaction_time_.is_null()) | 385 if (first_interaction_time_.is_null()) |
384 first_interaction_time_ = base::Time::Now(); | 386 first_interaction_time_ = base::Time::Now(); |
385 allow_preview_to_show_search_suggestions_ = true; | 387 allow_preview_to_show_search_suggestions_ = true; |
| 388 |
| 389 // For extended mode, if the loader is not ready at this point, switch over |
| 390 // to a backup loader. |
| 391 if (extended_enabled_ && !loader_->supports_instant() && |
| 392 !loader_->IsUsingLocalPreview() && browser_->GetActiveWebContents()) { |
| 393 CreateLoader(kLocalOmniboxPopupURL, browser_->GetActiveWebContents()); |
| 394 } |
| 395 |
386 loader_->Update(extended_enabled_ ? user_text : full_text, | 396 loader_->Update(extended_enabled_ ? user_text : full_text, |
387 selection_start, selection_end, verbatim); | 397 selection_start, selection_end, verbatim); |
388 } | 398 } |
389 | 399 |
390 content::NotificationService::current()->Notify( | 400 content::NotificationService::current()->Notify( |
391 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, | 401 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, |
392 content::Source<InstantController>(this), | 402 content::Source<InstantController>(this), |
393 content::NotificationService::NoDetails()); | 403 content::NotificationService::NoDetails()); |
394 | 404 |
395 // We don't have new suggestions yet, but we can either reuse the existing | 405 // We don't have new suggestions yet, but we can either reuse the existing |
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
936 // - The page is not in NTP mode and we are navigating to a URL with a | 946 // - The page is not in NTP mode and we are navigating to a URL with a |
937 // different host or path than the instant URL. This enables the instant | 947 // different host or path than the instant URL. This enables the instant |
938 // page when it is showing search results to change the query parameters | 948 // page when it is showing search results to change the query parameters |
939 // and fragments of the URL without it navigating. | 949 // and fragments of the URL without it navigating. |
940 if (model_.mode().is_ntp() || | 950 if (model_.mode().is_ntp() || |
941 (url.host() != instant_url.host() || url.path() != instant_url.path())) { | 951 (url.host() != instant_url.host() || url.path() != instant_url.path())) { |
942 CommitIfPossible(INSTANT_COMMIT_NAVIGATED); | 952 CommitIfPossible(INSTANT_COMMIT_NAVIGATED); |
943 } | 953 } |
944 } | 954 } |
945 | 955 |
| 956 void InstantController::InstantLoaderRenderViewCreated() { |
| 957 if (!extended_enabled_) |
| 958 return; |
| 959 |
| 960 // Ensure the searchbox API has the correct initial state. |
| 961 loader_->SetDisplayInstantResults(instant_enabled_); |
| 962 loader_->SearchModeChanged(search_mode_); |
| 963 loader_->KeyCaptureChanged(omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); |
| 964 loader_->SetMarginSize(start_margin_, end_margin_); |
| 965 loader_->InitializeFonts(); |
| 966 } |
| 967 |
946 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { | 968 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { |
947 // If the preview is showing custom NTP content, don't hide it, commit it | 969 // If the preview is showing custom NTP content, don't hide it, commit it |
948 // (no matter where the user clicked) or try to recreate it. | 970 // (no matter where the user clicked) or try to recreate it. |
949 if (model_.mode().is_ntp()) | 971 if (model_.mode().is_ntp()) |
950 return; | 972 return; |
951 | 973 |
952 // If the preview is not showing at all, recreate it if it's stale. | 974 // If the preview is not showing at all, recreate it if it's stale. |
953 if (model_.mode().is_default()) { | 975 if (model_.mode().is_default()) { |
954 OnStaleLoader(); | 976 OnStaleLoader(); |
| 977 MaybeSwitchToRemoteLoader(); |
955 return; | 978 return; |
956 } | 979 } |
957 | 980 |
958 // The preview is showing search suggestions. If GetPreviewContents() is NULL, | 981 // The preview is showing search suggestions. If GetPreviewContents() is NULL, |
959 // we are in the commit path. Don't do anything. | 982 // we are in the commit path. Don't do anything. |
960 if (!GetPreviewContents()) | 983 if (!GetPreviewContents()) |
961 return; | 984 return; |
962 | 985 |
963 #if defined(OS_MACOSX) | 986 #if defined(OS_MACOSX) |
964 // TODO(sreeram): See if Mac really needs this special treatment. | 987 // TODO(sreeram): See if Mac really needs this special treatment. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1011 } | 1034 } |
1012 | 1035 |
1013 if (!loader_ || loader_->instant_url() != instant_url) | 1036 if (!loader_ || loader_->instant_url() != instant_url) |
1014 CreateLoader(instant_url, active_tab); | 1037 CreateLoader(instant_url, active_tab); |
1015 | 1038 |
1016 return true; | 1039 return true; |
1017 } | 1040 } |
1018 | 1041 |
1019 void InstantController::CreateLoader(const std::string& instant_url, | 1042 void InstantController::CreateLoader(const std::string& instant_url, |
1020 const content::WebContents* active_tab) { | 1043 const content::WebContents* active_tab) { |
| 1044 // Update theme info so that the loader picks up the correct fonts. |
| 1045 if (extended_enabled_) |
| 1046 browser_->UpdateThemeInfoForPreview(); |
| 1047 |
1021 HideInternal(); | 1048 HideInternal(); |
1022 loader_.reset(new InstantLoader(this, instant_url)); | 1049 loader_.reset(new InstantLoader(this, instant_url)); |
1023 loader_->InitContents(active_tab); | 1050 loader_->InitContents(active_tab); |
1024 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | 1051 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( |
1025 "CreateLoader: instant_url='%s'", instant_url.c_str())); | 1052 "CreateLoader: instant_url='%s'", instant_url.c_str())); |
1026 | 1053 |
1027 // Ensure the searchbox API has the correct initial state. | |
1028 if (extended_enabled_) { | |
1029 browser_->UpdateThemeInfoForPreview(); | |
1030 loader_->SetDisplayInstantResults(instant_enabled_); | |
1031 loader_->SearchModeChanged(search_mode_); | |
1032 loader_->KeyCaptureChanged(omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); | |
1033 loader_->SetMarginSize(start_margin_, end_margin_); | |
1034 } | |
1035 | |
1036 // Restart the stale loader timer. | 1054 // Restart the stale loader timer. |
1037 stale_loader_timer_.Start(FROM_HERE, | 1055 stale_loader_timer_.Start(FROM_HERE, |
1038 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, | 1056 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, |
1039 &InstantController::OnStaleLoader); | 1057 &InstantController::OnStaleLoader); |
1040 } | 1058 } |
1041 | 1059 |
1042 void InstantController::OnStaleLoader() { | 1060 void InstantController::OnStaleLoader() { |
1043 // The local popup is never stale. | 1061 // The local popup is never stale. |
1044 if (loader_ && loader_->IsUsingLocalPreview()) | 1062 if (loader_ && loader_->IsUsingLocalPreview()) |
1045 return; | 1063 return; |
1046 | 1064 |
1047 // If the preview is showing or the omnibox has focus, don't delete the | 1065 // If the preview is showing or the omnibox has focus, don't delete the |
1048 // loader. It will get refreshed the next time the preview is hidden or the | 1066 // loader. It will get refreshed the next time the preview is hidden or the |
1049 // omnibox loses focus. | 1067 // omnibox loses focus. |
1050 if (!stale_loader_timer_.IsRunning() && | 1068 if (!stale_loader_timer_.IsRunning() && |
1051 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && | 1069 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && |
1052 model_.mode().is_default()) { | 1070 model_.mode().is_default()) { |
1053 loader_.reset(); | 1071 loader_.reset(); |
1054 EnsureLoaderIsCurrent(); | 1072 EnsureLoaderIsCurrent(); |
1055 } | 1073 } |
1056 } | 1074 } |
1057 | 1075 |
| 1076 void InstantController::MaybeSwitchToRemoteLoader() { |
| 1077 if (!loader_ || omnibox_focus_state_ != OMNIBOX_FOCUS_NONE || |
| 1078 !model_.mode().is_default()) { |
| 1079 return; |
| 1080 } |
| 1081 |
| 1082 EnsureLoaderIsCurrent(); |
| 1083 } |
| 1084 |
1058 void InstantController::ResetInstantTab() { | 1085 void InstantController::ResetInstantTab() { |
1059 // Do not wire up the InstantTab if instant should only use local previews, to | 1086 // Do not wire up the InstantTab if instant should only use local previews, to |
1060 // prevent it from sending data to the page. | 1087 // prevent it from sending data to the page. |
1061 if (search_mode_.is_origin_search() && !use_local_preview_only_) { | 1088 if (search_mode_.is_origin_search() && !use_local_preview_only_) { |
1062 content::WebContents* active_tab = browser_->GetActiveWebContents(); | 1089 content::WebContents* active_tab = browser_->GetActiveWebContents(); |
1063 if (!instant_tab_ || active_tab != instant_tab_->contents()) { | 1090 if (!instant_tab_ || active_tab != instant_tab_->contents()) { |
1064 instant_tab_.reset(new InstantTab(this, active_tab)); | 1091 instant_tab_.reset(new InstantTab(this, active_tab)); |
1065 instant_tab_->Init(); | 1092 instant_tab_->Init(); |
1066 instant_tab_->SetDisplayInstantResults(instant_enabled_); | 1093 instant_tab_->SetDisplayInstantResults(instant_enabled_); |
1067 instant_tab_->SetMarginSize(start_margin_, end_margin_); | 1094 instant_tab_->SetMarginSize(start_margin_, end_margin_); |
| 1095 instant_tab_->InitializeFonts(); |
1068 } | 1096 } |
1069 | 1097 |
1070 // Hide the |loader_| since we are now using |instant_tab_| instead. | 1098 // Hide the |loader_| since we are now using |instant_tab_| instead. |
1071 HideLoader(); | 1099 HideLoader(); |
1072 } else { | 1100 } else { |
1073 instant_tab_.reset(); | 1101 instant_tab_.reset(); |
1074 } | 1102 } |
1075 } | 1103 } |
1076 | 1104 |
1077 void InstantController::HideLoader() { | 1105 void InstantController::HideLoader() { |
1078 HideInternal(); | 1106 HideInternal(); |
1079 OnStaleLoader(); | 1107 OnStaleLoader(); |
| 1108 MaybeSwitchToRemoteLoader(); |
1080 } | 1109 } |
1081 | 1110 |
1082 void InstantController::HideInternal() { | 1111 void InstantController::HideInternal() { |
1083 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); | 1112 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); |
1084 | 1113 |
1085 // If GetPreviewContents() returns NULL, either we're already in the desired | 1114 // If GetPreviewContents() returns NULL, either we're already in the desired |
1086 // MODE_DEFAULT state, or we're in the commit path. For the latter, don't | 1115 // MODE_DEFAULT state, or we're in the commit path. For the latter, don't |
1087 // change the state just yet; else we may hide the preview unnecessarily. | 1116 // change the state just yet; else we may hide the preview unnecessarily. |
1088 // Instead, the state will be set correctly after the commit is done. | 1117 // Instead, the state will be set correctly after the commit is done. |
1089 if (GetPreviewContents()) { | 1118 if (GetPreviewContents()) { |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1264 iter->second > kMaxInstantSupportFailures) { | 1293 iter->second > kMaxInstantSupportFailures) { |
1265 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); | 1294 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); |
1266 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | 1295 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( |
1267 "GetInstantURL: Instant URL blacklisted: url=%s", | 1296 "GetInstantURL: Instant URL blacklisted: url=%s", |
1268 instant_url->c_str())); | 1297 instant_url->c_str())); |
1269 return false; | 1298 return false; |
1270 } | 1299 } |
1271 | 1300 |
1272 return true; | 1301 return true; |
1273 } | 1302 } |
OLD | NEW |