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_search_prerenderer.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"
29 using base::UserMetricsAction
;
33 InstantSearchPrerenderer
* GetInstantSearchPrerenderer(Profile
* profile
) {
35 InstantService
* instant_service
=
36 InstantServiceFactory::GetForProfile(profile
);
37 return instant_service
? instant_service
->instant_search_prerenderer() : NULL
;
42 ////////////////////////////////////////////////////////////////////////////////
43 // BrowserInstantController, public:
45 BrowserInstantController::BrowserInstantController(Browser
* browser
)
48 browser_
->search_model()->AddObserver(this);
50 InstantService
* instant_service
=
51 InstantServiceFactory::GetForProfile(profile());
52 instant_service
->AddObserver(this);
55 BrowserInstantController::~BrowserInstantController() {
56 browser_
->search_model()->RemoveObserver(this);
58 InstantService
* instant_service
=
59 InstantServiceFactory::GetForProfile(profile());
60 instant_service
->RemoveObserver(this);
63 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition
,
65 // Unsupported dispositions.
66 if (disposition
== NEW_BACKGROUND_TAB
|| disposition
== NEW_WINDOW
||
67 disposition
== NEW_FOREGROUND_TAB
)
70 // The omnibox currently doesn't use other dispositions, so we don't attempt
71 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
72 // support for the new disposition.
73 DCHECK(disposition
== CURRENT_TAB
) << disposition
;
75 const base::string16
& search_terms
=
76 chrome::ExtractSearchTermsFromURL(profile(), url
);
77 if (search_terms
.empty())
80 InstantSearchPrerenderer
* prerenderer
=
81 GetInstantSearchPrerenderer(profile());
83 if (prerenderer
->CanCommitQuery(GetActiveWebContents(), search_terms
)) {
84 // Submit query to render the prefetched results. Browser will swap the
85 // prerendered contents with the active tab contents.
86 prerenderer
->Commit(search_terms
);
89 prerenderer
->Cancel();
93 // If we will not be replacing search terms from this URL, don't send to
95 if (!chrome::IsQueryExtractionAllowedForURL(profile(), url
))
98 return instant_
.SubmitQuery(search_terms
);
101 Profile
* BrowserInstantController::profile() const {
102 return browser_
->profile();
105 content::WebContents
* BrowserInstantController::GetActiveWebContents() const {
106 return browser_
->tab_strip_model()->GetActiveWebContents();
109 void BrowserInstantController::ActiveTabChanged() {
110 instant_
.ActiveTabChanged();
113 void BrowserInstantController::TabDeactivated(content::WebContents
* contents
) {
114 instant_
.TabDeactivated(contents
);
116 InstantSearchPrerenderer
* prerenderer
=
117 GetInstantSearchPrerenderer(profile());
119 prerenderer
->Cancel();
122 ////////////////////////////////////////////////////////////////////////////////
123 // BrowserInstantController, SearchModelObserver implementation:
125 void BrowserInstantController::ModelChanged(
126 const SearchModel::State
& old_state
,
127 const SearchModel::State
& new_state
) {
128 if (old_state
.mode
!= new_state
.mode
) {
129 const SearchMode
& new_mode
= new_state
.mode
;
131 // Record some actions corresponding to the mode change. Note that to get
132 // the full story, it's necessary to look at other UMA actions as well,
133 // such as tab switches.
134 if (new_mode
.is_search_results())
135 content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP"));
136 else if (new_mode
.is_ntp())
137 content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP"));
139 instant_
.SearchModeChanged(old_state
.mode
, new_mode
);
142 if (old_state
.instant_support
!= new_state
.instant_support
)
143 instant_
.InstantSupportChanged(new_state
.instant_support
);
146 ////////////////////////////////////////////////////////////////////////////////
147 // BrowserInstantController, InstantServiceObserver implementation:
149 void BrowserInstantController::DefaultSearchProviderChanged() {
150 ReloadTabsInInstantProcess();
153 void BrowserInstantController::GoogleURLUpdated() {
154 ReloadTabsInInstantProcess();
157 void BrowserInstantController::ReloadTabsInInstantProcess() {
158 InstantService
* instant_service
=
159 InstantServiceFactory::GetForProfile(profile());
160 if (!instant_service
)
163 TabStripModel
* tab_model
= browser_
->tab_strip_model();
164 int count
= tab_model
->count();
165 for (int index
= 0; index
< count
; ++index
) {
166 content::WebContents
* contents
= tab_model
->GetWebContentsAt(index
);
170 // Send new search URLs to the renderer.
171 content::RenderProcessHost
* rph
= contents
->GetRenderProcessHost();
172 instant_service
->SendSearchURLsToRenderer(rph
);
174 // Reload the contents to ensure that it gets assigned to a non-priviledged
176 if (!instant_service
->IsInstantProcess(rph
->GetID()))
178 contents
->GetController().Reload(false);