blob: 8a67894b864b72e0f6453ef659c760eb91138b5e [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"
[email protected]d57ce6a2014-07-03 15:39:2316#include "chrome/browser/ui/location_bar/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]a6827652012-11-20 23:41:0828
[email protected]c4b2af22014-05-11 19:48:5329
30// Helpers --------------------------------------------------------------------
[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]c4b2af22014-05-11 19:48:5343
44// BrowserInstantController ---------------------------------------------------
[email protected]7acfaf92012-07-11 15:51:5945
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]dcd0249872013-12-06 23:58:4576 const base::string16& search_terms =
[email protected]50da1902014-02-14 02:40:1977 chrome::ExtractSearchTermsFromURL(profile(), url);
[email protected]413558cb2013-06-10 16:44:4578 if (search_terms.empty())
79 return false;
80
[email protected]e97887c2013-12-11 01:27:3181 InstantSearchPrerenderer* prerenderer =
82 GetInstantSearchPrerenderer(profile());
[email protected]f028cf5b2014-01-24 07:36:0383 if (prerenderer) {
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);
88 return false;
89 } else {
90 prerenderer->Cancel();
91 }
[email protected]e97887c2013-12-11 01:27:3192 }
93
[email protected]50da1902014-02-14 02:40:1994 // If we will not be replacing search terms from this URL, don't send to
95 // InstantController.
96 if (!chrome::IsQueryExtractionAllowedForURL(profile(), url))
97 return false;
98
[email protected]413558cb2013-06-10 16:44:4599 return instant_.SubmitQuery(search_terms);
[email protected]7acfaf92012-07-11 15:51:59100}
101
[email protected]0c9406632013-02-08 01:13:33102Profile* BrowserInstantController::profile() const {
103 return browser_->profile();
104}
105
[email protected]cd533bf2012-12-04 19:14:59106content::WebContents* BrowserInstantController::GetActiveWebContents() const {
107 return browser_->tab_strip_model()->GetActiveWebContents();
[email protected]7acfaf92012-07-11 15:51:59108}
109
[email protected]e41982a72012-11-20 07:16:51110void BrowserInstantController::ActiveTabChanged() {
111 instant_.ActiveTabChanged();
112}
113
[email protected]3d6a8952012-12-14 03:18:07114void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
115 instant_.TabDeactivated(contents);
[email protected]e97887c2013-12-11 01:27:31116
117 InstantSearchPrerenderer* prerenderer =
118 GetInstantSearchPrerenderer(profile());
119 if (prerenderer)
120 prerenderer->Cancel();
[email protected]3d6a8952012-12-14 03:18:07121}
122
[email protected]5ee671f2013-03-19 11:23:05123void BrowserInstantController::ModelChanged(
[email protected]165fe422013-03-27 06:34:03124 const SearchModel::State& old_state,
125 const SearchModel::State& new_state) {
[email protected]4066a695d2013-06-20 14:08:54126 if (old_state.mode != new_state.mode) {
127 const SearchMode& new_mode = new_state.mode;
[email protected]5ee671f2013-03-19 11:23:05128
[email protected]a780c7b22013-08-02 18:36:59129 // Record some actions corresponding to the mode change. Note that to get
130 // the full story, it's necessary to look at other UMA actions as well,
131 // such as tab switches.
132 if (new_mode.is_search_results())
[email protected]c4b2af22014-05-11 19:48:53133 content::RecordAction(base::UserMetricsAction("InstantExtended.ShowSRP"));
[email protected]a780c7b22013-08-02 18:36:59134 else if (new_mode.is_ntp())
[email protected]c4b2af22014-05-11 19:48:53135 content::RecordAction(base::UserMetricsAction("InstantExtended.ShowNTP"));
[email protected]5ee671f2013-03-19 11:23:05136
[email protected]4066a695d2013-06-20 14:08:54137 instant_.SearchModeChanged(old_state.mode, new_mode);
[email protected]c19ba1042013-03-11 17:17:13138 }
139
[email protected]4066a695d2013-06-20 14:08:54140 if (old_state.instant_support != new_state.instant_support)
141 instant_.InstantSupportChanged(new_state.instant_support);
[email protected]7acfaf92012-07-11 15:51:59142}
143
[email protected]c8a118e2013-09-24 21:33:40144void BrowserInstantController::DefaultSearchProviderChanged() {
[email protected]0a46856e2013-04-24 00:33:02145 InstantService* instant_service =
[email protected]c8a118e2013-09-24 21:33:40146 InstantServiceFactory::GetForProfile(profile());
[email protected]0a46856e2013-04-24 00:33:02147 if (!instant_service)
148 return;
149
150 TabStripModel* tab_model = browser_->tab_strip_model();
151 int count = tab_model->count();
152 for (int index = 0; index < count; ++index) {
153 content::WebContents* contents = tab_model->GetWebContentsAt(index);
154 if (!contents)
155 continue;
156
[email protected]2309e912013-10-01 01:33:30157 // Send new search URLs to the renderer.
158 content::RenderProcessHost* rph = contents->GetRenderProcessHost();
159 instant_service->SendSearchURLsToRenderer(rph);
[email protected]0a46856e2013-04-24 00:33:02160
161 // Reload the contents to ensure that it gets assigned to a non-priviledged
162 // renderer.
[email protected]2309e912013-10-01 01:33:30163 if (!instant_service->IsInstantProcess(rph->GetID()))
164 continue;
[email protected]0a46856e2013-04-24 00:33:02165 contents->GetController().Reload(false);
166 }
[email protected]0a46856e2013-04-24 00:33:02167}