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

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

Issue 141893009: Create a new helper function to extract search terms from the URL irrespective of the availablility (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/search/search.h ('k') | chrome/browser/search/search_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/search/search.h" 5 #include "chrome/browser/search/search.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/metrics/field_trial.h" 8 #include "base/metrics/field_trial.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 return false; 196 return false;
197 197
198 const InstantService* instant_service = 198 const InstantService* instant_service =
199 InstantServiceFactory::GetForProfile(profile); 199 InstantServiceFactory::GetForProfile(profile);
200 if (!instant_service) 200 if (!instant_service)
201 return false; 201 return false;
202 202
203 return instant_service->IsInstantProcess(process_host->GetID()); 203 return instant_service->IsInstantProcess(process_host->GetID());
204 } 204 }
205 205
206 // Returns true if |url| passes some basic checks that must succeed for it to be 206 // |url| should either have a secure scheme or have a non-HTTPS base URL that
207 // usable as an instant URL: 207 // the user specified using --google-base-url. (This allows testers to use
208 // (1) It contains the search terms replacement key of |template_url|, which is 208 // --google-base-url to point at non-HTTPS servers, which eases testing.)
209 // expected to be the TemplateURL* for the default search provider.
210 // (2) Either it has a secure scheme, or else the user has manually specified a
211 // --google-base-url and it uses that base URL. (This allows testers to use
212 // --google-base-url to point at non-HTTPS servers, which eases testing.)
213 bool IsSuitableURLForInstant(const GURL& url, const TemplateURL* template_url) { 209 bool IsSuitableURLForInstant(const GURL& url, const TemplateURL* template_url) {
214 return template_url->HasSearchTermsReplacementKey(url) && 210 return template_url->HasSearchTermsReplacementKey(url) &&
215 (url.SchemeIsSecure() || 211 (url.SchemeIsSecure() ||
216 google_util::StartsWithCommandLineGoogleBaseURL(url)); 212 google_util::StartsWithCommandLineGoogleBaseURL(url));
217 } 213 }
218 214
219 // Returns true if |url| can be used as an Instant URL for |profile|. 215 // Returns true if |url| can be used as an Instant URL for |profile|.
220 bool IsInstantURL(const GURL& url, Profile* profile) { 216 bool IsInstantURL(const GURL& url, Profile* profile) {
221 if (!IsInstantExtendedAPIEnabled()) 217 if (!IsInstantExtendedAPIEnabled())
222 return false; 218 return false;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 if (!IsRenderedInInstantProcess(contents, profile) && 262 if (!IsRenderedInInstantProcess(contents, profile) &&
267 ((entry == contents->GetController().GetLastCommittedEntry()) || 263 ((entry == contents->GetController().GetLastCommittedEntry()) ||
268 !ShouldAssignURLToInstantRenderer(entry->GetURL(), profile))) 264 !ShouldAssignURLToInstantRenderer(entry->GetURL(), profile)))
269 return base::string16(); 265 return base::string16();
270 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) 266 #endif // !defined(OS_IOS) && !defined(OS_ANDROID)
271 // Check to see if search terms have already been extracted. 267 // Check to see if search terms have already been extracted.
272 base::string16 search_terms = GetSearchTermsFromNavigationEntry(entry); 268 base::string16 search_terms = GetSearchTermsFromNavigationEntry(entry);
273 if (!search_terms.empty()) 269 if (!search_terms.empty())
274 return search_terms; 270 return search_terms;
275 271
272 if (!IsQueryExtractionAllowedForURL(profile, entry->GetVirtualURL()))
273 return base::string16();
274
276 // Otherwise, extract from the URL. 275 // Otherwise, extract from the URL.
277 return GetSearchTermsFromURL(profile, entry->GetVirtualURL()); 276 return ExtractSearchTermsFromURL(profile, entry->GetVirtualURL());
278 } 277 }
279 278
280 bool IsURLAllowedForSupervisedUser(const GURL& url, Profile* profile) { 279 bool IsURLAllowedForSupervisedUser(const GURL& url, Profile* profile) {
281 #if defined(ENABLE_MANAGED_USERS) 280 #if defined(ENABLE_MANAGED_USERS)
282 ManagedUserService* managed_user_service = 281 ManagedUserService* managed_user_service =
283 ManagedUserServiceFactory::GetForProfile(profile); 282 ManagedUserServiceFactory::GetForProfile(profile);
284 ManagedModeURLFilter* url_filter = 283 ManagedModeURLFilter* url_filter =
285 managed_user_service->GetURLFilterForUIThread(); 284 managed_user_service->GetURLFilterForUIThread();
286 if (url_filter->GetFilteringBehaviorForURL(url) == 285 if (url_filter->GetFilteringBehaviorForURL(url) ==
287 ManagedModeURLFilter::BLOCK) { 286 ManagedModeURLFilter::BLOCK) {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 380 const CommandLine* command_line = CommandLine::ForCurrentProcess();
382 if (command_line->HasSwitch(switches::kEnableQueryExtraction)) 381 if (command_line->HasSwitch(switches::kEnableQueryExtraction))
383 return true; 382 return true;
384 383
385 FieldTrialFlags flags; 384 FieldTrialFlags flags;
386 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( 385 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault(
387 kEnableQueryExtractionFlagName, false, flags); 386 kEnableQueryExtractionFlagName, false, flags);
388 #endif // defined(OS_IOS) || defined(OS_ANDROID) 387 #endif // defined(OS_IOS) || defined(OS_ANDROID)
389 } 388 }
390 389
391 base::string16 GetSearchTermsFromURL(Profile* profile, const GURL& url) { 390 base::string16 ExtractSearchTermsFromURL(Profile* profile, const GURL& url) {
392 if (url.is_valid() && url == GetSearchResultPrefetchBaseURL(profile)) { 391 if (url.is_valid() && url == GetSearchResultPrefetchBaseURL(profile)) {
393 // InstantSearchPrerenderer has the search query for the Instant search base 392 // InstantSearchPrerenderer has the search query for the Instant search base
394 // page. 393 // page.
395 InstantSearchPrerenderer* prerenderer = 394 InstantSearchPrerenderer* prerenderer =
396 InstantSearchPrerenderer::GetForProfile(profile); 395 InstantSearchPrerenderer::GetForProfile(profile);
397 DCHECK(prerenderer); 396 DCHECK(prerenderer);
398 return prerenderer->get_last_query(); 397 return prerenderer->get_last_query();
399 } 398 }
400 399
400 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
401 base::string16 search_terms; 401 base::string16 search_terms;
402 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 402 if (template_url)
403 if (template_url && IsSuitableURLForInstant(url, template_url))
404 template_url->ExtractSearchTermsFromURL(url, &search_terms); 403 template_url->ExtractSearchTermsFromURL(url, &search_terms);
405 return search_terms; 404 return search_terms;
406 } 405 }
407 406
407 bool IsQueryExtractionAllowedForURL(Profile* profile, const GURL& url) {
408 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
409 return template_url && IsSuitableURLForInstant(url, template_url);
410 }
411
408 base::string16 GetSearchTermsFromNavigationEntry( 412 base::string16 GetSearchTermsFromNavigationEntry(
409 const content::NavigationEntry* entry) { 413 const content::NavigationEntry* entry) {
410 base::string16 search_terms; 414 base::string16 search_terms;
411 if (entry) 415 if (entry)
412 entry->GetExtraData(sessions::kSearchTermsKey, &search_terms); 416 entry->GetExtraData(sessions::kSearchTermsKey, &search_terms);
413 return search_terms; 417 return search_terms;
414 } 418 }
415 419
416 base::string16 GetSearchTerms(const content::WebContents* contents) { 420 base::string16 GetSearchTerms(const content::WebContents* contents) {
417 if (!contents) 421 if (!contents)
(...skipping 29 matching lines...) Expand all
447 url.host() == chrome::kChromeSearchOnlineNtpHost); 451 url.host() == chrome::kChromeSearchOnlineNtpHost);
448 } 452 }
449 453
450 bool IsNTPURL(const GURL& url, Profile* profile) { 454 bool IsNTPURL(const GURL& url, Profile* profile) {
451 if (!url.is_valid()) 455 if (!url.is_valid())
452 return false; 456 return false;
453 457
454 if (!IsInstantExtendedAPIEnabled()) 458 if (!IsInstantExtendedAPIEnabled())
455 return url == GURL(chrome::kChromeUINewTabURL); 459 return url == GURL(chrome::kChromeUINewTabURL);
456 460
461 const base::string16 search_terms = ExtractSearchTermsFromURL(profile, url);
457 return profile && 462 return profile &&
458 ((IsInstantURL(url, profile) && 463 ((IsInstantURL(url, profile) && search_terms.empty()) ||
459 GetSearchTermsFromURL(profile, url).empty()) ||
460 url == GURL(chrome::kChromeSearchLocalNtpUrl)); 464 url == GURL(chrome::kChromeSearchLocalNtpUrl));
461 } 465 }
462 466
463 bool IsInstantNTP(const content::WebContents* contents) { 467 bool IsInstantNTP(const content::WebContents* contents) {
464 if (!contents) 468 if (!contents)
465 return false; 469 return false;
466 470
467 return NavEntryIsInstantNTP(contents, 471 return NavEntryIsInstantNTP(contents,
468 contents->GetController().GetVisibleEntry()); 472 contents->GetController().GetVisibleEntry());
469 } 473 }
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 829
826 // Given a FieldTrialFlags object, returns the boolean value of the provided 830 // Given a FieldTrialFlags object, returns the boolean value of the provided
827 // flag. 831 // flag.
828 bool GetBoolValueForFlagWithDefault(const std::string& flag, 832 bool GetBoolValueForFlagWithDefault(const std::string& flag,
829 bool default_value, 833 bool default_value,
830 const FieldTrialFlags& flags) { 834 const FieldTrialFlags& flags) {
831 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); 835 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags);
832 } 836 }
833 837
834 } // namespace chrome 838 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/search/search.h ('k') | chrome/browser/search/search_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698