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 #include "mojo/edk/system/remote_message_pipe_bootstrap.h" | 5 #include "mojo/edk/system/remote_message_pipe_bootstrap.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 // The port name of the sender's local bootstrap port. | 24 // The port name of the sender's local bootstrap port. |
25 ports::PortName port_name; | 25 ports::PortName port_name; |
26 }; | 26 }; |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 // static | 30 // static |
31 void RemoteMessagePipeBootstrap::Create( | 31 void RemoteMessagePipeBootstrap::Create( |
32 NodeController* node_controller, | 32 NodeController* node_controller, |
33 ScopedPlatformHandle platform_handle, | 33 ScopedPlatformHandle platform_handle, |
34 const ports::PortRef& port, | 34 const ports::PortRef& port) { |
35 const base::Closure& callback) { | |
36 if (node_controller->io_task_runner()->RunsTasksOnCurrentThread()) { | 35 if (node_controller->io_task_runner()->RunsTasksOnCurrentThread()) { |
37 // Owns itself. | 36 // Owns itself. |
38 new RemoteMessagePipeBootstrap(node_controller, std::move(platform_handle), | 37 new RemoteMessagePipeBootstrap( |
39 port, callback); | 38 node_controller, std::move(platform_handle), port); |
40 } else { | 39 } else { |
41 node_controller->io_task_runner()->PostTask( | 40 node_controller->io_task_runner()->PostTask( |
42 FROM_HERE, | 41 FROM_HERE, |
43 base::Bind(&RemoteMessagePipeBootstrap::Create, | 42 base::Bind(&RemoteMessagePipeBootstrap::Create, |
44 base::Unretained(node_controller), | 43 base::Unretained(node_controller), |
45 base::Passed(&platform_handle), port, callback)); | 44 base::Passed(&platform_handle), port)); |
46 } | 45 } |
47 } | 46 } |
48 | 47 |
49 RemoteMessagePipeBootstrap::~RemoteMessagePipeBootstrap() { | 48 RemoteMessagePipeBootstrap::~RemoteMessagePipeBootstrap() { |
50 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 49 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
51 base::MessageLoop::current()->RemoveDestructionObserver(this); | 50 base::MessageLoop::current()->RemoveDestructionObserver(this); |
52 if (channel_) | 51 if (channel_) |
53 channel_->ShutDown(); | 52 channel_->ShutDown(); |
54 } | 53 } |
55 | 54 |
56 RemoteMessagePipeBootstrap::RemoteMessagePipeBootstrap( | 55 RemoteMessagePipeBootstrap::RemoteMessagePipeBootstrap( |
57 NodeController* node_controller, | 56 NodeController* node_controller, |
58 ScopedPlatformHandle platform_handle, | 57 ScopedPlatformHandle platform_handle, |
59 const ports::PortRef& port, | 58 const ports::PortRef& port) |
60 const base::Closure& callback) | |
61 : node_controller_(node_controller), | 59 : node_controller_(node_controller), |
62 local_port_(port), | 60 local_port_(port), |
63 callback_(callback), | |
64 io_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 61 io_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
65 channel_(Channel::Create(this, std::move(platform_handle), | 62 channel_(Channel::Create(this, std::move(platform_handle), |
66 io_task_runner_)) { | 63 io_task_runner_)) { |
67 base::MessageLoop::current()->AddDestructionObserver(this); | 64 base::MessageLoop::current()->AddDestructionObserver(this); |
68 channel_->Start(); | 65 channel_->Start(); |
69 | 66 |
70 Channel::MessagePtr message(new Channel::Message(sizeof(BootstrapData), 0)); | 67 Channel::MessagePtr message(new Channel::Message(sizeof(BootstrapData), 0)); |
71 BootstrapData* data = static_cast<BootstrapData*>(message->mutable_payload()); | 68 BootstrapData* data = static_cast<BootstrapData*>(message->mutable_payload()); |
72 data->node_name = node_controller_->name(); | 69 data->node_name = node_controller_->name(); |
73 data->port_name = local_port_.name(); | 70 data->port_name = local_port_.name(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 return; | 109 return; |
113 } | 110 } |
114 | 111 |
115 if (payload_size != sizeof(BootstrapData)) { | 112 if (payload_size != sizeof(BootstrapData)) { |
116 DLOG(ERROR) << "Invalid bootstrap payload."; | 113 DLOG(ERROR) << "Invalid bootstrap payload."; |
117 ShutDown(); | 114 ShutDown(); |
118 return; | 115 return; |
119 } | 116 } |
120 | 117 |
121 peer_info_received_ = true; | 118 peer_info_received_ = true; |
122 node_controller_->ConnectToRemotePort( | 119 |
123 local_port_, data->node_name, data->port_name, callback_); | 120 // We need to choose one side to initiate the port merge. It doesn't matter |
| 121 // who does it as long as they don't both try. Simple solution: pick the one |
| 122 // with the "smaller" port name. |
| 123 if (local_port_.name() < data->port_name) { |
| 124 node_controller_->node()->MergePorts(local_port_, data->node_name, |
| 125 data->port_name); |
| 126 } |
124 | 127 |
125 // Send another ping to the other end to trigger shutdown. This may race with | 128 // Send another ping to the other end to trigger shutdown. This may race with |
126 // the other end sending its own ping, but it doesn't matter. Whoever wins | 129 // the other end sending its own ping, but it doesn't matter. Whoever wins |
127 // will cause the other end to tear down, and the ensuing channel error will | 130 // will cause the other end to tear down, and the ensuing channel error will |
128 // in turn clean up the remaining end. | 131 // in turn clean up the remaining end. |
129 | 132 |
130 Channel::MessagePtr message(new Channel::Message(0, 0)); | 133 Channel::MessagePtr message(new Channel::Message(0, 0)); |
131 channel_->Write(std::move(message)); | 134 channel_->Write(std::move(message)); |
132 } | 135 } |
133 | 136 |
134 void RemoteMessagePipeBootstrap::OnChannelError() { | 137 void RemoteMessagePipeBootstrap::OnChannelError() { |
135 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 138 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
136 ShutDown(); | 139 ShutDown(); |
137 } | 140 } |
138 | 141 |
139 void RemoteMessagePipeBootstrap::ShutDownNow() { | 142 void RemoteMessagePipeBootstrap::ShutDownNow() { |
140 delete this; | 143 delete this; |
141 } | 144 } |
142 | 145 |
143 } // namespace edk | 146 } // namespace edk |
144 } // namespace mojo | 147 } // namespace mojo |
OLD | NEW |