1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_ipc_RawEndpoint_h
8 #define mozilla_ipc_RawEndpoint_h
9 
10 #include "mojo/core/ports/port_ref.h"
11 
12 namespace mozilla::ipc {
13 
14 class NodeController;
15 
16 /// A uniquely owned raw IPC endpoint, connected to our peers over the node
17 /// controller. This type can be sent over IPC channels to establish
18 /// connections.
19 ///
20 /// In general, prefer using Endpoint over ScopedPort for type safety reasons.
21 class ScopedPort {
22   using PortName = mojo::core::ports::PortName;
23   using PortRef = mojo::core::ports::PortRef;
24 
25  public:
26   ScopedPort();
27   ~ScopedPort();
28 
29   ScopedPort(PortRef aPort, NodeController* aController);
30 
31   ScopedPort(ScopedPort&& aOther);
32   ScopedPort(const ScopedPort&) = delete;
33 
34   ScopedPort& operator=(ScopedPort&& aOther);
35   ScopedPort& operator=(const ScopedPort&) = delete;
36 
37   // Allow checking if this `ScopedPort` is valid or not.
IsValid()38   bool IsValid() const { return mValid; }
39   explicit operator bool() const { return IsValid(); }
40 
41   // Underlying port and controller which are used by this ScopedPort.
Name()42   const PortName& Name() const { return mPort.name(); }
Port()43   const PortRef& Port() const { return mPort; }
Controller()44   NodeController* Controller() const { return mController; }
45 
46   // Release ownership over the contained `ScopedPort`, meaning that it will
47   // not be closed when this ScopedPort is destroyed. This will make the
48   // endpoint invalid.
49   PortRef Release();
50 
51  private:
52   void Reset();
53 
54   bool mValid = false;
55   PortRef mPort;
56   RefPtr<NodeController> mController;
57 
58   // NOTE: This type does not contain PID information about the other process,
59   // which will need to be sent separately if necessary.
60 };
61 
62 }  // namespace mozilla::ipc
63 
64 namespace IPC {
65 
66 template <typename T>
67 struct ParamTraits;
68 
69 template <>
70 struct ParamTraits<mozilla::ipc::ScopedPort> {
71   using paramType = mozilla::ipc::ScopedPort;
72 
73   static void Write(Message* aMsg, paramType&& aParam);
74   static bool Read(const Message* aMsg, PickleIterator* aIter,
75                    paramType* aResult);
76 };
77 
78 }  // namespace IPC
79 
80 #endif
81