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