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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 pref_hash_seed_ = ResourceBundle::GetSharedInstance().GetRawDataResource( | 85 pref_hash_seed_ = ResourceBundle::GetSharedInstance().GetRawDataResource( |
86 IDR_PREF_HASH_SEED_BIN).as_string(); | 86 IDR_PREF_HASH_SEED_BIN).as_string(); |
87 | 87 |
88 RecordLaunchPrefs(); | 88 RecordLaunchPrefs(); |
89 | 89 |
90 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); | 90 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
91 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs)); | 91 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs)); |
92 | 92 |
93 RegisterSyncedPrefObservers(); | 93 RegisterSyncedPrefObservers(); |
94 | 94 |
95 MarkNeedsEmptyValueForTrackedPreferences(); | |
96 | |
97 // The following code might cause callbacks into this instance before we exit | 95 // The following code might cause callbacks into this instance before we exit |
98 // the constructor. This instance should be initialized at this point. | 96 // the constructor. This instance should be initialized at this point. |
99 #if defined(OS_WIN) || defined(OS_MACOSX) | 97 #if defined(OS_WIN) || defined(OS_MACOSX) |
100 // We need the machine id to compute pref value hashes. Fetch that, and then | 98 // We need the machine id to compute pref value hashes. Fetch that, and then |
101 // call CheckTrackedPreferences in the callback. | 99 // call CheckTrackedPreferences in the callback. |
102 extensions::api::DeviceId::GetDeviceId( | 100 extensions::api::DeviceId::GetDeviceId( |
103 "PrefMetricsService", // non-empty string to obfuscate the device id. | 101 "PrefMetricsService", // non-empty string to obfuscate the device id. |
104 Bind(&PrefMetricsService::GetDeviceIdCallback, | 102 Bind(&PrefMetricsService::GetDeviceIdCallback, |
105 weak_factory_.GetWeakPtr())); | 103 weak_factory_.GetWeakPtr())); |
106 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 104 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
107 } | 105 } |
108 | 106 |
109 // For unit testing only. | 107 // For unit testing only. |
110 PrefMetricsService::PrefMetricsService(Profile* profile, | 108 PrefMetricsService::PrefMetricsService(Profile* profile, |
111 PrefService* local_state, | 109 PrefService* local_state, |
112 const std::string& device_id, | 110 const std::string& device_id, |
113 const char** tracked_pref_paths, | 111 const char** tracked_pref_paths, |
114 int tracked_pref_path_count) | 112 int tracked_pref_path_count) |
115 : profile_(profile), | 113 : profile_(profile), |
116 prefs_(profile->GetPrefs()), | 114 prefs_(profile->GetPrefs()), |
117 local_state_(local_state), | 115 local_state_(local_state), |
118 profile_name_(profile_->GetPath().AsUTF8Unsafe()), | 116 profile_name_(profile_->GetPath().AsUTF8Unsafe()), |
119 pref_hash_seed_(kSHA256DigestSize, 0), | 117 pref_hash_seed_(kSHA256DigestSize, 0), |
120 device_id_(device_id), | 118 device_id_(device_id), |
121 tracked_pref_paths_(tracked_pref_paths), | 119 tracked_pref_paths_(tracked_pref_paths), |
122 tracked_pref_path_count_(tracked_pref_path_count), | 120 tracked_pref_path_count_(tracked_pref_path_count), |
123 checked_tracked_prefs_(false), | 121 checked_tracked_prefs_(false), |
124 weak_factory_(this) { | 122 weak_factory_(this) { |
125 MarkNeedsEmptyValueForTrackedPreferences(); | |
126 CheckTrackedPreferences(); | 123 CheckTrackedPreferences(); |
127 } | 124 } |
128 | 125 |
129 PrefMetricsService::~PrefMetricsService() { | 126 PrefMetricsService::~PrefMetricsService() { |
130 } | 127 } |
131 | 128 |
132 void PrefMetricsService::RecordLaunchPrefs() { | 129 void PrefMetricsService::RecordLaunchPrefs() { |
133 bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton); | 130 bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton); |
134 bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage); | 131 bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage); |
135 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); | 132 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 #if !defined(OS_WIN) || defined(ENABLE_RLZ) | 263 #if !defined(OS_WIN) || defined(ENABLE_RLZ) |
267 // A device_id is expected in all scenarios except when RLZ is disabled on | 264 // A device_id is expected in all scenarios except when RLZ is disabled on |
268 // Windows. | 265 // Windows. |
269 DCHECK(!device_id.empty()); | 266 DCHECK(!device_id.empty()); |
270 #endif | 267 #endif |
271 | 268 |
272 device_id_ = device_id; | 269 device_id_ = device_id; |
273 CheckTrackedPreferences(); | 270 CheckTrackedPreferences(); |
274 } | 271 } |
275 | 272 |
276 void PrefMetricsService::MarkNeedsEmptyValueForTrackedPreferences() { | |
277 for (int i = 0; i < tracked_pref_path_count_; ++i) { | |
278 // Skip prefs that haven't been registered. | |
279 if (!prefs_->FindPreference(tracked_pref_paths_[i])) | |
280 continue; | |
281 | |
282 // Make sure tracked prefs are saved to disk even if empty. | |
283 // TODO(gab): Guarantee this for all prefs at a lower level and remove this | |
284 // hack. | |
285 prefs_->MarkUserStoreNeedsEmptyValue(tracked_pref_paths_[i]); | |
286 } | |
287 } | |
288 | |
289 // To detect changes to Preferences that happen outside of Chrome, we hash | 273 // To detect changes to Preferences that happen outside of Chrome, we hash |
290 // selected pref values and save them in local state. CheckTrackedPreferences | 274 // selected pref values and save them in local state. CheckTrackedPreferences |
291 // compares the saved values to the values observed in the profile's prefs. A | 275 // compares the saved values to the values observed in the profile's prefs. A |
292 // dictionary of dictionaries in local state holds the hashed values, grouped by | 276 // dictionary of dictionaries in local state holds the hashed values, grouped by |
293 // profile. To make the system more resistant to spoofing, pref values are | 277 // profile. To make the system more resistant to spoofing, pref values are |
294 // hashed with the pref path and the device id. | 278 // hashed with the pref path and the device id. |
295 void PrefMetricsService::CheckTrackedPreferences() { | 279 void PrefMetricsService::CheckTrackedPreferences() { |
296 // Make sure this is only called once per instance of this service. | 280 // Make sure this is only called once per instance of this service. |
297 DCHECK(!checked_tracked_prefs_); | 281 DCHECK(!checked_tracked_prefs_); |
298 checked_tracked_prefs_ = true; | 282 checked_tracked_prefs_ = true; |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 } | 454 } |
471 | 455 |
472 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { | 456 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { |
473 return false; | 457 return false; |
474 } | 458 } |
475 | 459 |
476 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( | 460 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( |
477 content::BrowserContext* context) const { | 461 content::BrowserContext* context) const { |
478 return chrome::GetBrowserContextRedirectedInIncognito(context); | 462 return chrome::GetBrowserContextRedirectedInIncognito(context); |
479 } | 463 } |
OLD | NEW |