OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/search/search.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_split.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "components/search/search_switches.h" |
| 14 |
| 15 namespace chrome { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Configuration options for Embedded Search. |
| 20 // EmbeddedSearch field trials are named in such a way that we can parse out |
| 21 // the experiment configuration from the trial's group name in order to give |
| 22 // us maximum flexability in running experiments. |
| 23 // Field trial groups should be named things like "Group7 espv:2 instant:1". |
| 24 // The first token is always GroupN for some integer N, followed by a |
| 25 // space-delimited list of key:value pairs which correspond to these flags: |
| 26 const char kEmbeddedPageVersionFlagName[] = "espv"; |
| 27 |
| 28 #if defined(OS_IOS) |
| 29 const uint64 kEmbeddedPageVersionDefault = 1; |
| 30 #elif defined(OS_ANDROID) |
| 31 const uint64 kEmbeddedPageVersionDefault = 1; |
| 32 // Use this variant to enable EmbeddedSearch SearchBox API in the results page. |
| 33 const uint64 kEmbeddedSearchEnabledVersion = 2; |
| 34 #else |
| 35 const uint64 kEmbeddedPageVersionDefault = 2; |
| 36 #endif |
| 37 |
| 38 // Constants for the field trial name and group prefix. |
| 39 // Note in M30 and below this field trial was named "InstantExtended" and in |
| 40 // M31 was renamed to EmbeddedSearch for clarity and cleanliness. Since we |
| 41 // can't easilly sync up Finch configs with the pushing of this change to |
| 42 // Dev & Canary, for now the code accepts both names. |
| 43 // TODO(dcblack): Remove the InstantExtended name once M31 hits the Beta |
| 44 // channel. |
| 45 const char kInstantExtendedFieldTrialName[] = "InstantExtended"; |
| 46 const char kEmbeddedSearchFieldTrialName[] = "EmbeddedSearch"; |
| 47 |
| 48 // If the field trial's group name ends with this string its configuration will |
| 49 // be ignored and Instant Extended will not be enabled by default. |
| 50 const char kDisablingSuffix[] = "DISABLED"; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 bool IsInstantExtendedAPIEnabled() { |
| 55 #if defined(OS_IOS) |
| 56 return false; |
| 57 #elif defined(OS_ANDROID) |
| 58 return EmbeddedSearchPageVersion() == kEmbeddedSearchEnabledVersion; |
| 59 #else |
| 60 return true; |
| 61 #endif // defined(OS_IOS) |
| 62 } |
| 63 |
| 64 // Determine what embedded search page version to request from the user's |
| 65 // default search provider. If 0, the embedded search UI should not be enabled. |
| 66 uint64 EmbeddedSearchPageVersion() { |
| 67 #if defined(OS_ANDROID) |
| 68 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 69 switches::kEnableEmbeddedSearchAPI)) { |
| 70 return kEmbeddedSearchEnabledVersion; |
| 71 } |
| 72 #endif |
| 73 |
| 74 FieldTrialFlags flags; |
| 75 if (GetFieldTrialInfo(&flags)) { |
| 76 return GetUInt64ValueForFlagWithDefault(kEmbeddedPageVersionFlagName, |
| 77 kEmbeddedPageVersionDefault, |
| 78 flags); |
| 79 } |
| 80 return kEmbeddedPageVersionDefault; |
| 81 } |
| 82 |
| 83 bool GetFieldTrialInfo(FieldTrialFlags* flags) { |
| 84 // Get the group name. If the EmbeddedSearch trial doesn't exist, look for |
| 85 // the older InstantExtended name. |
| 86 std::string group_name = base::FieldTrialList::FindFullName( |
| 87 kEmbeddedSearchFieldTrialName); |
| 88 if (group_name.empty()) { |
| 89 group_name = base::FieldTrialList::FindFullName( |
| 90 kInstantExtendedFieldTrialName); |
| 91 } |
| 92 |
| 93 if (EndsWith(group_name, kDisablingSuffix, true)) |
| 94 return false; |
| 95 |
| 96 // We have a valid trial that isn't disabled. Extract the flags. |
| 97 std::string group_prefix(group_name); |
| 98 size_t first_space = group_name.find(" "); |
| 99 if (first_space != std::string::npos) { |
| 100 // There is a flags section of the group name. Split that out and parse it. |
| 101 group_prefix = group_name.substr(0, first_space); |
| 102 if (!base::SplitStringIntoKeyValuePairs(group_name.substr(first_space), |
| 103 ':', ' ', flags)) { |
| 104 // Failed to parse the flags section. Assume the whole group name is |
| 105 // invalid. |
| 106 return false; |
| 107 } |
| 108 } |
| 109 return true; |
| 110 } |
| 111 |
| 112 // Given a FieldTrialFlags object, returns the string value of the provided |
| 113 // flag. |
| 114 std::string GetStringValueForFlagWithDefault(const std::string& flag, |
| 115 const std::string& default_value, |
| 116 const FieldTrialFlags& flags) { |
| 117 FieldTrialFlags::const_iterator i; |
| 118 for (i = flags.begin(); i != flags.end(); i++) { |
| 119 if (i->first == flag) |
| 120 return i->second; |
| 121 } |
| 122 return default_value; |
| 123 } |
| 124 |
| 125 // Given a FieldTrialFlags object, returns the uint64 value of the provided |
| 126 // flag. |
| 127 uint64 GetUInt64ValueForFlagWithDefault(const std::string& flag, |
| 128 uint64 default_value, |
| 129 const FieldTrialFlags& flags) { |
| 130 uint64 value; |
| 131 std::string str_value = |
| 132 GetStringValueForFlagWithDefault(flag, std::string(), flags); |
| 133 if (base::StringToUint64(str_value, &value)) |
| 134 return value; |
| 135 return default_value; |
| 136 } |
| 137 |
| 138 // Given a FieldTrialFlags object, returns the boolean value of the provided |
| 139 // flag. |
| 140 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
| 141 bool default_value, |
| 142 const FieldTrialFlags& flags) { |
| 143 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
| 144 } |
| 145 |
| 146 } // namespace chrome |
OLD | NEW |