OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/master_connection_manager.h" | 5 #include "mojo/edk/system/master_connection_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <unordered_map> | 8 #include <unordered_map> |
9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
13 #include "base/location.h" | 14 #include "base/location.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
16 #include "mojo/edk/embedder/master_process_delegate.h" | 17 #include "mojo/edk/embedder/master_process_delegate.h" |
17 #include "mojo/edk/embedder/platform_channel_pair.h" | 18 #include "mojo/edk/embedder/platform_channel_pair.h" |
18 #include "mojo/edk/embedder/platform_handle.h" | 19 #include "mojo/edk/embedder/platform_handle.h" |
19 #include "mojo/edk/embedder/platform_handle_vector.h" | 20 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 21 #include "mojo/edk/embedder/scoped_platform_handle.h" |
20 #include "mojo/edk/system/connection_manager_messages.h" | 22 #include "mojo/edk/system/connection_manager_messages.h" |
21 #include "mojo/edk/system/message_in_transit.h" | 23 #include "mojo/edk/system/message_in_transit.h" |
22 #include "mojo/edk/system/raw_channel.h" | 24 #include "mojo/edk/system/raw_channel.h" |
23 #include "mojo/edk/system/transport_data.h" | 25 #include "mojo/edk/system/transport_data.h" |
24 #include "mojo/edk/util/make_unique.h" | 26 #include "mojo/edk/util/make_unique.h" |
25 #include "mojo/edk/util/waitable_event.h" | 27 #include "mojo/edk/util/waitable_event.h" |
26 #include "mojo/public/cpp/system/macros.h" | 28 #include "mojo/public/cpp/system/macros.h" |
27 | 29 |
| 30 using mojo::embedder::ScopedPlatformHandle; |
28 using mojo::platform::TaskRunner; | 31 using mojo::platform::TaskRunner; |
29 using mojo::util::AutoResetWaitableEvent; | 32 using mojo::util::AutoResetWaitableEvent; |
| 33 using mojo::util::MakeUnique; |
30 using mojo::util::MutexLocker; | 34 using mojo::util::MutexLocker; |
31 using mojo::util::RefPtr; | 35 using mojo::util::RefPtr; |
32 | 36 |
33 namespace mojo { | 37 namespace mojo { |
34 namespace system { | 38 namespace system { |
35 | 39 |
36 namespace { | 40 namespace { |
37 | 41 |
38 const ProcessIdentifier kFirstSlaveProcessIdentifier = 2; | 42 const ProcessIdentifier kFirstSlaveProcessIdentifier = 2; |
39 | 43 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 ProcessIdentifier process_identifier, | 81 ProcessIdentifier process_identifier, |
78 embedder::SlaveInfo slave_info, | 82 embedder::SlaveInfo slave_info, |
79 embedder::ScopedPlatformHandle platform_handle); | 83 embedder::ScopedPlatformHandle platform_handle); |
80 ~Helper() override; | 84 ~Helper() override; |
81 | 85 |
82 void Init(); | 86 void Init(); |
83 embedder::SlaveInfo Shutdown(); | 87 embedder::SlaveInfo Shutdown(); |
84 | 88 |
85 private: | 89 private: |
86 // |RawChannel::Delegate| methods: | 90 // |RawChannel::Delegate| methods: |
87 void OnReadMessage( | 91 void OnReadMessage(const MessageInTransit::View& message_view, |
88 const MessageInTransit::View& message_view, | 92 std::unique_ptr<std::vector<ScopedPlatformHandle>> |
89 embedder::ScopedPlatformHandleVectorPtr platform_handles) override; | 93 platform_handles) override; |
90 void OnError(Error error) override; | 94 void OnError(Error error) override; |
91 | 95 |
92 // Handles an error that's fatal to this object. Note that this probably | 96 // Handles an error that's fatal to this object. Note that this probably |
93 // results in |Shutdown()| being called (in the nested context) and then this | 97 // results in |Shutdown()| being called (in the nested context) and then this |
94 // object being destroyed. | 98 // object being destroyed. |
95 void FatalError(); | 99 void FatalError(); |
96 | 100 |
97 MasterConnectionManager* const owner_; | 101 MasterConnectionManager* const owner_; |
98 const ProcessIdentifier process_identifier_; | 102 const ProcessIdentifier process_identifier_; |
99 embedder::SlaveInfo const slave_info_; | 103 embedder::SlaveInfo const slave_info_; |
(...skipping 22 matching lines...) Expand all Loading... |
122 } | 126 } |
123 | 127 |
124 embedder::SlaveInfo MasterConnectionManager::Helper::Shutdown() { | 128 embedder::SlaveInfo MasterConnectionManager::Helper::Shutdown() { |
125 raw_channel_->Shutdown(); | 129 raw_channel_->Shutdown(); |
126 raw_channel_.reset(); | 130 raw_channel_.reset(); |
127 return slave_info_; | 131 return slave_info_; |
128 } | 132 } |
129 | 133 |
130 void MasterConnectionManager::Helper::OnReadMessage( | 134 void MasterConnectionManager::Helper::OnReadMessage( |
131 const MessageInTransit::View& message_view, | 135 const MessageInTransit::View& message_view, |
132 embedder::ScopedPlatformHandleVectorPtr platform_handles) { | 136 std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles) { |
133 if (message_view.type() != MessageInTransit::Type::CONNECTION_MANAGER) { | 137 if (message_view.type() != MessageInTransit::Type::CONNECTION_MANAGER) { |
134 LOG(ERROR) << "Invalid message type " << message_view.type(); | 138 LOG(ERROR) << "Invalid message type " << message_view.type(); |
135 FatalError(); // WARNING: This destroys us. | 139 FatalError(); // WARNING: This destroys us. |
136 return; | 140 return; |
137 } | 141 } |
138 | 142 |
139 // Currently, all the messages simply have a |ConnectionIdentifier| as data. | 143 // Currently, all the messages simply have a |ConnectionIdentifier| as data. |
140 if (message_view.num_bytes() != sizeof(ConnectionIdentifier)) { | 144 if (message_view.num_bytes() != sizeof(ConnectionIdentifier)) { |
141 LOG(ERROR) << "Invalid message size " << message_view.num_bytes(); | 145 LOG(ERROR) << "Invalid message size " << message_view.num_bytes(); |
142 FatalError(); // WARNING: This destroys us. | 146 FatalError(); // WARNING: This destroys us. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 | 196 |
193 std::unique_ptr<MessageInTransit> response(new MessageInTransit( | 197 std::unique_ptr<MessageInTransit> response(new MessageInTransit( |
194 MessageInTransit::Type::CONNECTION_MANAGER_ACK, | 198 MessageInTransit::Type::CONNECTION_MANAGER_ACK, |
195 ConnectionManagerResultToMessageInTransitSubtype(result), num_bytes, | 199 ConnectionManagerResultToMessageInTransitSubtype(result), num_bytes, |
196 bytes)); | 200 bytes)); |
197 | 201 |
198 if (result == Result::SUCCESS_CONNECT_NEW_CONNECTION) { | 202 if (result == Result::SUCCESS_CONNECT_NEW_CONNECTION) { |
199 DCHECK_EQ(message_view.subtype(), | 203 DCHECK_EQ(message_view.subtype(), |
200 MessageInTransit::Subtype::CONNECTION_MANAGER_CONNECT); | 204 MessageInTransit::Subtype::CONNECTION_MANAGER_CONNECT); |
201 DCHECK(platform_handle.is_valid()); | 205 DCHECK(platform_handle.is_valid()); |
202 embedder::ScopedPlatformHandleVectorPtr platform_handles( | 206 auto platform_handles = MakeUnique<std::vector<ScopedPlatformHandle>>(); |
203 new embedder::PlatformHandleVector()); | 207 platform_handles->push_back(std::move(platform_handle)); |
204 platform_handles->push_back(platform_handle.release()); | |
205 response->SetTransportData(util::MakeUnique<TransportData>( | 208 response->SetTransportData(util::MakeUnique<TransportData>( |
206 std::move(platform_handles), | 209 std::move(platform_handles), |
207 raw_channel_->GetSerializedPlatformHandleSize())); | 210 raw_channel_->GetSerializedPlatformHandleSize())); |
208 } else { | 211 } else { |
209 DCHECK(!platform_handle.is_valid()); | 212 DCHECK(!platform_handle.is_valid()); |
210 } | 213 } |
211 | 214 |
212 if (!raw_channel_->WriteMessage(std::move(response))) { | 215 if (!raw_channel_->WriteMessage(std::move(response))) { |
213 LOG(ERROR) << "WriteMessage failed"; | 216 LOG(ERROR) << "WriteMessage failed"; |
214 FatalError(); // WARNING: This destroys us. | 217 FatalError(); // WARNING: This destroys us. |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 } else if (status == ConnectionStatus::PENDING) { | 313 } else if (status == ConnectionStatus::PENDING) { |
311 DCHECK(pending_platform_handle.is_valid()); | 314 DCHECK(pending_platform_handle.is_valid()); |
312 process_connections_[to_process_identifier] = | 315 process_connections_[to_process_identifier] = |
313 pending_platform_handle.release(); | 316 pending_platform_handle.release(); |
314 } else { | 317 } else { |
315 NOTREACHED(); | 318 NOTREACHED(); |
316 } | 319 } |
317 } | 320 } |
318 | 321 |
319 private: | 322 private: |
| 323 // TODO(vtl): Make |second| |ScopedPlatformHandle|s. |
320 std::unordered_map<ProcessIdentifier, embedder::PlatformHandle> | 324 std::unordered_map<ProcessIdentifier, embedder::PlatformHandle> |
321 process_connections_; // "Owns" any valid platform handles. | 325 process_connections_; // "Owns" any valid platform handles. |
322 | 326 |
323 MOJO_DISALLOW_COPY_AND_ASSIGN(ProcessConnections); | 327 MOJO_DISALLOW_COPY_AND_ASSIGN(ProcessConnections); |
324 }; | 328 }; |
325 | 329 |
326 // MasterConnectionManager ----------------------------------------------------- | 330 // MasterConnectionManager ----------------------------------------------------- |
327 | 331 |
328 MasterConnectionManager::MasterConnectionManager( | 332 MasterConnectionManager::MasterConnectionManager( |
329 embedder::PlatformSupport* platform_support) | 333 embedder::PlatformSupport* platform_support) |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 } | 746 } |
743 | 747 |
744 void MasterConnectionManager::AssertOnPrivateThread() const { | 748 void MasterConnectionManager::AssertOnPrivateThread() const { |
745 // This should only be called after |Init()| and before |Shutdown()|. | 749 // This should only be called after |Init()| and before |Shutdown()|. |
746 DCHECK(private_thread_.message_loop()); | 750 DCHECK(private_thread_.message_loop()); |
747 DCHECK_EQ(base::MessageLoop::current(), private_thread_.message_loop()); | 751 DCHECK_EQ(base::MessageLoop::current(), private_thread_.message_loop()); |
748 } | 752 } |
749 | 753 |
750 } // namespace system | 754 } // namespace system |
751 } // namespace mojo | 755 } // namespace mojo |
OLD | NEW |