| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/edk/system/endpoint_relayer.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "mojo/edk/system/channel_endpoint.h" | |
| 9 #include "mojo/edk/system/message_in_transit.h" | |
| 10 | |
| 11 namespace mojo { | |
| 12 namespace system { | |
| 13 | |
| 14 EndpointRelayer::EndpointRelayer() { | |
| 15 } | |
| 16 | |
| 17 // static | |
| 18 unsigned EndpointRelayer::GetPeerPort(unsigned port) { | |
| 19 DCHECK(port == 0 || port == 1); | |
| 20 return port ^ 1; | |
| 21 } | |
| 22 | |
| 23 void EndpointRelayer::Init(ChannelEndpoint* endpoint0, | |
| 24 ChannelEndpoint* endpoint1) { | |
| 25 DCHECK(endpoint0); | |
| 26 DCHECK(endpoint1); | |
| 27 DCHECK(!endpoints_[0]); | |
| 28 DCHECK(!endpoints_[1]); | |
| 29 endpoints_[0] = endpoint0; | |
| 30 endpoints_[1] = endpoint1; | |
| 31 } | |
| 32 | |
| 33 bool EndpointRelayer::OnReadMessage(unsigned port, MessageInTransit* message) { | |
| 34 DCHECK(message); | |
| 35 | |
| 36 base::AutoLock locker(lock_); | |
| 37 | |
| 38 // If we're no longer the client, then reject the message. | |
| 39 if (!endpoints_[port]) | |
| 40 return false; | |
| 41 | |
| 42 // Otherwise, consume it even if the peer port is closed. | |
| 43 unsigned peer_port = GetPeerPort(port); | |
| 44 if (endpoints_[peer_port]) | |
| 45 endpoints_[peer_port]->EnqueueMessage(make_scoped_ptr(message)); | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 void EndpointRelayer::OnDetachFromChannel(unsigned port) { | |
| 50 base::AutoLock locker(lock_); | |
| 51 | |
| 52 if (endpoints_[port]) { | |
| 53 endpoints_[port]->DetachFromClient(); | |
| 54 endpoints_[port] = nullptr; | |
| 55 } | |
| 56 | |
| 57 unsigned peer_port = GetPeerPort(port); | |
| 58 if (endpoints_[peer_port]) { | |
| 59 endpoints_[peer_port]->DetachFromClient(); | |
| 60 endpoints_[peer_port] = nullptr; | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 EndpointRelayer::~EndpointRelayer() { | |
| 65 DCHECK(!endpoints_[0]); | |
| 66 DCHECK(!endpoints_[1]); | |
| 67 } | |
| 68 | |
| 69 } // namespace system | |
| 70 } // namespace mojo | |
| OLD | NEW |