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

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

Issue 12386019: Instant: Use only one hidden WebContents per profile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 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 (c) 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/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/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/prefs/pref_service.h"
10 #include "base/string_split.h" 11 #include "base/string_split.h"
11 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
12 #include "chrome/browser/instant/instant_service.h" 13 #include "chrome/browser/instant/instant_service.h"
13 #include "chrome/browser/instant/instant_service_factory.h" 14 #include "chrome/browser/instant/instant_service_factory.h"
15 #include "chrome/browser/prefs/pref_registry_syncable.h"
14 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/search_engines/template_url_service.h" 17 #include "chrome/browser/search_engines/template_url_service.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h" 18 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h" 21 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/navigation_entry.h" 22 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/render_process_host.h" 23 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/web_contents.h" 24 #include "content/public/browser/web_contents.h"
22 25
23 namespace { 26 namespace {
24 27
28 // The default value we should assign to the instant_extended.enabled pref.
29 // As with other prefs, the default is used only when the user hasn't toggled
30 // the pref explicitly.
31 enum InstantExtendedDefault {
32 INSTANT_DEFAULT_ON, // Default the pref to be enabled.
33 INSTANT_USE_EXISTING, // Use the current value of the instant.enabled pref.
34 INSTANT_DEFAULT_OFF, // Default the pref to be disabled.
35 };
36
25 // Configuration options for Embedded Search. 37 // Configuration options for Embedded Search.
26 // InstantExtended field trials are named in such a way that we can parse out 38 // InstantExtended field trials are named in such a way that we can parse out
27 // the experiment configuration from the trial's group name in order to give 39 // the experiment configuration from the trial's group name in order to give
28 // us maximum flexability in running experiments. 40 // us maximum flexability in running experiments.
29 // Field trial groups should be named things like "Group7 espv:2 instant:1". 41 // Field trial groups should be named things like "Group7 espv:2 instant:1".
30 // The first token is always GroupN for some integer N, followed by a 42 // The first token is always GroupN for some integer N, followed by a
31 // space-delimited list of key:value pairs which correspond to these flags: 43 // space-delimited list of key:value pairs which correspond to these flags:
32 const char kEmbeddedPageVersionFlagName[] = "espv"; 44 const char kEmbeddedPageVersionFlagName[] = "espv";
33 const uint64 kEmbeddedPageVersionDisabled = 0; 45 const uint64 kEmbeddedPageVersionDisabled = 0;
34 const uint64 kEmbeddedPageVersionDefault = 2; 46 const uint64 kEmbeddedPageVersionDefault = 2;
35 47
36 const char kInstantExtendedActivationName[] = "instant"; 48 const char kInstantExtendedActivationName[] = "instant";
37 const chrome::search::InstantExtendedDefault kInstantExtendedActivationDefault = 49 const InstantExtendedDefault kInstantExtendedActivationDefault =
38 chrome::search::INSTANT_DEFAULT_ON; 50 INSTANT_DEFAULT_ON;
39 51
40 // Constants for the field trial name and group prefix. 52 // Constants for the field trial name and group prefix.
41 const char kInstantExtendedFieldTrialName[] = "InstantExtended"; 53 const char kInstantExtendedFieldTrialName[] = "InstantExtended";
42 const char kGroupNumberPrefix[] = "Group"; 54 const char kGroupNumberPrefix[] = "Group";
43 55
44 // If the field trial's group name ends with this string its configuration will 56 // If the field trial's group name ends with this string its configuration will
45 // be ignored and Instant Extended will not be enabled by default. 57 // be ignored and Instant Extended will not be enabled by default.
46 const char kDisablingSuffix[] = "DISABLED"; 58 const char kDisablingSuffix[] = "DISABLED";
47 59
48 chrome::search::InstantExtendedDefault InstantExtendedDefaultFromInt64(
49 int64 default_value) {
50 switch (default_value) {
51 case 0: return chrome::search::INSTANT_DEFAULT_ON;
52 case 1: return chrome::search::INSTANT_USE_EXISTING;
53 case 2: return chrome::search::INSTANT_DEFAULT_OFF;
54 default: return chrome::search::INSTANT_USE_EXISTING;
55 }
56 }
57
58 TemplateURL* GetDefaultSearchProviderTemplateURL(Profile* profile) { 60 TemplateURL* GetDefaultSearchProviderTemplateURL(Profile* profile) {
59 TemplateURLService* template_url_service = 61 TemplateURLService* template_url_service =
60 TemplateURLServiceFactory::GetForProfile(profile); 62 TemplateURLServiceFactory::GetForProfile(profile);
61 if (template_url_service) 63 if (template_url_service)
62 return template_url_service->GetDefaultSearchProvider(); 64 return template_url_service->GetDefaultSearchProvider();
63 return NULL; 65 return NULL;
64 } 66 }
65 67
66 GURL TemplateURLRefToGURL(const TemplateURLRef& ref) { 68 GURL TemplateURLRefToGURL(const TemplateURLRef& ref) {
67 return GURL( 69 return GURL(
(...skipping 25 matching lines...) Expand all
93 TemplateURLRef ref(template_url, i); 95 TemplateURLRef ref(template_url, i);
94 search_url = TemplateURLRefToGURL(ref); 96 search_url = TemplateURLRefToGURL(ref);
95 if (search_url.is_valid() && MatchesOriginAndPath(url, search_url)) 97 if (search_url.is_valid() && MatchesOriginAndPath(url, search_url))
96 return true; 98 return true;
97 } 99 }
98 100
99 return false; 101 return false;
100 } 102 }
101 103
102 enum OptInState { 104 enum OptInState {
103 // The user has not manually opted-in to or opted-out of InstantExtended. 105 NOT_SET, // The user has not manually opted into or out of InstantExtended.
104 NOT_SET, 106 OPT_IN, // The user has opted-in to InstantExtended.
105 // The user has opted-in to InstantExtended. 107 OPT_OUT, // The user has opted-out of InstantExtended.
106 OPT_IN,
107 // The user has opted-out of InstantExtended.
108 OPT_OUT,
109 // Number of enum entries, used for UMA histogram reporting macros.
110 OPT_IN_STATE_ENUM_COUNT, 108 OPT_IN_STATE_ENUM_COUNT,
111 }; 109 };
112 110
113 void RecordInstantExtendedOptInState(OptInState state) {
114 static bool recorded = false;
115 if (!recorded) {
116 UMA_HISTOGRAM_ENUMERATION("InstantExtended.OptInState", state,
117 OPT_IN_STATE_ENUM_COUNT);
118 recorded = true;
119 }
120 }
121
122 } // namespace 111 } // namespace
123 112
124 namespace chrome { 113 namespace chrome {
125 namespace search { 114 namespace search {
126 115
127 const char kInstantExtendedSearchTermsKey[] = "search_terms"; 116 const char kInstantExtendedSearchTermsKey[] = "search_terms";
128 117
129 const char kLocalOmniboxPopupURL[] = 118 const char kLocalOmniboxPopupURL[] =
130 "chrome://local-omnibox-popup/local-omnibox-popup.html"; 119 "chrome://local-omnibox-popup/local-omnibox-popup.html";
131 120
132 InstantExtendedDefault GetInstantExtendedDefaultSetting() {
133 // Check the command-line/about:flags setting first, which should have
134 // precedence and allows the trial to not be reported (if it's never queried).
135 const CommandLine* command_line = CommandLine::ForCurrentProcess();
136 if (command_line->HasSwitch(switches::kDisableInstantExtendedAPI))
137 return chrome::search::INSTANT_DEFAULT_OFF;
138 if (command_line->HasSwitch(switches::kEnableInstantExtendedAPI))
139 return chrome::search::INSTANT_DEFAULT_ON;
140
141 FieldTrialFlags flags;
142 if (GetFieldTrialInfo(
143 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
144 &flags, NULL)) {
145 uint64 trial_default = GetUInt64ValueForFlagWithDefault(
146 kInstantExtendedActivationName,
147 kInstantExtendedActivationDefault,
148 flags);
149 return InstantExtendedDefaultFromInt64(trial_default);
150 }
151
152 return kInstantExtendedActivationDefault;
153 }
154
155 bool IsInstantExtendedAPIEnabled(const Profile* profile) { 121 bool IsInstantExtendedAPIEnabled(const Profile* profile) {
156 return EmbeddedSearchPageVersion(profile) != kEmbeddedPageVersionDisabled; 122 return EmbeddedSearchPageVersion(profile) != kEmbeddedPageVersionDisabled;
157 } 123 }
158 124
159 // Determine what embedded search page version to request from the user's 125 // Determine what embedded search page version to request from the user's
160 // default search provider. If 0, the embedded search UI should not be enabled. 126 // default search provider. If 0, the embedded search UI should not be enabled.
161 uint64 EmbeddedSearchPageVersion(const Profile* profile) { 127 uint64 EmbeddedSearchPageVersion(const Profile* profile) {
162 if (!profile || profile->IsOffTheRecord()) 128 if (!profile || profile->IsOffTheRecord())
163 return kEmbeddedPageVersionDisabled; 129 return kEmbeddedPageVersionDisabled;
164 130
131 static uint64 embedded_page_version = kuint64max;
132 if (embedded_page_version != kuint64max)
133 return embedded_page_version;
134
135 embedded_page_version = kEmbeddedPageVersionDisabled;
136
165 // Check the command-line/about:flags setting first, which should have 137 // Check the command-line/about:flags setting first, which should have
166 // precedence and allows the trial to not be reported (if it's never queried). 138 // precedence and allows the trial to not be reported (if it's never queried).
167 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 139 const CommandLine* command_line = CommandLine::ForCurrentProcess();
168 if (command_line->HasSwitch(switches::kDisableInstantExtendedAPI)) {
169 RecordInstantExtendedOptInState(OPT_OUT);
170 return kEmbeddedPageVersionDisabled;
171 }
172 if (command_line->HasSwitch(switches::kEnableInstantExtendedAPI)) { 140 if (command_line->HasSwitch(switches::kEnableInstantExtendedAPI)) {
173 // The user has set the about:flags switch to Enabled - give the default 141 // The user has set the about:flags switch to Enabled - give the default
174 // UI version. 142 // UI version.
175 RecordInstantExtendedOptInState(OPT_IN); 143 UMA_HISTOGRAM_ENUMERATION("InstantExtended.OptInState", OPT_IN,
176 return kEmbeddedPageVersionDefault; 144 OPT_IN_STATE_ENUM_COUNT);
145 embedded_page_version = kEmbeddedPageVersionDefault;
146 } else if (command_line->HasSwitch(switches::kDisableInstantExtendedAPI)) {
147 UMA_HISTOGRAM_ENUMERATION("InstantExtended.OptInState", OPT_OUT,
148 OPT_IN_STATE_ENUM_COUNT);
149 } else {
150 UMA_HISTOGRAM_ENUMERATION("InstantExtended.OptInState", NOT_SET,
151 OPT_IN_STATE_ENUM_COUNT);
152 FieldTrialFlags flags;
153 if (GetFieldTrialInfo(
154 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
155 &flags, NULL)) {
156 embedded_page_version = GetUInt64ValueForFlagWithDefault(
157 kEmbeddedPageVersionFlagName,
158 kEmbeddedPageVersionDefault,
159 flags);
160 }
177 } 161 }
178 162
179 RecordInstantExtendedOptInState(NOT_SET); 163 return embedded_page_version;
180 FieldTrialFlags flags;
181 if (GetFieldTrialInfo(
182 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
183 &flags, NULL)) {
184 return GetUInt64ValueForFlagWithDefault(kEmbeddedPageVersionFlagName,
185 kEmbeddedPageVersionDefault,
186 flags);
187 }
188
189 return kEmbeddedPageVersionDisabled;
190 } 164 }
191 165
192 bool IsQueryExtractionEnabled(const Profile* profile) { 166 bool IsQueryExtractionEnabled(const Profile* profile) {
193 #if defined(OS_IOS) 167 #if defined(OS_IOS)
194 const CommandLine* cl = CommandLine::ForCurrentProcess(); 168 const CommandLine* cl = CommandLine::ForCurrentProcess();
195 return cl->HasSwitch(switches::kEnableQueryExtraction); 169 return cl->HasSwitch(switches::kEnableQueryExtraction);
196 #else 170 #else
197 // On desktop, query extraction is controlled by the instant-extended-api 171 // On desktop, query extraction is controlled by the instant-extended-api
198 // flag. 172 // flag.
199 return IsInstantExtendedAPIEnabled(profile); 173 return IsInstantExtendedAPIEnabled(profile);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); 246 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref();
273 effective_url = CoerceCommandLineURLToTemplateURL(url, instant_url_ref); 247 effective_url = CoerceCommandLineURLToTemplateURL(url, instant_url_ref);
274 } 248 }
275 249
276 return ShouldAssignURLToInstantRendererImpl( 250 return ShouldAssignURLToInstantRendererImpl(
277 effective_url, 251 effective_url,
278 IsInstantExtendedAPIEnabled(profile), 252 IsInstantExtendedAPIEnabled(profile),
279 template_url); 253 template_url);
280 } 254 }
281 255
256 void RegisterUserPrefs(PrefRegistrySyncable* registry) {
257 registry->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, false,
258 PrefRegistrySyncable::SYNCABLE_PREF);
259 registry->RegisterBooleanPref(prefs::kInstantEnabled, false,
260 PrefRegistrySyncable::SYNCABLE_PREF);
261 // This default is overridden by the InstantService constructor.
262 registry->RegisterBooleanPref(prefs::kInstantExtendedEnabled, false,
263 PrefRegistrySyncable::SYNCABLE_PREF);
264 }
265
266 const char* GetInstantPrefName(const Profile* profile) {
267 return IsInstantExtendedAPIEnabled(profile) ? prefs::kInstantExtendedEnabled :
268 prefs::kInstantEnabled;
269 }
270
271 bool IsInstantPrefEnabled(Profile* profile) {
272 if (!profile || profile->IsOffTheRecord())
273 return false;
274
275 const PrefService* prefs = profile->GetPrefs();
276 if (!prefs)
277 return false;
278
279 return prefs->GetBoolean(GetInstantPrefName(profile));
280 }
281
282 void SetInstantExtendedPrefDefault(Profile* profile) {
283 PrefService* prefs = profile ? profile->GetPrefs() : NULL;
284 if (!prefs)
285 return;
286
287 bool pref_default = false;
288
289 // Check the command-line/about:flags setting first, which should have
290 // precedence and allows the trial to not be reported (if it's never queried).
291 const CommandLine* command_line = CommandLine::ForCurrentProcess();
292 if (command_line->HasSwitch(switches::kEnableInstantExtendedAPI)) {
293 pref_default = true;
294 } else if (!command_line->HasSwitch(switches::kDisableInstantExtendedAPI)) {
295 uint64 trial_default = kInstantExtendedActivationDefault;
296
297 FieldTrialFlags flags;
298 if (GetFieldTrialInfo(
299 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
300 &flags, NULL)) {
301 trial_default = GetUInt64ValueForFlagWithDefault(
302 kInstantExtendedActivationName,
303 kInstantExtendedActivationDefault,
304 flags);
305 }
306
307 if (trial_default == INSTANT_DEFAULT_ON) {
308 pref_default = true;
309 } else if (trial_default != INSTANT_DEFAULT_OFF) {
310 pref_default = prefs->GetBoolean(prefs::kInstantEnabled);
311 }
312 }
313
314 // TODO(sreeram): Fix when https://codereview.chromium.org/12315116/ lands.
315 #if 0
316 prefs->SetDefaultPrefValue(prefs::kInstantExtendedEnabled,
317 Value::CreateBooleanValue(pref_default));
318 #else
319 prefs->SetBoolean(prefs::kInstantExtendedEnabled, pref_default);
320 #endif
321 }
322
323 GURL GetInstantURL(Profile* profile) {
324 bool extended_api_enabled = IsInstantExtendedAPIEnabled(profile);
325
326 const PrefService* prefs = profile && !profile->IsOffTheRecord() ?
327 profile->GetPrefs() : NULL;
328 if (!IsInstantPrefEnabled(profile) &&
329 !(extended_api_enabled && prefs &&
330 prefs->GetBoolean(prefs::kSearchSuggestEnabled)))
331 return GURL();
332
333 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
334 if (!template_url)
335 return GURL();
336
337 CommandLine* cl = CommandLine::ForCurrentProcess();
338 if (cl->HasSwitch(switches::kInstantURL)) {
339 GURL instant_url(cl->GetSwitchValueASCII(switches::kInstantURL));
340 if (extended_api_enabled) {
341 // Extended mode won't work if the search terms replacement key is absent.
342 GURL coerced_url = CoerceCommandLineURLToTemplateURL(
343 instant_url, template_url->instant_url_ref());
344 if (!template_url->HasSearchTermsReplacementKey(coerced_url))
345 return GURL();
346 }
347 return instant_url;
348 }
349
350 GURL instant_url = TemplateURLRefToGURL(template_url->instant_url_ref());
351
352 if (extended_api_enabled) {
353 // Extended mode won't work if the search terms replacement key is absent.
354 if (!template_url->HasSearchTermsReplacementKey(instant_url))
355 return GURL();
356
357 // Extended mode requires HTTPS. Force it if necessary.
358 if (!instant_url.SchemeIsSecure()) {
359 GURL::Replacements replacements;
360 replacements.SetSchemeStr(chrome::kHttpsScheme);
361 instant_url = instant_url.ReplaceComponents(replacements);
362 }
363 }
364
365 return instant_url;
366 }
367
368 bool IsInstantEnabled(Profile* profile) {
369 return GetInstantURL(profile).is_valid();
370 }
371
282 void EnableInstantExtendedAPIForTesting() { 372 void EnableInstantExtendedAPIForTesting() {
283 CommandLine* cl = CommandLine::ForCurrentProcess(); 373 CommandLine* cl = CommandLine::ForCurrentProcess();
284 cl->AppendSwitch(switches::kEnableInstantExtendedAPI); 374 cl->AppendSwitch(switches::kEnableInstantExtendedAPI);
285 } 375 }
286 376
287 void EnableQueryExtractionForTesting() { 377 void EnableQueryExtractionForTesting() {
378 CommandLine* cl = CommandLine::ForCurrentProcess();
288 #if defined(OS_IOS) 379 #if defined(OS_IOS)
289 CommandLine* cl = CommandLine::ForCurrentProcess();
290 cl->AppendSwitch(switches::kEnableQueryExtraction); 380 cl->AppendSwitch(switches::kEnableQueryExtraction);
291 #else 381 #else
292 EnableInstantExtendedAPIForTesting(); 382 cl->AppendSwitch(switches::kEnableInstantExtendedAPI);
293 #endif 383 #endif
294 } 384 }
295 385
296 bool ShouldAssignURLToInstantRendererImpl(const GURL& url, 386 bool ShouldAssignURLToInstantRendererImpl(const GURL& url,
297 bool extended_api_enabled, 387 bool extended_api_enabled,
298 TemplateURL* template_url) { 388 TemplateURL* template_url) {
299 if (!url.is_valid()) 389 if (!url.is_valid())
300 return false; 390 return false;
301 391
302 if (url.SchemeIs(chrome::kChromeSearchScheme)) 392 if (url.SchemeIs(chrome::kChromeSearchScheme))
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 GURL::Replacements replacements; 499 GURL::Replacements replacements;
410 replacements.SetSchemeStr(search_scheme); 500 replacements.SetSchemeStr(search_scheme);
411 replacements.SetHostStr(search_host); 501 replacements.SetHostStr(search_host);
412 replacements.SetPortStr(search_port); 502 replacements.SetPortStr(search_port);
413 replacements.SetPathStr(search_path); 503 replacements.SetPathStr(search_path);
414 return instant_url.ReplaceComponents(replacements); 504 return instant_url.ReplaceComponents(replacements);
415 } 505 }
416 506
417 } // namespace search 507 } // namespace search
418 } // namespace chrome 508 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698