Commit 2934006b authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

Introduce a new enum ModelTypeForHistograms

This is a variant of ModelType that fulfills the requirements for
recording in histograms (i.e. no reordering/renumbering). It's a
type-safe replacement for the current system based on ints.
The actual values stay identical to the current ints, so the histograms
themselves won't be affected.

A follow-up CL will update all histogram recording sites to use the new
enum.

Bug: 1007293
Change-Id: I3e36c97303ba08e34baccffafbab8b55d8b9288b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1821787Reviewed-by: default avatarJan Krcal <jkrcal@chromium.org>
Commit-Queue: Marc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699663}
parent 2dd0e9b3
This diff is collapsed.
...@@ -176,6 +176,64 @@ inline ModelType ModelTypeFromInt(int i) { ...@@ -176,6 +176,64 @@ inline ModelType ModelTypeFromInt(int i) {
return static_cast<ModelType>(i); return static_cast<ModelType>(i);
} }
// A version of the ModelType enum for use in histograms. ModelType does not
// have stable values (e.g. new ones may be inserted in the middle), so it can't
// be recorded directly.
// Instead of using entries from this enum directly, you'll usually want to get
// them via ModelTypeHistogramValue(model_type).
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused. When you add a new entry, also update
// SyncModelTypes in enums.xml
enum class ModelTypeForHistograms {
kUnspecified = 0,
kTopLevelFolder = 1,
kBookmarks = 2,
kPreferences = 3,
kPasswords = 4,
kAutofillProfile = 5,
kAutofill = 6,
kThemes = 7,
kTypedUrls = 8,
kExtensions = 9,
kSearchEngines = 10,
kSessions = 11,
kApps = 12,
kAppSettings = 13,
kExtensionSettings = 14,
kDeprecatedAppNotifications = 15,
kHistoryDeleteDirectices = 16,
kNigori = 17,
kDeviceInfo = 18,
kDeprecatedExperiments = 19,
kDeprecatedSyncedNotifications = 20,
kPriorityPreferences = 21,
kDictionary = 22,
kFaviconImages = 23,
kFaviconTracking = 24,
kProxyTabs = 25,
kSupervisedUserSettings = 26,
kDeprecatedSupervisedUsers = 27,
kDeprecatedArticles = 28,
kAppList = 29,
kDeprecatedSupervisedUserSharedSettings = 30,
kDeprecatedSyncedNotificationAppInfo = 31,
kDeprecatedWifiCredentials = 32,
kSupervisedUserWhitelists = 33,
kAutofillWalletData = 34,
kAutofillWalletMetadata = 35,
kArcPackage = 36,
kPrinters = 37,
kReadingList = 38,
kUserEvents = 39,
kMountainShares = 40,
kUserConsents = 41,
kSendTabToSelf = 42,
kSecurityEvents = 43,
kWifiConfigurations = 44,
kWebApps = 45,
kMaxValue = kWebApps
};
// Used to mark the type of EntitySpecifics that has no actual data. // Used to mark the type of EntitySpecifics that has no actual data.
void AddDefaultFieldValue(ModelType type, sync_pb::EntitySpecifics* specifics); void AddDefaultFieldValue(ModelType type, sync_pb::EntitySpecifics* specifics);
...@@ -308,7 +366,10 @@ const char* ModelTypeToHistogramSuffix(ModelType model_type); ...@@ -308,7 +366,10 @@ const char* ModelTypeToHistogramSuffix(ModelType model_type);
// The mapping from ModelType to integer is defined here. It defines a // The mapping from ModelType to integer is defined here. It defines a
// completely different order than the ModelType enum itself. The mapping should // completely different order than the ModelType enum itself. The mapping should
// match the SyncModelTypes mapping from integer to labels defined in enums.xml. // match the SyncModelTypes mapping from integer to labels defined in enums.xml.
// TODO(crbug.com/1007293): Update all histogram recording sites to use
// ModelTypeHistogramValue() and remove ModelTypeToHistogramInt();
int ModelTypeToHistogramInt(ModelType model_type); int ModelTypeToHistogramInt(ModelType model_type);
ModelTypeForHistograms ModelTypeHistogramValue(ModelType model_type);
// Returns for every model_type a positive unique integer that is stable over // Returns for every model_type a positive unique integer that is stable over
// time and thus can be used when persisting data. // time and thus can be used when persisting data.
......
...@@ -73,20 +73,17 @@ TEST_F(ModelTypeTest, ModelTypeOfInvalidSpecificsFieldNumber) { ...@@ -73,20 +73,17 @@ TEST_F(ModelTypeTest, ModelTypeOfInvalidSpecificsFieldNumber) {
} }
TEST_F(ModelTypeTest, ModelTypeHistogramMapping) { TEST_F(ModelTypeTest, ModelTypeHistogramMapping) {
std::set<int> histogram_values; std::set<ModelTypeForHistograms> histogram_values;
ModelTypeSet all_types = ModelTypeSet::All(); ModelTypeSet all_types = ModelTypeSet::All();
for (ModelType type : all_types) { for (ModelType type : all_types) {
SCOPED_TRACE(ModelTypeToString(type)); SCOPED_TRACE(ModelTypeToString(type));
int histogram_value = ModelTypeToHistogramInt(type); ModelTypeForHistograms histogram_value = ModelTypeHistogramValue(type);
EXPECT_TRUE(histogram_values.insert(histogram_value).second) EXPECT_TRUE(histogram_values.insert(histogram_value).second)
<< "Expected histogram values to be unique"; << "Expected histogram values to be unique";
// This is not necessary for the mapping to be valid, but most instances of EXPECT_LE(static_cast<int>(histogram_value),
// UMA_HISTOGRAM that use this mapping specify ModelType::NUM_ENTRIES as the static_cast<int>(ModelTypeForHistograms::kMaxValue));
// maximum possible value. If you break this assumption, you should update
// those histograms.
EXPECT_LT(histogram_value, ModelType::NUM_ENTRIES);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment