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"
|
2019-06-25 09:44:38 +03:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2015-07-22 21:37:18 +03:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
2019-07-02 01:24:09 +03:00
|
|
|
#include "mozilla/dom/BrowsingContext.h"
|
|
|
|
#include "mozilla/dom/BrowsingContextBinding.h"
|
2017-05-21 01:09:24 +03:00
|
|
|
#include "mozilla/dom/StructuredCloneBlob.h"
|
2016-03-20 13:56:10 +03:00
|
|
|
#include "mozilla/dom/Directory.h"
|
2016-04-12 15:51:04 +03:00
|
|
|
#include "mozilla/dom/DirectoryBinding.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-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-08-19 00:55:21 +03:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2019-06-25 09:44:38 +03:00
|
|
|
#include "mozilla/dom/WebIDLSerializable.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 "nsQueryObject.h"
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-28 10:23:17 +03:00
|
|
|
bool StructuredCloneCallbacksCanTransfer(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aObject,
|
|
|
|
void* aClosure) {
|
|
|
|
StructuredCloneHolderBase* holder =
|
|
|
|
static_cast<StructuredCloneHolderBase*>(aClosure);
|
|
|
|
MOZ_ASSERT(holder);
|
|
|
|
return holder->CustomCanTransferHandler(aCx, aObject);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-13 20:37:15 +03:00
|
|
|
void AssertTagValues() {
|
|
|
|
static_assert(SCTAG_DOM_IMAGEDATA == 0xffff8007 &&
|
2019-06-15 20:26:25 +03:00
|
|
|
SCTAG_DOM_DOMPOINT == 0xffff8008 &&
|
2019-06-25 09:46:20 +03:00
|
|
|
SCTAG_DOM_DOMPOINTREADONLY == 0xffff8009 &&
|
2019-06-25 09:48:41 +03:00
|
|
|
SCTAG_DOM_CRYPTOKEY == 0xffff800a &&
|
2019-06-13 20:37:15 +03:00
|
|
|
SCTAG_DOM_NULL_PRINCIPAL == 0xffff800b &&
|
|
|
|
SCTAG_DOM_SYSTEM_PRINCIPAL == 0xffff800c &&
|
|
|
|
SCTAG_DOM_CONTENT_PRINCIPAL == 0xffff800d &&
|
2019-06-15 20:26:25 +03:00
|
|
|
SCTAG_DOM_DOMQUAD == 0xffff800e &&
|
2019-06-25 09:49:06 +03:00
|
|
|
SCTAG_DOM_RTCCERTIFICATE == 0xffff800f &&
|
2019-06-15 20:26:25 +03:00
|
|
|
SCTAG_DOM_DOMRECT == 0xffff8010 &&
|
2019-06-25 09:46:47 +03:00
|
|
|
SCTAG_DOM_DOMRECTREADONLY == 0xffff8011 &&
|
2019-06-15 20:26:25 +03:00
|
|
|
SCTAG_DOM_EXPANDED_PRINCIPAL == 0xffff8012 &&
|
|
|
|
SCTAG_DOM_DOMMATRIX == 0xffff8013 &&
|
2019-06-13 20:37:15 +03:00
|
|
|
SCTAG_DOM_URLSEARCHPARAMS == 0xffff8014 &&
|
2019-06-25 09:48:29 +03:00
|
|
|
SCTAG_DOM_DOMMATRIXREADONLY == 0xffff8015 &&
|
2019-06-30 19:43:10 +03:00
|
|
|
SCTAG_DOM_STRUCTUREDCLONETESTER == 0xffff8018,
|
2019-06-13 20:37:15 +03:00
|
|
|
"Something has changed the sctag values. This is wrong!");
|
|
|
|
}
|
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
const JSStructuredCloneCallbacks StructuredCloneHolder::sCallbacks = {
|
2015-07-19 10:54:41 +03:00
|
|
|
StructuredCloneCallbacksRead, StructuredCloneCallbacksWrite,
|
|
|
|
StructuredCloneCallbacksError, StructuredCloneCallbacksReadTransfer,
|
|
|
|
StructuredCloneCallbacksWriteTransfer, StructuredCloneCallbacksFreeTransfer,
|
2018-03-28 10:23:17 +03:00
|
|
|
StructuredCloneCallbacksCanTransfer,
|
2015-07-19 10:54:41 +03:00
|
|
|
};
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
// StructuredCloneHolderBase class
|
2015-07-22 21:37:18 +03:00
|
|
|
|
2016-07-21 16:29:42 +03:00
|
|
|
StructuredCloneHolderBase::StructuredCloneHolderBase(
|
|
|
|
StructuredCloneScope aScope)
|
|
|
|
: mStructuredCloneScope(aScope)
|
2015-07-24 14:12:51 +03:00
|
|
|
#ifdef DEBUG
|
2016-07-21 16:29:42 +03:00
|
|
|
,
|
|
|
|
mClearCalled(false)
|
2015-07-24 14:12:51 +03:00
|
|
|
#endif
|
|
|
|
{
|
2018-11-30 13:46:48 +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
|
|
|
MOZ_ASSERT(mClearCalled);
|
2015-07-24 14:12:51 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
void 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-09-30 15:22:08 +03:00
|
|
|
bool StructuredCloneHolderBase::Write(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) {
|
2016-10-24 16:14:45 +03:00
|
|
|
return Write(aCx, aValue, JS::UndefinedHandleValue,
|
|
|
|
JS::CloneDataPolicy().denySharedArrayBuffer());
|
2015-07-19 10:54:41 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
bool StructuredCloneHolderBase::Write(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
2016-10-24 16:14:45 +03:00
|
|
|
JS::Handle<JS::Value> aTransfer,
|
|
|
|
JS::CloneDataPolicy cloneDataPolicy) {
|
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
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
mBuffer = MakeUnique<JSAutoStructuredCloneBuffer>(
|
|
|
|
mStructuredCloneScope, &StructuredCloneHolder::sCallbacks, this);
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2016-10-24 16:14:45 +03:00
|
|
|
if (!mBuffer->write(aCx, aValue, aTransfer, cloneDataPolicy,
|
|
|
|
&StructuredCloneHolder::sCallbacks, this)) {
|
2015-09-02 19:20:30 +03:00
|
|
|
mBuffer = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
bool 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
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
bool ok =
|
|
|
|
mBuffer->read(aCx, aValue, &StructuredCloneHolder::sCallbacks, this);
|
2015-07-19 10:54:41 +03:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
bool 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;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
bool 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;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
void 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.");
|
|
|
|
}
|
|
|
|
|
2018-03-28 10:23:17 +03:00
|
|
|
bool StructuredCloneHolderBase::CustomCanTransferHandler(
|
|
|
|
JSContext* aCx, JS::Handle<JSObject*> aObj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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(
|
2015-09-02 19:20:30 +03:00
|
|
|
CloningSupport aSupportsCloning, TransferringSupport aSupportsTransferring,
|
2016-07-21 16:29:42 +03:00
|
|
|
StructuredCloneScope aScope)
|
|
|
|
: StructuredCloneHolderBase(aScope),
|
|
|
|
mSupportsCloning(aSupportsCloning == CloningSupported),
|
2015-07-28 10:38:16 +03:00
|
|
|
mSupportsTransferring(aSupportsTransferring == TransferringSupported),
|
2015-07-22 21:37:18 +03:00
|
|
|
mParent(nullptr)
|
2015-09-02 19:20:30 +03:00
|
|
|
#ifdef DEBUG
|
2017-06-01 23:42:05 +03:00
|
|
|
,
|
|
|
|
mCreationEventTarget(GetCurrentThreadEventTarget())
|
2015-09-02 19:20:30 +03:00
|
|
|
#endif
|
2015-07-24 14:12:51 +03:00
|
|
|
{
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder::~StructuredCloneHolder() {
|
|
|
|
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-09-30 15:22:08 +03:00
|
|
|
void StructuredCloneHolder::Write(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
2015-07-31 03:38:00 +03:00
|
|
|
ErrorResult& aRv) {
|
2016-10-24 16:14:45 +03:00
|
|
|
Write(aCx, aValue, JS::UndefinedHandleValue,
|
|
|
|
JS::CloneDataPolicy().denySharedArrayBuffer(), aRv);
|
2015-07-28 10:38:16 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
void StructuredCloneHolder::Write(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
2015-07-31 03:38:00 +03:00
|
|
|
JS::Handle<JS::Value> aTransfer,
|
2016-10-24 16:14:45 +03:00
|
|
|
JS::CloneDataPolicy cloneDataPolicy,
|
2015-07-31 03:38:00 +03:00
|
|
|
ErrorResult& aRv) {
|
2016-07-21 16:29:42 +03:00
|
|
|
MOZ_ASSERT_IF(
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread,
|
2017-06-01 23:42:05 +03:00
|
|
|
mCreationEventTarget->IsOnCurrentThread());
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2016-10-24 16:14:45 +03:00
|
|
|
if (!StructuredCloneHolderBase::Write(aCx, aValue, aTransfer,
|
|
|
|
cloneDataPolicy)) {
|
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-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
void StructuredCloneHolder::Read(nsISupports* aParent, JSContext* aCx,
|
2015-07-31 03:38:00 +03:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
2016-07-21 16:29:42 +03:00
|
|
|
MOZ_ASSERT_IF(
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread,
|
2017-06-01 23:42:05 +03:00
|
|
|
mCreationEventTarget->IsOnCurrentThread());
|
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);
|
2017-02-28 22:50:35 +03:00
|
|
|
return;
|
2015-07-31 03:38:00 +03:00
|
|
|
}
|
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
// If we are tranferring something, we cannot call 'Read()' more than once.
|
|
|
|
if (mSupportsTransferring) {
|
|
|
|
mBlobImplArray.Clear();
|
2017-03-29 09:47:49 +03:00
|
|
|
mWasmModuleArray.Clear();
|
2015-12-18 09:52:16 +03:00
|
|
|
mClonedSurfaces.Clear();
|
2017-04-05 20:02:57 +03:00
|
|
|
mInputStreamArray.Clear();
|
2015-09-30 15:22:08 +03:00
|
|
|
Clear();
|
2015-08-26 15:17:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
void StructuredCloneHolder::ReadFromBuffer(nsISupports* aParent, JSContext* aCx,
|
2016-04-22 13:04:20 +03:00
|
|
|
JSStructuredCloneData& aBuffer,
|
2015-08-26 15:17:23 +03:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
ReadFromBuffer(aParent, aCx, aBuffer, JS_STRUCTURED_CLONE_VERSION, aValue,
|
|
|
|
aRv);
|
2015-07-28 10:38:16 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
void StructuredCloneHolder::ReadFromBuffer(nsISupports* aParent, JSContext* aCx,
|
2016-04-22 13:04:20 +03:00
|
|
|
JSStructuredCloneData& aBuffer,
|
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) {
|
2016-07-21 16:29:42 +03:00
|
|
|
MOZ_ASSERT_IF(
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread,
|
2017-06-01 23:42:05 +03:00
|
|
|
mCreationEventTarget->IsOnCurrentThread());
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2015-07-28 10:38:16 +03:00
|
|
|
MOZ_ASSERT(!mBuffer, "ReadFromBuffer() must be called without a Write().");
|
|
|
|
|
|
|
|
mozilla::AutoRestore<nsISupports*> guard(mParent);
|
|
|
|
mParent = aParent;
|
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
if (!JS_ReadStructuredClone(aCx, aBuffer, aAlgorithmVersion,
|
|
|
|
mStructuredCloneScope, aValue, &sCallbacks,
|
2016-07-21 16:29:42 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
JSObject* StructuredCloneHolder::ReadFullySerializableObjects(
|
2015-09-14 20:05:44 +03:00
|
|
|
JSContext* aCx, JSStructuredCloneReader* aReader, uint32_t aTag) {
|
2019-06-13 20:37:15 +03:00
|
|
|
AssertTagValues();
|
|
|
|
|
2019-06-25 09:44:38 +03:00
|
|
|
nsIGlobalObject* global = xpc::CurrentNativeGlobal(aCx);
|
|
|
|
if (!global) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebIDLDeserializer deserializer =
|
|
|
|
LookupDeserializer(StructuredCloneTags(aTag));
|
|
|
|
if (deserializer) {
|
|
|
|
return deserializer(aCx, global, aReader);
|
2015-09-14 20:05:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't know what this is. Bail.
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
bool StructuredCloneHolder::WriteFullySerializableObjects(
|
2015-09-14 20:05:44 +03:00
|
|
|
JSContext* aCx, JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj) {
|
2019-06-13 20:37:15 +03:00
|
|
|
AssertTagValues();
|
|
|
|
|
2019-06-25 09:44:38 +03:00
|
|
|
// Window and Location are not serializable, so it's OK to just do a static
|
|
|
|
// unwrap here.
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, js::CheckedUnwrapStatic(aObj));
|
|
|
|
if (!obj) {
|
|
|
|
return xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
}
|
2017-07-10 23:05:24 +03:00
|
|
|
|
2019-06-25 09:44:38 +03:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(obj);
|
|
|
|
if (domClass && domClass->mSerializer) {
|
|
|
|
return domClass->mSerializer(aCx, aWriter, obj);
|
2015-09-14 20:05:44 +03:00
|
|
|
}
|
|
|
|
|
2019-02-02 06:24:45 +03:00
|
|
|
if (NS_IsMainThread() && xpc::IsReflector(obj, aCx)) {
|
|
|
|
// We only care about principals, so ReflectorToISupportsStatic is fine.
|
|
|
|
nsCOMPtr<nsISupports> base = xpc::ReflectorToISupportsStatic(obj);
|
2015-09-14 20:05:44 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't know what this is
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 09:48:33 +03:00
|
|
|
/* static */
|
|
|
|
bool StructuredCloneHolder::ReadString(JSStructuredCloneReader* aReader,
|
|
|
|
nsString& aString) {
|
|
|
|
uint32_t length, zero;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &length, &zero)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!aString.SetLength(length, fallible))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t charSize = sizeof(nsString::char_type);
|
|
|
|
return JS_ReadBytes(aReader, (void*)aString.BeginWriting(),
|
|
|
|
length * charSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
bool StructuredCloneHolder::WriteString(JSStructuredCloneWriter* aWriter,
|
|
|
|
const nsString& aString) {
|
|
|
|
size_t charSize = sizeof(nsString::char_type);
|
|
|
|
return JS_WriteUint32Pair(aWriter, aString.Length(), 0) &&
|
|
|
|
JS_WriteBytes(aWriter, aString.get(), aString.Length() * charSize);
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
JSObject* ReadBlob(JSContext* aCx, uint32_t aIndex,
|
2015-09-30 15:22:08 +03:00
|
|
|
StructuredCloneHolder* aHolder) {
|
|
|
|
MOZ_ASSERT(aHolder);
|
2018-02-23 16:51:26 +03:00
|
|
|
#ifdef FUZZING
|
|
|
|
if (aIndex >= aHolder->BlobImpls().Length()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
2015-09-30 15:22:08 +03:00
|
|
|
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
|
|
|
|
2017-05-23 08:50:09 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false));
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2016-04-12 15:51:04 +03:00
|
|
|
// RefPtr<File> needs to go out of scope before toObject() 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
|
|
|
|
2016-08-17 08:54:00 +03:00
|
|
|
if (JS_GetStructuredCloneScope(aWriter) !=
|
|
|
|
JS::StructuredCloneScope::SameProcessSameThread &&
|
|
|
|
!aBlob->Impl()->MayBeClonedToOtherThreads()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-23 08:50:09 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = aBlob->Impl();
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false));
|
2015-11-28 13:48:28 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:51:04 +03:00
|
|
|
// A directory is serialized as:
|
2016-04-18 10:32:30 +03:00
|
|
|
// - pair of ints: SCTAG_DOM_DIRECTORY, path length
|
2016-04-12 15:51:04 +03: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 10:32:30 +03:00
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_DIRECTORY, path.Length()) &&
|
2016-04-12 15:51:04 +03:00
|
|
|
JS_WriteBytes(aWriter, path.get(), path.Length() * charSize);
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
already_AddRefed<Directory> ReadDirectoryInternal(
|
|
|
|
JSStructuredCloneReader* aReader, uint32_t aPathLength,
|
|
|
|
StructuredCloneHolder* aHolder) {
|
2016-04-12 15:51:04 +03:00
|
|
|
MOZ_ASSERT(aReader);
|
|
|
|
MOZ_ASSERT(aHolder);
|
|
|
|
|
|
|
|
nsAutoString path;
|
2018-02-23 16:57:00 +03:00
|
|
|
if (NS_WARN_IF(!path.SetLength(aPathLength, fallible))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-12 15:51:04 +03:00
|
|
|
size_t charSize = sizeof(nsString::char_type);
|
|
|
|
if (!JS_ReadBytes(aReader, (void*)path.BeginWriting(),
|
2016-04-18 10:32:30 +03:00
|
|
|
aPathLength * charSize)) {
|
2016-04-12 15:51:04 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
2017-01-18 16:58:37 +03:00
|
|
|
nsresult rv = NS_NewLocalFile(path, true, getter_AddRefs(file));
|
2016-04-12 15:51:04 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:31 +03: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 15:51:04 +03: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 10:01:31 +03:00
|
|
|
ReadDirectoryInternal(aReader, aPathLength, aHolder);
|
|
|
|
if (!directory) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-12 15:51:04 +03:00
|
|
|
|
|
|
|
if (!ToJSValue(aCx, directory, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &val.toObject();
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
// Read the WriteFileList for the format.
|
|
|
|
JSObject* ReadFileList(JSContext* aCx, JSStructuredCloneReader* aReader,
|
2015-09-30 15:22:08 +03:00
|
|
|
uint32_t aCount, 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-04-12 15:51:52 +03:00
|
|
|
uint32_t zero, index;
|
|
|
|
// |index| is the index of the first blobImpl.
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &zero, &index)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
2016-04-12 15:51:52 +03:00
|
|
|
MOZ_ASSERT(zero == 0);
|
2015-08-29 02:19:56 +03:00
|
|
|
|
2016-04-12 15:51:52 +03: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;
|
2018-02-23 16:51:26 +03:00
|
|
|
#ifdef FUZZING
|
|
|
|
if (pos >= aHolder->BlobImpls().Length()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-12 15:51:52 +03:00
|
|
|
MOZ_ASSERT(pos < aHolder->BlobImpls().Length());
|
2016-04-12 18:56:00 +03:00
|
|
|
|
2016-04-12 15:51:52 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = aHolder->BlobImpls()[pos];
|
|
|
|
MOZ_ASSERT(blobImpl->IsFile());
|
2016-03-20 05:44:22 +03:00
|
|
|
|
2017-05-23 08:50:09 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false));
|
2016-04-12 15:51:52 +03:00
|
|
|
|
|
|
|
RefPtr<File> file = File::Create(aHolder->ParentDuringRead(), blobImpl);
|
|
|
|
if (!fileList->Append(file)) {
|
2015-07-28 10:38:16 +03:00
|
|
|
return nullptr;
|
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-04-12 15:51:52 +03:00
|
|
|
// - pair of ints: 0, The offset of the BlobImpl array
|
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
|
|
|
|
2016-04-12 15:51:52 +03: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 19:19:13 +03:00
|
|
|
if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_FILELIST, aFileList->Length()) ||
|
2016-04-12 15:51:52 +03:00
|
|
|
!JS_WriteUint32Pair(aWriter, 0, aHolder->BlobImpls().Length())) {
|
2015-08-27 19:19:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-05 00:15:46 +03:00
|
|
|
nsTArray<RefPtr<BlobImpl>> blobImpls;
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
for (uint32_t i = 0; i < aFileList->Length(); ++i) {
|
2017-05-23 08:50:09 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = aFileList->Item(i)->Impl();
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false));
|
2016-04-12 15:51:52 +03:00
|
|
|
blobImpls.AppendElement(blobImpl);
|
2015-08-27 19:19:13 +03:00
|
|
|
}
|
|
|
|
|
2016-04-12 15:51:52 +03:00
|
|
|
aHolder->BlobImpls().AppendElements(blobImpls);
|
2015-08-27 19:19:13 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the WriteFormData for the format.
|
|
|
|
JSObject* ReadFormData(JSContext* aCx, JSStructuredCloneReader* aReader,
|
2015-09-30 15:22:08 +03:00
|
|
|
uint32_t aCount, 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;
|
2019-06-25 09:48:33 +03:00
|
|
|
if (!StructuredCloneHolder::ReadString(aReader, name)) {
|
2015-08-27 19:19:13 +03:00
|
|
|
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) {
|
2018-02-23 16:51:26 +03:00
|
|
|
#ifdef FUZZING
|
|
|
|
if (indexOrLengthOfString >= aHolder->BlobImpls().Length()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
2015-09-30 15:22:08 +03:00
|
|
|
MOZ_ASSERT(indexOrLengthOfString < aHolder->BlobImpls().Length());
|
2015-07-28 10:38:16 +03:00
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = aHolder->BlobImpls()[indexOrLengthOfString];
|
2017-05-23 08:50:09 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false));
|
2016-11-16 17:04:23 +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
|
|
|
|
2017-05-23 08:50:09 +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())) {
|
2016-06-14 15:10:39 +03:00
|
|
|
rv.SuppressException();
|
2015-12-05 00:15:46 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:31 +03: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 19:19:13 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(tag == 0);
|
|
|
|
|
|
|
|
nsAutoString value;
|
2018-02-23 16:57:00 +03:00
|
|
|
if (NS_WARN_IF(!value.SetLength(indexOrLengthOfString, fallible))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-27 19:19:13 +03:00
|
|
|
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())) {
|
2016-06-14 15:10:39 +03:00
|
|
|
rv.SuppressException();
|
2015-12-05 00:15:46 +03:00
|
|
|
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.
|
2016-07-14 10:01:31 +03:00
|
|
|
// - if it's a directory (See WriteDirectory):
|
|
|
|
// - pair of ints: SCTAG_DOM_DIRECTORY, path length
|
|
|
|
// - path as string
|
2015-08-27 19:19:13 +03:00
|
|
|
// - else:
|
|
|
|
// - pair of ints: 0, string length
|
|
|
|
// - value string
|
|
|
|
bool WriteFormData(JSStructuredCloneWriter* aWriter, 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, StructuredCloneHolder* aHolder)
|
|
|
|
: mWriter(aWriter), mHolder(aHolder) {}
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
static bool Write(const nsString& aName,
|
|
|
|
const OwningBlobOrDirectoryOrUSVString& 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);
|
2019-06-25 09:48:33 +03:00
|
|
|
if (!StructuredCloneHolder::WriteString(closure->mWriter, aName)) {
|
2015-08-27 19:19:13 +03:00
|
|
|
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()) {
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-23 08:50:09 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = aValue.GetAsBlob()->Impl();
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(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
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
if (aValue.IsDirectory()) {
|
|
|
|
Directory* directory = aValue.GetAsDirectory();
|
|
|
|
return WriteDirectory(closure->mWriter, directory);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-10-28 15:44:04 +03:00
|
|
|
JSObject* ReadWasmModule(JSContext* aCx, uint32_t aIndex,
|
|
|
|
StructuredCloneHolder* aHolder) {
|
|
|
|
MOZ_ASSERT(aHolder);
|
|
|
|
MOZ_ASSERT(
|
|
|
|
aHolder->CloneScope() ==
|
|
|
|
StructuredCloneHolder::StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
aHolder->CloneScope() == StructuredCloneHolder::StructuredCloneScope::
|
|
|
|
SameProcessDifferentThread);
|
2018-02-23 16:51:26 +03:00
|
|
|
#ifdef FUZZING
|
|
|
|
if (aIndex >= aHolder->WasmModules().Length()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
MOZ_ASSERT(aIndex < aHolder->WasmModules().Length());
|
2016-10-28 15:44:04 +03:00
|
|
|
|
2017-08-29 18:38:49 +03:00
|
|
|
return aHolder->WasmModules()[aIndex]->createObject(aCx);
|
2016-10-28 15:44:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteWasmModule(JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::WasmModule* aWasmModule,
|
|
|
|
StructuredCloneHolder* aHolder) {
|
|
|
|
MOZ_ASSERT(aWriter);
|
|
|
|
MOZ_ASSERT(aWasmModule);
|
|
|
|
MOZ_ASSERT(aHolder);
|
|
|
|
MOZ_ASSERT(
|
|
|
|
aHolder->CloneScope() ==
|
|
|
|
StructuredCloneHolder::StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
aHolder->CloneScope() == StructuredCloneHolder::StructuredCloneScope::
|
|
|
|
SameProcessDifferentThread);
|
|
|
|
|
|
|
|
// We store the position of the wasmModule in the array as index.
|
|
|
|
if (JS_WriteUint32Pair(aWriter, SCTAG_DOM_WASM,
|
|
|
|
aHolder->WasmModules().Length())) {
|
|
|
|
aHolder->WasmModules().AppendElement(aWasmModule);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:02:57 +03:00
|
|
|
JSObject* ReadInputStream(JSContext* aCx, uint32_t aIndex,
|
|
|
|
StructuredCloneHolder* aHolder) {
|
|
|
|
MOZ_ASSERT(aHolder);
|
2018-02-23 16:51:26 +03:00
|
|
|
#ifdef FUZZING
|
|
|
|
if (aIndex >= aHolder->InputStreams().Length()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
2017-04-05 20:02:57 +03:00
|
|
|
MOZ_ASSERT(aIndex < aHolder->InputStreams().Length());
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream = aHolder->InputStreams()[aIndex];
|
|
|
|
|
|
|
|
JS::RootedValue result(aCx);
|
|
|
|
nsresult rv = nsContentUtils::WrapNative(
|
|
|
|
aCx, inputStream, &NS_GET_IID(nsIInputStream), &result);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result.toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteInputStream(JSStructuredCloneWriter* aWriter,
|
|
|
|
nsIInputStream* aInputStream,
|
|
|
|
StructuredCloneHolder* aHolder) {
|
|
|
|
MOZ_ASSERT(aWriter);
|
|
|
|
MOZ_ASSERT(aInputStream);
|
|
|
|
MOZ_ASSERT(aHolder);
|
|
|
|
|
|
|
|
// We store the position of the inputStream in the array as index.
|
|
|
|
if (JS_WriteUint32Pair(aWriter, SCTAG_DOM_INPUTSTREAM,
|
|
|
|
aHolder->InputStreams().Length())) {
|
|
|
|
aHolder->InputStreams().AppendElement(aInputStream);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
JSObject* 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);
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:51:04 +03:00
|
|
|
if (aTag == SCTAG_DOM_DIRECTORY) {
|
|
|
|
return ReadDirectory(aCx, aReader, aIndex, this);
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03: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 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 10:02:16 +03:00
|
|
|
if (aTag == SCTAG_DOM_IMAGEBITMAP &&
|
|
|
|
(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope ==
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread)) {
|
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, parent, GetSurfaces(),
|
2015-12-18 09:52:16 +03:00
|
|
|
aIndex);
|
2016-10-28 15:44:04 +03:00
|
|
|
}
|
|
|
|
|
2017-06-13 00:42:49 +03:00
|
|
|
if (aTag == SCTAG_DOM_STRUCTURED_CLONE_HOLDER) {
|
|
|
|
return StructuredCloneBlob::ReadStructuredClone(aCx, aReader, this);
|
|
|
|
}
|
|
|
|
|
2018-05-10 10:02:16 +03:00
|
|
|
if (aTag == SCTAG_DOM_WASM &&
|
|
|
|
(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope ==
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread)) {
|
2016-10-28 15:44:04 +03:00
|
|
|
return ReadWasmModule(aCx, aIndex, this);
|
|
|
|
}
|
2015-07-30 21:50:00 +03:00
|
|
|
|
2017-04-05 20:02:57 +03:00
|
|
|
if (aTag == SCTAG_DOM_INPUTSTREAM) {
|
|
|
|
return ReadInputStream(aCx, aIndex, this);
|
|
|
|
}
|
|
|
|
|
2019-07-02 01:24:09 +03:00
|
|
|
if (aTag == SCTAG_DOM_BROWSING_CONTEXT) {
|
|
|
|
return BrowsingContext::ReadStructuredClone(aCx, aReader, this);
|
|
|
|
}
|
|
|
|
|
2015-10-07 22:32:14 +03:00
|
|
|
return ReadFullySerializableObjects(aCx, aReader, aTag);
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
bool StructuredCloneHolder::CustomWriteHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj) {
|
2015-07-28 10:38:16 +03:00
|
|
|
if (!mSupportsCloning) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-10 23:05:24 +03:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, aObj);
|
|
|
|
|
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;
|
2017-07-10 23:05:24 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(Blob, &obj, blob))) {
|
2015-08-27 19:19:13 +03:00
|
|
|
return WriteBlob(aWriter, blob, this);
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:51:04 +03:00
|
|
|
// See if this is a Directory object.
|
|
|
|
{
|
|
|
|
Directory* directory = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(Directory, &obj, directory))) {
|
2016-04-12 15:51:04 +03:00
|
|
|
return WriteDirectory(aWriter, directory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2017-07-10 23:05:24 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(FileList, &obj, fileList))) {
|
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;
|
2017-07-10 23:05:24 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(FormData, &obj, formData))) {
|
2015-08-27 19:19:13 +03:00
|
|
|
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.
|
2016-07-21 16:29:42 +03:00
|
|
|
if (mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope ==
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread) {
|
2015-07-30 21:50:00 +03:00
|
|
|
ImageBitmap* imageBitmap = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(ImageBitmap, &obj, imageBitmap))) {
|
2015-07-30 21:50:00 +03:00
|
|
|
return ImageBitmap::WriteStructuredClone(aWriter, GetSurfaces(),
|
|
|
|
imageBitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:42:49 +03:00
|
|
|
// See if this is a StructuredCloneBlob object.
|
|
|
|
{
|
|
|
|
StructuredCloneBlob* holder = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(StructuredCloneHolder, &obj, holder))) {
|
2017-06-13 00:42:49 +03:00
|
|
|
return holder->WriteStructuredClone(aCx, aWriter, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 01:24:09 +03:00
|
|
|
// See if this is a BrowsingContext object.
|
|
|
|
if (mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope == StructuredCloneScope::DifferentProcess) {
|
|
|
|
BrowsingContext* holder = nullptr;
|
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(BrowsingContext, &obj, holder))) {
|
|
|
|
return holder->WriteStructuredClone(aCx, aWriter, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 15:44:04 +03:00
|
|
|
// See if this is a WasmModule.
|
|
|
|
if ((mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope ==
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread) &&
|
2017-07-10 23:05:24 +03:00
|
|
|
JS::IsWasmModuleObject(obj)) {
|
|
|
|
RefPtr<JS::WasmModule> module = JS::GetWasmModule(obj);
|
2016-10-28 15:44:04 +03:00
|
|
|
MOZ_ASSERT(module);
|
|
|
|
|
|
|
|
return WriteWasmModule(aWriter, module, this);
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:02:57 +03:00
|
|
|
{
|
2019-02-02 06:24:45 +03:00
|
|
|
// We only care about streams, so ReflectorToISupportsStatic is fine.
|
|
|
|
nsCOMPtr<nsISupports> base = xpc::ReflectorToISupportsStatic(aObj);
|
2017-04-05 20:02:57 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = do_QueryInterface(base);
|
|
|
|
if (inputStream) {
|
|
|
|
return WriteInputStream(aWriter, inputStream, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 20:05:44 +03:00
|
|
|
return WriteFullySerializableObjects(aCx, aWriter, aObj);
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
bool StructuredCloneHolder::CustomReadTransferHandler(
|
|
|
|
JSContext* aCx, JSStructuredCloneReader* aReader, uint32_t aTag,
|
|
|
|
void* aContent, uint64_t aExtraData,
|
|
|
|
JS::MutableHandleObject aReturnObject) {
|
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) {
|
2018-02-23 16:51:26 +03:00
|
|
|
#ifdef FUZZING
|
|
|
|
if (aExtraData >= mPortIdentifiers.Length()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
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;
|
2016-03-01 17:21:11 +03:00
|
|
|
RefPtr<MessagePort> port = MessagePort::Create(global, portIdentifier, rv);
|
2015-07-22 21:37:18 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2016-06-14 15:10:39 +03:00
|
|
|
rv.SuppressException();
|
2015-07-22 21:37:18 +03: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;
|
|
|
|
}
|
|
|
|
|
2018-05-10 10:02:16 +03:00
|
|
|
if (aTag == SCTAG_DOM_CANVAS &&
|
|
|
|
(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope ==
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread)) {
|
2015-10-12 06:21:03 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-10 10:02:16 +03:00
|
|
|
if (aTag == SCTAG_DOM_IMAGEBITMAP &&
|
|
|
|
(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope ==
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread)) {
|
2015-12-18 09:52:16 +03: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 21:37:18 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
bool StructuredCloneHolder::CustomWriteTransferHandler(
|
|
|
|
JSContext* aCx, JS::Handle<JSObject*> aObj, uint32_t* aTag,
|
|
|
|
JS::TransferableOwnership* aOwnership, void** aContent,
|
|
|
|
uint64_t* aExtraData) {
|
2015-07-28 10:38:16 +03:00
|
|
|
if (!mSupportsTransferring) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-10 23:05:24 +03:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, aObj);
|
|
|
|
|
2015-07-28 10:38:16 +03:00
|
|
|
{
|
2015-09-16 06:27:56 +03:00
|
|
|
MessagePort* port = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
nsresult rv = UNWRAP_OBJECT(MessagePort, &obj, port);
|
2015-07-22 21:37:18 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// We use aExtraData to store the index of this new port identifier.
|
|
|
|
*aExtraData = mPortIdentifiers.Length();
|
|
|
|
MessagePortIdentifier* identifier = mPortIdentifiers.AppendElement();
|
|
|
|
|
2018-03-28 10:23:17 +03:00
|
|
|
if (!port->CanBeCloned()) {
|
2018-03-28 10:23:17 +03:00
|
|
|
return false;
|
|
|
|
}
|
2015-07-22 21:37:18 +03:00
|
|
|
|
2018-03-28 10:23:17 +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
|
|
|
|
2016-07-21 16:29:42 +03:00
|
|
|
if (mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope ==
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread) {
|
2015-10-12 06:21:03 +03:00
|
|
|
OffscreenCanvas* canvas = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
rv = UNWRAP_OBJECT(OffscreenCanvas, &obj, canvas);
|
2015-10-12 06:21:03 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
MOZ_ASSERT(canvas);
|
|
|
|
|
2018-03-28 10:23:17 +03:00
|
|
|
if (canvas->IsNeutered()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
*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;
|
2017-07-10 23:05:24 +03:00
|
|
|
rv = UNWRAP_OBJECT(ImageBitmap, &obj, bitmap);
|
2015-12-18 09:52:16 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
MOZ_ASSERT(bitmap);
|
|
|
|
|
|
|
|
*aExtraData = 0;
|
|
|
|
*aTag = SCTAG_DOM_IMAGEBITMAP;
|
|
|
|
*aOwnership = JS::SCTAG_TMO_CUSTOM;
|
2018-03-14 19:37:34 +03:00
|
|
|
|
|
|
|
UniquePtr<ImageBitmapCloneData> clonedBitmap = bitmap->ToCloneData();
|
|
|
|
if (!clonedBitmap) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aContent = clonedBitmap.release();
|
2015-12-18 09:52:16 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
void StructuredCloneHolder::CustomFreeTransferHandler(
|
|
|
|
uint32_t aTag, JS::TransferableOwnership aOwnership, void* aContent,
|
|
|
|
uint64_t aExtraData) {
|
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);
|
2018-02-23 16:51:26 +03:00
|
|
|
#ifdef FUZZING
|
|
|
|
if (aExtraData >= mPortIdentifiers.Length()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-22 21:37:18 +03:00
|
|
|
MOZ_ASSERT(aExtraData < mPortIdentifiers.Length());
|
|
|
|
MessagePort::ForceClose(mPortIdentifiers[aExtraData]);
|
2015-10-12 06:21:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-10 10:02:16 +03:00
|
|
|
if (aTag == SCTAG_DOM_CANVAS &&
|
|
|
|
(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope ==
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread)) {
|
2015-10-12 06:21:03 +03:00
|
|
|
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
|
|
|
|
2018-05-10 10:02:16 +03:00
|
|
|
if (aTag == SCTAG_DOM_IMAGEBITMAP &&
|
|
|
|
(mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope ==
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread)) {
|
2015-12-18 09:52:16 +03:00
|
|
|
MOZ_ASSERT(aContent);
|
|
|
|
ImageBitmapCloneData* data = static_cast<ImageBitmapCloneData*>(aContent);
|
|
|
|
delete data;
|
|
|
|
return;
|
|
|
|
}
|
2015-07-22 21:37:18 +03:00
|
|
|
}
|
|
|
|
|
2018-03-28 10:23:17 +03:00
|
|
|
bool StructuredCloneHolder::CustomCanTransferHandler(
|
|
|
|
JSContext* aCx, JS::Handle<JSObject*> aObj) {
|
|
|
|
if (!mSupportsTransferring) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, aObj);
|
|
|
|
|
|
|
|
{
|
|
|
|
MessagePort* port = nullptr;
|
|
|
|
nsresult rv = UNWRAP_OBJECT(MessagePort, &obj, port);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
|
|
|
|
mStructuredCloneScope ==
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread) {
|
|
|
|
OffscreenCanvas* canvas = nullptr;
|
|
|
|
rv = UNWRAP_OBJECT(OffscreenCanvas, &obj, canvas);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageBitmap* bitmap = nullptr;
|
|
|
|
rv = UNWRAP_OBJECT(ImageBitmap, &obj, bitmap);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-26 23:00:17 +03:00
|
|
|
bool StructuredCloneHolder::TakeTransferredPortsAsSequence(
|
|
|
|
Sequence<OwningNonNull<mozilla::dom::MessagePort>>& aPorts) {
|
|
|
|
nsTArray<RefPtr<MessagePort>> ports = TakeTransferredPorts();
|
|
|
|
|
|
|
|
aPorts.Clear();
|
|
|
|
for (uint32_t i = 0, len = ports.Length(); i < len; ++i) {
|
|
|
|
if (!aPorts.AppendElement(ports[i].forget(), fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-19 10:54:41 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|