blob: f2296ffd704642394b1ae7a10b17d3ad691342ac [file] [log] [blame]
Viet-Trung Luu5abda572015-05-27 11:14:42 -07001// 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 Luu266a79f2015-09-18 08:27:48 -07008#include <memory>
9
Viet-Trung Luu5abda572015-05-27 11:14:42 -070010#include "mojo/edk/system/channel_endpoint.h"
11#include "mojo/edk/system/channel_endpoint_client.h"
Viet-Trung Luu18744c82015-05-27 14:42:35 -070012#include "mojo/edk/system/message_in_transit_queue.h"
Viet-Trung Luud42e48a2015-11-03 14:54:55 -080013#include "mojo/edk/util/mutex.h"
Viet-Trung Luu6a1f1a82015-10-30 15:18:01 -070014#include "mojo/edk/util/ref_ptr.h"
Viet-Trung Luud42e48a2015-11-03 14:54:55 -080015#include "mojo/edk/util/thread_annotations.h"
Viet-Trung Luu7d5ced22015-06-24 15:21:39 -070016#include "mojo/public/cpp/system/macros.h"
Viet-Trung Luu5abda572015-05-27 11:14:42 -070017
Viet-Trung Luu5abda572015-05-27 11:14:42 -070018namespace mojo {
Viet-Trung Luudd010e12015-11-06 14:12:00 -080019
Viet-Trung Luu9a32daa2015-11-06 15:24:41 -080020namespace util {
Viet-Trung Luudd010e12015-11-06 14:12:00 -080021class ManualResetWaitableEvent;
Viet-Trung Luu9a32daa2015-11-06 15:24:41 -080022}
Viet-Trung Luudd010e12015-11-06 14:12:00 -080023
Viet-Trung Luu9a32daa2015-11-06 15:24:41 -080024namespace system {
Viet-Trung Luu5abda572015-05-27 11:14:42 -070025namespace test {
26
Viet-Trung Luu8da45ae2015-06-29 16:03:27 -070027class TestChannelEndpointClient final : public ChannelEndpointClient {
Viet-Trung Luu5abda572015-05-27 11:14:42 -070028 public:
Viet-Trung Luu6a1f1a82015-10-30 15:18:01 -070029 // Note: Use |util::MakeRefCounted<TestChannelEndpointClient>()|.
Viet-Trung Luu5abda572015-05-27 11:14:42 -070030
31 // Initializes with the given port and endpoint.
Viet-Trung Luu6a1f1a82015-10-30 15:18:01 -070032 void Init(unsigned port, util::RefPtr<ChannelEndpoint>&& endpoint);
Viet-Trung Luu5abda572015-05-27 11:14:42 -070033
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 Luu266a79f2015-09-18 08:27:48 -070042 std::unique_ptr<MessageInTransit> PopMessage();
Viet-Trung Luu5abda572015-05-27 11:14:42 -070043
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 Luu9a32daa2015-11-06 15:24:41 -080046 void SetReadEvent(util::ManualResetWaitableEvent* read_event);
Viet-Trung Luu5abda572015-05-27 11:14:42 -070047
48 // |ChannelEndpointClient| implementation:
49 bool OnReadMessage(unsigned port, MessageInTransit* message) override;
50 void OnDetachFromChannel(unsigned port) override;
51
52 private:
Viet-Trung Luue1116132015-10-15 12:22:40 -070053 FRIEND_MAKE_REF_COUNTED(TestChannelEndpointClient);
54
55 TestChannelEndpointClient();
Viet-Trung Luu5abda572015-05-27 11:14:42 -070056 ~TestChannelEndpointClient() override;
57
Viet-Trung Luud42e48a2015-11-03 14:54:55 -080058 mutable util::Mutex mutex_;
Viet-Trung Luu5abda572015-05-27 11:14:42 -070059
Viet-Trung Luuc7cbdd22015-07-10 15:14:52 -070060 unsigned port_ MOJO_GUARDED_BY(mutex_);
Viet-Trung Luu6a1f1a82015-10-30 15:18:01 -070061 util::RefPtr<ChannelEndpoint> endpoint_ MOJO_GUARDED_BY(mutex_);
Viet-Trung Luu5abda572015-05-27 11:14:42 -070062
Viet-Trung Luuc7cbdd22015-07-10 15:14:52 -070063 MessageInTransitQueue messages_ MOJO_GUARDED_BY(mutex_);
Viet-Trung Luu5abda572015-05-27 11:14:42 -070064
65 // Event to trigger if we read a message (may be null).
Viet-Trung Luu9a32daa2015-11-06 15:24:41 -080066 util::ManualResetWaitableEvent* read_event_ MOJO_GUARDED_BY(mutex_);
Viet-Trung Luu5abda572015-05-27 11:14:42 -070067
Viet-Trung Luu7d5ced22015-06-24 15:21:39 -070068 MOJO_DISALLOW_COPY_AND_ASSIGN(TestChannelEndpointClient);
Viet-Trung Luu5abda572015-05-27 11:14:42 -070069};
70
71} // namespace test
72} // namespace system
73} // namespace mojo
74
75#endif // MOJO_EDK_SYSTEM_TEST_CHANNEL_ENDPOINT_CLIENT_H_