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..d27e96fd532b76480bec1440e3aa3b5ce230c201 100644 |
--- a/chrome/browser/prefs/pref_metrics_service.cc |
+++ b/chrome/browser/prefs/pref_metrics_service.cc |
@@ -5,9 +5,17 @@ |
#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" |
+#include "chrome/browser/extensions/api/music_manager_private/device_id.h" |
Mattias Nissler (ping if slow)
2013/08/15 10:14:51
I'd consider this a layering violation. chrome/bro
bbudge
2013/08/15 17:33:49
This appears to be the only way to get a unique pe
Mattias Nissler (ping if slow)
2013/08/20 12:53:54
That'd be appreciated. Can you file a bug and put
bbudge
2013/08/20 18:17:50
Done.
|
+#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,21 +26,68 @@ |
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; |
+// In order to detect changes to Preferences that happen outside of Chrome, we |
+// save selected prefs in Local Settings. When Chrome is started up, we compare |
Mattias Nissler (ping if slow)
2013/08/15 10:14:51
local state, here and below
bbudge
2013/08/15 17:33:49
Done.
|
+// the saved values against the values observed in the user's Preferences file. |
+// We add a dictionary of dictionaries to Local Settings to hold the saved |
+// values, grouped by profile. To make the system faster and more resistant to |
Mattias Nissler (ping if slow)
2013/08/15 10:14:51
I don't think hashing makes things faster, given t
bbudge
2013/08/15 17:33:49
I was thinking of I/O and comparing values, but I
|
+// spoofing, each preference value is hashed with its path and the device id. |
+const char kProfilePreferenceHashes[] = "profile.preference_hashes"; |
+ |
+// 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, |
Mattias Nissler (ping if slow)
2013/08/15 10:14:51
FWIW, this should be defined in pref_names.{h,cc}
bbudge
2013/08/15 17:33:49
Perhaps a separate CL.
|
+}; |
+ |
+const int kTrackedPrefsCount = arraysize(kTrackedPrefs); |
+ |
+bool BrowserShuttingDown() { |
+ return browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID; |
+} |
+ |
} // namespace |
PrefMetricsService::PrefMetricsService(Profile* profile) |
- : profile_(profile) { |
+ : weak_factory_(this), |
+ profile_(profile) { |
RecordLaunchPrefs(); |
+ // Make sure local state is initialized. |
+ g_browser_process->local_state(); |
Mattias Nissler (ping if slow)
2013/08/15 10:14:51
This shouldn't be required, local state gets initi
bbudge
2013/08/15 17:33:49
I thought it might fix some tests that are crashin
|
+ |
+#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) |
+ |
+ // 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 Settings. |
+ pref_registrar_.Init(profile_->GetPrefs()); |
+ for (size_t i = 0; i < arraysize(kTrackedPrefs); ++i) { |
+ pref_registrar_.Add( |
+ kTrackedPrefs[i], |
+ base::Bind(&PrefMetricsService::UpdateTrackedPreference, |
+ weak_factory_.GetWeakPtr(), |
+ kTrackedPrefs[i])); |
+ } |
+ |
PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs)); |
@@ -62,6 +117,13 @@ void PrefMetricsService::RecordLaunchPrefs() { |
} |
} |
+// static |
+void PrefMetricsService::RegisterPrefs(PrefRegistrySimple* registry) { |
+ // Register the top level dictionary to map profile names to dictionaries of |
+ // tracked preferences. |
+ registry->RegisterDictionaryPref(kProfilePreferenceHashes); |
+} |
+ |
void PrefMetricsService::RegisterSyncedPrefObservers() { |
LogHistogramValueCallback booleanHandler = base::Bind( |
&PrefMetricsService::LogBooleanPrefChange, base::Unretained(this)); |
@@ -139,6 +201,112 @@ void PrefMetricsService::LogListPrefChange( |
} |
} |
+void PrefMetricsService::GetDeviceIdCallback(const std::string& device_id) { |
+ device_id_ = device_id; |
+ CheckTrackedPreferences(); |
+} |
+ |
+void PrefMetricsService::CheckTrackedPreferences() { |
+ PrefService* local_state = g_browser_process->local_state(); |
+ const base::DictionaryValue* profiles = |
+ local_state->GetDictionary(kProfilePreferenceHashes); |
+ DCHECK(profiles); |
+ // 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; |
+ profiles->GetDictionary(profile_->GetProfileName(), &hashed_prefs); |
+ const PrefService* pref_service = profile_->GetPrefs(); |
+ for (size_t i = 0; i < arraysize(kTrackedPrefs); ++i) { |
+ const base::Value* value = |
+ pref_service->GetUserPrefValue(kTrackedPrefs[i]); |
+ if (value) { |
+ std::string value_hash = GetHashedPrefValue(kTrackedPrefs[i], value); |
+ std::string last_hash; |
+ if (hashed_prefs && |
+ hashed_prefs->GetString(kTrackedPrefs[i], &last_hash)) { |
+ if (value_hash != last_hash) { |
+ // Record that the preference changed from its last value. |
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceChanged", |
+ i, arraysize(kTrackedPrefs)); |
+ UpdateTrackedPreference(kTrackedPrefs[i]); |
+ } |
+ } else { |
+ // Record that we haven't tracked this preference yet, or the hash in |
+ // local settings was removed. |
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceInitialized", |
+ i, arraysize(kTrackedPrefs)); |
+ UpdateTrackedPreference(kTrackedPrefs[i]); |
+ } |
+ } else { |
+ // There is no preference set. Remove any hashed value from local settings |
+ // and if one was present, record that a pref was removed. |
+ if (RemoveTrackedPreference(kTrackedPrefs[i])) { |
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceRemoved", |
+ i, arraysize(kTrackedPrefs)); |
+ } |
+ } |
+ } |
+} |
+ |
+void PrefMetricsService::UpdateTrackedPreference(const char* path) { |
+ // Don't try to change local settings during browser shutdown. |
+ if (BrowserShuttingDown()) |
+ return; |
+ |
+ PrefService* pref_service = profile_->GetPrefs(); |
+ const base::Value* value = pref_service->GetUserPrefValue(path); |
+ if (value) { |
+ PrefService* local_state = g_browser_process->local_state(); |
+ DictionaryPrefUpdate update(local_state, 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 settings during browser shutdown. |
+ if (BrowserShuttingDown()) |
+ return false; |
+ |
+ PrefService* local_state = g_browser_process->local_state(); |
+ DictionaryPrefUpdate update(local_state, 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_->GetProfileName()); |
+ 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); |
+} |
+ |
// static |
PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { |
return Singleton<PrefMetricsService::Factory>::get(); |