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