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

Side by Side Diff: chrome/browser/search/search.cc

Issue 150033002: NTP: add histogram to track New Tab page URL status. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address 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
« no previous file with comments | « no previous file | chrome/browser/search/search_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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
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
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 struct NewTabURLDetails {
291 NewTabURLDetails(const GURL& url, NewTabPageURLState state)
292 : url(url), state(state) {}
Jered 2014/02/01 00:01:40 nit: This could have an interface like static NewT
samarth 2014/02/01 01:36:00 Did something like this and also tried to clean th
293 GURL url;
294 NewTabPageURLState state;
295 };
296
297 NewTabPageURLState GetNewTabPageURLState(Profile* profile, GURL* new_tab_url) {
298 if (!profile)
299 return NEW_TAB_URL_BAD;
300
301 if (profile->IsOffTheRecord())
302 return NEW_TAB_URL_INCOGNITO;
303
304 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
305 if (!template_url)
306 return NEW_TAB_URL_BAD;
307
308 *new_tab_url = TemplateURLRefToGURL(template_url->new_tab_url_ref(),
309 kDisableStartMargin, false, false);
310 if (!new_tab_url->is_valid())
311 return NEW_TAB_URL_NOT_SET;
312
313 if (!new_tab_url->SchemeIsSecure())
314 return NEW_TAB_URL_INSECURE;
315
316 if (!IsSuggestPrefEnabled(profile))
317 return NEW_TAB_URL_SUGGEST_OFF;
318
319 if (!IsURLAllowedForSupervisedUser(*new_tab_url, profile))
320 return NEW_TAB_URL_BLOCKED;
321
322 return NEW_TAB_URL_VALID;
323 }
324
325 NewTabURLDetails GetNewTabPageURLDetails(Profile* profile) {
326 GURL new_tab_url;
327 NewTabPageURLState state = GetNewTabPageURLState(profile, &new_tab_url);
328 if (state == NEW_TAB_URL_INCOGNITO)
329 new_tab_url = GURL();
330 else if (state != NEW_TAB_URL_VALID)
331 new_tab_url = GURL(chrome::kChromeSearchLocalNtpUrl);
332
333 return NewTabURLDetails(new_tab_url, 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
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 GetNewTabPageURLDetails(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
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 = GetNewTabPageURLDetails(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
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
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/search/search_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698