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. | |
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 Loading... |
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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 373 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
375 if (command_line->HasSwitch(switches::kEnableQueryExtraction)) | 374 if (command_line->HasSwitch(switches::kEnableQueryExtraction)) |
376 return true; | 375 return true; |
377 | 376 |
378 FieldTrialFlags flags; | 377 FieldTrialFlags flags; |
379 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( | 378 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( |
380 kEnableQueryExtractionFlagName, false, flags); | 379 kEnableQueryExtractionFlagName, false, flags); |
381 #endif // defined(OS_IOS) || defined(OS_ANDROID) | 380 #endif // defined(OS_IOS) || defined(OS_ANDROID) |
382 } | 381 } |
383 | 382 |
384 base::string16 GetSearchTermsFromURL(Profile* profile, const GURL& url) { | 383 base::string16 ExtractSearchTermsFromURL(Profile* profile, const GURL& url) { |
385 if (url.is_valid() && url == GetSearchResultPrefetchBaseURL(profile)) { | 384 if (url.is_valid() && url == GetSearchResultPrefetchBaseURL(profile)) { |
386 // InstantSearchPrerenderer has the search query for the Instant search base | 385 // InstantSearchPrerenderer has the search query for the Instant search base |
387 // page. | 386 // page. |
388 InstantSearchPrerenderer* prerenderer = | 387 InstantSearchPrerenderer* prerenderer = |
389 InstantSearchPrerenderer::GetForProfile(profile); | 388 InstantSearchPrerenderer::GetForProfile(profile); |
390 DCHECK(prerenderer); | 389 DCHECK(prerenderer); |
391 return prerenderer->get_last_query(); | 390 return prerenderer->get_last_query(); |
392 } | 391 } |
393 | 392 |
| 393 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
394 base::string16 search_terms; | 394 base::string16 search_terms; |
395 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 395 if (template_url) |
396 if (template_url && IsSuitableURLForInstant(url, template_url)) | |
397 template_url->ExtractSearchTermsFromURL(url, &search_terms); | 396 template_url->ExtractSearchTermsFromURL(url, &search_terms); |
398 return search_terms; | 397 return search_terms; |
399 } | 398 } |
400 | 399 |
| 400 bool IsQueryExtractionAllowedForURL(Profile* profile, const GURL& url) { |
| 401 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
| 402 return template_url && IsSuitableURLForInstant(url, template_url); |
| 403 } |
| 404 |
401 base::string16 GetSearchTermsFromNavigationEntry( | 405 base::string16 GetSearchTermsFromNavigationEntry( |
402 const content::NavigationEntry* entry) { | 406 const content::NavigationEntry* entry) { |
403 base::string16 search_terms; | 407 base::string16 search_terms; |
404 if (entry) | 408 if (entry) |
405 entry->GetExtraData(sessions::kSearchTermsKey, &search_terms); | 409 entry->GetExtraData(sessions::kSearchTermsKey, &search_terms); |
406 return search_terms; | 410 return search_terms; |
407 } | 411 } |
408 | 412 |
409 base::string16 GetSearchTerms(const content::WebContents* contents) { | 413 base::string16 GetSearchTerms(const content::WebContents* contents) { |
410 if (!contents) | 414 if (!contents) |
(...skipping 29 matching lines...) Expand all Loading... |
440 url.host() == chrome::kChromeSearchOnlineNtpHost); | 444 url.host() == chrome::kChromeSearchOnlineNtpHost); |
441 } | 445 } |
442 | 446 |
443 bool IsNTPURL(const GURL& url, Profile* profile) { | 447 bool IsNTPURL(const GURL& url, Profile* profile) { |
444 if (!url.is_valid()) | 448 if (!url.is_valid()) |
445 return false; | 449 return false; |
446 | 450 |
447 if (!IsInstantExtendedAPIEnabled()) | 451 if (!IsInstantExtendedAPIEnabled()) |
448 return url == GURL(chrome::kChromeUINewTabURL); | 452 return url == GURL(chrome::kChromeUINewTabURL); |
449 | 453 |
| 454 const base::string16 search_terms = ExtractSearchTermsFromURL(profile, url); |
450 return profile && | 455 return profile && |
451 ((IsInstantURL(url, profile) && | 456 ((IsInstantURL(url, profile) && search_terms.empty()) || |
452 GetSearchTermsFromURL(profile, url).empty()) || | |
453 url == GURL(chrome::kChromeSearchLocalNtpUrl)); | 457 url == GURL(chrome::kChromeSearchLocalNtpUrl)); |
454 } | 458 } |
455 | 459 |
456 bool IsInstantNTP(const content::WebContents* contents) { | 460 bool IsInstantNTP(const content::WebContents* contents) { |
457 if (!contents) | 461 if (!contents) |
458 return false; | 462 return false; |
459 | 463 |
460 return NavEntryIsInstantNTP(contents, | 464 return NavEntryIsInstantNTP(contents, |
461 contents->GetController().GetVisibleEntry()); | 465 contents->GetController().GetVisibleEntry()); |
462 } | 466 } |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
818 | 822 |
819 // Given a FieldTrialFlags object, returns the boolean value of the provided | 823 // Given a FieldTrialFlags object, returns the boolean value of the provided |
820 // flag. | 824 // flag. |
821 bool GetBoolValueForFlagWithDefault(const std::string& flag, | 825 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
822 bool default_value, | 826 bool default_value, |
823 const FieldTrialFlags& flags) { | 827 const FieldTrialFlags& flags) { |
824 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 828 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
825 } | 829 } |
826 | 830 |
827 } // namespace chrome | 831 } // namespace chrome |
OLD | NEW |