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