2012-08-02 10:02:29 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set sw=4 ts=8 et 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"
|
|
|
|
|
2014-10-01 22:43:26 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2014-10-08 20:15:22 +04:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2012-08-02 10:02:29 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
2013-09-24 01:30:40 +04:00
|
|
|
#include "MainThreadUtils.h"
|
2012-08-02 10:02:29 +04:00
|
|
|
#include "StructuredCloneTags.h"
|
2013-08-20 10:43:47 +04:00
|
|
|
#include "jsapi.h"
|
2012-08-02 10:02:29 +04:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void
|
|
|
|
Error(JSContext* aCx, uint32_t aErrorId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_DOMStructuredCloneError(aCx, aErrorId);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
Read(JSContext* aCx, JSStructuredCloneReader* aReader, uint32_t aTag,
|
|
|
|
uint32_t aData, void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
StructuredCloneClosure* closure =
|
|
|
|
static_cast<StructuredCloneClosure*>(aClosure);
|
|
|
|
|
2014-10-07 21:16:11 +04:00
|
|
|
if (aTag == SCTAG_DOM_BLOB) {
|
2014-10-08 20:15:23 +04:00
|
|
|
// nsRefPtr<File> needs to go out of scope before toObjectOrNull() is
|
2014-10-08 20:15:22 +04: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);
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData < closure->mBlobs.Length());
|
2014-10-08 20:15:23 +04:00
|
|
|
nsRefPtr<File> blob = closure->mBlobs[aData];
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-10-08 20:15:22 +04:00
|
|
|
{
|
|
|
|
// File should not be mutable.
|
|
|
|
bool isMutable;
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(blob->GetMutable(&isMutable)));
|
|
|
|
MOZ_ASSERT(!isMutable);
|
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
#endif
|
2012-08-02 10:02:29 +04:00
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
// Let's create a new blob with the correct parent.
|
|
|
|
nsIGlobalObject *global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
nsRefPtr<File> newBlob = new File(global, blob->Impl());
|
2014-10-08 20:15:22 +04:00
|
|
|
if (!WrapNewBindingObject(aCx, newBlob, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
return &val.toObject();
|
2012-08-02 10:02:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_DOMReadStructuredClone(aCx, aReader, aTag, aData, nullptr);
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2013-05-09 11:27:40 +04:00
|
|
|
Write(JSContext* aCx, JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj, void* aClosure)
|
2012-08-02 10:02:29 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
StructuredCloneClosure* closure =
|
|
|
|
static_cast<StructuredCloneClosure*>(aClosure);
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
// See if the wrapped native is a File/Blob.
|
2014-10-08 20:15:22 +04:00
|
|
|
{
|
2014-10-08 20:15:23 +04:00
|
|
|
File* blob = nullptr;
|
2014-10-08 20:15:22 +04:00
|
|
|
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;
|
2012-08-02 10:02:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_DOMWriteStructuredClone(aCx, aWriter, aObj, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks gCallbacks = {
|
|
|
|
Read,
|
|
|
|
Write,
|
2013-11-15 08:42:34 +04:00
|
|
|
Error,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr
|
2012-08-02 10:02:29 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
bool
|
2012-10-04 02:19:22 +04:00
|
|
|
ReadStructuredClone(JSContext* aCx, uint64_t* aData, size_t aDataLength,
|
2013-10-23 04:18:32 +04:00
|
|
|
const StructuredCloneClosure& aClosure,
|
|
|
|
JS::MutableHandle<JS::Value> aClone)
|
2012-08-02 10:02:29 +04:00
|
|
|
{
|
|
|
|
void* closure = &const_cast<StructuredCloneClosure&>(aClosure);
|
|
|
|
return !!JS_ReadStructuredClone(aCx, aData, aDataLength,
|
|
|
|
JS_STRUCTURED_CLONE_VERSION, aClone,
|
|
|
|
&gCallbacks, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-23 04:18:32 +04:00
|
|
|
WriteStructuredClone(JSContext* aCx, JS::Handle<JS::Value> aSource,
|
2012-08-02 10:02:29 +04:00
|
|
|
JSAutoStructuredCloneBuffer& aBuffer,
|
|
|
|
StructuredCloneClosure& aClosure)
|
|
|
|
{
|
|
|
|
return aBuffer.write(aCx, aSource, &gCallbacks, &aClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|