Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(698)

Side by Side Diff: chrome/browser/ui/search/search_tab_helper.cc

Issue 1260033003: Partially componentize //chrome/browser/search/search.{h,cc} (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compilation on iOS Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/search/search_tab_helper.h" 5 #include "chrome/browser/ui/search/search_tab_helper.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 #include "ui/base/page_transition_types.h" 53 #include "ui/base/page_transition_types.h"
54 #include "url/gurl.h" 54 #include "url/gurl.h"
55 55
56 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); 56 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper);
57 57
58 namespace { 58 namespace {
59 59
60 bool IsCacheableNTP(const content::WebContents* contents) { 60 bool IsCacheableNTP(const content::WebContents* contents) {
61 const content::NavigationEntry* entry = 61 const content::NavigationEntry* entry =
62 contents->GetController().GetLastCommittedEntry(); 62 contents->GetController().GetLastCommittedEntry();
63 return chrome::NavEntryIsInstantNTP(contents, entry) && 63 return search::NavEntryIsInstantNTP(contents, entry) &&
64 entry->GetURL() != GURL(chrome::kChromeSearchLocalNtpUrl); 64 entry->GetURL() != GURL(chrome::kChromeSearchLocalNtpUrl);
65 } 65 }
66 66
67 bool IsNTP(const content::WebContents* contents) { 67 bool IsNTP(const content::WebContents* contents) {
68 // We can't use WebContents::GetURL() because that uses the active entry, 68 // We can't use WebContents::GetURL() because that uses the active entry,
69 // whereas we want the visible entry. 69 // whereas we want the visible entry.
70 const content::NavigationEntry* entry = 70 const content::NavigationEntry* entry =
71 contents->GetController().GetVisibleEntry(); 71 contents->GetController().GetVisibleEntry();
72 if (entry && entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL)) 72 if (entry && entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL))
73 return true; 73 return true;
74 74
75 return chrome::IsInstantNTP(contents); 75 return search::IsInstantNTP(contents);
76 } 76 }
77 77
78 bool IsSearchResults(const content::WebContents* contents) { 78 bool IsSearchResults(const content::WebContents* contents) {
79 return !chrome::GetSearchTerms(contents).empty(); 79 return !search::GetSearchTerms(contents).empty();
80 } 80 }
81 81
82 bool IsLocal(const content::WebContents* contents) { 82 bool IsLocal(const content::WebContents* contents) {
83 if (!contents) 83 if (!contents)
84 return false; 84 return false;
85 const content::NavigationEntry* entry = 85 const content::NavigationEntry* entry =
86 contents->GetController().GetVisibleEntry(); 86 contents->GetController().GetVisibleEntry();
87 return entry && entry->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl); 87 return entry && entry->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl);
88 } 88 }
89 89
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 } 136 }
137 137
138 bool OmniboxHasFocus(OmniboxView* omnibox) { 138 bool OmniboxHasFocus(OmniboxView* omnibox) {
139 return omnibox && omnibox->model()->has_focus(); 139 return omnibox && omnibox->model()->has_focus();
140 } 140 }
141 141
142 } // namespace 142 } // namespace
143 143
144 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) 144 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents)
145 : WebContentsObserver(web_contents), 145 : WebContentsObserver(web_contents),
146 is_search_enabled_(chrome::IsInstantExtendedAPIEnabled()), 146 is_search_enabled_(search::IsInstantExtendedAPIEnabled()),
147 web_contents_(web_contents), 147 web_contents_(web_contents),
148 ipc_router_(web_contents, 148 ipc_router_(web_contents,
149 this, 149 this,
150 make_scoped_ptr(new SearchIPCRouterPolicyImpl(web_contents))), 150 make_scoped_ptr(new SearchIPCRouterPolicyImpl(web_contents))),
151 instant_service_(NULL), 151 instant_service_(NULL),
152 delegate_(NULL), 152 delegate_(NULL),
153 omnibox_has_focus_fn_(&OmniboxHasFocus) { 153 omnibox_has_focus_fn_(&OmniboxHasFocus) {
154 if (!is_search_enabled_) 154 if (!is_search_enabled_)
155 return; 155 return;
156 156
(...skipping 30 matching lines...) Expand all
187 ipc_router_.OmniboxFocusChanged(state, reason); 187 ipc_router_.OmniboxFocusChanged(state, reason);
188 188
189 // Don't send oninputstart/oninputend updates in response to focus changes 189 // Don't send oninputstart/oninputend updates in response to focus changes
190 // if there's a navigation in progress. This prevents Chrome from sending 190 // if there's a navigation in progress. This prevents Chrome from sending
191 // a spurious oninputend when the user accepts a match in the omnibox. 191 // a spurious oninputend when the user accepts a match in the omnibox.
192 if (web_contents_->GetController().GetPendingEntry() == NULL) { 192 if (web_contents_->GetController().GetPendingEntry() == NULL) {
193 ipc_router_.SetInputInProgress(IsInputInProgress()); 193 ipc_router_.SetInputInProgress(IsInputInProgress());
194 194
195 InstantSearchPrerenderer* prerenderer = 195 InstantSearchPrerenderer* prerenderer =
196 InstantSearchPrerenderer::GetForProfile(profile()); 196 InstantSearchPrerenderer::GetForProfile(profile());
197 if (!prerenderer || !chrome::ShouldPrerenderInstantUrlOnOmniboxFocus()) 197 if (!prerenderer || !search::ShouldPrerenderInstantUrlOnOmniboxFocus())
198 return; 198 return;
199 199
200 if (state == OMNIBOX_FOCUS_NONE) { 200 if (state == OMNIBOX_FOCUS_NONE) {
201 prerenderer->Cancel(); 201 prerenderer->Cancel();
202 return; 202 return;
203 } 203 }
204 204
205 if (!IsSearchResultsPage()) { 205 if (!IsSearchResultsPage()) {
206 prerenderer->Init( 206 prerenderer->Init(
207 web_contents_->GetController().GetDefaultSessionStorageNamespace(), 207 web_contents_->GetController().GetDefaultSessionStorageNamespace(),
(...skipping 14 matching lines...) Expand all
222 return; 222 return;
223 223
224 InstantSupportState new_state = instant_support ? INSTANT_SUPPORT_YES : 224 InstantSupportState new_state = instant_support ? INSTANT_SUPPORT_YES :
225 INSTANT_SUPPORT_NO; 225 INSTANT_SUPPORT_NO;
226 226
227 model_.SetInstantSupportState(new_state); 227 model_.SetInstantSupportState(new_state);
228 228
229 content::NavigationEntry* entry = 229 content::NavigationEntry* entry =
230 web_contents_->GetController().GetLastCommittedEntry(); 230 web_contents_->GetController().GetLastCommittedEntry();
231 if (entry) { 231 if (entry) {
232 chrome::SetInstantSupportStateInNavigationEntry(new_state, entry); 232 search::SetInstantSupportStateInNavigationEntry(new_state, entry);
233 if (delegate_ && !instant_support) 233 if (delegate_ && !instant_support)
234 delegate_->OnWebContentsInstantSupportDisabled(web_contents_); 234 delegate_->OnWebContentsInstantSupportDisabled(web_contents_);
235 } 235 }
236 } 236 }
237 237
238 bool SearchTabHelper::SupportsInstant() const { 238 bool SearchTabHelper::SupportsInstant() const {
239 return model_.instant_support() == INSTANT_SUPPORT_YES; 239 return model_.instant_support() == INSTANT_SUPPORT_YES;
240 } 240 }
241 241
242 void SearchTabHelper::SetSuggestionToPrefetch( 242 void SearchTabHelper::SetSuggestionToPrefetch(
243 const InstantSuggestion& suggestion) { 243 const InstantSuggestion& suggestion) {
244 ipc_router_.SetSuggestionToPrefetch(suggestion); 244 ipc_router_.SetSuggestionToPrefetch(suggestion);
245 } 245 }
246 246
247 void SearchTabHelper::Submit(const base::string16& text, 247 void SearchTabHelper::Submit(const base::string16& text,
248 const EmbeddedSearchRequestParams& params) { 248 const EmbeddedSearchRequestParams& params) {
249 ipc_router_.Submit(text, params); 249 ipc_router_.Submit(text, params);
250 } 250 }
251 251
252 void SearchTabHelper::OnTabActivated() { 252 void SearchTabHelper::OnTabActivated() {
253 ipc_router_.OnTabActivated(); 253 ipc_router_.OnTabActivated();
254 254
255 OmniboxView* omnibox_view = GetOmniboxView(); 255 OmniboxView* omnibox_view = GetOmniboxView();
256 if (chrome::ShouldPrerenderInstantUrlOnOmniboxFocus() && 256 if (search::ShouldPrerenderInstantUrlOnOmniboxFocus() &&
257 omnibox_has_focus_fn_(omnibox_view)) { 257 omnibox_has_focus_fn_(omnibox_view)) {
258 InstantSearchPrerenderer* prerenderer = 258 InstantSearchPrerenderer* prerenderer =
259 InstantSearchPrerenderer::GetForProfile(profile()); 259 InstantSearchPrerenderer::GetForProfile(profile());
260 if (prerenderer && !IsSearchResultsPage()) { 260 if (prerenderer && !IsSearchResultsPage()) {
261 prerenderer->Init( 261 prerenderer->Init(
262 web_contents_->GetController().GetDefaultSessionStorageNamespace(), 262 web_contents_->GetController().GetDefaultSessionStorageNamespace(),
263 web_contents_->GetContainerBounds().size()); 263 web_contents_->GetContainerBounds().size());
264 } 264 }
265 } 265 }
266 } 266 }
(...skipping 11 matching lines...) Expand all
278 } 278 }
279 279
280 void SearchTabHelper::RenderViewCreated( 280 void SearchTabHelper::RenderViewCreated(
281 content::RenderViewHost* render_view_host) { 281 content::RenderViewHost* render_view_host) {
282 ipc_router_.SetPromoInformation(IsAppLauncherEnabled()); 282 ipc_router_.SetPromoInformation(IsAppLauncherEnabled());
283 } 283 }
284 284
285 void SearchTabHelper::DidStartNavigationToPendingEntry( 285 void SearchTabHelper::DidStartNavigationToPendingEntry(
286 const GURL& url, 286 const GURL& url,
287 content::NavigationController::ReloadType /* reload_type */) { 287 content::NavigationController::ReloadType /* reload_type */) {
288 if (chrome::IsNTPURL(url, profile())) { 288 if (search::IsNTPURL(url, profile())) {
289 // Set the title on any pending entry corresponding to the NTP. This 289 // Set the title on any pending entry corresponding to the NTP. This
290 // prevents any flickering of the tab title. 290 // prevents any flickering of the tab title.
291 content::NavigationEntry* entry = 291 content::NavigationEntry* entry =
292 web_contents_->GetController().GetPendingEntry(); 292 web_contents_->GetController().GetPendingEntry();
293 if (entry) 293 if (entry)
294 entry->SetTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); 294 entry->SetTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
295 } 295 }
296 } 296 }
297 297
298 void SearchTabHelper::DidNavigateMainFrame( 298 void SearchTabHelper::DidNavigateMainFrame(
299 const content::LoadCommittedDetails& details, 299 const content::LoadCommittedDetails& details,
300 const content::FrameNavigateParams& params) { 300 const content::FrameNavigateParams& params) {
301 if (IsCacheableNTP(web_contents_)) { 301 if (IsCacheableNTP(web_contents_)) {
302 UMA_HISTOGRAM_ENUMERATION("InstantExtended.CacheableNTPLoad", 302 UMA_HISTOGRAM_ENUMERATION("InstantExtended.CacheableNTPLoad",
303 chrome::CACHEABLE_NTP_LOAD_SUCCEEDED, 303 search::CACHEABLE_NTP_LOAD_SUCCEEDED,
304 chrome::CACHEABLE_NTP_LOAD_MAX); 304 search::CACHEABLE_NTP_LOAD_MAX);
305 } 305 }
306 306
307 // Always set the title on the new tab page to be the one from our UI 307 // Always set the title on the new tab page to be the one from our UI
308 // resources. Normally, we set the title when we begin a NTP load, but it can 308 // resources. Normally, we set the title when we begin a NTP load, but it can
309 // get reset in several places (like when you press Reload). This check 309 // get reset in several places (like when you press Reload). This check
310 // ensures that the title is properly set to the string defined by the Chrome 310 // ensures that the title is properly set to the string defined by the Chrome
311 // UI language (rather than the server language) in all cases. 311 // UI language (rather than the server language) in all cases.
312 // 312 //
313 // We only override the title when it's nonempty to allow the page to set the 313 // We only override the title when it's nonempty to allow the page to set the
314 // title if it really wants. An empty title means to use the default. There's 314 // title if it really wants. An empty title means to use the default. There's
315 // also a race condition between this code and the page's SetTitle call which 315 // also a race condition between this code and the page's SetTitle call which
316 // this rule avoids. 316 // this rule avoids.
317 content::NavigationEntry* entry = 317 content::NavigationEntry* entry =
318 web_contents_->GetController().GetLastCommittedEntry(); 318 web_contents_->GetController().GetLastCommittedEntry();
319 if (entry && entry->GetTitle().empty() && 319 if (entry && entry->GetTitle().empty() &&
320 (entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL) || 320 (entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL) ||
321 chrome::NavEntryIsInstantNTP(web_contents_, entry))) { 321 search::NavEntryIsInstantNTP(web_contents_, entry))) {
322 entry->SetTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); 322 entry->SetTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
323 } 323 }
324 } 324 }
325 325
326 void SearchTabHelper::DidFinishLoad(content::RenderFrameHost* render_frame_host, 326 void SearchTabHelper::DidFinishLoad(content::RenderFrameHost* render_frame_host,
327 const GURL& /* validated_url */) { 327 const GURL& /* validated_url */) {
328 if (!render_frame_host->GetParent()) { 328 if (!render_frame_host->GetParent()) {
329 if (chrome::IsInstantNTP(web_contents_)) 329 if (search::IsInstantNTP(web_contents_))
330 RecordNewTabLoadTime(web_contents_); 330 RecordNewTabLoadTime(web_contents_);
331 331
332 DetermineIfPageSupportsInstant(); 332 DetermineIfPageSupportsInstant();
333 } 333 }
334 } 334 }
335 335
336 void SearchTabHelper::NavigationEntryCommitted( 336 void SearchTabHelper::NavigationEntryCommitted(
337 const content::LoadCommittedDetails& load_details) { 337 const content::LoadCommittedDetails& load_details) {
338 if (!is_search_enabled_) 338 if (!is_search_enabled_)
339 return; 339 return;
340 340
341 if (!load_details.is_main_frame) 341 if (!load_details.is_main_frame)
342 return; 342 return;
343 343
344 if (chrome::ShouldAssignURLToInstantRenderer(web_contents_->GetURL(), 344 if (search::ShouldAssignURLToInstantRenderer(web_contents_->GetURL(),
345 profile())) { 345 profile())) {
346 InstantService* instant_service = 346 InstantService* instant_service =
347 InstantServiceFactory::GetForProfile(profile()); 347 InstantServiceFactory::GetForProfile(profile());
348 ipc_router_.SetOmniboxStartMargin(instant_service->omnibox_start_margin()); 348 ipc_router_.SetOmniboxStartMargin(instant_service->omnibox_start_margin());
349 ipc_router_.SetDisplayInstantResults(); 349 ipc_router_.SetDisplayInstantResults();
350 } 350 }
351 351
352 UpdateMode(true, false); 352 UpdateMode(true, false);
353 353
354 content::NavigationEntry* entry = 354 content::NavigationEntry* entry =
355 web_contents_->GetController().GetVisibleEntry(); 355 web_contents_->GetController().GetVisibleEntry();
356 DCHECK(entry); 356 DCHECK(entry);
357 357
358 // Already determined the instant support state for this page, do not reset 358 // Already determined the instant support state for this page, do not reset
359 // the instant support state. 359 // the instant support state.
360 if (load_details.is_in_page) { 360 if (load_details.is_in_page) {
361 // When an "in-page" navigation happens, we will not receive a 361 // When an "in-page" navigation happens, we will not receive a
362 // DidFinishLoad() event. Therefore, we will not determine the Instant 362 // DidFinishLoad() event. Therefore, we will not determine the Instant
363 // support for the navigated page. So, copy over the Instant support from 363 // support for the navigated page. So, copy over the Instant support from
364 // the previous entry. If the page does not support Instant, update the 364 // the previous entry. If the page does not support Instant, update the
365 // location bar from here to turn off search terms replacement. 365 // location bar from here to turn off search terms replacement.
366 chrome::SetInstantSupportStateInNavigationEntry(model_.instant_support(), 366 search::SetInstantSupportStateInNavigationEntry(model_.instant_support(),
367 entry); 367 entry);
368 if (delegate_ && model_.instant_support() == INSTANT_SUPPORT_NO) 368 if (delegate_ && model_.instant_support() == INSTANT_SUPPORT_NO)
369 delegate_->OnWebContentsInstantSupportDisabled(web_contents_); 369 delegate_->OnWebContentsInstantSupportDisabled(web_contents_);
370 return; 370 return;
371 } 371 }
372 372
373 model_.SetInstantSupportState(INSTANT_SUPPORT_UNKNOWN); 373 model_.SetInstantSupportState(INSTANT_SUPPORT_UNKNOWN);
374 model_.SetVoiceSearchSupported(false); 374 model_.SetVoiceSearchSupported(false);
375 chrome::SetInstantSupportStateInNavigationEntry(model_.instant_support(), 375 search::SetInstantSupportStateInNavigationEntry(model_.instant_support(),
376 entry); 376 entry);
377 377
378 if (InInstantProcess(profile(), web_contents_)) 378 if (InInstantProcess(profile(), web_contents_))
379 ipc_router_.OnNavigationEntryCommitted(); 379 ipc_router_.OnNavigationEntryCommitted();
380 } 380 }
381 381
382 void SearchTabHelper::OnInstantSupportDetermined(bool supports_instant) { 382 void SearchTabHelper::OnInstantSupportDetermined(bool supports_instant) {
383 InstantSupportChanged(supports_instant); 383 InstantSupportChanged(supports_instant);
384 } 384 }
385 385
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 586
587 bool SearchTabHelper::IsInputInProgress() const { 587 bool SearchTabHelper::IsInputInProgress() const {
588 OmniboxView* omnibox = GetOmniboxView(); 588 OmniboxView* omnibox = GetOmniboxView();
589 return !model_.mode().is_ntp() && omnibox && 589 return !model_.mode().is_ntp() && omnibox &&
590 omnibox->model()->focus_state() == OMNIBOX_FOCUS_VISIBLE; 590 omnibox->model()->focus_state() == OMNIBOX_FOCUS_VISIBLE;
591 } 591 }
592 592
593 OmniboxView* SearchTabHelper::GetOmniboxView() const { 593 OmniboxView* SearchTabHelper::GetOmniboxView() const {
594 return delegate_ ? delegate_->GetOmniboxView() : NULL; 594 return delegate_ ? delegate_->GetOmniboxView() : NULL;
595 } 595 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/search/search_model.cc ('k') | chrome/browser/ui/search/search_tab_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698