OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/ui/search/search.h" | 5 #include "chrome/browser/ui/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/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // us maximum flexability in running experiments. | 26 // us maximum flexability in running experiments. |
27 // Field trials should be named things like "Group7 espv:2 themes:0". | 27 // Field trials should be named things like "Group7 espv:2 themes:0". |
28 // The first token is always GroupN for some integer N, followed by a | 28 // The first token is always GroupN for some integer N, followed by a |
29 // space-delimited list of key:value pairs which correspond to these flags: | 29 // space-delimited list of key:value pairs which correspond to these flags: |
30 const char kEnableOnThemesFlagName[] = "themes"; | 30 const char kEnableOnThemesFlagName[] = "themes"; |
31 const bool kEnableOnThemesDefault = false; | 31 const bool kEnableOnThemesDefault = false; |
32 | 32 |
33 const char kEmbeddedPageVersionFlagName[] = "espv"; | 33 const char kEmbeddedPageVersionFlagName[] = "espv"; |
34 const int kEmbeddedPageVersionDefault = 1; | 34 const int kEmbeddedPageVersionDefault = 1; |
35 | 35 |
| 36 const char kInstantExtendedActivationName[] = "instant"; |
| 37 const chrome::search::InstantExtendedDefault kInstantExtendedActivationDefault = |
| 38 chrome::search::INSTANT_USE_EXISTING; |
| 39 |
36 // Constants for the field trial name and group prefix. | 40 // Constants for the field trial name and group prefix. |
37 const char kInstantExtendedFieldTrialName[] = "InstantExtended"; | 41 const char kInstantExtendedFieldTrialName[] = "InstantExtended"; |
38 const char kGroupNumberPrefix[] = "Group"; | 42 const char kGroupNumberPrefix[] = "Group"; |
39 | 43 |
40 // If the field trial's group name ends with this string its configuration will | 44 // If the field trial's group name ends with this string its configuration will |
41 // be ignored and Instant Extended will not be enabled by default. | 45 // be ignored and Instant Extended will not be enabled by default. |
42 const char kDisablingSuffix[] = "DISABLED"; | 46 const char kDisablingSuffix[] = "DISABLED"; |
43 | 47 |
44 } // namespace | 48 } // namespace |
45 | 49 |
46 namespace chrome { | 50 namespace chrome { |
47 namespace search { | 51 namespace search { |
48 | 52 |
| 53 InstantExtendedDefault InstantExtendedDefaultFromInt64(int64 default_value) { |
| 54 switch (default_value) { |
| 55 case 0: return INSTANT_FORCE_ON; |
| 56 case 1: return INSTANT_USE_EXISTING; |
| 57 case 2: return INSTANT_FORCE_OFF; |
| 58 default: return INSTANT_USE_EXISTING; |
| 59 } |
| 60 } |
| 61 |
| 62 InstantExtendedDefault GetInstantExtendedDefaultSetting() { |
| 63 InstantExtendedDefault default_setting = INSTANT_USE_EXISTING; |
| 64 |
| 65 FieldTrialFlags flags; |
| 66 if (GetFieldTrialInfo( |
| 67 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName), |
| 68 &flags, NULL)) { |
| 69 uint64 trial_default = GetUInt64ValueForFlagWithDefault( |
| 70 kInstantExtendedActivationName, |
| 71 kInstantExtendedActivationDefault, |
| 72 flags); |
| 73 default_setting = InstantExtendedDefaultFromInt64(trial_default); |
| 74 } |
| 75 |
| 76 return default_setting; |
| 77 } |
| 78 |
49 // Check whether or not the Extended API should be used on the given profile. | 79 // Check whether or not the Extended API should be used on the given profile. |
50 bool IsInstantExtendedAPIEnabled(Profile* profile) { | 80 bool IsInstantExtendedAPIEnabled(Profile* profile) { |
51 return EmbeddedSearchPageVersion(profile) != 0; | 81 return EmbeddedSearchPageVersion(profile) != 0; |
52 } | 82 } |
53 | 83 |
54 // Determine what embedded search page version to request from the user's | 84 // Determine what embedded search page version to request from the user's |
55 // default search provider. If 0, the embedded search UI should not be enabled. | 85 // default search provider. If 0, the embedded search UI should not be enabled. |
56 // Note that the profile object here isn't const because we need to determine | 86 // Note that the profile object here isn't const because we need to determine |
57 // whether or not the user has a theme installed as part of this check, and | 87 // whether or not the user has a theme installed as part of this check, and |
58 // that logic requires a non-const profile for whatever reason. | 88 // that logic requires a non-const profile for whatever reason. |
59 uint64 EmbeddedSearchPageVersion(Profile* profile) { | 89 uint64 EmbeddedSearchPageVersion(Profile* profile) { |
60 // Incognito windows do not currently use the embedded search API. | 90 // Incognito windows do not currently use the embedded search API. |
61 if (!profile || profile->IsOffTheRecord()) | 91 if (!profile || profile->IsOffTheRecord()) |
62 return 0; | 92 return 0; |
63 | 93 |
64 // Check Finch field trials. | 94 // Check Finch field trials. |
65 FieldTrialFlags flags; | 95 FieldTrialFlags flags; |
66 uint64 group_number = 0; | 96 if (GetFieldTrialInfo( |
67 base::FieldTrial* trial = | 97 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName), |
68 base::FieldTrialList::Find(kInstantExtendedFieldTrialName); | 98 &flags, NULL)) { |
69 if (trial) { | |
70 std::string group_name = trial->group_name(); | |
71 GetFieldTrialInfo(group_name, &flags, &group_number); | |
72 } | |
73 | |
74 if (group_number > 0) { | |
75 uint64 espv = GetUInt64ValueForFlagWithDefault( | 99 uint64 espv = GetUInt64ValueForFlagWithDefault( |
76 kEmbeddedPageVersionFlagName, | 100 kEmbeddedPageVersionFlagName, |
77 kEmbeddedPageVersionDefault, | 101 kEmbeddedPageVersionDefault, |
78 flags); | 102 flags); |
79 | 103 |
80 // Check for themes. | 104 // Check for themes. |
81 bool has_theme = false; | 105 bool has_theme = false; |
82 #if !defined(OS_ANDROID) | 106 #if !defined(OS_ANDROID) |
83 has_theme = | 107 has_theme = |
84 !ThemeServiceFactory::GetForProfile(profile)->UsingDefaultTheme(); | 108 !ThemeServiceFactory::GetForProfile(profile)->UsingDefaultTheme(); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 if (!command_line->HasSwitch(switches::kInstantURL)) | 168 if (!command_line->HasSwitch(switches::kInstantURL)) |
145 return false; | 169 return false; |
146 | 170 |
147 GURL instant_url(command_line->GetSwitchValueASCII(switches::kInstantURL)); | 171 GURL instant_url(command_line->GetSwitchValueASCII(switches::kInstantURL)); |
148 return url.scheme() == instant_url.scheme() && | 172 return url.scheme() == instant_url.scheme() && |
149 url.host() == instant_url.host() && | 173 url.host() == instant_url.host() && |
150 url.port() == instant_url.port() && | 174 url.port() == instant_url.port() && |
151 url.path() == instant_url.path(); | 175 url.path() == instant_url.path(); |
152 } | 176 } |
153 | 177 |
154 // Given a field trial group name in the above format, parses out the group | 178 bool GetFieldTrialInfo(const std::string& group_name, |
155 // number and configuration flags. Will return a group number of 0 on error. | |
156 void GetFieldTrialInfo(const std::string& group_name, | |
157 FieldTrialFlags* flags, | 179 FieldTrialFlags* flags, |
158 uint64* group_number) { | 180 uint64* group_number) { |
159 if (!EndsWith(group_name, kDisablingSuffix, true) && | 181 if (EndsWith(group_name, kDisablingSuffix, true) || |
160 StartsWithASCII(group_name, kGroupNumberPrefix, true)) { | 182 !StartsWithASCII(group_name, kGroupNumberPrefix, true)) { |
161 // We have a valid trial that starts with "Group" and isn't disabled. | 183 return false; |
162 size_t first_space = group_name.find(" "); | 184 } |
163 std::string group_prefix = group_name; | 185 |
164 if (first_space != std::string::npos) { | 186 // We have a valid trial that starts with "Group" and isn't disabled. |
165 // There is a flags section of the group name. Split that out and parse | 187 // First extract the flags. |
166 // it. | 188 std::string group_prefix(group_name); |
167 group_prefix = group_name.substr(0, first_space); | 189 |
168 base::SplitStringIntoKeyValuePairs( | 190 size_t first_space = group_name.find(" "); |
169 group_name.substr(first_space), ':', ' ', flags); | 191 if (first_space != std::string::npos) { |
170 } | 192 // There is a flags section of the group name. Split that out and parse |
171 if (!base::StringToUint64(group_prefix.substr(strlen(kGroupNumberPrefix)), | 193 // it. |
172 group_number)) { | 194 group_prefix = group_name.substr(0, first_space); |
173 // Could not parse group number. | 195 if (!base::SplitStringIntoKeyValuePairs(group_name.substr(first_space), |
174 *group_number = 0; | 196 ':', ' ', flags)) { |
| 197 // Failed to parse the flags section. Assume the whole group name is |
| 198 // invalid. |
| 199 return false; |
175 } | 200 } |
176 } | 201 } |
| 202 |
| 203 // Now extract the group number, making sure we get a non-zero value. |
| 204 uint64 temp_group_number = 0; |
| 205 if (!base::StringToUint64(group_prefix.substr(strlen(kGroupNumberPrefix)), |
| 206 &temp_group_number) || |
| 207 temp_group_number == 0) { |
| 208 return false; |
| 209 } |
| 210 |
| 211 if (group_number) |
| 212 *group_number = temp_group_number; |
| 213 |
| 214 return true; |
177 } | 215 } |
178 | 216 |
179 // Given a FieldTrialFlags object, returns the string value of the provided | 217 // Given a FieldTrialFlags object, returns the string value of the provided |
180 // flag. | 218 // flag. |
181 std::string GetStringValueForFlagWithDefault( | 219 std::string GetStringValueForFlagWithDefault( |
182 const std::string& flag, | 220 const std::string& flag, |
183 const std::string& default_value, | 221 const std::string& default_value, |
184 FieldTrialFlags& flags) { | 222 FieldTrialFlags& flags) { |
185 FieldTrialFlags::const_iterator i; | 223 FieldTrialFlags::const_iterator i; |
186 for (i = flags.begin(); i != flags.end(); i++) { | 224 for (i = flags.begin(); i != flags.end(); i++) { |
(...skipping 16 matching lines...) Expand all Loading... |
203 | 241 |
204 // Given a FieldTrialFlags object, returns the boolean value of the provided | 242 // Given a FieldTrialFlags object, returns the boolean value of the provided |
205 // flag. | 243 // flag. |
206 bool GetBoolValueForFlagWithDefault( | 244 bool GetBoolValueForFlagWithDefault( |
207 const std::string& flag, bool default_value, FieldTrialFlags& flags) { | 245 const std::string& flag, bool default_value, FieldTrialFlags& flags) { |
208 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 246 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
209 } | 247 } |
210 | 248 |
211 } // namespace search | 249 } // namespace search |
212 } // namespace chrome | 250 } // namespace chrome |
OLD | NEW |