OLD | NEW |
---|---|
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_helpers.h" | 8 #include "base/bind_helpers.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
10 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
12 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/strings/string_number_conversions.h" | |
13 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
14 #include "base/task_runner_util.h" | 16 #include "base/task_runner_util.h" |
15 #include "base/threading/sequenced_worker_pool.h" | 17 #include "base/threading/sequenced_worker_pool.h" |
18 #include "base/time/time.h" | |
19 #include "base/values.h" | |
20 #include "chrome/browser/browser_process.h" | |
21 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" | |
16 #include "chrome/browser/profile_resetter/jtl_interpreter.h" | 22 #include "chrome/browser/profile_resetter/jtl_interpreter.h" |
17 #include "chrome/browser/profiles/profile.h" | 23 #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_factory.h" | |
18 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
19 #include "grit/browser_resources.h" | 27 #include "grit/browser_resources.h" |
20 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
21 | 29 |
30 | |
31 // AutomaticProfileResetter::EvaluationResults ------------------------------- | |
32 | |
33 // Encapsulates the output values extracted from the evaluator program. | |
34 struct AutomaticProfileResetter::EvaluationResults { | |
35 EvaluationResults() | |
36 : had_prompted_already(false), | |
37 satisfied_criteria_mask(0), | |
38 combined_status_mask(0) {} | |
39 | |
40 std::string memento_value_in_prefs; | |
41 std::string memento_value_in_local_state; | |
42 std::string memento_value_in_file; | |
43 | |
44 bool had_prompted_already; | |
45 uint32 satisfied_criteria_mask; | |
46 uint32 combined_status_mask; | |
47 }; | |
48 | |
49 | |
50 // Helpers ------------------------------------------------------------------- | |
51 | |
22 namespace { | 52 namespace { |
23 | 53 |
24 // Number of bits, and maximum value (exclusive) for the mask whose bits | |
25 // indicate which of reset criteria were satisfied. | |
26 const size_t kSatisfiedCriteriaMaskBits = 2; | |
27 const uint32 kSatisfiedCriteriaMaskMaximumValue = | |
28 (1 << kSatisfiedCriteriaMaskBits); | |
29 | |
30 // Number of bits, and maximum value (exclusive) for the mask whose bits | |
31 // indicate if any of reset criteria were satisfied, and which of the mementos | |
32 // were already present. | |
33 const size_t kCombinedStatusMaskBits = 4; | |
34 const uint32 kCombinedStatusMaskMaximumValue = (1 << kCombinedStatusMaskBits); | |
35 | |
36 // Name constants for the field trial behind which we enable this feature. | 54 // Name constants for the field trial behind which we enable this feature. |
37 const char kAutomaticProfileResetStudyName[] = "AutomaticProfileReset"; | 55 const char kAutomaticProfileResetStudyName[] = "AutomaticProfileReset"; |
38 const char kAutomaticProfileResetStudyDryRunGroupName[] = "DryRun"; | 56 const char kAutomaticProfileResetStudyDryRunGroupName[] = "DryRun"; |
39 const char kAutomaticProfileResetStudyEnabledGroupName[] = "Enabled"; | 57 const char kAutomaticProfileResetStudyEnabledGroupName[] = "Enabled"; |
40 | 58 |
59 // How long to wait after start-up before unleashing the evaluation flow. | |
60 const int64 kEvaluationFlowDelayInSeconds = 55; | |
61 | |
41 // Keys used in the input dictionary of the program. | 62 // Keys used in the input dictionary of the program. |
42 // TODO(engedy): Add these here on an as-needed basis. | 63 const char kDefaultSearchProviderKey[] = "default_search_provider"; |
64 const char kDefaultSearchProviderIsUserControlledKey[] = | |
65 "default_search_provider_iuc"; | |
66 const char kLoadedModuleDigestsKey[] = "loaded_modules"; | |
67 const char kLocalStateKey[] = "local_state"; | |
68 const char kLocalStateIsUserControlledKey[] = "local_state_iuc"; | |
69 const char kSearchProvidersKey[] = "search_providers"; | |
70 const char kUserPreferencesKey[] = "preferences"; | |
71 const char kUserPreferencesIsUserControlledKey[] = "preferences_iuc"; | |
43 | 72 |
44 // Keys used in the output dictionary of the program. | 73 // Keys used in the output dictionary of the program. |
74 const char kCombinedStatusMaskKeys[][26] = { | |
75 "combined_status_mask_bit1", "combined_status_mask_bit2", | |
76 "combined_status_mask_bit3", "combined_status_mask_bit4"}; | |
45 const char kHadPromptedAlreadyKey[] = "had_prompted_already"; | 77 const char kHadPromptedAlreadyKey[] = "had_prompted_already"; |
46 const char kSatisfiedCriteriaMaskKeys[][29] = {"satisfied_criteria_mask_bit1", | 78 const char kSatisfiedCriteriaMaskKeys[][29] = {"satisfied_criteria_mask_bit1", |
47 "satisfied_criteria_mask_bit2"}; | 79 "satisfied_criteria_mask_bit2"}; |
48 const char kCombinedStatusMaskKeys[][26] = { | |
49 "combined_status_mask_bit1", "combined_status_mask_bit2", | |
50 "combined_status_mask_bit3", "combined_status_mask_bit4"}; | |
51 | 80 |
52 // Keys used in both the input and output dictionary of the program. | 81 // Keys used in both the input and output dictionary of the program. |
82 const char kMementoValueInFileKey[] = "memento_value_in_file"; | |
83 const char kMementoValueInLocalStateKey[] = "memento_value_in_local_state"; | |
53 const char kMementoValueInPrefsKey[] = "memento_value_in_prefs"; | 84 const char kMementoValueInPrefsKey[] = "memento_value_in_prefs"; |
54 const char kMementoValueInLocalStateKey[] = "memento_value_in_local_state"; | |
55 const char kMementoValueInFileKey[] = "memento_value_in_file"; | |
56 | 85 |
57 COMPILE_ASSERT( | |
58 arraysize(kSatisfiedCriteriaMaskKeys) == kSatisfiedCriteriaMaskBits, | |
59 satisfied_criteria_mask_bits_mismatch); | |
60 COMPILE_ASSERT(arraysize(kCombinedStatusMaskKeys) == kCombinedStatusMaskBits, | |
61 combined_status_mask_bits_mismatch); | |
62 | 86 |
63 // Implementation detail classes --------------------------------------------- | 87 // Number of bits, and maximum value (exclusive) for the mask whose bits |
88 // indicate which of reset criteria were satisfied. | |
89 const size_t kSatisfiedCriteriaMaskNumberOfBits = 2u; | |
90 const uint32 kSatisfiedCriteriaMaskMaximumValue = | |
91 (1u << kSatisfiedCriteriaMaskNumberOfBits); | |
64 | 92 |
65 class AutomaticProfileResetterDelegateImpl | 93 // Number of bits, and maximum value (exclusive) for the mask whose bits |
66 : public AutomaticProfileResetterDelegate { | 94 // indicate if any of reset criteria were satisfied, and which of the mementos |
67 public: | 95 // were already present. |
68 AutomaticProfileResetterDelegateImpl() {} | 96 const size_t kCombinedStatusMaskNumberOfBits = 4u; |
69 virtual ~AutomaticProfileResetterDelegateImpl() {} | 97 const uint32 kCombinedStatusMaskMaximumValue = |
98 (1u << kCombinedStatusMaskNumberOfBits); | |
70 | 99 |
71 // AutomaticProfileResetterDelegate overrides: | 100 COMPILE_ASSERT(arraysize(kSatisfiedCriteriaMaskKeys) == |
72 | 101 kSatisfiedCriteriaMaskNumberOfBits, satisfied_criteria_mask_bits_mismatch); |
Peter Kasting
2013/10/18 18:39:16
Tiny nit: Consider linebreaking these like:
COMPI
engedy
2013/10/22 19:44:07
Done.
| |
73 virtual void ShowPrompt() OVERRIDE { | 102 COMPILE_ASSERT(arraysize(kCombinedStatusMaskKeys) == |
74 // TODO(engedy): Call the UI from here once we have it. | 103 kCombinedStatusMaskNumberOfBits, combined_status_mask_bits_mismatch); |
75 } | |
76 | |
77 virtual void ReportStatistics(uint32 satisfied_criteria_mask, | |
78 uint32 combined_status_mask) OVERRIDE { | |
79 UMA_HISTOGRAM_ENUMERATION("AutomaticProfileReset.SatisfiedCriteriaMask", | |
80 satisfied_criteria_mask, | |
81 kSatisfiedCriteriaMaskMaximumValue); | |
82 UMA_HISTOGRAM_ENUMERATION("AutomaticProfileReset.CombinedStatusMask", | |
83 combined_status_mask, | |
84 kCombinedStatusMaskMaximumValue); | |
85 } | |
86 | |
87 private: | |
88 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateImpl); | |
89 }; | |
90 | 104 |
91 // Enumeration of the possible outcomes of showing the profile reset prompt. | 105 // Enumeration of the possible outcomes of showing the profile reset prompt. |
92 enum PromptResult { | 106 enum PromptResult { |
93 // Prompt was not shown because only a dry-run was performed. | 107 // Prompt was not shown because only a dry-run was performed. |
94 PROMPT_NOT_SHOWN, | 108 PROMPT_NOT_SHOWN, |
95 PROMPT_ACTION_RESET, | 109 PROMPT_ACTION_RESET, |
96 PROMPT_ACTION_NO_RESET, | 110 PROMPT_ACTION_NO_RESET, |
97 PROMPT_DISMISSED, | 111 PROMPT_DISMISSED, |
98 // Prompt was still shown (not dismissed by the user) when Chrome was closed. | 112 // Prompt was still shown (not dismissed by the user) when Chrome was closed. |
99 PROMPT_IGNORED, | 113 PROMPT_IGNORED, |
100 PROMPT_RESULT_MAX | 114 PROMPT_RESULT_MAX |
101 }; | 115 }; |
102 | 116 |
117 // Returns whether or not a dry-run shall be performed. | |
118 bool ShouldPerformDryRun() { | |
119 return base::FieldTrialList::FindFullName(kAutomaticProfileResetStudyName) == | |
120 kAutomaticProfileResetStudyDryRunGroupName; | |
121 } | |
122 | |
123 // Returns whether or not a live-run shall be performed. | |
124 bool ShouldPerformLiveRun() { | |
125 return base::FieldTrialList::FindFullName(kAutomaticProfileResetStudyName) == | |
126 kAutomaticProfileResetStudyEnabledGroupName; | |
127 } | |
128 | |
129 // Deep-copies all preferences in |source| to a sub-tree named |value_tree_key| | |
130 // in |target_dictionary|, with path expansion, and also creates an isomorphic | |
131 // sub-tree under the key |is_user_controlled_tree_key| that contains only | |
132 // Boolean values, indicating whether or not the corresponding preferences are | |
133 // coming from the 'user' PrefStore. | |
134 void BuildSubTreesFromPreferences(const PrefService* source, | |
135 const char* value_tree_key, | |
136 const char* is_user_controlled_tree_key, | |
137 base::DictionaryValue* target_dictionary) { | |
138 scoped_ptr<base::DictionaryValue> pref_name_to_value_map( | |
139 source->GetPreferenceValuesWithoutPathExpansion()); | |
140 std::vector<std::string> pref_names; | |
141 pref_names.reserve(pref_name_to_value_map->size()); | |
142 for (base::DictionaryValue::Iterator it(*pref_name_to_value_map); | |
143 !it.IsAtEnd(); it.Advance()) | |
144 pref_names.push_back(it.key()); | |
145 | |
146 base::DictionaryValue* value_tree = new base::DictionaryValue; | |
147 base::DictionaryValue* is_user_controlled_tree = new base::DictionaryValue; | |
148 for (std::vector<std::string>::const_iterator it = pref_names.begin(); | |
149 it != pref_names.end(); ++it) { | |
150 scoped_ptr<Value> pref_value_owned; | |
151 if (pref_name_to_value_map->RemoveWithoutPathExpansion(*it, | |
152 &pref_value_owned)) { | |
153 value_tree->Set(*it, pref_value_owned.release()); | |
154 const PrefService::Preference* pref = source->FindPreference(it->c_str()); | |
155 is_user_controlled_tree->Set( | |
156 *it, new base::FundamentalValue(pref->IsUserControlled())); | |
157 } | |
158 } | |
159 target_dictionary->Set(value_tree_key, value_tree); | |
160 target_dictionary->Set(is_user_controlled_tree_key, is_user_controlled_tree); | |
161 } | |
162 | |
103 } // namespace | 163 } // namespace |
104 | 164 |
105 // AutomaticProfileResetter::EvaluationResults ------------------------------- | |
106 | |
107 // Encapsulates the output values extracted from the evaluator program. | |
108 struct AutomaticProfileResetter::EvaluationResults { | |
109 EvaluationResults() | |
110 : had_prompted_already(false), | |
111 satisfied_criteria_mask(0), | |
112 combined_status_mask(0) {} | |
113 | |
114 std::string memento_value_in_prefs; | |
115 std::string memento_value_in_local_state; | |
116 std::string memento_value_in_file; | |
117 | |
118 bool had_prompted_already; | |
119 uint32 satisfied_criteria_mask; | |
120 uint32 combined_status_mask; | |
121 }; | |
122 | 165 |
123 // AutomaticProfileResetter -------------------------------------------------- | 166 // AutomaticProfileResetter -------------------------------------------------- |
124 | 167 |
125 AutomaticProfileResetter::AutomaticProfileResetter(Profile* profile) | 168 AutomaticProfileResetter::AutomaticProfileResetter(Profile* profile) |
126 : profile_(profile), | 169 : profile_(profile), |
127 state_(STATE_UNINITIALIZED), | 170 state_(STATE_UNINITIALIZED), |
171 enumeration_of_loaded_modules_ready_(false), | |
172 template_url_service_ready_(false), | |
128 memento_in_prefs_(profile_), | 173 memento_in_prefs_(profile_), |
129 memento_in_local_state_(profile_), | 174 memento_in_local_state_(profile_), |
130 memento_in_file_(profile_), | 175 memento_in_file_(profile_), |
131 weak_ptr_factory_(this) { | 176 weak_ptr_factory_(this) { |
132 DCHECK(profile_); | 177 DCHECK(profile_); |
178 Initialize(); | |
133 } | 179 } |
134 | 180 |
135 AutomaticProfileResetter::~AutomaticProfileResetter() {} | 181 AutomaticProfileResetter::~AutomaticProfileResetter() {} |
136 | 182 |
183 void AutomaticProfileResetter::Activate() { | |
184 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
185 DCHECK(state_ == STATE_INITIALIZED || state_ == STATE_DISABLED); | |
186 | |
187 if (state_ == STATE_INITIALIZED) { | |
188 if (!program_.empty()) { | |
189 // Some steps in the flow (e.g. loaded modules, file-based memento) are | |
190 // IO-intensive, so defer execution until some time later. | |
191 task_runner_for_waiting_->PostDelayedTask( | |
192 FROM_HERE, | |
193 base::Bind(&AutomaticProfileResetter::PrepareEvaluationFlow, | |
194 weak_ptr_factory_.GetWeakPtr()), | |
195 base::TimeDelta::FromSeconds(kEvaluationFlowDelayInSeconds)); | |
196 } else { | |
197 // Terminate early if there is no program included (nor set by tests). | |
198 state_ = STATE_DISABLED; | |
199 } | |
200 } | |
201 } | |
202 | |
203 void AutomaticProfileResetter::SetHashSeedForTesting( | |
204 const base::StringPiece& hash_key) { | |
205 hash_seed_ = hash_key; | |
206 } | |
207 | |
208 void AutomaticProfileResetter::SetProgramForTesting( | |
209 const base::StringPiece& program) { | |
210 program_ = program; | |
211 } | |
212 | |
213 void AutomaticProfileResetter::SetDelegateForTesting( | |
214 scoped_ptr<AutomaticProfileResetterDelegate> delegate) { | |
215 delegate_ = delegate.Pass(); | |
216 } | |
217 | |
218 void AutomaticProfileResetter::SetTaskRunnerForWaitingForTesting( | |
219 const scoped_refptr<base::TaskRunner>& task_runner) { | |
220 task_runner_for_waiting_ = task_runner; | |
221 } | |
222 | |
137 void AutomaticProfileResetter::Initialize() { | 223 void AutomaticProfileResetter::Initialize() { |
138 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 224 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
139 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 225 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
140 | 226 |
141 if (ShouldPerformDryRun() || ShouldPerformLiveRun()) { | 227 if (ShouldPerformDryRun() || ShouldPerformLiveRun()) { |
142 ui::ResourceBundle& resources(ui::ResourceBundle::GetSharedInstance()); | 228 ui::ResourceBundle& resources(ui::ResourceBundle::GetSharedInstance()); |
143 if (ShouldPerformLiveRun()) { | 229 if (ShouldPerformLiveRun()) { |
144 program_ = | 230 program_ = |
145 resources.GetRawDataResource(IDR_AUTOMATIC_PROFILE_RESET_RULES); | 231 resources.GetRawDataResource(IDR_AUTOMATIC_PROFILE_RESET_RULES); |
146 hash_seed_ = | 232 hash_seed_ = |
147 resources.GetRawDataResource(IDR_AUTOMATIC_PROFILE_RESET_HASH_SEED); | 233 resources.GetRawDataResource(IDR_AUTOMATIC_PROFILE_RESET_HASH_SEED); |
148 } else { // ShouldPerformDryRun() | 234 } else { // ShouldPerformDryRun() |
149 program_ = | 235 program_ = |
150 resources.GetRawDataResource(IDR_AUTOMATIC_PROFILE_RESET_RULES_DRY); | 236 resources.GetRawDataResource(IDR_AUTOMATIC_PROFILE_RESET_RULES_DRY); |
151 hash_seed_ = resources.GetRawDataResource( | 237 hash_seed_ = resources.GetRawDataResource( |
152 IDR_AUTOMATIC_PROFILE_RESET_HASH_SEED_DRY); | 238 IDR_AUTOMATIC_PROFILE_RESET_HASH_SEED_DRY); |
153 } | 239 } |
154 delegate_.reset(new AutomaticProfileResetterDelegateImpl()); | 240 delegate_.reset(new AutomaticProfileResetterDelegateImpl( |
241 TemplateURLServiceFactory::GetForProfile(profile_))); | |
242 task_runner_for_waiting_ = | |
243 content::BrowserThread::GetMessageLoopProxyForThread( | |
244 content::BrowserThread::UI); | |
245 state_ = STATE_INITIALIZED; | |
246 } else { | |
247 state_ = STATE_DISABLED; | |
248 } | |
249 } | |
155 | 250 |
251 void AutomaticProfileResetter::PrepareEvaluationFlow() { | |
252 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
253 DCHECK_EQ(state_, STATE_INITIALIZED); | |
254 | |
255 state_ = STATE_WAITING_ON_DEPENDENCIES; | |
256 | |
257 delegate_->RequestCallbackWhenTemplateURLServiceIsLoaded( | |
258 base::Bind(&AutomaticProfileResetter::OnTemplateURLServiceIsLoaded, | |
259 weak_ptr_factory_.GetWeakPtr())); | |
260 delegate_->RequestCallbackWhenLoadedModulesAreEnumerated( | |
261 base::Bind(&AutomaticProfileResetter::OnLoadedModulesAreEnumerated, | |
262 weak_ptr_factory_.GetWeakPtr())); | |
263 delegate_->LoadTemplateURLServiceIfNeeded(); | |
264 delegate_->EnumerateLoadedModulesIfNeeded(); | |
265 } | |
266 | |
267 void AutomaticProfileResetter::OnTemplateURLServiceIsLoaded() { | |
268 template_url_service_ready_ = true; | |
269 OnDependencyIsReady(); | |
270 } | |
271 | |
272 void AutomaticProfileResetter::OnLoadedModulesAreEnumerated() { | |
273 enumeration_of_loaded_modules_ready_ = true; | |
274 OnDependencyIsReady(); | |
275 } | |
276 | |
277 void AutomaticProfileResetter::OnDependencyIsReady() { | |
278 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
279 DCHECK_EQ(state_, STATE_WAITING_ON_DEPENDENCIES); | |
280 | |
281 if (template_url_service_ready_ && enumeration_of_loaded_modules_ready_) { | |
156 state_ = STATE_READY; | 282 state_ = STATE_READY; |
157 | |
158 content::BrowserThread::PostTask( | 283 content::BrowserThread::PostTask( |
159 content::BrowserThread::UI, | 284 content::BrowserThread::UI, |
160 FROM_HERE, | 285 FROM_HERE, |
161 base::Bind(&AutomaticProfileResetter::BeginEvaluationFlow, | 286 base::Bind(&AutomaticProfileResetter::BeginEvaluationFlow, |
162 weak_ptr_factory_.GetWeakPtr())); | 287 weak_ptr_factory_.GetWeakPtr())); |
163 } else { | |
164 state_ = STATE_DISABLED; | |
165 } | 288 } |
166 } | 289 } |
167 | 290 |
168 bool AutomaticProfileResetter::ShouldPerformDryRun() const { | |
169 return base::FieldTrialList::FindFullName(kAutomaticProfileResetStudyName) == | |
170 kAutomaticProfileResetStudyDryRunGroupName; | |
171 } | |
172 | |
173 bool AutomaticProfileResetter::ShouldPerformLiveRun() const { | |
174 return base::FieldTrialList::FindFullName(kAutomaticProfileResetStudyName) == | |
175 kAutomaticProfileResetStudyEnabledGroupName; | |
176 } | |
177 | |
178 void AutomaticProfileResetter::BeginEvaluationFlow() { | 291 void AutomaticProfileResetter::BeginEvaluationFlow() { |
179 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 292 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
180 DCHECK_EQ(state_, STATE_READY); | 293 DCHECK_EQ(state_, STATE_READY); |
294 DCHECK(!program_.empty()); | |
181 | 295 |
182 if (!program_.empty()) { | 296 state_ = STATE_WORKING; |
183 state_ = STATE_WORKING; | 297 memento_in_file_.ReadValue( |
184 memento_in_file_.ReadValue( | 298 base::Bind(&AutomaticProfileResetter::ContinueWithEvaluationFlow, |
185 base::Bind(&AutomaticProfileResetter::ContinueWithEvaluationFlow, | 299 weak_ptr_factory_.GetWeakPtr())); |
186 weak_ptr_factory_.GetWeakPtr())); | |
187 } else { | |
188 // Terminate early if there is no program included (nor set by tests). | |
189 state_ = STATE_DISABLED; | |
190 } | |
191 } | 300 } |
192 | 301 |
193 scoped_ptr<base::DictionaryValue> | 302 scoped_ptr<base::DictionaryValue> |
194 AutomaticProfileResetter::BuildEvaluatorProgramInput( | 303 AutomaticProfileResetter::BuildEvaluatorProgramInput( |
195 const std::string& memento_value_in_file) { | 304 const std::string& memento_value_in_file) { |
196 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 305 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
197 // TODO(engedy): Add any additional state here that is needed by the program. | 306 |
198 scoped_ptr<base::DictionaryValue> input(new base::DictionaryValue); | 307 scoped_ptr<base::DictionaryValue> input(new base::DictionaryValue); |
308 | |
309 // Include memento values (or empty strings in case mementos are not there). | |
199 input->SetString(kMementoValueInPrefsKey, memento_in_prefs_.ReadValue()); | 310 input->SetString(kMementoValueInPrefsKey, memento_in_prefs_.ReadValue()); |
200 input->SetString(kMementoValueInLocalStateKey, | 311 input->SetString(kMementoValueInLocalStateKey, |
201 memento_in_local_state_.ReadValue()); | 312 memento_in_local_state_.ReadValue()); |
202 input->SetString(kMementoValueInFileKey, memento_value_in_file); | 313 input->SetString(kMementoValueInFileKey, memento_value_in_file); |
314 | |
315 // Include all user (i.e. profile-specific) preferences, along with | |
316 // information about whether the value is coming from the 'user' PrefStore. | |
317 PrefService* prefs = profile_->GetPrefs(); | |
318 DCHECK(prefs); | |
319 BuildSubTreesFromPreferences(prefs, | |
320 kUserPreferencesKey, | |
321 kUserPreferencesIsUserControlledKey, | |
322 input.get()); | |
323 | |
324 // Include all local state (i.e. shared) preferences, along with information | |
325 // about whether the value is coming from the 'user' PrefStore. | |
326 PrefService* local_state = g_browser_process->local_state(); | |
327 DCHECK(local_state); | |
328 BuildSubTreesFromPreferences( | |
329 local_state, kLocalStateKey, kLocalStateIsUserControlledKey, input.get()); | |
330 | |
331 // Include all information related to search engines. | |
332 scoped_ptr<base::DictionaryValue> default_search_provider_details( | |
333 delegate_->GetDefaultSearchProviderDetails()); | |
334 input->Set(kDefaultSearchProviderKey, | |
335 default_search_provider_details.release()); | |
336 | |
337 scoped_ptr<base::ListValue> search_providers_details( | |
338 delegate_->GetPrepopulatedSearchProvidersDetails()); | |
339 input->Set(kSearchProvidersKey, search_providers_details.release()); | |
340 | |
341 input->SetBoolean(kDefaultSearchProviderIsUserControlledKey, | |
342 !delegate_->IsDefaultSearchProviderManaged()); | |
343 | |
344 // Include information about loaded modules. | |
345 scoped_ptr<base::ListValue> loaded_module_digests( | |
346 delegate_->GetLoadedModuleNameDigests()); | |
347 input->Set(kLoadedModuleDigestsKey, loaded_module_digests.release()); | |
348 | |
203 return input.Pass(); | 349 return input.Pass(); |
204 } | 350 } |
205 | 351 |
206 void AutomaticProfileResetter::ContinueWithEvaluationFlow( | 352 void AutomaticProfileResetter::ContinueWithEvaluationFlow( |
207 const std::string& memento_value_in_file) { | 353 const std::string& memento_value_in_file) { |
208 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 354 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
209 DCHECK_EQ(state_, STATE_WORKING); | 355 DCHECK_EQ(state_, STATE_WORKING); |
210 PrefService* prefs = profile_->GetPrefs(); | 356 PrefService* prefs = profile_->GetPrefs(); |
211 DCHECK(prefs); | 357 DCHECK(prefs); |
212 | 358 |
(...skipping 12 matching lines...) Expand all Loading... | |
225 base::Bind(&EvaluateConditionsOnWorkerPoolThread, | 371 base::Bind(&EvaluateConditionsOnWorkerPoolThread, |
226 hash_seed_, | 372 hash_seed_, |
227 program_, | 373 program_, |
228 base::Passed(&input)), | 374 base::Passed(&input)), |
229 base::Bind(&AutomaticProfileResetter::FinishEvaluationFlow, | 375 base::Bind(&AutomaticProfileResetter::FinishEvaluationFlow, |
230 weak_ptr_factory_.GetWeakPtr())); | 376 weak_ptr_factory_.GetWeakPtr())); |
231 } | 377 } |
232 | 378 |
233 // static | 379 // static |
234 scoped_ptr<AutomaticProfileResetter::EvaluationResults> | 380 scoped_ptr<AutomaticProfileResetter::EvaluationResults> |
235 AutomaticProfileResetter::EvaluateConditionsOnWorkerPoolThread( | 381 AutomaticProfileResetter::EvaluateConditionsOnWorkerPoolThread( |
236 const base::StringPiece& hash_seed, | 382 const base::StringPiece& hash_seed, |
237 const base::StringPiece& program, | 383 const base::StringPiece& program, |
238 scoped_ptr<base::DictionaryValue> program_input) { | 384 scoped_ptr<base::DictionaryValue> program_input) { |
239 std::string hash_seed_str(hash_seed.as_string()); | 385 JtlInterpreter interpreter( |
240 std::string program_str(program.as_string()); | 386 hash_seed.as_string(), program.as_string(), program_input.get()); |
241 JtlInterpreter interpreter(hash_seed_str, program_str, program_input.get()); | |
242 interpreter.Execute(); | 387 interpreter.Execute(); |
243 UMA_HISTOGRAM_ENUMERATION("AutomaticProfileReset.InterpreterResult", | 388 UMA_HISTOGRAM_ENUMERATION("AutomaticProfileReset.InterpreterResult", |
244 interpreter.result(), | 389 interpreter.result(), |
245 JtlInterpreter::RESULT_MAX); | 390 JtlInterpreter::RESULT_MAX); |
246 | 391 |
247 // In each case below, the respective field in result originally contains the | 392 // In each case below, the respective field in result originally contains the |
248 // default, so if the getter fails, we still have the correct value there. | 393 // default, so if the getter fails, we still have the correct value there. |
249 scoped_ptr<EvaluationResults> results(new EvaluationResults()); | 394 scoped_ptr<EvaluationResults> results( |
395 new AutomaticProfileResetter::EvaluationResults); | |
250 interpreter.GetOutputBoolean(kHadPromptedAlreadyKey, | 396 interpreter.GetOutputBoolean(kHadPromptedAlreadyKey, |
251 &results->had_prompted_already); | 397 &results->had_prompted_already); |
252 interpreter.GetOutputString(kMementoValueInPrefsKey, | 398 interpreter.GetOutputString(kMementoValueInPrefsKey, |
253 &results->memento_value_in_prefs); | 399 &results->memento_value_in_prefs); |
254 interpreter.GetOutputString(kMementoValueInLocalStateKey, | 400 interpreter.GetOutputString(kMementoValueInLocalStateKey, |
255 &results->memento_value_in_local_state); | 401 &results->memento_value_in_local_state); |
256 interpreter.GetOutputString(kMementoValueInFileKey, | 402 interpreter.GetOutputString(kMementoValueInFileKey, |
257 &results->memento_value_in_file); | 403 &results->memento_value_in_file); |
258 for (size_t i = 0; i < arraysize(kCombinedStatusMaskKeys); ++i) { | 404 for (size_t i = 0; i < arraysize(kCombinedStatusMaskKeys); ++i) { |
259 bool flag = false; | 405 bool flag = false; |
260 if (interpreter.GetOutputBoolean(kCombinedStatusMaskKeys[i], &flag) && flag) | 406 if (interpreter.GetOutputBoolean(kCombinedStatusMaskKeys[i], &flag) && flag) |
261 results->combined_status_mask |= (1 << i); | 407 results->combined_status_mask |= (1 << i); |
262 } | 408 } |
263 for (size_t i = 0; i < arraysize(kSatisfiedCriteriaMaskKeys); ++i) { | 409 for (size_t i = 0; i < arraysize(kSatisfiedCriteriaMaskKeys); ++i) { |
264 bool flag = false; | 410 bool flag = false; |
265 if (interpreter.GetOutputBoolean(kSatisfiedCriteriaMaskKeys[i], &flag) && | 411 if (interpreter.GetOutputBoolean(kSatisfiedCriteriaMaskKeys[i], &flag) && |
266 flag) | 412 flag) |
267 results->satisfied_criteria_mask |= (1 << i); | 413 results->satisfied_criteria_mask |= (1 << i); |
268 } | 414 } |
269 return results.Pass(); | 415 return results.Pass(); |
270 } | 416 } |
271 | 417 |
272 void AutomaticProfileResetter::FinishEvaluationFlow( | 418 void AutomaticProfileResetter::FinishEvaluationFlow( |
273 scoped_ptr<EvaluationResults> results) { | 419 scoped_ptr<EvaluationResults> results) { |
274 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 420 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
275 DCHECK_EQ(state_, STATE_WORKING); | 421 DCHECK_EQ(state_, STATE_WORKING); |
276 | 422 |
277 delegate_->ReportStatistics(results->satisfied_criteria_mask, | 423 ReportStatistics(results->satisfied_criteria_mask, |
278 results->combined_status_mask); | 424 results->combined_status_mask); |
279 | 425 |
280 if (results->satisfied_criteria_mask != 0 && !results->had_prompted_already) { | 426 if (results->satisfied_criteria_mask != 0 && !results->had_prompted_already) { |
281 memento_in_prefs_.StoreValue(results->memento_value_in_prefs); | 427 memento_in_prefs_.StoreValue(results->memento_value_in_prefs); |
282 memento_in_local_state_.StoreValue(results->memento_value_in_local_state); | 428 memento_in_local_state_.StoreValue(results->memento_value_in_local_state); |
283 memento_in_file_.StoreValue(results->memento_value_in_file); | 429 memento_in_file_.StoreValue(results->memento_value_in_file); |
284 | 430 |
285 if (ShouldPerformLiveRun()) { | 431 if (ShouldPerformLiveRun()) { |
286 delegate_->ShowPrompt(); | 432 delegate_->ShowPrompt(); |
287 } else { | 433 } else { |
288 UMA_HISTOGRAM_ENUMERATION("AutomaticProfileReset.PromptResult", | 434 UMA_HISTOGRAM_ENUMERATION("AutomaticProfileReset.PromptResult", |
289 PROMPT_NOT_SHOWN, | 435 PROMPT_NOT_SHOWN, |
290 PROMPT_RESULT_MAX); | 436 PROMPT_RESULT_MAX); |
291 } | 437 } |
292 } | 438 } |
293 | 439 |
294 state_ = STATE_DONE; | 440 state_ = STATE_DONE; |
295 } | 441 } |
296 | 442 |
297 void AutomaticProfileResetter::SetHashSeedForTesting( | 443 void AutomaticProfileResetter::ReportStatistics(uint32 satisfied_criteria_mask, |
298 const base::StringPiece& hash_key) { | 444 uint32 combined_status_mask) { |
299 hash_seed_ = hash_key; | 445 UMA_HISTOGRAM_ENUMERATION("AutomaticProfileReset.SatisfiedCriteriaMask", |
300 } | 446 satisfied_criteria_mask, |
301 | 447 kSatisfiedCriteriaMaskMaximumValue); |
302 void AutomaticProfileResetter::SetProgramForTesting( | 448 UMA_HISTOGRAM_ENUMERATION("AutomaticProfileReset.CombinedStatusMask", |
303 const base::StringPiece& program) { | 449 combined_status_mask, |
304 program_ = program; | 450 kCombinedStatusMaskMaximumValue); |
305 } | |
306 | |
307 void AutomaticProfileResetter::SetDelegateForTesting( | |
308 AutomaticProfileResetterDelegate* delegate) { | |
309 delegate_.reset(delegate); | |
310 } | 451 } |
311 | 452 |
312 void AutomaticProfileResetter::Shutdown() { | 453 void AutomaticProfileResetter::Shutdown() { |
313 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 454 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
314 | 455 |
315 state_ = STATE_DISABLED; | 456 state_ = STATE_DISABLED; |
316 delegate_.reset(); | 457 delegate_.reset(); |
317 weak_ptr_factory_.InvalidateWeakPtrs(); | 458 weak_ptr_factory_.InvalidateWeakPtrs(); |
318 } | 459 } |
OLD | NEW |