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