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
|
|
|
#ifndef mozilla_dom_ipc_StructuredCloneData_h
|
|
|
|
#define mozilla_dom_ipc_StructuredCloneData_h
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2015-10-10 23:32:20 +03:00
|
|
|
#include <algorithm>
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-09-30 15:22:08 +03:00
|
|
|
#include "mozilla/dom/StructuredCloneHolder.h"
|
2015-10-10 23:32:20 +03:00
|
|
|
#include "nsISupportsImpl.h"
|
2017-04-05 20:02:57 +03:00
|
|
|
#include "nsIInputStream.h"
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2015-09-09 10:10:32 +03:00
|
|
|
namespace IPC {
|
|
|
|
class Message;
|
2022-03-04 18:39:39 +03:00
|
|
|
class MessageReader;
|
|
|
|
class MessageWriter;
|
|
|
|
} // namespace IPC
|
2016-04-21 07:09:15 +03:00
|
|
|
class PickleIterator;
|
2015-09-09 10:10:32 +03:00
|
|
|
|
2015-09-02 19:20:30 +03:00
|
|
|
namespace mozilla {
|
2016-11-07 16:38:02 +03:00
|
|
|
namespace ipc {
|
|
|
|
|
2017-04-05 20:02:57 +03:00
|
|
|
class AutoIPCStream;
|
2016-11-07 16:38:02 +03:00
|
|
|
class PBackgroundChild;
|
|
|
|
class PBackgroundParent;
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
|
2015-09-02 19:20:30 +03:00
|
|
|
namespace dom {
|
2016-11-07 16:38:02 +03:00
|
|
|
|
2019-02-25 23:04:49 +03:00
|
|
|
class ContentChild;
|
2019-02-25 23:04:47 +03:00
|
|
|
class ContentParent;
|
2016-11-07 16:38:02 +03:00
|
|
|
|
2015-09-10 23:50:58 +03:00
|
|
|
namespace ipc {
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2016-11-07 16:38:02 +03:00
|
|
|
/**
|
|
|
|
* Wraps the non-reference-counted JSStructuredCloneData class to have a
|
|
|
|
* reference count so that multiple StructuredCloneData instances can reference
|
|
|
|
* a single underlying serialized representation.
|
|
|
|
*
|
|
|
|
* As used by StructuredCloneData, it is an invariant that our
|
|
|
|
* JSStructuredCloneData owns its buffers. (For the non-owning case,
|
|
|
|
* StructuredCloneData uses mExternalData which holds a BufferList::Borrow()ed
|
|
|
|
* read-only view of the data.)
|
|
|
|
*/
|
2015-10-10 23:32:20 +03:00
|
|
|
class SharedJSAllocatedData final {
|
|
|
|
public:
|
2016-04-22 13:04:20 +03:00
|
|
|
explicit SharedJSAllocatedData(JSStructuredCloneData&& aData)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mData(std::move(aData)) {}
|
2015-10-10 23:32:20 +03:00
|
|
|
|
|
|
|
static already_AddRefed<SharedJSAllocatedData> CreateFromExternalData(
|
2016-04-22 13:04:20 +03:00
|
|
|
const char* aData, size_t aDataLength) {
|
2018-04-03 21:17:33 +03:00
|
|
|
JSStructuredCloneData buf(JS::StructuredCloneScope::DifferentProcess);
|
2019-03-27 23:41:17 +03:00
|
|
|
NS_ENSURE_TRUE(buf.AppendBytes(aData, aDataLength), nullptr);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedJSAllocatedData> sharedData =
|
2018-05-30 22:15:35 +03:00
|
|
|
new SharedJSAllocatedData(std::move(buf));
|
2015-10-10 23:32:20 +03:00
|
|
|
return sharedData.forget();
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:32:37 +03:00
|
|
|
static already_AddRefed<SharedJSAllocatedData> CreateFromExternalData(
|
2016-04-22 13:04:20 +03:00
|
|
|
const JSStructuredCloneData& aData) {
|
2018-04-03 21:17:33 +03:00
|
|
|
JSStructuredCloneData buf(aData.scope());
|
2019-03-27 23:41:17 +03:00
|
|
|
NS_ENSURE_TRUE(buf.Append(aData), nullptr);
|
2016-05-17 00:32:37 +03:00
|
|
|
RefPtr<SharedJSAllocatedData> sharedData =
|
2018-05-30 22:15:35 +03:00
|
|
|
new SharedJSAllocatedData(std::move(buf));
|
2016-05-17 00:32:37 +03:00
|
|
|
return sharedData.forget();
|
|
|
|
}
|
|
|
|
|
2020-01-24 10:58:34 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedJSAllocatedData)
|
2015-10-10 23:32:20 +03:00
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
JSStructuredCloneData& Data() { return mData; }
|
|
|
|
size_t DataLength() const { return mData.Size(); }
|
2015-10-10 23:32:20 +03:00
|
|
|
|
|
|
|
private:
|
2020-03-06 12:11:40 +03:00
|
|
|
~SharedJSAllocatedData() = default;
|
2015-10-10 23:32:20 +03:00
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
JSStructuredCloneData mData;
|
2015-10-10 23:32:20 +03:00
|
|
|
};
|
|
|
|
|
2016-11-07 16:38:02 +03:00
|
|
|
/**
|
|
|
|
* IPC-aware StructuredCloneHolder subclass that serves as both a helper class
|
|
|
|
* for dealing with message data (blobs, transferables) and also an IPDL
|
|
|
|
* data-type in cases where message data is not needed. If your use-case does
|
|
|
|
* not (potentially) involve IPC, then you should use StructuredCloneHolder or
|
|
|
|
* one of its other subclasses instead.
|
|
|
|
*
|
|
|
|
* ## Usage ##
|
|
|
|
*
|
|
|
|
* The general recipe for using this class is:
|
|
|
|
* - In your IPDL definition, use the ClonedMessageData type whenever you want
|
|
|
|
* to send a structured clone that may include blobs or transferables such as
|
|
|
|
* message ports.
|
|
|
|
* - To send the data, instantiate a StructuredCloneData instance and Write()
|
|
|
|
* into it like a normal structure clone. When you are ready to send the
|
|
|
|
* ClonedMessageData-bearing IPC message, use the appropriate
|
|
|
|
* BuildClonedMessageDataFor{Parent,Child,BackgroundParent,BackgroundChild}
|
|
|
|
* method to populate the ClonedMessageData and then send it before your
|
|
|
|
* StructuredCloneData instance is destroyed. (Buffer borrowing is used
|
|
|
|
* under-the-hood to avoid duplicating the serialized data, requiring this.)
|
|
|
|
* - To receive the data, instantiate a StructuredCloneData and use the
|
|
|
|
* appropriate {Borrow,Copy,Steal}FromClonedMessageDataFor{Parent,Child,
|
|
|
|
* BackgroundParent,BackgroundChild} method. See the memory management
|
|
|
|
* section for more info.
|
|
|
|
*
|
|
|
|
* Variations:
|
|
|
|
* - If transferables are not allowed (ex: BroadcastChannel), then use the
|
|
|
|
* StructuredCloneDataNoTransfers subclass instead of StructuredCloneData.
|
|
|
|
*
|
|
|
|
* ## Memory Management ##
|
|
|
|
*
|
|
|
|
* Serialized structured clone representations can be quite large. So it's best
|
|
|
|
* to avoid wasteful duplication. When Write()ing into the StructuredCloneData,
|
|
|
|
* you don't need to worry about this[1], but when you already have serialized
|
|
|
|
* structured clone data you plan to Read(), you do need to. Similarly, if
|
|
|
|
* you're using StructuredCloneData as an IPDL type, it efficiently unmarshals.
|
|
|
|
*
|
|
|
|
* The from-ClonedMessageData memory management strategies available are:
|
|
|
|
* - Borrow: Create a JSStructuredCloneData that holds a non-owning, read-only
|
|
|
|
* BufferList::Borrow()ed copy of the source. Your StructuredCloneData needs
|
|
|
|
* to be destroyed before the source is. Commonly used when the
|
|
|
|
* StructuredCloneData instance is stack-allocated (and Read() is used before
|
|
|
|
* the function returns).
|
|
|
|
* - Copy: Makes a reference-counted copy of the source JSStructuredCloneData,
|
|
|
|
* making it safe for the StructuredCloneData to outlive the source data.
|
|
|
|
* - Steal: Steal the buffers from the underlying JSStructuredCloneData so that
|
|
|
|
* it's safe for the StructuredCloneData to outlive the source data. This is
|
|
|
|
* safe to use with IPC-provided ClonedMessageData instances because
|
|
|
|
* JSStructuredCloneData's IPC ParamTraits::Read method uses ExtractBuffers,
|
|
|
|
* returning a fatal false if unable to extract. (And
|
|
|
|
* SerializedStructuredCloneBuffer wraps/defers to it.) But if it's possible
|
|
|
|
* the ClonedMessageData came from a different source that might have borrowed
|
2020-11-21 02:37:01 +03:00
|
|
|
* the buffers itself, then things will crash. That would be a pretty strange
|
2016-11-07 16:38:02 +03:00
|
|
|
* implementation; if you see one, change it to use SharedJSAllocatedData.
|
|
|
|
*
|
|
|
|
* 1: Specifically, in the Write() case an owning SharedJSAllocatedData is
|
|
|
|
* created efficiently (by stealing from StructuredCloneHolder). The
|
|
|
|
* BuildClonedMessageDataFor* method can be called at any time and it will
|
|
|
|
* borrow the underlying memory. While it would be even better if
|
|
|
|
* SerializedStructuredCloneBuffer could hold a SharedJSAllocatedData ref,
|
|
|
|
* there's no reason you can't wait to BuildClonedMessageDataFor* until you
|
|
|
|
* need to make the IPC Send* call.
|
|
|
|
*/
|
2015-09-30 15:22:08 +03:00
|
|
|
class StructuredCloneData : public StructuredCloneHolder {
|
2015-09-02 19:20:30 +03:00
|
|
|
public:
|
2017-04-05 20:02:57 +03:00
|
|
|
StructuredCloneData();
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2015-09-10 23:50:58 +03:00
|
|
|
StructuredCloneData(const StructuredCloneData&) = delete;
|
2015-09-09 10:10:32 +03:00
|
|
|
|
2017-04-05 20:02:57 +03:00
|
|
|
StructuredCloneData(StructuredCloneData&& aOther);
|
2016-08-10 09:18:29 +03:00
|
|
|
|
2020-01-24 10:58:21 +03:00
|
|
|
// Only DifferentProcess and UnknownDestination scopes are supported.
|
2020-01-24 11:00:08 +03:00
|
|
|
StructuredCloneData(StructuredCloneScope aScope,
|
|
|
|
TransferringSupport aSupportsTransferring);
|
2020-01-24 10:58:21 +03:00
|
|
|
|
2017-04-05 20:02:57 +03:00
|
|
|
~StructuredCloneData();
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2015-09-10 23:50:58 +03:00
|
|
|
StructuredCloneData& operator=(const StructuredCloneData& aOther) = delete;
|
2015-09-09 10:10:32 +03:00
|
|
|
|
2017-04-05 20:02:57 +03:00
|
|
|
StructuredCloneData& operator=(StructuredCloneData&& aOther);
|
2016-08-10 09:18:29 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
const nsTArray<RefPtr<BlobImpl>>& BlobImpls() const { return mBlobImplArray; }
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<BlobImpl>>& BlobImpls() { return mBlobImplArray; }
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2017-04-05 20:02:57 +03:00
|
|
|
const nsTArray<nsCOMPtr<nsIInputStream>>& InputStreams() const {
|
|
|
|
return mInputStreamArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsIInputStream>>& InputStreams() {
|
|
|
|
return mInputStreamArray;
|
|
|
|
}
|
|
|
|
|
2015-09-10 23:50:58 +03:00
|
|
|
bool Copy(const StructuredCloneData& aData);
|
2015-09-02 19:20:30 +03:00
|
|
|
|
|
|
|
void Read(JSContext* aCx, JS::MutableHandle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2020-01-24 19:31:21 +03:00
|
|
|
void Read(JSContext* aCx, JS::MutableHandle<JS::Value> aValue,
|
2020-02-06 16:46:25 +03:00
|
|
|
const JS::CloneDataPolicy& aCloneDataPolicy, ErrorResult& aRv);
|
2020-01-24 19:31:21 +03:00
|
|
|
|
2020-02-06 16:46:25 +03:00
|
|
|
// Write with no transfer objects and with the default CloneDataPolicy. With
|
|
|
|
// a default CloneDataPolicy, read and write will not be considered as part of
|
|
|
|
// the same agent cluster and shared memory objects will not be supported.
|
|
|
|
void Write(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) override;
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2020-02-06 16:46:25 +03:00
|
|
|
// The most generic Write method, with tansfers and CloneDataPolicy.
|
2016-02-01 00:48:00 +03:00
|
|
|
void Write(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
2020-02-06 16:46:25 +03:00
|
|
|
JS::Handle<JS::Value> aTransfers,
|
|
|
|
const JS::CloneDataPolicy& aCloneDataPolicy,
|
|
|
|
ErrorResult& aRv) override;
|
2016-02-01 00:48:00 +03:00
|
|
|
|
2016-11-07 16:38:02 +03:00
|
|
|
// Actor-varying methods to convert the structured clone stored in this holder
|
|
|
|
// by a previous call to Write() into ClonedMessageData IPC representation.
|
2017-04-24 13:09:40 +03:00
|
|
|
// (Blobs are represented in IPC by IPCBlob actors, so we need the parent to
|
|
|
|
// be able to create them.)
|
2019-02-25 23:04:47 +03:00
|
|
|
bool BuildClonedMessageDataForParent(ContentParent* aParent,
|
2016-11-07 16:38:02 +03:00
|
|
|
ClonedMessageData& aClonedData);
|
2019-02-25 23:04:49 +03:00
|
|
|
bool BuildClonedMessageDataForChild(ContentChild* aChild,
|
2016-11-07 16:38:02 +03:00
|
|
|
ClonedMessageData& aClonedData);
|
|
|
|
bool BuildClonedMessageDataForBackgroundParent(
|
|
|
|
mozilla::ipc::PBackgroundParent* aParent, ClonedMessageData& aClonedData);
|
|
|
|
bool BuildClonedMessageDataForBackgroundChild(
|
|
|
|
mozilla::ipc::PBackgroundChild* aChild, ClonedMessageData& aClonedData);
|
|
|
|
|
|
|
|
// Actor-varying and memory-management-strategy-varying methods to initialize
|
|
|
|
// this holder from a ClonedMessageData representation.
|
|
|
|
void BorrowFromClonedMessageDataForParent(
|
|
|
|
const ClonedMessageData& aClonedData);
|
|
|
|
void BorrowFromClonedMessageDataForChild(
|
|
|
|
const ClonedMessageData& aClonedData);
|
|
|
|
void BorrowFromClonedMessageDataForBackgroundParent(
|
|
|
|
const ClonedMessageData& aClonedData);
|
|
|
|
void BorrowFromClonedMessageDataForBackgroundChild(
|
|
|
|
const ClonedMessageData& aClonedData);
|
|
|
|
|
|
|
|
void CopyFromClonedMessageDataForParent(const ClonedMessageData& aClonedData);
|
|
|
|
void CopyFromClonedMessageDataForChild(const ClonedMessageData& aClonedData);
|
|
|
|
void CopyFromClonedMessageDataForBackgroundParent(
|
|
|
|
const ClonedMessageData& aClonedData);
|
|
|
|
void CopyFromClonedMessageDataForBackgroundChild(
|
|
|
|
const ClonedMessageData& aClonedData);
|
|
|
|
|
|
|
|
// The steal variants of course take a non-const ClonedMessageData.
|
|
|
|
void StealFromClonedMessageDataForParent(ClonedMessageData& aClonedData);
|
|
|
|
void StealFromClonedMessageDataForChild(ClonedMessageData& aClonedData);
|
|
|
|
void StealFromClonedMessageDataForBackgroundParent(
|
|
|
|
ClonedMessageData& aClonedData);
|
|
|
|
void StealFromClonedMessageDataForBackgroundChild(
|
|
|
|
ClonedMessageData& aClonedData);
|
|
|
|
|
|
|
|
// Initialize this instance, borrowing the contents of the given
|
|
|
|
// JSStructuredCloneData. You are responsible for ensuring that this
|
|
|
|
// StructuredCloneData instance is destroyed before aData is destroyed.
|
2016-04-22 13:04:20 +03:00
|
|
|
bool UseExternalData(const JSStructuredCloneData& aData) {
|
2018-03-16 02:56:09 +03:00
|
|
|
auto iter = aData.Start();
|
2016-04-22 13:04:20 +03:00
|
|
|
bool success = false;
|
2018-04-03 21:17:33 +03:00
|
|
|
mExternalData = aData.Borrow(iter, aData.Size(), &success);
|
2016-04-22 13:04:20 +03:00
|
|
|
mInitialized = true;
|
|
|
|
return success;
|
2015-09-02 19:20:30 +03:00
|
|
|
}
|
|
|
|
|
2016-11-07 16:38:02 +03:00
|
|
|
// Initialize this instance by copying the given data that probably came from
|
|
|
|
// nsStructuredClone doing a base64 decode. Don't use this.
|
2016-04-22 13:04:20 +03:00
|
|
|
bool CopyExternalData(const char* aData, size_t aDataLength);
|
2016-11-07 16:38:02 +03:00
|
|
|
// Initialize this instance by copying the contents of an existing
|
|
|
|
// JSStructuredCloneData. Use when this StructuredCloneData instance may
|
|
|
|
// outlive aData.
|
|
|
|
bool CopyExternalData(const JSStructuredCloneData& aData);
|
|
|
|
|
|
|
|
// Initialize this instance by stealing the contents of aData via Move
|
|
|
|
// constructor, clearing the original aData as a side-effect. This is only
|
|
|
|
// safe if aData owns the underlying buffers. This is the case for instances
|
|
|
|
// provided by IPC to Recv calls.
|
|
|
|
bool StealExternalData(JSStructuredCloneData& aData);
|
2015-09-09 10:11:38 +03:00
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
JSStructuredCloneData& Data() {
|
|
|
|
return mSharedData ? mSharedData->Data() : mExternalData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JSStructuredCloneData& Data() const {
|
2015-10-10 23:32:20 +03:00
|
|
|
return mSharedData ? mSharedData->Data() : mExternalData;
|
2015-09-02 19:20:30 +03:00
|
|
|
}
|
|
|
|
|
2018-04-03 21:17:33 +03:00
|
|
|
void InitScope(JS::StructuredCloneScope aScope) { Data().initScope(aScope); }
|
|
|
|
|
2015-09-02 19:20:30 +03:00
|
|
|
size_t DataLength() const {
|
2016-04-22 13:04:20 +03:00
|
|
|
return mSharedData ? mSharedData->DataLength() : mExternalData.Size();
|
2015-10-10 23:32:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedJSAllocatedData* SharedData() const { return mSharedData; }
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2016-11-07 16:38:02 +03:00
|
|
|
bool SupportsTransferring() { return mSupportsTransferring; }
|
|
|
|
|
2017-04-05 20:02:57 +03:00
|
|
|
FallibleTArray<mozilla::ipc::AutoIPCStream>& IPCStreams() {
|
|
|
|
return mIPCStreams;
|
|
|
|
}
|
|
|
|
|
2015-09-09 10:10:32 +03:00
|
|
|
// For IPC serialization
|
|
|
|
void WriteIPCParams(IPC::Message* aMessage) const;
|
2016-04-21 07:09:15 +03:00
|
|
|
bool ReadIPCParams(const IPC::Message* aMessage, PickleIterator* aIter);
|
2015-09-09 10:10:32 +03:00
|
|
|
|
2016-11-07 16:38:02 +03:00
|
|
|
protected:
|
2018-06-25 17:03:18 +03:00
|
|
|
already_AddRefed<SharedJSAllocatedData> TakeSharedData();
|
|
|
|
|
2015-09-02 19:20:30 +03:00
|
|
|
private:
|
2016-04-22 13:04:20 +03:00
|
|
|
JSStructuredCloneData mExternalData;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedJSAllocatedData> mSharedData;
|
2017-04-05 20:02:57 +03:00
|
|
|
|
|
|
|
// This array is needed because AutoIPCStream DTOR must be executed after the
|
|
|
|
// sending of the data via IPC. This will be fixed by bug 1353475.
|
|
|
|
FallibleTArray<mozilla::ipc::AutoIPCStream> mIPCStreams;
|
2016-04-22 13:04:20 +03:00
|
|
|
bool mInitialized;
|
2015-09-02 19:20:30 +03:00
|
|
|
};
|
|
|
|
|
2015-09-10 23:50:58 +03:00
|
|
|
} // namespace ipc
|
2015-09-02 19:20:30 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-09-10 23:50:58 +03:00
|
|
|
#endif // mozilla_dom_ipc_StructuredCloneData_h
|