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 #include "mojo/public/cpp/system/isolated_connection.h"
6 
7 #include "mojo/public/cpp/platform/platform_channel.h"
8 #include "mojo/public/cpp/system/invitation.h"
9 
10 namespace mojo {
11 
IsolatedConnection()12 IsolatedConnection::IsolatedConnection()
13     : token_(base::UnguessableToken::Create()) {}
14 
~IsolatedConnection()15 IsolatedConnection::~IsolatedConnection() {
16   // We send a dummy invitation over a temporary channel, re-using |token_| as
17   // the name. This ensures that the connection set up by Connect(), if any,
18   // will be replaced with a short-lived, self-terminating connection.
19   //
20   // This is a bit of a hack since Mojo does not provide any API for explicitly
21   // terminating isolated connections, but this is a decision made to minimize
22   // the API surface dedicated to isolated connections in anticipation of the
23   // concept being deprecated eventually.
24   PlatformChannel channel;
25   OutgoingInvitation::SendIsolated(channel.TakeLocalEndpoint(),
26                                    token_.ToString());
27 }
28 
Connect(PlatformChannelEndpoint endpoint)29 ScopedMessagePipeHandle IsolatedConnection::Connect(
30     PlatformChannelEndpoint endpoint) {
31   return OutgoingInvitation::SendIsolated(std::move(endpoint),
32                                           token_.ToString());
33 }
34 
Connect(PlatformChannelServerEndpoint endpoint)35 ScopedMessagePipeHandle IsolatedConnection::Connect(
36     PlatformChannelServerEndpoint endpoint) {
37   return OutgoingInvitation::SendIsolated(std::move(endpoint),
38                                           token_.ToString());
39 }
40 
41 }  // namespace mojo
42