Skip to content

Commit

Permalink
Add metrics for schemes of content setting exceptions
Browse files Browse the repository at this point in the history
Add a uma metric to collect data for schemes (http/https/file/chrome-extension) of
content setting exceptions.

BUG=621724

Review-Url: https://codereview.chromium.org/2226643002
Cr-Commit-Position: refs/heads/master@{#411245}
  • Loading branch information
lshang authored and Commit bot committed Aug 11, 2016
1 parent 4926894 commit d06a12d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ HostContentSettingsMap::HostContentSettingsMap(PrefService* prefs,

MigrateKeygenSettings();
MigrateDomainScopedSettings(false);
RecordNumberOfExceptions();
RecordExceptionMetrics();
}

// static
Expand Down Expand Up @@ -600,7 +600,7 @@ base::WeakPtr<HostContentSettingsMap> HostContentSettingsMap::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}

void HostContentSettingsMap::RecordNumberOfExceptions() {
void HostContentSettingsMap::RecordExceptionMetrics() {
for (const content_settings::WebsiteSettingsInfo* info :
*content_settings::WebsiteSettingsRegistry::GetInstance()) {
ContentSettingsType content_type = info->type();
Expand All @@ -610,6 +610,17 @@ void HostContentSettingsMap::RecordNumberOfExceptions() {
GetSettingsForOneType(content_type, std::string(), &settings);
size_t num_exceptions = 0;
for (const ContentSettingPatternSource& setting_entry : settings) {
// Skip default settings.
if (setting_entry.primary_pattern == ContentSettingsPattern::Wildcard() &&
setting_entry.secondary_pattern ==
ContentSettingsPattern::Wildcard()) {
continue;
}

UMA_HISTOGRAM_ENUMERATION("ContentSettings.ExceptionScheme",
setting_entry.primary_pattern.GetScheme(),
ContentSettingsPattern::SCHEME_MAX);

if (setting_entry.source == "preference")
++num_exceptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ class HostContentSettingsMap : public content_settings::Observer,
// leave in some code to remove old-format settings for a long time.
void MigrateKeygenSettings();

// Collect UMA data about the number of exceptions.
void RecordNumberOfExceptions();
// Collect UMA data of exceptions.
void RecordExceptionMetrics();

// Adds content settings for |content_type| and |resource_identifier|,
// provided by |provider|, into |settings|. If |incognito| is true, adds only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ namespace {
// The component supports only one scheme for simplicity.
const char* non_port_non_domain_wildcard_scheme = NULL;

// Keep it consistent with enum SchemeType in content_settings_pattern.h.
const char* const kSchemeNames[] = {
"wildcard",
"other",
url::kHttpScheme,
url::kHttpsScheme,
url::kFileScheme,
"chrome-extension",
};

static_assert(arraysize(kSchemeNames) == ContentSettingsPattern::SCHEME_MAX,
"kSchemeNames should have SCHEME_MAX elements");

std::string GetDefaultPort(const std::string& scheme) {
if (scheme == url::kHttpScheme)
return "80";
Expand Down Expand Up @@ -590,6 +603,17 @@ std::string ContentSettingsPattern::ToString() const {
return std::string();
}

ContentSettingsPattern::SchemeType ContentSettingsPattern::GetScheme() const {
if (parts_.is_scheme_wildcard)
return SCHEME_WILDCARD;

for (size_t i = 2; i < arraysize(kSchemeNames); ++i) {
if (parts_.scheme == kSchemeNames[i])
return static_cast<SchemeType>(i);
}
return SCHEME_OTHER;
}

ContentSettingsPattern::Relation ContentSettingsPattern::Compare(
const ContentSettingsPattern& other) const {
// Two invalid patterns are identical in the way they behave. They don't match
Expand Down
16 changes: 16 additions & 0 deletions components/content_settings/core/common/content_settings_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ class ContentSettingsPattern {
DISJOINT_ORDER_PRE = 2,
};

// This enum is used to back an UMA histogram, the order of existing values
// should not be changed. New values should only append before SCHEME_MAX.
// Also keep it consistent with kSchemeNames in content_settings_pattern.cc.
enum SchemeType {
SCHEME_WILDCARD,
SCHEME_OTHER,
SCHEME_HTTP,
SCHEME_HTTPS,
SCHEME_FILE,
SCHEME_CHROMEEXTENSION,
SCHEME_MAX,
};

struct PatternParts {
PatternParts();
PatternParts(const PatternParts& other);
Expand Down Expand Up @@ -178,6 +191,9 @@ class ContentSettingsPattern {
// Returns a std::string representation of this pattern.
std::string ToString() const;

// Returns scheme type of pattern.
ContentSettingsPattern::SchemeType GetScheme() const;

// Compares the pattern with a given |other| pattern and returns the
// |Relation| of the two patterns.
Relation Compare(const ContentSettingsPattern& other) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,4 +808,22 @@ TEST(ContentSettingsPatternTest, MigrateFromDomainToOrigin) {
ContentSettingsPattern::FromString("https://[*.]google.com:443"),
&origin_pattern));
EXPECT_EQ("https://google.com:443", origin_pattern.ToString());
}

TEST(ContentSettingsPatternTest, Schemes) {
EXPECT_EQ(ContentSettingsPattern::SCHEME_HTTP,
Pattern("http://www.example.com").GetScheme());
EXPECT_EQ(ContentSettingsPattern::SCHEME_HTTPS,
Pattern("https://www.example.com").GetScheme());
EXPECT_EQ(ContentSettingsPattern::SCHEME_FILE,
Pattern("file:///tmp/file.html").GetScheme());
EXPECT_EQ(ContentSettingsPattern::SCHEME_CHROMEEXTENSION,
Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/")
.GetScheme());
EXPECT_EQ(ContentSettingsPattern::SCHEME_WILDCARD,
Pattern("192.168.0.1").GetScheme());
EXPECT_EQ(ContentSettingsPattern::SCHEME_WILDCARD,
Pattern("www.example.com").GetScheme());
EXPECT_EQ(ContentSettingsPattern::SCHEME_OTHER,
Pattern("filesystem:http://www.google.com/temporary/").GetScheme());
}
17 changes: 17 additions & 0 deletions tools/metrics/histograms/histograms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6604,6 +6604,14 @@ http://cs/file:chrome/histograms.xml - but prefer this file for new entries.
</summary>
</histogram>

<histogram name="ContentSettings.ExceptionScheme" enum="ContentSettingScheme">
<owner>lshang@chromium.org</owner>
<summary>
Records the schemes of content setting exceptions at browser start. Each
exception provides one sample.
</summary>
</histogram>

<histogram name="ContentSettings.LastSettingParsed" enum="LastSettingParsed">
<obsolete>
Deprecated 2015-10-05 in Issue 433475. Histogram was used temorarily for
Expand Down Expand Up @@ -71028,6 +71036,15 @@ http://cs/file:chrome/histograms.xml - but prefer this file for new entries.
<int value="3" label="Clicked 'Learn more'"/>
</enum>

<enum name="ContentSettingScheme" type="int">
<int value="0" label="(wildcard)"/>
<int value="1" label="(other)"/>
<int value="2" label="http"/>
<int value="3" label="https"/>
<int value="4" label="file"/>
<int value="5" label="chrome-extension"/>
</enum>

<enum name="ContentType" type="int">
<int value="-1" label="Invalid setting"/>
<int value="0" label="Cookies setting"/>
Expand Down

0 comments on commit d06a12d

Please sign in to comment.