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

Side by Side Diff: chrome/browser/profile_resetter/automatic_profile_resetter.cc

Issue 50883003: Added the ability to specify AutomaticProfileResetter's evaluation program through field trials. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Formatting nit. Created 7 years, 1 month 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/profile_resetter/automatic_profile_resetter.h" 5 #include "chrome/browser/profile_resetter/automatic_profile_resetter.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/metrics/field_trial.h" 11 #include "base/metrics/field_trial.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
15 #include "base/task_runner.h" 16 #include "base/task_runner.h"
16 #include "base/task_runner_util.h" 17 #include "base/task_runner_util.h"
17 #include "base/threading/sequenced_worker_pool.h" 18 #include "base/threading/sequenced_worker_pool.h"
18 #include "base/time/time.h" 19 #include "base/time/time.h"
19 #include "base/values.h" 20 #include "base/values.h"
20 #include "chrome/browser/browser_process.h" 21 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" 22 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h"
22 #include "chrome/browser/profile_resetter/jtl_interpreter.h" 23 #include "chrome/browser/profile_resetter/jtl_interpreter.h"
23 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/search_engines/template_url_service.h" 25 #include "chrome/browser/search_engines/template_url_service.h"
25 #include "chrome/browser/search_engines/template_url_service_factory.h" 26 #include "chrome/browser/search_engines/template_url_service_factory.h"
27 #include "components/variations/variations_associated_data.h"
26 #include "content/public/browser/browser_thread.h" 28 #include "content/public/browser/browser_thread.h"
27 #include "grit/browser_resources.h" 29 #include "grit/browser_resources.h"
28 #include "ui/base/resource/resource_bundle.h" 30 #include "ui/base/resource/resource_bundle.h"
29 31
30 32
31 // AutomaticProfileResetter::EvaluationResults ------------------------------- 33 // AutomaticProfileResetter::EvaluationResults -------------------------------
32 34
33 // Encapsulates the output values extracted from the evaluator program. 35 // Encapsulates the output values extracted from the evaluator program.
34 struct AutomaticProfileResetter::EvaluationResults { 36 struct AutomaticProfileResetter::EvaluationResults {
35 EvaluationResults() 37 EvaluationResults()
(...skipping 12 matching lines...) Expand all
48 50
49 51
50 // Helpers ------------------------------------------------------------------- 52 // Helpers -------------------------------------------------------------------
51 53
52 namespace { 54 namespace {
53 55
54 // Name constants for the field trial behind which we enable this feature. 56 // Name constants for the field trial behind which we enable this feature.
55 const char kAutomaticProfileResetStudyName[] = "AutomaticProfileReset"; 57 const char kAutomaticProfileResetStudyName[] = "AutomaticProfileReset";
56 const char kAutomaticProfileResetStudyDryRunGroupName[] = "DryRun"; 58 const char kAutomaticProfileResetStudyDryRunGroupName[] = "DryRun";
57 const char kAutomaticProfileResetStudyEnabledGroupName[] = "Enabled"; 59 const char kAutomaticProfileResetStudyEnabledGroupName[] = "Enabled";
60 const char kAutomaticProfileResetStudyProgramParameterName[] = "program";
61 const char kAutomaticProfileResetStudyHashSeedParameterName[] = "hash_seed";
58 62
59 // How long to wait after start-up before unleashing the evaluation flow. 63 // How long to wait after start-up before unleashing the evaluation flow.
60 const int64 kEvaluationFlowDelayInSeconds = 55; 64 const int64 kEvaluationFlowDelayInSeconds = 55;
61 65
62 // Keys used in the input dictionary of the program. 66 // Keys used in the input dictionary of the program.
63 const char kDefaultSearchProviderKey[] = "default_search_provider"; 67 const char kDefaultSearchProviderKey[] = "default_search_provider";
64 const char kDefaultSearchProviderIsUserControlledKey[] = 68 const char kDefaultSearchProviderIsUserControlledKey[] =
65 "default_search_provider_iuc"; 69 "default_search_provider_iuc";
66 const char kLoadedModuleDigestsKey[] = "loaded_modules"; 70 const char kLoadedModuleDigestsKey[] = "loaded_modules";
67 const char kLocalStateKey[] = "local_state"; 71 const char kLocalStateKey[] = "local_state";
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 PROMPT_ACTION_RESET, 114 PROMPT_ACTION_RESET,
111 PROMPT_ACTION_NO_RESET, 115 PROMPT_ACTION_NO_RESET,
112 PROMPT_DISMISSED, 116 PROMPT_DISMISSED,
113 // Prompt was still shown (not dismissed by the user) when Chrome was closed. 117 // Prompt was still shown (not dismissed by the user) when Chrome was closed.
114 PROMPT_IGNORED, 118 PROMPT_IGNORED,
115 PROMPT_RESULT_MAX 119 PROMPT_RESULT_MAX
116 }; 120 };
117 121
118 // Returns whether or not a dry-run shall be performed. 122 // Returns whether or not a dry-run shall be performed.
119 bool ShouldPerformDryRun() { 123 bool ShouldPerformDryRun() {
120 return base::FieldTrialList::FindFullName(kAutomaticProfileResetStudyName) == 124 return StartsWithASCII(
121 kAutomaticProfileResetStudyDryRunGroupName; 125 base::FieldTrialList::FindFullName(kAutomaticProfileResetStudyName),
126 kAutomaticProfileResetStudyDryRunGroupName, true);
122 } 127 }
123 128
124 // Returns whether or not a live-run shall be performed. 129 // Returns whether or not a live-run shall be performed.
125 bool ShouldPerformLiveRun() { 130 bool ShouldPerformLiveRun() {
126 return base::FieldTrialList::FindFullName(kAutomaticProfileResetStudyName) == 131 return StartsWithASCII(
127 kAutomaticProfileResetStudyEnabledGroupName; 132 base::FieldTrialList::FindFullName(kAutomaticProfileResetStudyName),
133 kAutomaticProfileResetStudyEnabledGroupName, true);
134 }
135
136 // Returns whether or not the currently active experiment group prescribes the
137 // program and hash seed to use instead of the baked-in ones.
138 bool DoesExperimentOverrideProgramAndHashSeed() {
139 #if defined(GOOGLE_CHROME_BUILD)
140 std::map<std::string, std::string> params;
141 chrome_variations::GetVariationParams(kAutomaticProfileResetStudyName,
142 &params);
143 return params.count(kAutomaticProfileResetStudyProgramParameterName) &&
144 params.count(kAutomaticProfileResetStudyHashSeedParameterName);
145 #else
146 return false;
147 #endif
128 } 148 }
129 149
130 // Deep-copies all preferences in |source| to a sub-tree named |value_tree_key| 150 // Deep-copies all preferences in |source| to a sub-tree named |value_tree_key|
131 // in |target_dictionary|, with path expansion, and also creates an isomorphic 151 // in |target_dictionary|, with path expansion, and also creates an isomorphic
132 // sub-tree under the key |is_user_controlled_tree_key| that contains only 152 // sub-tree under the key |is_user_controlled_tree_key| that contains only
133 // Boolean values, indicating whether or not the corresponding preferences are 153 // Boolean values, indicating whether or not the corresponding preferences are
134 // coming from the 'user' PrefStore. 154 // coming from the 'user' PrefStore.
135 void BuildSubTreesFromPreferences(const PrefService* source, 155 void BuildSubTreesFromPreferences(const PrefService* source,
136 const char* value_tree_key, 156 const char* value_tree_key,
137 const char* is_user_controlled_tree_key, 157 const char* is_user_controlled_tree_key,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 AutomaticProfileResetter::AutomaticProfileResetter(Profile* profile) 189 AutomaticProfileResetter::AutomaticProfileResetter(Profile* profile)
170 : profile_(profile), 190 : profile_(profile),
171 state_(STATE_UNINITIALIZED), 191 state_(STATE_UNINITIALIZED),
172 enumeration_of_loaded_modules_ready_(false), 192 enumeration_of_loaded_modules_ready_(false),
173 template_url_service_ready_(false), 193 template_url_service_ready_(false),
174 memento_in_prefs_(profile_), 194 memento_in_prefs_(profile_),
175 memento_in_local_state_(profile_), 195 memento_in_local_state_(profile_),
176 memento_in_file_(profile_), 196 memento_in_file_(profile_),
177 weak_ptr_factory_(this) { 197 weak_ptr_factory_(this) {
178 DCHECK(profile_); 198 DCHECK(profile_);
179 Initialize();
180 } 199 }
181 200
182 AutomaticProfileResetter::~AutomaticProfileResetter() {} 201 AutomaticProfileResetter::~AutomaticProfileResetter() {}
183 202
203 void AutomaticProfileResetter::Initialize() {
204 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
205 DCHECK_EQ(state_, STATE_UNINITIALIZED);
206
207 if (!ShouldPerformDryRun() && !ShouldPerformLiveRun()) {
208 state_ = STATE_DISABLED;
209 return;
210 }
211
212 ui::ResourceBundle& resources(ui::ResourceBundle::GetSharedInstance());
213 if (DoesExperimentOverrideProgramAndHashSeed()) {
Alexei Svitkine (slow) 2013/11/06 15:24:56 I would have made this call: GetOverrideProgramAn
engedy 2013/11/06 16:55:01 Done.
214 program_ = chrome_variations::GetVariationParamValue(
215 kAutomaticProfileResetStudyName,
216 kAutomaticProfileResetStudyProgramParameterName);
217 hash_seed_ = chrome_variations::GetVariationParamValue(
218 kAutomaticProfileResetStudyName,
219 kAutomaticProfileResetStudyHashSeedParameterName);
220 } else if (ShouldPerformLiveRun()) {
221 program_ = resources.GetRawDataResource(
222 IDR_AUTOMATIC_PROFILE_RESET_RULES).as_string();
223 hash_seed_ = resources.GetRawDataResource(
224 IDR_AUTOMATIC_PROFILE_RESET_HASH_SEED).as_string();
225 } else { // ShouldPerformDryRun()
226 program_ = resources.GetRawDataResource(
227 IDR_AUTOMATIC_PROFILE_RESET_RULES_DRY).as_string();
228 hash_seed_ = resources.GetRawDataResource(
229 IDR_AUTOMATIC_PROFILE_RESET_HASH_SEED_DRY).as_string();
230 }
231
232 delegate_.reset(new AutomaticProfileResetterDelegateImpl(
233 TemplateURLServiceFactory::GetForProfile(profile_)));
234 task_runner_for_waiting_ =
235 content::BrowserThread::GetMessageLoopProxyForThread(
236 content::BrowserThread::UI);
237
238 state_ = STATE_INITIALIZED;
239 }
240
184 void AutomaticProfileResetter::Activate() { 241 void AutomaticProfileResetter::Activate() {
185 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 242 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
186 DCHECK(state_ == STATE_INITIALIZED || state_ == STATE_DISABLED); 243 DCHECK(state_ == STATE_INITIALIZED || state_ == STATE_DISABLED);
187 244
188 if (state_ == STATE_INITIALIZED) { 245 if (state_ == STATE_INITIALIZED) {
189 if (!program_.empty()) { 246 if (!program_.empty()) {
190 // Some steps in the flow (e.g. loaded modules, file-based memento) are 247 // Some steps in the flow (e.g. loaded modules, file-based memento) are
191 // IO-intensive, so defer execution until some time later. 248 // IO-intensive, so defer execution until some time later.
192 task_runner_for_waiting_->PostDelayedTask( 249 task_runner_for_waiting_->PostDelayedTask(
193 FROM_HERE, 250 FROM_HERE,
194 base::Bind(&AutomaticProfileResetter::PrepareEvaluationFlow, 251 base::Bind(&AutomaticProfileResetter::PrepareEvaluationFlow,
195 weak_ptr_factory_.GetWeakPtr()), 252 weak_ptr_factory_.GetWeakPtr()),
196 base::TimeDelta::FromSeconds(kEvaluationFlowDelayInSeconds)); 253 base::TimeDelta::FromSeconds(kEvaluationFlowDelayInSeconds));
197 } else { 254 } else {
198 // Terminate early if there is no program included (nor set by tests). 255 // Terminate early if there is no program included (nor set by tests).
199 state_ = STATE_DISABLED; 256 state_ = STATE_DISABLED;
200 } 257 }
201 } 258 }
202 } 259 }
203 260
204 void AutomaticProfileResetter::SetHashSeedForTesting( 261 void AutomaticProfileResetter::SetProgramForTesting(
205 const base::StringPiece& hash_key) { 262 const std::string& program) {
206 hash_seed_ = hash_key; 263 program_ = program;
207 } 264 }
208 265
209 void AutomaticProfileResetter::SetProgramForTesting( 266 void AutomaticProfileResetter::SetHashSeedForTesting(
210 const base::StringPiece& program) { 267 const std::string& hash_key) {
211 program_ = program; 268 hash_seed_ = hash_key;
212 } 269 }
213 270
214 void AutomaticProfileResetter::SetDelegateForTesting( 271 void AutomaticProfileResetter::SetDelegateForTesting(
215 scoped_ptr<AutomaticProfileResetterDelegate> delegate) { 272 scoped_ptr<AutomaticProfileResetterDelegate> delegate) {
216 delegate_ = delegate.Pass(); 273 delegate_ = delegate.Pass();
217 } 274 }
218 275
219 void AutomaticProfileResetter::SetTaskRunnerForWaitingForTesting( 276 void AutomaticProfileResetter::SetTaskRunnerForWaitingForTesting(
220 const scoped_refptr<base::TaskRunner>& task_runner) { 277 const scoped_refptr<base::TaskRunner>& task_runner) {
221 task_runner_for_waiting_ = task_runner; 278 task_runner_for_waiting_ = task_runner;
222 } 279 }
223 280
224 void AutomaticProfileResetter::Initialize() {
225 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
226 DCHECK_EQ(state_, STATE_UNINITIALIZED);
227
228 if (ShouldPerformDryRun() || ShouldPerformLiveRun()) {
229 ui::ResourceBundle& resources(ui::ResourceBundle::GetSharedInstance());
230 if (ShouldPerformLiveRun()) {
231 program_ =
232 resources.GetRawDataResource(IDR_AUTOMATIC_PROFILE_RESET_RULES);
233 hash_seed_ =
234 resources.GetRawDataResource(IDR_AUTOMATIC_PROFILE_RESET_HASH_SEED);
235 } else { // ShouldPerformDryRun()
236 program_ =
237 resources.GetRawDataResource(IDR_AUTOMATIC_PROFILE_RESET_RULES_DRY);
238 hash_seed_ = resources.GetRawDataResource(
239 IDR_AUTOMATIC_PROFILE_RESET_HASH_SEED_DRY);
240 }
241 delegate_.reset(new AutomaticProfileResetterDelegateImpl(
242 TemplateURLServiceFactory::GetForProfile(profile_)));
243 task_runner_for_waiting_ =
244 content::BrowserThread::GetMessageLoopProxyForThread(
245 content::BrowserThread::UI);
246 state_ = STATE_INITIALIZED;
247 } else {
248 state_ = STATE_DISABLED;
249 }
250 }
251
252 void AutomaticProfileResetter::PrepareEvaluationFlow() { 281 void AutomaticProfileResetter::PrepareEvaluationFlow() {
253 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 282 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
254 DCHECK_EQ(state_, STATE_INITIALIZED); 283 DCHECK_EQ(state_, STATE_INITIALIZED);
255 284
256 state_ = STATE_WAITING_ON_DEPENDENCIES; 285 state_ = STATE_WAITING_ON_DEPENDENCIES;
257 286
258 delegate_->RequestCallbackWhenTemplateURLServiceIsLoaded( 287 delegate_->RequestCallbackWhenTemplateURLServiceIsLoaded(
259 base::Bind(&AutomaticProfileResetter::OnTemplateURLServiceIsLoaded, 288 base::Bind(&AutomaticProfileResetter::OnTemplateURLServiceIsLoaded,
260 weak_ptr_factory_.GetWeakPtr())); 289 weak_ptr_factory_.GetWeakPtr()));
261 delegate_->RequestCallbackWhenLoadedModulesAreEnumerated( 290 delegate_->RequestCallbackWhenLoadedModulesAreEnumerated(
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 hash_seed_, 402 hash_seed_,
374 program_, 403 program_,
375 base::Passed(&input)), 404 base::Passed(&input)),
376 base::Bind(&AutomaticProfileResetter::FinishEvaluationFlow, 405 base::Bind(&AutomaticProfileResetter::FinishEvaluationFlow,
377 weak_ptr_factory_.GetWeakPtr())); 406 weak_ptr_factory_.GetWeakPtr()));
378 } 407 }
379 408
380 // static 409 // static
381 scoped_ptr<AutomaticProfileResetter::EvaluationResults> 410 scoped_ptr<AutomaticProfileResetter::EvaluationResults>
382 AutomaticProfileResetter::EvaluateConditionsOnWorkerPoolThread( 411 AutomaticProfileResetter::EvaluateConditionsOnWorkerPoolThread(
383 const base::StringPiece& hash_seed, 412 const std::string& hash_seed,
384 const base::StringPiece& program, 413 const std::string& program,
385 scoped_ptr<base::DictionaryValue> program_input) { 414 scoped_ptr<base::DictionaryValue> program_input) {
386 JtlInterpreter interpreter( 415 JtlInterpreter interpreter(hash_seed, program, program_input.get());
387 hash_seed.as_string(), program.as_string(), program_input.get());
388 interpreter.Execute(); 416 interpreter.Execute();
389 UMA_HISTOGRAM_ENUMERATION("AutomaticProfileReset.InterpreterResult", 417 UMA_HISTOGRAM_ENUMERATION("AutomaticProfileReset.InterpreterResult",
390 interpreter.result(), 418 interpreter.result(),
391 JtlInterpreter::RESULT_MAX); 419 JtlInterpreter::RESULT_MAX);
392 420
393 // In each case below, the respective field in result originally contains the 421 // In each case below, the respective field in result originally contains the
394 // default, so if the getter fails, we still have the correct value there. 422 // default, so if the getter fails, we still have the correct value there.
395 scoped_ptr<EvaluationResults> results(new EvaluationResults); 423 scoped_ptr<EvaluationResults> results(new EvaluationResults);
396 interpreter.GetOutputBoolean(kHadPromptedAlreadyKey, 424 interpreter.GetOutputBoolean(kHadPromptedAlreadyKey,
397 &results->had_prompted_already); 425 &results->had_prompted_already);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 kCombinedStatusMaskMaximumValue); 478 kCombinedStatusMaskMaximumValue);
451 } 479 }
452 480
453 void AutomaticProfileResetter::Shutdown() { 481 void AutomaticProfileResetter::Shutdown() {
454 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 482 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
455 483
456 state_ = STATE_DISABLED; 484 state_ = STATE_DISABLED;
457 delegate_.reset(); 485 delegate_.reset();
458 weak_ptr_factory_.InvalidateWeakPtrs(); 486 weak_ptr_factory_.InvalidateWeakPtrs();
459 } 487 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698