From 4ae79415742e7ddd070bd40fd1d91677e955a207 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Mon, 6 Jul 2015 19:28:41 +0100 Subject: [PATCH] Bug 1165270 - Use origin for BroadcastChannel, r=bholley --- dom/broadcastchannel/BroadcastChannel.cpp | 51 +++---------------- dom/broadcastchannel/BroadcastChannel.h | 4 +- .../BroadcastChannelChild.cpp | 6 +-- dom/broadcastchannel/BroadcastChannelChild.h | 2 +- .../BroadcastChannelParent.cpp | 4 +- dom/broadcastchannel/BroadcastChannelParent.h | 6 +-- .../BroadcastChannelService.cpp | 6 +-- .../BroadcastChannelService.h | 2 +- ipc/glue/BackgroundChildImpl.cpp | 2 +- ipc/glue/BackgroundChildImpl.h | 2 +- ipc/glue/BackgroundParentImpl.cpp | 21 ++++---- ipc/glue/BackgroundParentImpl.h | 4 +- ipc/glue/PBackground.ipdl | 2 +- 13 files changed, 36 insertions(+), 76 deletions(-) diff --git a/dom/broadcastchannel/BroadcastChannel.cpp b/dom/broadcastchannel/BroadcastChannel.cpp index ae00470858a6..6c577c4b258c 100644 --- a/dom/broadcastchannel/BroadcastChannel.cpp +++ b/dom/broadcastchannel/BroadcastChannel.cpp @@ -49,45 +49,6 @@ private: namespace { -void -GetOrigin(nsIPrincipal* aPrincipal, nsAString& aOrigin, ErrorResult& aRv) -{ - MOZ_ASSERT(aPrincipal); - - nsAutoString tmp; - aRv = nsContentUtils::GetUTFOrigin(aPrincipal, tmp); - if (NS_WARN_IF(aRv.Failed())) { - return; - } - - // 'null' means an unknown origin (it can be chrome code or it can be some - // about: page). - - aOrigin = tmp; - if (!aOrigin.EqualsASCII("null")) { - return; - } - - nsCOMPtr uri; - aRv = aPrincipal->GetURI(getter_AddRefs(uri)); - if (NS_WARN_IF(aRv.Failed())) { - return; - } - - if (NS_WARN_IF(!uri)) { - aRv.Throw(NS_ERROR_FAILURE); - return; - } - - nsAutoCString spec; - aRv = uri->GetSpec(spec); - if (NS_WARN_IF(aRv.Failed())) { - return; - } - - aOrigin = NS_ConvertUTF8toUTF16(spec); -} - nsIPrincipal* GetPrincipalFromWorkerPrivate(WorkerPrivate* aWorkerPrivate) { @@ -108,7 +69,7 @@ GetPrincipalFromWorkerPrivate(WorkerPrivate* aWorkerPrivate) class InitializeRunnable final : public WorkerMainThreadRunnable { public: - InitializeRunnable(WorkerPrivate* aWorkerPrivate, nsAString& aOrigin, + InitializeRunnable(WorkerPrivate* aWorkerPrivate, nsACString& aOrigin, PrincipalInfo& aPrincipalInfo, bool& aPrivateBrowsing, ErrorResult& aRv) : WorkerMainThreadRunnable(aWorkerPrivate) @@ -147,7 +108,7 @@ public: return true; } - GetOrigin(principal, mOrigin, mRv); + mRv = principal->GetOrigin(mOrigin); if (NS_WARN_IF(mRv.Failed())) { return true; } @@ -177,7 +138,7 @@ public: private: WorkerPrivate* mWorkerPrivate; - nsAString& mOrigin; + nsACString& mOrigin; PrincipalInfo& mPrincipalInfo; bool& mPrivateBrowsing; ErrorResult& mRv; @@ -384,7 +345,7 @@ BroadcastChannel::IsEnabled(JSContext* aCx, JSObject* aGlobal) BroadcastChannel::BroadcastChannel(nsPIDOMWindow* aWindow, const PrincipalInfo& aPrincipalInfo, - const nsAString& aOrigin, + const nsACString& aOrigin, const nsAString& aChannel, bool aPrivateBrowsing) : DOMEventTargetHelper(aWindow) @@ -420,7 +381,7 @@ BroadcastChannel::Constructor(const GlobalObject& aGlobal, nsCOMPtr window = do_QueryInterface(aGlobal.GetAsSupports()); // Window is null in workers. - nsAutoString origin; + nsAutoCString origin; PrincipalInfo principalInfo; bool privateBrowsing = false; WorkerPrivate* workerPrivate = nullptr; @@ -450,7 +411,7 @@ BroadcastChannel::Constructor(const GlobalObject& aGlobal, return nullptr; } - GetOrigin(principal, origin, aRv); + aRv = principal->GetOrigin(origin); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } diff --git a/dom/broadcastchannel/BroadcastChannel.h b/dom/broadcastchannel/BroadcastChannel.h index 792b53019d5f..e0c38e210477 100644 --- a/dom/broadcastchannel/BroadcastChannel.h +++ b/dom/broadcastchannel/BroadcastChannel.h @@ -92,7 +92,7 @@ public: private: BroadcastChannel(nsPIDOMWindow* aWindow, const PrincipalInfo& aPrincipalInfo, - const nsAString& aOrigin, + const nsACString& aOrigin, const nsAString& aChannel, bool aPrivateBrowsing); @@ -117,7 +117,7 @@ private: nsAutoPtr mPrincipalInfo; - nsString mOrigin; + nsCString mOrigin; nsString mChannel; bool mPrivateBrowsing; diff --git a/dom/broadcastchannel/BroadcastChannelChild.cpp b/dom/broadcastchannel/BroadcastChannelChild.cpp index 6b8647064581..ae8c7477a010 100644 --- a/dom/broadcastchannel/BroadcastChannelChild.cpp +++ b/dom/broadcastchannel/BroadcastChannelChild.cpp @@ -26,10 +26,10 @@ namespace dom { using namespace workers; -BroadcastChannelChild::BroadcastChannelChild(const nsAString& aOrigin) - : mOrigin(aOrigin) - , mActorDestroyed(false) +BroadcastChannelChild::BroadcastChannelChild(const nsACString& aOrigin) + : mActorDestroyed(false) { + CopyUTF8toUTF16(aOrigin, mOrigin); } BroadcastChannelChild::~BroadcastChannelChild() diff --git a/dom/broadcastchannel/BroadcastChannelChild.h b/dom/broadcastchannel/BroadcastChannelChild.h index 08f1becf8686..8230c9b199c3 100644 --- a/dom/broadcastchannel/BroadcastChannelChild.h +++ b/dom/broadcastchannel/BroadcastChannelChild.h @@ -39,7 +39,7 @@ public: } private: - explicit BroadcastChannelChild(const nsAString& aOrigin); + explicit BroadcastChannelChild(const nsACString& aOrigin); ~BroadcastChannelChild(); virtual void ActorDestroy(ActorDestroyReason aWhy) override; diff --git a/dom/broadcastchannel/BroadcastChannelParent.cpp b/dom/broadcastchannel/BroadcastChannelParent.cpp index 3c258ae0565c..fe77d91c3bb2 100644 --- a/dom/broadcastchannel/BroadcastChannelParent.cpp +++ b/dom/broadcastchannel/BroadcastChannelParent.cpp @@ -20,7 +20,7 @@ namespace dom { BroadcastChannelParent::BroadcastChannelParent( const PrincipalInfo& aPrincipalInfo, - const nsAString& aOrigin, + const nsACString& aOrigin, const nsAString& aChannel, bool aPrivateBrowsing) : mService(BroadcastChannelService::GetOrCreate()) @@ -91,7 +91,7 @@ BroadcastChannelParent::ActorDestroy(ActorDestroyReason aWhy) void BroadcastChannelParent::CheckAndDeliver(const ClonedMessageData& aData, - const nsString& aOrigin, + const nsCString& aOrigin, uint64_t aAppId, bool aInBrowserElement, const nsString& aChannel, diff --git a/dom/broadcastchannel/BroadcastChannelParent.h b/dom/broadcastchannel/BroadcastChannelParent.h index 71be3b6db9a5..6da25ff8b062 100644 --- a/dom/broadcastchannel/BroadcastChannelParent.h +++ b/dom/broadcastchannel/BroadcastChannelParent.h @@ -28,7 +28,7 @@ class BroadcastChannelParent final : public PBroadcastChannelParent public: void CheckAndDeliver(const ClonedMessageData& aData, - const nsString& aOrigin, + const nsCString& aOrigin, const uint64_t aAppId, const bool aIsInBrowserElement, const nsString& aChannel, @@ -36,7 +36,7 @@ public: private: BroadcastChannelParent(const PrincipalInfo& aPrincipalInfo, - const nsAString& aOrigin, + const nsACString& aOrigin, const nsAString& aChannel, bool aPrivateBrowsing); ~BroadcastChannelParent(); @@ -49,7 +49,7 @@ private: virtual void ActorDestroy(ActorDestroyReason aWhy) override; nsRefPtr mService; - nsString mOrigin; + nsCString mOrigin; nsString mChannel; uint64_t mAppId; bool mIsInBrowserElement; diff --git a/dom/broadcastchannel/BroadcastChannelService.cpp b/dom/broadcastchannel/BroadcastChannelService.cpp index edf1256c4d3f..3d058dcca2c7 100644 --- a/dom/broadcastchannel/BroadcastChannelService.cpp +++ b/dom/broadcastchannel/BroadcastChannelService.cpp @@ -83,7 +83,7 @@ struct MOZ_STACK_CLASS PostMessageData final { PostMessageData(BroadcastChannelParent* aParent, const ClonedMessageData& aData, - const nsAString& aOrigin, + const nsACString& aOrigin, uint64_t aAppId, bool aIsInBrowserElement, const nsAString& aChannel, @@ -121,7 +121,7 @@ struct MOZ_STACK_CLASS PostMessageData final BroadcastChannelParent* mParent; const ClonedMessageData& mData; nsTArray> mBlobs; - const nsString mOrigin; + const nsCString mOrigin; const nsString mChannel; uint64_t mAppId; bool mIsInBrowserElement; @@ -151,7 +151,7 @@ PostMessageEnumerator(nsPtrHashKey* aKey, void* aPtr) void BroadcastChannelService::PostMessage(BroadcastChannelParent* aParent, const ClonedMessageData& aData, - const nsAString& aOrigin, + const nsACString& aOrigin, uint64_t aAppId, bool aIsInBrowserElement, const nsAString& aChannel, diff --git a/dom/broadcastchannel/BroadcastChannelService.h b/dom/broadcastchannel/BroadcastChannelService.h index 8ad66a1752f9..05d2012cf568 100644 --- a/dom/broadcastchannel/BroadcastChannelService.h +++ b/dom/broadcastchannel/BroadcastChannelService.h @@ -33,7 +33,7 @@ public: void PostMessage(BroadcastChannelParent* aParent, const ClonedMessageData& aData, - const nsAString& aOrigin, + const nsACString& aOrigin, uint64_t aAppId, bool aIsInBrowserElement, const nsAString& aChannel, diff --git a/ipc/glue/BackgroundChildImpl.cpp b/ipc/glue/BackgroundChildImpl.cpp index 884dc7c6b5f9..6f02d971b5f7 100644 --- a/ipc/glue/BackgroundChildImpl.cpp +++ b/ipc/glue/BackgroundChildImpl.cpp @@ -242,7 +242,7 @@ BackgroundChildImpl::DeallocPUDPSocketChild(PUDPSocketChild* child) dom::PBroadcastChannelChild* BackgroundChildImpl::AllocPBroadcastChannelChild(const PrincipalInfo& aPrincipalInfo, - const nsString& aOrigin, + const nsCString& aOrigin, const nsString& aChannel, const bool& aPrivateBrowsing) { diff --git a/ipc/glue/BackgroundChildImpl.h b/ipc/glue/BackgroundChildImpl.h index 345209090595..fce00b0b6e1e 100644 --- a/ipc/glue/BackgroundChildImpl.h +++ b/ipc/glue/BackgroundChildImpl.h @@ -85,7 +85,7 @@ protected: virtual PBroadcastChannelChild* AllocPBroadcastChannelChild(const PrincipalInfo& aPrincipalInfo, - const nsString& aOrigin, + const nsCString& aOrigin, const nsString& aChannel, const bool& aPrivateBrowsing) override; diff --git a/ipc/glue/BackgroundParentImpl.cpp b/ipc/glue/BackgroundParentImpl.cpp index 6125453cbf2d..a82ecdd404dc 100644 --- a/ipc/glue/BackgroundParentImpl.cpp +++ b/ipc/glue/BackgroundParentImpl.cpp @@ -347,7 +347,7 @@ BackgroundParentImpl::DeallocPUDPSocketParent(PUDPSocketParent* actor) mozilla::dom::PBroadcastChannelParent* BackgroundParentImpl::AllocPBroadcastChannelParent( const PrincipalInfo& aPrincipalInfo, - const nsString& aOrigin, + const nsCString& aOrigin, const nsString& aChannel, const bool& aPrivateBrowsing) { @@ -365,7 +365,7 @@ class CheckPrincipalRunnable final : public nsRunnable public: CheckPrincipalRunnable(already_AddRefed aParent, const PrincipalInfo& aPrincipalInfo, - const nsString& aOrigin) + const nsCString& aOrigin) : mContentParent(aParent) , mPrincipalInfo(aPrincipalInfo) , mOrigin(aOrigin) @@ -408,16 +408,15 @@ public: return NS_OK; } - nsCOMPtr uri; - rv = NS_NewURI(getter_AddRefs(uri), mOrigin); - if (NS_FAILED(rv) || !uri) { - mContentParent->KillHard("BroadcastChannel killed: invalid origin URI."); + nsAutoCString origin; + rv = principal->GetOrigin(origin); + if (NS_FAILED(rv)) { + mContentParent->KillHard("BroadcastChannel killed: principal::GetOrigin failed."); return NS_OK; } - rv = principal->CheckMayLoad(uri, false, false); - if (NS_FAILED(rv)) { - mContentParent->KillHard("BroadcastChannel killed: the url cannot be loaded by the principal."); + if (NS_WARN_IF(!mOrigin.Equals(origin))) { + mContentParent->KillHard("BroadcastChannel killed: origins do not match."); return NS_OK; } @@ -427,7 +426,7 @@ public: private: nsRefPtr mContentParent; PrincipalInfo mPrincipalInfo; - nsString mOrigin; + nsCString mOrigin; nsCOMPtr mBackgroundThread; }; @@ -437,7 +436,7 @@ bool BackgroundParentImpl::RecvPBroadcastChannelConstructor( PBroadcastChannelParent* actor, const PrincipalInfo& aPrincipalInfo, - const nsString& aOrigin, + const nsCString& aOrigin, const nsString& aChannel, const bool& aPrivateBrowsing) { diff --git a/ipc/glue/BackgroundParentImpl.h b/ipc/glue/BackgroundParentImpl.h index 777303f8feec..d60ce1907c9a 100644 --- a/ipc/glue/BackgroundParentImpl.h +++ b/ipc/glue/BackgroundParentImpl.h @@ -72,14 +72,14 @@ protected: virtual PBroadcastChannelParent* AllocPBroadcastChannelParent(const PrincipalInfo& aPrincipalInfo, - const nsString& aOrigin, + const nsCString& aOrigin, const nsString& aChannel, const bool& aPrivateBrowsing) override; virtual bool RecvPBroadcastChannelConstructor(PBroadcastChannelParent* actor, const PrincipalInfo& aPrincipalInfo, - const nsString& origin, + const nsCString& origin, const nsString& channel, const bool& aPrivateBrowsing) override; diff --git a/ipc/glue/PBackground.ipdl b/ipc/glue/PBackground.ipdl index 705b094ed062..72c929c4828b 100644 --- a/ipc/glue/PBackground.ipdl +++ b/ipc/glue/PBackground.ipdl @@ -49,7 +49,7 @@ parent: PVsync(); PUDPSocket(OptionalPrincipalInfo pInfo, nsCString filter); - PBroadcastChannel(PrincipalInfo pInfo, nsString origin, nsString channel, + PBroadcastChannel(PrincipalInfo pInfo, nsCString origin, nsString channel, bool privateBrowsing); PServiceWorkerManager();