OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef MOJO_EDK_SYSTEM_TEST_CHANNEL_ENDPOINT_CLIENT_H_ | 5 #ifndef MOJO_EDK_SYSTEM_TEST_CHANNEL_ENDPOINT_CLIENT_H_ |
6 #define MOJO_EDK_SYSTEM_TEST_CHANNEL_ENDPOINT_CLIENT_H_ | 6 #define MOJO_EDK_SYSTEM_TEST_CHANNEL_ENDPOINT_CLIENT_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "mojo/edk/system/channel_endpoint.h" | 10 #include "mojo/edk/system/channel_endpoint.h" |
11 #include "mojo/edk/system/channel_endpoint_client.h" | 11 #include "mojo/edk/system/channel_endpoint_client.h" |
12 #include "mojo/edk/system/message_in_transit_queue.h" | 12 #include "mojo/edk/system/message_in_transit_queue.h" |
13 #include "mojo/edk/system/mutex.h" | 13 #include "mojo/edk/system/mutex.h" |
14 #include "mojo/edk/system/ref_ptr.h" | 14 #include "mojo/edk/system/ref_ptr.h" |
15 #include "mojo/public/cpp/system/macros.h" | 15 #include "mojo/public/cpp/system/macros.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 class WaitableEvent; | 18 class WaitableEvent; |
19 } | 19 } |
20 | 20 |
21 namespace mojo { | 21 namespace mojo { |
22 namespace system { | 22 namespace system { |
23 namespace test { | 23 namespace test { |
24 | 24 |
25 class TestChannelEndpointClient final : public ChannelEndpointClient { | 25 class TestChannelEndpointClient final : public ChannelEndpointClient { |
26 public: | 26 public: |
27 TestChannelEndpointClient(); | 27 // Note: Use |MakeRefCounted<TestChannelEndpointClient>()|. |
28 | 28 |
29 // Initializes with the given port and endpoint. | 29 // Initializes with the given port and endpoint. |
30 void Init(unsigned port, RefPtr<ChannelEndpoint>&& endpoint); | 30 void Init(unsigned port, RefPtr<ChannelEndpoint>&& endpoint); |
31 | 31 |
32 // Returns true if we're detached from the |ChannelEndpoint|. | 32 // Returns true if we're detached from the |ChannelEndpoint|. |
33 bool IsDetached() const; | 33 bool IsDetached() const; |
34 | 34 |
35 // Gets the current number of messages received (but not dequeued). | 35 // Gets the current number of messages received (but not dequeued). |
36 size_t NumMessages() const; | 36 size_t NumMessages() const; |
37 | 37 |
38 // Gets/removes a message that was received (|NumMessages()| must be | 38 // Gets/removes a message that was received (|NumMessages()| must be |
39 // non-zero), in FIFO order. | 39 // non-zero), in FIFO order. |
40 std::unique_ptr<MessageInTransit> PopMessage(); | 40 std::unique_ptr<MessageInTransit> PopMessage(); |
41 | 41 |
42 // Sets an event to signal when we receive a message. (|read_event| must live | 42 // Sets an event to signal when we receive a message. (|read_event| must live |
43 // until this object is destroyed or the read event is reset to null.) | 43 // until this object is destroyed or the read event is reset to null.) |
44 void SetReadEvent(base::WaitableEvent* read_event); | 44 void SetReadEvent(base::WaitableEvent* read_event); |
45 | 45 |
46 // |ChannelEndpointClient| implementation: | 46 // |ChannelEndpointClient| implementation: |
47 bool OnReadMessage(unsigned port, MessageInTransit* message) override; | 47 bool OnReadMessage(unsigned port, MessageInTransit* message) override; |
48 void OnDetachFromChannel(unsigned port) override; | 48 void OnDetachFromChannel(unsigned port) override; |
49 | 49 |
50 private: | 50 private: |
| 51 FRIEND_MAKE_REF_COUNTED(TestChannelEndpointClient); |
| 52 |
| 53 TestChannelEndpointClient(); |
51 ~TestChannelEndpointClient() override; | 54 ~TestChannelEndpointClient() override; |
52 | 55 |
53 mutable Mutex mutex_; | 56 mutable Mutex mutex_; |
54 | 57 |
55 unsigned port_ MOJO_GUARDED_BY(mutex_); | 58 unsigned port_ MOJO_GUARDED_BY(mutex_); |
56 RefPtr<ChannelEndpoint> endpoint_ MOJO_GUARDED_BY(mutex_); | 59 RefPtr<ChannelEndpoint> endpoint_ MOJO_GUARDED_BY(mutex_); |
57 | 60 |
58 MessageInTransitQueue messages_ MOJO_GUARDED_BY(mutex_); | 61 MessageInTransitQueue messages_ MOJO_GUARDED_BY(mutex_); |
59 | 62 |
60 // Event to trigger if we read a message (may be null). | 63 // Event to trigger if we read a message (may be null). |
61 base::WaitableEvent* read_event_ MOJO_GUARDED_BY(mutex_); | 64 base::WaitableEvent* read_event_ MOJO_GUARDED_BY(mutex_); |
62 | 65 |
63 MOJO_DISALLOW_COPY_AND_ASSIGN(TestChannelEndpointClient); | 66 MOJO_DISALLOW_COPY_AND_ASSIGN(TestChannelEndpointClient); |
64 }; | 67 }; |
65 | 68 |
66 } // namespace test | 69 } // namespace test |
67 } // namespace system | 70 } // namespace system |
68 } // namespace mojo | 71 } // namespace mojo |
69 | 72 |
70 #endif // MOJO_EDK_SYSTEM_TEST_CHANNEL_ENDPOINT_CLIENT_H_ | 73 #endif // MOJO_EDK_SYSTEM_TEST_CHANNEL_ENDPOINT_CLIENT_H_ |
OLD | NEW |