Bug 1452666. Implement nsISerializable on expanded principals. r=kmag

This commit is contained in:
Boris Zbarsky 2018-05-24 02:43:14 -04:00
parent 95fd40defd
commit b134958200
3 changed files with 77 additions and 4 deletions

View File

@ -13,10 +13,12 @@ NS_IMPL_CLASSINFO(ExpandedPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
NS_EXPANDEDPRINCIPAL_CID)
NS_IMPL_QUERY_INTERFACE_CI(ExpandedPrincipal,
nsIPrincipal,
nsIExpandedPrincipal)
nsIExpandedPrincipal,
nsISerializable)
NS_IMPL_CI_INTERFACE_GETTER(ExpandedPrincipal,
nsIPrincipal,
nsIExpandedPrincipal)
nsIExpandedPrincipal,
nsISerializable)
struct OriginComparator
{
@ -54,6 +56,11 @@ ExpandedPrincipal::ExpandedPrincipal(nsTArray<nsCOMPtr<nsIPrincipal>> &aWhiteLis
}
}
ExpandedPrincipal::ExpandedPrincipal()
: BasePrincipal(eExpandedPrincipal)
{
}
ExpandedPrincipal::~ExpandedPrincipal()
{ }
@ -234,14 +241,71 @@ ExpandedPrincipal::GetScriptLocation(nsACString& aStr)
// Methods implementing nsISerializable //
//////////////////////////////////////////
// We've had way too many issues with unversioned serializations, so
// explicitly version this one.
static const uint32_t kSerializationVersion = 1;
NS_IMETHODIMP
ExpandedPrincipal::Read(nsIObjectInputStream* aStream)
{
return NS_ERROR_NOT_IMPLEMENTED;
uint32_t version;
nsresult rv = aStream->Read32(&version);
if (version != kSerializationVersion) {
MOZ_ASSERT(false,
"We really need to add handling of the old(?) version here");
return NS_ERROR_UNEXPECTED;
}
uint32_t count;
rv = aStream->Read32(&count);
if (NS_FAILED(rv)) {
return rv;
}
if (!mPrincipals.SetCapacity(count, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
OriginComparator c;
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsISupports> read;
rv = aStream->ReadObject(true, getter_AddRefs(read));
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(read);
if (!principal) {
return NS_ERROR_UNEXPECTED;
}
// Play it safe and InsertElementSorted, in case the sort order
// changed for some bizarre reason.
mPrincipals.InsertElementSorted(Move(principal), c);
}
return NS_OK;
}
NS_IMETHODIMP
ExpandedPrincipal::Write(nsIObjectOutputStream* aStream)
{
return NS_ERROR_NOT_IMPLEMENTED;
nsresult rv = aStream->Write32(kSerializationVersion);
if (NS_FAILED(rv)) {
return rv;
}
rv = aStream->Write32(mPrincipals.Length());
if (NS_FAILED(rv)) {
return rv;
}
for (auto& principal : mPrincipals) {
rv = aStream->WriteObject(principal, true);
if (NS_FAILED(rv)) {
return rv;
}
}
return NS_OK;
}

View File

@ -22,6 +22,10 @@ public:
static PrincipalKind Kind() { return eExpandedPrincipal; }
// For use from the XPCOM factory constructor only. Do not ever use this
// constructor by hand!
ExpandedPrincipal();
NS_DECL_NSIEXPANDEDPRINCIPAL
NS_DECL_NSISERIALIZABLE

View File

@ -100,6 +100,7 @@ using mozilla::dom::AudioChannelAgent;
#include "nsScriptSecurityManager.h"
#include "ContentPrincipal.h"
#include "ExpandedPrincipal.h"
#include "SystemPrincipal.h"
#include "NullPrincipal.h"
#include "nsNetCID.h"
@ -462,6 +463,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(CSPService)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsMixedContentBlocker)
NS_GENERIC_FACTORY_CONSTRUCTOR(ContentPrincipal)
NS_GENERIC_FACTORY_CONSTRUCTOR(ExpandedPrincipal)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(SystemPrincipal,
nsScriptSecurityManager::SystemPrincipalSingletonConstructor)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(NullPrincipal, Init)
@ -578,6 +580,7 @@ NS_DEFINE_NAMED_CID(NS_PARENTPROCESSMESSAGEMANAGER_CID);
NS_DEFINE_NAMED_CID(NS_CHILDPROCESSMESSAGEMANAGER_CID);
NS_DEFINE_NAMED_CID(NS_SCRIPTSECURITYMANAGER_CID);
NS_DEFINE_NAMED_CID(NS_PRINCIPAL_CID);
NS_DEFINE_NAMED_CID(NS_EXPANDEDPRINCIPAL_CID);
NS_DEFINE_NAMED_CID(NS_SYSTEMPRINCIPAL_CID);
NS_DEFINE_NAMED_CID(NS_NULLPRINCIPAL_CID);
NS_DEFINE_NAMED_CID(THIRDPARTYUTIL_CID);
@ -825,6 +828,7 @@ static const mozilla::Module::CIDEntry kLayoutCIDs[] = {
{ &kNS_CHILDPROCESSMESSAGEMANAGER_CID, false, nullptr, CreateChildMessageManager },
{ &kNS_SCRIPTSECURITYMANAGER_CID, false, nullptr, Construct_nsIScriptSecurityManager },
{ &kNS_PRINCIPAL_CID, false, nullptr, ContentPrincipalConstructor },
{ &kNS_EXPANDEDPRINCIPAL_CID, false, nullptr, ExpandedPrincipalConstructor },
{ &kNS_SYSTEMPRINCIPAL_CID, false, nullptr, SystemPrincipalConstructor },
{ &kNS_NULLPRINCIPAL_CID, false, nullptr, NullPrincipalConstructor },
{ &kNS_DEVICE_SENSORS_CID, false, nullptr, nsDeviceSensorsConstructor },
@ -929,6 +933,7 @@ static const mozilla::Module::ContractIDEntry kLayoutContracts[] = {
{ NS_CHILDPROCESSMESSAGEMANAGER_CONTRACTID, &kNS_CHILDPROCESSMESSAGEMANAGER_CID },
{ NS_SCRIPTSECURITYMANAGER_CONTRACTID, &kNS_SCRIPTSECURITYMANAGER_CID },
{ NS_PRINCIPAL_CONTRACTID, &kNS_PRINCIPAL_CID },
{ NS_EXPANDEDPRINCIPAL_CONTRACTID, &kNS_EXPANDEDPRINCIPAL_CID },
{ NS_SYSTEMPRINCIPAL_CONTRACTID, &kNS_SYSTEMPRINCIPAL_CID },
{ NS_NULLPRINCIPAL_CONTRACTID, &kNS_NULLPRINCIPAL_CID },
{ NS_DEVICE_SENSORS_CONTRACTID, &kNS_DEVICE_SENSORS_CID },