| OLD | NEW | 
|    1 // Copyright 2012 The Chromium Authors. All rights reserved. |    1 // Copyright 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/metrics/histogram.h" |    8 #include "base/metrics/histogram.h" | 
|    9 #include "base/string_util.h" |    9 #include "base/string_util.h" | 
|   10 #include "base/utf_string_conversions.h" |   10 #include "base/utf_string_conversions.h" | 
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  145  |  145  | 
|  146 InstantController::InstantController(chrome::BrowserInstantController* browser, |  146 InstantController::InstantController(chrome::BrowserInstantController* browser, | 
|  147                                      bool extended_enabled) |  147                                      bool extended_enabled) | 
|  148     : browser_(browser), |  148     : browser_(browser), | 
|  149       extended_enabled_(extended_enabled), |  149       extended_enabled_(extended_enabled), | 
|  150       instant_enabled_(false), |  150       instant_enabled_(false), | 
|  151       model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |  151       model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 
|  152       last_verbatim_(false), |  152       last_verbatim_(false), | 
|  153       last_transition_type_(content::PAGE_TRANSITION_LINK), |  153       last_transition_type_(content::PAGE_TRANSITION_LINK), | 
|  154       last_match_was_search_(false), |  154       last_match_was_search_(false), | 
|  155       is_omnibox_focused_(false) { |  155       omnibox_focus_state_(NONE) { | 
|  156 } |  156 } | 
|  157  |  157  | 
|  158 InstantController::~InstantController() { |  158 InstantController::~InstantController() { | 
|  159 } |  159 } | 
|  160  |  160  | 
|  161 bool InstantController::Update(const AutocompleteMatch& match, |  161 bool InstantController::Update(const AutocompleteMatch& match, | 
|  162                                const string16& user_text, |  162                                const string16& user_text, | 
|  163                                const string16& full_text, |  163                                const string16& full_text, | 
|  164                                const bool verbatim, |  164                                const bool verbatim, | 
|  165                                const bool user_input_in_progress, |  165                                const bool user_input_in_progress, | 
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  469  |  469  | 
|  470   // Try to create another loader immediately so that it is ready for the next |  470   // Try to create another loader immediately so that it is ready for the next | 
|  471   // user interaction. |  471   // user interaction. | 
|  472   CreateDefaultLoader(); |  472   CreateDefaultLoader(); | 
|  473  |  473  | 
|  474   return true; |  474   return true; | 
|  475 } |  475 } | 
|  476  |  476  | 
|  477 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { |  477 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { | 
|  478   DVLOG(1) << "OmniboxLostFocus"; |  478   DVLOG(1) << "OmniboxLostFocus"; | 
|  479   is_omnibox_focused_ = false; |  479   omnibox_focus_state_ = NONE; | 
 |  480   SendKeyCaptureModeToPage(); | 
|  480  |  481  | 
|  481   if (!extended_enabled_ && !instant_enabled_) |  482   if (!extended_enabled_ && !instant_enabled_) | 
|  482     return; |  483     return; | 
|  483  |  484  | 
|  484   // If the preview isn't showing search suggestions, nothing to do. The check |  485   // If the preview isn't showing search suggestions, nothing to do. The check | 
|  485   // for GetPreviewContents() (which normally is redundant, given IsCurrent()) |  486   // for GetPreviewContents() (which normally is redundant, given IsCurrent()) | 
|  486   // is to handle the case when we get here during a commit. |  487   // is to handle the case when we get here during a commit. | 
|  487   if (!IsCurrent() || !GetPreviewContents()) { |  488   if (!IsCurrent() || !GetPreviewContents()) { | 
|  488     OnStaleLoader(); |  489     OnStaleLoader(); | 
|  489     return; |  490     return; | 
|  490   } |  491   } | 
|  491  |  492  | 
|  492 #if defined(OS_MACOSX) |  493 #if defined(OS_MACOSX) | 
|  493   if (!loader_->IsPointerDownFromActivate()) |  494   if (!loader_->IsPointerDownFromActivate()) | 
|  494     Hide(true); |  495     Hide(true); | 
|  495 #else |  496 #else | 
|  496   if (IsViewInContents(GetViewGainingFocus(view_gaining_focus), |  497   if (IsViewInContents(GetViewGainingFocus(view_gaining_focus), | 
|  497                        GetPreviewContents()->web_contents())) |  498                        GetPreviewContents()->web_contents())) | 
|  498     CommitIfCurrent(INSTANT_COMMIT_FOCUS_LOST); |  499     CommitIfCurrent(INSTANT_COMMIT_FOCUS_LOST); | 
|  499   else |  500   else | 
|  500     Hide(true); |  501     Hide(true); | 
|  501 #endif |  502 #endif | 
|  502 } |  503 } | 
|  503  |  504  | 
|  504 void InstantController::OmniboxGotFocus() { |  505 void InstantController::OmniboxGotFocus(bool focus_is_visible) { | 
|  505   DVLOG(1) << "OmniboxGotFocus"; |  506   DVLOG(1) << "OmniboxGotFocus"; | 
|  506   is_omnibox_focused_ = true; |  507   omnibox_focus_state_ = focus_is_visible ? VISIBLE : INVISIBLE; | 
 |  508   SendKeyCaptureModeToPage(); | 
|  507  |  509  | 
|  508   if (!extended_enabled_ && !instant_enabled_) |  510   if (!extended_enabled_ && !instant_enabled_) | 
|  509     return; |  511     return; | 
|  510  |  512  | 
|  511   if (!GetPreviewContents()) |  513   if (!GetPreviewContents()) | 
|  512     CreateDefaultLoader(); |  514     CreateDefaultLoader(); | 
|  513 } |  515 } | 
|  514  |  516  | 
|  515 void InstantController::SearchModeChanged( |  517 void InstantController::SearchModeChanged( | 
|  516     const chrome::search::Mode& old_mode, |  518     const chrome::search::Mode& old_mode, | 
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  700   if (!GetInstantURL(template_url, &instant_url)) |  702   if (!GetInstantURL(template_url, &instant_url)) | 
|  701     return false; |  703     return false; | 
|  702  |  704  | 
|  703   if (GetPreviewContents() && loader_->instant_url() != instant_url) |  705   if (GetPreviewContents() && loader_->instant_url() != instant_url) | 
|  704     DeleteLoader(); |  706     DeleteLoader(); | 
|  705  |  707  | 
|  706   if (!GetPreviewContents()) { |  708   if (!GetPreviewContents()) { | 
|  707     loader_.reset(new InstantLoader(this, instant_url, active_tab)); |  709     loader_.reset(new InstantLoader(this, instant_url, active_tab)); | 
|  708     loader_->Init(); |  710     loader_->Init(); | 
|  709  |  711  | 
|  710     // Ensure the searchbox API has the correct theme-related info and context. |  712     // Ensure the searchbox API has correct state. | 
|  711     if (extended_enabled_) { |  713     if (extended_enabled_) { | 
|  712       browser_->UpdateThemeInfoForPreview(); |  714       browser_->UpdateThemeInfoForPreview(); | 
|  713       loader_->SearchModeChanged(search_mode_); |  715       loader_->SearchModeChanged(search_mode_); | 
 |  716       SendKeyCaptureModeToPage(); | 
|  714     } |  717     } | 
|  715  |  718  | 
|  716     // Reset the loader timer. |  719     // Reset the loader timer. | 
|  717     stale_loader_timer_.Start( |  720     stale_loader_timer_.Start( | 
|  718         FROM_HERE, |  721         FROM_HERE, | 
|  719         base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, |  722         base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, | 
|  720         &InstantController::OnStaleLoader); |  723         &InstantController::OnStaleLoader); | 
|  721   } |  724   } | 
|  722  |  725  | 
|  723   return true; |  726   return true; | 
|  724 } |  727 } | 
|  725  |  728  | 
|  726 bool InstantController::CreateDefaultLoader() { |  729 bool InstantController::CreateDefaultLoader() { | 
|  727   // If there's no active tab, the browser is closing. |  730   // If there's no active tab, the browser is closing. | 
|  728   const TabContents* active_tab = browser_->GetActiveTabContents(); |  731   const TabContents* active_tab = browser_->GetActiveTabContents(); | 
|  729   if (!active_tab) |  732   if (!active_tab) | 
|  730     return false; |  733     return false; | 
|  731  |  734  | 
|  732   const TemplateURL* template_url = |  735   const TemplateURL* template_url = | 
|  733       TemplateURLServiceFactory::GetForProfile(active_tab->profile())-> |  736       TemplateURLServiceFactory::GetForProfile(active_tab->profile())-> | 
|  734                                  GetDefaultSearchProvider(); |  737                                  GetDefaultSearchProvider(); | 
|  735  |  738  | 
|  736   return ResetLoader(template_url, active_tab); |  739   return ResetLoader(template_url, active_tab); | 
|  737 } |  740 } | 
|  738  |  741  | 
|  739 void InstantController::OnStaleLoader() { |  742 void InstantController::OnStaleLoader() { | 
|  740   // If the preview is showing or the omnibox has focus, don't delete the |  743   // If the preview is showing or the omnibox has focus, don't delete the | 
|  741   // loader. It will get refreshed the next time the preview is hidden or the |  744   // loader. It will get refreshed the next time the preview is hidden or the | 
|  742   // omnibox loses focus. |  745   // omnibox loses focus. | 
|  743   if (!stale_loader_timer_.IsRunning() && !is_omnibox_focused_ && |  746   if (!stale_loader_timer_.IsRunning() && omnibox_focus_state_ == NONE && | 
|  744       model_.mode().is_default()) { |  747       model_.mode().is_default()) { | 
|  745     DeleteLoader(); |  748     DeleteLoader(); | 
|  746     CreateDefaultLoader(); |  749     CreateDefaultLoader(); | 
|  747   } |  750   } | 
|  748 } |  751 } | 
|  749  |  752  | 
|  750 void InstantController::DeleteLoader() { |  753 void InstantController::DeleteLoader() { | 
|  751   // Clear all state, except |last_transition_type_| as it's used during commit. |  754   // Clear all state, except |last_transition_type_| as it's used during commit. | 
|  752   last_user_text_.clear(); |  755   last_user_text_.clear(); | 
|  753   last_full_text_.clear(); |  756   last_full_text_.clear(); | 
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  855   // There is no point in sanity-checking |intersection.y()| because the omnibox |  858   // There is no point in sanity-checking |intersection.y()| because the omnibox | 
|  856   // can be placed anywhere vertically relative to the preview (for example, in |  859   // can be placed anywhere vertically relative to the preview (for example, in | 
|  857   // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds). |  860   // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds). | 
|  858   DCHECK_LE(0, intersection.x()); |  861   DCHECK_LE(0, intersection.x()); | 
|  859   DCHECK_LE(0, intersection.width()); |  862   DCHECK_LE(0, intersection.width()); | 
|  860   DCHECK_LE(0, intersection.height()); |  863   DCHECK_LE(0, intersection.height()); | 
|  861  |  864  | 
|  862   loader_->SetOmniboxBounds(intersection); |  865   loader_->SetOmniboxBounds(intersection); | 
|  863 } |  866 } | 
|  864  |  867  | 
 |  868 void InstantController::SendKeyCaptureModeToPage() { | 
 |  869   if (extended_enabled_ && GetPreviewContents()) | 
 |  870     loader_->OnKeyCaptureChange(omnibox_focus_state_ == INVISIBLE); | 
 |  871 } | 
 |  872  | 
|  865 bool InstantController::GetInstantURL(const TemplateURL* template_url, |  873 bool InstantController::GetInstantURL(const TemplateURL* template_url, | 
|  866                                       std::string* instant_url) const { |  874                                       std::string* instant_url) const { | 
|  867   CommandLine* command_line = CommandLine::ForCurrentProcess(); |  875   CommandLine* command_line = CommandLine::ForCurrentProcess(); | 
|  868   if (command_line->HasSwitch(switches::kInstantURL)) { |  876   if (command_line->HasSwitch(switches::kInstantURL)) { | 
|  869     *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL); |  877     *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL); | 
|  870     return template_url != NULL; |  878     return template_url != NULL; | 
|  871   } |  879   } | 
|  872  |  880  | 
|  873   if (!template_url) |  881   if (!template_url) | 
|  874     return false; |  882     return false; | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  906   } |  914   } | 
|  907  |  915  | 
|  908   std::map<std::string, int>::const_iterator iter = |  916   std::map<std::string, int>::const_iterator iter = | 
|  909       blacklisted_urls_.find(*instant_url); |  917       blacklisted_urls_.find(*instant_url); | 
|  910   if (iter != blacklisted_urls_.end() && |  918   if (iter != blacklisted_urls_.end() && | 
|  911       iter->second > kMaxInstantSupportFailures) |  919       iter->second > kMaxInstantSupportFailures) | 
|  912     return false; |  920     return false; | 
|  913  |  921  | 
|  914   return true; |  922   return true; | 
|  915 } |  923 } | 
| OLD | NEW |