Files
palemoon27/dom/ipc/StructuredCloneUtils.cpp
T
roytam1 a47e7b316d import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1120957 - fix the source of searches made with paste & search, r=gijs,bwinton (da403a065)
- Goanna -> Gecko (e8a5d073c)
- Bug 1126057 - Provide better error when ./mach robocop is run without MOZ_HOST_BIN. r=nalexander (730edb656)
- Bug 1147307 - Use the target_out value for b2g mach mochitest. r=ahal (98dd31571)
- goanna -> gecko (4061244fd)
- Bug 1136700 - enable tweaking of the maximum number of timeouts for mochitests; r=jmaher (da0c9df86)
- goanna -> gecko (c7e86c998)
- Bug 1146234 - Allow non-object values to be used as the this value when invoking IDL callback functions. r=peterv (b00049033)
- Bug 1146333. Get rid of WrapCallThisValue and just use ToJSValue, now that we have it. r=peterv (095eb6d9d)
- whitespace fix (caf4808f0)
- Bug 1159401 - Split Blob and File classes, r=bz (700dbfd9b)
2020-07-11 00:22:20 +08:00

140 lines
3.8 KiB
C++

/* -*- 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 "StructuredCloneUtils.h"
#include "nsIDOMDOMException.h"
#include "nsIMutable.h"
#include "nsIXPConnect.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/BlobBinding.h"
#include "mozilla/dom/File.h"
#include "nsContentUtils.h"
#include "nsJSEnvironment.h"
#include "MainThreadUtils.h"
#include "StructuredCloneTags.h"
#include "jsapi.h"
using namespace mozilla::dom;
namespace {
void
Error(JSContext* aCx, uint32_t aErrorId)
{
if (NS_IsMainThread()) {
NS_DOMStructuredCloneError(aCx, aErrorId);
} else {
Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
}
}
JSObject*
Read(JSContext* aCx, JSStructuredCloneReader* aReader, uint32_t aTag,
uint32_t aData, void* aClosure)
{
MOZ_ASSERT(aClosure);
StructuredCloneClosure* closure =
static_cast<StructuredCloneClosure*>(aClosure);
if (aTag == SCTAG_DOM_BLOB) {
// nsRefPtr<File> needs to go out of scope before toObjectOrNull() 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);
{
MOZ_ASSERT(aData < closure->mBlobs.Length());
nsRefPtr<Blob> blob = closure->mBlobs[aData];
#ifdef DEBUG
{
// Blob should not be mutable.
bool isMutable;
MOZ_ASSERT(NS_SUCCEEDED(blob->GetMutable(&isMutable)));
MOZ_ASSERT(!isMutable);
}
#endif
// Let's create a new blob with the correct parent.
nsIGlobalObject *global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
MOZ_ASSERT(global);
nsRefPtr<Blob> newBlob = Blob::Create(global, blob->Impl());
if (!ToJSValue(aCx, newBlob, &val)) {
return nullptr;
}
}
return &val.toObject();
}
return NS_DOMReadStructuredClone(aCx, aReader, aTag, aData, nullptr);
}
bool
Write(JSContext* aCx, JSStructuredCloneWriter* aWriter,
JS::Handle<JSObject*> aObj, void* aClosure)
{
MOZ_ASSERT(aClosure);
StructuredCloneClosure* closure =
static_cast<StructuredCloneClosure*>(aClosure);
// See if the wrapped native is a File/Blob.
{
Blob* blob = nullptr;
if (NS_SUCCEEDED(UNWRAP_OBJECT(Blob, aObj, blob)) &&
NS_SUCCEEDED(blob->SetMutable(false)) &&
JS_WriteUint32Pair(aWriter, SCTAG_DOM_BLOB,
closure->mBlobs.Length())) {
closure->mBlobs.AppendElement(blob);
return true;
}
}
return NS_DOMWriteStructuredClone(aCx, aWriter, aObj, nullptr);
}
const JSStructuredCloneCallbacks gCallbacks = {
Read,
Write,
Error,
nullptr,
nullptr,
nullptr
};
} // namespace
namespace mozilla {
namespace dom {
bool
ReadStructuredClone(JSContext* aCx, uint64_t* aData, size_t aDataLength,
const StructuredCloneClosure& aClosure,
JS::MutableHandle<JS::Value> aClone)
{
void* closure = &const_cast<StructuredCloneClosure&>(aClosure);
return !!JS_ReadStructuredClone(aCx, aData, aDataLength,
JS_STRUCTURED_CLONE_VERSION, aClone,
&gCallbacks, closure);
}
bool
WriteStructuredClone(JSContext* aCx, JS::Handle<JS::Value> aSource,
JSAutoStructuredCloneBuffer& aBuffer,
StructuredCloneClosure& aClosure)
{
return aBuffer.write(aCx, aSource, &gCallbacks, &aClosure);
}
} // namespace dom
} // namespace mozilla