OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/instant/instant_controller.h" | 5 #include "chrome/browser/instant/instant_controller.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "chrome/browser/autocomplete/autocomplete_match.h" | 10 #include "chrome/browser/autocomplete/autocomplete_match.h" |
11 #include "chrome/browser/instant/instant_delegate.h" | 11 #include "chrome/browser/instant/instant_delegate.h" |
12 #include "chrome/browser/instant/instant_loader.h" | 12 #include "chrome/browser/instant/instant_loader.h" |
13 #include "chrome/browser/instant/instant_loader_manager.h" | 13 #include "chrome/browser/instant/instant_loader_manager.h" |
14 #include "chrome/browser/instant/promo_counter.h" | 14 #include "chrome/browser/instant/promo_counter.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/profile.h" | 17 #include "chrome/browser/profile.h" |
18 #include "chrome/browser/renderer_host/render_widget_host_view.h" | 18 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
19 #include "chrome/browser/search_engines/template_url.h" | 19 #include "chrome/browser/search_engines/template_url.h" |
20 #include "chrome/browser/search_engines/template_url_model.h" | 20 #include "chrome/browser/search_engines/template_url_model.h" |
21 #include "chrome/browser/tab_contents/tab_contents.h" | 21 #include "chrome/browser/tab_contents/tab_contents.h" |
| 22 #include "chrome/browser/tab_contents_wrapper.h" |
22 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
23 #include "chrome/common/notification_service.h" | 24 #include "chrome/common/notification_service.h" |
24 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
25 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
26 | 27 |
27 // Number of ms to delay between loading urls. | 28 // Number of ms to delay between loading urls. |
28 static const int kUpdateDelayMS = 200; | 29 static const int kUpdateDelayMS = 200; |
29 | 30 |
30 static InstantController::Type GetType(Profile* profile) { | 31 static InstantController::Type GetType(Profile* profile) { |
31 return InstantController::IsEnabled(profile, | 32 return InstantController::IsEnabled(profile, |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 return; | 149 return; |
149 | 150 |
150 base::TimeDelta delta = | 151 base::TimeDelta delta = |
151 base::Time::Now() - base::Time::FromInternalValue(enable_time); | 152 base::Time::Now() - base::Time::FromInternalValue(enable_time); |
152 std::string name = IsEnabled(profile, PREDICTIVE_TYPE) ? | 153 std::string name = IsEnabled(profile, PREDICTIVE_TYPE) ? |
153 "Instant.TimeToDisable.Predictive" : "Instant.TimeToDisable.Verbatim"; | 154 "Instant.TimeToDisable.Predictive" : "Instant.TimeToDisable.Verbatim"; |
154 // histogram from 1 minute to 10 days. | 155 // histogram from 1 minute to 10 days. |
155 UMA_HISTOGRAM_CUSTOM_COUNTS(name, delta.InMinutes(), 1, 60 * 24 * 10, 50); | 156 UMA_HISTOGRAM_CUSTOM_COUNTS(name, delta.InMinutes(), 1, 60 * 24 * 10, 50); |
156 } | 157 } |
157 | 158 |
158 | 159 void InstantController::Update(TabContentsWrapper* tab_contents, |
159 void InstantController::Update(TabContents* tab_contents, | |
160 const AutocompleteMatch& match, | 160 const AutocompleteMatch& match, |
161 const string16& user_text, | 161 const string16& user_text, |
162 string16* suggested_text) { | 162 string16* suggested_text) { |
163 if (tab_contents != tab_contents_) | 163 if (tab_contents != tab_contents_) |
164 DestroyPreviewContents(); | 164 DestroyPreviewContents(); |
165 | 165 |
166 const GURL& url = match.destination_url; | 166 const GURL& url = match.destination_url; |
167 | 167 |
168 tab_contents_ = tab_contents; | 168 tab_contents_ = tab_contents; |
169 commit_on_mouse_up_ = false; | 169 commit_on_mouse_up_ = false; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 } | 223 } |
224 | 224 |
225 bool InstantController::IsCurrent() { | 225 bool InstantController::IsCurrent() { |
226 return loader_manager_.get() && loader_manager_->active_loader()->ready() && | 226 return loader_manager_.get() && loader_manager_->active_loader()->ready() && |
227 !update_timer_.IsRunning(); | 227 !update_timer_.IsRunning(); |
228 } | 228 } |
229 | 229 |
230 void InstantController::CommitCurrentPreview(InstantCommitType type) { | 230 void InstantController::CommitCurrentPreview(InstantCommitType type) { |
231 DCHECK(loader_manager_.get()); | 231 DCHECK(loader_manager_.get()); |
232 DCHECK(loader_manager_->current_loader()); | 232 DCHECK(loader_manager_->current_loader()); |
233 TabContents* tab = ReleasePreviewContents(type); | 233 TabContentsWrapper* tab = ReleasePreviewContents(type); |
234 delegate_->CommitInstant(tab); | 234 delegate_->CommitInstant(tab); |
235 CompleteRelease(tab); | 235 CompleteRelease(tab->tab_contents()); |
236 } | 236 } |
237 | 237 |
238 void InstantController::SetCommitOnMouseUp() { | 238 void InstantController::SetCommitOnMouseUp() { |
239 commit_on_mouse_up_ = true; | 239 commit_on_mouse_up_ = true; |
240 } | 240 } |
241 | 241 |
242 bool InstantController::IsMouseDownFromActivate() { | 242 bool InstantController::IsMouseDownFromActivate() { |
243 DCHECK(loader_manager_.get()); | 243 DCHECK(loader_manager_.get()); |
244 DCHECK(loader_manager_->current_loader()); | 244 DCHECK(loader_manager_->current_loader()); |
245 return loader_manager_->current_loader()->IsMouseDownFromActivate(); | 245 return loader_manager_->current_loader()->IsMouseDownFromActivate(); |
246 } | 246 } |
247 | 247 |
248 void InstantController::OnAutocompleteLostFocus( | 248 void InstantController::OnAutocompleteLostFocus( |
249 gfx::NativeView view_gaining_focus) { | 249 gfx::NativeView view_gaining_focus) { |
250 if (!is_active() || !GetPreviewContents()) | 250 if (!is_active() || !GetPreviewContents()) |
251 return; | 251 return; |
252 | 252 |
253 RenderWidgetHostView* rwhv = | 253 RenderWidgetHostView* rwhv = |
254 GetPreviewContents()->GetRenderWidgetHostView(); | 254 GetPreviewContents()->tab_contents()->GetRenderWidgetHostView(); |
255 if (!view_gaining_focus || !rwhv) { | 255 if (!view_gaining_focus || !rwhv) { |
256 DestroyPreviewContents(); | 256 DestroyPreviewContents(); |
257 return; | 257 return; |
258 } | 258 } |
259 | 259 |
260 gfx::NativeView tab_view = GetPreviewContents()->GetNativeView(); | 260 gfx::NativeView tab_view = |
| 261 GetPreviewContents()->tab_contents()->GetNativeView(); |
261 // Focus is going to the renderer. | 262 // Focus is going to the renderer. |
262 if (rwhv->GetNativeView() == view_gaining_focus || | 263 if (rwhv->GetNativeView() == view_gaining_focus || |
263 tab_view == view_gaining_focus) { | 264 tab_view == view_gaining_focus) { |
264 if (!IsMouseDownFromActivate()) { | 265 if (!IsMouseDownFromActivate()) { |
265 // If the mouse is not down, focus is not going to the renderer. Someone | 266 // If the mouse is not down, focus is not going to the renderer. Someone |
266 // else moved focus and we shouldn't commit. | 267 // else moved focus and we shouldn't commit. |
267 DestroyPreviewContents(); | 268 DestroyPreviewContents(); |
268 return; | 269 return; |
269 } | 270 } |
270 | 271 |
(...skipping 21 matching lines...) Expand all Loading... |
292 } | 293 } |
293 | 294 |
294 if (view_gaining_focus_ancestor) { | 295 if (view_gaining_focus_ancestor) { |
295 CommitCurrentPreview(INSTANT_COMMIT_FOCUS_LOST); | 296 CommitCurrentPreview(INSTANT_COMMIT_FOCUS_LOST); |
296 return; | 297 return; |
297 } | 298 } |
298 | 299 |
299 DestroyPreviewContents(); | 300 DestroyPreviewContents(); |
300 } | 301 } |
301 | 302 |
302 TabContents* InstantController::ReleasePreviewContents(InstantCommitType type) { | 303 TabContentsWrapper* InstantController::ReleasePreviewContents( |
| 304 InstantCommitType type) { |
303 if (!loader_manager_.get()) | 305 if (!loader_manager_.get()) |
304 return NULL; | 306 return NULL; |
305 | 307 |
306 scoped_ptr<InstantLoader> loader(loader_manager_->ReleaseCurrentLoader()); | 308 scoped_ptr<InstantLoader> loader(loader_manager_->ReleaseCurrentLoader()); |
307 TabContents* tab = loader->ReleasePreviewContents(type); | 309 TabContentsWrapper* tab = loader->ReleasePreviewContents(type); |
308 | 310 |
309 ClearBlacklist(); | 311 ClearBlacklist(); |
310 is_active_ = false; | 312 is_active_ = false; |
311 omnibox_bounds_ = gfx::Rect(); | 313 omnibox_bounds_ = gfx::Rect(); |
312 commit_on_mouse_up_ = false; | 314 commit_on_mouse_up_ = false; |
313 loader_manager_.reset(NULL); | 315 loader_manager_.reset(NULL); |
314 update_timer_.Stop(); | 316 update_timer_.Stop(); |
315 return tab; | 317 return tab; |
316 } | 318 } |
317 | 319 |
318 void InstantController::CompleteRelease(TabContents* tab) { | 320 void InstantController::CompleteRelease(TabContents* tab) { |
319 tab->SetAllContentsBlocked(false); | 321 tab->SetAllContentsBlocked(false); |
320 } | 322 } |
321 | 323 |
322 TabContents* InstantController::GetPreviewContents() { | 324 TabContentsWrapper* InstantController::GetPreviewContents() { |
323 return loader_manager_.get() ? | 325 return loader_manager_.get() ? |
324 loader_manager_->current_loader()->preview_contents() : NULL; | 326 loader_manager_->current_loader()->preview_contents() : NULL; |
325 } | 327 } |
326 | 328 |
327 bool InstantController::IsShowingInstant() { | 329 bool InstantController::IsShowingInstant() { |
328 return loader_manager_.get() && | 330 return loader_manager_.get() && |
329 loader_manager_->current_loader()->is_showing_instant(); | 331 loader_manager_->current_loader()->is_showing_instant(); |
330 } | 332 } |
331 | 333 |
332 void InstantController::ShowInstantLoader(InstantLoader* loader) { | 334 void InstantController::ShowInstantLoader(InstantLoader* loader) { |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 template_url = model ? model->GetDefaultSearchProvider() : NULL; | 511 template_url = model ? model->GetDefaultSearchProvider() : NULL; |
510 } | 512 } |
511 if (template_url && template_url->id() && | 513 if (template_url && template_url->id() && |
512 template_url->instant_url() && | 514 template_url->instant_url() && |
513 !IsBlacklistedFromInstant(template_url->id()) && | 515 !IsBlacklistedFromInstant(template_url->id()) && |
514 template_url->instant_url()->SupportsReplacement()) { | 516 template_url->instant_url()->SupportsReplacement()) { |
515 return template_url; | 517 return template_url; |
516 } | 518 } |
517 return NULL; | 519 return NULL; |
518 } | 520 } |
OLD | NEW |