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