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

Unified Diff: base/metrics/histogram.cc

Issue 141393002: Return a NULL histogram pointer on construction error (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add construction param to restore old CHECK for non-API calls Created 6 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: base/metrics/histogram.cc
diff --git a/base/metrics/histogram.cc b/base/metrics/histogram.cc
index fbe66d05d29fd3c2896f8499dd1dc07d9793d095..2d8a7c9d33844156d8184747d5a263b2a37fc133 100644
--- a/base/metrics/histogram.cc
+++ b/base/metrics/histogram.cc
@@ -76,6 +76,23 @@ bool ValidateRangeChecksum(const HistogramBase& histogram,
return casted_histogram.bucket_ranges()->checksum() == range_checksum;
}
+// DummyHistogram is an object that overrides Histogram's Add* functions
+// to provide no functionality. This can be returned in the case of mismatched
+// construction arguments to any of the other Histograms' FactoryGet calls.
+class DummyHistogram : public Histogram {
+ public:
+ virtual void Add(Sample value) OVERRIDE {}
+ virtual void AddSamples(const HistogramSamples& samples) OVERRIDE {}
+ virtual bool AddSamplesFromPickle(PickleIterator* iter) OVERRIDE {
+ return false;
+ }
+
+ DummyHistogram() : Histogram("Dummy", 0, 1, NULL) {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(DummyHistogram);
+};
+
} // namespace
typedef HistogramBase::Count Count;
@@ -88,7 +105,8 @@ HistogramBase* Histogram::FactoryGet(const string& name,
Sample minimum,
Sample maximum,
size_t bucket_count,
- int32 flags) {
+ int32 flags,
+ int32 construction_behavior) {
bool valid_arguments =
InspectConstructionArguments(name, &minimum, &maximum, &bucket_count);
DCHECK(valid_arguments);
@@ -110,7 +128,21 @@ HistogramBase* Histogram::FactoryGet(const string& name,
}
DCHECK_EQ(HISTOGRAM, histogram->GetHistogramType());
- CHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count));
+ if (construction_behavior == kAllowBadConstruction) {
+ if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) {
+ // The construction arguments do not match the existing histogram. This
+ // can come about if an extension updates in the middle of a chrome run
+ // and has changed one of them. We return a dummy object here instead of
+ // crashing so poor use of extension/Pepper APIs do not cause crashes in
+ // Chrome.
+ DLOG(ERROR) << "Histogram " << name << " has bad construction arguments";
+ DummyHistogram* tentative_histogram = new DummyHistogram;
+ histogram =
+ StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
+ }
+ } else {
+ CHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count));
+ }
return histogram;
}
@@ -118,9 +150,10 @@ HistogramBase* Histogram::FactoryTimeGet(const string& name,
TimeDelta minimum,
TimeDelta maximum,
size_t bucket_count,
- int32 flags) {
+ int32 flags,
+ int32 construction_behavior) {
return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
- bucket_count, flags);
+ bucket_count, flags, construction_behavior);
}
TimeTicks Histogram::DebugNow() {
@@ -517,18 +550,20 @@ HistogramBase* LinearHistogram::FactoryGet(const string& name,
Sample minimum,
Sample maximum,
size_t bucket_count,
- int32 flags) {
+ int32 flags,
+ int32 construction_behavior) {
return FactoryGetWithRangeDescription(
- name, minimum, maximum, bucket_count, flags, NULL);
+ name, minimum, maximum, bucket_count, flags, NULL, construction_behavior);
}
HistogramBase* LinearHistogram::FactoryTimeGet(const string& name,
TimeDelta minimum,
TimeDelta maximum,
size_t bucket_count,
- int32 flags) {
+ int32 flags,
+ int32 construction_behavior) {
return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
- bucket_count, flags);
+ bucket_count, flags, construction_behavior);
}
HistogramBase* LinearHistogram::FactoryGetWithRangeDescription(
@@ -537,7 +572,8 @@ HistogramBase* LinearHistogram::FactoryGetWithRangeDescription(
Sample maximum,
size_t bucket_count,
int32 flags,
- const DescriptionPair descriptions[]) {
+ const DescriptionPair descriptions[],
+ int32 construction_behavior) {
bool valid_arguments = Histogram::InspectConstructionArguments(
name, &minimum, &maximum, &bucket_count);
DCHECK(valid_arguments);
@@ -567,7 +603,21 @@ HistogramBase* LinearHistogram::FactoryGetWithRangeDescription(
}
DCHECK_EQ(LINEAR_HISTOGRAM, histogram->GetHistogramType());
- CHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count));
+ if (construction_behavior == kAllowBadConstruction) {
+ if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) {
+ // The construction arguments do not match the existing histogram. This
+ // can come about if an extension updates in the middle of a chrome run
+ // and has changed one of them. We return a dummy object here instead of
+ // crashing so poor use of extension/Pepper APIs do not cause crashes in
+ // Chrome.
+ DLOG(ERROR) << "Histogram " << name << " has bad construction arguments";
+ DummyHistogram* tentative_histogram = new DummyHistogram;
+ histogram =
+ StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
+ }
+ } else {
+ CHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count));
+ }
return histogram;
}

Powered by Google App Engine
This is Rietveld 408576698