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

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

Issue 10836031: Remove Instant v1 API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 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/bind.h"
8 #include "base/command_line.h" 7 #include "base/command_line.h"
9 #include "base/message_loop.h" 8 #include "base/i18n/case_conversion.h"
10 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/autocomplete/autocomplete_match.h" 10 #include "chrome/browser/autocomplete/autocomplete_match.h"
11 #include "chrome/browser/history/history.h"
12 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/instant/instant_controller_delegate.h" 13 #include "chrome/browser/instant/instant_controller_delegate.h"
14 #include "chrome/browser/instant/instant_loader.h" 14 #include "chrome/browser/instant/instant_loader.h"
15 #include "chrome/browser/platform_util.h" 15 #include "chrome/browser/platform_util.h"
16 #include "chrome/browser/prefs/pref_service.h" 16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/search_engines/template_url.h"
19 #include "chrome/browser/search_engines/template_url_service.h" 17 #include "chrome/browser/search_engines/template_url_service.h"
20 #include "chrome/browser/search_engines/template_url_service_factory.h" 18 #include "chrome/browser/search_engines/template_url_service_factory.h"
21 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h"
22 #include "chrome/browser/ui/tab_contents/tab_contents.h" 19 #include "chrome/browser/ui/tab_contents/tab_contents.h"
23 #include "chrome/common/chrome_notification_types.h" 20 #include "chrome/common/chrome_notification_types.h"
24 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h" 22 #include "chrome/common/pref_names.h"
26 #include "content/public/browser/notification_service.h" 23 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/render_widget_host_view.h" 24 #include "content/public/browser/render_widget_host_view.h"
28 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
29 26
30 #if defined(TOOLKIT_VIEWS) 27 #if defined(TOOLKIT_VIEWS)
31 #include "ui/views/focus/focus_manager.h"
32 #include "ui/views/view.h"
33 #include "ui/views/widget/widget.h" 28 #include "ui/views/widget/widget.h"
34 #endif 29 #endif
35 30
31 namespace {
32
33 enum PreviewUsageType {
34 PREVIEW_CREATED = 0,
35 PREVIEW_DELETED,
36 PREVIEW_LOADED,
37 PREVIEW_SHOWED,
38 PREVIEW_COMMITTED,
39 PREVIEW_NUM_TYPES,
40 };
41
42 // An artificial delay (in milliseconds) we introduce before telling the Instant
43 // page about the new omnibox bounds, in cases where the bounds shrink. This is
44 // to avoid the page jumping up/down very fast in response to bounds changes.
45 const int kUpdateBoundsDelayMS = 1000;
46
47 // The maximum number of times we'll load a non-Instant-supporting search engine
48 // before we give up and blacklist it for the rest of the browsing session.
49 const int kMaxInstantSupportFailures = 10;
50
51 std::string ModeToString(InstantController::Mode mode) {
52 switch (mode) {
53 case InstantController::INSTANT: return "Instant";
54 case InstantController::SUGGEST: return "Suggest";
55 case InstantController::HIDDEN: return "Hidden";
56 case InstantController::SILENT: return "Silent";
57 }
58
59 NOTREACHED();
60 return std::string();
61 }
62
63 void AddPreviewUsageForHistogram(InstantController::Mode mode,
64 PreviewUsageType usage) {
65 DCHECK(0 <= usage && usage < PREVIEW_NUM_TYPES) << usage;
66 base::Histogram* histogram = base::LinearHistogram::FactoryGet(
67 "Instant.Previews." + ModeToString(mode), 1, PREVIEW_NUM_TYPES,
68 PREVIEW_NUM_TYPES + 1, base::Histogram::kUmaTargetedHistogramFlag);
69 histogram->Add(usage);
70 }
71
72 } // namespace
73
36 InstantController::InstantController(InstantControllerDelegate* delegate, 74 InstantController::InstantController(InstantControllerDelegate* delegate,
37 Mode mode) 75 Mode mode)
38 : delegate_(delegate), 76 : delegate_(delegate),
39 is_displayable_(false), 77 mode_(mode),
40 is_out_of_date_(true), 78 last_active_tab_(NULL),
41 commit_on_pointer_release_(false), 79 last_verbatim_(false),
80 last_complete_behavior_(INSTANT_COMPLETE_NOW),
42 last_transition_type_(content::PAGE_TRANSITION_LINK), 81 last_transition_type_(content::PAGE_TRANSITION_LINK),
43 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), 82 is_showing_(false),
44 mode_(mode) { 83 loader_processed_last_update_(false) {
45 DCHECK(mode_ == INSTANT || mode_ == SUGGEST || mode_ == HIDDEN ||
46 mode_ == SILENT);
47 } 84 }
48 85
49 InstantController::~InstantController() { 86 InstantController::~InstantController() {
87 // We do this explicitly for two reasons: To Hide() if we are showing the
88 // preview, and for proper accounting of PREVIEW_DELETED in histograms.
89 DeleteLoader();
50 } 90 }
51 91
52 // static 92 // static
53 void InstantController::RegisterUserPrefs(PrefService* prefs) { 93 void InstantController::RegisterUserPrefs(PrefService* prefs) {
54 prefs->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, 94 prefs->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, false,
55 false,
56 PrefService::SYNCABLE_PREF); 95 PrefService::SYNCABLE_PREF);
57 prefs->RegisterBooleanPref(prefs::kInstantEnabled, 96 prefs->RegisterBooleanPref(prefs::kInstantEnabled, false,
58 false,
59 PrefService::SYNCABLE_PREF); 97 PrefService::SYNCABLE_PREF);
60 98
61 // TODO(jamescook): Move this to search controller. 99 // TODO(jamescook): Move this to search controller.
62 prefs->RegisterDoublePref(prefs::kInstantAnimationScaleFactor, 100 prefs->RegisterDoublePref(prefs::kInstantAnimationScaleFactor,
63 1.0, 101 1.0,
64 PrefService::UNSYNCABLE_PREF); 102 PrefService::UNSYNCABLE_PREF);
65 } 103 }
66 104
67 // static 105 // static
68 void InstantController::RecordMetrics(Profile* profile) {
69 UMA_HISTOGRAM_ENUMERATION("Instant.Status", IsEnabled(profile), 2);
70 }
71
72 // static
73 bool InstantController::IsEnabled(Profile* profile) { 106 bool InstantController::IsEnabled(Profile* profile) {
74 const PrefService* prefs = profile->GetPrefs(); 107 const PrefService* prefs = profile ? profile->GetPrefs() : NULL;
75 return prefs && prefs->GetBoolean(prefs::kInstantEnabled); 108 return prefs && prefs->GetBoolean(prefs::kInstantEnabled);
76 } 109 }
77 110
78 // static
79 void InstantController::Enable(Profile* profile) {
80 PrefService* prefs = profile->GetPrefs();
81 if (!prefs)
82 return;
83
84 prefs->SetBoolean(prefs::kInstantEnabled, true);
85 prefs->SetBoolean(prefs::kInstantConfirmDialogShown, true);
86 UMA_HISTOGRAM_ENUMERATION("Instant.Preference", 1, 2);
87 }
88
89 // static
90 void InstantController::Disable(Profile* profile) {
91 PrefService* prefs = profile->GetPrefs();
92 if (!prefs)
93 return;
94
95 prefs->SetBoolean(prefs::kInstantEnabled, false);
96 UMA_HISTOGRAM_ENUMERATION("Instant.Preference", 0, 2);
97 }
98
99 bool InstantController::Update(const AutocompleteMatch& match, 111 bool InstantController::Update(const AutocompleteMatch& match,
100 const string16& user_text, 112 const string16& user_text,
101 bool verbatim, 113 bool verbatim,
102 string16* suggested_text) { 114 string16* suggested_text,
103 suggested_text->clear(); 115 InstantCompleteBehavior* complete_behavior) {
116 const TabContents* active_tab = delegate_->GetActiveTabContents();
104 117
105 is_out_of_date_ = false; 118 // We could get here with no active tab if the Browser is closing.
106 commit_on_pointer_release_ = false; 119 if (!active_tab) {
107 last_transition_type_ = match.transition;
108 last_url_ = match.destination_url;
109 last_user_text_ = user_text;
110
111 TabContents* tab_contents = delegate_->GetInstantHostTabContents();
112 if (!tab_contents) {
113 Hide(); 120 Hide();
114 return false; 121 return false;
115 } 122 }
116 123
117 Profile* profile = tab_contents->profile(); 124 std::string instant_url;
118 const TemplateURL* template_url = match.GetTemplateURL(profile); 125 Profile* profile = active_tab->profile();
119 const TemplateURL* default_t_url = 126
120 TemplateURLServiceFactory::GetForProfile(profile) 127 // If the match's TemplateURL isn't valid, it is likely not a query.
121 ->GetDefaultSearchProvider(); 128 if (!GetInstantURL(match.GetTemplateURL(profile), &instant_url)) {
122 if (!IsValidInstantTemplateURL(template_url) || !default_t_url ||
123 template_url->id() != default_t_url->id()) {
124 Hide(); 129 Hide();
125 return false; 130 return false;
126 } 131 }
127 132
128 if (!loader_.get() || loader_->template_url_id() != template_url->id()) 133 string16 full_text = user_text + *suggested_text;
129 loader_.reset(new InstantLoader(this, template_url->id(), std::string()));
130 134
131 if (mode_ == SILENT) { 135 if (full_text.empty()) {
132 // For the SILENT mode, we process |user_text| at commit time. 136 Hide();
133 loader_->MaybeLoadInstantURL(tab_contents, template_url); 137 return false;
138 }
139
140 // The presence of any suggested_text implies verbatim.
141 DCHECK(suggested_text->empty() || verbatim)
142 << user_text << "|" << *suggested_text;
143
144 ResetLoader(instant_url, active_tab);
145 last_active_tab_ = active_tab;
146
147 // Track the non-Instant search URL for this query.
148 url_for_history_ = match.destination_url;
149 last_transition_type_ = match.transition;
150
151 last_user_text_ = user_text;
152
153 // Don't send an update to the loader if the query text hasn't changed.
154 if (full_text == last_full_text_ && verbatim == last_verbatim_) {
155 // Since we are updating |suggested_text|, shouldn't we also update
156 // |last_full_text_|? No. There's no guarantee that our suggestion will
157 // actually be inline autocompleted. For example, it may get trumped by
158 // a history suggestion. If our suggestion does make it, the omnibox will
159 // call Update() again, at which time we'll update |last_full_text_|.
160 *suggested_text = last_suggestion_;
161 *complete_behavior = last_complete_behavior_;
162
163 // We need to call Show() here because of this:
164 // 1. User has typed a query (say Q). Instant overlay is showing results.
165 // 2. User arrows-down to a URL entry or erases all omnibox text. Both of
166 // these cause the overlay to Hide().
167 // 3. User arrows-up to Q or types Q again. The last text we processed is
168 // still Q, so we don't Update() the loader, but we do need to Show().
169 if (loader_processed_last_update_ && mode_ == INSTANT)
170 Show();
134 return true; 171 return true;
135 } 172 }
136 173
137 UpdateLoader(tab_contents, template_url, match.destination_url, 174 last_full_text_ = full_text;
138 match.transition, user_text, verbatim, suggested_text); 175 last_verbatim_ = verbatim;
176 loader_processed_last_update_ = false;
139 177
140 content::NotificationService::current()->Notify( 178 // Reset the last suggestion, as it's no longer valid.
141 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, 179 suggested_text->clear();
142 content::Source<InstantController>(this), 180 last_suggestion_.clear();
143 content::NotificationService::NoDetails()); 181 *complete_behavior = last_complete_behavior_ = INSTANT_COMPLETE_NOW;
182
183 if (mode_ != SILENT) {
184 loader_->Update(last_full_text_, last_verbatim_);
185
186 content::NotificationService::current()->Notify(
187 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED,
188 content::Source<InstantController>(this),
189 content::NotificationService::NoDetails());
190 }
191
144 return true; 192 return true;
145 } 193 }
146 194
195 // TODO(tonyg): This method only fires when the omnibox bounds change. It also
196 // needs to fire when the preview bounds change (e.g.: open/close info bar).
147 void InstantController::SetOmniboxBounds(const gfx::Rect& bounds) { 197 void InstantController::SetOmniboxBounds(const gfx::Rect& bounds) {
148 if (omnibox_bounds_ == bounds) 198 if (omnibox_bounds_ == bounds || mode_ != INSTANT)
149 return; 199 return;
150 200
151 // Always track the omnibox bounds. That way if Update is later invoked the
152 // bounds are in sync.
153 omnibox_bounds_ = bounds; 201 omnibox_bounds_ = bounds;
154 202 if (omnibox_bounds_.height() > last_omnibox_bounds_.height()) {
155 if (loader_.get() && !is_out_of_date_ && mode_ == INSTANT) 203 update_bounds_timer_.Stop();
156 loader_->SetOmniboxBounds(bounds); 204 SendBoundsToPage();
205 } else if (!update_bounds_timer_.IsRunning()) {
206 update_bounds_timer_.Start(FROM_HERE,
207 base::TimeDelta::FromMilliseconds(kUpdateBoundsDelayMS), this,
208 &InstantController::SendBoundsToPage);
209 }
157 } 210 }
158 211
159 void InstantController::DestroyPreviewContents() { 212 TabContents* InstantController::GetPreviewContents() const {
160 if (!loader_.get()) { 213 return loader_.get() ? loader_->preview_contents() : NULL;
161 // We're not showing anything, nothing to do.
162 return;
163 }
164
165 if (is_displayable_) {
166 is_displayable_ = false;
167 delegate_->HideInstant();
168 }
169 delete ReleasePreviewContents(INSTANT_COMMIT_DESTROY, NULL);
170 } 214 }
171 215
172 void InstantController::Hide() { 216 void InstantController::Hide() {
173 is_out_of_date_ = true; 217 last_active_tab_ = NULL;
174 commit_on_pointer_release_ = false; 218 if (is_showing_) {
175 if (is_displayable_) { 219 is_showing_ = false;
176 is_displayable_ = false;
177 delegate_->HideInstant(); 220 delegate_->HideInstant();
178 } 221 }
179 } 222 }
180 223
181 bool InstantController::IsCurrent() const { 224 bool InstantController::IsCurrent() const {
182 // TODO(mmenke): See if we can do something more intelligent in the 225 DCHECK(IsOutOfDate() || GetPreviewContents());
183 // navigation pending case. 226 return !IsOutOfDate() && GetPreviewContents() && loader_->supports_instant();
184 return is_displayable_ && !loader_->IsNavigationPending() &&
185 !loader_->needs_reload();
186 }
187
188 bool InstantController::PrepareForCommit() {
189 if (is_out_of_date_ || !loader_.get())
190 return false;
191
192 // If we are in the visible (INSTANT) mode, return the status of the preview.
193 if (mode_ == INSTANT)
194 return IsCurrent();
195
196 TabContents* tab_contents = delegate_->GetInstantHostTabContents();
197 if (!tab_contents)
198 return false;
199
200 const TemplateURL* template_url =
201 TemplateURLServiceFactory::GetForProfile(tab_contents->profile())
202 ->GetDefaultSearchProvider();
203 if (!IsValidInstantTemplateURL(template_url) ||
204 loader_->template_url_id() != template_url->id() ||
205 loader_->IsNavigationPending() ||
206 loader_->is_determining_if_page_supports_instant()) {
207 return false;
208 }
209
210 // In the SUGGEST and HIDDEN modes, we must have sent an Update() by now, so
211 // check if the loader failed to process it.
212 if ((mode_ == SUGGEST || mode_ == HIDDEN)
213 && (!loader_->ready() || !loader_->http_status_ok())) {
214 return false;
215 }
216
217 // Ignore the suggested text, as we are about to commit the verbatim query.
218 string16 suggested_text;
219 UpdateLoader(tab_contents, template_url, last_url_, last_transition_type_,
220 last_user_text_, true, &suggested_text);
221 return true;
222 } 227 }
223 228
224 TabContents* InstantController::CommitCurrentPreview(InstantCommitType type) { 229 TabContents* InstantController::CommitCurrentPreview(InstantCommitType type) {
225 DCHECK(loader_.get()); 230 const TabContents* active_tab = delegate_->GetActiveTabContents();
226 TabContents* tab_contents = delegate_->GetInstantHostTabContents(); 231 TabContents* preview = ReleasePreviewContents(type);
227 DCHECK(tab_contents);
228 TabContents* preview = ReleasePreviewContents(type, tab_contents);
229 preview->web_contents()->GetController().CopyStateFromAndPrune( 232 preview->web_contents()->GetController().CopyStateFromAndPrune(
230 &tab_contents->web_contents()->GetController()); 233 &active_tab->web_contents()->GetController());
231 delegate_->CommitInstant(preview); 234 delegate_->CommitInstant(preview);
232 CompleteRelease(preview);
233 return preview; 235 return preview;
234 } 236 }
235 237
236 bool InstantController::CommitIfCurrent() { 238 TabContents* InstantController::ReleasePreviewContents(InstantCommitType type) {
237 if (IsCurrent()) { 239 TabContents* preview = loader_->ReleasePreviewContents(type, last_full_text_);
238 CommitCurrentPreview(INSTANT_COMMIT_PRESSED_ENTER); 240
239 return true; 241 // Add a fake history entry with a non-Instant search URL, so that search
242 // terms extraction (for autocomplete history matches) works.
243 HistoryService* history = HistoryServiceFactory::GetForProfile(
244 preview->profile(), Profile::EXPLICIT_ACCESS);
245 if (history) {
246 history->AddPage(url_for_history_, NULL, 0, GURL(), last_transition_type_,
Jered 2012/07/31 22:36:02 It feels weird that we add history even if this is
sreeram 2012/07/31 23:13:19 ReleasePreviewContents() is only called on a commi
Jered 2012/08/01 21:56:45 Having these two closely related methods (Commit a
sreeram 2012/08/01 22:56:49 Agreed. We're going to be doing just that when we
247 history::RedirectList(), history::SOURCE_BROWSED, false);
240 } 248 }
241 return false; 249
250 AddPreviewUsageForHistogram(mode_, PREVIEW_COMMITTED);
251
252 // We may have gotten here from CommitInstant(), which means the loader may
253 // still be on the stack. So, schedule a destruction for later.
254 MessageLoop::current()->DeleteSoon(FROM_HERE, loader_.release());
255
256 // This call is here to hide the preview and reset view state. It won't
257 // actually delete |loader_| because it was just released to DeleteSoon().
258 DeleteLoader();
259
260 return preview;
242 } 261 }
243 262
244 void InstantController::SetCommitOnPointerRelease() { 263 // TODO(sreeram): Since we never delete the loader except when committing
245 commit_on_pointer_release_ = true; 264 // Instant, the loader may have a very stale page. Reload it when stale.
246 } 265 void InstantController::OnAutocompleteLostFocus(
266 gfx::NativeView view_gaining_focus) {
267 DCHECK(!is_showing_ || GetPreviewContents());
247 268
248 bool InstantController::IsPointerDownFromActivate() { 269 // If the preview is not showing, nothing to do.
249 DCHECK(loader_.get()); 270 //
250 return loader_->IsPointerDownFromActivate(); 271 // If the preview is showing, we may get here with a NULL GetPreviewContents()
251 } 272 // in one case: When the user presses Enter, ReleasePreviewContents() is
273 // called, which releases the preview and calls delegate_->CommitInstant(),
274 // which ends up causing the omnibox to lose focus.
275 if (!is_showing_ || !GetPreviewContents())
276 return;
252 277
253 #if defined(OS_MACOSX) 278 #if defined(OS_MACOSX)
254 void InstantController::OnAutocompleteLostFocus( 279 if (!loader_->IsPointerDownFromActivate())
255 gfx::NativeView view_gaining_focus) { 280 Hide();
256 // If |IsPointerDownFromActivate()| returns false, the RenderWidgetHostView
257 // did not receive a mouseDown event. Therefore, we should destroy the
258 // preview. Otherwise, the RWHV was clicked, so we commit the preview.
259 if (!IsCurrent() || !IsPointerDownFromActivate())
260 DestroyPreviewContents();
261 else
262 SetCommitOnPointerRelease();
263 }
264 #else 281 #else
265 void InstantController::OnAutocompleteLostFocus( 282 content::RenderWidgetHostView* rwhv =
266 gfx::NativeView view_gaining_focus) { 283 GetPreviewContents()->web_contents()->GetRenderWidgetHostView();
267 if (!IsCurrent()) { 284 if (!view_gaining_focus || !rwhv) {
268 DestroyPreviewContents(); 285 Hide();
269 return; 286 return;
270 } 287 }
271 288
272 content::RenderWidgetHostView* rwhv =
273 GetPreviewContents()->web_contents()->GetRenderWidgetHostView();
274 if (!view_gaining_focus || !rwhv) {
275 DestroyPreviewContents();
276 return;
277 }
278
279 #if defined(TOOLKIT_VIEWS) 289 #if defined(TOOLKIT_VIEWS)
280 // For views the top level widget is always focused. If the focus change 290 // For views the top level widget is always focused. If the focus change
281 // originated in views determine the child Widget from the view that is being 291 // originated in views determine the child Widget from the view that is being
282 // focused. 292 // focused.
283 views::Widget* widget = 293 views::Widget* widget =
284 views::Widget::GetWidgetForNativeView(view_gaining_focus); 294 views::Widget::GetWidgetForNativeView(view_gaining_focus);
285 if (widget) { 295 if (widget) {
286 views::FocusManager* focus_manager = widget->GetFocusManager(); 296 views::FocusManager* focus_manager = widget->GetFocusManager();
287 if (focus_manager && focus_manager->is_changing_focus() && 297 if (focus_manager && focus_manager->is_changing_focus() &&
288 focus_manager->GetFocusedView() && 298 focus_manager->GetFocusedView() &&
289 focus_manager->GetFocusedView()->GetWidget()) { 299 focus_manager->GetFocusedView()->GetWidget()) {
290 view_gaining_focus = 300 view_gaining_focus =
291 focus_manager->GetFocusedView()->GetWidget()->GetNativeView(); 301 focus_manager->GetFocusedView()->GetWidget()->GetNativeView();
292 } 302 }
293 } 303 }
294 #endif 304 #endif
295 305
296 gfx::NativeView tab_view = 306 gfx::NativeView tab_view =
297 GetPreviewContents()->web_contents()->GetNativeView(); 307 GetPreviewContents()->web_contents()->GetNativeView();
308
298 // Focus is going to the renderer. 309 // Focus is going to the renderer.
299 if (rwhv->GetNativeView() == view_gaining_focus || 310 if (rwhv->GetNativeView() == view_gaining_focus ||
300 tab_view == view_gaining_focus) { 311 tab_view == view_gaining_focus) {
301 if (!IsPointerDownFromActivate()) {
302 // If the mouse is not down, focus is not going to the renderer. Someone
303 // else moved focus and we shouldn't commit.
304 DestroyPreviewContents();
305 return;
306 }
307 312
308 // We're showing instant results. As instant results may shift when 313 // If the mouse is not down, focus is not going to the renderer. Someone
309 // committing we commit on the mouse up. This way a slow click still works 314 // else moved focus and we shouldn't commit.
310 // fine. 315 if (!loader_->IsPointerDownFromActivate())
311 SetCommitOnPointerRelease(); 316 Hide();
317
312 return; 318 return;
313 } 319 }
314 320
315 // Walk up the view hierarchy. If the view gaining focus is a subview of the 321 // Walk up the view hierarchy. If the view gaining focus is a subview of the
316 // WebContents view (such as a windowed plugin or http auth dialog), we want 322 // WebContents view (such as a windowed plugin or http auth dialog), we want
317 // to keep the preview contents. Otherwise, focus has gone somewhere else, 323 // to keep the preview contents. Otherwise, focus has gone somewhere else,
318 // such as the JS inspector, and we want to cancel the preview. 324 // such as the JS inspector, and we want to cancel the preview.
319 gfx::NativeView view_gaining_focus_ancestor = view_gaining_focus; 325 gfx::NativeView view_gaining_focus_ancestor = view_gaining_focus;
320 while (view_gaining_focus_ancestor && 326 while (view_gaining_focus_ancestor &&
321 view_gaining_focus_ancestor != tab_view) { 327 view_gaining_focus_ancestor != tab_view) {
322 view_gaining_focus_ancestor = 328 view_gaining_focus_ancestor =
323 platform_util::GetParent(view_gaining_focus_ancestor); 329 platform_util::GetParent(view_gaining_focus_ancestor);
324 } 330 }
325 331
326 if (view_gaining_focus_ancestor) { 332 if (view_gaining_focus_ancestor) {
327 CommitCurrentPreview(INSTANT_COMMIT_FOCUS_LOST); 333 CommitCurrentPreview(INSTANT_COMMIT_FOCUS_LOST);
328 return; 334 return;
329 } 335 }
330 336
331 DestroyPreviewContents(); 337 Hide();
332 }
333 #endif 338 #endif
339 }
334 340
335 void InstantController::OnAutocompleteGotFocus() { 341 void InstantController::OnAutocompleteGotFocus() {
336 TabContents* tab_contents = delegate_->GetInstantHostTabContents(); 342 const TabContents* active_tab = delegate_->GetActiveTabContents();
337 if (!tab_contents) 343
338 return; 344 // We could get here with no active tab if the Browser is closing.
339 345 if (!active_tab)
346 return;
347
348 // Since we don't have any autocomplete match to work with, we'll just use
349 // the default search provider's Instant URL.
340 const TemplateURL* template_url = 350 const TemplateURL* template_url =
341 TemplateURLServiceFactory::GetForProfile(tab_contents->profile()) 351 TemplateURLServiceFactory::GetForProfile(active_tab->profile())->
342 ->GetDefaultSearchProvider(); 352 GetDefaultSearchProvider();
343 if (!IsValidInstantTemplateURL(template_url)) 353
344 return; 354 std::string instant_url;
345 355 if (!GetInstantURL(template_url, &instant_url))
346 if (!loader_.get() || loader_->template_url_id() != template_url->id()) 356 return;
347 loader_.reset(new InstantLoader(this, template_url->id(), std::string())); 357
348 loader_->MaybeLoadInstantURL(tab_contents, template_url); 358 ResetLoader(instant_url, active_tab);
349 } 359 }
350 360
351 TabContents* InstantController::ReleasePreviewContents( 361 bool InstantController::commit_on_pointer_release() const {
352 InstantCommitType type, 362 return GetPreviewContents() && loader_->IsPointerDownFromActivate();
353 TabContents* current_tab) { 363 }
354 if (!loader_.get()) 364
355 return NULL; 365 void InstantController::SetSuggestions(
356
357 TabContents* tab = loader_->ReleasePreviewContents(type, current_tab);
358 ClearBlacklist();
359 is_out_of_date_ = true;
360 is_displayable_ = false;
361 commit_on_pointer_release_ = false;
362 omnibox_bounds_ = gfx::Rect();
363 loader_.reset();
364 return tab;
365 }
366
367 void InstantController::CompleteRelease(TabContents* tab) {
368 tab->blocked_content_tab_helper()->SetAllContentsBlocked(false);
369 }
370
371 TabContents* InstantController::GetPreviewContents() const {
372 return loader_.get() ? loader_->preview_contents() : NULL;
373 }
374
375 void InstantController::InstantStatusChanged(InstantLoader* loader) {
376 DCHECK(loader_.get());
377 UpdateIsDisplayable();
378 }
379
380 void InstantController::SetSuggestedTextFor(
381 InstantLoader* loader, 366 InstantLoader* loader,
382 const string16& text, 367 const std::vector<string16>& suggestions,
383 InstantCompleteBehavior behavior) { 368 InstantCompleteBehavior behavior) {
384 if (is_out_of_date_) 369 DCHECK_EQ(loader_.get(), loader);
385 return; 370 if (loader_ != loader || IsOutOfDate() || mode_ == SILENT || mode_ == HIDDEN)
386 371 return;
387 if (mode_ == INSTANT || mode_ == SUGGEST) 372
388 delegate_->SetSuggestedText(text, behavior); 373 loader_processed_last_update_ = true;
389 } 374
390 375 string16 suggestion;
391 gfx::Rect InstantController::GetInstantBounds() { 376 if (!suggestions.empty()) {
392 return delegate_->GetInstantBounds(); 377 suggestion = suggestions[0];
393 } 378 }
394 379
395 bool InstantController::ShouldCommitInstantOnPointerRelease() { 380 string16 suggestion_lower = base::i18n::ToLower(suggestion);
396 return commit_on_pointer_release_; 381 string16 user_text_lower = base::i18n::ToLower(last_user_text_);
382 if (user_text_lower.size() >= suggestion_lower.size() ||
383 suggestion_lower.compare(0, user_text_lower.size(), user_text_lower)) {
384 suggestion.clear();
385 } else {
386 suggestion.erase(0, last_user_text_.size());
387 }
388
389 last_suggestion_ = suggestion;
390 last_complete_behavior_ = behavior;
391 if (!last_verbatim_)
392 delegate_->SetSuggestedText(suggestion, behavior);
393
394 if (mode_ != SUGGEST)
395 Show();
397 } 396 }
398 397
399 void InstantController::CommitInstantLoader(InstantLoader* loader) { 398 void InstantController::CommitInstantLoader(InstantLoader* loader) {
400 if (loader_.get() && loader_.get() == loader) { 399 DCHECK_EQ(loader_.get(), loader);
401 CommitCurrentPreview(INSTANT_COMMIT_FOCUS_LOST); 400 DCHECK(is_showing_ && !IsOutOfDate()) << is_showing_;
401 if (loader_ != loader || !is_showing_ || IsOutOfDate())
402 return;
403
404 CommitCurrentPreview(INSTANT_COMMIT_FOCUS_LOST);
405 }
406
407 void InstantController::InstantLoaderPreviewLoaded(InstantLoader* loader) {
408 DCHECK_EQ(loader_.get(), loader);
409 AddPreviewUsageForHistogram(mode_, PREVIEW_LOADED);
410 }
411
412 void InstantController::InstantSupportDetermined(InstantLoader* loader,
413 bool supports_instant) {
414 DCHECK_EQ(loader_.get(), loader);
415 if (supports_instant) {
416 blacklisted_urls_.erase(loader->instant_url());
402 } else { 417 } else {
403 // This can happen if the mouse was down, we swapped out the preview and 418 ++blacklisted_urls_[loader->instant_url()];
404 // the mouse was released. Generally this shouldn't happen, but if it does 419 if (loader_ == loader) {
405 // revert. 420 if (GetPreviewContents())
406 DestroyPreviewContents(); 421 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED);
407 } 422
408 } 423 // Because of the state of the stack, we can't destroy the loader now.
409 424 MessageLoop::current()->DeleteSoon(FROM_HERE, loader_.release());
410 void InstantController::InstantLoaderDoesntSupportInstant( 425 DeleteLoader();
411 InstantLoader* loader) { 426 }
412 VLOG(1) << "provider does not support instant"; 427 }
413 428
414 // Don't attempt to use instant for this search engine again. 429 content::Details<const bool> details(&supports_instant);
415 BlacklistFromInstant(); 430 content::NotificationService::current()->Notify(
416 } 431 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED,
417 432 content::NotificationService::AllSources(),
418 void InstantController::AddToBlacklist(InstantLoader* loader, const GURL& url) { 433 details);
419 // Don't attempt to use instant for this search engine again.
420 BlacklistFromInstant();
421 } 434 }
422 435
423 void InstantController::SwappedTabContents(InstantLoader* loader) { 436 void InstantController::SwappedTabContents(InstantLoader* loader) {
424 if (is_displayable_) 437 DCHECK_EQ(loader_.get(), loader);
425 delegate_->ShowInstant(loader->preview_contents()); 438 if (loader_ == loader && is_showing_)
426 } 439 delegate_->ShowInstant();
427 440 }
428 void InstantController::InstantLoaderContentsFocused() { 441
442 void InstantController::InstantLoaderContentsFocused(InstantLoader* loader) {
443 DCHECK_EQ(loader_.get(), loader);
444 DCHECK(is_showing_ && !IsOutOfDate()) << is_showing_;
429 #if defined(USE_AURA) 445 #if defined(USE_AURA)
430 // On aura the omnibox only receives a focus lost if we initiate the focus 446 // On aura the omnibox only receives a focus lost if we initiate the focus
431 // change. This does that. 447 // change. This does that.
432 if (mode_ == INSTANT) 448 if (is_showing_ && !IsOutOfDate())
433 delegate_->InstantPreviewFocused(); 449 delegate_->InstantPreviewFocused();
434 #endif 450 #endif
435 } 451 }
436 452
437 void InstantController::UpdateIsDisplayable() { 453 void InstantController::ResetLoader(const std::string& instant_url,
438 bool displayable = !is_out_of_date_ && loader_.get() && loader_->ready() && 454 const TabContents* active_tab) {
439 loader_->http_status_ok(); 455 if (GetPreviewContents() && loader_->instant_url() != instant_url)
440 if (displayable == is_displayable_ || mode_ != INSTANT) 456 DeleteLoader();
441 return; 457
442 458 if (!GetPreviewContents()) {
443 is_displayable_ = displayable; 459 loader_.reset(new InstantLoader(this, instant_url, active_tab));
444 if (!is_displayable_) { 460 loader_->Init();
445 delegate_->HideInstant(); 461 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED);
446 } else { 462 }
447 delegate_->ShowInstant(loader_->preview_contents()); 463 }
448 content::NotificationService::current()->Notify( 464
449 chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN, 465 void InstantController::DeleteLoader() {
450 content::Source<InstantController>(this), 466 Hide();
451 content::NotificationService::NoDetails()); 467 last_full_text_.clear();
452 } 468 last_user_text_.clear();
453 } 469 last_verbatim_ = false;
454 470 last_suggestion_.clear();
455 void InstantController::UpdateLoader(TabContents* tab_contents, 471 last_complete_behavior_ = INSTANT_COMPLETE_NOW;
456 const TemplateURL* template_url, 472 last_transition_type_ = content::PAGE_TRANSITION_LINK;
457 const GURL& url, 473 last_omnibox_bounds_ = gfx::Rect();
458 content::PageTransition transition_type, 474 url_for_history_ = GURL();
459 const string16& user_text, 475 if (GetPreviewContents())
460 bool verbatim, 476 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED);
461 string16* suggested_text) { 477 loader_.reset();
462 if (mode_ == INSTANT) 478 }
463 loader_->SetOmniboxBounds(omnibox_bounds_); 479
464 loader_->Update(tab_contents, template_url, url, transition_type, user_text, 480 void InstantController::Show() {
465 verbatim, suggested_text); 481 if (!is_showing_) {
466 UpdateIsDisplayable(); 482 is_showing_ = true;
467 // For the HIDDEN and SILENT modes, don't send back suggestions. 483 delegate_->ShowInstant();
468 if (mode_ == HIDDEN || mode_ == SILENT) 484 AddPreviewUsageForHistogram(mode_, PREVIEW_SHOWED);
469 suggested_text->clear(); 485 }
470 } 486 }
471 487
472 // Returns true if |template_url| is a valid TemplateURL for use by instant. 488 void InstantController::SendBoundsToPage() {
473 bool InstantController::IsValidInstantTemplateURL( 489 if (last_omnibox_bounds_ == omnibox_bounds_ || IsOutOfDate() ||
474 const TemplateURL* template_url) { 490 !GetPreviewContents() || loader_->IsPointerDownFromActivate()) {
475 return template_url && template_url->id() && 491 return;
476 template_url->instant_url_ref().SupportsReplacement() && 492 }
477 !IsBlacklistedFromInstant(template_url->id()); 493
478 } 494 last_omnibox_bounds_ = omnibox_bounds_;
479 495 gfx::Rect preview_bounds = delegate_->GetInstantBounds();
480 void InstantController::BlacklistFromInstant() { 496 gfx::Rect intersection = omnibox_bounds_.Intersect(preview_bounds);
481 if (!loader_.get()) 497
482 return; 498 // Translate into window coordinates.
483 499 if (!intersection.IsEmpty()) {
484 DCHECK(loader_->template_url_id()); 500 intersection.Offset(-preview_bounds.origin().x(),
485 blacklisted_ids_.insert(loader_->template_url_id()); 501 -preview_bounds.origin().y());
486 502 }
487 // Because of the state of the stack we can't destroy the loader now. 503
488 ScheduleDestroy(loader_.release()); 504 // In the current Chrome UI, these must always be true so they sanity check
489 UpdateIsDisplayable(); 505 // the above operations. In a future UI, these may be removed or adjusted.
490 } 506 // There is no point in sanity-checking |intersection.y()| because the omnibox
491 507 // can be placed anywhere vertically relative to the preview (for example, in
492 bool InstantController::IsBlacklistedFromInstant(TemplateURLID id) { 508 // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds).
493 return blacklisted_ids_.count(id) > 0; 509 DCHECK_LE(0, intersection.x());
494 } 510 DCHECK_LE(0, intersection.width());
495 511 DCHECK_LE(0, intersection.height());
496 void InstantController::ClearBlacklist() { 512
497 blacklisted_ids_.clear(); 513 loader_->SetOmniboxBounds(intersection);
498 } 514 }
499 515
500 void InstantController::ScheduleDestroy(InstantLoader* loader) { 516 bool InstantController::GetInstantURL(const TemplateURL* template_url,
501 loaders_to_destroy_.push_back(loader); 517 std::string* instant_url) const {
502 if (!weak_factory_.HasWeakPtrs()) { 518 CommandLine* command_line = CommandLine::ForCurrentProcess();
503 MessageLoop::current()->PostTask( 519 if (command_line->HasSwitch(switches::kInstantURL)) {
504 FROM_HERE, base::Bind(&InstantController::DestroyLoaders, 520 *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL);
505 weak_factory_.GetWeakPtr())); 521 return true;
506 } 522 }
507 } 523
508 524 if (!template_url)
509 void InstantController::DestroyLoaders() { 525 return false;
510 loaders_to_destroy_.clear(); 526
511 } 527 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref();
528 if (!instant_url_ref.IsValid() || !instant_url_ref.SupportsReplacement())
529 return false;
530
531 *instant_url = instant_url_ref.ReplaceSearchTerms(
532 TemplateURLRef::SearchTermsArgs(string16()));
533
534 std::map<std::string, int>::const_iterator iter =
535 blacklisted_urls_.find(*instant_url);
536 if (iter != blacklisted_urls_.end() &&
537 iter->second > kMaxInstantSupportFailures) {
538 instant_url->clear();
539 return false;
540 }
541
542 return true;
543 }
544
545 bool InstantController::IsOutOfDate() const {
546 return !last_active_tab_ ||
547 last_active_tab_ != delegate_->GetActiveTabContents();
548 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698