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_HOST_H_ | |
6 #define MOJO_EDK_SYSTEM_CHILD_BROKER_HOST_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <vector> | |
11 | |
12 #include "base/compiler_specific.h" | |
13 #include "base/macros.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/process/process.h" | |
16 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
17 #include "mojo/edk/system/routed_raw_channel.h" | |
18 #include "mojo/edk/system/system_impl_export.h" | |
19 | |
20 namespace mojo { | |
21 namespace edk { | |
22 | |
23 // Responds to requests from ChildBroker. This is used to handle message pipe | |
24 // multiplexing and Windows sandbox messages. There is one object of this class | |
25 // per child process host object. | |
26 // This object will delete itself when it notices that the pipe is broken. | |
27 class MOJO_SYSTEM_IMPL_EXPORT ChildBrokerHost | |
28 : public RawChannel::Delegate | |
29 #if defined(OS_WIN) | |
30 , NON_EXPORTED_BASE(public base::MessageLoopForIO::IOHandler) { | |
31 #else | |
32 { | |
33 #endif | |
34 public: | |
35 // |child_process| is a handle to the child process. It will be duplicated by | |
36 // this object. |pipe| is a handle to the communication pipe to the child | |
37 // process, which is generated inside mojo::edk::ChildProcessLaunched. It is | |
38 // owned by this class. | |
39 ChildBrokerHost(base::ProcessHandle child_process, ScopedPlatformHandle pipe); | |
40 | |
41 base::ProcessId GetProcessId(); | |
42 | |
43 // Sends a message to the child process to connect to |process_id| via |pipe|. | |
44 void ConnectToProcess(base::ProcessId process_id, ScopedPlatformHandle pipe); | |
45 | |
46 // Sends a message to the child process that |pipe_id|'s other end is in | |
47 // |process_id|. If the other end is in this parent process, |process_id| will | |
48 // be 0. | |
49 void ConnectMessagePipe(uint64_t pipe_id, base::ProcessId process_id); | |
50 | |
51 // Sends a message to the child process informing it that the peer process has | |
52 // died before it could connect. | |
53 void PeerDied(uint64_t pipe_id); | |
54 | |
55 RoutedRawChannel* channel() { return child_channel_; } | |
56 | |
57 private: | |
58 ~ChildBrokerHost() override; | |
59 | |
60 void InitOnIO(ScopedPlatformHandle parent_async_channel_handle); | |
61 | |
62 // RawChannel::Delegate implementation: | |
63 void OnReadMessage( | |
64 const MessageInTransit::View& message_view, | |
65 ScopedPlatformHandleVectorPtr platform_handles) override; | |
66 void OnError(Error error) override; | |
67 | |
68 // Callback for when child_channel_ is destroyed. | |
69 void ChannelDestructed(RoutedRawChannel* channel); | |
70 | |
71 #if defined(OS_WIN) | |
72 void BeginRead(); | |
73 | |
74 // base::MessageLoopForIO::IOHandler implementation: | |
75 void OnIOCompleted(base::MessageLoopForIO::IOContext* context, | |
76 DWORD bytes_transferred, | |
77 DWORD error) override; | |
78 | |
79 // Helper wrappers around DuplicateHandle. | |
80 HANDLE DuplicateToChild(HANDLE handle); | |
81 HANDLE DuplicateFromChild(HANDLE handle); | |
82 #endif | |
83 | |
84 base::ProcessId process_id_; | |
85 | |
86 // Channel used to receive and send multiplexing related messages. | |
87 RoutedRawChannel* child_channel_; | |
88 | |
89 #if defined(OS_WIN) | |
90 // Handle to the child process, used for duplication of handles. | |
91 base::Process child_process_; | |
92 | |
93 // Pipe used for synchronous messages from the child. Responses are written to | |
94 // it as well. | |
95 ScopedPlatformHandle sync_channel_; | |
96 | |
97 base::MessageLoopForIO::IOContext read_context_; | |
98 base::MessageLoopForIO::IOContext write_context_; | |
99 | |
100 std::vector<char> read_data_; | |
101 // How many bytes in read_data_ we already read. | |
102 uint32_t num_bytes_read_; | |
103 std::vector<char> write_data_; | |
104 #endif | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(ChildBrokerHost); | |
107 }; | |
108 | |
109 } // namespace edk | |
110 } // namespace mojo | |
111 | |
112 #endif // MOJO_EDK_SYSTEM_CHILD_BROKER_HOST_H_ | |
OLD | NEW |