Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/endpoint_relayer.h" | 5 #include "mojo/edk/system/endpoint_relayer.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "mojo/edk/system/channel_endpoint.h" | 8 #include "mojo/edk/system/channel_endpoint.h" |
| 9 #include "mojo/edk/system/message_in_transit.h" | 9 #include "mojo/edk/system/message_in_transit.h" |
| 10 | 10 |
| 11 namespace mojo { | 11 namespace mojo { |
| 12 namespace system { | 12 namespace system { |
| 13 | 13 |
| 14 EndpointRelayer::EndpointRelayer() { | 14 EndpointRelayer::EndpointRelayer() { |
| 15 } | 15 } |
| 16 | 16 |
| 17 // static | 17 // static |
| 18 unsigned EndpointRelayer::GetPeerPort(unsigned port) { | 18 unsigned EndpointRelayer::GetPeerPort(unsigned port) { |
| 19 DCHECK(port == 0 || port == 1); | 19 DCHECK(port == 0 || port == 1); |
| 20 return port ^ 1; | 20 return port ^ 1; |
| 21 } | 21 } |
| 22 | 22 |
| 23 void EndpointRelayer::Init(ChannelEndpoint* endpoint0, | 23 void EndpointRelayer::Init(ChannelEndpoint* endpoint0, |
| 24 ChannelEndpoint* endpoint1) { | 24 ChannelEndpoint* endpoint1) |
| 25 MOJO_NO_THREAD_SAFETY_ANALYSIS { | |
|
kulakowski
2015/07/10 21:53:47
I wonder if this pattern (having to make |Init| fu
| |
| 25 DCHECK(endpoint0); | 26 DCHECK(endpoint0); |
| 26 DCHECK(endpoint1); | 27 DCHECK(endpoint1); |
| 27 DCHECK(!endpoints_[0]); | 28 DCHECK(!endpoints_[0]); |
| 28 DCHECK(!endpoints_[1]); | 29 DCHECK(!endpoints_[1]); |
| 29 endpoints_[0] = endpoint0; | 30 endpoints_[0] = endpoint0; |
| 30 endpoints_[1] = endpoint1; | 31 endpoints_[1] = endpoint1; |
| 31 } | 32 } |
| 32 | 33 |
| 33 void EndpointRelayer::SetFilter(scoped_ptr<Filter> filter) { | 34 void EndpointRelayer::SetFilter(scoped_ptr<Filter> filter) { |
| 34 base::AutoLock locker(lock_); | 35 MutexLocker locker(&mutex_); |
| 35 filter_ = filter.Pass(); | 36 filter_ = filter.Pass(); |
| 36 } | 37 } |
| 37 | 38 |
| 38 bool EndpointRelayer::OnReadMessage(unsigned port, MessageInTransit* message) { | 39 bool EndpointRelayer::OnReadMessage(unsigned port, MessageInTransit* message) { |
| 39 DCHECK(message); | 40 DCHECK(message); |
| 40 | 41 |
| 41 base::AutoLock locker(lock_); | 42 MutexLocker locker(&mutex_); |
| 42 | 43 |
| 43 // If we're no longer the client, then reject the message. | 44 // If we're no longer the client, then reject the message. |
| 44 if (!endpoints_[port]) | 45 if (!endpoints_[port]) |
| 45 return false; | 46 return false; |
| 46 | 47 |
| 47 unsigned peer_port = GetPeerPort(port); | 48 unsigned peer_port = GetPeerPort(port); |
| 48 | 49 |
| 49 if (filter_ && message->type() == MessageInTransit::Type::ENDPOINT_CLIENT) { | 50 if (filter_ && message->type() == MessageInTransit::Type::ENDPOINT_CLIENT) { |
| 50 if (filter_->OnReadMessage(endpoints_[port].get(), | 51 if (filter_->OnReadMessage(endpoints_[port].get(), |
| 51 endpoints_[peer_port].get(), message)) | 52 endpoints_[peer_port].get(), message)) |
| 52 return true; | 53 return true; |
| 53 } | 54 } |
| 54 | 55 |
| 55 // Otherwise, consume it even if the peer port is closed. | 56 // Otherwise, consume it even if the peer port is closed. |
| 56 if (endpoints_[peer_port]) | 57 if (endpoints_[peer_port]) |
| 57 endpoints_[peer_port]->EnqueueMessage(make_scoped_ptr(message)); | 58 endpoints_[peer_port]->EnqueueMessage(make_scoped_ptr(message)); |
| 58 return true; | 59 return true; |
| 59 } | 60 } |
| 60 | 61 |
| 61 void EndpointRelayer::OnDetachFromChannel(unsigned port) { | 62 void EndpointRelayer::OnDetachFromChannel(unsigned port) { |
| 62 base::AutoLock locker(lock_); | 63 MutexLocker locker(&mutex_); |
| 63 | 64 |
| 64 if (endpoints_[port]) { | 65 if (endpoints_[port]) { |
| 65 endpoints_[port]->DetachFromClient(); | 66 endpoints_[port]->DetachFromClient(); |
| 66 endpoints_[port] = nullptr; | 67 endpoints_[port] = nullptr; |
| 67 } | 68 } |
| 68 | 69 |
| 69 unsigned peer_port = GetPeerPort(port); | 70 unsigned peer_port = GetPeerPort(port); |
| 70 if (endpoints_[peer_port]) { | 71 if (endpoints_[peer_port]) { |
| 71 endpoints_[peer_port]->DetachFromClient(); | 72 endpoints_[peer_port]->DetachFromClient(); |
| 72 endpoints_[peer_port] = nullptr; | 73 endpoints_[peer_port] = nullptr; |
| 73 } | 74 } |
| 74 } | 75 } |
| 75 | 76 |
| 76 EndpointRelayer::~EndpointRelayer() { | 77 EndpointRelayer::~EndpointRelayer() { |
| 77 DCHECK(!endpoints_[0]); | 78 DCHECK(!endpoints_[0]); |
| 78 DCHECK(!endpoints_[1]); | 79 DCHECK(!endpoints_[1]); |
| 79 } | 80 } |
| 80 | 81 |
| 81 } // namespace system | 82 } // namespace system |
| 82 } // namespace mojo | 83 } // namespace mojo |
| OLD | NEW |