blob: b30da97d3810e3470b2624eb3badaef61ca134a8 [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]4ff347e2013-07-22 19:39:0019#include "chrome/browser/ui/search/instant_ntp.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]233f0f962013-02-27 21:14:1930using content::UserMetricsAction;
31
[email protected]7acfaf92012-07-11 15:51:5932////////////////////////////////////////////////////////////////////////////////
33// BrowserInstantController, public:
34
35BrowserInstantController::BrowserInstantController(Browser* browser)
[email protected]8a236702012-09-28 13:30:5736 : browser_(browser),
[email protected]a780c7b22013-08-02 18:36:5937 instant_(this),
[email protected]6af41782013-06-22 13:49:1138 instant_unload_handler_(browser) {
[email protected]0b10c9ff2012-10-09 17:31:5539 browser_->search_model()->AddObserver(this);
[email protected]4ff347e2013-07-22 19:39:0040
41 InstantService* instant_service =
42 InstantServiceFactory::GetForProfile(profile());
43 instant_service->OnBrowserInstantControllerCreated();
[email protected]c8a118e2013-09-24 21:33:4044 instant_service->AddObserver(this);
[email protected]7acfaf92012-07-11 15:51:5945}
46
47BrowserInstantController::~BrowserInstantController() {
[email protected]0b10c9ff2012-10-09 17:31:5548 browser_->search_model()->RemoveObserver(this);
[email protected]4ff347e2013-07-22 19:39:0049
50 InstantService* instant_service =
51 InstantServiceFactory::GetForProfile(profile());
[email protected]c8a118e2013-09-24 21:33:4052 instant_service->RemoveObserver(this);
[email protected]4ff347e2013-07-22 19:39:0053 instant_service->OnBrowserInstantControllerDestroyed();
[email protected]7acfaf92012-07-11 15:51:5954}
55
[email protected]0c9406632013-02-08 01:13:3356bool BrowserInstantController::MaybeSwapInInstantNTPContents(
57 const GURL& url,
58 content::WebContents* source_contents,
59 content::WebContents** target_contents) {
60 if (url != GURL(chrome::kChromeUINewTabURL))
61 return false;
62
[email protected]26015ac2013-02-27 02:50:1363 GURL extension_url(url);
64 if (ExtensionWebUI::HandleChromeURLOverride(&extension_url, profile())) {
65 // If there is an extension overriding the NTP do not use the Instant NTP.
66 return false;
67 }
68
[email protected]4ff347e2013-07-22 19:39:0069 InstantService* instant_service =
70 InstantServiceFactory::GetForProfile(profile());
71 scoped_ptr<content::WebContents> instant_ntp =
72 instant_service->ReleaseNTPContents();
[email protected]0c9406632013-02-08 01:13:3373 if (!instant_ntp)
74 return false;
75
76 *target_contents = instant_ntp.get();
[email protected]0c9406632013-02-08 01:13:3377 if (source_contents) {
[email protected]474f8512013-05-31 22:31:1678 // If the Instant NTP hasn't yet committed an entry, we can't call
79 // CopyStateFromAndPrune. Instead, load the Local NTP URL directly in the
80 // source contents.
81 // TODO(sreeram): Always using the local URL is wrong in the case of the
82 // first tab in a window where we might want to use the remote URL. Fix.
[email protected]79368982013-11-13 01:11:0183 if (!instant_ntp->GetController().CanPruneAllButLastCommitted()) {
[email protected]474f8512013-05-31 22:31:1684 source_contents->GetController().LoadURL(chrome::GetLocalInstantURL(
85 profile()), content::Referrer(), content::PAGE_TRANSITION_GENERATED,
86 std::string());
87 *target_contents = source_contents;
88 } else {
89 instant_ntp->GetController().CopyStateFromAndPrune(
90 &source_contents->GetController());
91 ReplaceWebContentsAt(
92 browser_->tab_strip_model()->GetIndexOfWebContents(source_contents),
93 instant_ntp.Pass());
94 }
[email protected]0c9406632013-02-08 01:13:3395 } else {
[email protected]474f8512013-05-31 22:31:1696 // If the Instant NTP hasn't yet committed an entry, we can't call
[email protected]79368982013-11-13 01:11:0197 // PruneAllButLastCommitted. In that case, there shouldn't be any entries
98 // to prune anyway.
99 if (instant_ntp->GetController().CanPruneAllButLastCommitted())
100 instant_ntp->GetController().PruneAllButLastCommitted();
[email protected]474f8512013-05-31 22:31:16101 else
102 CHECK(!instant_ntp->GetController().GetLastCommittedEntry());
103
[email protected]0c9406632013-02-08 01:13:33104 // If |source_contents| is NULL, then the caller is responsible for
105 // inserting instant_ntp into the tabstrip and will take ownership.
106 ignore_result(instant_ntp.release());
107 }
[email protected]0c9406632013-02-08 01:13:33108 return true;
109}
110
[email protected]413558cb2013-06-10 16:44:45111bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
112 const GURL& url) {
[email protected]e41982a72012-11-20 07:16:51113 // Unsupported dispositions.
[email protected]413558cb2013-06-10 16:44:45114 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
115 disposition == NEW_FOREGROUND_TAB)
[email protected]7acfaf92012-07-11 15:51:59116 return false;
[email protected]7acfaf92012-07-11 15:51:59117
[email protected]7acfaf92012-07-11 15:51:59118 // The omnibox currently doesn't use other dispositions, so we don't attempt
[email protected]c72226c82012-10-01 21:02:32119 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
[email protected]7acfaf92012-07-11 15:51:59120 // support for the new disposition.
[email protected]413558cb2013-06-10 16:44:45121 DCHECK(disposition == CURRENT_TAB) << disposition;
[email protected]c72226c82012-10-01 21:02:32122
[email protected]413558cb2013-06-10 16:44:45123 // If we will not be replacing search terms from this URL, don't send to
124 // InstantController.
[email protected]dcd0249872013-12-06 23:58:45125 const base::string16& search_terms =
[email protected]413558cb2013-06-10 16:44:45126 chrome::GetSearchTermsFromURL(browser_->profile(), url);
127 if (search_terms.empty())
128 return false;
129
130 return instant_.SubmitQuery(search_terms);
[email protected]7acfaf92012-07-11 15:51:59131}
132
[email protected]0c9406632013-02-08 01:13:33133Profile* BrowserInstantController::profile() const {
134 return browser_->profile();
135}
136
[email protected]0c9406632013-02-08 01:13:33137void BrowserInstantController::ReplaceWebContentsAt(
138 int index,
139 scoped_ptr<content::WebContents> new_contents) {
140 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]d572bfd2013-02-14 06:14:20141 scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()->
142 ReplaceWebContentsAt(index, new_contents.release()));
143 instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(),
144 index);
[email protected]0c9406632013-02-08 01:13:33145}
146
[email protected]cd533bf2012-12-04 19:14:59147content::WebContents* BrowserInstantController::GetActiveWebContents() const {
148 return browser_->tab_strip_model()->GetActiveWebContents();
[email protected]7acfaf92012-07-11 15:51:59149}
150
[email protected]e41982a72012-11-20 07:16:51151void BrowserInstantController::ActiveTabChanged() {
152 instant_.ActiveTabChanged();
153}
154
[email protected]3d6a8952012-12-14 03:18:07155void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
156 instant_.TabDeactivated(contents);
157}
158
[email protected]fcde79a2013-02-28 02:25:09159void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) {
160 instant_.SetOmniboxBounds(bounds);
[email protected]ec4aad542012-12-14 01:11:04161}
162
[email protected]7acfaf92012-07-11 15:51:59163////////////////////////////////////////////////////////////////////////////////
[email protected]165fe422013-03-27 06:34:03164// BrowserInstantController, SearchModelObserver implementation:
[email protected]0b10c9ff2012-10-09 17:31:55165
[email protected]5ee671f2013-03-19 11:23:05166void BrowserInstantController::ModelChanged(
[email protected]165fe422013-03-27 06:34:03167 const SearchModel::State& old_state,
168 const SearchModel::State& new_state) {
[email protected]4066a695d2013-06-20 14:08:54169 if (old_state.mode != new_state.mode) {
170 const SearchMode& new_mode = new_state.mode;
[email protected]5ee671f2013-03-19 11:23:05171
[email protected]a780c7b22013-08-02 18:36:59172 // Record some actions corresponding to the mode change. Note that to get
173 // the full story, it's necessary to look at other UMA actions as well,
174 // such as tab switches.
175 if (new_mode.is_search_results())
176 content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP"));
177 else if (new_mode.is_ntp())
178 content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP"));
[email protected]5ee671f2013-03-19 11:23:05179
[email protected]4066a695d2013-06-20 14:08:54180 instant_.SearchModeChanged(old_state.mode, new_mode);
[email protected]c19ba1042013-03-11 17:17:13181 }
182
[email protected]4066a695d2013-06-20 14:08:54183 if (old_state.instant_support != new_state.instant_support)
184 instant_.InstantSupportChanged(new_state.instant_support);
[email protected]7acfaf92012-07-11 15:51:59185}
186
[email protected]c8a118e2013-09-24 21:33:40187////////////////////////////////////////////////////////////////////////////////
188// BrowserInstantController, InstantServiceObserver implementation:
[email protected]0a46856e2013-04-24 00:33:02189
[email protected]c8a118e2013-09-24 21:33:40190void BrowserInstantController::DefaultSearchProviderChanged() {
191 ReloadTabsInInstantProcess();
192}
[email protected]0a46856e2013-04-24 00:33:02193
[email protected]c8a118e2013-09-24 21:33:40194void BrowserInstantController::GoogleURLUpdated() {
195 ReloadTabsInInstantProcess();
196}
197
198void BrowserInstantController::ReloadTabsInInstantProcess() {
[email protected]0a46856e2013-04-24 00:33:02199 InstantService* instant_service =
[email protected]c8a118e2013-09-24 21:33:40200 InstantServiceFactory::GetForProfile(profile());
[email protected]0a46856e2013-04-24 00:33:02201 if (!instant_service)
202 return;
203
204 TabStripModel* tab_model = browser_->tab_strip_model();
205 int count = tab_model->count();
206 for (int index = 0; index < count; ++index) {
207 content::WebContents* contents = tab_model->GetWebContentsAt(index);
208 if (!contents)
209 continue;
210
[email protected]2309e912013-10-01 01:33:30211 // Send new search URLs to the renderer.
212 content::RenderProcessHost* rph = contents->GetRenderProcessHost();
213 instant_service->SendSearchURLsToRenderer(rph);
[email protected]0a46856e2013-04-24 00:33:02214
215 // Reload the contents to ensure that it gets assigned to a non-priviledged
216 // renderer.
[email protected]2309e912013-10-01 01:33:30217 if (!instant_service->IsInstantProcess(rph->GetID()))
218 continue;
[email protected]0a46856e2013-04-24 00:33:02219 contents->GetController().Reload(false);
220 }
[email protected]0a46856e2013-04-24 00:33:02221}