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