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/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/google/google_util.h" | 15 #include "chrome/browser/google/google_util.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 17 #include "chrome/browser/search/instant_service.h" | 18 #include "chrome/browser/search/instant_service.h" |
| 18 #include "chrome/browser/search/instant_service_factory.h" | 19 #include "chrome/browser/search/instant_service_factory.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 // Dev & Canary, for now the code accepts both names. | 80 // Dev & Canary, for now the code accepts both names. |
| 80 // TODO(dcblack): Remove the InstantExtended name once M31 hits the Beta | 81 // TODO(dcblack): Remove the InstantExtended name once M31 hits the Beta |
| 81 // channel. | 82 // channel. |
| 82 const char kInstantExtendedFieldTrialName[] = "InstantExtended"; | 83 const char kInstantExtendedFieldTrialName[] = "InstantExtended"; |
| 83 const char kEmbeddedSearchFieldTrialName[] = "EmbeddedSearch"; | 84 const char kEmbeddedSearchFieldTrialName[] = "EmbeddedSearch"; |
| 84 | 85 |
| 85 // If the field trial's group name ends with this string its configuration will | 86 // If the field trial's group name ends with this string its configuration will |
| 86 // be ignored and Instant Extended will not be enabled by default. | 87 // be ignored and Instant Extended will not be enabled by default. |
| 87 const char kDisablingSuffix[] = "DISABLED"; | 88 const char kDisablingSuffix[] = "DISABLED"; |
| 88 | 89 |
| 90 // Status of the New Tab URL for the default Search provider. NOTE: Used in a | |
| 91 // UMA histogram so values should only be added at the end and not reordered. | |
| 92 enum NewTabPageURLState { | |
| 93 // Valid URL that should be used. | |
| 94 NEW_TAB_URL_VALID = 0, | |
| 95 | |
| 96 // Corrupt state (e.g. no profile or template url). | |
| 97 NEW_TAB_URL_BAD = 1, | |
| 98 | |
| 99 // URL should not be used because in incognito window. | |
| 100 NEW_TAB_URL_INCOGNITO = 2, | |
| 101 | |
| 102 // No New Tab URL set for provider. | |
| 103 NEW_TAB_URL_NOT_SET = 3, | |
| 104 | |
| 105 // URL is not secure. | |
| 106 NEW_TAB_URL_INSECURE = 4, | |
| 107 | |
| 108 // URL should not be used because Suggest is disabled. | |
| 109 NEW_TAB_URL_SUGGEST_OFF = 5, | |
| 110 | |
| 111 // URL should not be used because it is blocked for a supervised user. | |
| 112 NEW_TAB_URL_BLOCKED = 6, | |
| 113 | |
| 114 NEW_TAB_URL_MAX | |
| 115 }; | |
| 116 | |
| 89 // Used to set the Instant support state of the Navigation entry. | 117 // Used to set the Instant support state of the Navigation entry. |
| 90 const char kInstantSupportStateKey[] = "instant_support_state"; | 118 const char kInstantSupportStateKey[] = "instant_support_state"; |
| 91 | 119 |
| 92 const char kInstantSupportEnabled[] = "Instant support enabled"; | 120 const char kInstantSupportEnabled[] = "Instant support enabled"; |
| 93 const char kInstantSupportDisabled[] = "Instant support disabled"; | 121 const char kInstantSupportDisabled[] = "Instant support disabled"; |
| 94 const char kInstantSupportUnknown[] = "Instant support unknown"; | 122 const char kInstantSupportUnknown[] = "Instant support unknown"; |
| 95 | 123 |
| 96 InstantSupportState StringToInstantSupportState(const base::string16& value) { | 124 InstantSupportState StringToInstantSupportState(const base::string16& value) { |
| 97 if (value == base::ASCIIToUTF16(kInstantSupportEnabled)) | 125 if (value == base::ASCIIToUTF16(kInstantSupportEnabled)) |
| 98 return INSTANT_SUPPORT_YES; | 126 return INSTANT_SUPPORT_YES; |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 ManagedModeURLFilter* url_filter = | 280 ManagedModeURLFilter* url_filter = |
| 253 managed_user_service->GetURLFilterForUIThread(); | 281 managed_user_service->GetURLFilterForUIThread(); |
| 254 if (url_filter->GetFilteringBehaviorForURL(url) == | 282 if (url_filter->GetFilteringBehaviorForURL(url) == |
| 255 ManagedModeURLFilter::BLOCK) { | 283 ManagedModeURLFilter::BLOCK) { |
| 256 return false; | 284 return false; |
| 257 } | 285 } |
| 258 #endif | 286 #endif |
| 259 return true; | 287 return true; |
| 260 } | 288 } |
| 261 | 289 |
| 290 NewTabPageURLState GetNewTabPageURLState(Profile* profile, GURL* new_tab_url) { | |
|
Jered
2014/01/31 21:56:23
See comment below. How about adding
struct NewTabU
samarth
2014/01/31 23:52:58
OK, I did something like this. WDYT?
| |
| 291 if (!profile) | |
| 292 return NEW_TAB_URL_BAD; | |
| 293 | |
| 294 if (profile->IsOffTheRecord()) | |
| 295 return NEW_TAB_URL_INCOGNITO; | |
| 296 | |
| 297 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | |
| 298 if (!template_url) | |
| 299 return NEW_TAB_URL_BAD; | |
| 300 | |
| 301 *new_tab_url = TemplateURLRefToGURL(template_url->new_tab_url_ref(), | |
| 302 kDisableStartMargin, false, false); | |
| 303 if (!new_tab_url->is_valid()) | |
| 304 return NEW_TAB_URL_NOT_SET; | |
| 305 | |
| 306 if (!new_tab_url->SchemeIsSecure()) | |
| 307 return NEW_TAB_URL_INSECURE; | |
| 308 | |
| 309 if (!IsSuggestPrefEnabled(profile)) | |
| 310 return NEW_TAB_URL_SUGGEST_OFF; | |
| 311 | |
| 312 if (!IsURLAllowedForSupervisedUser(*new_tab_url, profile)) | |
| 313 return NEW_TAB_URL_BLOCKED; | |
| 314 | |
| 315 return NEW_TAB_URL_VALID; | |
| 316 } | |
| 317 | |
| 262 } // namespace | 318 } // namespace |
| 263 | 319 |
| 264 // Negative start-margin values prevent the "es_sm" parameter from being used. | 320 // Negative start-margin values prevent the "es_sm" parameter from being used. |
| 265 const int kDisableStartMargin = -1; | 321 const int kDisableStartMargin = -1; |
| 266 | 322 |
| 267 bool IsInstantExtendedAPIEnabled() { | 323 bool IsInstantExtendedAPIEnabled() { |
| 268 #if defined(OS_IOS) || defined(OS_ANDROID) | 324 #if defined(OS_IOS) || defined(OS_ANDROID) |
| 269 return false; | 325 return false; |
| 270 #else | 326 #else |
| 271 return true; | 327 return true; |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 if (!template_url) | 499 if (!template_url) |
| 444 return result; | 500 return result; |
| 445 for (size_t i = 0; i < template_url->URLCount(); ++i) { | 501 for (size_t i = 0; i < template_url->URLCount(); ++i) { |
| 446 TemplateURLRef ref(template_url, i); | 502 TemplateURLRef ref(template_url, i); |
| 447 result.push_back(TemplateURLRefToGURL(ref, kDisableStartMargin, false, | 503 result.push_back(TemplateURLRefToGURL(ref, kDisableStartMargin, false, |
| 448 false)); | 504 false)); |
| 449 } | 505 } |
| 450 return result; | 506 return result; |
| 451 } | 507 } |
| 452 | 508 |
| 453 GURL GetNewTabPageURL(Profile* profile) { | 509 GURL GetNewTabPageURL(Profile* profile) { |
|
Jered
2014/01/31 21:56:23
It'd be nice to keep the property that this functi
samarth
2014/01/31 23:52:58
Done.
| |
| 454 if (!profile || profile->IsOffTheRecord()) | 510 GURL new_tab_url; |
| 455 return GURL(); | 511 return GetNewTabPageURLState(profile, &new_tab_url) == NEW_TAB_URL_VALID ? |
| 456 | 512 new_tab_url : GURL(); |
| 457 if (!IsSuggestPrefEnabled(profile)) | |
| 458 return GURL(chrome::kChromeSearchLocalNtpUrl); | |
| 459 | |
| 460 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | |
| 461 if (!template_url) | |
| 462 return GURL(chrome::kChromeSearchLocalNtpUrl); | |
| 463 | |
| 464 GURL url(TemplateURLRefToGURL(template_url->new_tab_url_ref(), | |
| 465 kDisableStartMargin, false, false)); | |
| 466 if (!url.is_valid() || !url.SchemeIsSecure()) | |
| 467 return GURL(chrome::kChromeSearchLocalNtpUrl); | |
| 468 | |
| 469 if (!IsURLAllowedForSupervisedUser(url, profile)) | |
| 470 return GURL(chrome::kChromeSearchLocalNtpUrl); | |
| 471 | |
| 472 return url; | |
| 473 } | 513 } |
| 474 | 514 |
| 475 GURL GetSearchResultPrefetchBaseURL(Profile* profile) { | 515 GURL GetSearchResultPrefetchBaseURL(Profile* profile) { |
| 476 return ShouldPrefetchSearchResults() ? | 516 return ShouldPrefetchSearchResults() ? |
| 477 GetInstantURL(profile, kDisableStartMargin, true) : GURL(); | 517 GetInstantURL(profile, kDisableStartMargin, true) : GURL(); |
| 478 } | 518 } |
| 479 | 519 |
| 480 bool ShouldPrefetchSearchResults() { | 520 bool ShouldPrefetchSearchResults() { |
| 481 FieldTrialFlags flags; | 521 FieldTrialFlags flags; |
| 482 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( | 522 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 bool HandleNewTabURLRewrite(GURL* url, | 627 bool HandleNewTabURLRewrite(GURL* url, |
| 588 content::BrowserContext* browser_context) { | 628 content::BrowserContext* browser_context) { |
| 589 if (!IsInstantExtendedAPIEnabled()) | 629 if (!IsInstantExtendedAPIEnabled()) |
| 590 return false; | 630 return false; |
| 591 | 631 |
| 592 if (!url->SchemeIs(chrome::kChromeUIScheme) || | 632 if (!url->SchemeIs(chrome::kChromeUIScheme) || |
| 593 url->host() != chrome::kChromeUINewTabHost) | 633 url->host() != chrome::kChromeUINewTabHost) |
| 594 return false; | 634 return false; |
| 595 | 635 |
| 596 Profile* profile = Profile::FromBrowserContext(browser_context); | 636 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 597 GURL new_tab_url(GetNewTabPageURL(profile)); | 637 GURL new_tab_url; |
| 598 if (!new_tab_url.is_valid()) | 638 NewTabPageURLState url_state = GetNewTabPageURLState(profile, &new_tab_url); |
| 599 return false; | 639 UMA_HISTOGRAM_ENUMERATION("NewTab.URLState", url_state, NEW_TAB_URL_MAX); |
| 600 | 640 |
| 601 *url = new_tab_url; | 641 if (url_state == NEW_TAB_URL_VALID) { |
| 602 return true; | 642 *url = new_tab_url; |
| 643 return true; | |
| 644 } | |
| 645 | |
| 646 if (url_state != NEW_TAB_URL_INCOGNITO) { | |
| 647 // For non-incognito profiles, use the local NTP when the default search | |
| 648 // provider has no valid new tab url. | |
| 649 *url = GURL(chrome::kChromeSearchLocalNtpUrl); | |
| 650 return true; | |
| 651 } | |
| 652 | |
| 653 return false; | |
| 603 } | 654 } |
| 604 | 655 |
| 605 bool HandleNewTabURLReverseRewrite(GURL* url, | 656 bool HandleNewTabURLReverseRewrite(GURL* url, |
| 606 content::BrowserContext* browser_context) { | 657 content::BrowserContext* browser_context) { |
| 607 if (!IsInstantExtendedAPIEnabled()) | 658 if (!IsInstantExtendedAPIEnabled()) |
| 608 return false; | 659 return false; |
| 609 | 660 |
| 661 // Do nothing in incognito. | |
| 610 Profile* profile = Profile::FromBrowserContext(browser_context); | 662 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 611 GURL new_tab_url(GetNewTabPageURL(profile)); | 663 if (profile && profile->IsOffTheRecord()) |
| 612 if (!new_tab_url.is_valid() || | |
| 613 !search::MatchesOriginAndPath(new_tab_url, *url)) | |
| 614 return false; | 664 return false; |
| 615 | 665 |
| 616 *url = GURL(chrome::kChromeUINewTabURL); | 666 if (search::MatchesOriginAndPath( |
| 617 return true; | 667 GURL(chrome::kChromeSearchLocalNtpUrl), *url)) { |
| 668 *url = GURL(chrome::kChromeUINewTabURL); | |
| 669 return true; | |
| 670 } | |
| 671 | |
| 672 GURL new_tab_url(GetNewTabPageURL(profile)); | |
| 673 if (new_tab_url.is_valid() && | |
| 674 search::MatchesOriginAndPath(new_tab_url, *url)) { | |
| 675 *url = GURL(chrome::kChromeUINewTabURL); | |
| 676 return true; | |
| 677 } | |
| 678 | |
| 679 return false; | |
| 618 } | 680 } |
| 619 | 681 |
| 620 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, | 682 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, |
| 621 content::NavigationEntry* entry) { | 683 content::NavigationEntry* entry) { |
| 622 if (!entry) | 684 if (!entry) |
| 623 return; | 685 return; |
| 624 | 686 |
| 625 entry->SetExtraData(kInstantSupportStateKey, | 687 entry->SetExtraData(kInstantSupportStateKey, |
| 626 InstantSupportStateToString(state)); | 688 InstantSupportStateToString(state)); |
| 627 } | 689 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 703 | 765 |
| 704 // Given a FieldTrialFlags object, returns the boolean value of the provided | 766 // Given a FieldTrialFlags object, returns the boolean value of the provided |
| 705 // flag. | 767 // flag. |
| 706 bool GetBoolValueForFlagWithDefault(const std::string& flag, | 768 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
| 707 bool default_value, | 769 bool default_value, |
| 708 const FieldTrialFlags& flags) { | 770 const FieldTrialFlags& flags) { |
| 709 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 771 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
| 710 } | 772 } |
| 711 | 773 |
| 712 } // namespace chrome | 774 } // namespace chrome |
| OLD | NEW |