Index: chrome/browser/prefs/pref_metrics_service.cc |
diff --git a/chrome/browser/prefs/pref_metrics_service.cc b/chrome/browser/prefs/pref_metrics_service.cc |
index a437c8c6db9b1bbd69f797558fe156d6d62959bc..31e43f2b532ad560513cd225650cdf6c39ea62aa 100644 |
--- a/chrome/browser/prefs/pref_metrics_service.cc |
+++ b/chrome/browser/prefs/pref_metrics_service.cc |
@@ -5,9 +5,19 @@ |
#include "chrome/browser/prefs/pref_metrics_service.h" |
#include "base/bind.h" |
+#include "base/json/json_string_value_serializer.h" |
+#include "base/md5.h" |
#include "base/metrics/histogram.h" |
+#include "base/prefs/pref_registry_simple.h" |
#include "base/prefs/pref_service.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/browser_shutdown.h" |
+// Accessing the Device ID API here is a layering violation. |
+// TODO(bbudge) Move the API so it's usable here. See crbug.com/276485. |
+#include "chrome/browser/extensions/api/music_manager_private/device_id.h" |
+#include "chrome/browser/extensions/extension_prefs.h" |
#include "chrome/browser/prefs/pref_service_syncable.h" |
+#include "chrome/browser/prefs/scoped_user_pref_update.h" |
#include "chrome/browser/prefs/session_startup_pref.h" |
#include "chrome/browser/prefs/synced_pref_change_registrar.h" |
#include "chrome/browser/profiles/incognito_helpers.h" |
@@ -18,50 +28,105 @@ |
namespace { |
-// Converts a host name into a domain name for easier matching. |
-std::string GetDomainFromHost(const std::string& host) { |
- return net::registry_controlled_domains::GetDomainAndRegistry( |
- host, |
- net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
-} |
- |
const int kSessionStartupPrefValueMax = SessionStartupPref::kPrefValueMax; |
+// These preferences must be kept in sync with the TrackedPreference enum in |
+// tools/metrics/histograms/histograms.xml. To add a new preference, append it |
+// to the array and add a corresponding value to the histogram enum. |
+const char* kTrackedPrefs[] = { |
+ prefs::kShowHomeButton, |
+ prefs::kHomePageIsNewTabPage, |
+ prefs::kHomePage, |
+ prefs::kRestoreOnStartup, |
+ prefs::kURLsToRestoreOnStartup, |
+ extensions::ExtensionPrefs::kExtensionsPref, |
+}; |
+ |
+bool BrowserShuttingDown() { |
+ return browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID; |
+} |
+ |
} // namespace |
PrefMetricsService::PrefMetricsService(Profile* profile) |
- : profile_(profile) { |
+ : weak_factory_(this), |
+ profile_(profile), |
+ prefs_(profile_->GetPrefs()), |
+ local_state_(g_browser_process->local_state()), |
+ tracked_pref_paths_(kTrackedPrefs), |
+ tracked_pref_path_count_(arraysize(kTrackedPrefs)) { |
+ // Get the profile name (some browser tests don't set this up, in which |
+ // case profile_name_ is the empty string). |
+ if (prefs_->FindPreference(prefs::kGoogleServicesUsername)) |
+ profile_name_ = profile_->GetProfileName(); |
+ |
RecordLaunchPrefs(); |
+#if !defined(OS_ANDROID) |
+ // We need the machine id to compute pref value hashes. Fetch that, and then |
+ // call CheckTrackedPreferences in the callback. |
+ extensions::api::DeviceId::GetDeviceId( |
+ "PrefMetricsService", // non-empty string to obfuscate the device id. |
+ Bind(&PrefMetricsService::GetDeviceIdCallback, |
+ weak_factory_.GetWeakPtr())); |
+#else |
+ // Android has no GetDeviceId. |
+ CheckTrackedPreferences(); |
+#endif // !defined(OS_ANDROID) |
+ |
+ InitializePrefObservers(); |
+ |
PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs)); |
RegisterSyncedPrefObservers(); |
} |
+PrefMetricsService::PrefMetricsService(Profile* profile, |
+ PrefService* local_state, |
+ const std::string& device_id, |
+ const char** tracked_pref_paths, |
+ int tracked_pref_path_count) |
+ : weak_factory_(this), |
+ profile_(profile), |
+ prefs_(profile->GetPrefs()), |
+ local_state_(local_state), |
+ device_id_(device_id), |
+ tracked_pref_paths_(tracked_pref_paths), |
+ tracked_pref_path_count_(tracked_pref_path_count) { |
+ CheckTrackedPreferences(); |
+ InitializePrefObservers(); |
+} |
+ |
PrefMetricsService::~PrefMetricsService() { |
} |
void PrefMetricsService::RecordLaunchPrefs() { |
- PrefService* prefs = profile_->GetPrefs(); |
- bool show_home_button = prefs->GetBoolean(prefs::kShowHomeButton); |
- bool home_page_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage); |
+ bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton); |
+ bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage); |
UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); |
if (show_home_button) { |
UMA_HISTOGRAM_BOOLEAN("Settings.GivenShowHomeButton_HomePageIsNewTabPage", |
home_page_is_ntp); |
} |
- int restore_on_startup = prefs->GetInteger(prefs::kRestoreOnStartup); |
+ int restore_on_startup = prefs_->GetInteger(prefs::kRestoreOnStartup); |
UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings", |
restore_on_startup, kSessionStartupPrefValueMax); |
if (restore_on_startup == SessionStartupPref::kPrefValueURLs) { |
- const int url_list_size = prefs->GetList( |
+ const int url_list_size = prefs_->GetList( |
prefs::kURLsToRestoreOnStartup)->GetSize(); |
UMA_HISTOGRAM_CUSTOM_COUNTS( |
"Settings.StartupPageLoadURLs", url_list_size, 1, 50, 20); |
} |
} |
+// static |
+void PrefMetricsService::RegisterPrefs(PrefRegistrySimple* registry) { |
+ // Register the top level dictionary to map profile names to dictionaries of |
+ // tracked preferences. |
+ registry->RegisterDictionaryPref(prefs::kProfilePreferenceHashes); |
+} |
+ |
void PrefMetricsService::RegisterSyncedPrefObservers() { |
LogHistogramValueCallback booleanHandler = base::Bind( |
&PrefMetricsService::LogBooleanPrefChange, base::Unretained(this)); |
@@ -139,6 +204,129 @@ void PrefMetricsService::LogListPrefChange( |
} |
} |
+void PrefMetricsService::GetDeviceIdCallback(const std::string& device_id) { |
+ device_id_ = device_id; |
+ CheckTrackedPreferences(); |
+} |
+ |
+// In order to detect changes to Preferences that happen outside of Chrome, we |
+// save selected pref values in local state. CheckTrackedPreferences compares |
+// the saved values to the values observed in the profile's prefs. A dictionary |
+// of dictionaries in local state holds the saved values, grouped by profile. |
+// To make the system more resistant to spoofing, pref values are hashed with |
+// the pref path and the device id. |
+void PrefMetricsService::CheckTrackedPreferences() { |
+ UMA_HISTOGRAM_BOOLEAN("Settings.TrackedPreferencesChecked", true); |
+ |
+ const base::DictionaryValue* pref_hash_dicts = |
+ local_state_->GetDictionary(prefs::kProfilePreferenceHashes); |
+ |
+ // Get the hashed prefs dictionary if it exists. If it doesn't, it will be |
+ // created if we set preference values below. |
+ const base::DictionaryValue* hashed_prefs = NULL; |
+ pref_hash_dicts->GetDictionary(profile_name_, &hashed_prefs); |
+ for (int i = 0; i < tracked_pref_path_count_; ++i) { |
+ const base::Value* value = prefs_->GetUserPrefValue(tracked_pref_paths_[i]); |
+ if (value) { |
+ std::string value_hash = |
+ GetHashedPrefValue(tracked_pref_paths_[i], value); |
+ std::string last_hash; |
+ if (hashed_prefs && |
+ hashed_prefs->GetString(tracked_pref_paths_[i], &last_hash)) { |
+ if (value_hash != last_hash) { |
+ // Record that the preference changed from its last value. |
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceChanged", |
+ i, tracked_pref_path_count_); |
+ UpdateTrackedPreference(tracked_pref_paths_[i]); |
+ } |
+ } else { |
+ // Record that we haven't tracked this preference yet, or the hash in |
+ // local state was removed. |
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceInitialized", |
+ i, tracked_pref_path_count_); |
+ UpdateTrackedPreference(tracked_pref_paths_[i]); |
+ } |
+ } else { |
+ // There is no preference set. Remove any hashed value from local state |
+ // and if one was present, record that a pref was removed. |
+ if (RemoveTrackedPreference(tracked_pref_paths_[i])) { |
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceRemoved", |
+ i, tracked_pref_path_count_); |
+ } |
+ } |
+ } |
+} |
+ |
+void PrefMetricsService::UpdateTrackedPreference(const char* path) { |
+ // Don't try to change local state during browser shutdown. |
+ if (BrowserShuttingDown()) |
+ return; |
+ |
+ const base::Value* value = prefs_->GetUserPrefValue(path); |
+ if (value) { |
+ DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); |
+ base::DictionaryValue* prefs = update.Get(); |
+ if (prefs) { |
+ prefs->SetString(GetHashedPrefPath(path), |
+ GetHashedPrefValue(path, value)); |
+ } |
+ } else { |
+ RemoveTrackedPreference(path); |
+ } |
+} |
+ |
+bool PrefMetricsService::RemoveTrackedPreference(const char* path) { |
+ // Don't try to change local state during browser shutdown. |
+ if (BrowserShuttingDown()) |
+ return false; |
+ |
+ DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); |
+ base::DictionaryValue* prefs = update.Get(); |
+ if (prefs) |
+ return prefs->Remove(GetHashedPrefPath(path), NULL); |
+ |
+ return false; |
+} |
+ |
+std::string PrefMetricsService::GetHashedPrefPath(const char* path) { |
+ std::string hash_pref_path(profile_name_); |
+ hash_pref_path.append("."); |
+ hash_pref_path.append(path); |
+ return hash_pref_path; |
+} |
+ |
+std::string PrefMetricsService::GetHashedPrefValue( |
+ const char* path, |
+ const base::Value* value) { |
+ DCHECK(value); |
+ std::string value_string; |
+ JSONStringValueSerializer serializer(&value_string); |
+ serializer.Serialize(*value); |
+ |
+ base::MD5Context context; |
+ base::MD5Init(&context); |
+ base::MD5Update(&context, device_id_); // Salt with device id. |
+ base::MD5Update(&context, path); // Salt with pref path. |
+ base::MD5Update(&context, value_string); |
+ base::MD5Digest digest; |
+ base::MD5Final(&digest, &context); |
+ return base::MD5DigestToBase16(digest); |
+} |
+ |
+void PrefMetricsService::InitializePrefObservers() { |
+ // It's tricky to save preference value hashes on browser shutdown. Instead, |
+ // register for notifications when tracked preferences change so we can update |
+ // the hashes in local state. |
+ pref_registrar_.Init(prefs_); |
+ for (int i = 0; i < tracked_pref_path_count_; ++i) { |
+ pref_registrar_.Add( |
+ tracked_pref_paths_[i], |
+ base::Bind(&PrefMetricsService::UpdateTrackedPreference, |
+ weak_factory_.GetWeakPtr(), |
+ tracked_pref_paths_[i])); |
+ } |
+} |
+ |
// static |
PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { |
return Singleton<PrefMetricsService::Factory>::get(); |