Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1743)

Unified Diff: chrome/browser/prefs/pref_hash_filter.cc

Issue 90563003: Fix a race condition in preference metric reporting. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to Gab. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/prefs/pref_hash_filter.cc
diff --git a/chrome/browser/prefs/pref_hash_filter.cc b/chrome/browser/prefs/pref_hash_filter.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8056bf468691b80e5daab067fa5a8ac17cb4fb31
--- /dev/null
+++ b/chrome/browser/prefs/pref_hash_filter.cc
@@ -0,0 +1,100 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/prefs/pref_hash_filter.h"
+
+#include "base/logging.h"
+#include "base/metrics/histogram.h"
+#include "base/prefs/persistent_pref_store.h"
+#include "base/values.h"
+#include "chrome/browser/prefs/pref_hash_store.h"
+#include "chrome/common/pref_names.h"
+
+namespace {
+
+// 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. Replace
+// removed preferences with "".
+const char* kTrackedPrefs[] = {
+ prefs::kShowHomeButton,
+ prefs::kHomePageIsNewTabPage,
+ prefs::kHomePage,
+ prefs::kRestoreOnStartup,
+ prefs::kURLsToRestoreOnStartup,
+ prefs::kExtensionsPref,
+ prefs::kGoogleServicesLastUsername,
+ prefs::kSearchProviderOverrides,
+ prefs::kDefaultSearchProviderSearchURL,
+ prefs::kDefaultSearchProviderKeyword,
+ prefs::kDefaultSearchProviderName,
+#if !defined(OS_ANDROID)
+ prefs::kPinnedTabs,
+#else
+ "",
+#endif
+ prefs::kExtensionKnownDisabled,
+};
+
+void ReportValidationResult(PrefHashStore::ValueState value_state,
+ size_t value_index) {
+ switch (value_state) {
+ case PrefHashStore::UNCHANGED:
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceUnchanged",
+ value_index, arraysize(kTrackedPrefs));
+ return;
+ case PrefHashStore::CLEARED:
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceCleared",
+ value_index, arraysize(kTrackedPrefs));
+ return;
+ case PrefHashStore::MIGRATED:
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceMigrated",
+ value_index, arraysize(kTrackedPrefs));
+ return;
+ case PrefHashStore::CHANGED:
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceChanged",
+ value_index, arraysize(kTrackedPrefs));
+ return;
+ case PrefHashStore::UNKNOWN_VALUE:
+ UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceInitialized",
+ value_index, arraysize(kTrackedPrefs));
+ return;
+ }
+ NOTREACHED() << "Unexpected PrefHashStore::ValueState: " << value_state;
+}
+
+} // namespace
+
+PrefHashFilter::PrefHashFilter(scoped_ptr<PrefHashStore> pref_hash_store)
+ : pref_hash_store_(pref_hash_store.Pass()),
+ tracked_paths_(kTrackedPrefs, kTrackedPrefs + arraysize(kTrackedPrefs)) {}
+
+PrefHashFilter::~PrefHashFilter() {}
+
+// Updates the stored hash to correspond to the updated preference value.
+void PrefHashFilter::FilterUpdate(PersistentPrefStore* pref_store,
+ const std::string& path) {
+ if (tracked_paths_.find(path) == tracked_paths_.end())
+ return;
+
+ const base::Value* value = NULL;
+ pref_store->GetValue(path, &value);
+ pref_hash_store_->StoreValue(path, value);
+}
+
+// Validates loaded preference values according to stored hashes, reports
+// validation results via UMA, and updates hashes in case of mismatch.
+void PrefHashFilter::FilterOnLoad(PersistentPrefStore* pref_store) {
+ for (size_t i = 0; i < arraysize(kTrackedPrefs); ++i) {
+ if (kTrackedPrefs[i][0] == '\0')
gab 2013/12/09 20:30:15 This feels hacky... why not just loop over the set
erikwright (departed) 2013/12/10 16:06:19 That would involve constructing std::string unnece
gab 2013/12/10 20:00:31 It wouldn't involve building a string; you would s
erikwright (departed) 2013/12/12 15:53:30 I can't iterate over the set; I need the index for
gab 2013/12/12 17:20:58 Right, I just realized that while working with thi
+ continue;
+ const base::Value* value = NULL;
+ pref_store->GetValue(kTrackedPrefs[i], &value);
+ PrefHashStore::ValueState value_state =
+ pref_hash_store_->CheckValue(kTrackedPrefs[i], value);
+ ReportValidationResult(value_state, i);
+ if (value_state != PrefHashStore::UNCHANGED)
+ pref_hash_store_->StoreValue(kTrackedPrefs[i], value);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698