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 bool is_in_fallback_mode = loader_ && loader_->is_in_fallback_mode(); |
265 if (!instant_tab_ && !EnsureLoaderIsCurrent()) { | 265 |
| 266 // If we have an |instant_tab_| use it, else ensure we have a loader that is |
| 267 // current or in fallback mode. |
| 268 if (!instant_tab_ && !is_in_fallback_mode && !EnsureLoaderIsCurrent()) { |
266 HideLoader(); | 269 HideLoader(); |
267 return false; | 270 return false; |
268 } | 271 } |
269 | 272 |
270 if (extended_enabled_) { | 273 if (extended_enabled_) { |
271 if (!omnibox_popup_is_open) { | 274 if (!omnibox_popup_is_open) { |
272 if (!user_input_in_progress) { | 275 if (!user_input_in_progress) { |
273 // If the user isn't typing and the omnibox popup is closed, it means a | 276 // 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. | 277 // regular navigation, tab-switch or the user hitting Escape. |
275 if (instant_tab_) { | 278 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. | 379 // this, but it's not called in non-extended mode, so fake it. |
377 if (!extended_enabled_) | 380 if (!extended_enabled_) |
378 search_mode_.mode = chrome::search::Mode::MODE_SEARCH_SUGGESTIONS; | 381 search_mode_.mode = chrome::search::Mode::MODE_SEARCH_SUGGESTIONS; |
379 | 382 |
380 if (instant_tab_) { | 383 if (instant_tab_) { |
381 instant_tab_->Update(user_text, selection_start, selection_end, verbatim); | 384 instant_tab_->Update(user_text, selection_start, selection_end, verbatim); |
382 } else { | 385 } else { |
383 if (first_interaction_time_.is_null()) | 386 if (first_interaction_time_.is_null()) |
384 first_interaction_time_ = base::Time::Now(); | 387 first_interaction_time_ = base::Time::Now(); |
385 allow_preview_to_show_search_suggestions_ = true; | 388 allow_preview_to_show_search_suggestions_ = true; |
| 389 |
| 390 // For extended mode, if the loader is not ready at this point, switch over |
| 391 // to a backup loader. |
| 392 if (extended_enabled_ && !loader_->supports_instant() && |
| 393 !loader_->IsUsingLocalPreview() && browser_->GetActiveWebContents()) { |
| 394 CreateLoader(kLocalOmniboxPopupURL, browser_->GetActiveWebContents()); |
| 395 loader_->set_is_in_fallback_mode(); |
| 396 } |
| 397 |
386 loader_->Update(extended_enabled_ ? user_text : full_text, | 398 loader_->Update(extended_enabled_ ? user_text : full_text, |
387 selection_start, selection_end, verbatim); | 399 selection_start, selection_end, verbatim); |
388 } | 400 } |
389 | 401 |
390 content::NotificationService::current()->Notify( | 402 content::NotificationService::current()->Notify( |
391 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, | 403 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, |
392 content::Source<InstantController>(this), | 404 content::Source<InstantController>(this), |
393 content::NotificationService::NoDetails()); | 405 content::NotificationService::NoDetails()); |
394 | 406 |
395 // We don't have new suggestions yet, but we can either reuse the existing | 407 // We don't have new suggestions yet, but we can either reuse the existing |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 | 957 |
946 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { | 958 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { |
947 // If the preview is showing custom NTP content, don't hide it, commit it | 959 // 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. | 960 // (no matter where the user clicked) or try to recreate it. |
949 if (model_.mode().is_ntp()) | 961 if (model_.mode().is_ntp()) |
950 return; | 962 return; |
951 | 963 |
952 // If the preview is not showing at all, recreate it if it's stale. | 964 // If the preview is not showing at all, recreate it if it's stale. |
953 if (model_.mode().is_default()) { | 965 if (model_.mode().is_default()) { |
954 OnStaleLoader(); | 966 OnStaleLoader(); |
| 967 MaybeSwitchToRemoteLoader(); |
955 return; | 968 return; |
956 } | 969 } |
957 | 970 |
958 // The preview is showing search suggestions. If GetPreviewContents() is NULL, | 971 // The preview is showing search suggestions. If GetPreviewContents() is NULL, |
959 // we are in the commit path. Don't do anything. | 972 // we are in the commit path. Don't do anything. |
960 if (!GetPreviewContents()) | 973 if (!GetPreviewContents()) |
961 return; | 974 return; |
962 | 975 |
963 #if defined(OS_MACOSX) | 976 #if defined(OS_MACOSX) |
964 // TODO(sreeram): See if Mac really needs this special treatment. | 977 // TODO(sreeram): See if Mac really needs this special treatment. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1011 } | 1024 } |
1012 | 1025 |
1013 if (!loader_ || loader_->instant_url() != instant_url) | 1026 if (!loader_ || loader_->instant_url() != instant_url) |
1014 CreateLoader(instant_url, active_tab); | 1027 CreateLoader(instant_url, active_tab); |
1015 | 1028 |
1016 return true; | 1029 return true; |
1017 } | 1030 } |
1018 | 1031 |
1019 void InstantController::CreateLoader(const std::string& instant_url, | 1032 void InstantController::CreateLoader(const std::string& instant_url, |
1020 const content::WebContents* active_tab) { | 1033 const content::WebContents* active_tab) { |
| 1034 // Update theme info so that the loader picks up the correct fonts. |
| 1035 if (extended_enabled_) |
| 1036 browser_->UpdateThemeInfoForPreview(); |
| 1037 |
1021 HideInternal(); | 1038 HideInternal(); |
1022 loader_.reset(new InstantLoader(this, instant_url)); | 1039 loader_.reset(new InstantLoader(this, instant_url)); |
1023 loader_->InitContents(active_tab); | 1040 loader_->InitContents(active_tab); |
1024 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | 1041 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( |
1025 "CreateLoader: instant_url='%s'", instant_url.c_str())); | 1042 "CreateLoader: instant_url='%s'", instant_url.c_str())); |
1026 | 1043 |
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. | 1044 // Restart the stale loader timer. |
1037 stale_loader_timer_.Start(FROM_HERE, | 1045 stale_loader_timer_.Start(FROM_HERE, |
1038 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, | 1046 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, |
1039 &InstantController::OnStaleLoader); | 1047 &InstantController::OnStaleLoader); |
1040 } | 1048 } |
1041 | 1049 |
1042 void InstantController::OnStaleLoader() { | 1050 void InstantController::OnStaleLoader() { |
1043 // The local popup is never stale. | 1051 // The local popup is never stale. |
1044 if (loader_ && loader_->IsUsingLocalPreview()) | 1052 if (loader_ && loader_->IsUsingLocalPreview()) |
1045 return; | 1053 return; |
1046 | 1054 |
1047 // If the preview is showing or the omnibox has focus, don't delete the | 1055 // 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 | 1056 // loader. It will get refreshed the next time the preview is hidden or the |
1049 // omnibox loses focus. | 1057 // omnibox loses focus. |
1050 if (!stale_loader_timer_.IsRunning() && | 1058 if (!stale_loader_timer_.IsRunning() && |
1051 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && | 1059 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && |
1052 model_.mode().is_default()) { | 1060 model_.mode().is_default()) { |
1053 loader_.reset(); | 1061 loader_.reset(); |
1054 EnsureLoaderIsCurrent(); | 1062 EnsureLoaderIsCurrent(); |
1055 } | 1063 } |
1056 } | 1064 } |
1057 | 1065 |
| 1066 void InstantController::MaybeSwitchToRemoteLoader() { |
| 1067 if (!loader_ || !loader_->is_in_fallback_mode() || |
| 1068 omnibox_focus_state_ != OMNIBOX_FOCUS_NONE || |
| 1069 !model_.mode().is_default()) { |
| 1070 return; |
| 1071 } |
| 1072 |
| 1073 EnsureLoaderIsCurrent(); |
| 1074 } |
| 1075 |
1058 void InstantController::ResetInstantTab() { | 1076 void InstantController::ResetInstantTab() { |
1059 // Do not wire up the InstantTab if instant should only use local previews, to | 1077 // Do not wire up the InstantTab if instant should only use local previews, to |
1060 // prevent it from sending data to the page. | 1078 // prevent it from sending data to the page. |
1061 if (search_mode_.is_origin_search() && !use_local_preview_only_) { | 1079 if (search_mode_.is_origin_search() && !use_local_preview_only_) { |
1062 content::WebContents* active_tab = browser_->GetActiveWebContents(); | 1080 content::WebContents* active_tab = browser_->GetActiveWebContents(); |
1063 if (!instant_tab_ || active_tab != instant_tab_->contents()) { | 1081 if (!instant_tab_ || active_tab != instant_tab_->contents()) { |
1064 instant_tab_.reset(new InstantTab(this, active_tab)); | 1082 instant_tab_.reset(new InstantTab(this, active_tab)); |
1065 instant_tab_->Init(); | 1083 instant_tab_->Init(); |
1066 instant_tab_->SetDisplayInstantResults(instant_enabled_); | 1084 instant_tab_->SetDisplayInstantResults(instant_enabled_); |
1067 instant_tab_->SetMarginSize(start_margin_, end_margin_); | 1085 instant_tab_->SetMarginSize(start_margin_, end_margin_); |
| 1086 instant_tab_->InitializeFonts(); |
1068 } | 1087 } |
1069 | 1088 |
1070 // Hide the |loader_| since we are now using |instant_tab_| instead. | 1089 // Hide the |loader_| since we are now using |instant_tab_| instead. |
1071 HideLoader(); | 1090 HideLoader(); |
1072 } else { | 1091 } else { |
1073 instant_tab_.reset(); | 1092 instant_tab_.reset(); |
1074 } | 1093 } |
1075 } | 1094 } |
1076 | 1095 |
1077 void InstantController::HideLoader() { | 1096 void InstantController::HideLoader() { |
1078 HideInternal(); | 1097 HideInternal(); |
1079 OnStaleLoader(); | 1098 OnStaleLoader(); |
| 1099 MaybeSwitchToRemoteLoader(); |
1080 } | 1100 } |
1081 | 1101 |
1082 void InstantController::HideInternal() { | 1102 void InstantController::HideInternal() { |
1083 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); | 1103 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); |
1084 | 1104 |
1085 // If GetPreviewContents() returns NULL, either we're already in the desired | 1105 // 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 | 1106 // 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. | 1107 // 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. | 1108 // Instead, the state will be set correctly after the commit is done. |
1089 if (GetPreviewContents()) { | 1109 if (GetPreviewContents()) { |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1264 iter->second > kMaxInstantSupportFailures) { | 1284 iter->second > kMaxInstantSupportFailures) { |
1265 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); | 1285 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); |
1266 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | 1286 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( |
1267 "GetInstantURL: Instant URL blacklisted: url=%s", | 1287 "GetInstantURL: Instant URL blacklisted: url=%s", |
1268 instant_url->c_str())); | 1288 instant_url->c_str())); |
1269 return false; | 1289 return false; |
1270 } | 1290 } |
1271 | 1291 |
1272 return true; | 1292 return true; |
1273 } | 1293 } |
OLD | NEW |