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/profiles/profile.h"
11 #include "chrome/browser/search/instant_service.h"
12 #include "chrome/browser/search/instant_service_factory.h"
13 #include "chrome/browser/search/search.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/omnibox/location_bar.h"
17 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
18 #include "chrome/browser/ui/omnibox/omnibox_view.h"
19 #include "chrome/browser/ui/search/instant_ntp.h"
20 #include "chrome/browser/ui/search/search_model.h"
21 #include "chrome/browser/ui/search/search_tab_helper.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
24 #include "chrome/common/url_constants.h"
25 #include "content/public/browser/render_process_host.h"
26 #include "content/public/browser/user_metrics.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_contents_view.h"
30 using content::UserMetricsAction;
32 ////////////////////////////////////////////////////////////////////////////////
33 // BrowserInstantController, public:
35 BrowserInstantController::BrowserInstantController(Browser* browser)
38 instant_unload_handler_(browser) {
39 browser_->search_model()->AddObserver(this);
41 InstantService* instant_service =
42 InstantServiceFactory::GetForProfile(profile());
43 instant_service->OnBrowserInstantControllerCreated();
44 instant_service->AddObserver(this);
47 BrowserInstantController::~BrowserInstantController() {
48 browser_->search_model()->RemoveObserver(this);
50 InstantService* instant_service =
51 InstantServiceFactory::GetForProfile(profile());
52 instant_service->RemoveObserver(this);
53 instant_service->OnBrowserInstantControllerDestroyed();
56 bool BrowserInstantController::MaybeSwapInInstantNTPContents(
58 content::WebContents* source_contents,
59 content::WebContents** target_contents) {
60 if (url != GURL(chrome::kChromeUINewTabURL))
63 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.
69 InstantService* instant_service =
70 InstantServiceFactory::GetForProfile(profile());
71 scoped_ptr<content::WebContents> instant_ntp =
72 instant_service->ReleaseNTPContents();
76 *target_contents = instant_ntp.get();
77 if (source_contents) {
78 // 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
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.
83 if (!instant_ntp->GetController().CanPruneAllButVisible()) {
84 source_contents->GetController().LoadURL(chrome::GetLocalInstantURL(
85 profile()), content::Referrer(), content::PAGE_TRANSITION_GENERATED,
87 *target_contents = source_contents;
89 instant_ntp->GetController().CopyStateFromAndPrune(
90 &source_contents->GetController());
92 browser_->tab_strip_model()->GetIndexOfWebContents(source_contents),
96 // If the Instant NTP hasn't yet committed an entry, we can't call
97 // PruneAllButVisible. In that case, there shouldn't be any entries to
99 if (instant_ntp->GetController().CanPruneAllButVisible())
100 instant_ntp->GetController().PruneAllButVisible();
102 CHECK(!instant_ntp->GetController().GetLastCommittedEntry());
104 // 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());
111 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
113 // Unsupported dispositions.
114 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
115 disposition == NEW_FOREGROUND_TAB)
118 // The omnibox currently doesn't use other dispositions, so we don't attempt
119 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
120 // support for the new disposition.
121 DCHECK(disposition == CURRENT_TAB) << disposition;
123 // If we will not be replacing search terms from this URL, don't send to
124 // InstantController.
125 const string16& search_terms =
126 chrome::GetSearchTermsFromURL(browser_->profile(), url);
127 if (search_terms.empty())
130 return instant_.SubmitQuery(search_terms);
133 Profile* BrowserInstantController::profile() const {
134 return browser_->profile();
137 void BrowserInstantController::ReplaceWebContentsAt(
139 scoped_ptr<content::WebContents> new_contents) {
140 DCHECK_NE(TabStripModel::kNoTab, index);
141 scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()->
142 ReplaceWebContentsAt(index, new_contents.release()));
143 instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(),
147 content::WebContents* BrowserInstantController::GetActiveWebContents() const {
148 return browser_->tab_strip_model()->GetActiveWebContents();
151 void BrowserInstantController::ActiveTabChanged() {
152 instant_.ActiveTabChanged();
155 void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
156 instant_.TabDeactivated(contents);
159 void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) {
160 instant_.SetOmniboxBounds(bounds);
163 void BrowserInstantController::ToggleVoiceSearch() {
164 instant_.ToggleVoiceSearch();
167 ////////////////////////////////////////////////////////////////////////////////
168 // BrowserInstantController, SearchModelObserver implementation:
170 void BrowserInstantController::ModelChanged(
171 const SearchModel::State& old_state,
172 const SearchModel::State& new_state) {
173 if (old_state.mode != new_state.mode) {
174 const SearchMode& new_mode = new_state.mode;
176 // Record some actions corresponding to the mode change. Note that to get
177 // the full story, it's necessary to look at other UMA actions as well,
178 // such as tab switches.
179 if (new_mode.is_search_results())
180 content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP"));
181 else if (new_mode.is_ntp())
182 content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP"));
184 instant_.SearchModeChanged(old_state.mode, new_mode);
187 if (old_state.instant_support != new_state.instant_support)
188 instant_.InstantSupportChanged(new_state.instant_support);
191 ////////////////////////////////////////////////////////////////////////////////
192 // BrowserInstantController, InstantServiceObserver implementation:
194 void BrowserInstantController::DefaultSearchProviderChanged() {
195 ReloadTabsInInstantProcess();
198 void BrowserInstantController::GoogleURLUpdated() {
199 ReloadTabsInInstantProcess();
202 void BrowserInstantController::ReloadTabsInInstantProcess() {
203 InstantService* instant_service =
204 InstantServiceFactory::GetForProfile(profile());
205 if (!instant_service)
208 TabStripModel* tab_model = browser_->tab_strip_model();
209 int count = tab_model->count();
210 for (int index = 0; index < count; ++index) {
211 content::WebContents* contents = tab_model->GetWebContentsAt(index);
215 // Send new search URLs to the renderer.
216 content::RenderProcessHost* rph = contents->GetRenderProcessHost();
217 instant_service->SendSearchURLsToRenderer(rph);
219 // Reload the contents to ensure that it gets assigned to a non-priviledged
221 if (!instant_service->IsInstantProcess(rph->GetID()))
223 contents->GetController().Reload(false);