OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_EDK_SYSTEM_CHILD_BROKER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_CHILD_BROKER_H_ |
6 #define MOJO_EDK_SYSTEM_CHILD_BROKER_H_ | 6 #define MOJO_EDK_SYSTEM_CHILD_BROKER_H_ |
7 | 7 |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/containers/hash_tables.h" |
| 10 #include "base/macros.h" |
8 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
| 12 #include "base/process/process_handle.h" |
9 #include "base/synchronization/lock_impl.h" | 13 #include "base/synchronization/lock_impl.h" |
10 #include "mojo/edk/embedder/scoped_platform_handle.h" | 14 #include "mojo/edk/embedder/scoped_platform_handle.h" |
11 #include "mojo/edk/system/broker.h" | 15 #include "mojo/edk/system/broker.h" |
| 16 #include "mojo/edk/system/raw_channel.h" |
12 #include "mojo/edk/system/system_impl_export.h" | 17 #include "mojo/edk/system/system_impl_export.h" |
13 | 18 |
14 namespace mojo { | 19 namespace mojo { |
15 namespace edk { | 20 namespace edk { |
| 21 class RoutedRawChannel; |
16 struct BrokerMessage; | 22 struct BrokerMessage; |
17 | 23 |
18 // An implementation of Broker used in (sandboxed) child processes. It talks | 24 // An implementation of Broker used in (sandboxed) child processes. It talks |
19 // over sync IPCs to the (unsandboxed) parent process (specifically, | 25 // over sync IPCs to the (unsandboxed) parent process (specifically, |
20 // ParentBroker) to convert handles to tokens and vice versa. | 26 // ChildBrokerHost) to convert handles to tokens and vice versa. |
21 class MOJO_SYSTEM_IMPL_EXPORT ChildBroker : public Broker { | 27 class MOJO_SYSTEM_IMPL_EXPORT ChildBroker |
| 28 : public Broker, public RawChannel::Delegate { |
22 public: | 29 public: |
23 static ChildBroker* GetInstance(); | 30 static ChildBroker* GetInstance(); |
24 | 31 |
25 // Passes the platform handle that is used to talk to ChildBrokerHost. | 32 // Passes the platform handle that is used to talk to ChildBrokerHost. |
26 void SetChildBrokerHostHandle(ScopedPlatformHandle handle); | 33 void SetChildBrokerHostHandle(ScopedPlatformHandle handle); |
27 | 34 |
28 // Broker implementation: | 35 // Broker implementation: |
29 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
30 void CreatePlatformChannelPair(ScopedPlatformHandle* server, | 37 void CreatePlatformChannelPair(ScopedPlatformHandle* server, |
31 ScopedPlatformHandle* client) override; | 38 ScopedPlatformHandle* client) override; |
32 void HandleToToken(const PlatformHandle* platform_handles, | 39 void HandleToToken(const PlatformHandle* platform_handles, |
33 size_t count, | 40 size_t count, |
34 uint64_t* tokens) override; | 41 uint64_t* tokens) override; |
35 void TokenToHandle(const uint64_t* tokens, | 42 void TokenToHandle(const uint64_t* tokens, |
36 size_t count, | 43 size_t count, |
37 PlatformHandle* handles) override; | 44 PlatformHandle* handles) override; |
38 #endif | 45 #endif |
| 46 void ConnectMessagePipe(uint64_t pipe_id, |
| 47 MessagePipeDispatcher* message_pipe) override; |
| 48 void CloseMessagePipe(uint64_t pipe_id, |
| 49 MessagePipeDispatcher* message_pipe) override; |
39 | 50 |
40 private: | 51 private: |
41 friend struct base::DefaultSingletonTraits<ChildBroker>; | 52 friend struct base::DefaultSingletonTraits<ChildBroker>; |
42 | 53 |
43 ChildBroker(); | 54 ChildBroker(); |
44 ~ChildBroker() override; | 55 ~ChildBroker() override; |
45 | 56 |
| 57 // RawChannel::Delegate implementation: |
| 58 void OnReadMessage( |
| 59 const MessageInTransit::View& message_view, |
| 60 ScopedPlatformHandleVectorPtr platform_handles) override; |
| 61 void OnError(Error error) override; |
| 62 |
| 63 // Callback for when a RoutedRawChannel is destroyed for cleanup. |
| 64 void ChannelDestructed(RoutedRawChannel* channel); |
| 65 |
| 66 #if defined(OS_WIN) |
46 // Helper method to write the given message and read back the result. | 67 // Helper method to write the given message and read back the result. |
47 bool WriteAndReadResponse(BrokerMessage* message, | 68 bool WriteAndReadResponse(BrokerMessage* message, |
48 void* response, | 69 void* response, |
49 uint32_t response_size); | 70 uint32_t response_size); |
50 | 71 |
| 72 void CreatePlatformChannelPairNoLock(ScopedPlatformHandle* server, |
| 73 ScopedPlatformHandle* client); |
| 74 |
| 75 // Pipe used for communication to the parent process. We use a pipe directly |
| 76 // instead of bindings or RawChannel because we need to send synchronous |
| 77 // messages with replies from any thread. |
| 78 ScopedPlatformHandle parent_sync_channel_; |
| 79 #endif |
| 80 |
| 81 // RawChannel used for asynchronous communication to and from the parent |
| 82 // process. Since these messages are bidirectional, we can't use |
| 83 // |parent_sync_channel_| which is only used for sync messages to the parent. |
| 84 // However since the messages are asynchronous, we can use RawChannel for |
| 85 // convenience instead of writing and reading from pipes manually. Although it |
| 86 // would be convenient, we don't use Mojo IPC because it would be a layering |
| 87 // violation (and cirular dependency) if the system layer depended on |
| 88 // bindings. |
| 89 RawChannel* parent_async_channel_; |
| 90 |
51 // Guards access to below. | 91 // Guards access to below. |
52 // We use LockImpl instead of Lock because the latter adds thread checking | 92 // We use LockImpl instead of Lock because the latter adds thread checking |
53 // that we don't want (since we lock in the constructor and unlock on another | 93 // that we don't want (since we lock in the constructor and unlock on another |
54 // thread. | 94 // thread. |
55 base::internal::LockImpl lock_; | 95 base::internal::LockImpl lock_; |
56 ScopedPlatformHandle handle_; | 96 |
| 97 // Maps from routing ids to the MessagePipeDispatcher that have requested to |
| 98 // connect to them. When the parent replies with which process they should be |
| 99 // connected to, they will migrate to |connected_pipes_|. |
| 100 base::hash_map<uint64_t, MessagePipeDispatcher*> pending_connects_; |
| 101 |
| 102 // Map from MessagePipeDispatcher to its RoutedRawChannel. This is needed so |
| 103 // that when a MessagePipeDispatcher is closed we can remove the route for the |
| 104 // corresponding RoutedRawChannel. |
| 105 // Note the key is MessagePipeDispatcher*, instead of pipe_id, because when |
| 106 // the two pipes are in the same process they will have one pipe_id but be |
| 107 // connected to the two RoutedRawChannel objects below. |
| 108 base::hash_map<MessagePipeDispatcher*, RoutedRawChannel*> connected_pipes_; |
| 109 |
| 110 // Holds a map of all the RoutedRawChannel that connect this child process to |
| 111 // any other process. The key is the peer process'd pid. |
| 112 base::hash_map<base::ProcessId, RoutedRawChannel*> channels_; |
| 113 |
| 114 // Used for message pipes in the same process. |
| 115 RoutedRawChannel* in_process_pipes_channel1_; |
| 116 RoutedRawChannel* in_process_pipes_channel2_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(ChildBroker); |
57 }; | 119 }; |
58 | 120 |
59 } // namespace edk | 121 } // namespace edk |
60 } // namespace mojo | 122 } // namespace mojo |
61 | 123 |
62 #endif // MOJO_EDK_SYSTEM_CHILD_BROKER_H_ | 124 #endif // MOJO_EDK_SYSTEM_CHILD_BROKER_H_ |
OLD | NEW |