blob: 6cbab7281d2b8cf3f3e76570786f6b5837f94eef [file] [log] [blame]
[email protected]e41982a72012-11-20 07:16:511// Copyright 2012 The Chromium Authors. All rights reserved.
[email protected]7acfaf92012-07-11 15:51:592// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/browser_instant_controller.h"
6
[email protected]0a46856e2013-04-24 00:33:027#include "base/bind.h"
[email protected]7acfaf92012-07-11 15:51:598#include "chrome/browser/extensions/extension_service.h"
[email protected]26015ac2013-02-27 02:50:139#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]7acfaf92012-07-11 15:51:5910#include "chrome/browser/profiles/profile.h"
[email protected]0a46856e2013-04-24 00:33:0211#include "chrome/browser/search/instant_service.h"
12#include "chrome/browser/search/instant_service_factory.h"
[email protected]a7b8e43d2013-03-18 18:52:4313#include "chrome/browser/search/search.h"
[email protected]7acfaf92012-07-11 15:51:5914#include "chrome/browser/ui/browser.h"
[email protected]7acfaf92012-07-11 15:51:5915#include "chrome/browser/ui/browser_window.h"
16#include "chrome/browser/ui/omnibox/location_bar.h"
[email protected]6cf51b62013-08-10 13:49:2217#include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
[email protected]c18cb672012-12-05 04:42:1218#include "chrome/browser/ui/omnibox/omnibox_view.h"
[email protected]e97887c2013-12-11 01:27:3119#include "chrome/browser/ui/search/instant_search_prerenderer.h"
[email protected]4066a695d2013-06-20 14:08:5420#include "chrome/browser/ui/search/search_model.h"
[email protected]9d3d11702012-11-08 01:01:1221#include "chrome/browser/ui/search/search_tab_helper.h"
[email protected]e41982a72012-11-20 07:16:5122#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]7acfaf92012-07-11 15:51:5923#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
[email protected]0c9406632013-02-08 01:13:3324#include "chrome/common/url_constants.h"
[email protected]0a46856e2013-04-24 00:33:0225#include "content/public/browser/render_process_host.h"
[email protected]233f0f962013-02-27 21:14:1926#include "content/public/browser/user_metrics.h"
[email protected]0a46856e2013-04-24 00:33:0227#include "content/public/browser/web_contents.h"
[email protected]6cf51b62013-08-10 13:49:2228#include "content/public/browser/web_contents_view.h"
[email protected]a6827652012-11-20 23:41:0829
[email protected]e6e30ac2014-01-13 21:24:3930using base::UserMetricsAction;
[email protected]233f0f962013-02-27 21:14:1931
[email protected]e97887c2013-12-11 01:27:3132namespace {
33
34InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) {
35 DCHECK(profile);
36 InstantService* instant_service =
37 InstantServiceFactory::GetForProfile(profile);
38 return instant_service ? instant_service->instant_search_prerenderer() : NULL;
39}
40
41} // namespace
42
[email protected]7acfaf92012-07-11 15:51:5943////////////////////////////////////////////////////////////////////////////////
44// BrowserInstantController, public:
45
46BrowserInstantController::BrowserInstantController(Browser* browser)
[email protected]8a236702012-09-28 13:30:5747 : browser_(browser),
[email protected]777590052014-01-17 22:11:5448 instant_(this) {
[email protected]0b10c9ff2012-10-09 17:31:5549 browser_->search_model()->AddObserver(this);
[email protected]4ff347e2013-07-22 19:39:0050
51 InstantService* instant_service =
52 InstantServiceFactory::GetForProfile(profile());
[email protected]c8a118e2013-09-24 21:33:4053 instant_service->AddObserver(this);
[email protected]7acfaf92012-07-11 15:51:5954}
55
56BrowserInstantController::~BrowserInstantController() {
[email protected]0b10c9ff2012-10-09 17:31:5557 browser_->search_model()->RemoveObserver(this);
[email protected]4ff347e2013-07-22 19:39:0058
59 InstantService* instant_service =
60 InstantServiceFactory::GetForProfile(profile());
[email protected]c8a118e2013-09-24 21:33:4061 instant_service->RemoveObserver(this);
[email protected]0c9406632013-02-08 01:13:3362}
63
[email protected]413558cb2013-06-10 16:44:4564bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
65 const GURL& url) {
[email protected]e41982a72012-11-20 07:16:5166 // Unsupported dispositions.
[email protected]413558cb2013-06-10 16:44:4567 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
68 disposition == NEW_FOREGROUND_TAB)
[email protected]7acfaf92012-07-11 15:51:5969 return false;
[email protected]7acfaf92012-07-11 15:51:5970
[email protected]7acfaf92012-07-11 15:51:5971 // The omnibox currently doesn't use other dispositions, so we don't attempt
[email protected]c72226c82012-10-01 21:02:3272 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
[email protected]7acfaf92012-07-11 15:51:5973 // support for the new disposition.
[email protected]413558cb2013-06-10 16:44:4574 DCHECK(disposition == CURRENT_TAB) << disposition;
[email protected]c72226c82012-10-01 21:02:3275
[email protected]413558cb2013-06-10 16:44:4576 // If we will not be replacing search terms from this URL, don't send to
77 // InstantController.
[email protected]dcd0249872013-12-06 23:58:4578 const base::string16& search_terms =
[email protected]413558cb2013-06-10 16:44:4579 chrome::GetSearchTermsFromURL(browser_->profile(), url);
80 if (search_terms.empty())
81 return false;
82
[email protected]e97887c2013-12-11 01:27:3183 InstantSearchPrerenderer* prerenderer =
84 GetInstantSearchPrerenderer(profile());
[email protected]f028cf5b2014-01-24 07:36:0385 if (prerenderer) {
86 if (prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) {
87 // Submit query to render the prefetched results. Browser will swap the
88 // prerendered contents with the active tab contents.
89 prerenderer->Commit(search_terms);
90 return false;
91 } else {
92 prerenderer->Cancel();
93 }
[email protected]e97887c2013-12-11 01:27:3194 }
95
[email protected]413558cb2013-06-10 16:44:4596 return instant_.SubmitQuery(search_terms);
[email protected]7acfaf92012-07-11 15:51:5997}
98
[email protected]0c9406632013-02-08 01:13:3399Profile* BrowserInstantController::profile() const {
100 return browser_->profile();
101}
102
[email protected]cd533bf2012-12-04 19:14:59103content::WebContents* BrowserInstantController::GetActiveWebContents() const {
104 return browser_->tab_strip_model()->GetActiveWebContents();
[email protected]7acfaf92012-07-11 15:51:59105}
106
[email protected]e41982a72012-11-20 07:16:51107void BrowserInstantController::ActiveTabChanged() {
108 instant_.ActiveTabChanged();
109}
110
[email protected]3d6a8952012-12-14 03:18:07111void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
112 instant_.TabDeactivated(contents);
[email protected]e97887c2013-12-11 01:27:31113
114 InstantSearchPrerenderer* prerenderer =
115 GetInstantSearchPrerenderer(profile());
116 if (prerenderer)
117 prerenderer->Cancel();
[email protected]3d6a8952012-12-14 03:18:07118}
119
[email protected]fcde79a2013-02-28 02:25:09120void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) {
121 instant_.SetOmniboxBounds(bounds);
[email protected]ec4aad542012-12-14 01:11:04122}
123
[email protected]7acfaf92012-07-11 15:51:59124////////////////////////////////////////////////////////////////////////////////
[email protected]165fe422013-03-27 06:34:03125// BrowserInstantController, SearchModelObserver implementation:
[email protected]0b10c9ff2012-10-09 17:31:55126
[email protected]5ee671f2013-03-19 11:23:05127void BrowserInstantController::ModelChanged(
[email protected]165fe422013-03-27 06:34:03128 const SearchModel::State& old_state,
129 const SearchModel::State& new_state) {
[email protected]4066a695d2013-06-20 14:08:54130 if (old_state.mode != new_state.mode) {
131 const SearchMode& new_mode = new_state.mode;
[email protected]5ee671f2013-03-19 11:23:05132
[email protected]a780c7b22013-08-02 18:36:59133 // Record some actions corresponding to the mode change. Note that to get
134 // the full story, it's necessary to look at other UMA actions as well,
135 // such as tab switches.
136 if (new_mode.is_search_results())
137 content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP"));
138 else if (new_mode.is_ntp())
139 content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP"));
[email protected]5ee671f2013-03-19 11:23:05140
[email protected]4066a695d2013-06-20 14:08:54141 instant_.SearchModeChanged(old_state.mode, new_mode);
[email protected]c19ba1042013-03-11 17:17:13142 }
143
[email protected]4066a695d2013-06-20 14:08:54144 if (old_state.instant_support != new_state.instant_support)
145 instant_.InstantSupportChanged(new_state.instant_support);
[email protected]7acfaf92012-07-11 15:51:59146}
147
[email protected]c8a118e2013-09-24 21:33:40148////////////////////////////////////////////////////////////////////////////////
149// BrowserInstantController, InstantServiceObserver implementation:
[email protected]0a46856e2013-04-24 00:33:02150
[email protected]c8a118e2013-09-24 21:33:40151void BrowserInstantController::DefaultSearchProviderChanged() {
152 ReloadTabsInInstantProcess();
153}
[email protected]0a46856e2013-04-24 00:33:02154
[email protected]c8a118e2013-09-24 21:33:40155void BrowserInstantController::GoogleURLUpdated() {
156 ReloadTabsInInstantProcess();
157}
158
159void BrowserInstantController::ReloadTabsInInstantProcess() {
[email protected]0a46856e2013-04-24 00:33:02160 InstantService* instant_service =
[email protected]c8a118e2013-09-24 21:33:40161 InstantServiceFactory::GetForProfile(profile());
[email protected]0a46856e2013-04-24 00:33:02162 if (!instant_service)
163 return;
164
165 TabStripModel* tab_model = browser_->tab_strip_model();
166 int count = tab_model->count();
167 for (int index = 0; index < count; ++index) {
168 content::WebContents* contents = tab_model->GetWebContentsAt(index);
169 if (!contents)
170 continue;
171
[email protected]2309e912013-10-01 01:33:30172 // Send new search URLs to the renderer.
173 content::RenderProcessHost* rph = contents->GetRenderProcessHost();
174 instant_service->SendSearchURLsToRenderer(rph);
[email protected]0a46856e2013-04-24 00:33:02175
176 // Reload the contents to ensure that it gets assigned to a non-priviledged
177 // renderer.
[email protected]2309e912013-10-01 01:33:30178 if (!instant_service->IsInstantProcess(rph->GetID()))
179 continue;
[email protected]0a46856e2013-04-24 00:33:02180 contents->GetController().Reload(false);
181 }
[email protected]0a46856e2013-04-24 00:33:02182}