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/search/instant_controller.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/platform_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/instant_service.h"
13 #include "chrome/browser/search/instant_service_factory.h"
14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h"
16 #include "chrome/browser/ui/browser_instant_controller.h"
17 #include "chrome/browser/ui/search/instant_tab.h"
18 #include "chrome/browser/ui/search/search_tab_helper.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/search_urls.h"
21 #include "chrome/common/url_constants.h"
22 #include "components/search_engines/template_url_service.h"
23 #include "components/sessions/serialized_navigation_entry.h"
24 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/render_process_host.h"
27 #include "content/public/browser/render_widget_host_view.h"
28 #include "content/public/browser/web_contents.h"
29 #include "net/base/escape.h"
30 #include "net/base/network_change_notifier.h"
35 bool IsContentsFrom(const InstantPage
* page
,
36 const content::WebContents
* contents
) {
37 return page
&& (page
->web_contents() == contents
);
40 // Adds a transient NavigationEntry to the supplied |contents|'s
41 // NavigationController if the page's URL has not already been updated with the
42 // supplied |search_terms|. Sets the |search_terms| on the transient entry for
43 // search terms extraction to work correctly.
44 void EnsureSearchTermsAreSet(content::WebContents
* contents
,
45 const base::string16
& search_terms
) {
46 content::NavigationController
* controller
= &contents
->GetController();
48 // If search terms are already correct or there is already a transient entry
49 // (there shouldn't be), bail out early.
50 if (chrome::GetSearchTerms(contents
) == search_terms
||
51 controller
->GetTransientEntry())
54 const content::NavigationEntry
* entry
= controller
->GetLastCommittedEntry();
55 scoped_ptr
<content::NavigationEntry
> transient
=
56 controller
->CreateNavigationEntry(
59 entry
->GetTransitionType(),
62 contents
->GetBrowserContext());
63 transient
->SetExtraData(sessions::kSearchTermsKey
, search_terms
);
64 controller
->SetTransientEntry(transient
.Pass());
66 SearchTabHelper::FromWebContents(contents
)->NavigationEntryUpdated();
71 InstantController::InstantController(BrowserInstantController
* browser
)
75 InstantController::~InstantController() {
78 bool InstantController::SubmitQuery(const base::string16
& search_terms
,
79 const EmbeddedSearchRequestParams
& params
) {
80 if (instant_tab_
&& instant_tab_
->supports_instant() &&
81 search_mode_
.is_origin_search()) {
82 // Use |instant_tab_| to run the query if we're already on a search results
83 // page. (NOTE: in particular, we do not send the query to NTPs.)
84 SearchTabHelper::FromWebContents(instant_tab_
->web_contents())->Submit(
85 search_terms
, params
);
86 instant_tab_
->web_contents()->Focus();
87 EnsureSearchTermsAreSet(instant_tab_
->web_contents(), search_terms
);
93 void InstantController::SearchModeChanged(const SearchMode
& old_mode
,
94 const SearchMode
& new_mode
) {
95 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
96 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode
.origin
,
97 old_mode
.mode
, new_mode
.origin
, new_mode
.mode
));
99 search_mode_
= new_mode
;
103 void InstantController::ActiveTabChanged() {
104 LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged");
108 void InstantController::TabDeactivated(content::WebContents
* contents
) {
109 // If user is deactivating an NTP tab, log the number of mouseovers for this
111 if (chrome::IsInstantNTP(contents
))
112 InstantTab::EmitNtpStatistics(contents
);
115 void InstantController::LogDebugEvent(const std::string
& info
) const {
118 debug_events_
.push_front(std::make_pair(
119 base::Time::Now().ToInternalValue(), info
));
120 static const size_t kMaxDebugEventSize
= 2000;
121 if (debug_events_
.size() > kMaxDebugEventSize
)
122 debug_events_
.pop_back();
125 void InstantController::ClearDebugEvents() {
126 debug_events_
.clear();
129 Profile
* InstantController::profile() const {
130 return browser_
->profile();
133 InstantTab
* InstantController::instant_tab() const {
134 return instant_tab_
.get();
137 void InstantController::InstantSupportChanged(
138 InstantSupportState instant_support
) {
139 // Handle INSTANT_SUPPORT_YES here because InstantPage is not hooked up to the
140 // active tab. Search model changed listener in InstantPage will handle other
142 if (instant_support
!= INSTANT_SUPPORT_YES
)
148 void InstantController::InstantSupportDetermined(
149 const content::WebContents
* contents
,
150 bool supports_instant
) {
151 DCHECK(IsContentsFrom(instant_tab(), contents
));
153 if (!supports_instant
)
154 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, instant_tab_
.release());
156 content::NotificationService::current()->Notify(
157 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
158 content::Source
<InstantController
>(this),
159 content::NotificationService::NoDetails());
162 void InstantController::InstantPageAboutToNavigateMainFrame(
163 const content::WebContents
* contents
,
165 DCHECK(IsContentsFrom(instant_tab(), contents
));
167 // The Instant tab navigated. Send it the data it needs to display
169 UpdateInfoForInstantTab();
172 void InstantController::ResetInstantTab() {
173 if (!search_mode_
.is_origin_default()) {
174 content::WebContents
* active_tab
= browser_
->GetActiveWebContents();
175 if (!instant_tab_
|| active_tab
!= instant_tab_
->web_contents()) {
176 instant_tab_
.reset(new InstantTab(this, browser_
->profile()));
177 instant_tab_
->Init(active_tab
);
178 UpdateInfoForInstantTab();
181 instant_tab_
.reset();
185 void InstantController::UpdateInfoForInstantTab() {
187 // Update theme details.
188 InstantService
* instant_service
= GetInstantService();
189 if (instant_service
) {
190 instant_service
->UpdateThemeInfo();
191 instant_service
->UpdateMostVisitedItemsInfo();
196 InstantService
* InstantController::GetInstantService() const {
197 return InstantServiceFactory::GetForProfile(profile());