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 {
|
|
|
|
|
|
|
|
StructuredCloneBlob::StructuredCloneBlob()
|
|
|
|
: StructuredCloneHolder(CloningSupported, TransferringNotSupported,
|
|
|
|
StructuredCloneScope::DifferentProcess) {}
|
2017-07-26 00:53:41 +03:00
|
|
|
|
|
|
|
StructuredCloneBlob::~StructuredCloneBlob() {
|
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
}
|
2017-05-21 01:09:24 +03:00
|
|
|
|
|
|
|
/* static */ already_AddRefed<StructuredCloneBlob>
|
|
|
|
StructuredCloneBlob::Constructor(GlobalObject& aGlobal, JS::HandleValue aValue,
|
2017-06-10 04:15:50 +03:00
|
|
|
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) {
|
2017-06-10 04:15:50 +03:00
|
|
|
JS::RootedObject targetGlobal(cx, js::CheckedUnwrap(aTargetGlobal));
|
|
|
|
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()) {
|
|
|
|
JS::RootedObject obj(cx, js::CheckedUnwrap(&value.toObject()));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
holder->Write(cx, value, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return holder.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StructuredCloneBlob::Deserialize(JSContext* aCx,
|
|
|
|
JS::HandleObject aTargetScope,
|
|
|
|
JS::MutableHandleValue aResult,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
JS::RootedObject scope(aCx, js::CheckedUnwrap(aTargetScope));
|
|
|
|
if (!scope) {
|
|
|
|
js::ReportAccessDenied(aCx);
|
|
|
|
aRv.NoteJSContextException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-08-02 09:48:40 +03:00
|
|
|
JSAutoRealm ar(aCx, scope);
|
2017-05-21 01:09:24 +03:00
|
|
|
|
|
|
|
Read(xpc::NativeGlobal(scope), aCx, aResult, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_WrapValue(aCx, aResult)) {
|
|
|
|
aResult.set(JS::UndefinedValue());
|
|
|
|
aRv.NoteJSContextException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:42:49 +03:00
|
|
|
/* static */ JSObject* StructuredCloneBlob::ReadStructuredClone(
|
|
|
|
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
|
|
|
|
2017-06-13 00:42:49 +03:00
|
|
|
if (!holder->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
|
|
|
}
|
|
|
|
|
2017-06-13 00:42:49 +03:00
|
|
|
bool StructuredCloneBlob::ReadStructuredCloneInternal(
|
|
|
|
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) {
|
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) {
|
|
|
|
MOZ_COLLECT_REPORT("explicit/dom/structured-clone-holder", KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
MallocSizeOf(this) + SizeOfExcludingThis(MallocSizeOf),
|
|
|
|
"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
|