Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: chrome/browser/instant/instant_controller.cc

Issue 10879043: Centralize logic around Instant modes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: renamed to CreateInstant; fixed ordering Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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)) {
Peter Kasting 2012/08/31 01:51:19 Nit: If you reverse this conditional you can omit
sreeram 2012/09/04 17:42:16 Done.
117 if (chrome::search::IsInstantExtendedAPIEnabled(profile))
Peter Kasting 2012/08/31 01:51:19 Nit: Also could use ?:
sreeram 2012/09/04 17:42:16 Done.
118 return InstantController::EXTENDED;
119 return InstantController::INSTANT;
120 }
121 return InstantController::DISABLED;
122 }
123
112 } // namespace 124 } // namespace
113 125
114 InstantController::InstantController(InstantControllerDelegate* delegate,
115 Mode mode)
116 : delegate_(delegate),
117 mode_(mode),
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 InstantController::~InstantController() {
126 if (GetPreviewContents()) 127 if (GetPreviewContents())
127 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); 128 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED);
128 } 129 }
129 130
130 // static 131 // static
132 InstantController* InstantController::CreateInstant(
133 Profile* profile,
134 InstantControllerDelegate* delegate) {
135 const Mode mode = GetModeForProfile(profile);
136 return mode == DISABLED ? NULL : new InstantController(delegate, mode);
137 }
138
139 // static
140 bool InstantController::IsExtendedAPIEnabled(Profile* profile) {
141 return GetModeForProfile(profile) == EXTENDED;
142 }
143
144 // static
145 bool InstantController::IsInstantEnabled(Profile* profile) {
146 const Mode mode = GetModeForProfile(profile);
147 return mode == EXTENDED || mode == INSTANT;
148 }
149
150 // static
151 bool InstantController::IsSuggestEnabled(Profile* profile) {
152 const Mode mode = GetModeForProfile(profile);
153 return mode == EXTENDED || mode == INSTANT || mode == SUGGEST;
154 }
155
156 // static
131 void InstantController::RegisterUserPrefs(PrefService* prefs) { 157 void InstantController::RegisterUserPrefs(PrefService* prefs) {
132 prefs->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, false, 158 prefs->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, false,
133 PrefService::SYNCABLE_PREF); 159 PrefService::SYNCABLE_PREF);
134 prefs->RegisterBooleanPref(prefs::kInstantEnabled, false, 160 prefs->RegisterBooleanPref(prefs::kInstantEnabled, false,
135 PrefService::SYNCABLE_PREF); 161 PrefService::SYNCABLE_PREF);
136 162
137 // TODO(jamescook): Move this to search controller. 163 // TODO(jamescook): Move this to search controller.
138 prefs->RegisterDoublePref(prefs::kInstantAnimationScaleFactor, 164 prefs->RegisterDoublePref(prefs::kInstantAnimationScaleFactor, 1.0,
139 1.0,
140 PrefService::UNSYNCABLE_PREF); 165 PrefService::UNSYNCABLE_PREF);
141 } 166 }
142 167
143 // static
144 bool InstantController::IsEnabled(Profile* profile) {
145 const PrefService* prefs = profile ? profile->GetPrefs() : NULL;
146 return prefs && prefs->GetBoolean(prefs::kInstantEnabled);
147 }
148
149 bool InstantController::Update(const AutocompleteMatch& match, 168 bool InstantController::Update(const AutocompleteMatch& match,
150 const string16& user_text, 169 const string16& user_text,
151 const string16& full_text, 170 const string16& full_text,
152 bool verbatim) { 171 bool verbatim) {
153 const TabContents* active_tab = delegate_->GetActiveTabContents(); 172 const TabContents* active_tab = delegate_->GetActiveTabContents();
154 173
155 // We could get here with no active tab if the Browser is closing. 174 // We could get here with no active tab if the Browser is closing.
156 if (!active_tab) { 175 if (!active_tab) {
157 Hide(); 176 Hide();
158 return false; 177 return false;
(...skipping 16 matching lines...) Expand all
175 ResetLoader(instant_url, active_tab); 194 ResetLoader(instant_url, active_tab);
176 last_active_tab_ = active_tab; 195 last_active_tab_ = active_tab;
177 196
178 // Track the non-Instant search URL for this query. 197 // Track the non-Instant search URL for this query.
179 url_for_history_ = match.destination_url; 198 url_for_history_ = match.destination_url;
180 last_transition_type_ = match.transition; 199 last_transition_type_ = match.transition;
181 200
182 // In EXTENDED mode, we send only |user_text| as the query text. In all other 201 // In EXTENDED mode, we send only |user_text| as the query text. In all other
183 // modes, we use the entire |full_text|. 202 // modes, we use the entire |full_text|.
184 const string16& query_text = mode_ == EXTENDED ? user_text : full_text; 203 const string16& query_text = mode_ == EXTENDED ? user_text : full_text;
185 string16 last_query_text = 204 string16 last_query_text = mode_ == EXTENDED ? last_user_text_ :
Peter Kasting 2012/08/31 01:51:19 Nit: Wrap after '?' instead
sreeram 2012/09/04 17:42:16 Done.
186 mode_ == EXTENDED ? last_user_text_ : last_full_text_; 205 last_full_text_;
187 last_user_text_ = user_text; 206 last_user_text_ = user_text;
188 last_full_text_ = full_text; 207 last_full_text_ = full_text;
189 208
190 // Don't send an update to the loader if the query text hasn't changed. 209 // 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_) { 210 if (query_text == last_query_text && verbatim == last_verbatim_) {
192 // Reuse the last suggestion, as it's still valid. 211 // Reuse the last suggestion, as it's still valid.
193 delegate_->SetSuggestedText(last_suggestion_.text, 212 delegate_->SetSuggestedText(last_suggestion_.text,
194 last_suggestion_.behavior); 213 last_suggestion_.behavior);
195 214
196 // We need to call Show() here because of this: 215 // We need to call Show() here because of this:
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 DCHECK_EQ(loader_.get(), loader); 546 DCHECK_EQ(loader_.get(), loader);
528 DCHECK(is_showing_ && !IsOutOfDate()) << is_showing_; 547 DCHECK(is_showing_ && !IsOutOfDate()) << is_showing_;
529 #if defined(USE_AURA) 548 #if defined(USE_AURA)
530 // On aura the omnibox only receives a focus lost if we initiate the focus 549 // On aura the omnibox only receives a focus lost if we initiate the focus
531 // change. This does that. 550 // change. This does that.
532 if (is_showing_ && !IsOutOfDate()) 551 if (is_showing_ && !IsOutOfDate())
533 delegate_->InstantPreviewFocused(); 552 delegate_->InstantPreviewFocused();
534 #endif 553 #endif
535 } 554 }
536 555
556 InstantController::InstantController(InstantControllerDelegate* delegate,
557 Mode mode)
558 : delegate_(delegate),
559 mode_(mode),
560 last_active_tab_(NULL),
561 last_verbatim_(false),
562 last_transition_type_(content::PAGE_TRANSITION_LINK),
563 is_showing_(false),
564 loader_processed_last_update_(false) {
565 }
566
537 void InstantController::ResetLoader(const std::string& instant_url, 567 void InstantController::ResetLoader(const std::string& instant_url,
538 const TabContents* active_tab) { 568 const TabContents* active_tab) {
539 if (GetPreviewContents() && loader_->instant_url() != instant_url) 569 if (GetPreviewContents() && loader_->instant_url() != instant_url)
540 DeleteLoader(); 570 DeleteLoader();
541 571
542 if (!GetPreviewContents()) { 572 if (!GetPreviewContents()) {
543 DCHECK(!loader_.get()); 573 DCHECK(!loader_.get());
544 loader_.reset(new InstantLoader(this, instant_url, active_tab)); 574 loader_.reset(new InstantLoader(this, instant_url, active_tab));
545 loader_->Init(); 575 loader_->Init();
546 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED); 576 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED);
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 return false; 716 return false;
687 } 717 }
688 718
689 return true; 719 return true;
690 } 720 }
691 721
692 bool InstantController::IsOutOfDate() const { 722 bool InstantController::IsOutOfDate() const {
693 return !last_active_tab_ || 723 return !last_active_tab_ ||
694 last_active_tab_ != delegate_->GetActiveTabContents(); 724 last_active_tab_ != delegate_->GetActiveTabContents();
695 } 725 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698