Bug 1795312 - Optimize serializing enums as JSON string keys for principals. r=farre

Depends on D166953

Differential Revision: https://phabricator.services.mozilla.com/D166954
This commit is contained in:
Peter Van der Beken 2023-02-14 10:17:17 +00:00
Родитель cec60f10c8
Коммит 46c52d2b18
5 изменённых файлов: 44 добавлений и 10 удалений

Просмотреть файл

@ -47,6 +47,13 @@
namespace mozilla {
const char* BasePrincipal::JSONEnumKeyStrings[4] = {
"0",
"1",
"2",
"3",
};
BasePrincipal::BasePrincipal(PrincipalKind aKind,
const nsACString& aOriginNoSuffix,
const OriginAttributes& aOriginAttributes)
@ -398,8 +405,10 @@ nsresult BasePrincipal::ToJSON(nsACString& aJSON) {
}
nsresult BasePrincipal::ToJSON(Json::Value& aObject) {
std::string key = std::to_string(Kind());
nsresult rv = PopulateJSONObject((aObject[key] = Json::objectValue));
static_assert(eKindMax < ArrayLength(JSONEnumKeyStrings));
nsresult rv = PopulateJSONObject(
(aObject[Json::StaticString(JSONEnumKeyStrings[Kind()])] =
Json::objectValue));
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
@ -1588,4 +1597,11 @@ BasePrincipal::Deserializer::Write(nsIObjectOutputStream* aStream) {
return NS_OK;
}
/* static */
void BasePrincipal::SetJSONValue(Json::Value& aObject, const char* aKey,
const nsCString& aValue) {
aObject[Json::StaticString(aKey)] =
Json::Value(aValue.BeginReading(), aValue.EndReading());
}
} // namespace mozilla

Просмотреть файл

@ -342,6 +342,23 @@ class BasePrincipal : public nsJSPrincipals {
RefPtr<BasePrincipal> mPrincipal;
};
private:
static const char* JSONEnumKeyStrings[4];
static void SetJSONValue(Json::Value& aObject, const char* aKey,
const nsCString& aValue);
protected:
template <size_t EnumValue>
static inline constexpr const char* JSONEnumKeyString() {
static_assert(EnumValue < ArrayLength(JSONEnumKeyStrings));
return JSONEnumKeyStrings[EnumValue];
}
template <size_t EnumValue>
static void SetJSONValue(Json::Value& aObject, const nsCString& aValue) {
SetJSONValue(aObject, JSONEnumKeyString<EnumValue>(), aValue);
}
private:
static already_AddRefed<BasePrincipal> CreateContentPrincipal(
nsIURI* aURI, const OriginAttributes& aAttrs,

Просмотреть файл

@ -623,7 +623,7 @@ nsresult ContentPrincipal::PopulateJSONObject(Json::Value& aObject) {
// Key ----------------------
// |
// Value
aObject[std::to_string(eURI)] = principalURI.get();
SetJSONValue<eURI>(aObject, principalURI);
if (GetHasExplicitDomain()) {
nsAutoCString domainStr;
@ -632,13 +632,13 @@ nsresult ContentPrincipal::PopulateJSONObject(Json::Value& aObject) {
rv = mDomain->GetSpec(domainStr);
NS_ENSURE_SUCCESS(rv, rv);
}
aObject[std::to_string(eDomain)] = domainStr.get();
SetJSONValue<eDomain>(aObject, domainStr);
}
nsAutoCString suffix;
OriginAttributesRef().CreateSuffix(suffix);
if (suffix.Length() > 0) {
aObject[std::to_string(eSuffix)] = suffix.get();
SetJSONValue<eSuffix>(aObject, suffix);
}
return NS_OK;

Просмотреть файл

@ -291,8 +291,9 @@ nsresult ExpandedPrincipal::GetSiteIdentifier(SiteIdentifier& aSite) {
}
nsresult ExpandedPrincipal::PopulateJSONObject(Json::Value& aObject) {
Json::Value& principalList = aObject[std::to_string(eSpecs)] =
Json::arrayValue;
Json::Value& principalList =
aObject[Json::StaticString(JSONEnumKeyString<eSpecs>())] =
Json::arrayValue;
for (const auto& principal : mPrincipals) {
Json::Value object = Json::objectValue;
nsresult rv = BasePrincipal::Cast(principal)->ToJSON(object);
@ -304,7 +305,7 @@ nsresult ExpandedPrincipal::PopulateJSONObject(Json::Value& aObject) {
nsAutoCString suffix;
OriginAttributesRef().CreateSuffix(suffix);
if (suffix.Length() > 0) {
aObject[std::to_string(eSuffix)] = suffix.get();
SetJSONValue<eSuffix>(aObject, suffix);
}
return NS_OK;

Просмотреть файл

@ -240,12 +240,12 @@ nsresult NullPrincipal::PopulateJSONObject(Json::Value& aObject) {
nsAutoCString principalURI;
nsresult rv = mURI->GetSpec(principalURI);
NS_ENSURE_SUCCESS(rv, rv);
aObject[std::to_string(eSpec)] = principalURI.get();
SetJSONValue<eSpec>(aObject, principalURI);
nsAutoCString suffix;
OriginAttributesRef().CreateSuffix(suffix);
if (suffix.Length() > 0) {
aObject[std::to_string(eSuffix)] = suffix.get();
SetJSONValue<eSuffix>(aObject, suffix);
}
return NS_OK;