blob: 4544ae9b1ba660e15e58e1f843abacaef334f086 [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]e97887c2013-12-11 01:27:3120#include "chrome/browser/ui/search/instant_search_prerenderer.h"
[email protected]4066a695d2013-06-20 14:08:5421#include "chrome/browser/ui/search/search_model.h"
[email protected]9d3d11702012-11-08 01:01:1222#include "chrome/browser/ui/search/search_tab_helper.h"
[email protected]e41982a72012-11-20 07:16:5123#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]7acfaf92012-07-11 15:51:5924#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
[email protected]0c9406632013-02-08 01:13:3325#include "chrome/common/url_constants.h"
[email protected]0a46856e2013-04-24 00:33:0226#include "content/public/browser/render_process_host.h"
[email protected]233f0f962013-02-27 21:14:1927#include "content/public/browser/user_metrics.h"
[email protected]0a46856e2013-04-24 00:33:0228#include "content/public/browser/web_contents.h"
[email protected]6cf51b62013-08-10 13:49:2229#include "content/public/browser/web_contents_view.h"
[email protected]a6827652012-11-20 23:41:0830
[email protected]233f0f962013-02-27 21:14:1931using content::UserMetricsAction;
32
[email protected]e97887c2013-12-11 01:27:3133namespace {
34
35InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) {
36 DCHECK(profile);
37 InstantService* instant_service =
38 InstantServiceFactory::GetForProfile(profile);
39 return instant_service ? instant_service->instant_search_prerenderer() : NULL;
40}
41
42} // namespace
43
[email protected]7acfaf92012-07-11 15:51:5944////////////////////////////////////////////////////////////////////////////////
45// BrowserInstantController, public:
46
47BrowserInstantController::BrowserInstantController(Browser* browser)
[email protected]8a236702012-09-28 13:30:5748 : browser_(browser),
[email protected]a780c7b22013-08-02 18:36:5949 instant_(this),
[email protected]6af41782013-06-22 13:49:1150 instant_unload_handler_(browser) {
[email protected]0b10c9ff2012-10-09 17:31:5551 browser_->search_model()->AddObserver(this);
[email protected]4ff347e2013-07-22 19:39:0052
53 InstantService* instant_service =
54 InstantServiceFactory::GetForProfile(profile());
55 instant_service->OnBrowserInstantControllerCreated();
[email protected]c8a118e2013-09-24 21:33:4056 instant_service->AddObserver(this);
[email protected]7acfaf92012-07-11 15:51:5957}
58
59BrowserInstantController::~BrowserInstantController() {
[email protected]0b10c9ff2012-10-09 17:31:5560 browser_->search_model()->RemoveObserver(this);
[email protected]4ff347e2013-07-22 19:39:0061
62 InstantService* instant_service =
63 InstantServiceFactory::GetForProfile(profile());
[email protected]c8a118e2013-09-24 21:33:4064 instant_service->RemoveObserver(this);
[email protected]4ff347e2013-07-22 19:39:0065 instant_service->OnBrowserInstantControllerDestroyed();
[email protected]7acfaf92012-07-11 15:51:5966}
67
[email protected]0c9406632013-02-08 01:13:3368bool BrowserInstantController::MaybeSwapInInstantNTPContents(
69 const GURL& url,
70 content::WebContents* source_contents,
71 content::WebContents** target_contents) {
72 if (url != GURL(chrome::kChromeUINewTabURL))
73 return false;
74
[email protected]26015ac2013-02-27 02:50:1375 GURL extension_url(url);
76 if (ExtensionWebUI::HandleChromeURLOverride(&extension_url, profile())) {
77 // If there is an extension overriding the NTP do not use the Instant NTP.
78 return false;
79 }
80
[email protected]4ff347e2013-07-22 19:39:0081 InstantService* instant_service =
82 InstantServiceFactory::GetForProfile(profile());
83 scoped_ptr<content::WebContents> instant_ntp =
84 instant_service->ReleaseNTPContents();
[email protected]0c9406632013-02-08 01:13:3385 if (!instant_ntp)
86 return false;
87
88 *target_contents = instant_ntp.get();
[email protected]0c9406632013-02-08 01:13:3389 if (source_contents) {
[email protected]474f8512013-05-31 22:31:1690 // If the Instant NTP hasn't yet committed an entry, we can't call
91 // CopyStateFromAndPrune. Instead, load the Local NTP URL directly in the
92 // source contents.
93 // TODO(sreeram): Always using the local URL is wrong in the case of the
94 // first tab in a window where we might want to use the remote URL. Fix.
[email protected]79368982013-11-13 01:11:0195 if (!instant_ntp->GetController().CanPruneAllButLastCommitted()) {
[email protected]474f8512013-05-31 22:31:1696 source_contents->GetController().LoadURL(chrome::GetLocalInstantURL(
97 profile()), content::Referrer(), content::PAGE_TRANSITION_GENERATED,
98 std::string());
99 *target_contents = source_contents;
100 } else {
101 instant_ntp->GetController().CopyStateFromAndPrune(
102 &source_contents->GetController());
103 ReplaceWebContentsAt(
104 browser_->tab_strip_model()->GetIndexOfWebContents(source_contents),
105 instant_ntp.Pass());
106 }
[email protected]0c9406632013-02-08 01:13:33107 } else {
[email protected]474f8512013-05-31 22:31:16108 // If the Instant NTP hasn't yet committed an entry, we can't call
[email protected]79368982013-11-13 01:11:01109 // PruneAllButLastCommitted. In that case, there shouldn't be any entries
110 // to prune anyway.
111 if (instant_ntp->GetController().CanPruneAllButLastCommitted())
112 instant_ntp->GetController().PruneAllButLastCommitted();
[email protected]474f8512013-05-31 22:31:16113 else
114 CHECK(!instant_ntp->GetController().GetLastCommittedEntry());
115
[email protected]0c9406632013-02-08 01:13:33116 // If |source_contents| is NULL, then the caller is responsible for
117 // inserting instant_ntp into the tabstrip and will take ownership.
118 ignore_result(instant_ntp.release());
119 }
[email protected]0c9406632013-02-08 01:13:33120 return true;
121}
122
[email protected]413558cb2013-06-10 16:44:45123bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
124 const GURL& url) {
[email protected]e41982a72012-11-20 07:16:51125 // Unsupported dispositions.
[email protected]413558cb2013-06-10 16:44:45126 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
127 disposition == NEW_FOREGROUND_TAB)
[email protected]7acfaf92012-07-11 15:51:59128 return false;
[email protected]7acfaf92012-07-11 15:51:59129
[email protected]7acfaf92012-07-11 15:51:59130 // The omnibox currently doesn't use other dispositions, so we don't attempt
[email protected]c72226c82012-10-01 21:02:32131 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
[email protected]7acfaf92012-07-11 15:51:59132 // support for the new disposition.
[email protected]413558cb2013-06-10 16:44:45133 DCHECK(disposition == CURRENT_TAB) << disposition;
[email protected]c72226c82012-10-01 21:02:32134
[email protected]413558cb2013-06-10 16:44:45135 // If we will not be replacing search terms from this URL, don't send to
136 // InstantController.
[email protected]dcd0249872013-12-06 23:58:45137 const base::string16& search_terms =
[email protected]413558cb2013-06-10 16:44:45138 chrome::GetSearchTermsFromURL(browser_->profile(), url);
139 if (search_terms.empty())
140 return false;
141
[email protected]e97887c2013-12-11 01:27:31142 InstantSearchPrerenderer* prerenderer =
143 GetInstantSearchPrerenderer(profile());
144 if (prerenderer &&
145 prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) {
146 // Submit query to render the prefetched results. Browser will swap the
147 // prerendered contents with the active tab contents.
148 prerenderer->Commit(search_terms);
149 return false;
150 }
151
[email protected]413558cb2013-06-10 16:44:45152 return instant_.SubmitQuery(search_terms);
[email protected]7acfaf92012-07-11 15:51:59153}
154
[email protected]0c9406632013-02-08 01:13:33155Profile* BrowserInstantController::profile() const {
156 return browser_->profile();
157}
158
[email protected]0c9406632013-02-08 01:13:33159void BrowserInstantController::ReplaceWebContentsAt(
160 int index,
161 scoped_ptr<content::WebContents> new_contents) {
162 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]d572bfd2013-02-14 06:14:20163 scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()->
164 ReplaceWebContentsAt(index, new_contents.release()));
165 instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(),
166 index);
[email protected]0c9406632013-02-08 01:13:33167}
168
[email protected]cd533bf2012-12-04 19:14:59169content::WebContents* BrowserInstantController::GetActiveWebContents() const {
170 return browser_->tab_strip_model()->GetActiveWebContents();
[email protected]7acfaf92012-07-11 15:51:59171}
172
[email protected]e41982a72012-11-20 07:16:51173void BrowserInstantController::ActiveTabChanged() {
174 instant_.ActiveTabChanged();
175}
176
[email protected]3d6a8952012-12-14 03:18:07177void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
178 instant_.TabDeactivated(contents);
[email protected]e97887c2013-12-11 01:27:31179
180 InstantSearchPrerenderer* prerenderer =
181 GetInstantSearchPrerenderer(profile());
182 if (prerenderer)
183 prerenderer->Cancel();
[email protected]3d6a8952012-12-14 03:18:07184}
185
[email protected]fcde79a2013-02-28 02:25:09186void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) {
187 instant_.SetOmniboxBounds(bounds);
[email protected]ec4aad542012-12-14 01:11:04188}
189
[email protected]7acfaf92012-07-11 15:51:59190////////////////////////////////////////////////////////////////////////////////
[email protected]165fe422013-03-27 06:34:03191// BrowserInstantController, SearchModelObserver implementation:
[email protected]0b10c9ff2012-10-09 17:31:55192
[email protected]5ee671f2013-03-19 11:23:05193void BrowserInstantController::ModelChanged(
[email protected]165fe422013-03-27 06:34:03194 const SearchModel::State& old_state,
195 const SearchModel::State& new_state) {
[email protected]4066a695d2013-06-20 14:08:54196 if (old_state.mode != new_state.mode) {
197 const SearchMode& new_mode = new_state.mode;
[email protected]5ee671f2013-03-19 11:23:05198
[email protected]a780c7b22013-08-02 18:36:59199 // Record some actions corresponding to the mode change. Note that to get
200 // the full story, it's necessary to look at other UMA actions as well,
201 // such as tab switches.
202 if (new_mode.is_search_results())
203 content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP"));
204 else if (new_mode.is_ntp())
205 content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP"));
[email protected]5ee671f2013-03-19 11:23:05206
[email protected]4066a695d2013-06-20 14:08:54207 instant_.SearchModeChanged(old_state.mode, new_mode);
[email protected]c19ba1042013-03-11 17:17:13208 }
209
[email protected]4066a695d2013-06-20 14:08:54210 if (old_state.instant_support != new_state.instant_support)
211 instant_.InstantSupportChanged(new_state.instant_support);
[email protected]7acfaf92012-07-11 15:51:59212}
213
[email protected]c8a118e2013-09-24 21:33:40214////////////////////////////////////////////////////////////////////////////////
215// BrowserInstantController, InstantServiceObserver implementation:
[email protected]0a46856e2013-04-24 00:33:02216
[email protected]c8a118e2013-09-24 21:33:40217void BrowserInstantController::DefaultSearchProviderChanged() {
218 ReloadTabsInInstantProcess();
219}
[email protected]0a46856e2013-04-24 00:33:02220
[email protected]c8a118e2013-09-24 21:33:40221void BrowserInstantController::GoogleURLUpdated() {
222 ReloadTabsInInstantProcess();
223}
224
225void BrowserInstantController::ReloadTabsInInstantProcess() {
[email protected]0a46856e2013-04-24 00:33:02226 InstantService* instant_service =
[email protected]c8a118e2013-09-24 21:33:40227 InstantServiceFactory::GetForProfile(profile());
[email protected]0a46856e2013-04-24 00:33:02228 if (!instant_service)
229 return;
230
231 TabStripModel* tab_model = browser_->tab_strip_model();
232 int count = tab_model->count();
233 for (int index = 0; index < count; ++index) {
234 content::WebContents* contents = tab_model->GetWebContentsAt(index);
235 if (!contents)
236 continue;
237
[email protected]2309e912013-10-01 01:33:30238 // Send new search URLs to the renderer.
239 content::RenderProcessHost* rph = contents->GetRenderProcessHost();
240 instant_service->SendSearchURLsToRenderer(rph);
[email protected]0a46856e2013-04-24 00:33:02241
242 // Reload the contents to ensure that it gets assigned to a non-priviledged
243 // renderer.
[email protected]2309e912013-10-01 01:33:30244 if (!instant_service->IsInstantProcess(rph->GetID()))
245 continue;
[email protected]0a46856e2013-04-24 00:33:02246 contents->GetController().Reload(false);
247 }
[email protected]0a46856e2013-04-24 00:33:02248}