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_BROKER_STATE_H_ | 5 #ifndef MOJO_EDK_SYSTEM_BROKER_STATE_H_ |
6 #define MOJO_EDK_SYSTEM_BROKER_STATE_H_ | 6 #define MOJO_EDK_SYSTEM_BROKER_STATE_H_ |
7 | 7 |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
10 #include "base/macros.h" | |
10 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
12 #include "base/process/process_handle.h" | |
11 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
12 #include "base/threading/thread.h" | |
13 #include "mojo/edk/embedder/scoped_platform_handle.h" | 14 #include "mojo/edk/embedder/scoped_platform_handle.h" |
14 #include "mojo/edk/system/broker.h" | 15 #include "mojo/edk/system/broker.h" |
15 #include "mojo/edk/system/system_impl_export.h" | 16 #include "mojo/edk/system/system_impl_export.h" |
16 | 17 |
17 namespace mojo { | 18 namespace mojo { |
18 namespace edk { | 19 namespace edk { |
20 class ChildBrokerHost; | |
21 class RoutedRawChannel; | |
19 | 22 |
20 // Common broker state that has to live in a parent process. There is one | 23 // Common broker state that has to live in a parent process. There is one |
21 // instance of this class in the parent process. This class implements the | 24 // instance of this class in the parent process. This class implements the |
22 // Broker interface for use by code in the parent process as well. | 25 // Broker interface for use by code in the parent process as well. |
23 class MOJO_SYSTEM_IMPL_EXPORT BrokerState : NON_EXPORTED_BASE(public Broker) { | 26 class MOJO_SYSTEM_IMPL_EXPORT BrokerState : NON_EXPORTED_BASE(public Broker) { |
24 public: | 27 public: |
25 static BrokerState* GetInstance(); | 28 static BrokerState* GetInstance(); |
26 | 29 |
27 // Broker implementation. | 30 // Broker implementation: |
28 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
29 void CreatePlatformChannelPair(ScopedPlatformHandle* server, | 32 void CreatePlatformChannelPair(ScopedPlatformHandle* server, |
30 ScopedPlatformHandle* client) override; | 33 ScopedPlatformHandle* client) override; |
31 void HandleToToken(const PlatformHandle* platform_handles, | 34 void HandleToToken(const PlatformHandle* platform_handles, |
32 size_t count, | 35 size_t count, |
33 uint64_t* tokens) override; | 36 uint64_t* tokens) override; |
34 void TokenToHandle(const uint64_t* tokens, | 37 void TokenToHandle(const uint64_t* tokens, |
35 size_t count, | 38 size_t count, |
36 PlatformHandle* handles) override; | 39 PlatformHandle* handles) override; |
37 #endif | 40 #endif |
41 void ConnectMessagePipe(uint64_t pipe_id, | |
42 MessagePipeDispatcher* message_pipe) override; | |
43 void CloseMessagePipe(uint64_t pipe_id, | |
44 MessagePipeDispatcher* message_pipe) override; | |
38 | 45 |
39 scoped_refptr<base::TaskRunner> broker_thread() { | 46 // Called by ChildBrokerHost on construction and destruction. |
40 return broker_thread_.task_runner(); | 47 void ChildBrokerHostCreated(ChildBrokerHost* child_broker_host); |
41 } | 48 void ChildBrokerHostDestructed(ChildBrokerHost* child_broker_host); |
49 | |
50 // These are called by ChildBrokerHost as they dispatch IPCs from ChildBroker. | |
51 void HandleConnectMessagePipe(ChildBrokerHost* pipe_process, | |
52 uint64_t pipe_id); | |
53 void HandleCancelConnectMessagePipe(uint64_t pipe_id); | |
42 | 54 |
43 private: | 55 private: |
44 friend struct base::DefaultSingletonTraits<BrokerState>; | 56 friend struct base::DefaultSingletonTraits<BrokerState>; |
45 | 57 |
46 BrokerState(); | 58 BrokerState(); |
47 ~BrokerState() override; | 59 ~BrokerState() override; |
48 | 60 |
49 // A separate thread to handle sync IPCs from child processes for exchanging | 61 // Checks if there's a direct channel between the two processes, and if not |
50 // platform handles with tokens. We use a separate thread because latency is | 62 // creates one and tells them about it. |
51 // very sensitive (since any time a pipe is created or sent, a child process | 63 // If one of the processes is the current one, it should be pid1. |
52 // makes a sync call to this class). | 64 void EnsureProcessesConnected(base::ProcessId pid1, base::ProcessId pid2); |
53 base::Thread broker_thread_; | 65 |
66 // Callback when a RoutedRawChannel is destroyed for cleanup. | |
67 void ChannelDestructed(RoutedRawChannel* channel); | |
54 | 68 |
55 #if defined(OS_WIN) | 69 #if defined(OS_WIN) |
56 // Used in the parent (unsandboxed) process to hold a mapping between HANDLES | 70 // Used in the parent (unsandboxed) process to hold a mapping between HANDLES |
57 // and tokens. When a child process wants to send a HANDLE to another process, | 71 // and tokens. When a child process wants to send a HANDLE to another process, |
58 // it exchanges it to a token and then the other process exchanges that token | 72 // it exchanges it to a token and then the other process exchanges that token |
59 // back to a HANDLE. | 73 // back to a HANDLE. |
60 base::Lock lock_; // Guards access to below. | 74 base::Lock token_map_lock_; |
61 base::hash_map<uint64_t, HANDLE> token_map_; | 75 base::hash_map<uint64_t, HANDLE> token_map_; |
62 #endif | 76 #endif |
77 | |
78 base::Lock lock_; // Guards access to below. | |
yzshen1
2015/12/03 23:37:50
If ConnectMessagePipe() and CloseMessagePipe() are
jam
2015/12/04 05:06:47
The issue is that child_processes_ is accessed in
yzshen1
2015/12/04 05:57:05
I see. Thanks for explanation!
| |
79 | |
80 // For pending connects originiating in this process. | |
81 base::hash_map<uint64_t, MessagePipeDispatcher*> pending_connects_; | |
82 | |
83 // For connected message pipes in this process. This is needed so that when a | |
84 // MessagePipeDispatcher is closed we can remove the route for the | |
85 // corresponding RoutedRawChannel. | |
86 base::hash_map<MessagePipeDispatcher*, RoutedRawChannel*> connected_pipes_; | |
87 | |
88 // Holds a map of all the RoutedRawChannel that connect this parent process to | |
89 // a child process. The key is the child process'd pid. | |
90 base::hash_map<base::ProcessId, RoutedRawChannel*> child_channels_; | |
91 | |
92 base::hash_map<uint64_t, ChildBrokerHost*> pending_child_connects_; | |
93 | |
94 // Each entry is an std::pair of ints of processe IDs that have | |
95 // RoutedRawChannel objects between them. The pair always has the smaller | |
96 // process id value first. | |
97 // For now, we don't reap connections if there are no more routes between two | |
98 // processes. | |
99 base::hash_set<std::pair<base::ProcessId, base::ProcessId>> | |
100 connected_processes_; | |
101 | |
102 base::hash_map<base::ProcessId, ChildBrokerHost*> child_processes_; | |
103 | |
104 // Used for message pipes in the same process. | |
105 RoutedRawChannel* in_process_pipes_channel1_; | |
106 RoutedRawChannel* in_process_pipes_channel2_; | |
107 | |
108 DISALLOW_COPY_AND_ASSIGN(BrokerState); | |
63 }; | 109 }; |
64 | 110 |
65 } // namespace edk | 111 } // namespace edk |
66 } // namespace mojo | 112 } // namespace mojo |
67 | 113 |
68 #endif // MOJO_EDK_SYSTEM_BROKER_STATE_H_ | 114 #endif // MOJO_EDK_SYSTEM_BROKER_STATE_H_ |
OLD | NEW |