From 621d7068b82a25da5e2d05e53f925a52dcb95575 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Tue, 31 Mar 2015 22:11:47 +0100 Subject: [PATCH] Bug 1148032 - BroadcastChannel API should not bypass private browsing mode, r=ehsan --- dom/broadcastchannel/BroadcastChannel.cpp | 27 ++-- dom/broadcastchannel/BroadcastChannel.h | 4 +- .../BroadcastChannelChild.cpp | 4 +- dom/broadcastchannel/BroadcastChannelChild.h | 5 +- .../BroadcastChannelParent.cpp | 13 +- dom/broadcastchannel/BroadcastChannelParent.h | 7 +- .../BroadcastChannelService.cpp | 13 +- .../BroadcastChannelService.h | 3 +- dom/broadcastchannel/moz.build | 1 + dom/broadcastchannel/tests/blank.html | 2 + dom/broadcastchannel/tests/chrome.ini | 5 + ...est_broadcastchannel_private_browsing.html | 118 ++++++++++++++++++ ipc/glue/BackgroundChildImpl.cpp | 5 +- ipc/glue/BackgroundChildImpl.h | 3 +- ipc/glue/BackgroundParentImpl.cpp | 8 +- ipc/glue/BackgroundParentImpl.h | 6 +- ipc/glue/PBackground.ipdl | 3 +- 17 files changed, 192 insertions(+), 35 deletions(-) create mode 100644 dom/broadcastchannel/tests/blank.html create mode 100644 dom/broadcastchannel/tests/chrome.ini create mode 100644 dom/broadcastchannel/tests/test_broadcastchannel_private_browsing.html diff --git a/dom/broadcastchannel/BroadcastChannel.cpp b/dom/broadcastchannel/BroadcastChannel.cpp index c7dfb0170a06..8ea52e231432 100644 --- a/dom/broadcastchannel/BroadcastChannel.cpp +++ b/dom/broadcastchannel/BroadcastChannel.cpp @@ -126,11 +126,13 @@ class InitializeRunnable final : public WorkerMainThreadRunnable { public: InitializeRunnable(WorkerPrivate* aWorkerPrivate, nsAString& aOrigin, - PrincipalInfo& aPrincipalInfo, ErrorResult& aRv) + PrincipalInfo& aPrincipalInfo, bool& aPrivateBrowsing, + ErrorResult& aRv) : WorkerMainThreadRunnable(aWorkerPrivate) , mWorkerPrivate(GetCurrentThreadWorkerPrivate()) , mOrigin(aOrigin) , mPrincipalInfo(aPrincipalInfo) + , mPrivateBrowsing(aPrivateBrowsing) , mRv(aRv) { MOZ_ASSERT(mWorkerPrivate); @@ -180,8 +182,10 @@ public: } nsIDocument* doc = window->GetExtantDoc(); - // No bfcache when BroadcastChannel is used. if (doc) { + mPrivateBrowsing = nsContentUtils::IsInPrivateBrowsing(doc); + + // No bfcache when BroadcastChannel is used. doc->DisallowBFCaching(); } @@ -192,6 +196,7 @@ private: WorkerPrivate* mWorkerPrivate; nsAString& mOrigin; PrincipalInfo& mPrincipalInfo; + bool& mPrivateBrowsing; ErrorResult& mRv; }; @@ -396,12 +401,14 @@ BroadcastChannel::IsEnabled(JSContext* aCx, JSObject* aGlobal) BroadcastChannel::BroadcastChannel(nsPIDOMWindow* aWindow, const PrincipalInfo& aPrincipalInfo, const nsAString& aOrigin, - const nsAString& aChannel) + const nsAString& aChannel, + bool aPrivateBrowsing) : DOMEventTargetHelper(aWindow) , mWorkerFeature(nullptr) , mPrincipalInfo(new PrincipalInfo(aPrincipalInfo)) , mOrigin(aOrigin) , mChannel(aChannel) + , mPrivateBrowsing(aPrivateBrowsing) , mIsKeptAlive(false) , mInnerID(0) , mState(StateActive) @@ -431,6 +438,7 @@ BroadcastChannel::Constructor(const GlobalObject& aGlobal, nsAutoString origin; PrincipalInfo principalInfo; + bool privateBrowsing = false; WorkerPrivate* workerPrivate = nullptr; if (NS_IsMainThread()) { @@ -469,8 +477,10 @@ BroadcastChannel::Constructor(const GlobalObject& aGlobal, } nsIDocument* doc = window->GetExtantDoc(); - // No bfcache when BroadcastChannel is used. if (doc) { + privateBrowsing = nsContentUtils::IsInPrivateBrowsing(doc); + + // No bfcache when BroadcastChannel is used. doc->DisallowBFCaching(); } } else { @@ -479,7 +489,8 @@ BroadcastChannel::Constructor(const GlobalObject& aGlobal, MOZ_ASSERT(workerPrivate); nsRefPtr runnable = - new InitializeRunnable(workerPrivate, origin, principalInfo, aRv); + new InitializeRunnable(workerPrivate, origin, principalInfo, + privateBrowsing, aRv); runnable->Dispatch(cx); } @@ -488,7 +499,8 @@ BroadcastChannel::Constructor(const GlobalObject& aGlobal, } nsRefPtr bc = - new BroadcastChannel(window, principalInfo, origin, aChannel); + new BroadcastChannel(window, principalInfo, origin, aChannel, + privateBrowsing); // Register this component to PBackground. PBackgroundChild* actor = BackgroundChild::GetForCurrentThread(); @@ -614,7 +626,8 @@ BroadcastChannel::ActorCreated(PBackgroundChild* aActor) } PBroadcastChannelChild* actor = - aActor->SendPBroadcastChannelConstructor(*mPrincipalInfo, mOrigin, mChannel); + aActor->SendPBroadcastChannelConstructor(*mPrincipalInfo, mOrigin, mChannel, + mPrivateBrowsing); mActor = static_cast(actor); MOZ_ASSERT(mActor); diff --git a/dom/broadcastchannel/BroadcastChannel.h b/dom/broadcastchannel/BroadcastChannel.h index 33a25c126fcd..11d0c54b3475 100644 --- a/dom/broadcastchannel/BroadcastChannel.h +++ b/dom/broadcastchannel/BroadcastChannel.h @@ -92,7 +92,8 @@ private: BroadcastChannel(nsPIDOMWindow* aWindow, const PrincipalInfo& aPrincipalInfo, const nsAString& aOrigin, - const nsAString& aChannel); + const nsAString& aChannel, + bool aPrivateBrowsing); ~BroadcastChannel(); @@ -112,6 +113,7 @@ private: nsString mOrigin; nsString mChannel; + bool mPrivateBrowsing; bool mIsKeptAlive; diff --git a/dom/broadcastchannel/BroadcastChannelChild.cpp b/dom/broadcastchannel/BroadcastChannelChild.cpp index 3bd8cf3ba7a8..88a457ca74c7 100644 --- a/dom/broadcastchannel/BroadcastChannelChild.cpp +++ b/dom/broadcastchannel/BroadcastChannelChild.cpp @@ -25,10 +25,8 @@ namespace dom { using namespace workers; -BroadcastChannelChild::BroadcastChannelChild(const nsAString& aOrigin, - const nsAString& aChannel) +BroadcastChannelChild::BroadcastChannelChild(const nsAString& aOrigin) : mOrigin(aOrigin) - , mChannel(aChannel) , mActorDestroyed(false) { } diff --git a/dom/broadcastchannel/BroadcastChannelChild.h b/dom/broadcastchannel/BroadcastChannelChild.h index d511f15052f8..ffd97490506e 100644 --- a/dom/broadcastchannel/BroadcastChannelChild.h +++ b/dom/broadcastchannel/BroadcastChannelChild.h @@ -37,9 +37,7 @@ public: } private: - BroadcastChannelChild(const nsAString& aOrigin, - const nsAString& aChannel); - + explicit BroadcastChannelChild(const nsAString& aOrigin); ~BroadcastChannelChild(); virtual void ActorDestroy(ActorDestroyReason aWhy) override; @@ -49,7 +47,6 @@ private: BroadcastChannel* mBC; nsString mOrigin; - nsString mChannel; bool mActorDestroyed; }; diff --git a/dom/broadcastchannel/BroadcastChannelParent.cpp b/dom/broadcastchannel/BroadcastChannelParent.cpp index b0550cf16126..7a1740dceaad 100644 --- a/dom/broadcastchannel/BroadcastChannelParent.cpp +++ b/dom/broadcastchannel/BroadcastChannelParent.cpp @@ -18,10 +18,12 @@ namespace dom { BroadcastChannelParent::BroadcastChannelParent( const nsAString& aOrigin, - const nsAString& aChannel) + const nsAString& aChannel, + bool aPrivateBrowsing) : mService(BroadcastChannelService::GetOrCreate()) , mOrigin(aOrigin) , mChannel(aChannel) + , mPrivateBrowsing(aPrivateBrowsing) { AssertIsOnBackgroundThread(); mService->RegisterActor(this); @@ -41,7 +43,7 @@ BroadcastChannelParent::RecvPostMessage(const ClonedMessageData& aData) return false; } - mService->PostMessage(this, aData, mOrigin, mChannel); + mService->PostMessage(this, aData, mOrigin, mChannel, mPrivateBrowsing); return true; } @@ -77,11 +79,14 @@ BroadcastChannelParent::ActorDestroy(ActorDestroyReason aWhy) void BroadcastChannelParent::CheckAndDeliver(const ClonedMessageData& aData, const nsString& aOrigin, - const nsString& aChannel) + const nsString& aChannel, + bool aPrivateBrowsing) { AssertIsOnBackgroundThread(); - if (aOrigin == mOrigin && aChannel == mChannel) { + if (aOrigin == mOrigin && + aChannel == mChannel && + aPrivateBrowsing == mPrivateBrowsing) { // We need to duplicate data only if we have blobs or if the manager of // them is different than the manager of this parent actor. if (aData.blobsParent().IsEmpty() || diff --git a/dom/broadcastchannel/BroadcastChannelParent.h b/dom/broadcastchannel/BroadcastChannelParent.h index d0b47417df0c..6ab3bb9637de 100644 --- a/dom/broadcastchannel/BroadcastChannelParent.h +++ b/dom/broadcastchannel/BroadcastChannelParent.h @@ -24,11 +24,13 @@ class BroadcastChannelParent final : public PBroadcastChannelParent public: void CheckAndDeliver(const ClonedMessageData& aData, const nsString& aOrigin, - const nsString& aChannel); + const nsString& aChannel, + bool aPrivateBrowsing); private: BroadcastChannelParent(const nsAString& aOrigin, - const nsAString& aChannel); + const nsAString& aChannel, + bool aPrivateBrowsing); ~BroadcastChannelParent(); virtual bool @@ -41,6 +43,7 @@ private: nsRefPtr mService; nsString mOrigin; nsString mChannel; + bool mPrivateBrowsing; }; } // dom namespace diff --git a/dom/broadcastchannel/BroadcastChannelService.cpp b/dom/broadcastchannel/BroadcastChannelService.cpp index 8313ae7e80d7..7b73c72abdc2 100644 --- a/dom/broadcastchannel/BroadcastChannelService.cpp +++ b/dom/broadcastchannel/BroadcastChannelService.cpp @@ -83,11 +83,13 @@ struct MOZ_STACK_CLASS PostMessageData final PostMessageData(BroadcastChannelParent* aParent, const ClonedMessageData& aData, const nsAString& aOrigin, - const nsAString& aChannel) + const nsAString& aChannel, + bool aPrivateBrowsing) : mParent(aParent) , mData(aData) , mOrigin(aOrigin) , mChannel(aChannel) + , mPrivateBrowsing(aPrivateBrowsing) { MOZ_ASSERT(aParent); MOZ_COUNT_CTOR(PostMessageData); @@ -116,6 +118,7 @@ struct MOZ_STACK_CLASS PostMessageData final nsTArray> mFiles; const nsString mOrigin; const nsString mChannel; + bool mPrivateBrowsing; }; PLDHashOperator @@ -128,7 +131,8 @@ PostMessageEnumerator(nsPtrHashKey* aKey, void* aPtr) MOZ_ASSERT(parent); if (parent != data->mParent) { - parent->CheckAndDeliver(data->mData, data->mOrigin, data->mChannel); + parent->CheckAndDeliver(data->mData, data->mOrigin, data->mChannel, + data->mPrivateBrowsing); } return PL_DHASH_NEXT; @@ -140,13 +144,14 @@ void BroadcastChannelService::PostMessage(BroadcastChannelParent* aParent, const ClonedMessageData& aData, const nsAString& aOrigin, - const nsAString& aChannel) + const nsAString& aChannel, + bool aPrivateBrowsing) { AssertIsOnBackgroundThread(); MOZ_ASSERT(aParent); MOZ_ASSERT(mAgents.Contains(aParent)); - PostMessageData data(aParent, aData, aOrigin, aChannel); + PostMessageData data(aParent, aData, aOrigin, aChannel, aPrivateBrowsing); mAgents.EnumerateEntries(PostMessageEnumerator, &data); } diff --git a/dom/broadcastchannel/BroadcastChannelService.h b/dom/broadcastchannel/BroadcastChannelService.h index 1955c85ac49e..3864e7860799 100644 --- a/dom/broadcastchannel/BroadcastChannelService.h +++ b/dom/broadcastchannel/BroadcastChannelService.h @@ -32,7 +32,8 @@ public: void PostMessage(BroadcastChannelParent* aParent, const ClonedMessageData& aData, const nsAString& aOrigin, - const nsAString& aChannel); + const nsAString& aChannel, + bool aPrivateBrowsing); private: BroadcastChannelService(); diff --git a/dom/broadcastchannel/moz.build b/dom/broadcastchannel/moz.build index 6ff7e04872cf..c421995eb8a1 100644 --- a/dom/broadcastchannel/moz.build +++ b/dom/broadcastchannel/moz.build @@ -24,6 +24,7 @@ LOCAL_INCLUDES += [ ] MOCHITEST_MANIFESTS += ['tests/mochitest.ini'] +MOCHITEST_CHROME_MANIFESTS += ['tests/chrome.ini'] include('/ipc/chromium/chromium-config.mozbuild') diff --git a/dom/broadcastchannel/tests/blank.html b/dom/broadcastchannel/tests/blank.html new file mode 100644 index 000000000000..358db717dd6e --- /dev/null +++ b/dom/broadcastchannel/tests/blank.html @@ -0,0 +1,2 @@ + + diff --git a/dom/broadcastchannel/tests/chrome.ini b/dom/broadcastchannel/tests/chrome.ini new file mode 100644 index 000000000000..c8ae5fda6c27 --- /dev/null +++ b/dom/broadcastchannel/tests/chrome.ini @@ -0,0 +1,5 @@ +[DEFAULT] +support-files = + blank.html + +[test_broadcastchannel_private_browsing.html] diff --git a/dom/broadcastchannel/tests/test_broadcastchannel_private_browsing.html b/dom/broadcastchannel/tests/test_broadcastchannel_private_browsing.html new file mode 100644 index 000000000000..9c89f45ca76b --- /dev/null +++ b/dom/broadcastchannel/tests/test_broadcastchannel_private_browsing.html @@ -0,0 +1,118 @@ + + + Test for BroadcastChannel - Private Browsing + + + + + + + + + + diff --git a/ipc/glue/BackgroundChildImpl.cpp b/ipc/glue/BackgroundChildImpl.cpp index 7d4c61f2f214..a0dd36aeb2a6 100644 --- a/ipc/glue/BackgroundChildImpl.cpp +++ b/ipc/glue/BackgroundChildImpl.cpp @@ -220,10 +220,11 @@ BackgroundChildImpl::DeallocPVsyncChild(PVsyncChild* aActor) dom::PBroadcastChannelChild* BackgroundChildImpl::AllocPBroadcastChannelChild(const PrincipalInfo& aPrincipalInfo, const nsString& aOrigin, - const nsString& aChannel) + const nsString& aChannel, + const bool& aPrivateBrowsing) { nsRefPtr agent = - new dom::BroadcastChannelChild(aOrigin, aChannel); + new dom::BroadcastChannelChild(aOrigin); return agent.forget().take(); } diff --git a/ipc/glue/BackgroundChildImpl.h b/ipc/glue/BackgroundChildImpl.h index b52a7ff75f7a..b28e238ea408 100644 --- a/ipc/glue/BackgroundChildImpl.h +++ b/ipc/glue/BackgroundChildImpl.h @@ -86,7 +86,8 @@ protected: virtual PBroadcastChannelChild* AllocPBroadcastChannelChild(const PrincipalInfo& aPrincipalInfo, const nsString& aOrigin, - const nsString& aChannel) override; + const nsString& aChannel, + const bool& aPrivateBrowsing) override; virtual bool DeallocPBroadcastChannelChild(PBroadcastChannelChild* aActor) override; diff --git a/ipc/glue/BackgroundParentImpl.cpp b/ipc/glue/BackgroundParentImpl.cpp index 09417ea1f225..035ab6277ee2 100644 --- a/ipc/glue/BackgroundParentImpl.cpp +++ b/ipc/glue/BackgroundParentImpl.cpp @@ -255,12 +255,13 @@ mozilla::dom::PBroadcastChannelParent* BackgroundParentImpl::AllocPBroadcastChannelParent( const PrincipalInfo& aPrincipalInfo, const nsString& aOrigin, - const nsString& aChannel) + const nsString& aChannel, + const bool& aPrivateBrowsing) { AssertIsInMainProcess(); AssertIsOnBackgroundThread(); - return new BroadcastChannelParent(aOrigin, aChannel); + return new BroadcastChannelParent(aOrigin, aChannel, aPrivateBrowsing); } namespace { @@ -328,7 +329,8 @@ BackgroundParentImpl::RecvPBroadcastChannelConstructor( PBroadcastChannelParent* actor, const PrincipalInfo& aPrincipalInfo, const nsString& aOrigin, - const nsString& aChannel) + const nsString& aChannel, + const bool& aPrivateBrowsing) { AssertIsInMainProcess(); AssertIsOnBackgroundThread(); diff --git a/ipc/glue/BackgroundParentImpl.h b/ipc/glue/BackgroundParentImpl.h index 6f36a0227f98..e2a2032421ed 100644 --- a/ipc/glue/BackgroundParentImpl.h +++ b/ipc/glue/BackgroundParentImpl.h @@ -73,13 +73,15 @@ protected: virtual PBroadcastChannelParent* AllocPBroadcastChannelParent(const PrincipalInfo& aPrincipalInfo, const nsString& aOrigin, - const nsString& aChannel) override; + const nsString& aChannel, + const bool& aPrivateBrowsing) override; virtual bool RecvPBroadcastChannelConstructor(PBroadcastChannelParent* actor, const PrincipalInfo& aPrincipalInfo, const nsString& origin, - const nsString& channel) override; + const nsString& channel, + const bool& aPrivateBrowsing) override; virtual bool DeallocPBroadcastChannelParent(PBroadcastChannelParent* aActor) override; diff --git a/ipc/glue/PBackground.ipdl b/ipc/glue/PBackground.ipdl index bdbc7ea4b68c..db28baa49076 100644 --- a/ipc/glue/PBackground.ipdl +++ b/ipc/glue/PBackground.ipdl @@ -46,7 +46,8 @@ parent: PVsync(); PMedia(); - PBroadcastChannel(PrincipalInfo pInfo, nsString origin, nsString channel); + PBroadcastChannel(PrincipalInfo pInfo, nsString origin, nsString channel, + bool privateBrowsing); RegisterServiceWorker(ServiceWorkerRegistrationData data); UnregisterServiceWorker(PrincipalInfo principalInfo,