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; |
} |