| 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_BROKER_STATE_H_ | |
| 6 #define MOJO_EDK_SYSTEM_BROKER_STATE_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.h" | |
| 17 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
| 18 #include "mojo/edk/system/broker.h" | |
| 19 #include "mojo/edk/system/system_impl_export.h" | |
| 20 | |
| 21 namespace mojo { | |
| 22 namespace edk { | |
| 23 class ChildBrokerHost; | |
| 24 class RoutedRawChannel; | |
| 25 | |
| 26 // Common broker state that has to live in a parent process. There is one | |
| 27 // instance of this class in the parent process. This class implements the | |
| 28 // Broker interface for use by code in the parent process as well. | |
| 29 class MOJO_SYSTEM_IMPL_EXPORT BrokerState : NON_EXPORTED_BASE(public Broker) { | |
| 30 public: | |
| 31 static BrokerState* GetInstance(); | |
| 32 | |
| 33 // Broker implementation: | |
| 34 #if defined(OS_WIN) | |
| 35 void CreatePlatformChannelPair(ScopedPlatformHandle* server, | |
| 36 ScopedPlatformHandle* client) override; | |
| 37 void HandleToToken(const PlatformHandle* platform_handles, | |
| 38 size_t count, | |
| 39 uint64_t* tokens) override; | |
| 40 void TokenToHandle(const uint64_t* tokens, | |
| 41 size_t count, | |
| 42 PlatformHandle* handles) override; | |
| 43 #endif | |
| 44 void ConnectMessagePipe(uint64_t pipe_id, | |
| 45 MessagePipeDispatcher* message_pipe) override; | |
| 46 void CloseMessagePipe(uint64_t pipe_id, | |
| 47 MessagePipeDispatcher* message_pipe) override; | |
| 48 | |
| 49 // Called by ChildBrokerHost on construction and destruction. | |
| 50 void ChildBrokerHostCreated(ChildBrokerHost* child_broker_host); | |
| 51 void ChildBrokerHostDestructed(ChildBrokerHost* child_broker_host); | |
| 52 | |
| 53 // These are called by ChildBrokerHost as they dispatch IPCs from ChildBroker. | |
| 54 // They are called on the IO thread. | |
| 55 void HandleConnectMessagePipe(ChildBrokerHost* pipe_process, | |
| 56 uint64_t pipe_id); | |
| 57 void HandleCancelConnectMessagePipe(uint64_t pipe_id); | |
| 58 | |
| 59 private: | |
| 60 friend struct base::DefaultSingletonTraits<BrokerState>; | |
| 61 | |
| 62 BrokerState(); | |
| 63 ~BrokerState() override; | |
| 64 | |
| 65 // Checks if there's a direct channel between the two processes, and if not | |
| 66 // creates one and tells them about it. | |
| 67 void EnsureProcessesConnected(base::ProcessId pid1, base::ProcessId pid2); | |
| 68 | |
| 69 // Callback when a RoutedRawChannel is destroyed for cleanup. | |
| 70 // Called on the IO thread. | |
| 71 void ChannelDestructed(RoutedRawChannel* channel); | |
| 72 | |
| 73 // Helper method to connect the given MessagePipe to the channel. | |
| 74 void AttachMessagePipe(MessagePipeDispatcher* message_pipe, | |
| 75 uint64_t pipe_id, | |
| 76 RoutedRawChannel* raw_channel); | |
| 77 | |
| 78 #if defined(OS_WIN) | |
| 79 // Used in the parent (unsandboxed) process to hold a mapping between HANDLES | |
| 80 // and tokens. When a child process wants to send a HANDLE to another process, | |
| 81 // it exchanges it to a token and then the other process exchanges that token | |
| 82 // back to a HANDLE. | |
| 83 base::Lock token_map_lock_; | |
| 84 base::hash_map<uint64_t, HANDLE> token_map_; | |
| 85 #endif | |
| 86 | |
| 87 // For pending connects originiating in this process. | |
| 88 // Only accessed on the IO thread. | |
| 89 base::hash_map<uint64_t, MessagePipeDispatcher*> pending_connects_; | |
| 90 | |
| 91 // For connected message pipes in this process. This is needed so that when a | |
| 92 // MessagePipeDispatcher is closed we can remove the route for the | |
| 93 // corresponding RoutedRawChannel. | |
| 94 // Only accessed on the IO thread. | |
| 95 base::hash_map<MessagePipeDispatcher*, RoutedRawChannel*> connected_pipes_; | |
| 96 | |
| 97 base::Lock lock_; // Guards access to below. | |
| 98 | |
| 99 base::hash_map<uint64_t, ChildBrokerHost*> pending_child_connects_; | |
| 100 | |
| 101 // Each entry is an std::pair of ints of processe IDs that have | |
| 102 // RoutedRawChannel objects between them. The pair always has the smaller | |
| 103 // process id value first. | |
| 104 // For now, we don't reap connections if there are no more routes between two | |
| 105 // processes. | |
| 106 base::hash_set<std::pair<base::ProcessId, base::ProcessId>> | |
| 107 connected_processes_; | |
| 108 | |
| 109 base::hash_map<base::ProcessId, ChildBrokerHost*> child_processes_; | |
| 110 | |
| 111 // Used for message pipes in the same process. | |
| 112 RoutedRawChannel* in_process_pipes_channel1_; | |
| 113 RoutedRawChannel* in_process_pipes_channel2_; | |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(BrokerState); | |
| 116 }; | |
| 117 | |
| 118 } // namespace edk | |
| 119 } // namespace mojo | |
| 120 | |
| 121 #endif // MOJO_EDK_SYSTEM_BROKER_STATE_H_ | |
| OLD | NEW |