| 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 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 // this, but it's not called in non-extended mode, so fake it. | 364 // this, but it's not called in non-extended mode, so fake it. |
| 365 if (!extended_enabled_) | 365 if (!extended_enabled_) |
| 366 search_mode_.mode = chrome::search::Mode::MODE_SEARCH_SUGGESTIONS; | 366 search_mode_.mode = chrome::search::Mode::MODE_SEARCH_SUGGESTIONS; |
| 367 | 367 |
| 368 if (instant_tab_) { | 368 if (instant_tab_) { |
| 369 instant_tab_->Update(user_text, selection_start, selection_end, verbatim); | 369 instant_tab_->Update(user_text, selection_start, selection_end, verbatim); |
| 370 } else { | 370 } else { |
| 371 if (first_interaction_time_.is_null()) | 371 if (first_interaction_time_.is_null()) |
| 372 first_interaction_time_ = base::Time::Now(); | 372 first_interaction_time_ = base::Time::Now(); |
| 373 allow_preview_to_show_search_suggestions_ = true; | 373 allow_preview_to_show_search_suggestions_ = true; |
| 374 |
| 375 // For extended mode, if the loader is not ready at this point, switch over |
| 376 // to a backup loader. |
| 377 if (extended_enabled_ && !loader_->supports_instant() && |
| 378 !loader_->IsUsingLocalPreview() && browser_->GetActiveWebContents()) { |
| 379 CreateLoader(kLocalOmniboxPopupURL, browser_->GetActiveWebContents()); |
| 380 loader_->set_is_in_fallback_mode(true); |
| 381 } |
| 382 |
| 374 loader_->Update(extended_enabled_ ? user_text : full_text, | 383 loader_->Update(extended_enabled_ ? user_text : full_text, |
| 375 selection_start, selection_end, verbatim); | 384 selection_start, selection_end, verbatim); |
| 376 } | 385 } |
| 377 | 386 |
| 378 content::NotificationService::current()->Notify( | 387 content::NotificationService::current()->Notify( |
| 379 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, | 388 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, |
| 380 content::Source<InstantController>(this), | 389 content::Source<InstantController>(this), |
| 381 content::NotificationService::NoDetails()); | 390 content::NotificationService::NoDetails()); |
| 382 | 391 |
| 383 // We don't have new suggestions yet, but we can either reuse the existing | 392 // We don't have new suggestions yet, but we can either reuse the existing |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 914 | 923 |
| 915 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { | 924 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { |
| 916 // If the preview is showing custom NTP content, don't hide it, commit it | 925 // If the preview is showing custom NTP content, don't hide it, commit it |
| 917 // (no matter where the user clicked) or try to recreate it. | 926 // (no matter where the user clicked) or try to recreate it. |
| 918 if (model_.mode().is_ntp()) | 927 if (model_.mode().is_ntp()) |
| 919 return; | 928 return; |
| 920 | 929 |
| 921 // If the preview is not showing at all, recreate it if it's stale. | 930 // If the preview is not showing at all, recreate it if it's stale. |
| 922 if (model_.mode().is_default()) { | 931 if (model_.mode().is_default()) { |
| 923 OnStaleLoader(); | 932 OnStaleLoader(); |
| 933 MaybeSwitchToRemoteLoader(); |
| 924 return; | 934 return; |
| 925 } | 935 } |
| 926 | 936 |
| 927 // The preview is showing search suggestions. If GetPreviewContents() is NULL, | 937 // The preview is showing search suggestions. If GetPreviewContents() is NULL, |
| 928 // we are in the commit path. Don't do anything. | 938 // we are in the commit path. Don't do anything. |
| 929 if (!GetPreviewContents()) | 939 if (!GetPreviewContents()) |
| 930 return; | 940 return; |
| 931 | 941 |
| 932 #if defined(OS_MACOSX) | 942 #if defined(OS_MACOSX) |
| 933 // TODO(sreeram): See if Mac really needs this special treatment. | 943 // TODO(sreeram): See if Mac really needs this special treatment. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 954 bool InstantController::EnsureLoaderIsCurrent() { | 964 bool InstantController::EnsureLoaderIsCurrent() { |
| 955 // If we are on a search results page, we'll use that instead of a loader. | 965 // If we are on a search results page, we'll use that instead of a loader. |
| 956 if (instant_tab_) | 966 if (instant_tab_) |
| 957 return true; | 967 return true; |
| 958 | 968 |
| 959 // If there's no active tab, the browser is closing. | 969 // If there's no active tab, the browser is closing. |
| 960 content::WebContents* active_tab = browser_->GetActiveWebContents(); | 970 content::WebContents* active_tab = browser_->GetActiveWebContents(); |
| 961 if (!active_tab) | 971 if (!active_tab) |
| 962 return false; | 972 return false; |
| 963 | 973 |
| 974 // In fallback mode, we do not change the loader. |
| 975 if (loader_ && loader_->is_in_fallback_mode()) |
| 976 return true; |
| 977 |
| 964 std::string instant_url; | 978 std::string instant_url; |
| 965 if (!GetInstantURL(active_tab, &instant_url)) { | 979 if (!GetInstantURL(active_tab, &instant_url)) { |
| 966 // If we are in extended mode, fallback to the local popup. | 980 // If we are in extended mode, fallback to the local popup. |
| 967 if (extended_enabled_) | 981 if (extended_enabled_) |
| 968 instant_url = kLocalOmniboxPopupURL; | 982 instant_url = kLocalOmniboxPopupURL; |
| 969 } | 983 } |
| 970 | 984 |
| 971 if (!loader_ || loader_->instant_url() != instant_url) | 985 if (!loader_ || loader_->instant_url() != instant_url) |
| 972 CreateLoader(instant_url, active_tab); | 986 CreateLoader(instant_url, active_tab); |
| 973 | 987 |
| 974 return true; | 988 return true; |
| 975 } | 989 } |
| 976 | 990 |
| 977 void InstantController::CreateLoader(const std::string& instant_url, | 991 void InstantController::CreateLoader(const std::string& instant_url, |
| 978 content::WebContents* active_tab) { | 992 content::WebContents* active_tab) { |
| 979 HideInternal(); | 993 HideInternal(); |
| 980 loader_.reset(new InstantLoader(this, instant_url)); | 994 loader_.reset(new InstantLoader(this, instant_url)); |
| 981 loader_->InitContents(active_tab); | 995 loader_->InitContents(active_tab); |
| 982 | 996 |
| 983 // Ensure the searchbox API has the correct initial state. | 997 // Ensure the searchbox API has the correct initial state. |
| 984 if (extended_enabled_) { | 998 if (extended_enabled_) { |
| 985 browser_->UpdateThemeInfoForPreview(); | 999 browser_->UpdateThemeInfoForPreview(); |
| 986 loader_->SetDisplayInstantResults(instant_enabled_); | 1000 loader_->SetDisplayInstantResults(instant_enabled_); |
| 987 loader_->SearchModeChanged(search_mode_); | 1001 loader_->SearchModeChanged(search_mode_); |
| 988 loader_->KeyCaptureChanged(omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); | 1002 loader_->KeyCaptureChanged(omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); |
| 989 loader_->SetMarginSize(start_margin_, end_margin_); | 1003 loader_->SetMarginSize(start_margin_, end_margin_); |
| 1004 loader_->InitializeFonts(); |
| 990 } | 1005 } |
| 991 | 1006 |
| 992 // Restart the stale loader timer. | 1007 // Restart the stale loader timer. |
| 993 stale_loader_timer_.Start(FROM_HERE, | 1008 stale_loader_timer_.Start(FROM_HERE, |
| 994 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, | 1009 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, |
| 995 &InstantController::OnStaleLoader); | 1010 &InstantController::OnStaleLoader); |
| 996 } | 1011 } |
| 997 | 1012 |
| 998 void InstantController::OnStaleLoader() { | 1013 void InstantController::OnStaleLoader() { |
| 999 // The local popup is never stale. | 1014 // The local popup is never stale. |
| 1000 if (loader_ && loader_->IsUsingLocalPreview()) | 1015 if (loader_ && loader_->IsUsingLocalPreview()) |
| 1001 return; | 1016 return; |
| 1002 | 1017 |
| 1003 // If the preview is showing or the omnibox has focus, don't delete the | 1018 // If the preview is showing or the omnibox has focus, don't delete the |
| 1004 // loader. It will get refreshed the next time the preview is hidden or the | 1019 // loader. It will get refreshed the next time the preview is hidden or the |
| 1005 // omnibox loses focus. | 1020 // omnibox loses focus. |
| 1006 if (!stale_loader_timer_.IsRunning() && | 1021 if (!stale_loader_timer_.IsRunning() && |
| 1007 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && | 1022 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && |
| 1008 model_.mode().is_default()) { | 1023 model_.mode().is_default()) { |
| 1009 loader_.reset(); | 1024 loader_.reset(); |
| 1010 EnsureLoaderIsCurrent(); | 1025 EnsureLoaderIsCurrent(); |
| 1011 } | 1026 } |
| 1012 } | 1027 } |
| 1013 | 1028 |
| 1029 void InstantController::MaybeSwitchToRemoteLoader() { |
| 1030 if (!loader_ || !loader_->is_in_fallback_mode() || |
| 1031 omnibox_focus_state_ != OMNIBOX_FOCUS_NONE || |
| 1032 !model_.mode().is_default()) { |
| 1033 return; |
| 1034 } |
| 1035 |
| 1036 loader_->set_is_in_fallback_mode(false); |
| 1037 EnsureLoaderIsCurrent(); |
| 1038 } |
| 1039 |
| 1014 void InstantController::ResetInstantTab() { | 1040 void InstantController::ResetInstantTab() { |
| 1015 // Do not wire up the InstantTab if instant should only use local previews, to | 1041 // Do not wire up the InstantTab if instant should only use local previews, to |
| 1016 // prevent it from sending data to the page. | 1042 // prevent it from sending data to the page. |
| 1017 if (search_mode_.is_origin_search() && !use_local_preview_only_) { | 1043 if (search_mode_.is_origin_search() && !use_local_preview_only_) { |
| 1018 content::WebContents* active_tab = browser_->GetActiveWebContents(); | 1044 content::WebContents* active_tab = browser_->GetActiveWebContents(); |
| 1019 if (!instant_tab_ || active_tab != instant_tab_->contents()) { | 1045 if (!instant_tab_ || active_tab != instant_tab_->contents()) { |
| 1020 instant_tab_.reset(new InstantTab(this, active_tab)); | 1046 instant_tab_.reset(new InstantTab(this, active_tab)); |
| 1021 instant_tab_->Init(); | 1047 instant_tab_->Init(); |
| 1022 instant_tab_->SetDisplayInstantResults(instant_enabled_); | 1048 instant_tab_->SetDisplayInstantResults(instant_enabled_); |
| 1023 instant_tab_->SetMarginSize(start_margin_, end_margin_); | 1049 instant_tab_->SetMarginSize(start_margin_, end_margin_); |
| 1050 instant_tab_->InitializeFonts(); |
| 1024 } | 1051 } |
| 1025 | 1052 |
| 1026 // Hide the |loader_| since we are now using |instant_tab_| instead. | 1053 // Hide the |loader_| since we are now using |instant_tab_| instead. |
| 1027 HideLoader(); | 1054 HideLoader(); |
| 1028 } else { | 1055 } else { |
| 1029 instant_tab_.reset(); | 1056 instant_tab_.reset(); |
| 1030 } | 1057 } |
| 1031 } | 1058 } |
| 1032 | 1059 |
| 1033 void InstantController::HideLoader() { | 1060 void InstantController::HideLoader() { |
| 1034 HideInternal(); | 1061 HideInternal(); |
| 1035 OnStaleLoader(); | 1062 OnStaleLoader(); |
| 1063 MaybeSwitchToRemoteLoader(); |
| 1036 } | 1064 } |
| 1037 | 1065 |
| 1038 void InstantController::HideInternal() { | 1066 void InstantController::HideInternal() { |
| 1039 DVLOG(1) << "Hide"; | 1067 DVLOG(1) << "Hide"; |
| 1040 | 1068 |
| 1041 // If GetPreviewContents() returns NULL, either we're already in the desired | 1069 // If GetPreviewContents() returns NULL, either we're already in the desired |
| 1042 // MODE_DEFAULT state, or we're in the commit path. For the latter, don't | 1070 // MODE_DEFAULT state, or we're in the commit path. For the latter, don't |
| 1043 // change the state just yet; else we may hide the preview unnecessarily. | 1071 // change the state just yet; else we may hide the preview unnecessarily. |
| 1044 // Instead, the state will be set correctly after the commit is done. | 1072 // Instead, the state will be set correctly after the commit is done. |
| 1045 if (GetPreviewContents()) { | 1073 if (GetPreviewContents()) { |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1192 std::map<std::string, int>::const_iterator iter = | 1220 std::map<std::string, int>::const_iterator iter = |
| 1193 blacklisted_urls_.find(*instant_url); | 1221 blacklisted_urls_.find(*instant_url); |
| 1194 if (iter != blacklisted_urls_.end() && | 1222 if (iter != blacklisted_urls_.end() && |
| 1195 iter->second > kMaxInstantSupportFailures) { | 1223 iter->second > kMaxInstantSupportFailures) { |
| 1196 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); | 1224 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); |
| 1197 return false; | 1225 return false; |
| 1198 } | 1226 } |
| 1199 | 1227 |
| 1200 return true; | 1228 return true; |
| 1201 } | 1229 } |
| OLD | NEW |