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

Side by Side Diff: components/content_settings/core/common/content_settings.cc

Issue 2728303002: Make it harder to get ContentSettingsType histogram values wrong (Closed)
Patch Set: Make it harder to get ContentSettingsType histogram values wrong Created 3 years, 8 months 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 unified diff | Download patch
« no previous file with comments | « no previous file | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "components/content_settings/core/common/content_settings.h" 5 #include "components/content_settings/core/common/content_settings.h"
6 6
7 #include "base/containers/hash_tables.h" 7 #include "base/containers/hash_tables.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "build/build_config.h" 11 #include "build/build_config.h"
12 12
13 ContentSetting IntToContentSetting(int content_setting) { 13 ContentSetting IntToContentSetting(int content_setting) {
14 return ((content_setting < 0) || 14 return ((content_setting < 0) ||
15 (content_setting >= CONTENT_SETTING_NUM_SETTINGS)) ? 15 (content_setting >= CONTENT_SETTING_NUM_SETTINGS)) ?
16 CONTENT_SETTING_DEFAULT : static_cast<ContentSetting>(content_setting); 16 CONTENT_SETTING_DEFAULT : static_cast<ContentSetting>(content_setting);
17 } 17 }
18 18
19 // WARNING: This array should not be reordered or removed as it is used for 19 struct HistogramValue {
20 // histogram values. If a ContentSettingsType value has been removed, the entry 20 ContentSettingsType type;
21 // must be replaced by a placeholder. It should correspond directly to the 21 int value;
22 // ContentType enum in histograms.xml. 22 };
23
24 // WARNING: The value specified here for a type should match exactly the value
25 // specified in the ContentType enum in histograms.xml. Since these values are
26 // used for histograms, please do not reuse the same value for a different
27 // content setting. Always append to the end and increment.
23 // TODO(raymes): We should use a sparse histogram here on the hash of the 28 // TODO(raymes): We should use a sparse histogram here on the hash of the
24 // content settings type name instead. 29 // content settings type name instead.
25 ContentSettingsType kHistogramOrder[] = { 30 HistogramValue kHistogramValue[] = {
26 CONTENT_SETTINGS_TYPE_COOKIES, 31 {CONTENT_SETTINGS_TYPE_COOKIES, 0},
27 CONTENT_SETTINGS_TYPE_IMAGES, 32 {CONTENT_SETTINGS_TYPE_IMAGES, 1},
28 CONTENT_SETTINGS_TYPE_JAVASCRIPT, 33 {CONTENT_SETTINGS_TYPE_JAVASCRIPT, 2},
29 CONTENT_SETTINGS_TYPE_PLUGINS, 34 {CONTENT_SETTINGS_TYPE_PLUGINS, 3},
30 CONTENT_SETTINGS_TYPE_POPUPS, 35 {CONTENT_SETTINGS_TYPE_POPUPS, 4},
31 CONTENT_SETTINGS_TYPE_GEOLOCATION, 36 {CONTENT_SETTINGS_TYPE_GEOLOCATION, 5},
32 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 37 {CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 6},
33 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, 38 {CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, 7},
34 CONTENT_SETTINGS_TYPE_DEFAULT, // FULLSCREEN (removed). 39 {CONTENT_SETTINGS_TYPE_MIXEDSCRIPT, 10},
35 CONTENT_SETTINGS_TYPE_DEFAULT, // MOUSELOCK (removed). 40 {CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, 12},
36 CONTENT_SETTINGS_TYPE_MIXEDSCRIPT, 41 {CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, 13},
37 CONTENT_SETTINGS_TYPE_DEFAULT, // MEDIASTREAM (removed). 42 {CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS, 14},
38 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, 43 {CONTENT_SETTINGS_TYPE_PPAPI_BROKER, 15},
39 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, 44 {CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, 16},
40 CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS, 45 {CONTENT_SETTINGS_TYPE_MIDI_SYSEX, 17},
41 CONTENT_SETTINGS_TYPE_PPAPI_BROKER, 46 {CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS, 19},
42 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS,
43 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
44 CONTENT_SETTINGS_TYPE_DEFAULT, // PUSH_MESSAGING (removed).
45 CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS,
46 CONTENT_SETTINGS_TYPE_DEFAULT, // METRO_SWITCH_TO_DESKTOP (removed).
47 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) 47 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
48 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, 48 {CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, 21},
49 #else
50 CONTENT_SETTINGS_TYPE_DEFAULT, // PROTECTED_MEDIA_IDENTIFIER (mobile only).
51 #endif 49 #endif
52 CONTENT_SETTINGS_TYPE_APP_BANNER, 50 {CONTENT_SETTINGS_TYPE_APP_BANNER, 22},
53 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, 51 {CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, 23},
54 CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, 52 {CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, 24},
55 CONTENT_SETTINGS_TYPE_DEFAULT, // KEYGEN (removed). 53 {CONTENT_SETTINGS_TYPE_BLUETOOTH_GUARD, 26},
56 CONTENT_SETTINGS_TYPE_BLUETOOTH_GUARD, 54 {CONTENT_SETTINGS_TYPE_BACKGROUND_SYNC, 27},
57 CONTENT_SETTINGS_TYPE_BACKGROUND_SYNC, 55 {CONTENT_SETTINGS_TYPE_AUTOPLAY, 28},
58 CONTENT_SETTINGS_TYPE_AUTOPLAY, 56 {CONTENT_SETTINGS_TYPE_IMPORTANT_SITE_INFO, 30},
59 CONTENT_SETTINGS_TYPE_DEFAULT, // PROMPT_NO_DECISION_COUNT (migrated). 57 {CONTENT_SETTINGS_TYPE_PERMISSION_AUTOBLOCKER_DATA, 31},
60 CONTENT_SETTINGS_TYPE_IMPORTANT_SITE_INFO, 58 {CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, 32},
61 CONTENT_SETTINGS_TYPE_PERMISSION_AUTOBLOCKER_DATA,
62 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER,
63 }; 59 };
64 60
65 int ContentSettingTypeToHistogramValue(ContentSettingsType content_setting, 61 int ContentSettingTypeToHistogramValue(ContentSettingsType content_setting,
66 size_t* num_values) { 62 size_t* num_values) {
67 // Translate the list above into a map for fast lookup. 63 // Translate the list above into a map for fast lookup.
68 typedef base::hash_map<int, int> Map; 64 typedef base::hash_map<int, int> Map;
69 CR_DEFINE_STATIC_LOCAL(Map, kMap, ()); 65 CR_DEFINE_STATIC_LOCAL(Map, kMap, ());
70 if (kMap.empty()) { 66 if (kMap.empty()) {
71 for (size_t i = 0; i < arraysize(kHistogramOrder); ++i) { 67 for (const HistogramValue& histogram_value : kHistogramValue)
72 if (kHistogramOrder[i] != CONTENT_SETTINGS_TYPE_DEFAULT) 68 kMap[histogram_value.type] = histogram_value.value;
73 kMap[kHistogramOrder[i]] = static_cast<int>(i);
74 }
75 } 69 }
76 70
77 DCHECK(base::ContainsKey(kMap, content_setting)); 71 DCHECK(base::ContainsKey(kMap, content_setting));
78 *num_values = arraysize(kHistogramOrder); 72 *num_values = arraysize(kHistogramValue);
79 return kMap[content_setting]; 73 return kMap[content_setting];
80 } 74 }
81 75
82 ContentSettingPatternSource::ContentSettingPatternSource( 76 ContentSettingPatternSource::ContentSettingPatternSource(
83 const ContentSettingsPattern& primary_pattern, 77 const ContentSettingsPattern& primary_pattern,
84 const ContentSettingsPattern& secondary_pattern, 78 const ContentSettingsPattern& secondary_pattern,
85 ContentSetting setting, 79 ContentSetting setting,
86 const std::string& source, 80 const std::string& source,
87 bool incognito) 81 bool incognito)
88 : primary_pattern(primary_pattern), 82 : primary_pattern(primary_pattern),
89 secondary_pattern(secondary_pattern), 83 secondary_pattern(secondary_pattern),
90 setting(setting), 84 setting(setting),
91 source(source), 85 source(source),
92 incognito(incognito) {} 86 incognito(incognito) {}
93 87
94 ContentSettingPatternSource::ContentSettingPatternSource() 88 ContentSettingPatternSource::ContentSettingPatternSource()
95 : setting(CONTENT_SETTING_DEFAULT), incognito(false) { 89 : setting(CONTENT_SETTING_DEFAULT), incognito(false) {
96 } 90 }
97 91
98 ContentSettingPatternSource::ContentSettingPatternSource( 92 ContentSettingPatternSource::ContentSettingPatternSource(
99 const ContentSettingPatternSource& other) = default; 93 const ContentSettingPatternSource& other) = default;
100 94
101 RendererContentSettingRules::RendererContentSettingRules() {} 95 RendererContentSettingRules::RendererContentSettingRules() {}
102 96
103 RendererContentSettingRules::~RendererContentSettingRules() {} 97 RendererContentSettingRules::~RendererContentSettingRules() {}
OLDNEW
« no previous file with comments | « no previous file | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698