Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef MOJO_EDK_SYSTEM_TEST_CHANNEL_ENDPOINT_CLIENT_H_ |
| 6 | #define MOJO_EDK_SYSTEM_TEST_CHANNEL_ENDPOINT_CLIENT_H_ |
| 7 | |
Viet-Trung Luu | 266a79f | 2015-09-18 08:27:48 -0700 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 10 | #include "mojo/edk/system/channel_endpoint.h" |
| 11 | #include "mojo/edk/system/channel_endpoint_client.h" |
Viet-Trung Luu | 18744c8 | 2015-05-27 14:42:35 -0700 | [diff] [blame] | 12 | #include "mojo/edk/system/message_in_transit_queue.h" |
Viet-Trung Luu | d42e48a | 2015-11-03 14:54:55 -0800 | [diff] [blame] | 13 | #include "mojo/edk/util/mutex.h" |
Viet-Trung Luu | 6a1f1a8 | 2015-10-30 15:18:01 -0700 | [diff] [blame] | 14 | #include "mojo/edk/util/ref_ptr.h" |
Viet-Trung Luu | d42e48a | 2015-11-03 14:54:55 -0800 | [diff] [blame] | 15 | #include "mojo/edk/util/thread_annotations.h" |
Viet-Trung Luu | 7d5ced2 | 2015-06-24 15:21:39 -0700 | [diff] [blame] | 16 | #include "mojo/public/cpp/system/macros.h" |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 17 | |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 18 | namespace mojo { |
Viet-Trung Luu | dd010e1 | 2015-11-06 14:12:00 -0800 | [diff] [blame] | 19 | |
Viet-Trung Luu | 9a32daa | 2015-11-06 15:24:41 -0800 | [diff] [blame] | 20 | namespace util { |
Viet-Trung Luu | dd010e1 | 2015-11-06 14:12:00 -0800 | [diff] [blame] | 21 | class ManualResetWaitableEvent; |
Viet-Trung Luu | 9a32daa | 2015-11-06 15:24:41 -0800 | [diff] [blame] | 22 | } |
Viet-Trung Luu | dd010e1 | 2015-11-06 14:12:00 -0800 | [diff] [blame] | 23 | |
Viet-Trung Luu | 9a32daa | 2015-11-06 15:24:41 -0800 | [diff] [blame] | 24 | namespace system { |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 25 | namespace test { |
| 26 | |
Viet-Trung Luu | 8da45ae | 2015-06-29 16:03:27 -0700 | [diff] [blame] | 27 | class TestChannelEndpointClient final : public ChannelEndpointClient { |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 28 | public: |
Viet-Trung Luu | 6a1f1a8 | 2015-10-30 15:18:01 -0700 | [diff] [blame] | 29 | // Note: Use |util::MakeRefCounted<TestChannelEndpointClient>()|. |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 30 | |
| 31 | // Initializes with the given port and endpoint. |
Viet-Trung Luu | 6a1f1a8 | 2015-10-30 15:18:01 -0700 | [diff] [blame] | 32 | void Init(unsigned port, util::RefPtr<ChannelEndpoint>&& endpoint); |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 33 | |
| 34 | // Returns true if we're detached from the |ChannelEndpoint|. |
| 35 | bool IsDetached() const; |
| 36 | |
| 37 | // Gets the current number of messages received (but not dequeued). |
| 38 | size_t NumMessages() const; |
| 39 | |
| 40 | // Gets/removes a message that was received (|NumMessages()| must be |
| 41 | // non-zero), in FIFO order. |
Viet-Trung Luu | 266a79f | 2015-09-18 08:27:48 -0700 | [diff] [blame] | 42 | std::unique_ptr<MessageInTransit> PopMessage(); |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 43 | |
| 44 | // Sets an event to signal when we receive a message. (|read_event| must live |
| 45 | // until this object is destroyed or the read event is reset to null.) |
Viet-Trung Luu | 9a32daa | 2015-11-06 15:24:41 -0800 | [diff] [blame] | 46 | void SetReadEvent(util::ManualResetWaitableEvent* read_event); |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 47 | |
| 48 | // |ChannelEndpointClient| implementation: |
| 49 | bool OnReadMessage(unsigned port, MessageInTransit* message) override; |
| 50 | void OnDetachFromChannel(unsigned port) override; |
| 51 | |
| 52 | private: |
Viet-Trung Luu | e111613 | 2015-10-15 12:22:40 -0700 | [diff] [blame] | 53 | FRIEND_MAKE_REF_COUNTED(TestChannelEndpointClient); |
| 54 | |
| 55 | TestChannelEndpointClient(); |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 56 | ~TestChannelEndpointClient() override; |
| 57 | |
Viet-Trung Luu | d42e48a | 2015-11-03 14:54:55 -0800 | [diff] [blame] | 58 | mutable util::Mutex mutex_; |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 59 | |
Viet-Trung Luu | c7cbdd2 | 2015-07-10 15:14:52 -0700 | [diff] [blame] | 60 | unsigned port_ MOJO_GUARDED_BY(mutex_); |
Viet-Trung Luu | 6a1f1a8 | 2015-10-30 15:18:01 -0700 | [diff] [blame] | 61 | util::RefPtr<ChannelEndpoint> endpoint_ MOJO_GUARDED_BY(mutex_); |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 62 | |
Viet-Trung Luu | c7cbdd2 | 2015-07-10 15:14:52 -0700 | [diff] [blame] | 63 | MessageInTransitQueue messages_ MOJO_GUARDED_BY(mutex_); |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 64 | |
| 65 | // Event to trigger if we read a message (may be null). |
Viet-Trung Luu | 9a32daa | 2015-11-06 15:24:41 -0800 | [diff] [blame] | 66 | util::ManualResetWaitableEvent* read_event_ MOJO_GUARDED_BY(mutex_); |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 67 | |
Viet-Trung Luu | 7d5ced2 | 2015-06-24 15:21:39 -0700 | [diff] [blame] | 68 | MOJO_DISALLOW_COPY_AND_ASSIGN(TestChannelEndpointClient); |
Viet-Trung Luu | 5abda57 | 2015-05-27 11:14:42 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace test |
| 72 | } // namespace system |
| 73 | } // namespace mojo |
| 74 | |
| 75 | #endif // MOJO_EDK_SYSTEM_TEST_CHANNEL_ENDPOINT_CLIENT_H_ |