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