1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_ISOLATED_CONNECTION_H_ 6 #define MOJO_PUBLIC_CPP_SYSTEM_ISOLATED_CONNECTION_H_ 7 8 #include "base/macros.h" 9 #include "base/unguessable_token.h" 10 #include "mojo/public/cpp/platform/platform_channel_endpoint.h" 11 #include "mojo/public/cpp/platform/platform_channel_server_endpoint.h" 12 #include "mojo/public/cpp/system/message_pipe.h" 13 #include "mojo/public/cpp/system/system_export.h" 14 15 namespace mojo { 16 17 // IsolatedConnection establishes a one-off Mojo IPC connection between two 18 // processes. Unlike more common connections established by invitation 19 // (see OutgoingInvitation and IncomingInvitation), isolated connections 20 // do not result in the two processes becoming part of the same connected 21 // graph of processes. As such, any message pipe established over this 22 // connection can only be used for direct IPC between the two processes in 23 // question. 24 // 25 // This means that if one of the processes sends a Mojo handle (e.g. another 26 // message pipe endpoint) to the other process, the receiving process cannot 27 // pass that handle to yet another process in its own graph. This limitation is 28 // subtle and can be difficult to work around, so use of IsolatedConnection 29 // should be rare. 30 // 31 // This is primarily useful when you already have two established Mojo process 32 // graphs isolated form each other, and you want to do some IPC between two 33 // processes, one in each graph. 34 // 35 // A connection established via |Connect()|, and any opened message pipes 36 // spanning that connection, will remain valid and connected as long as this 37 // object remains alive. 38 class MOJO_CPP_SYSTEM_EXPORT IsolatedConnection { 39 public: 40 IsolatedConnection(); 41 ~IsolatedConnection(); 42 43 // Connects to a process at the other end of the channel. Returns a primordial 44 // message pipe that can be used for Mojo IPC. The connection 45 // will be connected to a corresponding peer pipe in the remote process. 46 ScopedMessagePipeHandle Connect(PlatformChannelEndpoint endpoint); 47 48 // Same as above but works with a server endpoint. The corresponding client 49 // could use the above signature with NamedPlatformChannel::ConnectToServer. 50 ScopedMessagePipeHandle Connect(PlatformChannelServerEndpoint endpoint); 51 52 private: 53 const base::UnguessableToken token_; 54 55 DISALLOW_COPY_AND_ASSIGN(IsolatedConnection); 56 }; 57 58 } // namespace mojo 59 60 #endif // MOJO_PUBLIC_CPP_SYSTEM_ISOLATED_CONNECTION_H_ 61