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

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

Issue 393173002: Moves some functions in search.h to components. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: gn Created 6 years, 5 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
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/metrics/histogram.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 10 matching lines...) Expand all
21 #include "chrome/browser/ui/browser.h" 21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_instant_controller.h" 22 #include "chrome/browser/ui/browser_instant_controller.h"
23 #include "chrome/browser/ui/browser_iterator.h" 23 #include "chrome/browser/ui/browser_iterator.h"
24 #include "chrome/browser/ui/search/instant_search_prerenderer.h" 24 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
25 #include "chrome/common/chrome_switches.h" 25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
27 #include "chrome/common/search_urls.h" 27 #include "chrome/common/search_urls.h"
28 #include "chrome/common/url_constants.h" 28 #include "chrome/common/url_constants.h"
29 #include "components/google/core/browser/google_util.h" 29 #include "components/google/core/browser/google_util.h"
30 #include "components/pref_registry/pref_registry_syncable.h" 30 #include "components/pref_registry/pref_registry_syncable.h"
31 #include "components/search/search.h"
31 #include "components/search_engines/template_url_service.h" 32 #include "components/search_engines/template_url_service.h"
32 #include "components/sessions/serialized_navigation_entry.h" 33 #include "components/sessions/serialized_navigation_entry.h"
33 #include "content/public/browser/navigation_entry.h" 34 #include "content/public/browser/navigation_entry.h"
34 #include "content/public/browser/render_process_host.h" 35 #include "content/public/browser/render_process_host.h"
35 #include "content/public/browser/web_contents.h" 36 #include "content/public/browser/web_contents.h"
36 #include "grit/generated_resources.h" 37 #include "grit/generated_resources.h"
37 #include "ui/base/l10n/l10n_util.h" 38 #include "ui/base/l10n/l10n_util.h"
38 39
39 #if defined(ENABLE_MANAGED_USERS) 40 #if defined(ENABLE_MANAGED_USERS)
40 #include "chrome/browser/supervised_user/supervised_user_service.h" 41 #include "chrome/browser/supervised_user/supervised_user_service.h"
41 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" 42 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
42 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" 43 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
43 #endif 44 #endif
44 45
45 namespace chrome { 46 namespace chrome {
46 47
47 namespace { 48 namespace {
48 49
49 // Configuration options for Embedded Search.
50 // EmbeddedSearch field trials are named in such a way that we can parse out
51 // the experiment configuration from the trial's group name in order to give
52 // us maximum flexability in running experiments.
53 // Field trial groups should be named things like "Group7 espv:2 instant:1".
54 // The first token is always GroupN for some integer N, followed by a
55 // space-delimited list of key:value pairs which correspond to these flags:
56 const char kEmbeddedPageVersionFlagName[] = "espv";
57 #if defined(OS_IOS)
58 const uint64 kEmbeddedPageVersionDefault = 1;
59 #elif defined(OS_ANDROID)
60 const uint64 kEmbeddedPageVersionDefault = 1;
61 // Use this variant to enable EmbeddedSearch SearchBox API in the results page.
62 const uint64 kEmbeddedSearchEnabledVersion = 2;
63 #else
64 const uint64 kEmbeddedPageVersionDefault = 2;
65 #endif
66
67 const char kHideVerbatimFlagName[] = "hide_verbatim"; 50 const char kHideVerbatimFlagName[] = "hide_verbatim";
68 const char kPrefetchSearchResultsFlagName[] = "prefetch_results"; 51 const char kPrefetchSearchResultsFlagName[] = "prefetch_results";
69 const char kPrefetchSearchResultsOnSRP[] = "prefetch_results_srp"; 52 const char kPrefetchSearchResultsOnSRP[] = "prefetch_results_srp";
70 const char kAllowPrefetchNonDefaultMatch[] = "allow_prefetch_non_default_match"; 53 const char kAllowPrefetchNonDefaultMatch[] = "allow_prefetch_non_default_match";
71 const char kPrerenderInstantUrlOnOmniboxFocus[] = 54 const char kPrerenderInstantUrlOnOmniboxFocus[] =
72 "prerender_instant_url_on_omnibox_focus"; 55 "prerender_instant_url_on_omnibox_focus";
73 56
74 // Controls whether to reuse prerendered Instant Search base page to commit any 57 // Controls whether to reuse prerendered Instant Search base page to commit any
75 // search query. 58 // search query.
76 const char kReuseInstantSearchBasePage[] = "reuse_instant_search_base_page"; 59 const char kReuseInstantSearchBasePage[] = "reuse_instant_search_base_page";
77 60
78 // Controls whether to use the alternate Instant search base URL. This allows 61 // Controls whether to use the alternate Instant search base URL. This allows
79 // experimentation of Instant search. 62 // experimentation of Instant search.
80 const char kUseAltInstantURL[] = "use_alternate_instant_url"; 63 const char kUseAltInstantURL[] = "use_alternate_instant_url";
81 const char kAltInstantURLPath[] = "search"; 64 const char kAltInstantURLPath[] = "search";
82 const char kAltInstantURLQueryParams[] = "&qbp=1"; 65 const char kAltInstantURLQueryParams[] = "&qbp=1";
83 66
84 const char kDisplaySearchButtonFlagName[] = "display_search_button"; 67 const char kDisplaySearchButtonFlagName[] = "display_search_button";
85 const char kOriginChipFlagName[] = "origin_chip"; 68 const char kOriginChipFlagName[] = "origin_chip";
86 #if !defined(OS_IOS) && !defined(OS_ANDROID) 69 #if !defined(OS_IOS) && !defined(OS_ANDROID)
87 const char kEnableQueryExtractionFlagName[] = "query_extraction"; 70 const char kEnableQueryExtractionFlagName[] = "query_extraction";
88 #endif 71 #endif
89 const char kShouldShowGoogleLocalNTPFlagName[] = "google_local_ntp"; 72 const char kShouldShowGoogleLocalNTPFlagName[] = "google_local_ntp";
90 73
91 // Constants for the field trial name and group prefix.
92 // Note in M30 and below this field trial was named "InstantExtended" and in
93 // M31 was renamed to EmbeddedSearch for clarity and cleanliness. Since we
94 // can't easilly sync up Finch configs with the pushing of this change to
95 // Dev & Canary, for now the code accepts both names.
96 // TODO(dcblack): Remove the InstantExtended name once M31 hits the Beta
97 // channel.
98 const char kInstantExtendedFieldTrialName[] = "InstantExtended";
99 const char kEmbeddedSearchFieldTrialName[] = "EmbeddedSearch";
100
101 // If the field trial's group name ends with this string its configuration will
102 // be ignored and Instant Extended will not be enabled by default.
103 const char kDisablingSuffix[] = "DISABLED";
104
105 // Status of the New Tab URL for the default Search provider. NOTE: Used in a 74 // Status of the New Tab URL for the default Search provider. NOTE: Used in a
106 // UMA histogram so values should only be added at the end and not reordered. 75 // UMA histogram so values should only be added at the end and not reordered.
107 enum NewTabURLState { 76 enum NewTabURLState {
108 // Valid URL that should be used. 77 // Valid URL that should be used.
109 NEW_TAB_URL_VALID = 0, 78 NEW_TAB_URL_VALID = 0,
110 79
111 // Corrupt state (e.g. no profile or template url). 80 // Corrupt state (e.g. no profile or template url).
112 NEW_TAB_URL_BAD = 1, 81 NEW_TAB_URL_BAD = 1,
113 82
114 // URL should not be used because in incognito window. 83 // URL should not be used because in incognito window.
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 307
339 GURL url; 308 GURL url;
340 NewTabURLState state; 309 NewTabURLState state;
341 }; 310 };
342 311
343 } // namespace 312 } // namespace
344 313
345 // Negative start-margin values prevent the "es_sm" parameter from being used. 314 // Negative start-margin values prevent the "es_sm" parameter from being used.
346 const int kDisableStartMargin = -1; 315 const int kDisableStartMargin = -1;
347 316
348 bool IsInstantExtendedAPIEnabled() {
349 #if defined(OS_IOS)
350 return false;
351 #elif defined(OS_ANDROID)
352 return EmbeddedSearchPageVersion() == kEmbeddedSearchEnabledVersion;
353 #else
354 return true;
355 #endif // defined(OS_IOS)
356 }
357
358 // Determine what embedded search page version to request from the user's
359 // default search provider. If 0, the embedded search UI should not be enabled.
360 uint64 EmbeddedSearchPageVersion() {
361 #if defined(OS_ANDROID)
362 if (CommandLine::ForCurrentProcess()->HasSwitch(
363 switches::kEnableEmbeddedSearchAPI)) {
364 return kEmbeddedSearchEnabledVersion;
365 }
366 #endif
367
368 FieldTrialFlags flags;
369 if (GetFieldTrialInfo(&flags)) {
370 return GetUInt64ValueForFlagWithDefault(kEmbeddedPageVersionFlagName,
371 kEmbeddedPageVersionDefault,
372 flags);
373 }
374 return kEmbeddedPageVersionDefault;
375 }
376
377 std::string InstantExtendedEnabledParam(bool for_search) { 317 std::string InstantExtendedEnabledParam(bool for_search) {
378 if (for_search && !chrome::IsQueryExtractionEnabled()) 318 if (for_search && !chrome::IsQueryExtractionEnabled())
379 return std::string(); 319 return std::string();
380 return std::string(google_util::kInstantExtendedAPIParam) + "=" + 320 return std::string(google_util::kInstantExtendedAPIParam) + "=" +
381 base::Uint64ToString(EmbeddedSearchPageVersion()) + "&"; 321 base::Uint64ToString(EmbeddedSearchPageVersion()) + "&";
382 } 322 }
383 323
384 std::string ForceInstantResultsParam(bool for_prerender) { 324 std::string ForceInstantResultsParam(bool for_prerender) {
385 return (for_prerender || !IsInstantExtendedAPIEnabled()) ? 325 return (for_prerender || !IsInstantExtendedAPIEnabled()) ?
386 "ion=1&" : std::string(); 326 "ion=1&" : std::string();
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 FieldTrialFlags flags; 729 FieldTrialFlags flags;
790 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( 730 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault(
791 kPrefetchSearchResultsOnSRP, false, flags); 731 kPrefetchSearchResultsOnSRP, false, flags);
792 } 732 }
793 733
794 void EnableQueryExtractionForTesting() { 734 void EnableQueryExtractionForTesting() {
795 CommandLine* cl = CommandLine::ForCurrentProcess(); 735 CommandLine* cl = CommandLine::ForCurrentProcess();
796 cl->AppendSwitch(switches::kEnableQueryExtraction); 736 cl->AppendSwitch(switches::kEnableQueryExtraction);
797 } 737 }
798 738
799 bool GetFieldTrialInfo(FieldTrialFlags* flags) {
800 // Get the group name. If the EmbeddedSearch trial doesn't exist, look for
801 // the older InstantExtended name.
802 std::string group_name = base::FieldTrialList::FindFullName(
803 kEmbeddedSearchFieldTrialName);
804 if (group_name.empty()) {
805 group_name = base::FieldTrialList::FindFullName(
806 kInstantExtendedFieldTrialName);
807 }
808
809 if (EndsWith(group_name, kDisablingSuffix, true))
810 return false;
811
812 // We have a valid trial that isn't disabled. Extract the flags.
813 std::string group_prefix(group_name);
814 size_t first_space = group_name.find(" ");
815 if (first_space != std::string::npos) {
816 // There is a flags section of the group name. Split that out and parse it.
817 group_prefix = group_name.substr(0, first_space);
818 if (!base::SplitStringIntoKeyValuePairs(group_name.substr(first_space),
819 ':', ' ', flags)) {
820 // Failed to parse the flags section. Assume the whole group name is
821 // invalid.
822 return false;
823 }
824 }
825 return true;
826 }
827
828 // Given a FieldTrialFlags object, returns the string value of the provided
829 // flag.
830 std::string GetStringValueForFlagWithDefault(const std::string& flag,
831 const std::string& default_value,
832 const FieldTrialFlags& flags) {
833 FieldTrialFlags::const_iterator i;
834 for (i = flags.begin(); i != flags.end(); i++) {
835 if (i->first == flag)
836 return i->second;
837 }
838 return default_value;
839 }
840
841 // Given a FieldTrialFlags object, returns the uint64 value of the provided
842 // flag.
843 uint64 GetUInt64ValueForFlagWithDefault(const std::string& flag,
844 uint64 default_value,
845 const FieldTrialFlags& flags) {
846 uint64 value;
847 std::string str_value =
848 GetStringValueForFlagWithDefault(flag, std::string(), flags);
849 if (base::StringToUint64(str_value, &value))
850 return value;
851 return default_value;
852 }
853
854 // Given a FieldTrialFlags object, returns the boolean value of the provided
855 // flag.
856 bool GetBoolValueForFlagWithDefault(const std::string& flag,
857 bool default_value,
858 const FieldTrialFlags& flags) {
859 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags);
860 }
861
862 bool ShouldUseAltInstantURL() { 739 bool ShouldUseAltInstantURL() {
863 FieldTrialFlags flags; 740 FieldTrialFlags flags;
864 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( 741 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault(
865 kUseAltInstantURL, false, flags); 742 kUseAltInstantURL, false, flags);
866 } 743 }
867 744
868 } // namespace chrome 745 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698