OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_REMOTE_MESSAGE_PIPE_BOOTSTRAP_H_ | 5 #ifndef MOJO_EDK_SYSTEM_REMOTE_MESSAGE_PIPE_BOOTSTRAP_H_ |
6 #define MOJO_EDK_SYSTEM_REMOTE_MESSAGE_PIPE_BOOTSTRAP_H_ | 6 #define MOJO_EDK_SYSTEM_REMOTE_MESSAGE_PIPE_BOOTSTRAP_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
| 10 #include "base/callback.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/task_runner.h" | 14 #include "base/task_runner.h" |
14 #include "mojo/edk/embedder/scoped_platform_handle.h" | 15 #include "mojo/edk/embedder/scoped_platform_handle.h" |
15 #include "mojo/edk/system/channel.h" | 16 #include "mojo/edk/system/channel.h" |
16 #include "mojo/edk/system/ports/name.h" | 17 #include "mojo/edk/system/ports/name.h" |
17 #include "mojo/edk/system/ports/port_ref.h" | 18 #include "mojo/edk/system/ports/port_ref.h" |
18 | 19 |
19 namespace mojo { | 20 namespace mojo { |
20 namespace edk { | 21 namespace edk { |
21 | 22 |
22 class NodeController; | 23 class NodeController; |
23 | 24 |
24 // This is used by Core to negotiate a new cross-process message pipe given | 25 // This is used by Core to negotiate a new cross-process message pipe given |
25 // an arbitrary platform channel. Both ends of the channel must be passed to | 26 // an arbitrary platform channel. Both ends of the channel must be passed to |
26 // RemoteMessagePipeBootstrap::Create() in their respective processes. | 27 // RemoteMessagePipeBootstrap::Create() in their respective processes. |
27 // | 28 // |
28 // The bootstrapping procedure the same on either end: | 29 // The bootstrapping procedure the same on either end: |
29 // | 30 // |
30 // 1. Select a local port P to be merged with a remote port. | 31 // 1. Create a local port P. |
31 // 2. Write the local node name and P's name to the bootstrap pipe. | 32 // 2. Write the local node name and P's name to the bootstrap pipe. |
32 // 3. When a message is read from the pipe: | 33 // 3. When a message is read from the pipe: |
33 // - If it's the first message read, extract the remote node+port name and | 34 // - If it's the first message read, extract the remote node+port name and |
34 // and send an empty ACK message on the pipe. | 35 // initialize the local port. Send an empty ACK message on the pipe. |
35 // - If it's the second message read, close the channel, and delete |this|. | 36 // - If it's the second message read, close the channel, and delete |this|. |
36 // 4. When an error occus on the pipe, delete |this|. | 37 // 4. When an error occus on the pipe, delete |this|. |
37 // | 38 // |
38 // Excluding irrecoverable error conditions such as either process dying, | 39 // Excluding irrecoverable error conditions such as either process dying, |
39 // armageddon, etc., this ensures neither end closes the channel until both ends | 40 // armageddon, etc., this ensures neither end closes the channel until both ends |
40 // are aware of each other's port-to-merge. | 41 // have intiailized their corresponding local port. |
41 // | |
42 // At step 3, one side of the channel is chosen to issue a message at the Ports | |
43 // layer which eventually merges the two ports. | |
44 class RemoteMessagePipeBootstrap | 42 class RemoteMessagePipeBootstrap |
45 : public Channel::Delegate, | 43 : public Channel::Delegate, |
46 public base::MessageLoop::DestructionObserver { | 44 public base::MessageLoop::DestructionObserver { |
47 public: | 45 public: |
48 ~RemoteMessagePipeBootstrap() override; | 46 ~RemoteMessagePipeBootstrap() override; |
49 | 47 |
50 // |port| must be a reference to an uninitialized local port. | 48 // |port| must be a reference to an uninitialized local port. |
51 static void Create(NodeController* node_controller, | 49 static void Create(NodeController* node_controller, |
52 ScopedPlatformHandle platform_handle, | 50 ScopedPlatformHandle platform_handle, |
53 const ports::PortRef& port); | 51 const ports::PortRef& port, |
| 52 const base::Closure& callback); |
54 | 53 |
55 protected: | 54 protected: |
56 explicit RemoteMessagePipeBootstrap(NodeController* node_controller, | 55 explicit RemoteMessagePipeBootstrap( |
57 ScopedPlatformHandle platform_handle, | 56 NodeController* node_controller, |
58 const ports::PortRef& port); | 57 ScopedPlatformHandle platform_handle, |
| 58 const ports::PortRef& port, |
| 59 const base::Closure& callback); |
59 | 60 |
60 void ShutDown(); | 61 void ShutDown(); |
61 | 62 |
62 bool shutting_down_ = false; | 63 bool shutting_down_ = false; |
63 NodeController* node_controller_; | 64 NodeController* node_controller_; |
64 const ports::PortRef local_port_; | 65 const ports::PortRef local_port_; |
| 66 base::Closure callback_; |
65 | 67 |
66 scoped_refptr<base::TaskRunner> io_task_runner_; | 68 scoped_refptr<base::TaskRunner> io_task_runner_; |
67 scoped_refptr<Channel> channel_; | 69 scoped_refptr<Channel> channel_; |
68 | 70 |
69 bool peer_info_received_ = false; | 71 bool peer_info_received_ = false; |
70 | 72 |
71 private: | 73 private: |
72 // base::MessageLoop::DestructionObserver: | 74 // base::MessageLoop::DestructionObserver: |
73 void WillDestroyCurrentMessageLoop() override; | 75 void WillDestroyCurrentMessageLoop() override; |
74 | 76 |
75 // Channel::Delegate: | 77 // Channel::Delegate: |
76 void OnChannelMessage(const void* payload, | 78 void OnChannelMessage(const void* payload, |
77 size_t payload_size, | 79 size_t payload_size, |
78 ScopedPlatformHandleVectorPtr handles) override; | 80 ScopedPlatformHandleVectorPtr handles) override; |
79 void OnChannelError() override; | 81 void OnChannelError() override; |
80 | 82 |
81 void ShutDownNow(); | 83 void ShutDownNow(); |
82 | 84 |
83 DISALLOW_COPY_AND_ASSIGN(RemoteMessagePipeBootstrap); | 85 DISALLOW_COPY_AND_ASSIGN(RemoteMessagePipeBootstrap); |
84 }; | 86 }; |
85 | 87 |
86 } // namespace edk | 88 } // namespace edk |
87 } // namespace mojo | 89 } // namespace mojo |
88 | 90 |
89 #endif // MOJO_EDK_SYSTEM_REMOTE_MESSAGE_PIPE_BOOTSTRAP_H_ | 91 #endif // MOJO_EDK_SYSTEM_REMOTE_MESSAGE_PIPE_BOOTSTRAP_H_ |
OLD | NEW |