2015-01-15 19:58:40 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "BroadcastChannel.h"
|
|
|
|
#include "BroadcastChannelChild.h"
|
|
|
|
#include "mozilla/dom/BroadcastChannelBinding.h"
|
2015-01-15 19:58:41 +03:00
|
|
|
#include "mozilla/dom/Navigator.h"
|
2015-01-15 19:58:41 +03:00
|
|
|
#include "mozilla/dom/StructuredCloneUtils.h"
|
2015-01-15 19:58:40 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
2015-01-15 19:58:41 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2015-01-15 19:58:40 +03:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerRunnable.h"
|
|
|
|
|
|
|
|
#include "nsIAppsService.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
using namespace workers;
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
class BroadcastChannelMessage MOZ_FINAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(BroadcastChannelMessage)
|
|
|
|
|
|
|
|
JSAutoStructuredCloneBuffer mBuffer;
|
|
|
|
StructuredCloneClosure mClosure;
|
|
|
|
|
|
|
|
BroadcastChannelMessage()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~BroadcastChannelMessage()
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void
|
|
|
|
GetOrigin(nsIPrincipal* aPrincipal, nsAString& aOrigin, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
|
|
|
|
uint16_t appStatus = aPrincipal->GetAppStatus();
|
|
|
|
|
|
|
|
if (appStatus == nsIPrincipal::APP_STATUS_NOT_INSTALLED) {
|
|
|
|
nsAutoString tmp;
|
|
|
|
aRv = nsContentUtils::GetUTFOrigin(aPrincipal, tmp);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aOrigin = tmp;
|
2015-01-23 20:32:23 +03:00
|
|
|
if (aOrigin.EqualsASCII("null")) {
|
|
|
|
nsCOMPtr<nsIURI> 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);
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t appId = aPrincipal->GetAppId();
|
|
|
|
|
|
|
|
// If we are in "app code", use manifest URL as unique origin since
|
|
|
|
// multiple apps can share the same origin but not same broadcast messages.
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIAppsService> appsService =
|
|
|
|
do_GetService("@mozilla.org/AppsService;1", &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
appsService->GetManifestURLByLocalId(appId, aOrigin);
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
nsIPrincipal*
|
|
|
|
GetPrincipalFromWorkerPrivate(WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
nsIPrincipal* principal = aWorkerPrivate->GetPrincipal();
|
|
|
|
if (principal) {
|
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk up to our containing page
|
|
|
|
WorkerPrivate* wp = aWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return wp->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
class InitializeRunnable MOZ_FINAL : public WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InitializeRunnable(WorkerPrivate* aWorkerPrivate, nsAString& aOrigin,
|
|
|
|
PrincipalInfo& aPrincipalInfo, ErrorResult& aRv)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate)
|
|
|
|
, mWorkerPrivate(GetCurrentThreadWorkerPrivate())
|
|
|
|
, mOrigin(aOrigin)
|
|
|
|
, mPrincipalInfo(aPrincipalInfo)
|
|
|
|
, mRv(aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainThreadRun() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-15 19:58:41 +03:00
|
|
|
|
|
|
|
nsIPrincipal* principal = GetPrincipalFromWorkerPrivate(mWorkerPrivate);
|
|
|
|
if (!principal) {
|
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
bool isNullPrincipal;
|
|
|
|
mRv = principal->GetIsNullPrincipal(&isNullPrincipal);
|
|
|
|
if (NS_WARN_IF(mRv.Failed())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(isNullPrincipal)) {
|
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRv = PrincipalToPrincipalInfo(principal, &mPrincipalInfo);
|
|
|
|
if (NS_WARN_IF(mRv.Failed())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOrigin(principal, mOrigin, mRv);
|
|
|
|
if (NS_WARN_IF(mRv.Failed())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
// Walk up to our containing page
|
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Window doesn't exist for some kind of workers (eg: SharedWorkers)
|
|
|
|
nsPIDOMWindow* window = wp->GetWindow();
|
|
|
|
if (!window) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
|
|
|
// No bfcache when BroadcastChannel is used.
|
|
|
|
if (doc) {
|
|
|
|
doc->DisallowBFCaching();
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsAString& mOrigin;
|
|
|
|
PrincipalInfo& mPrincipalInfo;
|
|
|
|
ErrorResult& mRv;
|
|
|
|
};
|
|
|
|
|
2015-02-06 14:06:35 +03:00
|
|
|
class BCPostMessageRunnable MOZ_FINAL : public nsICancelableRunnable
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2015-02-06 14:06:35 +03:00
|
|
|
BCPostMessageRunnable(BroadcastChannelChild* aActor,
|
|
|
|
BroadcastChannelMessage* aData)
|
2015-01-15 19:58:40 +03:00
|
|
|
: mActor(aActor)
|
2015-01-15 19:58:41 +03:00
|
|
|
, mData(aData)
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
}
|
|
|
|
|
2015-01-21 18:44:12 +03:00
|
|
|
NS_IMETHODIMP Run() MOZ_OVERRIDE
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mActor);
|
2015-01-15 19:58:41 +03:00
|
|
|
if (mActor->IsActorDestroyed()) {
|
|
|
|
return NS_OK;
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
2015-01-15 19:58:41 +03:00
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
ClonedMessageData message;
|
2015-01-15 19:58:41 +03:00
|
|
|
|
|
|
|
SerializedStructuredCloneBuffer& buffer = message.data();
|
|
|
|
buffer.data = mData->mBuffer.data();
|
|
|
|
buffer.dataLength = mData->mBuffer.nbytes();
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
PBackgroundChild* backgroundManager = mActor->Manager();
|
|
|
|
MOZ_ASSERT(backgroundManager);
|
|
|
|
|
|
|
|
const nsTArray<nsRefPtr<File>>& blobs = mData->mClosure.mBlobs;
|
|
|
|
|
|
|
|
if (!blobs.IsEmpty()) {
|
|
|
|
message.blobsChild().SetCapacity(blobs.Length());
|
|
|
|
|
|
|
|
for (uint32_t i = 0, len = blobs.Length(); i < len; ++i) {
|
|
|
|
PBlobChild* blobChild =
|
|
|
|
BackgroundChild::GetOrCreateActorForBlob(backgroundManager, blobs[i]);
|
|
|
|
MOZ_ASSERT(blobChild);
|
|
|
|
|
|
|
|
message.blobsChild().AppendElement(blobChild);
|
|
|
|
}
|
2015-01-15 19:58:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mActor->SendPostMessage(message);
|
2015-01-15 19:58:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-21 18:44:12 +03:00
|
|
|
NS_IMETHODIMP Cancel() MOZ_OVERRIDE
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
mActor = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-02-06 14:06:35 +03:00
|
|
|
~BCPostMessageRunnable() {}
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
nsRefPtr<BroadcastChannelChild> mActor;
|
2015-01-15 19:58:41 +03:00
|
|
|
nsRefPtr<BroadcastChannelMessage> mData;
|
2015-01-15 19:58:40 +03:00
|
|
|
};
|
|
|
|
|
2015-02-06 14:06:35 +03:00
|
|
|
NS_IMPL_ISUPPORTS(BCPostMessageRunnable, nsICancelableRunnable, nsIRunnable)
|
2015-01-15 19:58:40 +03:00
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
class CloseRunnable MOZ_FINAL : public nsICancelableRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
explicit CloseRunnable(BroadcastChannel* aBC)
|
2015-01-15 19:58:42 +03:00
|
|
|
: mBC(aBC)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mBC);
|
|
|
|
}
|
|
|
|
|
2015-01-21 18:44:12 +03:00
|
|
|
NS_IMETHODIMP Run() MOZ_OVERRIDE
|
2015-01-15 19:58:42 +03:00
|
|
|
{
|
|
|
|
mBC->Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-21 18:44:12 +03:00
|
|
|
NS_IMETHODIMP Cancel() MOZ_OVERRIDE
|
2015-01-15 19:58:42 +03:00
|
|
|
{
|
|
|
|
mBC = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~CloseRunnable() {}
|
|
|
|
|
|
|
|
nsRefPtr<BroadcastChannel> mBC;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(CloseRunnable, nsICancelableRunnable, nsIRunnable)
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
class TeardownRunnable MOZ_FINAL : public nsICancelableRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
explicit TeardownRunnable(BroadcastChannelChild* aActor)
|
2015-01-15 19:58:40 +03:00
|
|
|
: mActor(aActor)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
}
|
|
|
|
|
2015-01-21 18:44:12 +03:00
|
|
|
NS_IMETHODIMP Run() MOZ_OVERRIDE
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
if (!mActor->IsActorDestroyed()) {
|
|
|
|
mActor->SendClose();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-21 18:44:12 +03:00
|
|
|
NS_IMETHODIMP Cancel() MOZ_OVERRIDE
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
mActor = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~TeardownRunnable() {}
|
|
|
|
|
|
|
|
nsRefPtr<BroadcastChannelChild> mActor;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(TeardownRunnable, nsICancelableRunnable, nsIRunnable)
|
|
|
|
|
|
|
|
class BroadcastChannelFeature MOZ_FINAL : public workers::WorkerFeature
|
|
|
|
{
|
|
|
|
BroadcastChannel* mChannel;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit BroadcastChannelFeature(BroadcastChannel* aChannel)
|
|
|
|
: mChannel(aChannel)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(BroadcastChannelFeature);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Notify(JSContext* aCx, workers::Status aStatus) MOZ_OVERRIDE
|
|
|
|
{
|
2015-02-20 00:21:18 +03:00
|
|
|
if (aStatus >= Closing) {
|
2015-01-15 19:58:40 +03:00
|
|
|
mChannel->Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~BroadcastChannelFeature()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(BroadcastChannelFeature);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
class PrefEnabledRunnable MOZ_FINAL : public WorkerMainThreadRunnable
|
2015-01-15 19:58:41 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-01-15 19:58:42 +03:00
|
|
|
explicit PrefEnabledRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate)
|
|
|
|
, mEnabled(false)
|
|
|
|
{ }
|
2015-01-15 19:58:41 +03:00
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
bool MainThreadRun() MOZ_OVERRIDE
|
2015-01-15 19:58:41 +03:00
|
|
|
{
|
2015-01-15 19:58:42 +03:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
mEnabled = Preferences::GetBool("dom.broadcastChannel.enabled", false);
|
|
|
|
return true;
|
2015-01-15 19:58:41 +03:00
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
bool IsEnabled() const
|
2015-01-15 19:58:41 +03:00
|
|
|
{
|
2015-01-15 19:58:42 +03:00
|
|
|
return mEnabled;
|
2015-01-15 19:58:41 +03:00
|
|
|
}
|
2015-01-15 19:58:42 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool mEnabled;
|
2015-01-15 19:58:41 +03:00
|
|
|
};
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
/* static */ bool
|
|
|
|
BroadcastChannel::IsEnabled(JSContext* aCx, JSObject* aGlobal)
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
2015-01-15 19:58:42 +03:00
|
|
|
return Preferences::GetBool("dom.broadcastChannel.enabled", false);
|
2015-01-15 19:58:41 +03:00
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
2015-01-15 19:58:41 +03:00
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
nsRefPtr<PrefEnabledRunnable> runnable =
|
|
|
|
new PrefEnabledRunnable(workerPrivate);
|
|
|
|
runnable->Dispatch(workerPrivate->GetJSContext());
|
2015-01-15 19:58:41 +03:00
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
return runnable->IsEnabled();
|
2015-01-15 19:58:41 +03:00
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
BroadcastChannel::BroadcastChannel(nsPIDOMWindow* aWindow,
|
|
|
|
const PrincipalInfo& aPrincipalInfo,
|
|
|
|
const nsAString& aOrigin,
|
|
|
|
const nsAString& aChannel)
|
|
|
|
: DOMEventTargetHelper(aWindow)
|
|
|
|
, mWorkerFeature(nullptr)
|
2015-01-15 19:58:42 +03:00
|
|
|
, mPrincipalInfo(new PrincipalInfo(aPrincipalInfo))
|
2015-01-15 19:58:40 +03:00
|
|
|
, mOrigin(aOrigin)
|
|
|
|
, mChannel(aChannel)
|
|
|
|
, mIsKeptAlive(false)
|
|
|
|
, mInnerID(0)
|
2015-01-15 19:58:40 +03:00
|
|
|
, mState(StateActive)
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
// Window can be null in workers
|
|
|
|
}
|
|
|
|
|
|
|
|
BroadcastChannel::~BroadcastChannel()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
MOZ_ASSERT(!mWorkerFeature);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
BroadcastChannel::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return BroadcastChannelBinding::Wrap(aCx, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<BroadcastChannel>
|
|
|
|
BroadcastChannel::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aChannel,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
// Window is null in workers.
|
|
|
|
|
|
|
|
nsAutoString origin;
|
|
|
|
PrincipalInfo principalInfo;
|
|
|
|
WorkerPrivate* workerPrivate = nullptr;
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIGlobalObject> incumbent = mozilla::dom::GetIncumbentGlobal();
|
|
|
|
|
|
|
|
if (!incumbent) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal* principal = incumbent->PrincipalOrNull();
|
|
|
|
if (!principal) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNullPrincipal;
|
|
|
|
aRv = principal->GetIsNullPrincipal(&isNullPrincipal);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(isNullPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOrigin(principal, origin, aRv);
|
2015-01-23 20:32:23 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
aRv = PrincipalToPrincipalInfo(principal, &principalInfo);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
|
|
|
// No bfcache when BroadcastChannel is used.
|
|
|
|
if (doc) {
|
|
|
|
doc->DisallowBFCaching();
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
} else {
|
|
|
|
JSContext* cx = aGlobal.Context();
|
|
|
|
workerPrivate = GetWorkerPrivateFromContext(cx);
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
nsRefPtr<InitializeRunnable> runnable =
|
|
|
|
new InitializeRunnable(workerPrivate, origin, principalInfo, aRv);
|
|
|
|
runnable->Dispatch(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<BroadcastChannel> bc =
|
|
|
|
new BroadcastChannel(window, principalInfo, origin, aChannel);
|
|
|
|
|
|
|
|
// Register this component to PBackground.
|
2015-01-15 19:58:42 +03:00
|
|
|
PBackgroundChild* actor = BackgroundChild::GetForCurrentThread();
|
|
|
|
if (actor) {
|
|
|
|
bc->ActorCreated(actor);
|
|
|
|
} else {
|
|
|
|
BackgroundChild::GetOrCreateForCurrentThread(bc);
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
if (!workerPrivate) {
|
|
|
|
MOZ_ASSERT(window);
|
|
|
|
MOZ_ASSERT(window->IsInnerWindow());
|
|
|
|
bc->mInnerID = window->WindowID();
|
|
|
|
|
|
|
|
// Register as observer for inner-window-destroyed.
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(bc, "inner-window-destroyed", false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bc->mWorkerFeature = new BroadcastChannelFeature(bc);
|
|
|
|
JSContext* cx = workerPrivate->GetJSContext();
|
|
|
|
if (NS_WARN_IF(!workerPrivate->AddFeature(cx, bc->mWorkerFeature))) {
|
|
|
|
NS_WARNING("Failed to register the BroadcastChannel worker feature.");
|
2015-03-09 16:51:47 +03:00
|
|
|
bc->mWorkerFeature = nullptr;
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2015-01-15 19:58:40 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bc.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-15 19:58:41 +03:00
|
|
|
BroadcastChannel::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
|
|
|
ErrorResult& aRv)
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
if (mState != StateActive) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
PostMessageInternal(aCx, aMessage, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannel::PostMessageInternal(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aMessage,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<BroadcastChannelMessage> data = new BroadcastChannelMessage();
|
|
|
|
|
|
|
|
if (!WriteStructuredClone(aCx, aMessage, data->mBuffer, data->mClosure)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
const nsTArray<nsRefPtr<File>>& blobs = data->mClosure.mBlobs;
|
|
|
|
for (uint32_t i = 0, len = blobs.Length(); i < len; ++i) {
|
|
|
|
if (!blobs[i]->Impl()->MayBeClonedToOtherThreads()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
PostMessageData(data);
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-15 19:58:41 +03:00
|
|
|
BroadcastChannel::PostMessageData(BroadcastChannelMessage* aData)
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
if (mActor) {
|
2015-02-06 14:06:35 +03:00
|
|
|
nsRefPtr<BCPostMessageRunnable> runnable =
|
|
|
|
new BCPostMessageRunnable(mActor, aData);
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(NS_DispatchToCurrentThread(runnable))) {
|
|
|
|
NS_WARNING("Failed to dispatch to the current thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
mPendingMessages.AppendElement(aData);
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
void
|
|
|
|
BroadcastChannel::Close()
|
|
|
|
{
|
|
|
|
if (mState != StateActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPendingMessages.IsEmpty()) {
|
2015-01-15 19:58:42 +03:00
|
|
|
// We cannot call Shutdown() immediatelly because we could have some
|
|
|
|
// postMessage runnable already dispatched. Instead, we change the state to
|
|
|
|
// StateClosed and we shutdown the actor asynchrounsly.
|
|
|
|
|
|
|
|
mState = StateClosed;
|
|
|
|
nsRefPtr<CloseRunnable> runnable = new CloseRunnable(this);
|
|
|
|
|
|
|
|
if (NS_FAILED(NS_DispatchToCurrentThread(runnable))) {
|
|
|
|
NS_WARNING("Failed to dispatch to the current thread!");
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(!mActor);
|
|
|
|
mState = StateClosing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
void
|
|
|
|
BroadcastChannel::ActorFailed()
|
|
|
|
{
|
|
|
|
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-15 19:58:42 +03:00
|
|
|
BroadcastChannel::ActorCreated(PBackgroundChild* aActor)
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aActor);
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
if (mState == StateClosed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
PBroadcastChannelChild* actor =
|
2015-01-15 19:58:42 +03:00
|
|
|
aActor->SendPBroadcastChannelConstructor(*mPrincipalInfo, mOrigin, mChannel);
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
mActor = static_cast<BroadcastChannelChild*>(actor);
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
mActor->SetParent(this);
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
// Flush pending messages.
|
|
|
|
for (uint32_t i = 0; i < mPendingMessages.Length(); ++i) {
|
2015-01-15 19:58:41 +03:00
|
|
|
PostMessageData(mPendingMessages[i]);
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mPendingMessages.Clear();
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
if (mState == StateClosing) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannel::Shutdown()
|
|
|
|
{
|
2015-01-15 19:58:40 +03:00
|
|
|
mState = StateClosed;
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
if (mWorkerFeature) {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
workerPrivate->RemoveFeature(workerPrivate->GetJSContext(), mWorkerFeature);
|
|
|
|
mWorkerFeature = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mActor) {
|
2015-01-15 19:58:41 +03:00
|
|
|
mActor->SetParent(nullptr);
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
nsRefPtr<TeardownRunnable> runnable = new TeardownRunnable(mActor);
|
|
|
|
NS_DispatchToCurrentThread(runnable);
|
|
|
|
|
|
|
|
mActor = nullptr;
|
|
|
|
}
|
2015-02-20 00:21:18 +03:00
|
|
|
|
|
|
|
// If shutdown() is called we have to release the reference if we still keep
|
|
|
|
// it.
|
|
|
|
if (mIsKeptAlive) {
|
|
|
|
mIsKeptAlive = false;
|
|
|
|
Release();
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
EventHandlerNonNull*
|
|
|
|
BroadcastChannel::GetOnmessage()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return GetEventHandler(nsGkAtoms::onmessage, EmptyString());
|
|
|
|
}
|
|
|
|
return GetEventHandler(nullptr, NS_LITERAL_STRING("message"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannel::SetOnmessage(EventHandlerNonNull* aCallback)
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
SetEventHandler(nsGkAtoms::onmessage, EmptyString(), aCallback);
|
|
|
|
} else {
|
|
|
|
SetEventHandler(nullptr, NS_LITERAL_STRING("message"), aCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannel::AddEventListener(const nsAString& aType,
|
|
|
|
EventListener* aCallback,
|
|
|
|
bool aCapture,
|
|
|
|
const dom::Nullable<bool>& aWantsUntrusted,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
DOMEventTargetHelper::AddEventListener(aType, aCallback, aCapture,
|
|
|
|
aWantsUntrusted, aRv);
|
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannel::RemoveEventListener(const nsAString& aType,
|
|
|
|
EventListener* aCallback,
|
|
|
|
bool aCapture,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
DOMEventTargetHelper::RemoveEventListener(aType, aCallback, aCapture, aRv);
|
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannel::UpdateMustKeepAlive()
|
|
|
|
{
|
|
|
|
bool toKeepAlive = HasListenersFor(NS_LITERAL_STRING("message"));
|
|
|
|
if (toKeepAlive == mIsKeptAlive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsKeptAlive = toKeepAlive;
|
|
|
|
|
|
|
|
if (toKeepAlive) {
|
|
|
|
AddRef();
|
|
|
|
} else {
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BroadcastChannel::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, "inner-window-destroyed"));
|
|
|
|
|
|
|
|
// If the window is destroyed we have to release the reference that we are
|
|
|
|
// keeping.
|
|
|
|
if (!mIsKeptAlive) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE(wrapper, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
uint64_t innerID;
|
|
|
|
nsresult rv = wrapper->GetData(&innerID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (innerID == mInnerID) {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this, "inner-window-destroyed");
|
|
|
|
}
|
|
|
|
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(BroadcastChannel)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BroadcastChannel,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BroadcastChannel,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
tmp->Shutdown();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BroadcastChannel)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIPCBackgroundChildCreateCallback)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(BroadcastChannel, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(BroadcastChannel, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|