2015-07-19 07:54:41 +00:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2015-09-30 12:22:08 +00:00
|
|
|
#include "StructuredCloneHolder.h"
|
2015-07-19 07:54:41 +00:00
|
|
|
|
2015-07-30 18:50:00 +00:00
|
|
|
#include "ImageContainer.h"
|
2015-08-18 21:55:21 +00:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2015-07-22 18:37:18 +00:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
2015-09-14 17:05:44 +00:00
|
|
|
#include "mozilla/dom/CryptoKey.h"
|
2016-03-20 10:56:10 +00:00
|
|
|
#include "mozilla/dom/Directory.h"
|
2016-04-12 12:51:04 +00:00
|
|
|
#include "mozilla/dom/DirectoryBinding.h"
|
2015-08-18 21:55:21 +00:00
|
|
|
#include "mozilla/dom/File.h"
|
|
|
|
#include "mozilla/dom/FileList.h"
|
2015-07-22 18:37:18 +00:00
|
|
|
#include "mozilla/dom/FileListBinding.h"
|
2016-01-07 19:30:36 +00:00
|
|
|
#include "mozilla/dom/FormData.h"
|
2015-07-30 18:50:00 +00:00
|
|
|
#include "mozilla/dom/ImageBitmap.h"
|
|
|
|
#include "mozilla/dom/ImageBitmapBinding.h"
|
2015-08-27 16:19:13 +00:00
|
|
|
#include "mozilla/dom/ImageData.h"
|
|
|
|
#include "mozilla/dom/ImageDataBinding.h"
|
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
|
|
|
#include "mozilla/dom/StructuredClone.h"
|
2015-08-18 21:55:21 +00:00
|
|
|
#include "mozilla/dom/MessagePort.h"
|
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
2015-10-12 03:21:03 +00:00
|
|
|
#include "mozilla/dom/OffscreenCanvas.h"
|
|
|
|
#include "mozilla/dom/OffscreenCanvasBinding.h"
|
2015-08-18 21:55:21 +00:00
|
|
|
#include "mozilla/dom/PMessagePort.h"
|
2015-07-22 18:37:18 +00:00
|
|
|
#include "mozilla/dom/StructuredCloneTags.h"
|
2015-09-14 17:05:44 +00:00
|
|
|
#include "mozilla/dom/SubtleCryptoBinding.h"
|
2015-08-18 21:55:21 +00:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2016-07-11 19:03:44 +00:00
|
|
|
#include "mozilla/dom/URLSearchParams.h"
|
2016-09-06 20:02:00 +00:00
|
|
|
#include "mozilla/dom/URLSearchParamsBinding.h"
|
2015-08-27 16:19:13 +00:00
|
|
|
#include "mozilla/dom/WebCryptoCommon.h"
|
2015-12-18 06:52:16 +00:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2015-08-27 16:19:13 +00:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
2015-09-14 17:05:44 +00:00
|
|
|
#include "mozilla/ipc/PBackgroundSharedTypes.h"
|
2015-08-27 16:19:13 +00:00
|
|
|
#include "MultipartBlobImpl.h"
|
|
|
|
#include "nsIRemoteBlob.h"
|
|
|
|
#include "nsQueryObject.h"
|
|
|
|
|
2015-09-14 17:05:44 +00:00
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
#include "mozilla/dom/RTCCertificate.h"
|
|
|
|
#include "mozilla/dom/RTCCertificateBinding.h"
|
|
|
|
#endif
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
using namespace mozilla::ipc;
|
2015-07-22 18:37:18 +00:00
|
|
|
|
2015-07-19 07:54:41 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
StructuredCloneCallbacksRead(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag, uint32_t aIndex,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomReadHandler(aCx, aReader, aTag, aIndex);
|
2015-07-19 07:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
StructuredCloneCallbacksWrite(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomWriteHandler(aCx, aWriter, aObj);
|
2015-07-19 07:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
StructuredCloneCallbacksReadTransfer(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData,
|
|
|
|
void* aClosure,
|
|
|
|
JS::MutableHandleObject aReturnObject)
|
|
|
|
{
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomReadTransferHandler(aCx, aReader, aTag, aContent,
|
|
|
|
aExtraData, aReturnObject);
|
2015-07-19 07:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
StructuredCloneCallbacksWriteTransfer(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
void* aClosure,
|
|
|
|
// Output:
|
|
|
|
uint32_t* aTag,
|
|
|
|
JS::TransferableOwnership* aOwnership,
|
|
|
|
void** aContent,
|
|
|
|
uint64_t* aExtraData)
|
|
|
|
{
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomWriteTransferHandler(aCx, aObj, aTag, aOwnership,
|
|
|
|
aContent, aExtraData);
|
2015-07-19 07:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StructuredCloneCallbacksFreeTransfer(uint32_t aTag,
|
|
|
|
JS::TransferableOwnership aOwnership,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomFreeTransferHandler(aTag, aOwnership, aContent,
|
|
|
|
aExtraData);
|
2015-07-19 07:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StructuredCloneCallbacksError(JSContext* aCx,
|
|
|
|
uint32_t aErrorId)
|
|
|
|
{
|
2015-07-24 11:12:51 +00:00
|
|
|
NS_WARNING("Failed to clone data.");
|
2015-07-19 07:54:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 10:04:20 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
const JSStructuredCloneCallbacks StructuredCloneHolder::sCallbacks = {
|
2015-07-19 07:54:41 +00:00
|
|
|
StructuredCloneCallbacksRead,
|
|
|
|
StructuredCloneCallbacksWrite,
|
|
|
|
StructuredCloneCallbacksError,
|
|
|
|
StructuredCloneCallbacksReadTransfer,
|
|
|
|
StructuredCloneCallbacksWriteTransfer,
|
|
|
|
StructuredCloneCallbacksFreeTransfer
|
|
|
|
};
|
|
|
|
|
2015-09-30 12:22:08 +00:00
|
|
|
// StructuredCloneHolderBase class
|
2015-07-22 18:37:18 +00:00
|
|
|
|
2016-07-21 13:29:42 +00:00
|
|
|
StructuredCloneHolderBase::StructuredCloneHolderBase(StructuredCloneScope aScope)
|
|
|
|
: mStructuredCloneScope(aScope)
|
2015-07-24 11:12:51 +00:00
|
|
|
#ifdef DEBUG
|
2016-07-21 13:29:42 +00:00
|
|
|
, mClearCalled(false)
|
2015-07-24 11:12:51 +00:00
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase::~StructuredCloneHolderBase()
|
2015-07-24 11:12:51 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2015-09-30 12:22:08 +00:00
|
|
|
MOZ_ASSERT(mClearCalled);
|
2015-07-24 11:12:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase::Clear()
|
2015-07-24 11:12:51 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2015-09-30 12:22:08 +00:00
|
|
|
mClearCalled = true;
|
2015-07-24 11:12:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
mBuffer = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-19 07:54:41 +00:00
|
|
|
bool
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase::Write(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
2015-07-19 07:54:41 +00:00
|
|
|
{
|
2015-08-27 16:19:13 +00:00
|
|
|
return Write(aCx, aValue, JS::UndefinedHandleValue);
|
2015-07-19 07:54:41 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 18:37:18 +00:00
|
|
|
bool
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase::Write(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
JS::Handle<JS::Value> aTransfer)
|
2015-07-22 18:37:18 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mBuffer, "Double Write is not allowed");
|
2015-09-30 12:22:08 +00:00
|
|
|
MOZ_ASSERT(!mClearCalled, "This method cannot be called after Clear.");
|
2015-07-22 18:37:18 +00:00
|
|
|
|
2016-04-22 10:04:20 +00:00
|
|
|
mBuffer = MakeUnique<JSAutoStructuredCloneBuffer>(mStructuredCloneScope, &StructuredCloneHolder::sCallbacks, this);
|
2015-09-02 16:20:30 +00:00
|
|
|
|
2016-04-22 10:04:20 +00:00
|
|
|
if (!mBuffer->write(aCx, aValue, aTransfer, &StructuredCloneHolder::sCallbacks, this)) {
|
2015-09-02 16:20:30 +00:00
|
|
|
mBuffer = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 07:54:41 +00:00
|
|
|
bool
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase::Read(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aValue)
|
2015-07-19 07:54:41 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mBuffer, "Read() without Write() is not allowed.");
|
2015-09-30 12:22:08 +00:00
|
|
|
MOZ_ASSERT(!mClearCalled, "This method cannot be called after Clear.");
|
2015-07-19 07:54:41 +00:00
|
|
|
|
2016-04-22 10:04:20 +00:00
|
|
|
bool ok = mBuffer->read(aCx, aValue, &StructuredCloneHolder::sCallbacks, this);
|
2015-07-19 07:54:41 +00:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase::CustomReadTransferHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData,
|
|
|
|
JS::MutableHandleObject aReturnObject)
|
2015-07-19 07:54:41 +00:00
|
|
|
{
|
|
|
|
MOZ_CRASH("Nothing to read.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase::CustomWriteTransferHandler(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
uint32_t* aTag,
|
|
|
|
JS::TransferableOwnership* aOwnership,
|
|
|
|
void** aContent,
|
|
|
|
uint64_t* aExtraData)
|
2015-07-19 07:54:41 +00:00
|
|
|
{
|
|
|
|
// No transfers are supported by default.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolderBase::CustomFreeTransferHandler(uint32_t aTag,
|
|
|
|
JS::TransferableOwnership aOwnership,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData)
|
2015-07-19 07:54:41 +00:00
|
|
|
{
|
|
|
|
MOZ_CRASH("Nothing to free.");
|
|
|
|
}
|
|
|
|
|
2015-09-30 12:22:08 +00:00
|
|
|
// StructuredCloneHolder class
|
2015-07-22 18:37:18 +00:00
|
|
|
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::StructuredCloneHolder(CloningSupport aSupportsCloning,
|
2015-09-02 16:20:30 +00:00
|
|
|
TransferringSupport aSupportsTransferring,
|
2016-07-21 13:29:42 +00:00
|
|
|
StructuredCloneScope aScope)
|
|
|
|
: StructuredCloneHolderBase(aScope)
|
|
|
|
, mSupportsCloning(aSupportsCloning == CloningSupported)
|
2015-07-28 07:38:16 +00:00
|
|
|
, mSupportsTransferring(aSupportsTransferring == TransferringSupported)
|
2015-07-22 18:37:18 +00:00
|
|
|
, mParent(nullptr)
|
2015-09-02 16:20:30 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mCreationThread(NS_GetCurrentThread())
|
|
|
|
#endif
|
2015-07-22 18:37:18 +00:00
|
|
|
{}
|
|
|
|
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::~StructuredCloneHolder()
|
2015-07-24 11:12:51 +00:00
|
|
|
{
|
2015-09-30 12:22:08 +00:00
|
|
|
Clear();
|
2015-08-27 16:19:13 +00:00
|
|
|
MOZ_ASSERT(mTransferredPorts.IsEmpty());
|
2015-07-24 11:12:51 +00:00
|
|
|
}
|
2015-07-22 18:37:18 +00:00
|
|
|
|
2015-07-31 00:38:00 +00:00
|
|
|
void
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::Write(JSContext* aCx,
|
2015-07-31 00:38:00 +00:00
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv)
|
2015-07-28 07:38:16 +00:00
|
|
|
{
|
2015-09-02 16:20:30 +00:00
|
|
|
Write(aCx, aValue, JS::UndefinedHandleValue, aRv);
|
2015-07-28 07:38:16 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:38:00 +00:00
|
|
|
void
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::Write(JSContext* aCx,
|
2015-07-22 18:37:18 +00:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2015-07-31 00:38:00 +00:00
|
|
|
JS::Handle<JS::Value> aTransfer,
|
|
|
|
ErrorResult& aRv)
|
2015-07-22 18:37:18 +00:00
|
|
|
{
|
2016-07-21 13:29:42 +00:00
|
|
|
MOZ_ASSERT_IF(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread,
|
2015-09-02 16:20:30 +00:00
|
|
|
mCreationThread == NS_GetCurrentThread());
|
|
|
|
|
2015-09-30 12:22:08 +00:00
|
|
|
if (!StructuredCloneHolderBase::Write(aCx, aValue, aTransfer)) {
|
2015-07-31 00:38:00 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
2015-08-27 16:19:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:38:00 +00:00
|
|
|
void
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::Read(nsISupports* aParent,
|
2015-07-22 18:37:18 +00:00
|
|
|
JSContext* aCx,
|
2015-07-31 00:38:00 +00:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv)
|
2015-07-22 18:37:18 +00:00
|
|
|
{
|
2016-07-21 13:29:42 +00:00
|
|
|
MOZ_ASSERT_IF(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread,
|
2015-09-02 16:20:30 +00:00
|
|
|
mCreationThread == NS_GetCurrentThread());
|
2016-03-01 14:21:11 +00:00
|
|
|
MOZ_ASSERT(aParent);
|
2015-09-02 16:20:30 +00:00
|
|
|
|
2015-07-22 18:37:18 +00:00
|
|
|
mozilla::AutoRestore<nsISupports*> guard(mParent);
|
|
|
|
mParent = aParent;
|
|
|
|
|
2015-09-30 12:22:08 +00:00
|
|
|
if (!StructuredCloneHolderBase::Read(aCx, aValue)) {
|
2015-07-31 00:38:00 +00:00
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
}
|
|
|
|
|
2015-08-26 12:17:23 +00:00
|
|
|
// If we are tranferring something, we cannot call 'Read()' more than once.
|
|
|
|
if (mSupportsTransferring) {
|
|
|
|
mBlobImplArray.Clear();
|
2015-12-18 06:52:16 +00:00
|
|
|
mClonedSurfaces.Clear();
|
2015-09-30 12:22:08 +00:00
|
|
|
Clear();
|
2015-08-26 12:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::ReadFromBuffer(nsISupports* aParent,
|
2015-08-26 12:17:23 +00:00
|
|
|
JSContext* aCx,
|
2016-04-22 10:04:20 +00:00
|
|
|
JSStructuredCloneData& aBuffer,
|
2015-08-26 12:17:23 +00:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2016-04-22 10:04:20 +00:00
|
|
|
ReadFromBuffer(aParent, aCx, aBuffer,
|
2015-08-26 12:17:23 +00:00
|
|
|
JS_STRUCTURED_CLONE_VERSION, aValue, aRv);
|
2015-07-28 07:38:16 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:38:00 +00:00
|
|
|
void
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::ReadFromBuffer(nsISupports* aParent,
|
2015-07-28 07:38:16 +00:00
|
|
|
JSContext* aCx,
|
2016-04-22 10:04:20 +00:00
|
|
|
JSStructuredCloneData& aBuffer,
|
2015-08-26 12:17:23 +00:00
|
|
|
uint32_t aAlgorithmVersion,
|
2015-07-31 00:38:00 +00:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv)
|
2015-07-28 07:38:16 +00:00
|
|
|
{
|
2016-07-21 13:29:42 +00:00
|
|
|
MOZ_ASSERT_IF(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread,
|
2015-09-02 16:20:30 +00:00
|
|
|
mCreationThread == NS_GetCurrentThread());
|
|
|
|
|
2015-07-28 07:38:16 +00:00
|
|
|
MOZ_ASSERT(!mBuffer, "ReadFromBuffer() must be called without a Write().");
|
|
|
|
|
|
|
|
mozilla::AutoRestore<nsISupports*> guard(mParent);
|
|
|
|
mParent = aParent;
|
|
|
|
|
2016-04-22 10:04:20 +00:00
|
|
|
if (!JS_ReadStructuredClone(aCx, aBuffer, aAlgorithmVersion,
|
|
|
|
mStructuredCloneScope, aValue, &sCallbacks,
|
2016-07-21 13:29:42 +00:00
|
|
|
this)) {
|
2015-07-31 00:38:00 +00:00
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
}
|
2015-07-28 10:47:36 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 17:05:44 +00:00
|
|
|
/* static */ JSObject*
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::ReadFullySerializableObjects(JSContext* aCx,
|
2015-09-14 17:05:44 +00:00
|
|
|
JSStructuredCloneReader* aReader,
|
2015-10-07 19:32:14 +00:00
|
|
|
uint32_t aTag)
|
2015-09-14 17:05:44 +00:00
|
|
|
{
|
|
|
|
if (aTag == SCTAG_DOM_IMAGEDATA) {
|
|
|
|
return ReadStructuredCloneImageData(aCx, aReader);
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:03:44 +00:00
|
|
|
if (aTag == SCTAG_DOM_WEBCRYPTO_KEY || aTag == SCTAG_DOM_URLSEARCHPARAMS) {
|
2015-09-14 17:05:44 +00:00
|
|
|
nsIGlobalObject *global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
|
|
|
|
if (!global) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent the return value from being trashed by a GC during ~nsRefPtr.
|
|
|
|
JS::Rooted<JSObject*> result(aCx);
|
|
|
|
{
|
2016-07-11 19:03:44 +00:00
|
|
|
if (aTag == SCTAG_DOM_WEBCRYPTO_KEY) {
|
|
|
|
RefPtr<CryptoKey> key = new CryptoKey(global);
|
|
|
|
if (!key->ReadStructuredClone(aReader)) {
|
|
|
|
result = nullptr;
|
|
|
|
} else {
|
|
|
|
result = key->WrapObject(aCx, nullptr);
|
|
|
|
}
|
|
|
|
} else if (aTag == SCTAG_DOM_URLSEARCHPARAMS) {
|
|
|
|
RefPtr<URLSearchParams> usp = new URLSearchParams(global);
|
|
|
|
if (!usp->ReadStructuredClone(aReader)) {
|
|
|
|
result = nullptr;
|
|
|
|
} else {
|
|
|
|
result = usp->WrapObject(aCx, nullptr);
|
|
|
|
}
|
2015-09-14 17:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_NULL_PRINCIPAL ||
|
|
|
|
aTag == SCTAG_DOM_SYSTEM_PRINCIPAL ||
|
2016-02-04 23:12:52 +00:00
|
|
|
aTag == SCTAG_DOM_CONTENT_PRINCIPAL ||
|
|
|
|
aTag == SCTAG_DOM_EXPANDED_PRINCIPAL) {
|
2015-10-02 14:44:00 +00:00
|
|
|
JSPrincipals* prin;
|
|
|
|
if (!nsJSPrincipals::ReadKnownPrincipalType(aCx, aReader, aTag, &prin)) {
|
2015-09-14 17:05:44 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-02 14:44:00 +00:00
|
|
|
// nsJSPrincipals::ReadKnownPrincipalType addrefs for us, but because of the
|
|
|
|
// casting between JSPrincipals* and nsIPrincipal* we can't use
|
|
|
|
// getter_AddRefs above and have to already_AddRefed here.
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = already_AddRefed<nsIPrincipal>(nsJSPrincipals::get(prin));
|
2015-09-14 17:05:44 +00:00
|
|
|
|
|
|
|
JS::RootedValue result(aCx);
|
2015-10-02 14:44:00 +00:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(aCx, principal,
|
|
|
|
&NS_GET_IID(nsIPrincipal),
|
|
|
|
&result);
|
2015-09-14 17:05:44 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.toObjectOrNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
if (aTag == SCTAG_DOM_RTC_CERTIFICATE) {
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIGlobalObject *global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
|
|
|
|
if (!global) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent the return value from being trashed by a GC during ~nsRefPtr.
|
|
|
|
JS::Rooted<JSObject*> result(aCx);
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<RTCCertificate> cert = new RTCCertificate(global);
|
2015-09-14 17:05:44 +00:00
|
|
|
if (!cert->ReadStructuredClone(aReader)) {
|
|
|
|
result = nullptr;
|
|
|
|
} else {
|
|
|
|
result = cert->WrapObject(aCx, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Don't know what this is. Bail.
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::WriteFullySerializableObjects(JSContext* aCx,
|
2015-09-14 17:05:44 +00:00
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj)
|
|
|
|
{
|
|
|
|
// See if this is a ImageData object.
|
|
|
|
{
|
|
|
|
ImageData* imageData = nullptr;
|
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(ImageData, aObj, imageData))) {
|
|
|
|
return WriteStructuredCloneImageData(aCx, aWriter, imageData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:03:44 +00:00
|
|
|
// Handle URLSearchParams cloning
|
|
|
|
{
|
|
|
|
URLSearchParams* usp = nullptr;
|
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(URLSearchParams, aObj, usp))) {
|
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_URLSEARCHPARAMS, 0) &&
|
|
|
|
usp->WriteStructuredClone(aWriter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 17:05:44 +00:00
|
|
|
// Handle Key cloning
|
|
|
|
{
|
2015-10-15 09:37:00 +00:00
|
|
|
CryptoKey* key = nullptr;
|
2015-09-14 17:05:44 +00:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(CryptoKey, aObj, key))) {
|
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_WEBCRYPTO_KEY, 0) &&
|
|
|
|
key->WriteStructuredClone(aWriter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
{
|
|
|
|
// Handle WebRTC Certificate cloning
|
2015-10-15 09:37:00 +00:00
|
|
|
RTCCertificate* cert = nullptr;
|
2015-09-14 17:05:44 +00:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(RTCCertificate, aObj, cert))) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_RTC_CERTIFICATE, 0) &&
|
|
|
|
cert->WriteStructuredClone(aWriter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (NS_IsMainThread() && xpc::IsReflector(aObj)) {
|
|
|
|
nsCOMPtr<nsISupports> base = xpc::UnwrapReflectorToISupports(aObj);
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(base);
|
|
|
|
if (principal) {
|
2015-10-02 14:44:00 +00:00
|
|
|
auto nsjsprincipals = nsJSPrincipals::get(principal);
|
|
|
|
return nsjsprincipals->write(aCx, aWriter);
|
2015-09-14 17:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't know what this is
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Recursive!
|
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
EnsureBlobForBackgroundManager(BlobImpl* aBlobImpl,
|
2015-12-04 21:15:46 +00:00
|
|
|
PBackgroundChild* aManager,
|
|
|
|
ErrorResult& aRv)
|
2015-08-27 16:19:13 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBlobImpl);
|
2015-11-28 10:48:28 +00:00
|
|
|
RefPtr<BlobImpl> blobImpl = aBlobImpl;
|
2015-08-27 16:19:13 +00:00
|
|
|
|
|
|
|
if (!aManager) {
|
|
|
|
aManager = BackgroundChild::GetForCurrentThread();
|
2015-11-28 10:48:28 +00:00
|
|
|
if (!aManager) {
|
|
|
|
return blobImpl.forget();
|
|
|
|
}
|
2015-08-27 16:19:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
const nsTArray<RefPtr<BlobImpl>>* subBlobImpls =
|
2015-08-27 16:19:13 +00:00
|
|
|
aBlobImpl->GetSubBlobImpls();
|
|
|
|
|
|
|
|
if (!subBlobImpls || !subBlobImpls->Length()) {
|
|
|
|
if (nsCOMPtr<nsIRemoteBlob> remoteBlob = do_QueryObject(blobImpl)) {
|
|
|
|
// Always make sure we have a blob from an actor we can use on this
|
|
|
|
// thread.
|
|
|
|
BlobChild* blobChild = BlobChild::GetOrCreate(aManager, blobImpl);
|
|
|
|
MOZ_ASSERT(blobChild);
|
|
|
|
|
|
|
|
blobImpl = blobChild->GetBlobImpl();
|
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
|
|
|
DebugOnly<bool> isMutable;
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(blobImpl->GetMutable(&isMutable)));
|
|
|
|
MOZ_ASSERT(!isMutable);
|
|
|
|
} else {
|
2016-03-28 17:28:15 +00:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false));
|
2015-08-27 16:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return blobImpl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t subBlobCount = subBlobImpls->Length();
|
|
|
|
MOZ_ASSERT(subBlobCount);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
nsTArray<RefPtr<BlobImpl>> newSubBlobImpls;
|
2015-08-27 16:19:13 +00:00
|
|
|
newSubBlobImpls.SetLength(subBlobCount);
|
|
|
|
|
|
|
|
bool newBlobImplNeeded = false;
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < subBlobCount; index++) {
|
2015-10-18 05:24:48 +00:00
|
|
|
const RefPtr<BlobImpl>& subBlobImpl = subBlobImpls->ElementAt(index);
|
2015-08-27 16:19:13 +00:00
|
|
|
MOZ_ASSERT(subBlobImpl);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl>& newSubBlobImpl = newSubBlobImpls[index];
|
2015-08-27 16:19:13 +00:00
|
|
|
|
2015-12-04 21:15:46 +00:00
|
|
|
newSubBlobImpl = EnsureBlobForBackgroundManager(subBlobImpl, aManager, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
MOZ_ASSERT(newSubBlobImpl);
|
|
|
|
|
|
|
|
if (subBlobImpl != newSubBlobImpl) {
|
|
|
|
newBlobImplNeeded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newBlobImplNeeded) {
|
|
|
|
nsString contentType;
|
|
|
|
blobImpl->GetType(contentType);
|
|
|
|
|
|
|
|
if (blobImpl->IsFile()) {
|
|
|
|
nsString name;
|
|
|
|
blobImpl->GetName(name);
|
|
|
|
|
2016-09-21 01:29:28 +00:00
|
|
|
blobImpl = MultipartBlobImpl::Create(Move(newSubBlobImpls), name,
|
2015-12-04 21:15:46 +00:00
|
|
|
contentType, aRv);
|
2015-08-27 16:19:13 +00:00
|
|
|
} else {
|
2016-09-21 01:29:28 +00:00
|
|
|
blobImpl = MultipartBlobImpl::Create(Move(newSubBlobImpls), contentType, aRv);
|
2015-12-04 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2015-08-27 16:19:13 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 17:28:15 +00:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false));
|
2015-08-27 16:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return blobImpl.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-22 18:37:18 +00:00
|
|
|
JSObject*
|
2015-08-27 16:19:13 +00:00
|
|
|
ReadBlob(JSContext* aCx,
|
|
|
|
uint32_t aIndex,
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 08:06:09 +00:00
|
|
|
{
|
2015-09-30 12:22:08 +00:00
|
|
|
MOZ_ASSERT(aHolder);
|
|
|
|
MOZ_ASSERT(aIndex < aHolder->BlobImpls().Length());
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl> blobImpl = aHolder->BlobImpls()[aIndex];
|
2015-08-27 16:19:13 +00:00
|
|
|
|
2015-12-04 21:15:46 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
blobImpl = EnsureBlobForBackgroundManager(blobImpl, nullptr, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
2016-04-12 12:51:04 +00:00
|
|
|
// RefPtr<File> needs to go out of scope before toObject() is
|
2015-08-27 16:19:13 +00:00
|
|
|
// called because the static analysis thinks dereferencing XPCOM objects
|
|
|
|
// can GC (because in some cases it can!), and a return statement with a
|
|
|
|
// JSObject* type means that JSObject* is on the stack as a raw pointer
|
|
|
|
// while destructors are running.
|
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> blob = Blob::Create(aHolder->ParentDuringRead(), blobImpl);
|
2015-08-27 16:19:13 +00:00
|
|
|
if (!ToJSValue(aCx, blob, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 08:06:09 +00:00
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
return &val.toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WriteBlob(JSStructuredCloneWriter* aWriter,
|
|
|
|
Blob* aBlob,
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 16:19:13 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWriter);
|
|
|
|
MOZ_ASSERT(aBlob);
|
2015-09-30 12:22:08 +00:00
|
|
|
MOZ_ASSERT(aHolder);
|
2015-08-27 16:19:13 +00:00
|
|
|
|
2016-08-17 05:54:00 +00:00
|
|
|
if (JS_GetStructuredCloneScope(aWriter) != JS::StructuredCloneScope::SameProcessSameThread &&
|
|
|
|
!aBlob->Impl()->MayBeClonedToOtherThreads()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-04 21:15:46 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
RefPtr<BlobImpl> blobImpl =
|
|
|
|
EnsureBlobForBackgroundManager(aBlob->Impl(), nullptr, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
2016-03-28 17:28:15 +00:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false));
|
2015-11-28 10:48:28 +00:00
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
// We store the position of the blobImpl in the array as index.
|
|
|
|
if (JS_WriteUint32Pair(aWriter, SCTAG_DOM_BLOB,
|
2015-09-30 12:22:08 +00:00
|
|
|
aHolder->BlobImpls().Length())) {
|
|
|
|
aHolder->BlobImpls().AppendElement(blobImpl);
|
2015-08-27 16:19:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-12 12:51:04 +00:00
|
|
|
// A directory is serialized as:
|
2016-04-18 07:32:30 +00:00
|
|
|
// - pair of ints: SCTAG_DOM_DIRECTORY, path length
|
2016-04-12 12:51:04 +00:00
|
|
|
// - path as string
|
|
|
|
bool
|
|
|
|
WriteDirectory(JSStructuredCloneWriter* aWriter,
|
|
|
|
Directory* aDirectory)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWriter);
|
|
|
|
MOZ_ASSERT(aDirectory);
|
|
|
|
|
|
|
|
nsAutoString path;
|
|
|
|
aDirectory->GetFullRealPath(path);
|
|
|
|
|
|
|
|
size_t charSize = sizeof(nsString::char_type);
|
2016-04-18 07:32:30 +00:00
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_DIRECTORY, path.Length()) &&
|
2016-04-12 12:51:04 +00:00
|
|
|
JS_WriteBytes(aWriter, path.get(), path.Length() * charSize);
|
|
|
|
}
|
|
|
|
|
2016-07-14 07:01:31 +00:00
|
|
|
already_AddRefed<Directory>
|
|
|
|
ReadDirectoryInternal(JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aPathLength,
|
|
|
|
StructuredCloneHolder* aHolder)
|
2016-04-12 12:51:04 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aReader);
|
|
|
|
MOZ_ASSERT(aHolder);
|
|
|
|
|
|
|
|
nsAutoString path;
|
2016-04-18 07:32:30 +00:00
|
|
|
path.SetLength(aPathLength);
|
2016-04-12 12:51:04 +00:00
|
|
|
size_t charSize = sizeof(nsString::char_type);
|
|
|
|
if (!JS_ReadBytes(aReader, (void*) path.BeginWriting(),
|
2016-04-18 07:32:30 +00:00
|
|
|
aPathLength * charSize)) {
|
2016-04-12 12:51:04 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = NS_NewNativeLocalFile(NS_ConvertUTF16toUTF8(path), true,
|
|
|
|
getter_AddRefs(file));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-14 07:01:31 +00:00
|
|
|
RefPtr<Directory> directory =
|
|
|
|
Directory::Create(aHolder->ParentDuringRead(), file);
|
|
|
|
return directory.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
ReadDirectory(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aPathLength,
|
|
|
|
StructuredCloneHolder* aHolder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aReader);
|
|
|
|
MOZ_ASSERT(aHolder);
|
|
|
|
|
2016-04-12 12:51:04 +00:00
|
|
|
// RefPtr<Directory> needs to go out of scope before toObject() is
|
|
|
|
// called because the static analysis thinks dereferencing XPCOM objects
|
|
|
|
// can GC (because in some cases it can!), and a return statement with a
|
|
|
|
// JSObject* type means that JSObject* is on the stack as a raw pointer
|
|
|
|
// while destructors are running.
|
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
|
|
{
|
|
|
|
RefPtr<Directory> directory =
|
2016-07-14 07:01:31 +00:00
|
|
|
ReadDirectoryInternal(aReader, aPathLength, aHolder);
|
|
|
|
if (!directory) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-12 12:51:04 +00:00
|
|
|
|
|
|
|
if (!ToJSValue(aCx, directory, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &val.toObject();
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
// Read the WriteFileList for the format.
|
|
|
|
JSObject*
|
|
|
|
ReadFileList(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aCount,
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 16:19:13 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aReader);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<FileList> fileList = new FileList(aHolder->ParentDuringRead());
|
2015-08-27 16:19:13 +00:00
|
|
|
|
2016-04-12 12:51:52 +00:00
|
|
|
uint32_t zero, index;
|
|
|
|
// |index| is the index of the first blobImpl.
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &zero, &index)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-20 10:56:10 +00:00
|
|
|
|
2016-04-12 12:51:52 +00:00
|
|
|
MOZ_ASSERT(zero == 0);
|
2015-08-28 23:19:56 +00:00
|
|
|
|
2016-04-12 12:51:52 +00:00
|
|
|
// |aCount| is the number of BlobImpls to use from the |index|.
|
|
|
|
for (uint32_t i = 0; i < aCount; ++i) {
|
|
|
|
uint32_t pos = index + i;
|
|
|
|
MOZ_ASSERT(pos < aHolder->BlobImpls().Length());
|
2016-04-12 15:56:00 +00:00
|
|
|
|
2016-04-12 12:51:52 +00:00
|
|
|
RefPtr<BlobImpl> blobImpl = aHolder->BlobImpls()[pos];
|
|
|
|
MOZ_ASSERT(blobImpl->IsFile());
|
2016-03-20 02:44:22 +00:00
|
|
|
|
2016-04-12 12:51:52 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
blobImpl = EnsureBlobForBackgroundManager(blobImpl, nullptr, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
2016-04-12 15:56:00 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-12 12:51:52 +00:00
|
|
|
|
2016-04-12 12:51:52 +00:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
|
|
|
RefPtr<File> file = File::Create(aHolder->ParentDuringRead(), blobImpl);
|
|
|
|
if (!fileList->Append(file)) {
|
2015-07-28 07:38:16 +00:00
|
|
|
return nullptr;
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-28 07:38:16 +00:00
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
if (!ToJSValue(aCx, fileList, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-27 08:06:09 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
return &val.toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The format of the FileList serialization is:
|
|
|
|
// - pair of ints: SCTAG_DOM_FILELIST, Length of the FileList
|
2016-04-12 12:51:52 +00:00
|
|
|
// - pair of ints: 0, The offset of the BlobImpl array
|
2015-08-27 16:19:13 +00:00
|
|
|
bool
|
|
|
|
WriteFileList(JSStructuredCloneWriter* aWriter,
|
|
|
|
FileList* aFileList,
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 16:19:13 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWriter);
|
|
|
|
MOZ_ASSERT(aFileList);
|
2015-09-30 12:22:08 +00:00
|
|
|
MOZ_ASSERT(aHolder);
|
2015-08-27 16:19:13 +00:00
|
|
|
|
2016-04-12 12:51:52 +00:00
|
|
|
// A FileList is serialized writing the X number of elements and the offset
|
|
|
|
// from mBlobImplArray. The Read will take X elements from mBlobImplArray
|
|
|
|
// starting from the offset.
|
2015-08-27 16:19:13 +00:00
|
|
|
if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_FILELIST,
|
2016-04-12 12:51:52 +00:00
|
|
|
aFileList->Length()) ||
|
|
|
|
!JS_WriteUint32Pair(aWriter, 0,
|
|
|
|
aHolder->BlobImpls().Length())) {
|
2015-08-27 16:19:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-04 21:15:46 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
nsTArray<RefPtr<BlobImpl>> blobImpls;
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
for (uint32_t i = 0; i < aFileList->Length(); ++i) {
|
2016-04-12 12:51:52 +00:00
|
|
|
RefPtr<BlobImpl> blobImpl =
|
|
|
|
EnsureBlobForBackgroundManager(aFileList->Item(i)->Impl(), nullptr, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
2016-04-12 15:56:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-12 12:51:52 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
blobImpls.AppendElement(blobImpl);
|
2015-08-27 16:19:13 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 12:51:52 +00:00
|
|
|
aHolder->BlobImpls().AppendElements(blobImpls);
|
2015-08-27 16:19:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the WriteFormData for the format.
|
|
|
|
JSObject*
|
|
|
|
ReadFormData(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aCount,
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 16:19:13 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aReader);
|
2015-09-30 12:22:08 +00:00
|
|
|
MOZ_ASSERT(aHolder);
|
2015-08-27 08:06:09 +00:00
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
// See the serialization of the FormData for the format.
|
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
|
|
{
|
2016-01-07 19:30:36 +00:00
|
|
|
RefPtr<FormData> formData =
|
|
|
|
new FormData(aHolder->ParentDuringRead());
|
2015-08-27 16:19:13 +00:00
|
|
|
|
|
|
|
Optional<nsAString> thirdArg;
|
|
|
|
for (uint32_t i = 0; i < aCount; ++i) {
|
|
|
|
nsAutoString name;
|
|
|
|
if (!ReadString(aReader, name)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t tag, indexOrLengthOfString;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &tag, &indexOrLengthOfString)) {
|
2015-07-28 07:38:16 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
if (tag == SCTAG_DOM_BLOB) {
|
2015-09-30 12:22:08 +00:00
|
|
|
MOZ_ASSERT(indexOrLengthOfString < aHolder->BlobImpls().Length());
|
2015-07-28 07:38:16 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl> blobImpl =
|
2015-09-30 12:22:08 +00:00
|
|
|
aHolder->BlobImpls()[indexOrLengthOfString];
|
2015-07-28 07:38:16 +00:00
|
|
|
|
2016-01-20 17:25:03 +00:00
|
|
|
RefPtr<Blob> blob =
|
|
|
|
Blob::Create(aHolder->ParentDuringRead(), blobImpl);
|
|
|
|
MOZ_ASSERT(blob);
|
2015-08-27 16:19:13 +00:00
|
|
|
|
2015-12-04 21:15:46 +00:00
|
|
|
ErrorResult rv;
|
2016-01-20 17:25:03 +00:00
|
|
|
formData->Append(name, *blob, thirdArg, rv);
|
2015-12-04 21:15:46 +00:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2016-06-14 12:10:39 +00:00
|
|
|
rv.SuppressException();
|
2015-12-04 21:15:46 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-14 07:01:31 +00:00
|
|
|
} else if (tag == SCTAG_DOM_DIRECTORY) {
|
|
|
|
RefPtr<Directory> directory =
|
|
|
|
ReadDirectoryInternal(aReader, indexOrLengthOfString, aHolder);
|
|
|
|
if (!directory) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
formData->Append(name, directory);
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(tag == 0);
|
|
|
|
|
|
|
|
nsAutoString value;
|
|
|
|
value.SetLength(indexOrLengthOfString);
|
|
|
|
size_t charSize = sizeof(nsString::char_type);
|
|
|
|
if (!JS_ReadBytes(aReader, (void*) value.BeginWriting(),
|
|
|
|
indexOrLengthOfString * charSize)) {
|
2015-07-22 18:37:18 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-27 16:19:13 +00:00
|
|
|
|
2015-12-04 21:15:46 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
formData->Append(name, value, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2016-06-14 12:10:39 +00:00
|
|
|
rv.SuppressException();
|
2015-12-04 21:15:46 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-27 16:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ToJSValue(aCx, formData, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &val.toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The format of the FormData serialization is:
|
|
|
|
// - pair of ints: SCTAG_DOM_FORMDATA, Length of the FormData elements
|
|
|
|
// - for each Element element:
|
|
|
|
// - name string
|
|
|
|
// - if it's a blob:
|
|
|
|
// - pair of ints: SCTAG_DOM_BLOB, index of the BlobImpl in the array
|
|
|
|
// mBlobImplArray.
|
2016-07-14 07:01:31 +00:00
|
|
|
// - if it's a directory (See WriteDirectory):
|
|
|
|
// - pair of ints: SCTAG_DOM_DIRECTORY, path length
|
|
|
|
// - path as string
|
2015-08-27 16:19:13 +00:00
|
|
|
// - else:
|
|
|
|
// - pair of ints: 0, string length
|
|
|
|
// - value string
|
|
|
|
bool
|
|
|
|
WriteFormData(JSStructuredCloneWriter* aWriter,
|
2016-01-07 19:30:36 +00:00
|
|
|
FormData* aFormData,
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 16:19:13 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWriter);
|
|
|
|
MOZ_ASSERT(aFormData);
|
2015-09-30 12:22:08 +00:00
|
|
|
MOZ_ASSERT(aHolder);
|
2015-08-27 16:19:13 +00:00
|
|
|
|
|
|
|
if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_FORMDATA,
|
|
|
|
aFormData->Length())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS Closure final
|
|
|
|
{
|
|
|
|
JSStructuredCloneWriter* mWriter;
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder* mHolder;
|
2015-08-27 16:19:13 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Closure(JSStructuredCloneWriter* aWriter,
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 16:19:13 +00:00
|
|
|
: mWriter(aWriter),
|
2015-09-30 12:22:08 +00:00
|
|
|
mHolder(aHolder)
|
2015-08-27 16:19:13 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
static bool
|
2016-07-14 07:01:31 +00:00
|
|
|
Write(const nsString& aName, const OwningBlobOrDirectoryOrUSVString& aValue,
|
2015-10-20 02:06:00 +00:00
|
|
|
void* aClosure)
|
2015-08-27 16:19:13 +00:00
|
|
|
{
|
|
|
|
Closure* closure = static_cast<Closure*>(aClosure);
|
|
|
|
if (!WriteString(closure->mWriter, aName)) {
|
|
|
|
return false;
|
2015-07-28 07:38:16 +00:00
|
|
|
}
|
2015-08-27 08:06:09 +00:00
|
|
|
|
2016-01-20 17:25:03 +00:00
|
|
|
if (aValue.IsBlob()) {
|
|
|
|
BlobImpl* blobImpl = aValue.GetAsBlob()->Impl();
|
2015-08-27 16:19:13 +00:00
|
|
|
if (!JS_WriteUint32Pair(closure->mWriter, SCTAG_DOM_BLOB,
|
2015-09-30 12:22:08 +00:00
|
|
|
closure->mHolder->BlobImpls().Length())) {
|
2015-08-27 16:19:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-30 12:22:08 +00:00
|
|
|
closure->mHolder->BlobImpls().AppendElement(blobImpl);
|
2015-08-27 16:19:13 +00:00
|
|
|
return true;
|
2015-08-27 08:06:09 +00:00
|
|
|
}
|
2015-08-27 16:19:13 +00:00
|
|
|
|
2016-07-14 07:01:31 +00:00
|
|
|
if (aValue.IsDirectory()) {
|
|
|
|
Directory* directory = aValue.GetAsDirectory();
|
|
|
|
|
2016-07-21 13:29:42 +00:00
|
|
|
if (closure->mHolder->CloneScope() !=
|
|
|
|
StructuredCloneHolder::StructuredCloneScope::SameProcessSameThread &&
|
2016-07-14 07:01:31 +00:00
|
|
|
!directory->ClonableToDifferentThreadOrProcess()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteDirectory(closure->mWriter, directory);
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
size_t charSize = sizeof(nsString::char_type);
|
2015-10-20 02:06:00 +00:00
|
|
|
if (!JS_WriteUint32Pair(closure->mWriter, 0,
|
|
|
|
aValue.GetAsUSVString().Length()) ||
|
|
|
|
!JS_WriteBytes(closure->mWriter, aValue.GetAsUSVString().get(),
|
|
|
|
aValue.GetAsUSVString().Length() * charSize)) {
|
2015-08-27 16:19:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
2015-08-27 16:19:13 +00:00
|
|
|
};
|
2015-09-30 12:22:08 +00:00
|
|
|
Closure closure(aWriter, aHolder);
|
2015-08-27 16:19:13 +00:00
|
|
|
return aFormData->ForEach(Closure::Write, &closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
JSObject*
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::CustomReadHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
uint32_t aIndex)
|
2015-08-27 16:19:13 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSupportsCloning);
|
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_BLOB) {
|
|
|
|
return ReadBlob(aCx, aIndex, this);
|
|
|
|
}
|
|
|
|
|
2016-04-12 12:51:04 +00:00
|
|
|
if (aTag == SCTAG_DOM_DIRECTORY) {
|
|
|
|
return ReadDirectory(aCx, aReader, aIndex, this);
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
if (aTag == SCTAG_DOM_FILELIST) {
|
|
|
|
return ReadFileList(aCx, aReader, aIndex, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_FORMDATA) {
|
|
|
|
return ReadFormData(aCx, aReader, aIndex, this);
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 18:50:00 +00:00
|
|
|
if (aTag == SCTAG_DOM_IMAGEBITMAP) {
|
2016-07-21 13:29:42 +00:00
|
|
|
MOZ_ASSERT(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::SameProcessDifferentThread);
|
2015-09-02 16:20:30 +00:00
|
|
|
|
2015-09-14 17:05:44 +00:00
|
|
|
// Get the current global object.
|
|
|
|
// This can be null.
|
|
|
|
nsCOMPtr<nsIGlobalObject> parent = do_QueryInterface(mParent);
|
|
|
|
// aIndex is the index of the cloned image.
|
|
|
|
return ImageBitmap::ReadStructuredClone(aCx, aReader,
|
2015-12-18 06:52:16 +00:00
|
|
|
parent, GetSurfaces(), aIndex);
|
2015-07-30 18:50:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 19:32:14 +00:00
|
|
|
return ReadFullySerializableObjects(aCx, aReader, aTag);
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::CustomWriteHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj)
|
2015-07-22 18:37:18 +00:00
|
|
|
{
|
2015-07-28 07:38:16 +00:00
|
|
|
if (!mSupportsCloning) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-22 18:37:18 +00:00
|
|
|
// See if this is a File/Blob object.
|
2015-07-28 07:38:16 +00:00
|
|
|
{
|
2015-07-22 18:37:18 +00:00
|
|
|
Blob* blob = nullptr;
|
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(Blob, aObj, blob))) {
|
2015-08-27 16:19:13 +00:00
|
|
|
return WriteBlob(aWriter, blob, this);
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-12 12:51:04 +00:00
|
|
|
// See if this is a Directory object.
|
|
|
|
{
|
|
|
|
Directory* directory = nullptr;
|
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(Directory, aObj, directory))) {
|
2016-07-21 13:29:42 +00:00
|
|
|
if (mStructuredCloneScope != StructuredCloneScope::SameProcessSameThread &&
|
2016-06-14 11:51:41 +00:00
|
|
|
!directory->ClonableToDifferentThreadOrProcess()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-12 12:51:04 +00:00
|
|
|
return WriteDirectory(aWriter, directory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
// See if this is a FileList object.
|
2016-03-20 10:56:10 +00:00
|
|
|
{
|
2015-07-22 18:37:18 +00:00
|
|
|
FileList* fileList = nullptr;
|
2016-04-12 12:51:52 +00:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(FileList, aObj, fileList))) {
|
2015-08-27 16:19:13 +00:00
|
|
|
return WriteFileList(aWriter, fileList, this);
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 07:38:16 +00:00
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
// See if this is a FormData object.
|
|
|
|
{
|
2016-01-07 19:30:36 +00:00
|
|
|
FormData* formData = nullptr;
|
2015-08-27 16:19:13 +00:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(FormData, aObj, formData))) {
|
|
|
|
return WriteFormData(aWriter, formData, this);
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 18:50:00 +00:00
|
|
|
// See if this is an ImageBitmap object.
|
2016-07-21 13:29:42 +00:00
|
|
|
if (mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::SameProcessDifferentThread) {
|
2015-07-30 18:50:00 +00:00
|
|
|
ImageBitmap* imageBitmap = nullptr;
|
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(ImageBitmap, aObj, imageBitmap))) {
|
|
|
|
return ImageBitmap::WriteStructuredClone(aWriter,
|
2015-12-18 06:52:16 +00:00
|
|
|
GetSurfaces(),
|
2015-07-30 18:50:00 +00:00
|
|
|
imageBitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 17:05:44 +00:00
|
|
|
return WriteFullySerializableObjects(aCx, aWriter, aObj);
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::CustomReadTransferHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData,
|
|
|
|
JS::MutableHandleObject aReturnObject)
|
2015-07-22 18:37:18 +00:00
|
|
|
{
|
2015-07-28 07:38:16 +00:00
|
|
|
MOZ_ASSERT(mSupportsTransferring);
|
2015-07-22 18:37:18 +00:00
|
|
|
|
2015-07-28 07:38:16 +00:00
|
|
|
if (aTag == SCTAG_DOM_MAP_MESSAGEPORT) {
|
2015-07-22 18:37:18 +00:00
|
|
|
MOZ_ASSERT(aExtraData < mPortIdentifiers.Length());
|
|
|
|
const MessagePortIdentifier& portIdentifier = mPortIdentifiers[aExtraData];
|
|
|
|
|
2016-03-01 14:21:11 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mParent);
|
|
|
|
|
2015-07-22 18:37:18 +00:00
|
|
|
ErrorResult rv;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MessagePort> port =
|
2016-03-01 14:21:11 +00:00
|
|
|
MessagePort::Create(global, portIdentifier, rv);
|
2015-07-22 18:37:18 +00:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2016-06-14 12:10:39 +00:00
|
|
|
rv.SuppressException();
|
2015-07-22 18:37:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTransferredPorts.AppendElement(port);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
if (!GetOrCreateDOMReflector(aCx, port, &value)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aReturnObject.set(&value.toObject());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-12 03:21:03 +00:00
|
|
|
if (aTag == SCTAG_DOM_CANVAS) {
|
2016-07-21 13:29:42 +00:00
|
|
|
MOZ_ASSERT(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::SameProcessDifferentThread);
|
2015-10-12 03:21:03 +00:00
|
|
|
MOZ_ASSERT(aContent);
|
|
|
|
OffscreenCanvasCloneData* data =
|
|
|
|
static_cast<OffscreenCanvasCloneData*>(aContent);
|
2015-12-18 06:52:17 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> parent = do_QueryInterface(mParent);
|
|
|
|
RefPtr<OffscreenCanvas> canvas = OffscreenCanvas::CreateFromCloneData(parent, data);
|
2015-10-12 03:21:03 +00:00
|
|
|
delete data;
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
if (!GetOrCreateDOMReflector(aCx, canvas, &value)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aReturnObject.set(&value.toObject());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-18 06:52:16 +00:00
|
|
|
if (aTag == SCTAG_DOM_IMAGEBITMAP) {
|
2016-07-21 13:29:42 +00:00
|
|
|
MOZ_ASSERT(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::SameProcessDifferentThread);
|
2015-12-18 06:52:16 +00:00
|
|
|
MOZ_ASSERT(aContent);
|
|
|
|
ImageBitmapCloneData* data =
|
|
|
|
static_cast<ImageBitmapCloneData*>(aContent);
|
|
|
|
nsCOMPtr<nsIGlobalObject> parent = do_QueryInterface(mParent);
|
|
|
|
RefPtr<ImageBitmap> bitmap = ImageBitmap::CreateFromCloneData(parent, data);
|
|
|
|
delete data;
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
if (!GetOrCreateDOMReflector(aCx, bitmap, &value)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aReturnObject.set(&value.toObject());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-22 18:37:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::CustomWriteTransferHandler(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
uint32_t* aTag,
|
|
|
|
JS::TransferableOwnership* aOwnership,
|
|
|
|
void** aContent,
|
|
|
|
uint64_t* aExtraData)
|
2015-07-22 18:37:18 +00:00
|
|
|
{
|
2015-07-28 07:38:16 +00:00
|
|
|
if (!mSupportsTransferring) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-09-16 03:27:56 +00:00
|
|
|
MessagePort* port = nullptr;
|
2015-07-22 18:37:18 +00:00
|
|
|
nsresult rv = UNWRAP_OBJECT(MessagePort, aObj, port);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// We use aExtraData to store the index of this new port identifier.
|
|
|
|
*aExtraData = mPortIdentifiers.Length();
|
|
|
|
MessagePortIdentifier* identifier = mPortIdentifiers.AppendElement();
|
|
|
|
|
2015-09-16 03:27:56 +00:00
|
|
|
port->CloneAndDisentangle(*identifier);
|
2015-07-22 18:37:18 +00:00
|
|
|
|
|
|
|
*aTag = SCTAG_DOM_MAP_MESSAGEPORT;
|
|
|
|
*aOwnership = JS::SCTAG_TMO_CUSTOM;
|
|
|
|
*aContent = nullptr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-12 03:21:03 +00:00
|
|
|
|
2016-07-21 13:29:42 +00:00
|
|
|
if (mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::SameProcessDifferentThread) {
|
2015-10-12 03:21:03 +00:00
|
|
|
OffscreenCanvas* canvas = nullptr;
|
|
|
|
rv = UNWRAP_OBJECT(OffscreenCanvas, aObj, canvas);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
MOZ_ASSERT(canvas);
|
|
|
|
|
|
|
|
*aExtraData = 0;
|
|
|
|
*aTag = SCTAG_DOM_CANVAS;
|
|
|
|
*aOwnership = JS::SCTAG_TMO_CUSTOM;
|
|
|
|
*aContent = canvas->ToCloneData();
|
|
|
|
MOZ_ASSERT(*aContent);
|
|
|
|
canvas->SetNeutered();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-18 06:52:16 +00:00
|
|
|
|
|
|
|
ImageBitmap* bitmap = nullptr;
|
|
|
|
rv = UNWRAP_OBJECT(ImageBitmap, aObj, bitmap);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
MOZ_ASSERT(bitmap);
|
|
|
|
|
|
|
|
*aExtraData = 0;
|
|
|
|
*aTag = SCTAG_DOM_IMAGEBITMAP;
|
|
|
|
*aOwnership = JS::SCTAG_TMO_CUSTOM;
|
|
|
|
*aContent = bitmap->ToCloneData();
|
|
|
|
MOZ_ASSERT(*aContent);
|
|
|
|
bitmap->Close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-12 03:21:03 +00:00
|
|
|
}
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-30 12:22:08 +00:00
|
|
|
StructuredCloneHolder::CustomFreeTransferHandler(uint32_t aTag,
|
|
|
|
JS::TransferableOwnership aOwnership,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData)
|
2015-07-22 18:37:18 +00:00
|
|
|
{
|
2015-07-28 07:38:16 +00:00
|
|
|
MOZ_ASSERT(mSupportsTransferring);
|
|
|
|
|
2015-07-22 18:37:18 +00:00
|
|
|
if (aTag == SCTAG_DOM_MAP_MESSAGEPORT) {
|
|
|
|
MOZ_ASSERT(!aContent);
|
|
|
|
MOZ_ASSERT(aExtraData < mPortIdentifiers.Length());
|
|
|
|
MessagePort::ForceClose(mPortIdentifiers[aExtraData]);
|
2015-10-12 03:21:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_CANVAS) {
|
2016-07-21 13:29:42 +00:00
|
|
|
MOZ_ASSERT(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::SameProcessDifferentThread);
|
2015-10-12 03:21:03 +00:00
|
|
|
MOZ_ASSERT(aContent);
|
|
|
|
OffscreenCanvasCloneData* data =
|
|
|
|
static_cast<OffscreenCanvasCloneData*>(aContent);
|
|
|
|
delete data;
|
|
|
|
return;
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
2015-12-18 06:52:16 +00:00
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_IMAGEBITMAP) {
|
2016-07-21 13:29:42 +00:00
|
|
|
MOZ_ASSERT(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::SameProcessDifferentThread);
|
2015-12-18 06:52:16 +00:00
|
|
|
MOZ_ASSERT(aContent);
|
|
|
|
ImageBitmapCloneData* data =
|
|
|
|
static_cast<ImageBitmapCloneData*>(aContent);
|
|
|
|
delete data;
|
|
|
|
return;
|
|
|
|
}
|
2015-07-22 18:37:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 07:54:41 +00:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|