2015-07-19 10:54:41 +03: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 15:22:08 +03:00
|
|
|
#include "StructuredCloneHolder.h"
|
2015-07-19 10:54:41 +03:00
|
|
|
|
2015-07-30 21:50:00 +03:00
|
|
|
#include "ImageContainer.h"
|
2015-08-19 00:55:21 +03:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2015-07-22 21:37:18 +03:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
2015-09-14 20:05:44 +03:00
|
|
|
#include "mozilla/dom/CryptoKey.h"
|
2016-03-20 13:56:10 +03:00
|
|
|
#include "mozilla/dom/Directory.h"
|
2015-08-19 00:55:21 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
|
|
|
#include "mozilla/dom/FileList.h"
|
2015-07-22 21:37:18 +03:00
|
|
|
#include "mozilla/dom/FileListBinding.h"
|
2016-01-07 22:30:36 +03:00
|
|
|
#include "mozilla/dom/FormData.h"
|
2015-07-30 21:50:00 +03:00
|
|
|
#include "mozilla/dom/ImageBitmap.h"
|
|
|
|
#include "mozilla/dom/ImageBitmapBinding.h"
|
2015-08-27 19:19:13 +03:00
|
|
|
#include "mozilla/dom/ImageData.h"
|
|
|
|
#include "mozilla/dom/ImageDataBinding.h"
|
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
|
|
|
#include "mozilla/dom/StructuredClone.h"
|
2015-08-19 00:55:21 +03:00
|
|
|
#include "mozilla/dom/MessagePort.h"
|
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "mozilla/dom/OffscreenCanvas.h"
|
|
|
|
#include "mozilla/dom/OffscreenCanvasBinding.h"
|
2015-08-19 00:55:21 +03:00
|
|
|
#include "mozilla/dom/PMessagePort.h"
|
2015-07-22 21:37:18 +03:00
|
|
|
#include "mozilla/dom/StructuredCloneTags.h"
|
2015-09-14 20:05:44 +03:00
|
|
|
#include "mozilla/dom/SubtleCryptoBinding.h"
|
2015-08-19 00:55:21 +03:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2015-08-27 19:19:13 +03:00
|
|
|
#include "mozilla/dom/WebCryptoCommon.h"
|
2015-12-18 09:52:16 +03:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2015-08-27 19:19:13 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
2015-09-14 20:05:44 +03:00
|
|
|
#include "mozilla/ipc/PBackgroundSharedTypes.h"
|
2015-08-27 19:19:13 +03:00
|
|
|
#include "MultipartBlobImpl.h"
|
|
|
|
#include "nsIRemoteBlob.h"
|
|
|
|
#include "nsQueryObject.h"
|
|
|
|
|
2015-09-14 20:05:44 +03:00
|
|
|
#ifdef MOZ_NFC
|
|
|
|
#include "mozilla/dom/MozNDEFRecord.h"
|
|
|
|
#endif // MOZ_NFC
|
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
#include "mozilla/dom/RTCCertificate.h"
|
|
|
|
#include "mozilla/dom/RTCCertificateBinding.h"
|
|
|
|
#endif
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
using namespace mozilla::ipc;
|
2015-07-22 21:37:18 +03:00
|
|
|
|
2015-07-19 10:54:41 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
StructuredCloneCallbacksRead(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag, uint32_t aIndex,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomReadHandler(aCx, aReader, aTag, aIndex);
|
2015-07-19 10:54:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
StructuredCloneCallbacksWrite(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomWriteHandler(aCx, aWriter, aObj);
|
2015-07-19 10:54:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
StructuredCloneCallbacksReadTransfer(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData,
|
|
|
|
void* aClosure,
|
|
|
|
JS::MutableHandleObject aReturnObject)
|
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomReadTransferHandler(aCx, aReader, aTag, aContent,
|
|
|
|
aExtraData, aReturnObject);
|
2015-07-19 10:54:41 +03: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 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomWriteTransferHandler(aCx, aObj, aTag, aOwnership,
|
|
|
|
aContent, aExtraData);
|
2015-07-19 10:54:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StructuredCloneCallbacksFreeTransfer(uint32_t aTag,
|
|
|
|
JS::TransferableOwnership aOwnership,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomFreeTransferHandler(aTag, aOwnership, aContent,
|
|
|
|
aExtraData);
|
2015-07-19 10:54:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StructuredCloneCallbacksError(JSContext* aCx,
|
|
|
|
uint32_t aErrorId)
|
|
|
|
{
|
2015-07-24 14:12:51 +03:00
|
|
|
NS_WARNING("Failed to clone data.");
|
2015-07-19 10:54:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const JSStructuredCloneCallbacks gCallbacks = {
|
|
|
|
StructuredCloneCallbacksRead,
|
|
|
|
StructuredCloneCallbacksWrite,
|
|
|
|
StructuredCloneCallbacksError,
|
|
|
|
StructuredCloneCallbacksReadTransfer,
|
|
|
|
StructuredCloneCallbacksWriteTransfer,
|
|
|
|
StructuredCloneCallbacksFreeTransfer
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
// StructuredCloneHolderBase class
|
2015-07-22 21:37:18 +03:00
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase::StructuredCloneHolderBase()
|
2015-07-24 14:12:51 +03:00
|
|
|
#ifdef DEBUG
|
2015-09-30 15:22:08 +03:00
|
|
|
: mClearCalled(false)
|
2015-07-24 14:12:51 +03:00
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase::~StructuredCloneHolderBase()
|
2015-07-24 14:12:51 +03:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(mClearCalled);
|
2015-07-24 14:12:51 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase::Clear()
|
2015-07-24 14:12:51 +03:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2015-09-30 15:22:08 +03:00
|
|
|
mClearCalled = true;
|
2015-07-24 14:12:51 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
mBuffer = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-19 10:54:41 +03:00
|
|
|
bool
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase::Write(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
2015-07-19 10:54:41 +03:00
|
|
|
{
|
2015-08-27 19:19:13 +03:00
|
|
|
return Write(aCx, aValue, JS::UndefinedHandleValue);
|
2015-07-19 10:54:41 +03:00
|
|
|
}
|
|
|
|
|
2015-07-22 21:37:18 +03:00
|
|
|
bool
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase::Write(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
JS::Handle<JS::Value> aTransfer)
|
2015-07-22 21:37:18 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mBuffer, "Double Write is not allowed");
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(!mClearCalled, "This method cannot be called after Clear.");
|
2015-07-22 21:37:18 +03:00
|
|
|
|
|
|
|
mBuffer = new JSAutoStructuredCloneBuffer(&gCallbacks, this);
|
2015-09-02 19:20:30 +03:00
|
|
|
|
|
|
|
if (!mBuffer->write(aCx, aValue, aTransfer, &gCallbacks, this)) {
|
|
|
|
mBuffer = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2015-07-19 10:54:41 +03:00
|
|
|
bool
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase::Read(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aValue)
|
2015-07-19 10:54:41 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mBuffer, "Read() without Write() is not allowed.");
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(!mClearCalled, "This method cannot be called after Clear.");
|
2015-07-19 10:54:41 +03:00
|
|
|
|
|
|
|
bool ok = mBuffer->read(aCx, aValue, &gCallbacks, this);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase::CustomReadTransferHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData,
|
|
|
|
JS::MutableHandleObject aReturnObject)
|
2015-07-19 10:54:41 +03:00
|
|
|
{
|
|
|
|
MOZ_CRASH("Nothing to read.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase::CustomWriteTransferHandler(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
uint32_t* aTag,
|
|
|
|
JS::TransferableOwnership* aOwnership,
|
|
|
|
void** aContent,
|
|
|
|
uint64_t* aExtraData)
|
2015-07-19 10:54:41 +03:00
|
|
|
{
|
|
|
|
// No transfers are supported by default.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolderBase::CustomFreeTransferHandler(uint32_t aTag,
|
|
|
|
JS::TransferableOwnership aOwnership,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData)
|
2015-07-19 10:54:41 +03:00
|
|
|
{
|
|
|
|
MOZ_CRASH("Nothing to free.");
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
// StructuredCloneHolder class
|
2015-07-22 21:37:18 +03:00
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::StructuredCloneHolder(CloningSupport aSupportsCloning,
|
2015-09-02 19:20:30 +03:00
|
|
|
TransferringSupport aSupportsTransferring,
|
|
|
|
ContextSupport aContext)
|
2015-07-28 10:38:16 +03:00
|
|
|
: mSupportsCloning(aSupportsCloning == CloningSupported)
|
|
|
|
, mSupportsTransferring(aSupportsTransferring == TransferringSupported)
|
2015-09-30 15:22:08 +03:00
|
|
|
, mSupportedContext(aContext)
|
2015-07-22 21:37:18 +03:00
|
|
|
, mParent(nullptr)
|
2015-09-02 19:20:30 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mCreationThread(NS_GetCurrentThread())
|
|
|
|
#endif
|
2015-07-22 21:37:18 +03:00
|
|
|
{}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::~StructuredCloneHolder()
|
2015-07-24 14:12:51 +03:00
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
Clear();
|
2015-08-27 19:19:13 +03:00
|
|
|
MOZ_ASSERT(mTransferredPorts.IsEmpty());
|
2015-07-24 14:12:51 +03:00
|
|
|
}
|
2015-07-22 21:37:18 +03:00
|
|
|
|
2015-07-31 03:38:00 +03:00
|
|
|
void
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::Write(JSContext* aCx,
|
2015-07-31 03:38:00 +03:00
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv)
|
2015-07-28 10:38:16 +03:00
|
|
|
{
|
2015-09-02 19:20:30 +03:00
|
|
|
Write(aCx, aValue, JS::UndefinedHandleValue, aRv);
|
2015-07-28 10:38:16 +03:00
|
|
|
}
|
|
|
|
|
2015-07-31 03:38:00 +03:00
|
|
|
void
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::Write(JSContext* aCx,
|
2015-07-22 21:37:18 +03:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2015-07-31 03:38:00 +03:00
|
|
|
JS::Handle<JS::Value> aTransfer,
|
|
|
|
ErrorResult& aRv)
|
2015-07-22 21:37:18 +03:00
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT_IF(mSupportedContext == SameProcessSameThread,
|
2015-09-02 19:20:30 +03:00
|
|
|
mCreationThread == NS_GetCurrentThread());
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
if (!StructuredCloneHolderBase::Write(aCx, aValue, aTransfer)) {
|
2015-07-31 03:38:00 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
2015-08-27 19:19:13 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
if (mSupportedContext != SameProcessSameThread) {
|
2015-08-27 19:19:13 +03:00
|
|
|
for (uint32_t i = 0, len = mBlobImplArray.Length(); i < len; ++i) {
|
|
|
|
if (!mBlobImplArray[i]->MayBeClonedToOtherThreads()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 03:38:00 +03:00
|
|
|
}
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2015-07-31 03:38:00 +03:00
|
|
|
void
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::Read(nsISupports* aParent,
|
2015-07-22 21:37:18 +03:00
|
|
|
JSContext* aCx,
|
2015-07-31 03:38:00 +03:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv)
|
2015-07-22 21:37:18 +03:00
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT_IF(mSupportedContext == SameProcessSameThread,
|
2015-09-02 19:20:30 +03:00
|
|
|
mCreationThread == NS_GetCurrentThread());
|
2016-03-01 17:21:11 +03:00
|
|
|
MOZ_ASSERT(aParent);
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2015-07-22 21:37:18 +03:00
|
|
|
mozilla::AutoRestore<nsISupports*> guard(mParent);
|
|
|
|
mParent = aParent;
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
if (!StructuredCloneHolderBase::Read(aCx, aValue)) {
|
2015-07-31 03:38:00 +03:00
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
// If we are tranferring something, we cannot call 'Read()' more than once.
|
|
|
|
if (mSupportsTransferring) {
|
|
|
|
mBlobImplArray.Clear();
|
2015-12-18 09:52:16 +03:00
|
|
|
mClonedSurfaces.Clear();
|
2015-09-30 15:22:08 +03:00
|
|
|
Clear();
|
2015-08-26 15:17:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::ReadFromBuffer(nsISupports* aParent,
|
2015-08-26 15:17:23 +03:00
|
|
|
JSContext* aCx,
|
|
|
|
uint64_t* aBuffer,
|
|
|
|
size_t aBufferLength,
|
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
ReadFromBuffer(aParent, aCx, aBuffer, aBufferLength,
|
|
|
|
JS_STRUCTURED_CLONE_VERSION, aValue, aRv);
|
2015-07-28 10:38:16 +03:00
|
|
|
}
|
|
|
|
|
2015-07-31 03:38:00 +03:00
|
|
|
void
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::ReadFromBuffer(nsISupports* aParent,
|
2015-07-28 10:38:16 +03:00
|
|
|
JSContext* aCx,
|
|
|
|
uint64_t* aBuffer,
|
|
|
|
size_t aBufferLength,
|
2015-08-26 15:17:23 +03:00
|
|
|
uint32_t aAlgorithmVersion,
|
2015-07-31 03:38:00 +03:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv)
|
2015-07-28 10:38:16 +03:00
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT_IF(mSupportedContext == SameProcessSameThread,
|
2015-09-02 19:20:30 +03:00
|
|
|
mCreationThread == NS_GetCurrentThread());
|
|
|
|
|
2015-07-28 10:38:16 +03:00
|
|
|
MOZ_ASSERT(!mBuffer, "ReadFromBuffer() must be called without a Write().");
|
|
|
|
MOZ_ASSERT(aBuffer);
|
|
|
|
|
|
|
|
mozilla::AutoRestore<nsISupports*> guard(mParent);
|
|
|
|
mParent = aParent;
|
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
if (!JS_ReadStructuredClone(aCx, aBuffer, aBufferLength, aAlgorithmVersion,
|
|
|
|
aValue, &gCallbacks, this)) {
|
2015-07-31 03:38:00 +03:00
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
}
|
2015-07-28 13:47:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::MoveBufferDataToArray(FallibleTArray<uint8_t>& aArray,
|
2015-07-28 13:47:36 +03:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT_IF(mSupportedContext == SameProcessSameThread,
|
2015-09-02 19:20:30 +03:00
|
|
|
mCreationThread == NS_GetCurrentThread());
|
|
|
|
|
2015-07-28 13:47:36 +03:00
|
|
|
MOZ_ASSERT(mBuffer, "MoveBuffer() cannot be called without a Write().");
|
2015-07-28 10:38:16 +03:00
|
|
|
|
2015-07-28 13:47:36 +03:00
|
|
|
if (NS_WARN_IF(!aArray.SetLength(BufferSize(), mozilla::fallible))) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-28 10:38:16 +03:00
|
|
|
|
2015-07-28 13:47:36 +03:00
|
|
|
uint64_t* buffer;
|
|
|
|
size_t size;
|
|
|
|
mBuffer->steal(&buffer, &size);
|
|
|
|
mBuffer = nullptr;
|
|
|
|
|
|
|
|
memcpy(aArray.Elements(), buffer, size);
|
|
|
|
js_free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::FreeBuffer(uint64_t* aBuffer,
|
2015-07-28 13:47:36 +03:00
|
|
|
size_t aBufferLength)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mBuffer, "FreeBuffer() must be called without a Write().");
|
|
|
|
MOZ_ASSERT(aBuffer);
|
|
|
|
MOZ_ASSERT(aBufferLength);
|
|
|
|
|
|
|
|
JS_ClearStructuredClone(aBuffer, aBufferLength, &gCallbacks, this, false);
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2015-09-14 20:05:44 +03:00
|
|
|
/* static */ JSObject*
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::ReadFullySerializableObjects(JSContext* aCx,
|
2015-09-14 20:05:44 +03:00
|
|
|
JSStructuredCloneReader* aReader,
|
2015-10-07 22:32:14 +03:00
|
|
|
uint32_t aTag)
|
2015-09-14 20:05:44 +03:00
|
|
|
{
|
|
|
|
if (aTag == SCTAG_DOM_IMAGEDATA) {
|
|
|
|
return ReadStructuredCloneImageData(aCx, aReader);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_WEBCRYPTO_KEY) {
|
|
|
|
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 08:24:48 +03:00
|
|
|
RefPtr<CryptoKey> key = new CryptoKey(global);
|
2015-09-14 20:05:44 +03:00
|
|
|
if (!key->ReadStructuredClone(aReader)) {
|
|
|
|
result = nullptr;
|
|
|
|
} else {
|
|
|
|
result = key->WrapObject(aCx, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_NULL_PRINCIPAL ||
|
|
|
|
aTag == SCTAG_DOM_SYSTEM_PRINCIPAL ||
|
2016-02-05 02:12:52 +03:00
|
|
|
aTag == SCTAG_DOM_CONTENT_PRINCIPAL ||
|
|
|
|
aTag == SCTAG_DOM_EXPANDED_PRINCIPAL) {
|
2015-10-02 17:44:00 +03:00
|
|
|
JSPrincipals* prin;
|
|
|
|
if (!nsJSPrincipals::ReadKnownPrincipalType(aCx, aReader, aTag, &prin)) {
|
2015-09-14 20:05:44 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-02 17:44:00 +03: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 20:05:44 +03:00
|
|
|
|
|
|
|
JS::RootedValue result(aCx);
|
2015-10-02 17:44:00 +03:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(aCx, principal,
|
|
|
|
&NS_GET_IID(nsIPrincipal),
|
|
|
|
&result);
|
2015-09-14 20:05:44 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.toObjectOrNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_NFC
|
|
|
|
if (aTag == SCTAG_DOM_NFC_NDEF) {
|
|
|
|
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 08:24:48 +03:00
|
|
|
RefPtr<MozNDEFRecord> ndefRecord = new MozNDEFRecord(global);
|
2015-09-14 20:05:44 +03:00
|
|
|
result = ndefRecord->ReadStructuredClone(aCx, aReader) ?
|
|
|
|
ndefRecord->WrapObject(aCx, nullptr) : nullptr;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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 08:24:48 +03:00
|
|
|
RefPtr<RTCCertificate> cert = new RTCCertificate(global);
|
2015-09-14 20:05:44 +03: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 15:22:08 +03:00
|
|
|
StructuredCloneHolder::WriteFullySerializableObjects(JSContext* aCx,
|
2015-09-14 20:05:44 +03: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Key cloning
|
|
|
|
{
|
2015-10-15 12:37:00 +03:00
|
|
|
CryptoKey* key = nullptr;
|
2015-09-14 20:05:44 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(CryptoKey, aObj, key))) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_WEBCRYPTO_KEY, 0) &&
|
|
|
|
key->WriteStructuredClone(aWriter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
{
|
|
|
|
// Handle WebRTC Certificate cloning
|
2015-10-15 12:37:00 +03:00
|
|
|
RTCCertificate* cert = nullptr;
|
2015-09-14 20:05:44 +03: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 17:44:00 +03:00
|
|
|
auto nsjsprincipals = nsJSPrincipals::get(principal);
|
|
|
|
return nsjsprincipals->write(aCx, aWriter);
|
2015-09-14 20:05:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_NFC
|
|
|
|
{
|
2015-10-15 12:37:00 +03:00
|
|
|
MozNDEFRecord* ndefRecord = nullptr;
|
2015-09-14 20:05:44 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(MozNDEFRecord, aObj, ndefRecord))) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_NFC_NDEF, 0) &&
|
|
|
|
ndefRecord->WriteStructuredClone(aCx, aWriter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // MOZ_NFC
|
|
|
|
|
|
|
|
// Don't know what this is
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Recursive!
|
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
EnsureBlobForBackgroundManager(BlobImpl* aBlobImpl,
|
2015-12-05 00:15:46 +03:00
|
|
|
PBackgroundChild* aManager,
|
|
|
|
ErrorResult& aRv)
|
2015-08-27 19:19:13 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBlobImpl);
|
2015-11-28 13:48:28 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = aBlobImpl;
|
2015-08-27 19:19:13 +03:00
|
|
|
|
|
|
|
if (!aManager) {
|
|
|
|
aManager = BackgroundChild::GetForCurrentThread();
|
2015-11-28 13:48:28 +03:00
|
|
|
if (!aManager) {
|
|
|
|
return blobImpl.forget();
|
|
|
|
}
|
2015-08-27 19:19:13 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
const nsTArray<RefPtr<BlobImpl>>* subBlobImpls =
|
2015-08-27 19:19:13 +03: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 {
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(blobImpl->SetMutable(false)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobImpl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t subBlobCount = subBlobImpls->Length();
|
|
|
|
MOZ_ASSERT(subBlobCount);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<BlobImpl>> newSubBlobImpls;
|
2015-08-27 19:19:13 +03:00
|
|
|
newSubBlobImpls.SetLength(subBlobCount);
|
|
|
|
|
|
|
|
bool newBlobImplNeeded = false;
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < subBlobCount; index++) {
|
2015-10-18 08:24:48 +03:00
|
|
|
const RefPtr<BlobImpl>& subBlobImpl = subBlobImpls->ElementAt(index);
|
2015-08-27 19:19:13 +03:00
|
|
|
MOZ_ASSERT(subBlobImpl);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl>& newSubBlobImpl = newSubBlobImpls[index];
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2015-12-05 00:15:46 +03:00
|
|
|
newSubBlobImpl = EnsureBlobForBackgroundManager(subBlobImpl, aManager, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
MOZ_ASSERT(newSubBlobImpl);
|
|
|
|
|
|
|
|
if (subBlobImpl != newSubBlobImpl) {
|
|
|
|
newBlobImplNeeded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newBlobImplNeeded) {
|
|
|
|
nsString contentType;
|
|
|
|
blobImpl->GetType(contentType);
|
|
|
|
|
|
|
|
if (blobImpl->IsFile()) {
|
|
|
|
nsString name;
|
|
|
|
blobImpl->GetName(name);
|
|
|
|
|
2015-12-05 00:15:46 +03:00
|
|
|
blobImpl = MultipartBlobImpl::Create(newSubBlobImpls, name,
|
|
|
|
contentType, aRv);
|
2015-08-27 19:19:13 +03:00
|
|
|
} else {
|
2015-12-05 00:15:46 +03:00
|
|
|
blobImpl = MultipartBlobImpl::Create(newSubBlobImpls, contentType, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2015-08-27 19:19:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(blobImpl->SetMutable(false)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobImpl.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-22 21:37:18 +03:00
|
|
|
JSObject*
|
2015-08-27 19:19:13 +03:00
|
|
|
ReadBlob(JSContext* aCx,
|
|
|
|
uint32_t aIndex,
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 11:06:09 +03:00
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(aHolder);
|
|
|
|
MOZ_ASSERT(aIndex < aHolder->BlobImpls().Length());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = aHolder->BlobImpls()[aIndex];
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2015-12-05 00:15:46 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
blobImpl = EnsureBlobForBackgroundManager(blobImpl, nullptr, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
// RefPtr<File> needs to go out of scope before toObjectOrNull() is
|
2015-08-27 19:19:13 +03: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 08:24:48 +03:00
|
|
|
RefPtr<Blob> blob = Blob::Create(aHolder->ParentDuringRead(), blobImpl);
|
2015-08-27 19:19:13 +03:00
|
|
|
if (!ToJSValue(aCx, blob, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 11:06:09 +03:00
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
return &val.toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WriteBlob(JSStructuredCloneWriter* aWriter,
|
|
|
|
Blob* aBlob,
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 19:19:13 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWriter);
|
|
|
|
MOZ_ASSERT(aBlob);
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(aHolder);
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2015-12-05 00:15:46 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
RefPtr<BlobImpl> blobImpl =
|
|
|
|
EnsureBlobForBackgroundManager(aBlob->Impl(), nullptr, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
2015-11-28 13:48:28 +03:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(blobImpl->SetMutable(false)));
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
// We store the position of the blobImpl in the array as index.
|
|
|
|
if (JS_WriteUint32Pair(aWriter, SCTAG_DOM_BLOB,
|
2015-09-30 15:22:08 +03:00
|
|
|
aHolder->BlobImpls().Length())) {
|
|
|
|
aHolder->BlobImpls().AppendElement(blobImpl);
|
2015-08-27 19:19:13 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the WriteFileList for the format.
|
|
|
|
JSObject*
|
|
|
|
ReadFileList(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aCount,
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 19:19:13 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aReader);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FileList> fileList = new FileList(aHolder->ParentDuringRead());
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
// |aCount| is the number of Files or Directory for this FileList.
|
|
|
|
for (uint32_t i = 0; i < aCount; ++i) {
|
|
|
|
uint32_t tagOrDirectoryType, indexOrLengthOfString;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &tagOrDirectoryType,
|
|
|
|
&indexOrLengthOfString)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(tagOrDirectoryType == SCTAG_DOM_BLOB ||
|
|
|
|
tagOrDirectoryType == Directory::eDOMRootDirectory ||
|
|
|
|
tagOrDirectoryType == Directory::eNotDOMRootDirectory);
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
if (tagOrDirectoryType == SCTAG_DOM_BLOB) {
|
|
|
|
MOZ_ASSERT(indexOrLengthOfString < aHolder->BlobImpls().Length());
|
2016-03-20 00:29:46 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl =
|
|
|
|
aHolder->BlobImpls()[indexOrLengthOfString];
|
|
|
|
MOZ_ASSERT(blobImpl->IsFile());
|
2015-12-05 00:15:46 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
blobImpl = EnsureBlobForBackgroundManager(blobImpl, nullptr, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<File> file =
|
|
|
|
File::Create(aHolder->ParentDuringRead(), blobImpl);
|
|
|
|
MOZ_ASSERT(file);
|
|
|
|
|
|
|
|
fileList->Append(file);
|
|
|
|
continue;
|
2016-03-20 00:29:46 +03:00
|
|
|
}
|
2015-08-29 02:19:56 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsAutoString path;
|
|
|
|
path.SetLength(indexOrLengthOfString);
|
|
|
|
size_t charSize = sizeof(nsString::char_type);
|
|
|
|
if (!JS_ReadBytes(aReader, (void*) path.BeginWriting(),
|
|
|
|
indexOrLengthOfString * charSize)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-20 05:44:22 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = NS_NewNativeLocalFile(NS_ConvertUTF16toUTF8(path), true,
|
|
|
|
getter_AddRefs(file));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-07-28 10:38:16 +03:00
|
|
|
return nullptr;
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
RefPtr<Directory> directory =
|
|
|
|
Directory::Create(aHolder->ParentDuringRead(), file,
|
|
|
|
(Directory::DirectoryType) tagOrDirectoryType);
|
|
|
|
fileList->Append(directory);
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
2015-07-28 10:38:16 +03:00
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
if (!ToJSValue(aCx, fileList, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-27 11:06:09 +03:00
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
return &val.toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The format of the FileList serialization is:
|
|
|
|
// - pair of ints: SCTAG_DOM_FILELIST, Length of the FileList
|
2016-03-20 13:56:10 +03:00
|
|
|
// - for each element of the FileList:
|
|
|
|
// - if it's a blob:
|
|
|
|
// - pair of ints: SCTAG_DOM_BLOB, index of the BlobImpl in the array
|
|
|
|
// mBlobImplArray.
|
|
|
|
// - else:
|
|
|
|
// - pair of ints: 0/1 is root, string length
|
|
|
|
// - value string
|
2015-08-27 19:19:13 +03:00
|
|
|
bool
|
|
|
|
WriteFileList(JSStructuredCloneWriter* aWriter,
|
|
|
|
FileList* aFileList,
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 19:19:13 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWriter);
|
|
|
|
MOZ_ASSERT(aFileList);
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(aHolder);
|
2015-08-27 19:19:13 +03:00
|
|
|
|
|
|
|
if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_FILELIST,
|
2016-03-20 13:56:10 +03:00
|
|
|
aFileList->Length())) {
|
2015-08-27 19:19:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-05 00:15:46 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
nsTArray<RefPtr<BlobImpl>> blobImpls;
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
for (uint32_t i = 0; i < aFileList->Length(); ++i) {
|
2016-03-20 13:56:10 +03:00
|
|
|
const OwningFileOrDirectory& data = aFileList->UnsafeItem(i);
|
|
|
|
|
|
|
|
if (data.IsFile()) {
|
|
|
|
RefPtr<BlobImpl> blobImpl =
|
|
|
|
EnsureBlobForBackgroundManager(data.GetAsFile()->Impl(), nullptr, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_BLOB,
|
|
|
|
aHolder->BlobImpls().Length())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aHolder->BlobImpls().AppendElement(blobImpl);
|
|
|
|
continue;
|
2016-03-20 00:29:46 +03:00
|
|
|
}
|
2016-03-20 05:44:22 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(data.IsDirectory());
|
|
|
|
|
|
|
|
nsAutoString path;
|
|
|
|
data.GetAsDirectory()->GetFullRealPath(path);
|
|
|
|
|
|
|
|
size_t charSize = sizeof(nsString::char_type);
|
|
|
|
if (!JS_WriteUint32Pair(aWriter,
|
|
|
|
(uint32_t)data.GetAsDirectory()->Type(),
|
|
|
|
path.Length()) ||
|
|
|
|
!JS_WriteBytes(aWriter, path.get(), path.Length() * charSize)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-27 19:19:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the WriteFormData for the format.
|
|
|
|
JSObject*
|
|
|
|
ReadFormData(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aCount,
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 19:19:13 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aReader);
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(aHolder);
|
2015-08-27 11:06:09 +03:00
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
// See the serialization of the FormData for the format.
|
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
|
|
{
|
2016-01-07 22:30:36 +03:00
|
|
|
RefPtr<FormData> formData =
|
|
|
|
new FormData(aHolder->ParentDuringRead());
|
2015-08-27 19:19:13 +03: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 10:38:16 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
if (tag == SCTAG_DOM_BLOB) {
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(indexOrLengthOfString < aHolder->BlobImpls().Length());
|
2015-07-28 10:38:16 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl =
|
2015-09-30 15:22:08 +03:00
|
|
|
aHolder->BlobImpls()[indexOrLengthOfString];
|
2015-07-28 10:38:16 +03:00
|
|
|
|
2016-01-20 20:25:03 +03:00
|
|
|
RefPtr<Blob> blob =
|
|
|
|
Blob::Create(aHolder->ParentDuringRead(), blobImpl);
|
|
|
|
MOZ_ASSERT(blob);
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2015-12-05 00:15:46 +03:00
|
|
|
ErrorResult rv;
|
2016-01-20 20:25:03 +03:00
|
|
|
formData->Append(name, *blob, thirdArg, rv);
|
2015-12-05 00:15:46 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03: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 21:37:18 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2015-12-05 00:15:46 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
formData->Append(name, value, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-27 19:19:13 +03: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.
|
|
|
|
// - else:
|
|
|
|
// - pair of ints: 0, string length
|
|
|
|
// - value string
|
|
|
|
bool
|
|
|
|
WriteFormData(JSStructuredCloneWriter* aWriter,
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData* aFormData,
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 19:19:13 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWriter);
|
|
|
|
MOZ_ASSERT(aFormData);
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(aHolder);
|
2015-08-27 19:19:13 +03:00
|
|
|
|
|
|
|
if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_FORMDATA,
|
|
|
|
aFormData->Length())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS Closure final
|
|
|
|
{
|
|
|
|
JSStructuredCloneWriter* mWriter;
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder* mHolder;
|
2015-08-27 19:19:13 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
Closure(JSStructuredCloneWriter* aWriter,
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder* aHolder)
|
2015-08-27 19:19:13 +03:00
|
|
|
: mWriter(aWriter),
|
2015-09-30 15:22:08 +03:00
|
|
|
mHolder(aHolder)
|
2015-08-27 19:19:13 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
static bool
|
2016-01-20 20:25:03 +03:00
|
|
|
Write(const nsString& aName, const OwningBlobOrUSVString& aValue,
|
2015-10-20 05:06:00 +03:00
|
|
|
void* aClosure)
|
2015-08-27 19:19:13 +03:00
|
|
|
{
|
|
|
|
Closure* closure = static_cast<Closure*>(aClosure);
|
|
|
|
if (!WriteString(closure->mWriter, aName)) {
|
|
|
|
return false;
|
2015-07-28 10:38:16 +03:00
|
|
|
}
|
2015-08-27 11:06:09 +03:00
|
|
|
|
2016-01-20 20:25:03 +03:00
|
|
|
if (aValue.IsBlob()) {
|
|
|
|
BlobImpl* blobImpl = aValue.GetAsBlob()->Impl();
|
2015-08-27 19:19:13 +03:00
|
|
|
if (!JS_WriteUint32Pair(closure->mWriter, SCTAG_DOM_BLOB,
|
2015-09-30 15:22:08 +03:00
|
|
|
closure->mHolder->BlobImpls().Length())) {
|
2015-08-27 19:19:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
closure->mHolder->BlobImpls().AppendElement(blobImpl);
|
2015-08-27 19:19:13 +03:00
|
|
|
return true;
|
2015-08-27 11:06:09 +03:00
|
|
|
}
|
2015-08-27 19:19:13 +03:00
|
|
|
|
|
|
|
size_t charSize = sizeof(nsString::char_type);
|
2015-10-20 05:06:00 +03:00
|
|
|
if (!JS_WriteUint32Pair(closure->mWriter, 0,
|
|
|
|
aValue.GetAsUSVString().Length()) ||
|
|
|
|
!JS_WriteBytes(closure->mWriter, aValue.GetAsUSVString().get(),
|
|
|
|
aValue.GetAsUSVString().Length() * charSize)) {
|
2015-08-27 19:19:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
2015-08-27 19:19:13 +03:00
|
|
|
};
|
2015-09-30 15:22:08 +03:00
|
|
|
Closure closure(aWriter, aHolder);
|
2015-08-27 19:19:13 +03:00
|
|
|
return aFormData->ForEach(Closure::Write, &closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
JSObject*
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::CustomReadHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
uint32_t aIndex)
|
2015-08-27 19:19:13 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSupportsCloning);
|
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_BLOB) {
|
|
|
|
return ReadBlob(aCx, aIndex, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:50:00 +03:00
|
|
|
if (aTag == SCTAG_DOM_IMAGEBITMAP) {
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(mSupportedContext == SameProcessSameThread ||
|
|
|
|
mSupportedContext == SameProcessDifferentThread);
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2015-09-14 20:05:44 +03: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 09:52:16 +03:00
|
|
|
parent, GetSurfaces(), aIndex);
|
2015-07-30 21:50:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:32:14 +03:00
|
|
|
return ReadFullySerializableObjects(aCx, aReader, aTag);
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::CustomWriteHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj)
|
2015-07-22 21:37:18 +03:00
|
|
|
{
|
2015-07-28 10:38:16 +03:00
|
|
|
if (!mSupportsCloning) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-22 21:37:18 +03:00
|
|
|
// See if this is a File/Blob object.
|
2015-07-28 10:38:16 +03:00
|
|
|
{
|
2015-07-22 21:37:18 +03:00
|
|
|
Blob* blob = nullptr;
|
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(Blob, aObj, blob))) {
|
2015-08-27 19:19:13 +03:00
|
|
|
return WriteBlob(aWriter, blob, this);
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
// See if this is a FileList object.
|
2016-03-20 13:56:10 +03:00
|
|
|
{
|
2015-07-22 21:37:18 +03:00
|
|
|
FileList* fileList = nullptr;
|
2016-03-20 13:56:10 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(FileList, aObj, fileList)) &&
|
|
|
|
(mSupportedContext == SameProcessSameThread ||
|
|
|
|
fileList->ClonableToDifferentThreadOrProcess())) {
|
2015-08-27 19:19:13 +03:00
|
|
|
return WriteFileList(aWriter, fileList, this);
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 10:38:16 +03:00
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
// See if this is a FormData object.
|
|
|
|
{
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData* formData = nullptr;
|
2015-08-27 19:19:13 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(FormData, aObj, formData))) {
|
|
|
|
return WriteFormData(aWriter, formData, this);
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:50:00 +03:00
|
|
|
// See if this is an ImageBitmap object.
|
2015-09-30 15:22:08 +03:00
|
|
|
if (mSupportedContext == SameProcessSameThread ||
|
|
|
|
mSupportedContext == SameProcessDifferentThread) {
|
2015-07-30 21:50:00 +03:00
|
|
|
ImageBitmap* imageBitmap = nullptr;
|
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(ImageBitmap, aObj, imageBitmap))) {
|
|
|
|
return ImageBitmap::WriteStructuredClone(aWriter,
|
2015-12-18 09:52:16 +03:00
|
|
|
GetSurfaces(),
|
2015-07-30 21:50:00 +03:00
|
|
|
imageBitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 20:05:44 +03:00
|
|
|
return WriteFullySerializableObjects(aCx, aWriter, aObj);
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::CustomReadTransferHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData,
|
|
|
|
JS::MutableHandleObject aReturnObject)
|
2015-07-22 21:37:18 +03:00
|
|
|
{
|
2015-07-28 10:38:16 +03:00
|
|
|
MOZ_ASSERT(mSupportsTransferring);
|
2015-07-22 21:37:18 +03:00
|
|
|
|
2015-07-28 10:38:16 +03:00
|
|
|
if (aTag == SCTAG_DOM_MAP_MESSAGEPORT) {
|
2015-07-22 21:37:18 +03:00
|
|
|
MOZ_ASSERT(aExtraData < mPortIdentifiers.Length());
|
|
|
|
const MessagePortIdentifier& portIdentifier = mPortIdentifiers[aExtraData];
|
|
|
|
|
2016-03-01 17:21:11 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mParent);
|
|
|
|
|
2015-07-22 21:37:18 +03:00
|
|
|
ErrorResult rv;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessagePort> port =
|
2016-03-01 17:21:11 +03:00
|
|
|
MessagePort::Create(global, portIdentifier, rv);
|
2015-07-22 21:37:18 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
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 06:21:03 +03:00
|
|
|
if (aTag == SCTAG_DOM_CANVAS) {
|
|
|
|
MOZ_ASSERT(mSupportedContext == SameProcessSameThread ||
|
|
|
|
mSupportedContext == SameProcessDifferentThread);
|
|
|
|
MOZ_ASSERT(aContent);
|
|
|
|
OffscreenCanvasCloneData* data =
|
|
|
|
static_cast<OffscreenCanvasCloneData*>(aContent);
|
2015-12-18 09:52:17 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> parent = do_QueryInterface(mParent);
|
|
|
|
RefPtr<OffscreenCanvas> canvas = OffscreenCanvas::CreateFromCloneData(parent, data);
|
2015-10-12 06:21:03 +03: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 09:52:16 +03:00
|
|
|
if (aTag == SCTAG_DOM_IMAGEBITMAP) {
|
|
|
|
MOZ_ASSERT(mSupportedContext == SameProcessSameThread ||
|
|
|
|
mSupportedContext == SameProcessDifferentThread);
|
|
|
|
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 21:37:18 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::CustomWriteTransferHandler(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
uint32_t* aTag,
|
|
|
|
JS::TransferableOwnership* aOwnership,
|
|
|
|
void** aContent,
|
|
|
|
uint64_t* aExtraData)
|
2015-07-22 21:37:18 +03:00
|
|
|
{
|
2015-07-28 10:38:16 +03:00
|
|
|
if (!mSupportsTransferring) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-09-16 06:27:56 +03:00
|
|
|
MessagePort* port = nullptr;
|
2015-07-22 21:37:18 +03: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 06:27:56 +03:00
|
|
|
port->CloneAndDisentangle(*identifier);
|
2015-07-22 21:37:18 +03:00
|
|
|
|
|
|
|
*aTag = SCTAG_DOM_MAP_MESSAGEPORT;
|
|
|
|
*aOwnership = JS::SCTAG_TMO_CUSTOM;
|
|
|
|
*aContent = nullptr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
if (mSupportedContext == SameProcessSameThread ||
|
|
|
|
mSupportedContext == SameProcessDifferentThread) {
|
|
|
|
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 09:52:16 +03: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 06:21:03 +03:00
|
|
|
}
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::CustomFreeTransferHandler(uint32_t aTag,
|
|
|
|
JS::TransferableOwnership aOwnership,
|
|
|
|
void* aContent,
|
|
|
|
uint64_t aExtraData)
|
2015-07-22 21:37:18 +03:00
|
|
|
{
|
2015-07-28 10:38:16 +03:00
|
|
|
MOZ_ASSERT(mSupportsTransferring);
|
|
|
|
|
2015-07-22 21:37:18 +03:00
|
|
|
if (aTag == SCTAG_DOM_MAP_MESSAGEPORT) {
|
|
|
|
MOZ_ASSERT(!aContent);
|
|
|
|
MOZ_ASSERT(aExtraData < mPortIdentifiers.Length());
|
|
|
|
MessagePort::ForceClose(mPortIdentifiers[aExtraData]);
|
2015-10-12 06:21:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_CANVAS) {
|
|
|
|
MOZ_ASSERT(mSupportedContext == SameProcessSameThread ||
|
|
|
|
mSupportedContext == SameProcessDifferentThread);
|
|
|
|
MOZ_ASSERT(aContent);
|
|
|
|
OffscreenCanvasCloneData* data =
|
|
|
|
static_cast<OffscreenCanvasCloneData*>(aContent);
|
|
|
|
delete data;
|
|
|
|
return;
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
2015-12-18 09:52:16 +03:00
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_IMAGEBITMAP) {
|
|
|
|
MOZ_ASSERT(mSupportedContext == SameProcessSameThread ||
|
|
|
|
mSupportedContext == SameProcessDifferentThread);
|
|
|
|
MOZ_ASSERT(aContent);
|
|
|
|
ImageBitmapCloneData* data =
|
|
|
|
static_cast<ImageBitmapCloneData*>(aContent);
|
|
|
|
delete data;
|
|
|
|
return;
|
|
|
|
}
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2015-07-19 10:54:41 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|