Skip to content

Commit

Permalink
Adding UseCounter specific for extensions
Browse files Browse the repository at this point in the history
Replaced Blink.UseCounter.Features by Blink.UseCounter.Extensions.Features when
URL protocol is chrome-extension://

BUG=687169

Review-Url: https://codereview.chromium.org/2796283005
Cr-Commit-Position: refs/heads/master@{#473409}
  • Loading branch information
LoonyBean authored and Commit bot committed May 20, 2017
1 parent d07d1d2 commit fc32d83
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 93 deletions.
31 changes: 27 additions & 4 deletions base/test/histogram_tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ std::vector<Bucket> HistogramTester::GetAllSamples(
return samples;
}

HistogramBase::Count HistogramTester::GetBucketCount(
const std::string& name,
HistogramBase::Sample sample) const {
HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
EXPECT_NE(nullptr, histogram)
<< "Histogram \"" << name << "\" does not exist.";
HistogramBase::Count count = 0;
if (histogram) {
std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
GetBucketCountForSamples(name, sample, *samples, &count);
}
return count;
}

void HistogramTester::GetBucketCountForSamples(
const std::string& name,
HistogramBase::Sample sample,
const HistogramSamples& samples,
HistogramBase::Count* count) const {
*count = samples.GetCount(sample);
auto histogram_data = histograms_snapshot_.find(name);
if (histogram_data != histograms_snapshot_.end())
*count -= histogram_data->second->GetCount(sample);
}

HistogramTester::CountsMap HistogramTester::GetTotalCountsForPrefix(
const std::string& prefix) const {
EXPECT_TRUE(prefix.find('.') != std::string::npos)
Expand Down Expand Up @@ -147,10 +172,8 @@ void HistogramTester::CheckBucketCount(const std::string& name,
HistogramBase::Sample sample,
HistogramBase::Count expected_count,
const HistogramSamples& samples) const {
int actual_count = samples.GetCount(sample);
auto histogram_data = histograms_snapshot_.find(name);
if (histogram_data != histograms_snapshot_.end())
actual_count -= histogram_data->second->GetCount(sample);
int actual_count;
GetBucketCountForSamples(name, sample, samples, &actual_count);

EXPECT_EQ(expected_count, actual_count)
<< "Histogram \"" << name
Expand Down
12 changes: 12 additions & 0 deletions base/test/histogram_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class HistogramTester {
// histogram_tester.GetAllSamples("HistogramName"));
std::vector<Bucket> GetAllSamples(const std::string& name) const;

// Returns the value of the |sample| bucket for ths histogram |name|.
HistogramBase::Count GetBucketCount(const std::string& name,
HistogramBase::Sample sample) const;

// Finds histograms whose names start with |prefix|, and returns them along
// with the counts of any samples added since the creation of this object.
// Histograms that are unchanged are omitted from the result. The return value
Expand Down Expand Up @@ -119,6 +123,14 @@ class HistogramTester {
Histogram::Count expected_count,
const HistogramSamples& samples) const;

// Sets the value for |count| to be the value in the |sample| bucket. The
// bucket's current value is determined from |samples| and is modified based
// on the snapshot stored for histogram |name|.
void GetBucketCountForSamples(const std::string& name,
HistogramBase::Sample sample,
const HistogramSamples& samples,
HistogramBase::Count* count) const;

// Used to determine the histogram changes made during this instance's
// lifecycle.
std::map<std::string, std::unique_ptr<HistogramSamples>> histograms_snapshot_;
Expand Down
69 changes: 43 additions & 26 deletions third_party/WebKit/Source/core/frame/UseCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,6 @@ int UseCounter::MapCSSPropertyIdToCSSSampleIdForHistogram(

UseCounter::UseCounter(Context context)
: mute_count_(0),
disable_reporting_(false),
context_(context),
features_recorded_(kNumberOfFeatures),
css_recorded_(numCSSPropertyIDs),
Expand All @@ -1119,15 +1118,15 @@ void UseCounter::RecordMeasurement(Feature feature) {
if (mute_count_)
return;

DCHECK(feature != kOBSOLETE_PageDestruction &&
feature !=
kPageVisits); // PageDestruction is reserved as a scaling factor.
DCHECK(feature < kNumberOfFeatures);
// PageDestruction is reserved as a scaling factor.
DCHECK_NE(kOBSOLETE_PageDestruction, feature);
DCHECK_NE(kPageVisits, feature);
DCHECK_GE(kNumberOfFeatures, feature);

if (!features_recorded_.QuickGet(feature)) {
// Note that HTTPArchive tooling looks specifically for this event - see
// https://github.com/HTTPArchive/httparchive/issues/59
if (!disable_reporting_) {
if (context_ != kDisabledContext) {
TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("blink.feature_usage"),
"FeatureFirstUsed", "feature", feature);
FeaturesHistogram().Count(feature);
Expand All @@ -1142,10 +1141,10 @@ bool UseCounter::HasRecordedMeasurement(Feature feature) const {
if (mute_count_)
return false;

DCHECK(feature != kOBSOLETE_PageDestruction &&
feature !=
kPageVisits); // PageDestruction is reserved as a scaling factor.
DCHECK(feature < kNumberOfFeatures);
// PageDestruction is reserved as a scaling factor.
DCHECK_NE(kOBSOLETE_PageDestruction, feature);
DCHECK_NE(kPageVisits, feature);
DCHECK_GE(kNumberOfFeatures, feature);

return features_recorded_.QuickGet(feature);
}
Expand All @@ -1158,23 +1157,27 @@ void UseCounter::DidCommitLoad(KURL url) {
legacy_counter_.UpdateMeasurements();

// Reset state from previous load.
disable_reporting_ = false;

// Use the protocol of the document being loaded into the main frame to
// decide whether this page is interesting from a metrics perspective.
// Note that SVGImage cases always have an about:blank URL
if (context_ == kDefaultContext &&
!SchemeRegistry::ShouldTrackUsageMetricsForScheme(url.Protocol())) {
disable_reporting_ = true;
if (context_ != kSVGImageContext) {
if (url.ProtocolIs("chrome-extension"))
context_ = kExtensionContext;
else if (SchemeRegistry::ShouldTrackUsageMetricsForScheme(url.Protocol()))
context_ = kDefaultContext;
else
context_ = kDisabledContext;
}

features_recorded_.ClearAll();
css_recorded_.ClearAll();
animated_css_recorded_.ClearAll();
if (!disable_reporting_ && !mute_count_) {
if (context_ != kDisabledContext && !mute_count_) {
FeaturesHistogram().Count(kPageVisits);
CssHistogram().Count(totalPagesMeasuredCSSSampleId());
AnimatedCSSHistogram().Count(totalPagesMeasuredCSSSampleId());
if (context_ != kExtensionContext) {
CssHistogram().Count(totalPagesMeasuredCSSSampleId());
AnimatedCSSHistogram().Count(totalPagesMeasuredCSSSampleId());
}
}
}

Expand Down Expand Up @@ -1247,7 +1250,7 @@ void UseCounter::Count(CSSParserMode css_parser_mode, CSSPropertyID property) {
// Note that HTTPArchive tooling looks specifically for this event - see
// https://github.com/HTTPArchive/httparchive/issues/59
int sample_id = MapCSSPropertyIdToCSSSampleIdForHistogram(property);
if (!disable_reporting_) {
if (context_ != kDisabledContext && context_ != kExtensionContext) {
TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("blink.feature_usage"),
"CSSFirstUsed", "feature", sample_id);
CssHistogram().Count(sample_id);
Expand Down Expand Up @@ -1294,7 +1297,7 @@ void UseCounter::CountAnimatedCSS(CSSPropertyID property) {

if (!animated_css_recorded_.QuickGet(property)) {
int sample_id = MapCSSPropertyIdToCSSSampleIdForHistogram(property);
if (!disable_reporting_) {
if (context_ != kDisabledContext && context_ != kExtensionContext) {
TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("blink.feature_usage"),
"AnimatedCSSFirstUsed", "feature", sample_id);
AnimatedCSSHistogram().Count(sample_id);
Expand All @@ -1305,7 +1308,7 @@ void UseCounter::CountAnimatedCSS(CSSPropertyID property) {

void UseCounter::NotifyFeatureCounted(Feature feature) {
DCHECK(!mute_count_);
DCHECK(!disable_reporting_);
DCHECK_NE(kDisabledContext, context_);
HeapHashSet<Member<Observer>> to_be_removed;
for (auto observer : observers_) {
if (observer->OnCountFeature(feature))
Expand All @@ -1315,23 +1318,37 @@ void UseCounter::NotifyFeatureCounted(Feature feature) {
}

EnumerationHistogram& UseCounter::FeaturesHistogram() const {
DCHECK_NE(kDisabledContext, context_);
// Every SVGImage has it's own Page instance, and multiple web pages can
// share the usage of a single SVGImage. Ideally perhaps we'd delegate
// metrics from an SVGImage to one of the Page's it's displayed in, but
// that's tricky (SVGImage is intentionally isolated, and the Page that
// created it may not even exist anymore).
// So instead we just use a dedicated histogram for the SVG case.
DEFINE_STATIC_LOCAL(
blink::EnumerationHistogram, histogram,
("Blink.UseCounter.Features", blink::UseCounter::kNumberOfFeatures));
DEFINE_STATIC_LOCAL(blink::EnumerationHistogram, svg_histogram,
("Blink.UseCounter.SVGImage.Features",
blink::UseCounter::kNumberOfFeatures));

return context_ == kSVGImageContext ? svg_histogram : histogram;
DEFINE_STATIC_LOCAL(blink::EnumerationHistogram, extension_histogram,
("Blink.UseCounter.Extensions.Features",
blink::UseCounter::kNumberOfFeatures));
DEFINE_STATIC_LOCAL(
blink::EnumerationHistogram, histogram,
("Blink.UseCounter.Features", blink::UseCounter::kNumberOfFeatures));
switch (context_) {
case kSVGImageContext:
return svg_histogram;
case kExtensionContext:
return extension_histogram;
case kDefaultContext:
case kDisabledContext:
break;
}
return histogram;
}

EnumerationHistogram& UseCounter::CssHistogram() const {
DCHECK_NE(kExtensionContext, context_);
DCHECK_NE(kDisabledContext, context_);
DEFINE_STATIC_LOCAL(blink::EnumerationHistogram, histogram,
("Blink.UseCounter.CSSProperties", kMaximumCSSSampleId));
DEFINE_STATIC_LOCAL(
Expand Down
21 changes: 12 additions & 9 deletions third_party/WebKit/Source/core/frame/UseCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ class CORE_EXPORT UseCounter {
enum Context {
kDefaultContext,
// Counters for SVGImages (lifetime independent from other pages).
kSVGImageContext
kSVGImageContext,
// Counters for extensions.
kExtensionContext,
// Context when counters should be disabled (eg, internal pages such as
// about, chrome-devtools, etc).
kDisabledContext
};

UseCounter(Context = kDefaultContext);
Expand Down Expand Up @@ -1598,8 +1603,8 @@ class CORE_EXPORT UseCounter {
};

// An interface to observe UseCounter changes. Note that this is never
// notified when the counter is disabled by |m_muteCount| or
// |m_disableReporting|.
// notified when the counter is disabled by |m_muteCount| or when |m_context|
// is kDisabledContext.
class Observer : public GarbageCollected<Observer> {
public:
// Notified when a feature is counted for the first time. This should return
Expand Down Expand Up @@ -1661,8 +1666,8 @@ class CORE_EXPORT UseCounter {

private:
// Notifies that a feature is newly counted to |m_observers|. This shouldn't
// be called when the counter is disabled by |m_muteCount| or
// |m_disableReporting|.
// be called when the counter is disabled by |m_muteCount| or when |m_context|
// if kDisabledContext.
void NotifyFeatureCounted(Feature);

EnumerationHistogram& FeaturesHistogram() const;
Expand All @@ -1672,10 +1677,8 @@ class CORE_EXPORT UseCounter {
// If non-zero, ignore all 'count' calls completely.
unsigned mute_count_;

// If true, disable reporting all histogram entries.
bool disable_reporting_;

// The scope represented by this UseCounter instance.
// The scope represented by this UseCounter instance, which must be fixed for
// the duration of a page but can change when a new page is loaded.
Context context_;

// Track what features/properties have been reported to the (non-legacy)
Expand Down
Loading

0 comments on commit fc32d83

Please sign in to comment.