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 #ifndef THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_ENDPOINT_RELAYER_H_ | |
6 #define THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_ENDPOINT_RELAYER_H_ | |
7 | |
8 #include "base/memory/ref_counted.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "mojo/public/cpp/system/macros.h" | |
11 #include "third_party/mojo/src/mojo/edk/system/channel_endpoint_client.h" | |
12 #include "third_party/mojo/src/mojo/edk/system/mutex.h" | |
13 #include "third_party/mojo/src/mojo/edk/system/system_impl_export.h" | |
14 | |
15 namespace mojo { | |
16 namespace system { | |
17 | |
18 class ChannelEndpoint; | |
19 | |
20 // This is a simple |ChannelEndpointClient| that just relays messages between | |
21 // two |ChannelEndpoint|s (without the overhead of |MessagePipe|). | |
22 class MOJO_SYSTEM_IMPL_EXPORT EndpointRelayer final | |
23 : public ChannelEndpointClient { | |
24 public: | |
25 // A class that can inspect and optionally handle messages of type | |
26 // |Type::ENDPOINT_CLIENT| received from either |ChannelEndpoint|. | |
27 // | |
28 // Instances of implementations of this class will be owned by | |
29 // |EndpointRelayer|s. | |
30 // | |
31 // Destructors may not call methods of the |EndpointRelayer| (nor of the | |
32 // |ChannelEndpoint|s). | |
33 class MOJO_SYSTEM_IMPL_EXPORT Filter { | |
34 public: | |
35 virtual ~Filter() {} | |
36 | |
37 // Called by |EndpointRelayer::OnReadMessage()| for messages of type | |
38 // |Type::ENDPOINT_CLIENT|. This is only called by the |EndpointRelayer| if | |
39 // it is still the client of the sending endpoint. | |
40 // | |
41 // |endpoint| (which will not be null) is the |ChannelEndpoint| | |
42 // corresponding to |port| (i.e., the endpoint the message was received | |
43 // from), whereas |peer_endpoint| (which may be null) is that corresponding | |
44 // to the peer port (i.e., the endpoint to which the message would be | |
45 // relayed). | |
46 // | |
47 // This should return true if the message is consumed (in which case | |
48 // ownership is transferred), and false if not (in which case the message | |
49 // will be relayed as usual). | |
50 // | |
51 // This will always be called under |EndpointRelayer|'s lock. This may call | |
52 // |ChannelEndpoint| methods. However, it may not call any of | |
53 // |EndpointRelayer|'s methods. | |
54 virtual bool OnReadMessage(ChannelEndpoint* endpoint, | |
55 ChannelEndpoint* peer_endpoint, | |
56 MessageInTransit* message) = 0; | |
57 | |
58 protected: | |
59 Filter() {} | |
60 | |
61 private: | |
62 MOJO_DISALLOW_COPY_AND_ASSIGN(Filter); | |
63 }; | |
64 | |
65 EndpointRelayer(); | |
66 | |
67 // Gets the other port number (i.e., 0 -> 1, 1 -> 0). | |
68 static unsigned GetPeerPort(unsigned port); | |
69 | |
70 // Initialize this object. This must be called before any other method. | |
71 void Init(ChannelEndpoint* endpoint0, | |
72 ChannelEndpoint* endpoint1) MOJO_NOT_THREAD_SAFE; | |
73 | |
74 // Sets (or resets) the filter, which can (optionally) handle/filter | |
75 // |Type::ENDPOINT_CLIENT| messages (see |Filter| above). | |
76 void SetFilter(scoped_ptr<Filter> filter); | |
77 | |
78 // |ChannelEndpointClient| methods: | |
79 bool OnReadMessage(unsigned port, MessageInTransit* message) override; | |
80 void OnDetachFromChannel(unsigned port) override; | |
81 | |
82 private: | |
83 ~EndpointRelayer() override; | |
84 | |
85 Mutex mutex_; | |
86 scoped_refptr<ChannelEndpoint> endpoints_[2] MOJO_GUARDED_BY(mutex_); | |
87 scoped_ptr<Filter> filter_ MOJO_GUARDED_BY(mutex_); | |
88 | |
89 MOJO_DISALLOW_COPY_AND_ASSIGN(EndpointRelayer); | |
90 }; | |
91 | |
92 } // namespace system | |
93 } // namespace mojo | |
94 | |
95 #endif // THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_ENDPOINT_RELAYER_H_ | |
OLD | NEW |