bug 1440673 - Allow changing the max number of keys per-keyed-scalar r?Dexter
I considered a few different ways to do this, but storing the limit inside the
KeyedScalar itself was the cleanest. The limit check is too deep to pass it as
an argument, and making it conditional on the keyed scalar being called
"telemetry.event_counts" was too fragile.
MozReview-Commit-ID: AyfEKB40Abb
--- a/toolkit/components/telemetry/TelemetryScalar.cpp
+++ b/toolkit/components/telemetry/TelemetryScalar.cpp
@@ -623,17 +623,20 @@ internal_ScalarAllocate(uint32_t aScalar
/**
* The implementation for the keyed scalar type.
*/
class KeyedScalar
{
public:
typedef mozilla::Pair<nsCString, nsCOMPtr<nsIVariant>> KeyValuePair;
- explicit KeyedScalar(uint32_t aScalarKind) : mScalarKind(aScalarKind) {};
+ explicit KeyedScalar(uint32_t aScalarKind)
+ : mScalarKind(aScalarKind)
+ , mMaximumNumberOfKeys(kMaximumNumberOfKeys)
+ { };
~KeyedScalar() = default;
// Set, Add and SetMaximum functions as described in the Telemetry IDL.
// These methods implicitly instantiate a Scalar[*] for each key.
ScalarResult SetValue(const nsAString& aKey, nsIVariant* aValue);
ScalarResult AddValue(const nsAString& aKey, nsIVariant* aValue);
ScalarResult SetMaximum(const nsAString& aKey, nsIVariant* aValue);
@@ -645,21 +648,28 @@ public:
// GetValue is used to get the key-value pairs stored in the keyed scalar
// when persisting it to JS.
nsresult GetValue(nsTArray<KeyValuePair>& aValues) const;
// To measure the memory stats.
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
+ // To permit more keys than normal.
+ void SetMaximumNumberOfKeys(uint32_t aMaximumNumberOfKeys)
+ {
+ mMaximumNumberOfKeys = aMaximumNumberOfKeys;
+ };
+
private:
typedef nsClassHashtable<nsCStringHashKey, ScalarBase> ScalarKeysMapType;
ScalarKeysMapType mScalarKeys;
const uint32_t mScalarKind;
+ uint32_t mMaximumNumberOfKeys;
ScalarResult GetScalarForKey(const nsAString& aKey, ScalarBase** aRet);
};
ScalarResult
KeyedScalar::SetValue(const nsAString& aKey, nsIVariant* aValue)
{
ScalarBase* scalar = nullptr;
@@ -793,17 +803,17 @@ KeyedScalar::GetScalarForKey(const nsASt
NS_ConvertUTF16toUTF8 utf8Key(aKey);
ScalarBase* scalar = nullptr;
if (mScalarKeys.Get(utf8Key, &scalar)) {
*aRet = scalar;
return ScalarResult::Ok;
}
- if (mScalarKeys.Count() >= kMaximumNumberOfKeys) {
+ if (mScalarKeys.Count() >= mMaximumNumberOfKeys) {
return ScalarResult::TooManyKeys;
}
scalar = internal_ScalarAllocate(mScalarKind);
if (!scalar) {
return ScalarResult::InvalidType;
}