OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search/search.h" | 5 #include "components/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/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "components/google/core/browser/google_util.h" |
13 #include "components/search/search_switches.h" | 14 #include "components/search/search_switches.h" |
| 15 #include "components/search_engines/template_url.h" |
| 16 #include "url/gurl.h" |
14 | 17 |
15 namespace chrome { | 18 namespace search { |
16 | 19 |
17 namespace { | 20 namespace { |
18 | 21 |
19 // Configuration options for Embedded Search. | 22 // Configuration options for Embedded Search. |
20 // EmbeddedSearch field trials are named in such a way that we can parse out | 23 // 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 | 24 // the experiment configuration from the trial's group name in order to give |
22 // us maximum flexability in running experiments. | 25 // us maximum flexability in running experiments. |
23 // Field trial groups should be named things like "Group7 espv:2 instant:1". | 26 // 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 | 27 // 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: | 28 // space-delimited list of key:value pairs which correspond to these flags: |
(...skipping 16 matching lines...) Expand all Loading... |
42 // Dev & Canary, for now the code accepts both names. | 45 // Dev & Canary, for now the code accepts both names. |
43 // TODO(dcblack): Remove the InstantExtended name once M31 hits the Beta | 46 // TODO(dcblack): Remove the InstantExtended name once M31 hits the Beta |
44 // channel. | 47 // channel. |
45 const char kInstantExtendedFieldTrialName[] = "InstantExtended"; | 48 const char kInstantExtendedFieldTrialName[] = "InstantExtended"; |
46 const char kEmbeddedSearchFieldTrialName[] = "EmbeddedSearch"; | 49 const char kEmbeddedSearchFieldTrialName[] = "EmbeddedSearch"; |
47 | 50 |
48 // If the field trial's group name ends with this string its configuration will | 51 // 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. | 52 // be ignored and Instant Extended will not be enabled by default. |
50 const char kDisablingSuffix[] = "DISABLED"; | 53 const char kDisablingSuffix[] = "DISABLED"; |
51 | 54 |
| 55 #if !defined(OS_IOS) && !defined(OS_ANDROID) |
| 56 const char kEnableQueryExtractionFlagName[] = "query_extraction"; |
| 57 #endif |
| 58 |
| 59 const char kAllowPrefetchNonDefaultMatch[] = "allow_prefetch_non_default_match"; |
| 60 |
| 61 #if defined(OS_ANDROID) |
| 62 const char kPrefetchSearchResultsFlagName[] = "prefetch_results"; |
| 63 |
| 64 // Controls whether to reuse prerendered Instant Search base page to commit any |
| 65 // search query. |
| 66 const char kReuseInstantSearchBasePage[] = "reuse_instant_search_base_page"; |
| 67 #endif |
| 68 |
52 } // namespace | 69 } // namespace |
53 | 70 |
| 71 // Negative start-margin values prevent the "es_sm" parameter from being used. |
| 72 const int kDisableStartMargin = -1; |
| 73 |
54 bool IsInstantExtendedAPIEnabled() { | 74 bool IsInstantExtendedAPIEnabled() { |
55 #if defined(OS_IOS) | 75 #if defined(OS_IOS) |
56 return false; | 76 return false; |
57 #elif defined(OS_ANDROID) | 77 #elif defined(OS_ANDROID) |
58 return EmbeddedSearchPageVersion() == kEmbeddedSearchEnabledVersion; | 78 return EmbeddedSearchPageVersion() == kEmbeddedSearchEnabledVersion; |
59 #else | 79 #else |
60 return true; | 80 return true; |
61 #endif // defined(OS_IOS) | 81 #endif // defined(OS_IOS) |
62 } | 82 } |
63 | 83 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 } | 157 } |
138 | 158 |
139 // Given a FieldTrialFlags object, returns the boolean value of the provided | 159 // Given a FieldTrialFlags object, returns the boolean value of the provided |
140 // flag. | 160 // flag. |
141 bool GetBoolValueForFlagWithDefault(const std::string& flag, | 161 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
142 bool default_value, | 162 bool default_value, |
143 const FieldTrialFlags& flags) { | 163 const FieldTrialFlags& flags) { |
144 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 164 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
145 } | 165 } |
146 | 166 |
147 } // namespace chrome | 167 std::string InstantExtendedEnabledParam(bool for_search) { |
| 168 if (for_search && !IsQueryExtractionEnabled()) |
| 169 return std::string(); |
| 170 return std::string(google_util::kInstantExtendedAPIParam) + "=" + |
| 171 base::Uint64ToString(EmbeddedSearchPageVersion()) + "&"; |
| 172 } |
| 173 |
| 174 std::string ForceInstantResultsParam(bool for_prerender) { |
| 175 return (for_prerender || !IsInstantExtendedAPIEnabled()) ? "ion=1&" |
| 176 : std::string(); |
| 177 } |
| 178 |
| 179 bool IsQueryExtractionEnabled() { |
| 180 #if defined(OS_IOS) || defined(OS_ANDROID) |
| 181 return true; |
| 182 #else |
| 183 if (!IsInstantExtendedAPIEnabled()) |
| 184 return false; |
| 185 |
| 186 const base::CommandLine* command_line = |
| 187 base::CommandLine::ForCurrentProcess(); |
| 188 if (command_line->HasSwitch(switches::kEnableQueryExtraction)) |
| 189 return true; |
| 190 |
| 191 FieldTrialFlags flags; |
| 192 return GetFieldTrialInfo(&flags) && |
| 193 GetBoolValueForFlagWithDefault(kEnableQueryExtractionFlagName, false, |
| 194 flags); |
| 195 #endif // defined(OS_IOS) || defined(OS_ANDROID) |
| 196 } |
| 197 |
| 198 bool ShouldPrefetchSearchResults() { |
| 199 if (!IsInstantExtendedAPIEnabled()) |
| 200 return false; |
| 201 |
| 202 #if defined(OS_ANDROID) |
| 203 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 204 switches::kPrefetchSearchResults)) { |
| 205 return true; |
| 206 } |
| 207 |
| 208 FieldTrialFlags flags; |
| 209 return GetFieldTrialInfo(&flags) && |
| 210 GetBoolValueForFlagWithDefault(kPrefetchSearchResultsFlagName, false, |
| 211 flags); |
| 212 #else |
| 213 return true; |
| 214 #endif |
| 215 } |
| 216 |
| 217 bool ShouldReuseInstantSearchBasePage() { |
| 218 if (!ShouldPrefetchSearchResults()) |
| 219 return false; |
| 220 |
| 221 #if defined(OS_ANDROID) |
| 222 FieldTrialFlags flags; |
| 223 return GetFieldTrialInfo(&flags) && |
| 224 GetBoolValueForFlagWithDefault(kReuseInstantSearchBasePage, false, |
| 225 flags); |
| 226 #else |
| 227 return true; |
| 228 #endif |
| 229 } |
| 230 |
| 231 bool ShouldAllowPrefetchNonDefaultMatch() { |
| 232 if (!ShouldPrefetchSearchResults()) |
| 233 return false; |
| 234 |
| 235 FieldTrialFlags flags; |
| 236 return GetFieldTrialInfo(&flags) && |
| 237 GetBoolValueForFlagWithDefault(kAllowPrefetchNonDefaultMatch, false, |
| 238 flags); |
| 239 } |
| 240 |
| 241 // |url| should either have a secure scheme or have a non-HTTPS base URL that |
| 242 // the user specified using --google-base-url. (This allows testers to use |
| 243 // --google-base-url to point at non-HTTPS servers, which eases testing.) |
| 244 bool IsSuitableURLForInstant(const GURL& url, const TemplateURL* template_url) { |
| 245 return template_url->HasSearchTermsReplacementKey(url) && |
| 246 (url.SchemeIsCryptographic() || |
| 247 google_util::StartsWithCommandLineGoogleBaseURL(url)); |
| 248 } |
| 249 |
| 250 void EnableQueryExtractionForTesting() { |
| 251 #if !defined(OS_IOS) && !defined(OS_ANDROID) |
| 252 base::CommandLine* cl = base::CommandLine::ForCurrentProcess(); |
| 253 cl->AppendSwitch(switches::kEnableQueryExtraction); |
| 254 #endif |
| 255 } |
| 256 |
| 257 } // namespace search |
OLD | NEW |