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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/browser_instant_controller.h"
8 #include "chrome/browser/infobars/infobar_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/search/instant_service.h"
11 #include "chrome/browser/search/instant_service_factory.h"
12 #include "chrome/browser/search/search.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/location_bar/location_bar.h"
16 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
17 #include "chrome/browser/ui/omnibox/omnibox_view.h"
18 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
19 #include "chrome/browser/ui/search/search_model.h"
20 #include "chrome/browser/ui/search/search_tab_helper.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/instant_types.h"
23 #include "chrome/common/url_constants.h"
24 #include "content/public/browser/render_process_host.h"
25 #include "content/public/browser/user_metrics.h"
26 #include "content/public/browser/web_contents.h"
29 // Helpers --------------------------------------------------------------------
33 InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) {
35 InstantService* instant_service =
36 InstantServiceFactory::GetForProfile(profile);
37 return instant_service ? instant_service->instant_search_prerenderer() : NULL;
43 // BrowserInstantController ---------------------------------------------------
45 BrowserInstantController::BrowserInstantController(Browser* browser)
48 browser_->search_model()->AddObserver(this);
50 InstantService* instant_service =
51 InstantServiceFactory::GetForProfile(profile());
52 instant_service->AddObserver(this);
55 BrowserInstantController::~BrowserInstantController() {
56 browser_->search_model()->RemoveObserver(this);
58 InstantService* instant_service =
59 InstantServiceFactory::GetForProfile(profile());
60 instant_service->RemoveObserver(this);
63 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
65 // Unsupported dispositions.
66 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
67 disposition == NEW_FOREGROUND_TAB)
70 // The omnibox currently doesn't use other dispositions, so we don't attempt
71 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
72 // support for the new disposition.
73 DCHECK(disposition == CURRENT_TAB) << disposition;
75 const base::string16& search_terms =
76 chrome::ExtractSearchTermsFromURL(profile(), url);
77 EmbeddedSearchRequestParams request_params(url);
78 if (search_terms.empty())
81 InstantSearchPrerenderer* prerenderer =
82 GetInstantSearchPrerenderer(profile());
84 if (prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) {
85 // Submit query to render the prefetched results. Browser will swap the
86 // prerendered contents with the active tab contents.
87 prerenderer->Commit(search_terms, request_params);
90 prerenderer->Cancel();
94 // If we will not be replacing search terms from this URL, don't send to
96 if (!chrome::IsQueryExtractionAllowedForURL(profile(), url))
98 return instant_.SubmitQuery(search_terms, request_params);
101 Profile* BrowserInstantController::profile() const {
102 return browser_->profile();
105 content::WebContents* BrowserInstantController::GetActiveWebContents() const {
106 return browser_->tab_strip_model()->GetActiveWebContents();
109 void BrowserInstantController::ActiveTabChanged() {
110 instant_.ActiveTabChanged();
113 void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
114 instant_.TabDeactivated(contents);
116 InstantSearchPrerenderer* prerenderer =
117 GetInstantSearchPrerenderer(profile());
119 prerenderer->Cancel();
122 void BrowserInstantController::ModelChanged(
123 const SearchModel::State& old_state,
124 const SearchModel::State& new_state) {
125 if (old_state.mode != new_state.mode) {
126 const SearchMode& new_mode = new_state.mode;
128 // Record some actions corresponding to the mode change. Note that to get
129 // the full story, it's necessary to look at other UMA actions as well,
130 // such as tab switches.
131 if (new_mode.is_search_results())
132 content::RecordAction(base::UserMetricsAction("InstantExtended.ShowSRP"));
133 else if (new_mode.is_ntp())
134 content::RecordAction(base::UserMetricsAction("InstantExtended.ShowNTP"));
136 instant_.SearchModeChanged(old_state.mode, new_mode);
139 if (old_state.instant_support != new_state.instant_support)
140 instant_.InstantSupportChanged(new_state.instant_support);
143 void BrowserInstantController::DefaultSearchProviderChanged() {
144 InstantService* instant_service =
145 InstantServiceFactory::GetForProfile(profile());
146 if (!instant_service)
149 TabStripModel* tab_model = browser_->tab_strip_model();
150 int count = tab_model->count();
151 for (int index = 0; index < count; ++index) {
152 content::WebContents* contents = tab_model->GetWebContentsAt(index);
156 // Send new search URLs to the renderer.
157 content::RenderProcessHost* rph = contents->GetRenderProcessHost();
158 instant_service->SendSearchURLsToRenderer(rph);
160 // Reload the contents to ensure that it gets assigned to a non-priviledged
162 if (!instant_service->IsInstantProcess(rph->GetID()))
165 contents->GetController().Reload(false);
167 // As the reload was not triggered by the user we don't want to close any
168 // infobars. We have to tell the InfoBarService after the reload, otherwise
169 // it would ignore this call when
170 // WebContentsObserver::DidStartNavigationToPendingEntry is invoked.
171 InfoBarService::FromWebContents(contents)->set_ignore_next_reload();