2015-09-02 19:20:30 +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-10 23:50:58 +03:00
|
|
|
#include "StructuredCloneData.h"
|
2015-09-02 19:20:30 +03:00
|
|
|
|
|
|
|
#include "nsIDOMDOMException.h"
|
|
|
|
#include "nsIMutable.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
|
2015-10-23 00:10:14 +03:00
|
|
|
#include "ipc/IPCMessageUtils.h"
|
2015-09-02 19:20:30 +03:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
|
|
|
#include "mozilla/dom/File.h"
|
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "StructuredCloneTags.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-09-10 23:50:58 +03:00
|
|
|
namespace ipc {
|
2015-09-02 19:20:30 +03:00
|
|
|
|
|
|
|
bool
|
2015-09-10 23:50:58 +03:00
|
|
|
StructuredCloneData::Copy(const StructuredCloneData& aData)
|
2015-09-02 19:20:30 +03:00
|
|
|
{
|
2016-04-22 13:04:20 +03:00
|
|
|
if (!aData.mInitialized) {
|
2015-09-02 19:20:30 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-10 23:32:20 +03:00
|
|
|
if (aData.SharedData()) {
|
|
|
|
mSharedData = aData.SharedData();
|
|
|
|
} else {
|
|
|
|
mSharedData =
|
2016-04-22 13:04:20 +03:00
|
|
|
SharedJSAllocatedData::CreateFromExternalData(aData.Data());
|
2015-10-10 23:32:20 +03:00
|
|
|
NS_ENSURE_TRUE(mSharedData, false);
|
2015-09-02 19:20:30 +03:00
|
|
|
}
|
|
|
|
|
2016-02-15 21:43:00 +03:00
|
|
|
PortIdentifiers().AppendElements(aData.PortIdentifiers());
|
|
|
|
|
2015-09-02 19:20:30 +03:00
|
|
|
MOZ_ASSERT(BlobImpls().IsEmpty());
|
2015-09-10 23:50:58 +03:00
|
|
|
BlobImpls().AppendElements(aData.BlobImpls());
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2015-12-18 09:52:16 +03:00
|
|
|
MOZ_ASSERT(GetSurfaces().IsEmpty());
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
mInitialized = true;
|
|
|
|
|
2015-09-02 19:20:30 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-10 23:50:58 +03:00
|
|
|
StructuredCloneData::Read(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
|
|
|
ErrorResult &aRv)
|
2015-09-02 19:20:30 +03:00
|
|
|
{
|
2016-04-22 13:04:20 +03:00
|
|
|
MOZ_ASSERT(mInitialized);
|
2015-09-02 19:20:30 +03:00
|
|
|
|
|
|
|
nsIGlobalObject *global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
ReadFromBuffer(global, aCx, Data(), aValue, aRv);
|
2015-09-02 19:20:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-10 23:50:58 +03:00
|
|
|
StructuredCloneData::Write(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult &aRv)
|
2016-02-01 00:48:00 +03:00
|
|
|
{
|
|
|
|
Write(aCx, aValue, JS::UndefinedHandleValue, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StructuredCloneData::Write(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
JS::Handle<JS::Value> aTransfer,
|
|
|
|
ErrorResult &aRv)
|
2015-09-02 19:20:30 +03:00
|
|
|
{
|
2016-04-22 13:04:20 +03:00
|
|
|
MOZ_ASSERT(!mInitialized);
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2016-10-24 16:14:45 +03:00
|
|
|
StructuredCloneHolder::Write(aCx, aValue, aTransfer,
|
|
|
|
JS::CloneDataPolicy().denySharedArrayBuffer(), aRv);
|
2015-09-02 19:20:30 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
JSStructuredCloneData data;
|
|
|
|
mBuffer->abandon();
|
|
|
|
mBuffer->steal(&data);
|
2015-09-02 19:20:30 +03:00
|
|
|
mBuffer = nullptr;
|
2016-04-22 13:04:20 +03:00
|
|
|
mSharedData = new SharedJSAllocatedData(Move(data));
|
|
|
|
mInitialized = true;
|
2015-09-02 19:20:30 +03:00
|
|
|
}
|
|
|
|
|
2015-09-09 10:10:32 +03:00
|
|
|
void
|
2015-10-23 00:10:14 +03:00
|
|
|
StructuredCloneData::WriteIPCParams(IPC::Message* aMsg) const
|
2015-09-09 10:10:32 +03:00
|
|
|
{
|
2016-04-22 13:04:20 +03:00
|
|
|
WriteParam(aMsg, Data());
|
2015-09-09 10:10:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-10 23:50:58 +03:00
|
|
|
StructuredCloneData::ReadIPCParams(const IPC::Message* aMsg,
|
2016-04-21 07:09:15 +03:00
|
|
|
PickleIterator* aIter)
|
2015-09-09 10:10:32 +03:00
|
|
|
{
|
2016-04-22 13:04:20 +03:00
|
|
|
MOZ_ASSERT(!mInitialized);
|
|
|
|
JSStructuredCloneData data;
|
|
|
|
if (!ReadParam(aMsg, aIter, &data)) {
|
2015-09-09 10:10:32 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-22 13:04:20 +03:00
|
|
|
mSharedData = new SharedJSAllocatedData(Move(data));
|
|
|
|
mInitialized = true;
|
2015-09-09 10:10:32 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
bool
|
2016-04-22 13:04:20 +03:00
|
|
|
StructuredCloneData::CopyExternalData(const char* aData,
|
2015-09-10 23:50:58 +03:00
|
|
|
size_t aDataLength)
|
2015-09-09 10:11:38 +03:00
|
|
|
{
|
2016-04-22 13:04:20 +03:00
|
|
|
MOZ_ASSERT(!mInitialized);
|
2015-10-10 23:32:20 +03:00
|
|
|
mSharedData = SharedJSAllocatedData::CreateFromExternalData(aData,
|
|
|
|
aDataLength);
|
|
|
|
NS_ENSURE_TRUE(mSharedData, false);
|
2016-04-22 13:04:20 +03:00
|
|
|
mInitialized = true;
|
2015-09-09 10:11:38 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-10 23:50:58 +03:00
|
|
|
} // namespace ipc
|
2015-09-02 19:20:30 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|