Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: mojo/edk/system/child_broker.h

Issue 1488853002: Add multiplexing of message pipes in the new EDK. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tsepez review comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/system/broker_state.cc ('k') | mojo/edk/system/child_broker.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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. It also sends
21 class MOJO_SYSTEM_IMPL_EXPORT ChildBroker : public Broker { 27 // async messages to handle message pipe multiplexing.
28 class MOJO_SYSTEM_IMPL_EXPORT ChildBroker
29 : public Broker, public RawChannel::Delegate {
22 public: 30 public:
23 static ChildBroker* GetInstance(); 31 static ChildBroker* GetInstance();
24 32
25 // Passes the platform handle that is used to talk to ChildBrokerHost. 33 // Passes the platform handle that is used to talk to ChildBrokerHost.
26 void SetChildBrokerHostHandle(ScopedPlatformHandle handle); 34 void SetChildBrokerHostHandle(ScopedPlatformHandle handle);
27 35
28 // Broker implementation: 36 // Broker implementation:
29 #if defined(OS_WIN) 37 #if defined(OS_WIN)
30 void CreatePlatformChannelPair(ScopedPlatformHandle* server, 38 void CreatePlatformChannelPair(ScopedPlatformHandle* server,
31 ScopedPlatformHandle* client) override; 39 ScopedPlatformHandle* client) override;
32 void HandleToToken(const PlatformHandle* platform_handles, 40 void HandleToToken(const PlatformHandle* platform_handles,
33 size_t count, 41 size_t count,
34 uint64_t* tokens) override; 42 uint64_t* tokens) override;
35 void TokenToHandle(const uint64_t* tokens, 43 void TokenToHandle(const uint64_t* tokens,
36 size_t count, 44 size_t count,
37 PlatformHandle* handles) override; 45 PlatformHandle* handles) override;
38 #endif 46 #endif
47 void ConnectMessagePipe(uint64_t pipe_id,
48 MessagePipeDispatcher* message_pipe) override;
49 void CloseMessagePipe(uint64_t pipe_id,
50 MessagePipeDispatcher* message_pipe) override;
39 51
40 private: 52 private:
41 friend struct base::DefaultSingletonTraits<ChildBroker>; 53 friend struct base::DefaultSingletonTraits<ChildBroker>;
42 54
43 ChildBroker(); 55 ChildBroker();
44 ~ChildBroker() override; 56 ~ChildBroker() override;
45 57
58 // RawChannel::Delegate implementation:
59 void OnReadMessage(
60 const MessageInTransit::View& message_view,
61 ScopedPlatformHandleVectorPtr platform_handles) override;
62 void OnError(Error error) override;
63
64 // Callback for when a RoutedRawChannel is destroyed for cleanup.
65 void ChannelDestructed(RoutedRawChannel* channel);
66
67 #if defined(OS_WIN)
46 // Helper method to write the given message and read back the result. 68 // Helper method to write the given message and read back the result.
47 bool WriteAndReadResponse(BrokerMessage* message, 69 bool WriteAndReadResponse(BrokerMessage* message,
48 void* response, 70 void* response,
49 uint32_t response_size); 71 uint32_t response_size);
50 72
73 void CreatePlatformChannelPairNoLock(ScopedPlatformHandle* server,
74 ScopedPlatformHandle* client);
75
76 // Pipe used for communication to the parent process. We use a pipe directly
77 // instead of bindings or RawChannel because we need to send synchronous
78 // messages with replies from any thread.
79 ScopedPlatformHandle parent_sync_channel_;
80 #endif
81
51 // Guards access to below. 82 // Guards access to below.
52 // We use LockImpl instead of Lock because the latter adds thread checking 83 // 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 84 // that we don't want (since we lock in the constructor and unlock on another
54 // thread. 85 // thread.
55 base::internal::LockImpl lock_; 86 base::internal::LockImpl lock_;
56 ScopedPlatformHandle handle_; 87
88 // RawChannel used for asynchronous communication to and from the parent
89 // process. Since these messages are bidirectional, we can't use
90 // |parent_sync_channel_| which is only used for sync messages to the parent.
91 // However since the messages are asynchronous, we can use RawChannel for
92 // convenience instead of writing and reading from pipes manually. Although it
93 // would be convenient, we don't use Mojo IPC because it would be a layering
94 // violation (and cirular dependency) if the system layer depended on
95 // bindings.
96 RawChannel* parent_async_channel_;
97
98 // Maps from routing ids to the MessagePipeDispatcher that have requested to
99 // connect to them. When the parent replies with which process they should be
100 // connected to, they will migrate to |connected_pipes_|.
101 base::hash_map<uint64_t, MessagePipeDispatcher*> pending_connects_;
102
103 // Map from MessagePipeDispatcher to its RoutedRawChannel. This is needed so
104 // that when a MessagePipeDispatcher is closed we can remove the route for the
105 // corresponding RoutedRawChannel.
106 // Note the key is MessagePipeDispatcher*, instead of pipe_id, because when
107 // the two pipes are in the same process they will have one pipe_id but be
108 // connected to the two RoutedRawChannel objects below.
109 base::hash_map<MessagePipeDispatcher*, RoutedRawChannel*> connected_pipes_;
110
111 // Holds a map of all the RoutedRawChannel that connect this child process to
112 // any other process. The key is the peer process'd pid.
113 base::hash_map<base::ProcessId, RoutedRawChannel*> channels_;
114
115 // Used for message pipes in the same process.
116 RoutedRawChannel* in_process_pipes_channel1_;
117 RoutedRawChannel* in_process_pipes_channel2_;
118
119 DISALLOW_COPY_AND_ASSIGN(ChildBroker);
57 }; 120 };
58 121
59 } // namespace edk 122 } // namespace edk
60 } // namespace mojo 123 } // namespace mojo
61 124
62 #endif // MOJO_EDK_SYSTEM_CHILD_BROKER_H_ 125 #endif // MOJO_EDK_SYSTEM_CHILD_BROKER_H_
OLDNEW
« no previous file with comments | « mojo/edk/system/broker_state.cc ('k') | mojo/edk/system/child_broker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698