Bug 1798493 - Part 1: Use nsIPrincipal instead of ContentPrincipalInfo for PLockManager r=asuth

Differential Revision: https://phabricator.services.mozilla.com/D163337
This commit is contained in:
Kagami Sascha Rosylight 2023-06-15 19:25:54 +00:00
Родитель 850d74a0e2
Коммит 60521aa51a
11 изменённых файлов: 29 добавлений и 93 удалений

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

@ -1,56 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef CAPS_PRINCIPALHASHKEY_H_
#define CAPS_PRINCIPALHASHKEY_H_
#include "mozilla/ipc/PBackgroundSharedTypes.h"
#include "PLDHashTable.h"
#include "nsHashKeys.h"
#include "nsUnicharUtils.h"
namespace mozilla {
class ContentPrincipalInfoHashKey : public PLDHashEntryHdr {
public:
using KeyType = const ipc::ContentPrincipalInfo&;
using KeyTypePointer = const ipc::ContentPrincipalInfo*;
explicit ContentPrincipalInfoHashKey(KeyTypePointer aKey)
: mPrincipalInfo(*aKey) {
MOZ_COUNT_CTOR(ContentPrincipalInfoHashKey);
}
ContentPrincipalInfoHashKey(ContentPrincipalInfoHashKey&& aOther) noexcept
: mPrincipalInfo(aOther.mPrincipalInfo) {
MOZ_COUNT_CTOR(ContentPrincipalInfoHashKey);
}
MOZ_COUNTED_DTOR(ContentPrincipalInfoHashKey)
KeyType GetKey() const { return mPrincipalInfo; }
bool KeyEquals(KeyTypePointer aKey) const {
// Mocks BasePrincipal::FastEquals()
return mPrincipalInfo.originNoSuffix() == aKey->originNoSuffix() &&
mPrincipalInfo.attrs() == aKey->attrs();
}
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
static PLDHashNumber HashKey(KeyTypePointer aKey) {
nsAutoCString suffix;
aKey->attrs().CreateSuffix(suffix);
return HashGeneric(HashString(aKey->originNoSuffix()), HashString(suffix));
}
enum { ALLOW_MEMMOVE = true };
protected:
const ipc::ContentPrincipalInfo mPrincipalInfo;
};
} // namespace mozilla
#endif // CAPS_PRINCIPALHASHKEY_H_

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

@ -25,7 +25,7 @@ class PrincipalHashKey : public PLDHashEntryHdr {
MOZ_ASSERT(aKey);
MOZ_COUNT_CTOR(PrincipalHashKey);
}
PrincipalHashKey(PrincipalHashKey&& aKey)
PrincipalHashKey(PrincipalHashKey&& aKey) noexcept
: mPrincipal(std::move(aKey.mPrincipal)) {
MOZ_COUNT_CTOR(PrincipalHashKey);
}
@ -43,7 +43,7 @@ class PrincipalHashKey : public PLDHashEntryHdr {
return aKey;
}
static PLDHashNumber HashKey(const nsIPrincipal* aKey) {
auto* bp = BasePrincipal::Cast(aKey);
const auto* bp = BasePrincipal::Cast(aKey);
return HashGeneric(bp->GetOriginNoSuffixHash(), bp->GetOriginSuffixHash());
}

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

@ -31,7 +31,6 @@ EXPORTS += [
EXPORTS.mozilla = [
"BasePrincipal.h",
"ContentPrincipal.h",
"ContentPrincipalInfoHashKey.h",
"ExpandedPrincipal.h",
"NullPrincipal.h",
"OriginAttributes.h",

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

@ -34,9 +34,10 @@ ClientInfo& ClientInfo::operator=(const ClientInfo& aRight) {
return *this;
}
ClientInfo::ClientInfo(ClientInfo&& aRight) : mData(std::move(aRight.mData)) {}
ClientInfo::ClientInfo(ClientInfo&& aRight) noexcept
: mData(std::move(aRight.mData)) {}
ClientInfo& ClientInfo::operator=(ClientInfo&& aRight) {
ClientInfo& ClientInfo::operator=(ClientInfo&& aRight) noexcept {
mData.reset();
mData = std::move(aRight.mData);
return *this;
@ -89,14 +90,14 @@ const IPCClientInfo& ClientInfo::ToIPC() const { return *mData; }
bool ClientInfo::IsPrivateBrowsing() const {
switch (PrincipalInfo().type()) {
case PrincipalInfo::TContentPrincipalInfo: {
auto& p = PrincipalInfo().get_ContentPrincipalInfo();
const auto& p = PrincipalInfo().get_ContentPrincipalInfo();
return p.attrs().mPrivateBrowsingId != 0;
}
case PrincipalInfo::TSystemPrincipalInfo: {
return false;
}
case PrincipalInfo::TNullPrincipalInfo: {
auto& p = PrincipalInfo().get_NullPrincipalInfo();
const auto& p = PrincipalInfo().get_NullPrincipalInfo();
return p.attrs().mPrivateBrowsingId != 0;
}
default: {
@ -107,7 +108,6 @@ bool ClientInfo::IsPrivateBrowsing() const {
}
Result<nsCOMPtr<nsIPrincipal>, nsresult> ClientInfo::GetPrincipal() const {
MOZ_ASSERT(NS_IsMainThread());
return PrincipalInfoToPrincipal(PrincipalInfo());
}

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

@ -45,9 +45,9 @@ class ClientInfo final {
ClientInfo& operator=(const ClientInfo& aRight);
ClientInfo(ClientInfo&& aRight);
ClientInfo(ClientInfo&& aRight) noexcept;
ClientInfo& operator=(ClientInfo&& aRight);
ClientInfo& operator=(ClientInfo&& aRight) noexcept;
explicit ClientInfo(const IPCClientInfo& aData);

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

@ -39,18 +39,16 @@ LockManager::LockManager(nsIGlobalObject* aGlobal) : mOwner(aGlobal) {
return;
}
const mozilla::ipc::PrincipalInfo& principalInfo =
clientInfo->PrincipalInfo();
if (principalInfo.type() !=
mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) {
nsCOMPtr<nsIPrincipal> principal =
clientInfo->GetPrincipal().unwrapOr(nullptr);
if (!principal || !principal->GetIsContentPrincipal()) {
return;
}
mozilla::ipc::PBackgroundChild* backgroundActor =
mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread();
mActor = new locks::LockManagerChild(aGlobal);
backgroundActor->SendPLockManagerConstructor(mActor, principalInfo,
backgroundActor->SendPLockManagerConstructor(mActor, WrapNotNull(principal),
clientInfo->Id());
if (!NS_IsMainThread()) {

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

@ -7,7 +7,6 @@
#include "LockManagerParent.h"
#include "LockRequestParent.h"
#include "mozilla/ContentPrincipalInfoHashKey.h"
#include "mozilla/PrincipalHashKey.h"
#include "mozilla/RefPtr.h"
#include "mozilla/StaticPtr.h"
@ -18,27 +17,24 @@
namespace mozilla::dom::locks {
static StaticAutoPtr<
nsTHashMap<ContentPrincipalInfoHashKey, WeakPtr<ManagedLocks>>>
static StaticAutoPtr<nsTHashMap<PrincipalHashKey, WeakPtr<ManagedLocks>>>
sManagedLocksMap;
using IPCResult = mozilla::ipc::IPCResult;
LockManagerParent::LockManagerParent(
const mozilla::ipc::ContentPrincipalInfo& aPrincipalInfo,
const nsID& aClientId)
: mClientId(NSID_TrimBracketsUTF16(aClientId)),
mPrincipalInfo(aPrincipalInfo) {
LockManagerParent::LockManagerParent(NotNull<nsIPrincipal*> aPrincipal,
const nsID& aClientId)
: mClientId(NSID_TrimBracketsUTF16(aClientId)), mPrincipal(aPrincipal) {
if (!sManagedLocksMap) {
sManagedLocksMap =
new nsTHashMap<ContentPrincipalInfoHashKey, WeakPtr<ManagedLocks>>();
new nsTHashMap<PrincipalHashKey, WeakPtr<ManagedLocks>>();
} else {
mManagedLocks = sManagedLocksMap->Get(aPrincipalInfo);
mManagedLocks = sManagedLocksMap->Get(aPrincipal);
}
if (!mManagedLocks) {
mManagedLocks = new ManagedLocks();
sManagedLocksMap->LookupOrInsert(aPrincipalInfo, mManagedLocks);
sManagedLocksMap->LookupOrInsert(aPrincipal, mManagedLocks);
}
}
@ -79,8 +75,8 @@ void LockManagerParent::ActorDestroy(ActorDestroyReason aWhy) {
mManagedLocks = nullptr;
// We just decreased the refcount and potentially deleted it, so check whether
// the weak pointer still points to anything and remove the entry if not.
if (!sManagedLocksMap->Get(mPrincipalInfo)) {
sManagedLocksMap->Remove(mPrincipalInfo);
if (!sManagedLocksMap->Get(mPrincipal)) {
sManagedLocksMap->Remove(mPrincipal);
}
}

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

@ -32,8 +32,7 @@ class LockManagerParent final : public PLockManagerParent {
public:
NS_INLINE_DECL_REFCOUNTING(LockManagerParent)
LockManagerParent(const mozilla::ipc::ContentPrincipalInfo& aPrincipalInfo,
const nsID& aClientId);
LockManagerParent(NotNull<nsIPrincipal*> aPrincipal, const nsID& aClientId);
void ProcessRequestQueue(nsTArray<RefPtr<LockRequestParent>>& aQueue);
bool IsGrantableRequest(const IPCLockRequest& aRequest);
@ -54,7 +53,7 @@ class LockManagerParent final : public PLockManagerParent {
RefPtr<ManagedLocks> mManagedLocks;
nsString mClientId;
mozilla::ipc::ContentPrincipalInfo mPrincipalInfo;
NotNull<nsCOMPtr<nsIPrincipal>> mPrincipal;
};
} // namespace mozilla::dom::locks

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

@ -1474,9 +1474,9 @@ bool BackgroundParentImpl::DeallocPMediaTransportParent(
}
already_AddRefed<dom::locks::PLockManagerParent>
BackgroundParentImpl::AllocPLockManagerParent(
const ContentPrincipalInfo& aPrincipalInfo, const nsID& aClientId) {
return MakeAndAddRef<mozilla::dom::locks::LockManagerParent>(aPrincipalInfo,
BackgroundParentImpl::AllocPLockManagerParent(NotNull<nsIPrincipal*> aPrincipal,
const nsID& aClientId) {
return MakeAndAddRef<mozilla::dom::locks::LockManagerParent>(aPrincipal,
aClientId);
}

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

@ -408,7 +408,7 @@ class BackgroundParentImpl : public PBackgroundParent {
PWebSocketConnectionParent* actor, const uint32_t& aListenerId) override;
already_AddRefed<PLockManagerParent> AllocPLockManagerParent(
const ContentPrincipalInfo& aPrincipalInfo, const nsID& aClientId) final;
NotNull<nsIPrincipal*> aPrincipal, const nsID& aClientId) final;
already_AddRefed<PFetchParent> AllocPFetchParent() override;
};

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

@ -313,7 +313,7 @@ parent:
async PWebSocketConnection(uint32_t aListenerId);
async PLockManager(ContentPrincipalInfo aPrincipalInfo, nsID aClientId);
async PLockManager(nsIPrincipal aPrincipalInfo, nsID aClientId);
async PIPCClientCerts();