2017-05-21 01:09:24 +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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/StructuredCloneBlob.h"
|
|
|
|
|
|
|
|
#include "js/StructuredClone.h"
|
|
|
|
#include "js/Utility.h"
|
2018-02-21 19:30:19 +03:00
|
|
|
#include "js/Wrapper.h"
|
2017-10-26 12:40:12 +03:00
|
|
|
#include "mozilla/dom/BlobImpl.h"
|
|
|
|
#include "mozilla/dom/StructuredCloneTags.h"
|
2017-05-21 01:09:24 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
2017-10-26 12:40:12 +03:00
|
|
|
#include "xpcpublic.h"
|
2017-05-21 01:09:24 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2019-01-23 00:01:22 +03:00
|
|
|
StructuredCloneBlob::StructuredCloneBlob() {
|
|
|
|
mHolder.emplace(Holder::CloningSupported, Holder::TransferringNotSupported,
|
|
|
|
Holder::StructuredCloneScope::DifferentProcess);
|
|
|
|
}
|
2017-07-26 00:53:41 +03:00
|
|
|
|
|
|
|
StructuredCloneBlob::~StructuredCloneBlob() {
|
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
}
|
2017-05-21 01:09:24 +03:00
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<StructuredCloneBlob> StructuredCloneBlob::Constructor(
|
|
|
|
GlobalObject& aGlobal, JS::HandleValue aValue,
|
|
|
|
JS::HandleObject aTargetGlobal, ErrorResult& aRv) {
|
2017-05-21 01:09:24 +03:00
|
|
|
JSContext* cx = aGlobal.Context();
|
|
|
|
|
2017-07-26 00:53:41 +03:00
|
|
|
RefPtr<StructuredCloneBlob> holder = StructuredCloneBlob::Create();
|
2017-05-21 01:09:24 +03:00
|
|
|
|
2018-08-02 09:48:40 +03:00
|
|
|
Maybe<JSAutoRealm> ar;
|
2017-05-21 01:09:24 +03:00
|
|
|
JS::RootedValue value(cx, aValue);
|
|
|
|
|
|
|
|
if (aTargetGlobal) {
|
2019-02-02 06:24:22 +03:00
|
|
|
// OK to unwrap if our caller (represented by cx's Realm) can do it.
|
|
|
|
JS::RootedObject targetGlobal(cx,
|
|
|
|
js::CheckedUnwrapDynamic(aTargetGlobal, cx));
|
2017-06-10 04:15:50 +03:00
|
|
|
if (!targetGlobal) {
|
|
|
|
js::ReportAccessDenied(cx);
|
|
|
|
aRv.NoteJSContextException(cx);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
ar.emplace(cx, targetGlobal);
|
2017-05-21 01:09:24 +03:00
|
|
|
|
|
|
|
if (!JS_WrapValue(cx, &value)) {
|
|
|
|
aRv.NoteJSContextException(cx);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else if (value.isObject()) {
|
2019-02-02 06:24:22 +03:00
|
|
|
// OK to unwrap if our caller (represented by cx's Realm) can do it.
|
|
|
|
JS::RootedObject obj(cx, js::CheckedUnwrapDynamic(&value.toObject(), cx));
|
2017-05-21 01:09:24 +03:00
|
|
|
if (!obj) {
|
|
|
|
js::ReportAccessDenied(cx);
|
|
|
|
aRv.NoteJSContextException(cx);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
ar.emplace(cx, obj);
|
2017-05-21 01:09:24 +03:00
|
|
|
value = JS::ObjectValue(*obj);
|
|
|
|
}
|
|
|
|
|
2019-01-23 00:01:22 +03:00
|
|
|
holder->mHolder->Write(cx, value, aRv);
|
2017-05-21 01:09:24 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return holder.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StructuredCloneBlob::Deserialize(JSContext* aCx,
|
|
|
|
JS::HandleObject aTargetScope,
|
2019-01-23 00:01:22 +03:00
|
|
|
bool aKeepData,
|
2017-05-21 01:09:24 +03:00
|
|
|
JS::MutableHandleValue aResult,
|
|
|
|
ErrorResult& aRv) {
|
2019-02-02 06:24:22 +03:00
|
|
|
// OK to unwrap if our caller (represented by aCx's Realm) can do it.
|
|
|
|
JS::RootedObject scope(aCx, js::CheckedUnwrapDynamic(aTargetScope, aCx));
|
2017-05-21 01:09:24 +03:00
|
|
|
if (!scope) {
|
|
|
|
js::ReportAccessDenied(aCx);
|
|
|
|
aRv.NoteJSContextException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-23 00:01:22 +03:00
|
|
|
if (!mHolder.isSome()) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-21 01:09:24 +03:00
|
|
|
{
|
2018-08-02 09:48:40 +03:00
|
|
|
JSAutoRealm ar(aCx, scope);
|
2017-05-21 01:09:24 +03:00
|
|
|
|
2019-01-23 00:01:22 +03:00
|
|
|
mHolder->Read(xpc::NativeGlobal(scope), aCx, aResult, aRv);
|
2017-05-21 01:09:24 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 00:01:22 +03:00
|
|
|
if (!aKeepData) {
|
|
|
|
mHolder.reset();
|
|
|
|
}
|
|
|
|
|
2017-05-21 01:09:24 +03:00
|
|
|
if (!JS_WrapValue(aCx, aResult)) {
|
|
|
|
aResult.set(JS::UndefinedValue());
|
|
|
|
aRv.NoteJSContextException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
JSObject* StructuredCloneBlob::ReadStructuredClone(
|
2017-06-13 00:42:49 +03:00
|
|
|
JSContext* aCx, JSStructuredCloneReader* aReader,
|
|
|
|
StructuredCloneHolder* aHolder) {
|
2017-05-21 01:09:24 +03:00
|
|
|
JS::RootedObject obj(aCx);
|
2017-06-05 10:13:15 +03:00
|
|
|
{
|
2017-07-26 00:53:41 +03:00
|
|
|
RefPtr<StructuredCloneBlob> holder = StructuredCloneBlob::Create();
|
2017-06-05 10:13:15 +03:00
|
|
|
|
2019-01-23 00:01:22 +03:00
|
|
|
if (!holder->mHolder->ReadStructuredCloneInternal(aCx, aReader, aHolder) ||
|
2017-06-05 10:13:15 +03:00
|
|
|
!holder->WrapObject(aCx, nullptr, &obj)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-05-21 01:09:24 +03:00
|
|
|
}
|
2017-06-05 10:13:15 +03:00
|
|
|
return obj.get();
|
2017-05-21 01:09:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 00:01:22 +03:00
|
|
|
bool StructuredCloneBlob::Holder::ReadStructuredCloneInternal(
|
2017-06-13 00:42:49 +03:00
|
|
|
JSContext* aCx, JSStructuredCloneReader* aReader,
|
|
|
|
StructuredCloneHolder* aHolder) {
|
2017-05-21 01:09:24 +03:00
|
|
|
uint32_t length;
|
|
|
|
uint32_t version;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &length, &version)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:42:49 +03:00
|
|
|
uint32_t blobOffset;
|
|
|
|
uint32_t blobCount;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &blobOffset, &blobCount)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (blobCount) {
|
2018-02-23 16:51:26 +03:00
|
|
|
#ifdef FUZZING
|
|
|
|
if (blobOffset >= aHolder->BlobImpls().Length()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-13 00:42:49 +03:00
|
|
|
BlobImpls().AppendElements(&aHolder->BlobImpls()[blobOffset], blobCount);
|
|
|
|
}
|
|
|
|
|
2018-04-03 21:17:33 +03:00
|
|
|
JSStructuredCloneData data(mStructuredCloneScope);
|
2017-06-20 02:51:34 +03:00
|
|
|
while (length) {
|
|
|
|
size_t size;
|
|
|
|
char* buffer = data.AllocateBytes(length, &size);
|
|
|
|
if (!buffer || !JS_ReadBytes(aReader, buffer, size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
length -= size;
|
2017-05-21 01:09:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mBuffer = MakeUnique<JSAutoStructuredCloneBuffer>(
|
|
|
|
mStructuredCloneScope, &StructuredCloneHolder::sCallbacks, this);
|
2018-05-30 22:15:35 +03:00
|
|
|
mBuffer->adopt(std::move(data), version, &StructuredCloneHolder::sCallbacks);
|
2017-05-21 01:09:24 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:42:49 +03:00
|
|
|
bool StructuredCloneBlob::WriteStructuredClone(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
StructuredCloneHolder* aHolder) {
|
2019-01-23 00:01:22 +03:00
|
|
|
if (mHolder.isNothing()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return mHolder->WriteStructuredClone(aCx, aWriter, aHolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StructuredCloneBlob::Holder::WriteStructuredClone(
|
|
|
|
JSContext* aCx, JSStructuredCloneWriter* aWriter,
|
|
|
|
StructuredCloneHolder* aHolder) {
|
2017-05-21 01:09:24 +03:00
|
|
|
auto& data = mBuffer->data();
|
|
|
|
if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_STRUCTURED_CLONE_HOLDER, 0) ||
|
2017-06-13 00:42:49 +03:00
|
|
|
!JS_WriteUint32Pair(aWriter, data.Size(), JS_STRUCTURED_CLONE_VERSION) ||
|
|
|
|
!JS_WriteUint32Pair(aWriter, aHolder->BlobImpls().Length(),
|
|
|
|
BlobImpls().Length())) {
|
2017-05-21 01:09:24 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:42:49 +03:00
|
|
|
aHolder->BlobImpls().AppendElements(BlobImpls());
|
|
|
|
|
2018-03-16 02:56:09 +03:00
|
|
|
return data.ForEachDataChunk([&](const char* aData, size_t aSize) {
|
|
|
|
return JS_WriteBytes(aWriter, aData, aSize);
|
|
|
|
});
|
2017-05-21 01:09:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StructuredCloneBlob::WrapObject(JSContext* aCx,
|
|
|
|
JS::HandleObject aGivenProto,
|
|
|
|
JS::MutableHandleObject aResult) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return StructuredCloneHolder_Binding::Wrap(aCx, this, aGivenProto, aResult);
|
2017-05-21 01:09:24 +03:00
|
|
|
}
|
|
|
|
|
2017-07-26 00:53:41 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
StructuredCloneBlob::CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, bool aAnonymize) {
|
2019-01-23 00:01:22 +03:00
|
|
|
size_t size = MallocSizeOf(this);
|
|
|
|
if (mHolder.isSome()) {
|
|
|
|
size += mHolder->SizeOfExcludingThis(MallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2017-07-26 00:53:41 +03:00
|
|
|
MOZ_COLLECT_REPORT("explicit/dom/structured-clone-holder", KIND_HEAP,
|
|
|
|
UNITS_BYTES, size,
|
|
|
|
"Memory used by StructuredCloneHolder DOM objects.");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(StructuredCloneBlob, nsIMemoryReporter)
|
|
|
|
|
2017-05-21 01:09:24 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|