Chromium Code Reviews| 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_util.h" | 11 #include "base/string_util.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/themes/theme_service.h" | |
| 14 #include "chrome/browser/themes/theme_service_factory.h" | |
| 12 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/chrome_version_info.h" | 16 #include "chrome/common/chrome_version_info.h" |
| 14 | 17 |
| 15 namespace chrome { | 18 namespace chrome { |
| 16 namespace search { | 19 namespace search { |
| 17 | 20 |
| 18 bool IsInstantExtendedAPIEnabled(const Profile* profile) { | 21 // Configuration options for Embedded Search. |
| 22 // InstantExtended field trials are named in such a way that we can parse out | |
| 23 // the experiment configuration from the trial's group name in order to give | |
| 24 // us maximum flexability in running experiments. | |
| 25 // Field trials should be named things like "Group7 espv:2 themes:0" - | |
|
dhollowa
2013/01/04 17:11:49
nit: period "." at end of sentence, not "-".
David Black
2013/01/04 19:55:32
Done.
| |
| 26 // The first token is always GroupN for some integer N, followed by a | |
| 27 // space-delimited list of key:value pairs which correspond to these flags: | |
| 28 const char kEnableOnThemesFlagName[] = "themes"; | |
| 29 const bool kEnableOnThemesDefault = false; | |
| 30 | |
| 31 const char kEmbeddedPageVersionFlagName[] = "espv"; | |
| 32 const int kEmbeddedPageVersionDefault = 1; | |
| 33 | |
| 34 // Constants for the field trial name and group prefix. | |
| 35 const char kInstantExtendedFieldTrialName[] = "InstantExtended"; | |
| 36 const char kGroupNumberPrefix[] = "Group"; | |
| 37 | |
| 38 // If the field trial's group name ends with this string its configuration will | |
| 39 // be ignored and Instant Extended will not be enabled by default. | |
| 40 const char kDisablingSuffix[] = "DISABLED"; | |
| 41 | |
| 42 // Given a field trial group name in the above format, parses out the group | |
| 43 // number and configuration flags. Will return a group number of 0 on error. | |
| 44 void GetFieldTrialInfo(const std::string& group_name, | |
| 45 FieldTrialFlags* flags, | |
| 46 uint64* group_number) { | |
| 47 if (!EndsWith(group_name, kDisablingSuffix, true) && | |
| 48 StartsWithASCII(group_name, kGroupNumberPrefix, true)) { | |
| 49 // We have a valid trial that starts with "Group" and isn't disabled. | |
| 50 size_t first_space = group_name.find(" "); | |
| 51 std::string group_prefix = group_name; | |
| 52 if (first_space != std::string::npos) { | |
| 53 // There is a flags section of the group name. Split that out and parse | |
| 54 // it. | |
| 55 group_prefix = group_name.substr(0, first_space); | |
| 56 base::SplitStringIntoKeyValuePairs( | |
| 57 group_name.substr(first_space), ':', ' ', flags); | |
| 58 } | |
| 59 if (!base::StringToUint64(group_prefix.substr(strlen(kGroupNumberPrefix)), | |
| 60 group_number)) { | |
| 61 // Could not parse group number. | |
| 62 *group_number = 0; | |
| 63 } | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 // Given a FieldTrialFlags object, returns the string value of the provided | |
| 68 // flag. | |
| 69 std::string GetStringValueForFlagWithDefault( | |
| 70 const std::string& flag, | |
| 71 const std::string& default_value, | |
| 72 FieldTrialFlags& flags) { | |
| 73 FieldTrialFlags::const_iterator i; | |
| 74 for (i = flags.begin(); i != flags.end(); i++) { | |
| 75 if (i->first == flag) | |
| 76 return i->second; | |
| 77 } | |
| 78 return default_value; | |
| 79 } | |
| 80 | |
| 81 // Given a FieldTrialFlags object, returns the uint64 value of the provided | |
| 82 // flag. | |
| 83 uint64 GetUInt64ValueForFlagWithDefault( | |
| 84 const std::string& flag, uint64 default_value, FieldTrialFlags& flags) { | |
| 85 uint64 value; | |
| 86 if (!base::StringToUint64(GetStringValueForFlagWithDefault(flag, "", flags), | |
| 87 &value)) | |
| 88 return default_value; | |
| 89 return value; | |
| 90 } | |
| 91 | |
| 92 // Given a FieldTrialFlags object, returns the boolean value of the provided | |
| 93 // flag. | |
| 94 bool GetBoolValueForFlagWithDefault( | |
| 95 const std::string& flag, bool default_value, FieldTrialFlags& flags) { | |
| 96 return GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | |
| 97 } | |
| 98 | |
| 99 // Check whether or not the Extended API should be used on the given profile. | |
| 100 bool IsInstantExtendedAPIEnabled(Profile* profile) { | |
| 19 return EmbeddedSearchPageVersion(profile) != 0; | 101 return EmbeddedSearchPageVersion(profile) != 0; |
| 20 } | 102 } |
| 21 | 103 |
| 22 uint64 EmbeddedSearchPageVersion(const Profile* profile) { | 104 // Determine what embedded search page version to request from the user's |
| 105 // default search provider. If 0, the embedded search UI should not be enabled. | |
| 106 // Note that the profile object here isn't const because we need to determine | |
| 107 // whether or not the user has a theme installed as part of this check, and | |
| 108 // that logic requires a non-const profile for whatever reason. | |
| 109 uint64 EmbeddedSearchPageVersion(Profile* profile) { | |
| 23 // Incognito windows do not currently use the embedded search API. | 110 // Incognito windows do not currently use the embedded search API. |
| 24 if (!profile || profile->IsOffTheRecord()) | 111 if (!profile || profile->IsOffTheRecord()) |
| 25 return 0; | 112 return 0; |
| 26 | 113 |
| 27 // Check Finch field trials. | 114 // Check Finch field trials. |
| 28 base::FieldTrial* trial = base::FieldTrialList::Find("InstantExtended"); | 115 FieldTrialFlags flags; |
| 29 if (trial && StartsWithASCII(trial->group_name(), "Group", true)) { | 116 uint64 group_number = 0; |
| 30 uint64 group_number; | 117 base::FieldTrial* trial = |
| 31 if (base::StringToUint64(trial->group_name().substr(5), &group_number)) { | 118 base::FieldTrialList::Find(kInstantExtendedFieldTrialName); |
| 32 return group_number; | 119 if (trial) { |
| 33 } | 120 std::string group_name = trial->group_name(); |
| 121 GetFieldTrialInfo(group_name, &flags, &group_number); | |
| 122 } | |
| 123 | |
| 124 if (group_number > 0) { | |
| 125 uint64 espv = GetUInt64ValueForFlagWithDefault( | |
| 126 kEmbeddedPageVersionFlagName, | |
| 127 kEmbeddedPageVersionDefault, | |
| 128 flags); | |
| 129 | |
| 130 // Check for themes. | |
| 131 bool has_theme = | |
| 132 !ThemeServiceFactory::GetForProfile(profile)->UsingDefaultTheme(); | |
| 133 bool enable_for_themes = | |
| 134 GetBoolValueForFlagWithDefault(kEnableOnThemesFlagName, | |
| 135 kEnableOnThemesDefault, | |
| 136 flags); | |
| 137 if (!has_theme || enable_for_themes) | |
| 138 return espv; | |
| 34 } | 139 } |
| 35 | 140 |
| 36 if (CommandLine::ForCurrentProcess()->HasSwitch( | 141 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 37 switches::kEnableInstantExtendedAPI)) { | 142 switches::kEnableInstantExtendedAPI)) { |
| 38 // The user has manually flipped the about:flags switch - give the default | 143 // The user has manually flipped the about:flags switch - give the default |
| 39 // UI version. | 144 // UI version. |
| 40 return 1; | 145 return kEmbeddedPageVersionDefault; |
| 41 } | 146 } |
| 42 return 0; | 147 return 0; |
| 43 } | 148 } |
| 44 | 149 |
| 45 void EnableInstantExtendedAPIForTesting() { | 150 void EnableInstantExtendedAPIForTesting() { |
| 46 CommandLine::ForCurrentProcess()->AppendSwitch( | 151 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 47 switches::kEnableInstantExtendedAPI); | 152 switches::kEnableInstantExtendedAPI); |
| 48 } | 153 } |
| 49 | 154 |
| 50 bool IsQueryExtractionEnabled(const Profile* profile) { | 155 bool IsQueryExtractionEnabled(Profile* profile) { |
| 51 return IsInstantExtendedAPIEnabled(profile) || | 156 return IsInstantExtendedAPIEnabled(profile) || |
| 52 CommandLine::ForCurrentProcess()->HasSwitch( | 157 CommandLine::ForCurrentProcess()->HasSwitch( |
| 53 switches::kEnableInstantExtendedAPI); | 158 switches::kEnableInstantExtendedAPI); |
| 54 } | 159 } |
| 55 | 160 |
| 56 void EnableQueryExtractionForTesting() { | 161 void EnableQueryExtractionForTesting() { |
| 57 CommandLine::ForCurrentProcess()->AppendSwitch( | 162 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 58 switches::kEnableInstantExtendedAPI); | 163 switches::kEnableInstantExtendedAPI); |
| 59 } | 164 } |
| 60 | 165 |
| 61 } // namespace search | 166 } // namespace search |
| 62 } // namespace chrome | 167 } // namespace chrome |
| OLD | NEW |