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/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_web_ui.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/instant_service.h"
13 #include "chrome/browser/search/instant_service_factory.h"
14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/location_bar/location_bar.h"
18 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
19 #include "chrome/browser/ui/omnibox/omnibox_view.h"
20 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
21 #include "chrome/browser/ui/search/search_model.h"
22 #include "chrome/browser/ui/search/search_tab_helper.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
25 #include "chrome/common/url_constants.h"
26 #include "content/public/browser/render_process_host.h"
27 #include "content/public/browser/user_metrics.h"
28 #include "content/public/browser/web_contents.h"
31 // Helpers --------------------------------------------------------------------
35 InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) {
37 InstantService* instant_service =
38 InstantServiceFactory::GetForProfile(profile);
39 return instant_service ? instant_service->instant_search_prerenderer() : NULL;
45 // BrowserInstantController ---------------------------------------------------
47 BrowserInstantController::BrowserInstantController(Browser* browser)
50 browser_->search_model()->AddObserver(this);
52 InstantService* instant_service =
53 InstantServiceFactory::GetForProfile(profile());
54 instant_service->AddObserver(this);
57 BrowserInstantController::~BrowserInstantController() {
58 browser_->search_model()->RemoveObserver(this);
60 InstantService* instant_service =
61 InstantServiceFactory::GetForProfile(profile());
62 instant_service->RemoveObserver(this);
65 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
67 // Unsupported dispositions.
68 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
69 disposition == NEW_FOREGROUND_TAB)
72 // The omnibox currently doesn't use other dispositions, so we don't attempt
73 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
74 // support for the new disposition.
75 DCHECK(disposition == CURRENT_TAB) << disposition;
77 const base::string16& search_terms =
78 chrome::ExtractSearchTermsFromURL(profile(), url);
79 if (search_terms.empty())
82 InstantSearchPrerenderer* prerenderer =
83 GetInstantSearchPrerenderer(profile());
85 if (prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) {
86 // Submit query to render the prefetched results. Browser will swap the
87 // prerendered contents with the active tab contents.
88 prerenderer->Commit(search_terms);
91 prerenderer->Cancel();
95 // If we will not be replacing search terms from this URL, don't send to
97 if (!chrome::IsQueryExtractionAllowedForURL(profile(), url))
100 return instant_.SubmitQuery(search_terms);
103 Profile* BrowserInstantController::profile() const {
104 return browser_->profile();
107 content::WebContents* BrowserInstantController::GetActiveWebContents() const {
108 return browser_->tab_strip_model()->GetActiveWebContents();
111 void BrowserInstantController::ActiveTabChanged() {
112 instant_.ActiveTabChanged();
115 void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
116 instant_.TabDeactivated(contents);
118 InstantSearchPrerenderer* prerenderer =
119 GetInstantSearchPrerenderer(profile());
121 prerenderer->Cancel();
124 void BrowserInstantController::ModelChanged(
125 const SearchModel::State& old_state,
126 const SearchModel::State& new_state) {
127 if (old_state.mode != new_state.mode) {
128 const SearchMode& new_mode = new_state.mode;
130 // Record some actions corresponding to the mode change. Note that to get
131 // the full story, it's necessary to look at other UMA actions as well,
132 // such as tab switches.
133 if (new_mode.is_search_results())
134 content::RecordAction(base::UserMetricsAction("InstantExtended.ShowSRP"));
135 else if (new_mode.is_ntp())
136 content::RecordAction(base::UserMetricsAction("InstantExtended.ShowNTP"));
138 instant_.SearchModeChanged(old_state.mode, new_mode);
141 if (old_state.instant_support != new_state.instant_support)
142 instant_.InstantSupportChanged(new_state.instant_support);
145 void BrowserInstantController::DefaultSearchProviderChanged() {
146 InstantService* instant_service =
147 InstantServiceFactory::GetForProfile(profile());
148 if (!instant_service)
151 TabStripModel* tab_model = browser_->tab_strip_model();
152 int count = tab_model->count();
153 for (int index = 0; index < count; ++index) {
154 content::WebContents* contents = tab_model->GetWebContentsAt(index);
158 // Send new search URLs to the renderer.
159 content::RenderProcessHost* rph = contents->GetRenderProcessHost();
160 instant_service->SendSearchURLsToRenderer(rph);
162 // Reload the contents to ensure that it gets assigned to a non-priviledged
164 if (!instant_service->IsInstantProcess(rph->GetID()))
167 contents->GetController().Reload(false);
169 // As the reload was not triggered by the user we don't want to close any
170 // infobars. We have to tell the InfoBarService after the reload, otherwise
171 // it would ignore this call when
172 // WebContentsObserver::DidStartNavigationToPendingEntry is invoked.
173 InfoBarService::FromWebContents(contents)->set_ignore_next_reload();