Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 11 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 12 #include "chrome/browser/favicon/favicon_service_factory.h" | 12 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 13 #include "chrome/browser/history/history.h" | 13 #include "chrome/browser/history/history.h" |
| 14 #include "chrome/browser/history/history_service_factory.h" | 14 #include "chrome/browser/history/history_service_factory.h" |
| 15 #include "chrome/browser/history/history_tab_helper.h" | 15 #include "chrome/browser/history/history_tab_helper.h" |
| 16 #include "chrome/browser/instant/instant_controller_delegate.h" | 16 #include "chrome/browser/instant/instant_controller_delegate.h" |
| 17 #include "chrome/browser/instant/instant_loader.h" | 17 #include "chrome/browser/instant/instant_loader.h" |
| 18 #include "chrome/browser/platform_util.h" | 18 #include "chrome/browser/platform_util.h" |
| 19 #include "chrome/browser/prefs/pref_service.h" | 19 #include "chrome/browser/prefs/pref_service.h" |
| 20 #include "chrome/browser/search_engines/template_url_service.h" | 20 #include "chrome/browser/search_engines/template_url_service.h" |
| 21 #include "chrome/browser/search_engines/template_url_service_factory.h" | 21 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 22 #include "chrome/browser/ui/search/search.h" | |
| 22 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 23 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 23 #include "chrome/common/chrome_notification_types.h" | 24 #include "chrome/common/chrome_notification_types.h" |
| 24 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 25 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 26 #include "content/public/browser/favicon_status.h" | 27 #include "content/public/browser/favicon_status.h" |
| 27 #include "content/public/browser/navigation_entry.h" | 28 #include "content/public/browser/navigation_entry.h" |
| 28 #include "content/public/browser/notification_service.h" | 29 #include "content/public/browser/notification_service.h" |
| 29 #include "content/public/browser/render_widget_host_view.h" | 30 #include "content/public/browser/render_widget_host_view.h" |
| 30 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 31 #include "ui/gfx/codec/png_codec.h" | 32 #include "ui/gfx/codec/png_codec.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 53 // The maximum number of times we'll load a non-Instant-supporting search engine | 54 // The maximum number of times we'll load a non-Instant-supporting search engine |
| 54 // before we give up and blacklist it for the rest of the browsing session. | 55 // before we give up and blacklist it for the rest of the browsing session. |
| 55 const int kMaxInstantSupportFailures = 10; | 56 const int kMaxInstantSupportFailures = 10; |
| 56 | 57 |
| 57 // If an Instant page has not been used in these many milliseconds, it is | 58 // If an Instant page has not been used in these many milliseconds, it is |
| 58 // reloaded so that the page does not become stale. | 59 // reloaded so that the page does not become stale. |
| 59 const int kStaleLoaderTimeoutMS = 3 * 3600 * 1000; | 60 const int kStaleLoaderTimeoutMS = 3 * 3600 * 1000; |
| 60 | 61 |
| 61 std::string ModeToString(InstantController::Mode mode) { | 62 std::string ModeToString(InstantController::Mode mode) { |
| 62 switch (mode) { | 63 switch (mode) { |
| 64 case InstantController::EXTENDED: return "_Extended"; | |
| 63 case InstantController::INSTANT: return "_Instant"; | 65 case InstantController::INSTANT: return "_Instant"; |
| 64 case InstantController::SUGGEST: return "_Suggest"; | 66 case InstantController::SUGGEST: return "_Suggest"; |
| 65 case InstantController::HIDDEN: return "_Hidden"; | 67 case InstantController::HIDDEN: return "_Hidden"; |
| 66 case InstantController::SILENT: return "_Silent"; | 68 case InstantController::SILENT: return "_Silent"; |
| 67 case InstantController::EXTENDED: return "_Extended"; | 69 case InstantController::DISABLED: return "_Disabled"; |
| 68 } | 70 } |
| 69 | 71 |
| 70 NOTREACHED(); | 72 NOTREACHED(); |
| 71 return std::string(); | 73 return std::string(); |
| 72 } | 74 } |
| 73 | 75 |
| 74 void AddPreviewUsageForHistogram(InstantController::Mode mode, | 76 void AddPreviewUsageForHistogram(InstantController::Mode mode, |
| 75 PreviewUsageType usage) { | 77 PreviewUsageType usage) { |
| 76 DCHECK(0 <= usage && usage < PREVIEW_NUM_TYPES) << usage; | 78 DCHECK(0 <= usage && usage < PREVIEW_NUM_TYPES) << usage; |
| 77 base::Histogram* histogram = base::LinearHistogram::FactoryGet( | 79 base::Histogram* histogram = base::LinearHistogram::FactoryGet( |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 102 ++it1, ++it2) { | 104 ++it1, ++it2) { |
| 103 if (it1->first != it2->first || it1->second != it2->second) { | 105 if (it1->first != it2->first || it1->second != it2->second) { |
| 104 is_session_storage_the_same = false; | 106 is_session_storage_the_same = false; |
| 105 break; | 107 break; |
| 106 } | 108 } |
| 107 } | 109 } |
| 108 } | 110 } |
| 109 histogram->AddBoolean(is_session_storage_the_same); | 111 histogram->AddBoolean(is_session_storage_the_same); |
| 110 } | 112 } |
| 111 | 113 |
| 114 InstantController::Mode GetModeForProfile(Profile* profile) { | |
| 115 if (profile && !profile->IsOffTheRecord() && profile->GetPrefs() && | |
| 116 profile->GetPrefs()->GetBoolean(prefs::kInstantEnabled)) { | |
| 117 if (chrome::search::IsInstantExtendedAPIEnabled(profile)) | |
| 118 return InstantController::EXTENDED; | |
| 119 return InstantController::INSTANT; | |
| 120 } | |
| 121 return InstantController::DISABLED; | |
| 122 } | |
| 123 | |
| 124 | |
| 112 } // namespace | 125 } // namespace |
| 113 | 126 |
| 114 InstantController::InstantController(InstantControllerDelegate* delegate, | 127 // static |
| 115 Mode mode) | 128 bool InstantController::IsExtendedAPIEnabled(Profile* profile) { |
| 116 : delegate_(delegate), | 129 const Mode mode = GetModeForProfile(profile); |
|
sky
2012/08/30 22:33:13
nit: combine these two lines, eg return GetM... ==
sreeram
2012/08/30 23:23:51
Done.
| |
| 117 mode_(mode), | 130 return mode == EXTENDED; |
| 118 last_active_tab_(NULL), | |
| 119 last_verbatim_(false), | |
| 120 last_transition_type_(content::PAGE_TRANSITION_LINK), | |
| 121 is_showing_(false), | |
| 122 loader_processed_last_update_(false) { | |
| 123 } | |
| 124 | |
| 125 InstantController::~InstantController() { | |
| 126 if (GetPreviewContents()) | |
| 127 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); | |
| 128 } | 131 } |
| 129 | 132 |
| 130 // static | 133 // static |
| 134 bool InstantController::IsInstantEnabled(Profile* profile) { | |
| 135 const Mode mode = GetModeForProfile(profile); | |
| 136 return mode == EXTENDED || mode == INSTANT; | |
| 137 } | |
| 138 | |
| 139 // static | |
| 140 bool InstantController::IsSuggestEnabled(Profile* profile) { | |
| 141 const Mode mode = GetModeForProfile(profile); | |
| 142 return mode == EXTENDED || mode == INSTANT || mode == SUGGEST; | |
| 143 } | |
| 144 | |
| 145 // static | |
| 131 void InstantController::RegisterUserPrefs(PrefService* prefs) { | 146 void InstantController::RegisterUserPrefs(PrefService* prefs) { |
| 132 prefs->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, false, | 147 prefs->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, false, |
| 133 PrefService::SYNCABLE_PREF); | 148 PrefService::SYNCABLE_PREF); |
| 134 prefs->RegisterBooleanPref(prefs::kInstantEnabled, false, | 149 prefs->RegisterBooleanPref(prefs::kInstantEnabled, false, |
| 135 PrefService::SYNCABLE_PREF); | 150 PrefService::SYNCABLE_PREF); |
| 136 | 151 |
| 137 // TODO(jamescook): Move this to search controller. | 152 // TODO(jamescook): Move this to search controller. |
| 138 prefs->RegisterDoublePref(prefs::kInstantAnimationScaleFactor, | 153 prefs->RegisterDoublePref(prefs::kInstantAnimationScaleFactor, 1.0, |
| 139 1.0, | |
| 140 PrefService::UNSYNCABLE_PREF); | 154 PrefService::UNSYNCABLE_PREF); |
| 141 } | 155 } |
| 142 | 156 |
| 143 // static | 157 // static |
| 144 bool InstantController::IsEnabled(Profile* profile) { | 158 InstantController* InstantController::GetInstance( |
| 145 const PrefService* prefs = profile ? profile->GetPrefs() : NULL; | 159 Profile* profile, |
| 146 return prefs && prefs->GetBoolean(prefs::kInstantEnabled); | 160 InstantControllerDelegate* delegate) { |
| 161 const Mode mode = GetModeForProfile(profile); | |
| 162 return mode == DISABLED ? NULL : new InstantController(delegate, mode); | |
| 163 } | |
| 164 | |
| 165 InstantController::~InstantController() { | |
| 166 if (GetPreviewContents()) | |
| 167 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); | |
| 147 } | 168 } |
| 148 | 169 |
| 149 bool InstantController::Update(const AutocompleteMatch& match, | 170 bool InstantController::Update(const AutocompleteMatch& match, |
| 150 const string16& user_text, | 171 const string16& user_text, |
| 151 const string16& full_text, | 172 const string16& full_text, |
| 152 bool verbatim) { | 173 bool verbatim) { |
| 153 const TabContents* active_tab = delegate_->GetActiveTabContents(); | 174 const TabContents* active_tab = delegate_->GetActiveTabContents(); |
| 154 | 175 |
| 155 // We could get here with no active tab if the Browser is closing. | 176 // We could get here with no active tab if the Browser is closing. |
| 156 if (!active_tab) { | 177 if (!active_tab) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 175 ResetLoader(instant_url, active_tab); | 196 ResetLoader(instant_url, active_tab); |
| 176 last_active_tab_ = active_tab; | 197 last_active_tab_ = active_tab; |
| 177 | 198 |
| 178 // Track the non-Instant search URL for this query. | 199 // Track the non-Instant search URL for this query. |
| 179 url_for_history_ = match.destination_url; | 200 url_for_history_ = match.destination_url; |
| 180 last_transition_type_ = match.transition; | 201 last_transition_type_ = match.transition; |
| 181 | 202 |
| 182 // In EXTENDED mode, we send only |user_text| as the query text. In all other | 203 // In EXTENDED mode, we send only |user_text| as the query text. In all other |
| 183 // modes, we use the entire |full_text|. | 204 // modes, we use the entire |full_text|. |
| 184 const string16& query_text = mode_ == EXTENDED ? user_text : full_text; | 205 const string16& query_text = mode_ == EXTENDED ? user_text : full_text; |
| 185 string16 last_query_text = | 206 string16 last_query_text = mode_ == EXTENDED ? last_user_text_ : |
| 186 mode_ == EXTENDED ? last_user_text_ : last_full_text_; | 207 last_full_text_; |
| 187 last_user_text_ = user_text; | 208 last_user_text_ = user_text; |
| 188 last_full_text_ = full_text; | 209 last_full_text_ = full_text; |
| 189 | 210 |
| 190 // Don't send an update to the loader if the query text hasn't changed. | 211 // Don't send an update to the loader if the query text hasn't changed. |
| 191 if (query_text == last_query_text && verbatim == last_verbatim_) { | 212 if (query_text == last_query_text && verbatim == last_verbatim_) { |
| 192 // Reuse the last suggestion, as it's still valid. | 213 // Reuse the last suggestion, as it's still valid. |
| 193 delegate_->SetSuggestedText(last_suggestion_.text, | 214 delegate_->SetSuggestedText(last_suggestion_.text, |
| 194 last_suggestion_.behavior); | 215 last_suggestion_.behavior); |
| 195 | 216 |
| 196 // We need to call Show() here because of this: | 217 // We need to call Show() here because of this: |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 527 DCHECK_EQ(loader_.get(), loader); | 548 DCHECK_EQ(loader_.get(), loader); |
| 528 DCHECK(is_showing_ && !IsOutOfDate()) << is_showing_; | 549 DCHECK(is_showing_ && !IsOutOfDate()) << is_showing_; |
| 529 #if defined(USE_AURA) | 550 #if defined(USE_AURA) |
| 530 // On aura the omnibox only receives a focus lost if we initiate the focus | 551 // On aura the omnibox only receives a focus lost if we initiate the focus |
| 531 // change. This does that. | 552 // change. This does that. |
| 532 if (is_showing_ && !IsOutOfDate()) | 553 if (is_showing_ && !IsOutOfDate()) |
| 533 delegate_->InstantPreviewFocused(); | 554 delegate_->InstantPreviewFocused(); |
| 534 #endif | 555 #endif |
| 535 } | 556 } |
| 536 | 557 |
| 558 InstantController::InstantController(InstantControllerDelegate* delegate, | |
| 559 Mode mode) | |
| 560 : delegate_(delegate), | |
| 561 mode_(mode), | |
| 562 last_active_tab_(NULL), | |
| 563 last_verbatim_(false), | |
| 564 last_transition_type_(content::PAGE_TRANSITION_LINK), | |
| 565 is_showing_(false), | |
| 566 loader_processed_last_update_(false) { | |
| 567 } | |
| 568 | |
| 537 void InstantController::ResetLoader(const std::string& instant_url, | 569 void InstantController::ResetLoader(const std::string& instant_url, |
| 538 const TabContents* active_tab) { | 570 const TabContents* active_tab) { |
| 539 if (GetPreviewContents() && loader_->instant_url() != instant_url) | 571 if (GetPreviewContents() && loader_->instant_url() != instant_url) |
| 540 DeleteLoader(); | 572 DeleteLoader(); |
| 541 | 573 |
| 542 if (!GetPreviewContents()) { | 574 if (!GetPreviewContents()) { |
| 543 DCHECK(!loader_.get()); | 575 DCHECK(!loader_.get()); |
| 544 loader_.reset(new InstantLoader(this, instant_url, active_tab)); | 576 loader_.reset(new InstantLoader(this, instant_url, active_tab)); |
| 545 loader_->Init(); | 577 loader_->Init(); |
| 546 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED); | 578 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 686 return false; | 718 return false; |
| 687 } | 719 } |
| 688 | 720 |
| 689 return true; | 721 return true; |
| 690 } | 722 } |
| 691 | 723 |
| 692 bool InstantController::IsOutOfDate() const { | 724 bool InstantController::IsOutOfDate() const { |
| 693 return !last_active_tab_ || | 725 return !last_active_tab_ || |
| 694 last_active_tab_ != delegate_->GetActiveTabContents(); | 726 last_active_tab_ != delegate_->GetActiveTabContents(); |
| 695 } | 727 } |
| OLD | NEW |