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/pref_names.h"
21 #include "chrome/common/search_urls.h"
22 #include "chrome/common/url_constants.h"
23 #include "components/search_engines/template_url_service.h"
24 #include "components/sessions/serialized_navigation_entry.h"
25 #include "content/public/browser/navigation_entry.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/render_process_host.h"
28 #include "content/public/browser/render_widget_host_view.h"
29 #include "content/public/browser/web_contents.h"
30 #include "net/base/escape.h"
31 #include "net/base/network_change_notifier.h"
34 #if defined(TOOLKIT_VIEWS)
35 #include "ui/views/widget/widget.h"
40 bool IsContentsFrom(const InstantPage
* page
,
41 const content::WebContents
* contents
) {
42 return page
&& (page
->web_contents() == contents
);
45 // Adds a transient NavigationEntry to the supplied |contents|'s
46 // NavigationController if the page's URL has not already been updated with the
47 // supplied |search_terms|. Sets the |search_terms| on the transient entry for
48 // search terms extraction to work correctly.
49 void EnsureSearchTermsAreSet(content::WebContents
* contents
,
50 const base::string16
& search_terms
) {
51 content::NavigationController
* controller
= &contents
->GetController();
53 // If search terms are already correct or there is already a transient entry
54 // (there shouldn't be), bail out early.
55 if (chrome::GetSearchTerms(contents
) == search_terms
||
56 controller
->GetTransientEntry())
59 const content::NavigationEntry
* entry
= controller
->GetLastCommittedEntry();
60 content::NavigationEntry
* transient
= controller
->CreateNavigationEntry(
63 entry
->GetTransitionType(),
66 contents
->GetBrowserContext());
67 transient
->SetExtraData(sessions::kSearchTermsKey
, search_terms
);
68 controller
->SetTransientEntry(transient
);
70 SearchTabHelper::FromWebContents(contents
)->NavigationEntryUpdated();
75 InstantController::InstantController(BrowserInstantController
* browser
)
79 InstantController::~InstantController() {
82 bool InstantController::SubmitQuery(const base::string16
& search_terms
) {
83 if (instant_tab_
&& instant_tab_
->supports_instant() &&
84 search_mode_
.is_origin_search()) {
85 // Use |instant_tab_| to run the query if we're already on a search results
86 // page. (NOTE: in particular, we do not send the query to NTPs.)
87 SearchTabHelper::FromWebContents(instant_tab_
->web_contents())->Submit(
89 instant_tab_
->web_contents()->Focus();
90 EnsureSearchTermsAreSet(instant_tab_
->web_contents(), search_terms
);
96 void InstantController::SearchModeChanged(const SearchMode
& old_mode
,
97 const SearchMode
& new_mode
) {
98 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
99 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode
.origin
,
100 old_mode
.mode
, new_mode
.origin
, new_mode
.mode
));
102 search_mode_
= new_mode
;
106 void InstantController::ActiveTabChanged() {
107 LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged");
111 void InstantController::TabDeactivated(content::WebContents
* contents
) {
112 // If user is deactivating an NTP tab, log the number of mouseovers for this
114 if (chrome::IsInstantNTP(contents
))
115 InstantTab::EmitNtpStatistics(contents
);
118 void InstantController::LogDebugEvent(const std::string
& info
) const {
121 debug_events_
.push_front(std::make_pair(
122 base::Time::Now().ToInternalValue(), info
));
123 static const size_t kMaxDebugEventSize
= 2000;
124 if (debug_events_
.size() > kMaxDebugEventSize
)
125 debug_events_
.pop_back();
128 void InstantController::ClearDebugEvents() {
129 debug_events_
.clear();
132 Profile
* InstantController::profile() const {
133 return browser_
->profile();
136 InstantTab
* InstantController::instant_tab() const {
137 return instant_tab_
.get();
140 void InstantController::InstantSupportChanged(
141 InstantSupportState instant_support
) {
142 // Handle INSTANT_SUPPORT_YES here because InstantPage is not hooked up to the
143 // active tab. Search model changed listener in InstantPage will handle other
145 if (instant_support
!= INSTANT_SUPPORT_YES
)
151 void InstantController::InstantSupportDetermined(
152 const content::WebContents
* contents
,
153 bool supports_instant
) {
154 DCHECK(IsContentsFrom(instant_tab(), contents
));
156 if (!supports_instant
)
157 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, instant_tab_
.release());
159 content::NotificationService::current()->Notify(
160 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
161 content::Source
<InstantController
>(this),
162 content::NotificationService::NoDetails());
165 void InstantController::InstantPageAboutToNavigateMainFrame(
166 const content::WebContents
* contents
,
168 DCHECK(IsContentsFrom(instant_tab(), contents
));
170 // The Instant tab navigated. Send it the data it needs to display
172 UpdateInfoForInstantTab();
175 void InstantController::ResetInstantTab() {
176 if (!search_mode_
.is_origin_default()) {
177 content::WebContents
* active_tab
= browser_
->GetActiveWebContents();
178 if (!instant_tab_
|| active_tab
!= instant_tab_
->web_contents()) {
179 instant_tab_
.reset(new InstantTab(this, browser_
->profile()));
180 instant_tab_
->Init(active_tab
);
181 UpdateInfoForInstantTab();
184 instant_tab_
.reset();
188 void InstantController::UpdateInfoForInstantTab() {
190 // Update theme details.
191 InstantService
* instant_service
= GetInstantService();
192 if (instant_service
) {
193 instant_service
->UpdateThemeInfo();
194 instant_service
->UpdateMostVisitedItemsInfo();
199 InstantService
* InstantController::GetInstantService() const {
200 return InstantServiceFactory::GetForProfile(profile());