2016-05-28 00:54:31 +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: */
|
2016-05-15 20:32:09 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_ipc_IPCStreamUtils_h
|
|
|
|
#define mozilla_ipc_IPCStreamUtils_h
|
|
|
|
|
|
|
|
#include "mozilla/ipc/IPCStream.h"
|
2018-02-23 23:26:38 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2016-05-15 20:32:09 +03:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace dom {
|
2019-02-25 23:04:49 +03:00
|
|
|
class ContentChild;
|
2019-02-25 23:04:47 +03:00
|
|
|
class ContentParent;
|
2016-05-15 20:32:09 +03:00
|
|
|
} // namespace dom
|
|
|
|
|
2019-12-03 16:41:58 +03:00
|
|
|
namespace net {
|
|
|
|
class SocketProcessParent;
|
|
|
|
class SocketProcessChild;
|
|
|
|
} // namespace net
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
class PBackgroundChild;
|
|
|
|
class PBackgroundParent;
|
|
|
|
|
|
|
|
// Deserialize an IPCStream received from an actor call. These methods
|
|
|
|
// work in both the child and parent.
|
|
|
|
already_AddRefed<nsIInputStream> DeserializeIPCStream(const IPCStream& aValue);
|
|
|
|
|
|
|
|
already_AddRefed<nsIInputStream> DeserializeIPCStream(
|
2019-02-22 21:46:14 +03:00
|
|
|
const Maybe<IPCStream>& aValue);
|
2016-05-15 20:32:09 +03:00
|
|
|
|
|
|
|
// RAII helper class that serializes an nsIInputStream into an IPCStream struct.
|
2017-03-14 14:28:58 +03:00
|
|
|
// Any file descriptor or PChildToParentStream actors are automatically managed
|
2016-05-15 20:32:09 +03:00
|
|
|
// correctly.
|
|
|
|
//
|
|
|
|
// Here is a simple example:
|
|
|
|
//
|
|
|
|
// // in ipdl file
|
|
|
|
// Protocol PMyStuff
|
|
|
|
// {
|
|
|
|
// parent:
|
|
|
|
// async DoStuff(IPCStream aStream);
|
|
|
|
// child:
|
|
|
|
// async StuffDone(IPCStream aStream);
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// // in child c++ code
|
|
|
|
// void CallDoStuff(PMyStuffChild* aActor, nsIInputStream* aStream)
|
|
|
|
// {
|
|
|
|
// AutoIPCStream autoStream;
|
|
|
|
// autoStream.Serialize(aStream, aActor->Manager());
|
|
|
|
// aActor->SendDoStuff(autoStream.TakeValue());
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // in parent c++ code
|
|
|
|
// bool
|
|
|
|
// MyStuffParent::RecvDoStuff(const IPCStream& aIPCStream) {
|
|
|
|
// nsCOMPtr<nsIInputStream> stream = DeserializeIPCStream(aIPCStream);
|
|
|
|
// // Do something with stream...
|
|
|
|
//
|
|
|
|
// // You can also serialize streams from parent-to-child as long as
|
2017-03-14 14:28:58 +03:00
|
|
|
// // they don't require PChildToParentStream actor support.
|
2016-05-15 20:32:09 +03:00
|
|
|
// AutoIPCStream anotherStream;
|
|
|
|
// anotherStream.Serialize(mFileStream, Manager());
|
|
|
|
// SendStuffDone(anotherStream.TakeValue());
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The AutoIPCStream RAII class may also be used if your stream is embedded
|
|
|
|
// in a more complex IPDL structure. In this case you attach the AutoIPCStream
|
|
|
|
// to the embedded IPCStream and call TakeValue() after you pass the structure.
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// // in ipdl file
|
|
|
|
// struct Stuff
|
|
|
|
// {
|
|
|
|
// IPCStream stream;
|
|
|
|
// nsCString name;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// Protocol PMyStuff
|
|
|
|
// {
|
|
|
|
// parent:
|
|
|
|
// async DoStuff(Stuff aStream);
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// // in child c++ code
|
|
|
|
// void CallDoStuff(PMyStuffChild* aActor, nsIInputStream* aStream)
|
|
|
|
// {
|
|
|
|
// Stuff stuff;
|
|
|
|
// AutoIPCStream autoStream(stuff.stream()); // attach to IPCStream here
|
|
|
|
// autoStream.Serialize(aStream, aActor->Manager());
|
|
|
|
// aActor->SendDoStuff(stuff);
|
|
|
|
// autoStream.TakeValue(); // call take value after send
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // in parent c++ code
|
|
|
|
// bool
|
|
|
|
// MyStuffParent::RecvDoStuff(const Stuff& aStuff) {
|
|
|
|
// nsCOMPtr<nsIInputStream> stream = DeserializeIPCStream(aStuff.stream());
|
|
|
|
// /* do something with the nsIInputStream */
|
|
|
|
// }
|
|
|
|
//
|
2017-03-14 14:29:43 +03:00
|
|
|
// Note: This example is about child-to-parent inputStream, but AutoIPCStream
|
|
|
|
// works also parent-to-child.
|
|
|
|
//
|
2019-02-22 21:46:14 +03:00
|
|
|
// The AutoIPCStream class also supports Maybe<IPCStream> values. As long as
|
2016-05-15 20:32:09 +03:00
|
|
|
// you did not initialize the object with a non-optional IPCStream, you can call
|
|
|
|
// TakeOptionalValue() instead.
|
|
|
|
//
|
|
|
|
// The AutoIPCStream class can also be used to serialize nsIInputStream objects
|
|
|
|
// on the parent side to send to the child. Currently, however, this only
|
2017-03-14 14:28:58 +03:00
|
|
|
// works for directly serializable stream types. The PChildToParentStream actor
|
|
|
|
// mechanism is not supported in this direction yet.
|
2016-05-15 20:32:09 +03:00
|
|
|
//
|
|
|
|
// Like SerializeInputStream(), the AutoIPCStream will crash if
|
|
|
|
// serialization cannot be completed.
|
|
|
|
//
|
|
|
|
// NOTE: This is not a MOZ_STACK_CLASS so that it can be more easily integrated
|
|
|
|
// with complex ipdl structures. For example, you may want to create an
|
|
|
|
// array of RAII AutoIPCStream objects or build your own wrapping
|
|
|
|
// RAII object to handle other actors that need to be cleaned up.
|
2019-11-28 22:50:43 +03:00
|
|
|
class AutoIPCStream {
|
2016-05-15 20:32:09 +03:00
|
|
|
public:
|
2019-02-22 21:46:14 +03:00
|
|
|
// Implicitly create an Maybe<IPCStream> value. Either
|
2016-05-15 20:32:09 +03:00
|
|
|
// TakeValue() or TakeOptionalValue() can be used.
|
2017-04-24 13:09:39 +03:00
|
|
|
explicit AutoIPCStream(bool aDelayedStart = false);
|
2016-05-15 20:32:09 +03:00
|
|
|
|
|
|
|
// Wrap an existing IPCStream. Only TakeValue() may be
|
|
|
|
// used. If a nullptr nsIInputStream is passed to SerializeOrSend() then
|
|
|
|
// a crash will be forced.
|
2017-04-24 13:09:39 +03:00
|
|
|
explicit AutoIPCStream(IPCStream& aTarget, bool aDelayedStart = false);
|
2016-05-15 20:32:09 +03:00
|
|
|
|
2019-02-22 21:46:14 +03:00
|
|
|
// Wrap an existing Maybe<IPCStream>. Either TakeValue()
|
2016-05-15 20:32:09 +03:00
|
|
|
// or TakeOptionalValue can be used.
|
2019-02-22 21:46:14 +03:00
|
|
|
explicit AutoIPCStream(Maybe<IPCStream>& aTarget, bool aDelayedStart = false);
|
2016-05-15 20:32:09 +03:00
|
|
|
|
2019-10-12 02:22:24 +03:00
|
|
|
AutoIPCStream(const AutoIPCStream&) = delete;
|
|
|
|
AutoIPCStream(AutoIPCStream&&) = delete;
|
|
|
|
|
|
|
|
AutoIPCStream& operator=(const AutoIPCStream&) = delete;
|
|
|
|
AutoIPCStream& operator=(AutoIPCStream&&) = delete;
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
~AutoIPCStream();
|
|
|
|
|
|
|
|
// Serialize the input stream or create a SendStream actor using the PContent
|
|
|
|
// manager. If neither of these succeed, then crash. This should only be
|
|
|
|
// used on the main thread.
|
2019-02-25 23:04:49 +03:00
|
|
|
bool Serialize(nsIInputStream* aStream, dom::ContentChild* aManager);
|
2016-05-15 20:32:09 +03:00
|
|
|
|
|
|
|
// Serialize the input stream or create a SendStream actor using the
|
|
|
|
// PBackground manager. If neither of these succeed, then crash. This can
|
|
|
|
// be called on the main thread or Worker threads.
|
|
|
|
bool Serialize(nsIInputStream* aStream, PBackgroundChild* aManager);
|
|
|
|
|
2019-12-03 16:41:58 +03:00
|
|
|
// Serialize the input stream or create a SendStream actor using the
|
|
|
|
// SocketProcess manager. If neither of these succeed, then crash. This
|
|
|
|
// should only be used on the main thread.
|
|
|
|
bool Serialize(nsIInputStream* aStream, net::SocketProcessChild* aManager);
|
|
|
|
|
2017-03-14 14:29:43 +03:00
|
|
|
// Serialize the input stream.
|
2020-04-17 01:14:21 +03:00
|
|
|
[[nodiscard]] bool Serialize(nsIInputStream* aStream,
|
|
|
|
dom::ContentParent* aManager);
|
2016-05-15 20:32:09 +03:00
|
|
|
|
2017-03-14 14:29:43 +03:00
|
|
|
// Serialize the input stream.
|
2020-04-17 01:14:21 +03:00
|
|
|
[[nodiscard]] bool Serialize(nsIInputStream* aStream,
|
|
|
|
PBackgroundParent* aManager);
|
2016-05-15 20:32:09 +03:00
|
|
|
|
2019-12-03 16:41:58 +03:00
|
|
|
// Serialize the input stream.
|
2020-04-17 01:14:21 +03:00
|
|
|
[[nodiscard]] bool Serialize(nsIInputStream* aStream,
|
|
|
|
net::SocketProcessParent* aManager);
|
2019-12-03 16:41:58 +03:00
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
// Get the IPCStream as a non-optional value. This will
|
|
|
|
// assert if a stream has not been serialized or if it has already been taken.
|
|
|
|
// This should only be called if the value is being, or has already been, sent
|
2017-03-14 14:29:43 +03:00
|
|
|
// to the other side.
|
2016-05-15 20:32:09 +03:00
|
|
|
IPCStream& TakeValue();
|
|
|
|
|
2019-02-22 21:46:14 +03:00
|
|
|
// Get the Maybe<IPCStream> value. This will assert if the value has already
|
|
|
|
// been taken. This should only be called if the value is being, or has
|
|
|
|
// already been, sent to the other side.
|
|
|
|
Maybe<IPCStream>& TakeOptionalValue();
|
2017-04-24 13:09:40 +03:00
|
|
|
|
|
|
|
private:
|
2019-10-12 02:22:24 +03:00
|
|
|
bool IsSet() const;
|
|
|
|
|
|
|
|
Maybe<IPCStream> mInlineValue;
|
|
|
|
IPCStream* const mValue = nullptr;
|
|
|
|
Maybe<IPCStream>* const mOptionalValue = nullptr;
|
|
|
|
bool mTaken = false;
|
|
|
|
const bool mDelayedStart;
|
2016-05-15 20:32:09 +03:00
|
|
|
};
|
|
|
|
|
2019-11-28 22:50:43 +03:00
|
|
|
class HoldIPCStream final : public AutoIPCStream {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(HoldIPCStream)
|
|
|
|
|
|
|
|
private:
|
|
|
|
~HoldIPCStream() = default;
|
|
|
|
};
|
|
|
|
|
2018-02-23 23:26:38 +03:00
|
|
|
template <>
|
2019-05-21 20:04:39 +03:00
|
|
|
struct IPDLParamTraits<nsIInputStream*> {
|
2018-02-23 23:26:38 +03:00
|
|
|
static void Write(IPC::Message* aMsg, IProtocol* aActor,
|
Bug 1443954 - Part 3: Add support for RefCounted types to IPDL, r=bz,froydnj,baku
This patch was reviewed in parts, however the intermediate states would not build:
Bug 1443954 - Part 3A: Strip pointers from the argument to WriteParam and WriteIPDLParam before selecting the ParamTraits impl, r=froydnj
Bug 1443954 - Part 3B: Move nsIAlertNotification serialization to the refcounted system, r=bz
Bug 1443954 - Part 3C: Move geolocation serialization to the refcounted system, r=bz
Bug 1443954 - Part 3D: Move nsIInputStream serialization to the refcounted system, r=baku
Bug 1443954 - Part 3E: Move BlobImpl serialization to the refcounted system, r=baku
Bug 1443954 - Part 3F: Correctly implement ParamTraits for actors after the ParamTraits changes, r=froydnj
2018-03-07 04:14:59 +03:00
|
|
|
nsIInputStream* aParam);
|
2018-02-23 23:26:38 +03:00
|
|
|
static bool Read(const IPC::Message* aMsg, PickleIterator* aIter,
|
Bug 1443954 - Part 3: Add support for RefCounted types to IPDL, r=bz,froydnj,baku
This patch was reviewed in parts, however the intermediate states would not build:
Bug 1443954 - Part 3A: Strip pointers from the argument to WriteParam and WriteIPDLParam before selecting the ParamTraits impl, r=froydnj
Bug 1443954 - Part 3B: Move nsIAlertNotification serialization to the refcounted system, r=bz
Bug 1443954 - Part 3C: Move geolocation serialization to the refcounted system, r=bz
Bug 1443954 - Part 3D: Move nsIInputStream serialization to the refcounted system, r=baku
Bug 1443954 - Part 3E: Move BlobImpl serialization to the refcounted system, r=baku
Bug 1443954 - Part 3F: Correctly implement ParamTraits for actors after the ParamTraits changes, r=froydnj
2018-03-07 04:14:59 +03:00
|
|
|
IProtocol* aActor, RefPtr<nsIInputStream>* aResult);
|
2018-02-23 23:26:38 +03:00
|
|
|
};
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_ipc_IPCStreamUtils_h
|