| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/message_pipe.h" | 5 #include "mojo/edk/system/message_pipe.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 142 |
| 143 endpoints_[port]->Close(); | 143 endpoints_[port]->Close(); |
| 144 if (endpoints_[peer_port]) { | 144 if (endpoints_[peer_port]) { |
| 145 if (!endpoints_[peer_port]->OnPeerClose()) | 145 if (!endpoints_[peer_port]->OnPeerClose()) |
| 146 endpoints_[peer_port].reset(); | 146 endpoints_[peer_port].reset(); |
| 147 } | 147 } |
| 148 endpoints_[port].reset(); | 148 endpoints_[port].reset(); |
| 149 } | 149 } |
| 150 | 150 |
| 151 // TODO(vtl): Handle flags. | 151 // TODO(vtl): Handle flags. |
| 152 MojoResult MessagePipe::WriteMessage( | 152 MojoResult MessagePipe::WriteMessage(unsigned port, |
| 153 unsigned port, | 153 UserPointer<const void> bytes, |
| 154 UserPointer<const void> bytes, | 154 uint32_t num_bytes, |
| 155 uint32_t num_bytes, | 155 std::vector<HandleTransport>* transports, |
| 156 std::vector<DispatcherTransport>* transports, | 156 MojoWriteMessageFlags flags) { |
| 157 MojoWriteMessageFlags flags) { | |
| 158 DCHECK(port == 0 || port == 1); | 157 DCHECK(port == 0 || port == 1); |
| 159 | 158 |
| 160 MutexLocker locker(&mutex_); | 159 MutexLocker locker(&mutex_); |
| 161 return EnqueueMessageNoLock( | 160 return EnqueueMessageNoLock( |
| 162 GetPeerPort(port), | 161 GetPeerPort(port), |
| 163 MakeUnique<MessageInTransit>( | 162 MakeUnique<MessageInTransit>( |
| 164 MessageInTransit::Type::ENDPOINT_CLIENT, | 163 MessageInTransit::Type::ENDPOINT_CLIENT, |
| 165 MessageInTransit::Subtype::ENDPOINT_CLIENT_DATA, num_bytes, bytes), | 164 MessageInTransit::Subtype::ENDPOINT_CLIENT_DATA, num_bytes, bytes), |
| 166 transports); | 165 transports); |
| 167 } | 166 } |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 // Owned by the dispatchers. The owning dispatchers should only release us via | 323 // Owned by the dispatchers. The owning dispatchers should only release us via |
| 325 // their |Close()| method, which should inform us of being closed via our | 324 // their |Close()| method, which should inform us of being closed via our |
| 326 // |Close()|. Thus these should already be null. | 325 // |Close()|. Thus these should already be null. |
| 327 DCHECK(!endpoints_[0]); | 326 DCHECK(!endpoints_[0]); |
| 328 DCHECK(!endpoints_[1]); | 327 DCHECK(!endpoints_[1]); |
| 329 } | 328 } |
| 330 | 329 |
| 331 MojoResult MessagePipe::EnqueueMessageNoLock( | 330 MojoResult MessagePipe::EnqueueMessageNoLock( |
| 332 unsigned port, | 331 unsigned port, |
| 333 std::unique_ptr<MessageInTransit> message, | 332 std::unique_ptr<MessageInTransit> message, |
| 334 std::vector<DispatcherTransport>* transports) { | 333 std::vector<HandleTransport>* transports) { |
| 335 DCHECK(port == 0 || port == 1); | 334 DCHECK(port == 0 || port == 1); |
| 336 DCHECK(message); | 335 DCHECK(message); |
| 337 | 336 |
| 338 DCHECK_EQ(message->type(), MessageInTransit::Type::ENDPOINT_CLIENT); | 337 DCHECK_EQ(message->type(), MessageInTransit::Type::ENDPOINT_CLIENT); |
| 339 DCHECK(endpoints_[GetPeerPort(port)]); | 338 DCHECK(endpoints_[GetPeerPort(port)]); |
| 340 | 339 |
| 341 // The destination port need not be open, unlike the source port. | 340 // The destination port need not be open, unlike the source port. |
| 342 if (!endpoints_[port]) | 341 if (!endpoints_[port]) |
| 343 return MOJO_RESULT_FAILED_PRECONDITION; | 342 return MOJO_RESULT_FAILED_PRECONDITION; |
| 344 | 343 |
| 345 if (transports) { | 344 if (transports) { |
| 346 MojoResult result = AttachTransportsNoLock(port, message.get(), transports); | 345 MojoResult result = AttachTransportsNoLock(port, message.get(), transports); |
| 347 if (result != MOJO_RESULT_OK) | 346 if (result != MOJO_RESULT_OK) |
| 348 return result; | 347 return result; |
| 349 } | 348 } |
| 350 | 349 |
| 351 // The endpoint's |EnqueueMessage()| may not report failure. | 350 // The endpoint's |EnqueueMessage()| may not report failure. |
| 352 endpoints_[port]->EnqueueMessage(std::move(message)); | 351 endpoints_[port]->EnqueueMessage(std::move(message)); |
| 353 return MOJO_RESULT_OK; | 352 return MOJO_RESULT_OK; |
| 354 } | 353 } |
| 355 | 354 |
| 356 MojoResult MessagePipe::AttachTransportsNoLock( | 355 MojoResult MessagePipe::AttachTransportsNoLock( |
| 357 unsigned port, | 356 unsigned port, |
| 358 MessageInTransit* message, | 357 MessageInTransit* message, |
| 359 std::vector<DispatcherTransport>* transports) { | 358 std::vector<HandleTransport>* transports) { |
| 360 DCHECK(!message->has_dispatchers()); | 359 DCHECK(!message->has_dispatchers()); |
| 361 | 360 |
| 362 // Clone the dispatchers and attach them to the message. (This must be done as | 361 // Clone the dispatchers and attach them to the message. (This must be done as |
| 363 // a separate loop, since we want to leave the dispatchers alone on failure.) | 362 // a separate loop, since we want to leave the dispatchers alone on failure.) |
| 364 std::unique_ptr<DispatcherVector> dispatchers(new DispatcherVector()); | 363 std::unique_ptr<DispatcherVector> dispatchers(new DispatcherVector()); |
| 365 dispatchers->reserve(transports->size()); | 364 dispatchers->reserve(transports->size()); |
| 366 for (size_t i = 0; i < transports->size(); i++) { | 365 for (size_t i = 0; i < transports->size(); i++) { |
| 367 if ((*transports)[i].is_valid()) { | 366 if ((*transports)[i].is_valid()) { |
| 368 dispatchers->push_back( | 367 dispatchers->push_back( |
| 369 (*transports)[i].CreateEquivalentDispatcherAndClose(this, port)); | 368 (*transports)[i].CreateEquivalentDispatcherAndClose(this, port)); |
| 370 } else { | 369 } else { |
| 371 LOG(WARNING) << "Enqueueing null dispatcher"; | 370 LOG(WARNING) << "Enqueueing null dispatcher"; |
| 372 dispatchers->push_back(nullptr); | 371 dispatchers->push_back(nullptr); |
| 373 } | 372 } |
| 374 } | 373 } |
| 375 message->SetDispatchers(std::move(dispatchers)); | 374 message->SetDispatchers(std::move(dispatchers)); |
| 376 return MOJO_RESULT_OK; | 375 return MOJO_RESULT_OK; |
| 377 } | 376 } |
| 378 | 377 |
| 379 } // namespace system | 378 } // namespace system |
| 380 } // namespace mojo | 379 } // namespace mojo |
| OLD | NEW |