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 NewTabURLState { | |
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; |
99 else if (value == base::ASCIIToUTF16(kInstantSupportDisabled)) | 127 else if (value == base::ASCIIToUTF16(kInstantSupportDisabled)) |
100 return INSTANT_SUPPORT_NO; | 128 return INSTANT_SUPPORT_NO; |
101 else | 129 else |
102 return INSTANT_SUPPORT_UNKNOWN; | 130 return INSTANT_SUPPORT_UNKNOWN; |
103 } | 131 } |
104 | 132 |
105 base::string16 InstantSupportStateToString(InstantSupportState state) { | 133 base::string16 InstantSupportStateToString(InstantSupportState state) { |
106 switch (state) { | 134 switch (state) { |
107 case INSTANT_SUPPORT_NO: | 135 case INSTANT_SUPPORT_NO: |
108 return base::ASCIIToUTF16(kInstantSupportDisabled); | 136 return base::ASCIIToUTF16(kInstantSupportDisabled); |
109 case INSTANT_SUPPORT_YES: | 137 case INSTANT_SUPPORT_YES: |
110 return base::ASCIIToUTF16(kInstantSupportEnabled); | 138 return base::ASCIIToUTF16(kInstantSupportEnabled); |
111 case INSTANT_SUPPORT_UNKNOWN: | 139 case INSTANT_SUPPORT_UNKNOWN: |
112 return base::ASCIIToUTF16(kInstantSupportUnknown); | 140 return base::ASCIIToUTF16(kInstantSupportUnknown); |
113 } | 141 } |
114 return base::ASCIIToUTF16(kInstantSupportUnknown); | 142 return base::ASCIIToUTF16(kInstantSupportUnknown); |
115 } | 143 } |
116 | 144 |
117 TemplateURL* GetDefaultSearchProviderTemplateURL(Profile* profile) { | 145 TemplateURL* GetDefaultSearchProviderTemplateURL(Profile* profile) { |
118 TemplateURLService* template_url_service = | 146 if (profile) { |
119 TemplateURLServiceFactory::GetForProfile(profile); | 147 TemplateURLService* template_url_service = |
120 if (template_url_service) | 148 TemplateURLServiceFactory::GetForProfile(profile); |
121 return template_url_service->GetDefaultSearchProvider(); | 149 if (template_url_service) |
150 return template_url_service->GetDefaultSearchProvider(); | |
151 } | |
122 return NULL; | 152 return NULL; |
123 } | 153 } |
124 | 154 |
125 GURL TemplateURLRefToGURL(const TemplateURLRef& ref, | 155 GURL TemplateURLRefToGURL(const TemplateURLRef& ref, |
126 int start_margin, | 156 int start_margin, |
127 bool append_extra_query_params, | 157 bool append_extra_query_params, |
128 bool force_instant_results) { | 158 bool force_instant_results) { |
129 TemplateURLRef::SearchTermsArgs search_terms_args = | 159 TemplateURLRef::SearchTermsArgs search_terms_args = |
130 TemplateURLRef::SearchTermsArgs(base::string16()); | 160 TemplateURLRef::SearchTermsArgs(base::string16()); |
131 search_terms_args.omnibox_start_margin = start_margin; | 161 search_terms_args.omnibox_start_margin = start_margin; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 ManagedModeURLFilter* url_filter = | 282 ManagedModeURLFilter* url_filter = |
253 managed_user_service->GetURLFilterForUIThread(); | 283 managed_user_service->GetURLFilterForUIThread(); |
254 if (url_filter->GetFilteringBehaviorForURL(url) == | 284 if (url_filter->GetFilteringBehaviorForURL(url) == |
255 ManagedModeURLFilter::BLOCK) { | 285 ManagedModeURLFilter::BLOCK) { |
256 return false; | 286 return false; |
257 } | 287 } |
258 #endif | 288 #endif |
259 return true; | 289 return true; |
260 } | 290 } |
261 | 291 |
292 NewTabURLState IsValidNewTabURL(Profile* profile, const GURL& new_tab_url) { | |
293 if (profile->IsOffTheRecord()) | |
294 return NEW_TAB_URL_INCOGNITO; | |
295 if (!new_tab_url.is_valid()) | |
296 return NEW_TAB_URL_NOT_SET; | |
297 if (!new_tab_url.SchemeIsSecure()) | |
298 return NEW_TAB_URL_INSECURE; | |
299 if (!IsSuggestPrefEnabled(profile)) | |
300 return NEW_TAB_URL_SUGGEST_OFF; | |
301 if (!IsURLAllowedForSupervisedUser(new_tab_url, profile)) | |
302 return NEW_TAB_URL_BLOCKED; | |
303 return NEW_TAB_URL_VALID; | |
304 } | |
305 | |
306 struct NewTabURLDetails { | |
Alexei Svitkine (slow)
2014/02/03 18:08:38
Could probably use a comment briefly outlining wha
samarth
2014/02/03 18:33:31
Done.
| |
307 NewTabURLDetails(const GURL& url, NewTabURLState state) | |
308 : url(url), state(state) {} | |
309 | |
310 static NewTabURLDetails ForProfile(Profile* profile) { | |
311 const GURL local_url(chrome::kChromeSearchLocalNtpUrl); | |
312 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | |
313 if (!profile || !template_url) | |
314 return NewTabURLDetails(local_url, NEW_TAB_URL_BAD); | |
315 | |
316 GURL search_provider_url = TemplateURLRefToGURL( | |
317 template_url->new_tab_url_ref(), kDisableStartMargin, false, false); | |
318 NewTabURLState state = IsValidNewTabURL(profile, search_provider_url); | |
319 switch (state) { | |
320 case NEW_TAB_URL_VALID: | |
321 // We can use the search provider's page. | |
322 return NewTabURLDetails(search_provider_url, state); | |
323 case NEW_TAB_URL_INCOGNITO: | |
324 // Incognito has its own New Tab. | |
325 return NewTabURLDetails(GURL(), state); | |
326 default: | |
327 // Use the local New Tab otherwise. | |
328 return NewTabURLDetails(local_url, NEW_TAB_URL_BAD); | |
Alexei Svitkine (slow)
2014/02/03 18:08:38
Wouldn't this result in the histogram only ever lo
jered1
2014/02/03 18:18:11
Oops, good catch. Should this be state?
samarth
2014/02/03 18:33:31
Yep, thanks for catching that!
| |
329 }; | |
330 } | |
331 | |
332 GURL url; | |
333 NewTabURLState state; | |
334 }; | |
335 | |
262 } // namespace | 336 } // namespace |
263 | 337 |
264 // Negative start-margin values prevent the "es_sm" parameter from being used. | 338 // Negative start-margin values prevent the "es_sm" parameter from being used. |
265 const int kDisableStartMargin = -1; | 339 const int kDisableStartMargin = -1; |
266 | 340 |
267 bool IsInstantExtendedAPIEnabled() { | 341 bool IsInstantExtendedAPIEnabled() { |
268 #if defined(OS_IOS) || defined(OS_ANDROID) | 342 #if defined(OS_IOS) || defined(OS_ANDROID) |
269 return false; | 343 return false; |
270 #else | 344 #else |
271 return true; | 345 return true; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
444 return result; | 518 return result; |
445 for (size_t i = 0; i < template_url->URLCount(); ++i) { | 519 for (size_t i = 0; i < template_url->URLCount(); ++i) { |
446 TemplateURLRef ref(template_url, i); | 520 TemplateURLRef ref(template_url, i); |
447 result.push_back(TemplateURLRefToGURL(ref, kDisableStartMargin, false, | 521 result.push_back(TemplateURLRefToGURL(ref, kDisableStartMargin, false, |
448 false)); | 522 false)); |
449 } | 523 } |
450 return result; | 524 return result; |
451 } | 525 } |
452 | 526 |
453 GURL GetNewTabPageURL(Profile* profile) { | 527 GURL GetNewTabPageURL(Profile* profile) { |
454 if (!profile || profile->IsOffTheRecord()) | 528 return NewTabURLDetails::ForProfile(profile).url; |
455 return GURL(); | |
456 | |
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 } | 529 } |
474 | 530 |
475 GURL GetSearchResultPrefetchBaseURL(Profile* profile) { | 531 GURL GetSearchResultPrefetchBaseURL(Profile* profile) { |
476 return ShouldPrefetchSearchResults() ? | 532 return ShouldPrefetchSearchResults() ? |
477 GetInstantURL(profile, kDisableStartMargin, true) : GURL(); | 533 GetInstantURL(profile, kDisableStartMargin, true) : GURL(); |
478 } | 534 } |
479 | 535 |
480 bool ShouldPrefetchSearchResults() { | 536 bool ShouldPrefetchSearchResults() { |
481 FieldTrialFlags flags; | 537 FieldTrialFlags flags; |
482 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( | 538 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
587 bool HandleNewTabURLRewrite(GURL* url, | 643 bool HandleNewTabURLRewrite(GURL* url, |
588 content::BrowserContext* browser_context) { | 644 content::BrowserContext* browser_context) { |
589 if (!IsInstantExtendedAPIEnabled()) | 645 if (!IsInstantExtendedAPIEnabled()) |
590 return false; | 646 return false; |
591 | 647 |
592 if (!url->SchemeIs(chrome::kChromeUIScheme) || | 648 if (!url->SchemeIs(chrome::kChromeUIScheme) || |
593 url->host() != chrome::kChromeUINewTabHost) | 649 url->host() != chrome::kChromeUINewTabHost) |
594 return false; | 650 return false; |
595 | 651 |
596 Profile* profile = Profile::FromBrowserContext(browser_context); | 652 Profile* profile = Profile::FromBrowserContext(browser_context); |
597 GURL new_tab_url(GetNewTabPageURL(profile)); | 653 NewTabURLDetails details(NewTabURLDetails::ForProfile(profile)); |
598 if (!new_tab_url.is_valid()) | 654 UMA_HISTOGRAM_ENUMERATION("NewTabPage.URLState", |
599 return false; | 655 details.state, NEW_TAB_URL_MAX); |
600 | 656 if (details.url.is_valid()) { |
601 *url = new_tab_url; | 657 *url = details.url; |
602 return true; | 658 return true; |
659 } | |
660 return false; | |
603 } | 661 } |
604 | 662 |
605 bool HandleNewTabURLReverseRewrite(GURL* url, | 663 bool HandleNewTabURLReverseRewrite(GURL* url, |
606 content::BrowserContext* browser_context) { | 664 content::BrowserContext* browser_context) { |
607 if (!IsInstantExtendedAPIEnabled()) | 665 if (!IsInstantExtendedAPIEnabled()) |
608 return false; | 666 return false; |
609 | 667 |
668 // Do nothing in incognito. | |
610 Profile* profile = Profile::FromBrowserContext(browser_context); | 669 Profile* profile = Profile::FromBrowserContext(browser_context); |
611 GURL new_tab_url(GetNewTabPageURL(profile)); | 670 if (profile && profile->IsOffTheRecord()) |
612 if (!new_tab_url.is_valid() || | |
613 !search::MatchesOriginAndPath(new_tab_url, *url)) | |
614 return false; | 671 return false; |
615 | 672 |
616 *url = GURL(chrome::kChromeUINewTabURL); | 673 if (search::MatchesOriginAndPath( |
617 return true; | 674 GURL(chrome::kChromeSearchLocalNtpUrl), *url)) { |
675 *url = GURL(chrome::kChromeUINewTabURL); | |
676 return true; | |
677 } | |
678 | |
679 GURL new_tab_url(GetNewTabPageURL(profile)); | |
680 if (new_tab_url.is_valid() && | |
681 search::MatchesOriginAndPath(new_tab_url, *url)) { | |
682 *url = GURL(chrome::kChromeUINewTabURL); | |
683 return true; | |
684 } | |
685 | |
686 return false; | |
618 } | 687 } |
619 | 688 |
620 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, | 689 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, |
621 content::NavigationEntry* entry) { | 690 content::NavigationEntry* entry) { |
622 if (!entry) | 691 if (!entry) |
623 return; | 692 return; |
624 | 693 |
625 entry->SetExtraData(kInstantSupportStateKey, | 694 entry->SetExtraData(kInstantSupportStateKey, |
626 InstantSupportStateToString(state)); | 695 InstantSupportStateToString(state)); |
627 } | 696 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
703 | 772 |
704 // Given a FieldTrialFlags object, returns the boolean value of the provided | 773 // Given a FieldTrialFlags object, returns the boolean value of the provided |
705 // flag. | 774 // flag. |
706 bool GetBoolValueForFlagWithDefault(const std::string& flag, | 775 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
707 bool default_value, | 776 bool default_value, |
708 const FieldTrialFlags& flags) { | 777 const FieldTrialFlags& flags) { |
709 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 778 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
710 } | 779 } |
711 | 780 |
712 } // namespace chrome | 781 } // namespace chrome |
OLD | NEW |