73614754bf73f7da04109820b0ee65e24eeb9653
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / search / instant_controller.cc
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.
4
5 #include "chrome/browser/ui/search/instant_controller.h"
6
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/content_settings/content_settings_provider.h"
11 #include "chrome/browser/content_settings/host_content_settings_map.h"
12 #include "chrome/browser/platform_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search/instant_service.h"
15 #include "chrome/browser/search/instant_service_factory.h"
16 #include "chrome/browser/search/search.h"
17 #include "chrome/browser/search_engines/search_terms_data.h"
18 #include "chrome/browser/search_engines/template_url_service.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/ui/browser_instant_controller.h"
21 #include "chrome/browser/ui/search/instant_tab.h"
22 #include "chrome/browser/ui/search/search_tab_helper.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/content_settings_types.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/search_urls.h"
27 #include "chrome/common/url_constants.h"
28 #include "components/sessions/serialized_navigation_entry.h"
29 #include "content/public/browser/navigation_entry.h"
30 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/render_widget_host_view.h"
33 #include "content/public/browser/web_contents.h"
34 #include "net/base/escape.h"
35 #include "net/base/network_change_notifier.h"
36 #include "url/gurl.h"
37
38 #if defined(TOOLKIT_VIEWS)
39 #include "ui/views/widget/widget.h"
40 #endif
41
42 namespace {
43
44 bool IsContentsFrom(const InstantPage* page,
45                     const content::WebContents* contents) {
46   return page && (page->contents() == contents);
47 }
48
49 // Adds a transient NavigationEntry to the supplied |contents|'s
50 // NavigationController if the page's URL has not already been updated with the
51 // supplied |search_terms|. Sets the |search_terms| on the transient entry for
52 // search terms extraction to work correctly.
53 void EnsureSearchTermsAreSet(content::WebContents* contents,
54                              const base::string16& search_terms) {
55   content::NavigationController* controller = &contents->GetController();
56
57   // If search terms are already correct or there is already a transient entry
58   // (there shouldn't be), bail out early.
59   if (chrome::GetSearchTerms(contents) == search_terms ||
60       controller->GetTransientEntry())
61     return;
62
63   const content::NavigationEntry* entry = controller->GetLastCommittedEntry();
64   content::NavigationEntry* transient = controller->CreateNavigationEntry(
65       entry->GetURL(),
66       entry->GetReferrer(),
67       entry->GetTransitionType(),
68       false,
69       std::string(),
70       contents->GetBrowserContext());
71   transient->SetExtraData(sessions::kSearchTermsKey, search_terms);
72   controller->SetTransientEntry(transient);
73
74   SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated();
75 }
76
77 }  // namespace
78
79 InstantController::InstantController(BrowserInstantController* browser)
80     : browser_(browser) {
81 }
82
83 InstantController::~InstantController() {
84 }
85
86 bool InstantController::SubmitQuery(const base::string16& search_terms) {
87   if (instant_tab_ && instant_tab_->supports_instant() &&
88       search_mode_.is_origin_search()) {
89     // Use |instant_tab_| to run the query if we're already on a search results
90     // page. (NOTE: in particular, we do not send the query to NTPs.)
91     SearchTabHelper::FromWebContents(instant_tab_->contents())->Submit(
92         search_terms);
93     instant_tab_->contents()->Focus();
94     EnsureSearchTermsAreSet(instant_tab_->contents(), search_terms);
95     return true;
96   }
97   return false;
98 }
99
100 void InstantController::SearchModeChanged(const SearchMode& old_mode,
101                                           const SearchMode& new_mode) {
102   LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
103       "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin,
104       old_mode.mode, new_mode.origin, new_mode.mode));
105
106   search_mode_ = new_mode;
107   ResetInstantTab();
108 }
109
110 void InstantController::ActiveTabChanged() {
111   LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged");
112   ResetInstantTab();
113 }
114
115 void InstantController::TabDeactivated(content::WebContents* contents) {
116   // If user is deactivating an NTP tab, log the number of mouseovers for this
117   // NTP session.
118   if (chrome::IsInstantNTP(contents))
119     InstantTab::EmitNtpStatistics(contents);
120 }
121
122 void InstantController::LogDebugEvent(const std::string& info) const {
123   DVLOG(1) << info;
124
125   debug_events_.push_front(std::make_pair(
126       base::Time::Now().ToInternalValue(), info));
127   static const size_t kMaxDebugEventSize = 2000;
128   if (debug_events_.size() > kMaxDebugEventSize)
129     debug_events_.pop_back();
130 }
131
132 void InstantController::ClearDebugEvents() {
133   debug_events_.clear();
134 }
135
136 Profile* InstantController::profile() const {
137   return browser_->profile();
138 }
139
140 InstantTab* InstantController::instant_tab() const {
141   return instant_tab_.get();
142 }
143
144 void InstantController::InstantSupportChanged(
145     InstantSupportState instant_support) {
146   // Handle INSTANT_SUPPORT_YES here because InstantPage is not hooked up to the
147   // active tab. Search model changed listener in InstantPage will handle other
148   // cases.
149   if (instant_support != INSTANT_SUPPORT_YES)
150     return;
151
152   ResetInstantTab();
153 }
154
155 void InstantController::InstantSupportDetermined(
156     const content::WebContents* contents,
157     bool supports_instant) {
158   DCHECK(IsContentsFrom(instant_tab(), contents));
159
160   if (!supports_instant)
161     base::MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release());
162
163   content::NotificationService::current()->Notify(
164       chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
165       content::Source<InstantController>(this),
166       content::NotificationService::NoDetails());
167 }
168
169 void InstantController::InstantPageAboutToNavigateMainFrame(
170     const content::WebContents* contents,
171     const GURL& url) {
172   DCHECK(IsContentsFrom(instant_tab(), contents));
173
174   // The Instant tab navigated.  Send it the data it needs to display
175   // properly.
176   UpdateInfoForInstantTab();
177 }
178
179 void InstantController::ResetInstantTab() {
180   if (!search_mode_.is_origin_default()) {
181     content::WebContents* active_tab = browser_->GetActiveWebContents();
182     if (!instant_tab_ || active_tab != instant_tab_->contents()) {
183       instant_tab_.reset(new InstantTab(this, browser_->profile()));
184       instant_tab_->Init(active_tab);
185       UpdateInfoForInstantTab();
186     }
187   } else {
188     instant_tab_.reset();
189   }
190 }
191
192 void InstantController::UpdateInfoForInstantTab() {
193   if (instant_tab_) {
194     // Update theme details.
195     InstantService* instant_service = GetInstantService();
196     if (instant_service) {
197       instant_service->UpdateThemeInfo();
198       instant_service->UpdateMostVisitedItemsInfo();
199     }
200   }
201 }
202
203 InstantService* InstantController::GetInstantService() const {
204   return InstantServiceFactory::GetForProfile(profile());
205 }