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/prefs/pref_metrics_service.h" | 5 #include "chrome/browser/prefs/pref_metrics_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 | 73 |
74 } // namespace | 74 } // namespace |
75 | 75 |
76 PrefMetricsService::PrefMetricsService(Profile* profile) | 76 PrefMetricsService::PrefMetricsService(Profile* profile) |
77 : profile_(profile), | 77 : profile_(profile), |
78 prefs_(profile_->GetPrefs()), | 78 prefs_(profile_->GetPrefs()), |
79 local_state_(g_browser_process->local_state()), | 79 local_state_(g_browser_process->local_state()), |
80 profile_name_(profile_->GetPath().AsUTF8Unsafe()), | 80 profile_name_(profile_->GetPath().AsUTF8Unsafe()), |
81 tracked_pref_paths_(kTrackedPrefs), | 81 tracked_pref_paths_(kTrackedPrefs), |
82 tracked_pref_path_count_(arraysize(kTrackedPrefs)), | 82 tracked_pref_path_count_(arraysize(kTrackedPrefs)), |
| 83 #if defined(ENABLE_DCHECK) |
83 checked_tracked_prefs_(false), | 84 checked_tracked_prefs_(false), |
| 85 #endif |
84 weak_factory_(this) { | 86 weak_factory_(this) { |
85 pref_hash_seed_ = ResourceBundle::GetSharedInstance().GetRawDataResource( | 87 pref_hash_seed_ = ResourceBundle::GetSharedInstance().GetRawDataResource( |
86 IDR_PREF_HASH_SEED_BIN).as_string(); | 88 IDR_PREF_HASH_SEED_BIN).as_string(); |
87 | 89 |
88 RecordLaunchPrefs(); | 90 RecordLaunchPrefs(); |
89 | 91 |
90 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); | 92 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
91 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs)); | 93 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs)); |
92 | 94 |
93 RegisterSyncedPrefObservers(); | 95 RegisterSyncedPrefObservers(); |
(...skipping 17 matching lines...) Expand all Loading... |
111 const char** tracked_pref_paths, | 113 const char** tracked_pref_paths, |
112 int tracked_pref_path_count) | 114 int tracked_pref_path_count) |
113 : profile_(profile), | 115 : profile_(profile), |
114 prefs_(profile->GetPrefs()), | 116 prefs_(profile->GetPrefs()), |
115 local_state_(local_state), | 117 local_state_(local_state), |
116 profile_name_(profile_->GetPath().AsUTF8Unsafe()), | 118 profile_name_(profile_->GetPath().AsUTF8Unsafe()), |
117 pref_hash_seed_(kSHA256DigestSize, 0), | 119 pref_hash_seed_(kSHA256DigestSize, 0), |
118 device_id_(device_id), | 120 device_id_(device_id), |
119 tracked_pref_paths_(tracked_pref_paths), | 121 tracked_pref_paths_(tracked_pref_paths), |
120 tracked_pref_path_count_(tracked_pref_path_count), | 122 tracked_pref_path_count_(tracked_pref_path_count), |
| 123 #if defined(ENABLE_DCHECK) |
121 checked_tracked_prefs_(false), | 124 checked_tracked_prefs_(false), |
| 125 #endif |
122 weak_factory_(this) { | 126 weak_factory_(this) { |
123 CheckTrackedPreferences(); | 127 CheckTrackedPreferences(); |
124 } | 128 } |
125 | 129 |
126 PrefMetricsService::~PrefMetricsService() { | 130 PrefMetricsService::~PrefMetricsService() { |
127 } | 131 } |
128 | 132 |
129 void PrefMetricsService::RecordLaunchPrefs() { | 133 void PrefMetricsService::RecordLaunchPrefs() { |
130 bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton); | 134 bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton); |
131 bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage); | 135 bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( | 257 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( |
254 histogram_name, | 258 histogram_name, |
255 1, | 259 1, |
256 boundary_value, | 260 boundary_value, |
257 boundary_value + 1, | 261 boundary_value + 1, |
258 base::HistogramBase::kUmaTargetedHistogramFlag); | 262 base::HistogramBase::kUmaTargetedHistogramFlag); |
259 histogram->Add(integer_value); | 263 histogram->Add(integer_value); |
260 } | 264 } |
261 | 265 |
262 void PrefMetricsService::GetDeviceIdCallback(const std::string& device_id) { | 266 void PrefMetricsService::GetDeviceIdCallback(const std::string& device_id) { |
| 267 #if !defined(OS_WIN) || defined(ENABLE_RLZ) |
| 268 // A device_id is expected in all scenarios except when RLZ is disabled on |
| 269 // Windows. |
| 270 DCHECK(!device_id.empty()); |
| 271 #endif |
| 272 |
263 device_id_ = device_id; | 273 device_id_ = device_id; |
264 // On Aura, this seems to be called twice. | 274 CheckTrackedPreferences(); |
265 if (!checked_tracked_prefs_) | |
266 CheckTrackedPreferences(); | |
267 } | 275 } |
268 | 276 |
269 // To detect changes to Preferences that happen outside of Chrome, we hash | 277 // To detect changes to Preferences that happen outside of Chrome, we hash |
270 // selected pref values and save them in local state. CheckTrackedPreferences | 278 // selected pref values and save them in local state. CheckTrackedPreferences |
271 // compares the saved values to the values observed in the profile's prefs. A | 279 // compares the saved values to the values observed in the profile's prefs. A |
272 // dictionary of dictionaries in local state holds the hashed values, grouped by | 280 // dictionary of dictionaries in local state holds the hashed values, grouped by |
273 // profile. To make the system more resistant to spoofing, pref values are | 281 // profile. To make the system more resistant to spoofing, pref values are |
274 // hashed with the pref path and the device id. | 282 // hashed with the pref path and the device id. |
275 void PrefMetricsService::CheckTrackedPreferences() { | 283 void PrefMetricsService::CheckTrackedPreferences() { |
| 284 #if defined(ENABLE_DCHECK) |
| 285 // Make sure this is only called once per instance of this service. |
276 DCHECK(!checked_tracked_prefs_); | 286 DCHECK(!checked_tracked_prefs_); |
| 287 checked_tracked_prefs_ = true; |
| 288 #endif |
277 | 289 |
278 const base::DictionaryValue* pref_hash_dicts = | 290 const base::DictionaryValue* pref_hash_dicts = |
279 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); | 291 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); |
280 // Get the hashed prefs dictionary if it exists. If it doesn't, it will be | 292 // Get the hashed prefs dictionary if it exists. If it doesn't, it will be |
281 // created if we set preference values below. | 293 // created if we set preference values below. |
282 const base::DictionaryValue* hashed_prefs = NULL; | 294 const base::DictionaryValue* hashed_prefs = NULL; |
283 pref_hash_dicts->GetDictionaryWithoutPathExpansion(profile_name_, | 295 pref_hash_dicts->GetDictionaryWithoutPathExpansion(profile_name_, |
284 &hashed_prefs); | 296 &hashed_prefs); |
285 for (int i = 0; i < tracked_pref_path_count_; ++i) { | 297 for (int i = 0; i < tracked_pref_path_count_; ++i) { |
286 // Skip prefs that haven't been registered. | 298 // Skip prefs that haven't been registered. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 } | 342 } |
331 } else { | 343 } else { |
332 // Record that we haven't tracked this preference yet, or the hash in | 344 // Record that we haven't tracked this preference yet, or the hash in |
333 // local state was removed. | 345 // local state was removed. |
334 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceInitialized", | 346 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceInitialized", |
335 i, tracked_pref_path_count_); | 347 i, tracked_pref_path_count_); |
336 UpdateTrackedPreference(tracked_pref_paths_[i]); | 348 UpdateTrackedPreference(tracked_pref_paths_[i]); |
337 } | 349 } |
338 } | 350 } |
339 | 351 |
340 checked_tracked_prefs_ = true; | |
341 | |
342 // Now that we've checked the incoming preferences, register for change | 352 // Now that we've checked the incoming preferences, register for change |
343 // notifications, unless this is test code. | 353 // notifications, unless this is test code. |
344 // TODO(bbudge) Fix failing browser_tests and so we can remove this test. | 354 // TODO(bbudge) Fix failing browser_tests and so we can remove this test. |
345 // Several tests fail when they shutdown before they can write local state. | 355 // Several tests fail when they shutdown before they can write local state. |
346 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType) && | 356 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType) && |
347 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { | 357 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { |
348 InitializePrefObservers(); | 358 InitializePrefObservers(); |
349 } | 359 } |
350 } | 360 } |
351 | 361 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 } | 465 } |
456 | 466 |
457 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { | 467 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { |
458 return false; | 468 return false; |
459 } | 469 } |
460 | 470 |
461 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( | 471 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( |
462 content::BrowserContext* context) const { | 472 content::BrowserContext* context) const { |
463 return chrome::GetBrowserContextRedirectedInIncognito(context); | 473 return chrome::GetBrowserContextRedirectedInIncognito(context); |
464 } | 474 } |
OLD | NEW |