| 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" | |
| 10 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 11 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
| 12 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 13 #include "base/prefs/scoped_user_pref_update.h" | |
| 14 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 15 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/browser_shutdown.h" | 14 #include "chrome/browser/browser_shutdown.h" |
| 17 // Accessing the Device ID API here is a layering violation. | |
| 18 // TODO(bbudge) Move the API so it's usable here. | |
| 19 // http://crbug.com/276485 | |
| 20 #include "chrome/browser/extensions/api/music_manager_private/device_id.h" | |
| 21 #include "chrome/browser/prefs/pref_service_syncable.h" | 15 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 22 #include "chrome/browser/prefs/session_startup_pref.h" | 16 #include "chrome/browser/prefs/session_startup_pref.h" |
| 23 #include "chrome/browser/prefs/synced_pref_change_registrar.h" | 17 #include "chrome/browser/prefs/synced_pref_change_registrar.h" |
| 24 #include "chrome/browser/profiles/incognito_helpers.h" | 18 #include "chrome/browser/profiles/incognito_helpers.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 20 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 27 #include "chrome/browser/ui/tabs/pinned_tab_codec.h" | 21 #include "chrome/browser/ui/tabs/pinned_tab_codec.h" |
| 28 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 29 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 30 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" | 24 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" |
| 31 #include "crypto/hmac.h" | 25 #include "crypto/hmac.h" |
| 32 #include "grit/browser_resources.h" | |
| 33 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 26 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 34 #include "ui/base/resource/resource_bundle.h" | |
| 35 | 27 |
| 36 namespace { | 28 namespace { |
| 37 | 29 |
| 38 const int kSessionStartupPrefValueMax = SessionStartupPref::kPrefValueMax; | 30 const int kSessionStartupPrefValueMax = SessionStartupPref::kPrefValueMax; |
| 39 | 31 |
| 40 // An unregistered preference to fill in indices in kTrackedPrefs below for | |
| 41 // preferences that aren't defined on every platform. This is fine as the code | |
| 42 // below (e.g. CheckTrackedPreferences()) skips unregistered preferences and | |
| 43 // should thus never report any data about that index on the platforms where | |
| 44 // that preference is unimplemented. | |
| 45 const char kUnregisteredPreference[] = "_"; | |
| 46 | |
| 47 // These preferences must be kept in sync with the TrackedPreference enum in | |
| 48 // tools/metrics/histograms/histograms.xml. To add a new preference, append it | |
| 49 // to the array and add a corresponding value to the histogram enum. | |
| 50 const char* kTrackedPrefs[] = { | |
| 51 prefs::kShowHomeButton, | |
| 52 prefs::kHomePageIsNewTabPage, | |
| 53 prefs::kHomePage, | |
| 54 prefs::kRestoreOnStartup, | |
| 55 prefs::kURLsToRestoreOnStartup, | |
| 56 prefs::kExtensionsPref, | |
| 57 prefs::kGoogleServicesLastUsername, | |
| 58 prefs::kSearchProviderOverrides, | |
| 59 prefs::kDefaultSearchProviderSearchURL, | |
| 60 prefs::kDefaultSearchProviderKeyword, | |
| 61 prefs::kDefaultSearchProviderName, | |
| 62 #if !defined(OS_ANDROID) | |
| 63 prefs::kPinnedTabs, | |
| 64 #else | |
| 65 kUnregisteredPreference, | |
| 66 #endif | |
| 67 prefs::kExtensionKnownDisabled, | |
| 68 prefs::kProfileResetPromptMemento, | |
| 69 }; | |
| 70 | |
| 71 const size_t kSHA256DigestSize = 32; | |
| 72 | |
| 73 } // namespace | 32 } // namespace |
| 74 | 33 |
| 75 PrefMetricsService::PrefMetricsService(Profile* profile) | 34 PrefMetricsService::PrefMetricsService(Profile* profile) |
| 76 : profile_(profile), | 35 : profile_(profile), |
| 77 prefs_(profile_->GetPrefs()), | 36 prefs_(profile_->GetPrefs()), |
| 78 local_state_(g_browser_process->local_state()), | 37 local_state_(g_browser_process->local_state()), |
| 79 profile_name_(profile_->GetPath().AsUTF8Unsafe()), | |
| 80 tracked_pref_paths_(kTrackedPrefs), | |
| 81 tracked_pref_path_count_(arraysize(kTrackedPrefs)), | |
| 82 checked_tracked_prefs_(false), | |
| 83 weak_factory_(this) { | 38 weak_factory_(this) { |
| 84 pref_hash_seed_ = ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 85 IDR_PREF_HASH_SEED_BIN).as_string(); | |
| 86 | |
| 87 RecordLaunchPrefs(); | 39 RecordLaunchPrefs(); |
| 88 | 40 |
| 89 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); | 41 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
| 90 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs)); | 42 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs)); |
| 91 | 43 |
| 92 RegisterSyncedPrefObservers(); | 44 RegisterSyncedPrefObservers(); |
| 93 | |
| 94 // The following code might cause callbacks into this instance before we exit | |
| 95 // the constructor. This instance should be initialized at this point. | |
| 96 #if defined(OS_WIN) || defined(OS_MACOSX) | |
| 97 // We need the machine id to compute pref value hashes. Fetch that, and then | |
| 98 // call CheckTrackedPreferences in the callback. | |
| 99 extensions::api::DeviceId::GetDeviceId( | |
| 100 "PrefMetricsService", // non-empty string to obfuscate the device id. | |
| 101 Bind(&PrefMetricsService::GetDeviceIdCallback, | |
| 102 weak_factory_.GetWeakPtr())); | |
| 103 #endif // defined(OS_WIN) || defined(OS_MACOSX) | |
| 104 } | 45 } |
| 105 | 46 |
| 106 // For unit testing only. | 47 // For unit testing only. |
| 107 PrefMetricsService::PrefMetricsService(Profile* profile, | 48 PrefMetricsService::PrefMetricsService(Profile* profile, |
| 108 PrefService* local_state, | 49 PrefService* local_state) |
| 109 const std::string& device_id, | |
| 110 const char** tracked_pref_paths, | |
| 111 int tracked_pref_path_count) | |
| 112 : profile_(profile), | 50 : profile_(profile), |
| 113 prefs_(profile->GetPrefs()), | 51 prefs_(profile->GetPrefs()), |
| 114 local_state_(local_state), | 52 local_state_(local_state), |
| 115 profile_name_(profile_->GetPath().AsUTF8Unsafe()), | |
| 116 pref_hash_seed_(kSHA256DigestSize, 0), | |
| 117 device_id_(device_id), | |
| 118 tracked_pref_paths_(tracked_pref_paths), | |
| 119 tracked_pref_path_count_(tracked_pref_path_count), | |
| 120 checked_tracked_prefs_(false), | |
| 121 weak_factory_(this) { | 53 weak_factory_(this) { |
| 122 CheckTrackedPreferences(); | |
| 123 } | 54 } |
| 124 | 55 |
| 125 PrefMetricsService::~PrefMetricsService() { | 56 PrefMetricsService::~PrefMetricsService() { |
| 126 } | 57 } |
| 127 | 58 |
| 128 void PrefMetricsService::RecordLaunchPrefs() { | 59 void PrefMetricsService::RecordLaunchPrefs() { |
| 129 bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton); | 60 bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton); |
| 130 bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage); | 61 bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage); |
| 131 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); | 62 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); |
| 132 if (show_home_button) { | 63 if (show_home_button) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 if (start_url.is_valid()) { | 113 if (start_url.is_valid()) { |
| 183 UMA_HISTOGRAM_ENUMERATION( | 114 UMA_HISTOGRAM_ENUMERATION( |
| 184 "Settings.PinnedTabEngineTypes", | 115 "Settings.PinnedTabEngineTypes", |
| 185 TemplateURLPrepopulateData::GetEngineType(start_url), | 116 TemplateURLPrepopulateData::GetEngineType(start_url), |
| 186 SEARCH_ENGINE_MAX); | 117 SEARCH_ENGINE_MAX); |
| 187 } | 118 } |
| 188 } | 119 } |
| 189 #endif | 120 #endif |
| 190 } | 121 } |
| 191 | 122 |
| 192 // static | |
| 193 void PrefMetricsService::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 194 // Register the top level dictionary to map profile names to dictionaries of | |
| 195 // tracked preferences. | |
| 196 registry->RegisterDictionaryPref(prefs::kProfilePreferenceHashes); | |
| 197 } | |
| 198 | |
| 199 void PrefMetricsService::RegisterSyncedPrefObservers() { | 123 void PrefMetricsService::RegisterSyncedPrefObservers() { |
| 200 LogHistogramValueCallback booleanHandler = base::Bind( | 124 LogHistogramValueCallback booleanHandler = base::Bind( |
| 201 &PrefMetricsService::LogBooleanPrefChange, base::Unretained(this)); | 125 &PrefMetricsService::LogBooleanPrefChange, base::Unretained(this)); |
| 202 | 126 |
| 203 AddPrefObserver(prefs::kShowHomeButton, "ShowHomeButton", booleanHandler); | 127 AddPrefObserver(prefs::kShowHomeButton, "ShowHomeButton", booleanHandler); |
| 204 AddPrefObserver(prefs::kHomePageIsNewTabPage, "HomePageIsNewTabPage", | 128 AddPrefObserver(prefs::kHomePageIsNewTabPage, "HomePageIsNewTabPage", |
| 205 booleanHandler); | 129 booleanHandler); |
| 206 | 130 |
| 207 AddPrefObserver(prefs::kRestoreOnStartup, "StartupPageLoadSettings", | 131 AddPrefObserver(prefs::kRestoreOnStartup, "StartupPageLoadSettings", |
| 208 base::Bind(&PrefMetricsService::LogIntegerPrefChange, | 132 base::Bind(&PrefMetricsService::LogIntegerPrefChange, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 return; | 176 return; |
| 253 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( | 177 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( |
| 254 histogram_name, | 178 histogram_name, |
| 255 1, | 179 1, |
| 256 boundary_value, | 180 boundary_value, |
| 257 boundary_value + 1, | 181 boundary_value + 1, |
| 258 base::HistogramBase::kUmaTargetedHistogramFlag); | 182 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 259 histogram->Add(integer_value); | 183 histogram->Add(integer_value); |
| 260 } | 184 } |
| 261 | 185 |
| 262 void PrefMetricsService::GetDeviceIdCallback(const std::string& device_id) { | |
| 263 #if !defined(OS_WIN) || defined(ENABLE_RLZ) | |
| 264 // A device_id is expected in all scenarios except when RLZ is disabled on | |
| 265 // Windows. | |
| 266 DCHECK(!device_id.empty()); | |
| 267 #endif | |
| 268 | |
| 269 device_id_ = device_id; | |
| 270 CheckTrackedPreferences(); | |
| 271 } | |
| 272 | |
| 273 // To detect changes to Preferences that happen outside of Chrome, we hash | |
| 274 // selected pref values and save them in local state. CheckTrackedPreferences | |
| 275 // compares the saved values to the values observed in the profile's prefs. A | |
| 276 // dictionary of dictionaries in local state holds the hashed values, grouped by | |
| 277 // profile. To make the system more resistant to spoofing, pref values are | |
| 278 // hashed with the pref path and the device id. | |
| 279 void PrefMetricsService::CheckTrackedPreferences() { | |
| 280 // Make sure this is only called once per instance of this service. | |
| 281 DCHECK(!checked_tracked_prefs_); | |
| 282 checked_tracked_prefs_ = true; | |
| 283 | |
| 284 const base::DictionaryValue* pref_hash_dicts = | |
| 285 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); | |
| 286 // Get the hashed prefs dictionary if it exists. If it doesn't, it will be | |
| 287 // created if we set preference values below. | |
| 288 const base::DictionaryValue* hashed_prefs = NULL; | |
| 289 pref_hash_dicts->GetDictionaryWithoutPathExpansion(profile_name_, | |
| 290 &hashed_prefs); | |
| 291 for (int i = 0; i < tracked_pref_path_count_; ++i) { | |
| 292 if (!prefs_->FindPreference(tracked_pref_paths_[i])) { | |
| 293 // All tracked preferences need to have been registered already. | |
| 294 DCHECK_EQ(kUnregisteredPreference, tracked_pref_paths_[i]); | |
| 295 continue; | |
| 296 } | |
| 297 | |
| 298 const base::Value* value = prefs_->GetUserPrefValue(tracked_pref_paths_[i]); | |
| 299 std::string last_hash; | |
| 300 // First try to get the stored expected hash... | |
| 301 if (hashed_prefs && | |
| 302 hashed_prefs->GetString(tracked_pref_paths_[i], &last_hash)) { | |
| 303 // ... if we have one get the hash of the current value... | |
| 304 const std::string value_hash = | |
| 305 GetHashedPrefValue(tracked_pref_paths_[i], value, | |
| 306 HASHED_PREF_STYLE_NEW); | |
| 307 // ... and check that it matches... | |
| 308 if (value_hash == last_hash) { | |
| 309 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceUnchanged", | |
| 310 i, tracked_pref_path_count_); | |
| 311 } else { | |
| 312 // ... if it doesn't: was the value simply cleared? | |
| 313 if (!value) { | |
| 314 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceCleared", | |
| 315 i, tracked_pref_path_count_); | |
| 316 } else { | |
| 317 // ... or does it match the old style hash? | |
| 318 std::string old_style_hash = | |
| 319 GetHashedPrefValue(tracked_pref_paths_[i], value, | |
| 320 HASHED_PREF_STYLE_DEPRECATED); | |
| 321 if (old_style_hash == last_hash) { | |
| 322 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceMigrated", | |
| 323 i, tracked_pref_path_count_); | |
| 324 } else { | |
| 325 // ... or was it simply changed to something else? | |
| 326 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceChanged", | |
| 327 i, tracked_pref_path_count_); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 // ... either way update the expected hash to match the new value. | |
| 332 UpdateTrackedPreference(tracked_pref_paths_[i]); | |
| 333 } | |
| 334 } else { | |
| 335 // Record that we haven't tracked this preference yet, or the hash in | |
| 336 // local state was removed. | |
| 337 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceInitialized", | |
| 338 i, tracked_pref_path_count_); | |
| 339 UpdateTrackedPreference(tracked_pref_paths_[i]); | |
| 340 } | |
| 341 } | |
| 342 | |
| 343 // Now that we've checked the incoming preferences, register for change | |
| 344 // notifications, unless this is test code. | |
| 345 // TODO(bbudge) Fix failing browser_tests and so we can remove this test. | |
| 346 // Several tests fail when they shutdown before they can write local state. | |
| 347 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType) && | |
| 348 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { | |
| 349 InitializePrefObservers(); | |
| 350 } | |
| 351 } | |
| 352 | |
| 353 void PrefMetricsService::UpdateTrackedPreference(const char* path) { | |
| 354 const base::Value* value = prefs_->GetUserPrefValue(path); | |
| 355 DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); | |
| 356 DictionaryValue* child_dictionary = NULL; | |
| 357 | |
| 358 // Get the dictionary corresponding to the profile name, | |
| 359 // which may have a '.' | |
| 360 if (!update->GetDictionaryWithoutPathExpansion(profile_name_, | |
| 361 &child_dictionary)) { | |
| 362 child_dictionary = new DictionaryValue; | |
| 363 update->SetWithoutPathExpansion(profile_name_, child_dictionary); | |
| 364 } | |
| 365 | |
| 366 child_dictionary->SetString(path, | |
| 367 GetHashedPrefValue(path, value, | |
| 368 HASHED_PREF_STYLE_NEW)); | |
| 369 } | |
| 370 | |
| 371 std::string PrefMetricsService::GetHashedPrefValue( | |
| 372 const char* path, | |
| 373 const base::Value* value, | |
| 374 HashedPrefStyle desired_style) { | |
| 375 // Dictionary values may contain empty lists and sub-dictionaries. Make a | |
| 376 // deep copy with those removed to make the hash more stable. | |
| 377 const DictionaryValue* dict_value; | |
| 378 scoped_ptr<DictionaryValue> canonical_dict_value; | |
| 379 if (value && | |
| 380 value->GetAsDictionary(&dict_value)) { | |
| 381 canonical_dict_value.reset(dict_value->DeepCopyWithoutEmptyChildren()); | |
| 382 value = canonical_dict_value.get(); | |
| 383 } | |
| 384 | |
| 385 std::string value_as_string; | |
| 386 // If |value| is NULL, we will still build a unique hash based on |device_id_| | |
| 387 // and |path| below. | |
| 388 if (value) { | |
| 389 JSONStringValueSerializer serializer(&value_as_string); | |
| 390 serializer.Serialize(*value); | |
| 391 } | |
| 392 | |
| 393 std::string string_to_hash; | |
| 394 // TODO(gab): Remove this as the old style is phased out. | |
| 395 if (desired_style == HASHED_PREF_STYLE_NEW) { | |
| 396 string_to_hash.append(device_id_); | |
| 397 string_to_hash.append(path); | |
| 398 } | |
| 399 string_to_hash.append(value_as_string); | |
| 400 | |
| 401 crypto::HMAC hmac(crypto::HMAC::SHA256); | |
| 402 unsigned char digest[kSHA256DigestSize]; | |
| 403 if (!hmac.Init(pref_hash_seed_) || | |
| 404 !hmac.Sign(string_to_hash, digest, kSHA256DigestSize)) { | |
| 405 NOTREACHED(); | |
| 406 return std::string(); | |
| 407 } | |
| 408 | |
| 409 return base::HexEncode(digest, kSHA256DigestSize); | |
| 410 } | |
| 411 | |
| 412 void PrefMetricsService::InitializePrefObservers() { | |
| 413 pref_registrar_.Init(prefs_); | |
| 414 for (int i = 0; i < tracked_pref_path_count_; ++i) { | |
| 415 if (!prefs_->FindPreference(tracked_pref_paths_[i])) { | |
| 416 // All tracked preferences need to have been registered already. | |
| 417 DCHECK_EQ(kUnregisteredPreference, tracked_pref_paths_[i]); | |
| 418 continue; | |
| 419 } | |
| 420 | |
| 421 pref_registrar_.Add( | |
| 422 tracked_pref_paths_[i], | |
| 423 base::Bind(&PrefMetricsService::UpdateTrackedPreference, | |
| 424 weak_factory_.GetWeakPtr(), | |
| 425 tracked_pref_paths_[i])); | |
| 426 } | |
| 427 } | |
| 428 | |
| 429 // static | 186 // static |
| 430 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { | 187 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { |
| 431 return Singleton<PrefMetricsService::Factory>::get(); | 188 return Singleton<PrefMetricsService::Factory>::get(); |
| 432 } | 189 } |
| 433 | 190 |
| 434 // static | 191 // static |
| 435 PrefMetricsService* PrefMetricsService::Factory::GetForProfile( | 192 PrefMetricsService* PrefMetricsService::Factory::GetForProfile( |
| 436 Profile* profile) { | 193 Profile* profile) { |
| 437 return static_cast<PrefMetricsService*>( | 194 return static_cast<PrefMetricsService*>( |
| 438 GetInstance()->GetServiceForBrowserContext(profile, true)); | 195 GetInstance()->GetServiceForBrowserContext(profile, true)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 458 } | 215 } |
| 459 | 216 |
| 460 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { | 217 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { |
| 461 return false; | 218 return false; |
| 462 } | 219 } |
| 463 | 220 |
| 464 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( | 221 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( |
| 465 content::BrowserContext* context) const { | 222 content::BrowserContext* context) const { |
| 466 return chrome::GetBrowserContextRedirectedInIncognito(context); | 223 return chrome::GetBrowserContextRedirectedInIncognito(context); |
| 467 } | 224 } |
| OLD | NEW |