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

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: Addressed comments 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
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 // Returns true if |url| passes some basic checks that must succeed for it to be
Jered 2014/02/11 19:10:10 Omit this long comment here.
kmadhusu 2014/02/11 23:24:32 Done. Added a few lines about --google-base-url ca
207 // usable as an instant URL: 207 // usable as an instant URL:
208 // (1) It contains the search terms replacement key of |template_url|, which is 208 // (1) It contains the search terms replacement key of |template_url|, which is
209 // expected to be the TemplateURL* for the default search provider. 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 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 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.) 212 // --google-base-url to point at non-HTTPS servers, which eases testing.)
213 bool IsSuitableURLForInstant(const GURL& url, const TemplateURL* template_url) { 213 bool IsSuitableURLForInstantImpl(const GURL& url,
214 const TemplateURL* template_url) {
214 return template_url->HasSearchTermsReplacementKey(url) && 215 return template_url->HasSearchTermsReplacementKey(url) &&
215 (url.SchemeIsSecure() || 216 (url.SchemeIsSecure() ||
216 google_util::StartsWithCommandLineGoogleBaseURL(url)); 217 google_util::StartsWithCommandLineGoogleBaseURL(url));
217 } 218 }
218 219
219 // Returns true if |url| can be used as an Instant URL for |profile|. 220 // Returns true if |url| can be used as an Instant URL for |profile|.
220 bool IsInstantURL(const GURL& url, Profile* profile) { 221 bool IsInstantURL(const GURL& url, Profile* profile) {
221 if (!IsInstantExtendedAPIEnabled()) 222 if (!IsInstantExtendedAPIEnabled())
222 return false; 223 return false;
223 224
224 if (!url.is_valid()) 225 if (!url.is_valid())
225 return false; 226 return false;
226 227
227 const GURL new_tab_url(GetNewTabPageURL(profile)); 228 const GURL new_tab_url(GetNewTabPageURL(profile));
228 if (new_tab_url.is_valid() && 229 if (new_tab_url.is_valid() &&
229 search::MatchesOriginAndPath(url, new_tab_url)) 230 search::MatchesOriginAndPath(url, new_tab_url))
230 return true; 231 return true;
231 232
232 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 233 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
233 if (!template_url) 234 if (!template_url)
234 return false; 235 return false;
235 236
236 if (!IsSuitableURLForInstant(url, template_url)) 237 if (!IsSuitableURLForInstantImpl(url, template_url))
237 return false; 238 return false;
238 239
239 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); 240 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref();
240 const GURL instant_url = 241 const GURL instant_url =
241 TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin, false, false); 242 TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin, false, false);
242 if (!instant_url.is_valid()) 243 if (!instant_url.is_valid())
243 return false; 244 return false;
244 245
245 if (search::MatchesOriginAndPath(url, instant_url)) 246 if (search::MatchesOriginAndPath(url, instant_url))
246 return true; 247 return true;
(...skipping 20 matching lines...) Expand all
267 ((entry == contents->GetController().GetLastCommittedEntry()) || 268 ((entry == contents->GetController().GetLastCommittedEntry()) ||
268 !ShouldAssignURLToInstantRenderer(entry->GetURL(), profile))) 269 !ShouldAssignURLToInstantRenderer(entry->GetURL(), profile)))
269 return base::string16(); 270 return base::string16();
270 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) 271 #endif // !defined(OS_IOS) && !defined(OS_ANDROID)
271 // Check to see if search terms have already been extracted. 272 // Check to see if search terms have already been extracted.
272 base::string16 search_terms = GetSearchTermsFromNavigationEntry(entry); 273 base::string16 search_terms = GetSearchTermsFromNavigationEntry(entry);
273 if (!search_terms.empty()) 274 if (!search_terms.empty())
274 return search_terms; 275 return search_terms;
275 276
276 // Otherwise, extract from the URL. 277 // Otherwise, extract from the URL.
277 return GetSearchTermsFromURL(profile, entry->GetVirtualURL()); 278 return IsSuitableURLForInstant(profile, entry->GetVirtualURL()) ?
Jered 2014/02/11 19:10:10 nit: This would be clearer as if (!IsSuitableURLF
kmadhusu 2014/02/11 23:24:32 Done.
279 ExtractSearchTermsFromURL(profile, entry->GetVirtualURL()) :
280 base::string16();
278 } 281 }
279 282
280 bool IsURLAllowedForSupervisedUser(const GURL& url, Profile* profile) { 283 bool IsURLAllowedForSupervisedUser(const GURL& url, Profile* profile) {
281 #if defined(ENABLE_MANAGED_USERS) 284 #if defined(ENABLE_MANAGED_USERS)
282 ManagedUserService* managed_user_service = 285 ManagedUserService* managed_user_service =
283 ManagedUserServiceFactory::GetForProfile(profile); 286 ManagedUserServiceFactory::GetForProfile(profile);
284 ManagedModeURLFilter* url_filter = 287 ManagedModeURLFilter* url_filter =
285 managed_user_service->GetURLFilterForUIThread(); 288 managed_user_service->GetURLFilterForUIThread();
286 if (url_filter->GetFilteringBehaviorForURL(url) == 289 if (url_filter->GetFilteringBehaviorForURL(url) ==
287 ManagedModeURLFilter::BLOCK) { 290 ManagedModeURLFilter::BLOCK) {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 377 const CommandLine* command_line = CommandLine::ForCurrentProcess();
375 if (command_line->HasSwitch(switches::kEnableQueryExtraction)) 378 if (command_line->HasSwitch(switches::kEnableQueryExtraction))
376 return true; 379 return true;
377 380
378 FieldTrialFlags flags; 381 FieldTrialFlags flags;
379 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( 382 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault(
380 kEnableQueryExtractionFlagName, false, flags); 383 kEnableQueryExtractionFlagName, false, flags);
381 #endif // defined(OS_IOS) || defined(OS_ANDROID) 384 #endif // defined(OS_IOS) || defined(OS_ANDROID)
382 } 385 }
383 386
384 base::string16 GetSearchTermsFromURL(Profile* profile, const GURL& url) { 387 base::string16 ExtractSearchTermsFromURL(Profile* profile, const GURL& url) {
385 if (url.is_valid() && url == GetSearchResultPrefetchBaseURL(profile)) { 388 if (url.is_valid() && url == GetSearchResultPrefetchBaseURL(profile)) {
386 // InstantSearchPrerenderer has the search query for the Instant search base 389 // InstantSearchPrerenderer has the search query for the Instant search base
387 // page. 390 // page.
388 InstantSearchPrerenderer* prerenderer = 391 InstantSearchPrerenderer* prerenderer =
389 InstantSearchPrerenderer::GetForProfile(profile); 392 InstantSearchPrerenderer::GetForProfile(profile);
390 DCHECK(prerenderer); 393 DCHECK(prerenderer);
391 return prerenderer->get_last_query(); 394 return prerenderer->get_last_query();
392 } 395 }
393 396
397 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
394 base::string16 search_terms; 398 base::string16 search_terms;
395 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 399 if (template_url)
396 if (template_url && IsSuitableURLForInstant(url, template_url))
397 template_url->ExtractSearchTermsFromURL(url, &search_terms); 400 template_url->ExtractSearchTermsFromURL(url, &search_terms);
398 return search_terms; 401 return search_terms;
399 } 402 }
400 403
404 bool IsSuitableURLForInstant(Profile* profile, const GURL& url) {
405 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
406 return IsSuitableURLForInstantImpl(url, template_url);
Jered 2014/02/11 19:10:10 template_url && ...
kmadhusu 2014/02/11 23:24:32 Done.
407 }
408
401 base::string16 GetSearchTermsFromNavigationEntry( 409 base::string16 GetSearchTermsFromNavigationEntry(
402 const content::NavigationEntry* entry) { 410 const content::NavigationEntry* entry) {
403 base::string16 search_terms; 411 base::string16 search_terms;
404 if (entry) 412 if (entry)
405 entry->GetExtraData(sessions::kSearchTermsKey, &search_terms); 413 entry->GetExtraData(sessions::kSearchTermsKey, &search_terms);
406 return search_terms; 414 return search_terms;
407 } 415 }
408 416
409 base::string16 GetSearchTerms(const content::WebContents* contents) { 417 base::string16 GetSearchTerms(const content::WebContents* contents) {
410 if (!contents) 418 if (!contents)
(...skipping 29 matching lines...) Expand all
440 url.host() == chrome::kChromeSearchOnlineNtpHost); 448 url.host() == chrome::kChromeSearchOnlineNtpHost);
441 } 449 }
442 450
443 bool IsNTPURL(const GURL& url, Profile* profile) { 451 bool IsNTPURL(const GURL& url, Profile* profile) {
444 if (!url.is_valid()) 452 if (!url.is_valid())
445 return false; 453 return false;
446 454
447 if (!IsInstantExtendedAPIEnabled()) 455 if (!IsInstantExtendedAPIEnabled())
448 return url == GURL(chrome::kChromeUINewTabURL); 456 return url == GURL(chrome::kChromeUINewTabURL);
449 457
458 const base::string16 search_terms = ExtractSearchTermsFromURL(profile, url);
450 return profile && 459 return profile &&
451 ((IsInstantURL(url, profile) && 460 ((IsInstantURL(url, profile) && search_terms.empty()) ||
452 GetSearchTermsFromURL(profile, url).empty()) ||
453 url == GURL(chrome::kChromeSearchLocalNtpUrl)); 461 url == GURL(chrome::kChromeSearchLocalNtpUrl));
454 } 462 }
455 463
456 bool IsInstantNTP(const content::WebContents* contents) { 464 bool IsInstantNTP(const content::WebContents* contents) {
457 if (!contents) 465 if (!contents)
458 return false; 466 return false;
459 467
460 return NavEntryIsInstantNTP(contents, 468 return NavEntryIsInstantNTP(contents,
461 contents->GetController().GetVisibleEntry()); 469 contents->GetController().GetVisibleEntry());
462 } 470 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 502
495 GURL instant_url = 503 GURL instant_url =
496 TemplateURLRefToGURL(template_url->instant_url_ref(), start_margin, true, 504 TemplateURLRefToGURL(template_url->instant_url_ref(), start_margin, true,
497 force_instant_results); 505 force_instant_results);
498 if (!instant_url.is_valid() || 506 if (!instant_url.is_valid() ||
499 !template_url->HasSearchTermsReplacementKey(instant_url)) 507 !template_url->HasSearchTermsReplacementKey(instant_url))
500 return GURL(); 508 return GURL();
501 509
502 // Extended mode requires HTTPS. Force it unless the base URL was overridden 510 // Extended mode requires HTTPS. Force it unless the base URL was overridden
503 // on the command line, in which case we allow HTTP (see comments on 511 // on the command line, in which case we allow HTTP (see comments on
504 // IsSuitableURLForInstant()). 512 // IsSuitableURLForInstantImpl()).
505 if (!instant_url.SchemeIsSecure() && 513 if (!instant_url.SchemeIsSecure() &&
506 !google_util::StartsWithCommandLineGoogleBaseURL(instant_url)) { 514 !google_util::StartsWithCommandLineGoogleBaseURL(instant_url)) {
507 GURL::Replacements replacements; 515 GURL::Replacements replacements;
508 const std::string secure_scheme(content::kHttpsScheme); 516 const std::string secure_scheme(content::kHttpsScheme);
509 replacements.SetSchemeStr(secure_scheme); 517 replacements.SetSchemeStr(secure_scheme);
510 instant_url = instant_url.ReplaceComponents(replacements); 518 instant_url = instant_url.ReplaceComponents(replacements);
511 } 519 }
512 520
513 if (!IsURLAllowedForSupervisedUser(instant_url, profile)) 521 if (!IsURLAllowedForSupervisedUser(instant_url, profile))
514 return GURL(); 522 return GURL();
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 816
809 // Given a FieldTrialFlags object, returns the boolean value of the provided 817 // Given a FieldTrialFlags object, returns the boolean value of the provided
810 // flag. 818 // flag.
811 bool GetBoolValueForFlagWithDefault(const std::string& flag, 819 bool GetBoolValueForFlagWithDefault(const std::string& flag,
812 bool default_value, 820 bool default_value,
813 const FieldTrialFlags& flags) { 821 const FieldTrialFlags& flags) {
814 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); 822 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags);
815 } 823 }
816 824
817 } // namespace chrome 825 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698