OLD | NEW |
| (Empty) |
1 // Copyright 2017 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 "remoting/protocol/fake_message_pipe.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/memory/ptr_util.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "remoting/base/compound_buffer.h" | |
12 #include "remoting/protocol/fake_message_pipe_wrapper.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace remoting { | |
16 namespace protocol { | |
17 | |
18 FakeMessagePipe::FakeMessagePipe(bool asynchronous) | |
19 : asynchronous_(asynchronous) {} | |
20 | |
21 FakeMessagePipe::~FakeMessagePipe() = default; | |
22 | |
23 std::unique_ptr<FakeMessagePipeWrapper> FakeMessagePipe::Wrap() { | |
24 return base::MakeUnique<FakeMessagePipeWrapper>(this); | |
25 } | |
26 | |
27 void FakeMessagePipe::Start(EventHandler* event_handler) { | |
28 ASSERT_TRUE(event_handler_ == nullptr); | |
29 ASSERT_TRUE(event_handler != nullptr); | |
30 event_handler_ = event_handler; | |
31 } | |
32 | |
33 void FakeMessagePipe::Send(google::protobuf::MessageLite* message, | |
34 const base::Closure& done) { | |
35 if (asynchronous_) { | |
36 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
37 base::Bind([](FakeMessagePipe* me, | |
38 google::protobuf::MessageLite* message, | |
39 const base::Closure& done) { | |
40 me->SendImpl(message, done); | |
41 }, | |
42 base::Unretained(this), | |
43 base::Unretained(message), | |
44 done)); | |
45 return; | |
46 } | |
47 SendImpl(message, done); | |
48 } | |
49 | |
50 void FakeMessagePipe::Receive(std::unique_ptr<CompoundBuffer> message) { | |
51 if (asynchronous_) { | |
52 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
53 base::Bind([](FakeMessagePipe* me, | |
54 std::unique_ptr<CompoundBuffer> message) { | |
55 me->ReceiveImpl(std::move(message)); | |
56 }, | |
57 base::Unretained(this), | |
58 base::Passed(std::move(message)))); | |
59 return; | |
60 } | |
61 | |
62 ReceiveImpl(std::move(message)); | |
63 } | |
64 | |
65 void FakeMessagePipe::OpenPipe() { | |
66 if (asynchronous_) { | |
67 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::Bind( | |
68 [](FakeMessagePipe* me) { | |
69 me->OpenPipeImpl(); | |
70 }, | |
71 base::Unretained(this))); | |
72 return; | |
73 } | |
74 | |
75 OpenPipeImpl(); | |
76 } | |
77 | |
78 void FakeMessagePipe::ClosePipe() { | |
79 if (asynchronous_) { | |
80 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::Bind( | |
81 [](FakeMessagePipe* me) { | |
82 me->ClosePipeImpl(); | |
83 }, | |
84 base::Unretained(this))); | |
85 return; | |
86 } | |
87 | |
88 ClosePipeImpl(); | |
89 } | |
90 | |
91 void FakeMessagePipe::SendImpl( | |
92 google::protobuf::MessageLite* message, | |
93 const base::Closure& done) { | |
94 ASSERT_TRUE(pipe_opened_); | |
95 if (done) { | |
96 done.Run(); | |
97 } | |
98 } | |
99 | |
100 void FakeMessagePipe::ReceiveImpl(std::unique_ptr<CompoundBuffer> message) { | |
101 ASSERT_TRUE(pipe_opened_); | |
102 ASSERT_TRUE(event_handler_ != nullptr); | |
103 if (message) { | |
104 message->Lock(); | |
105 } | |
106 event_handler_->OnMessageReceived(std::move(message)); | |
107 } | |
108 | |
109 void FakeMessagePipe::OpenPipeImpl() { | |
110 ASSERT_FALSE(pipe_opened_); | |
111 ASSERT_TRUE(event_handler_ != nullptr); | |
112 pipe_opened_ = true; | |
113 event_handler_->OnMessagePipeOpen(); | |
114 } | |
115 | |
116 void FakeMessagePipe::ClosePipeImpl() { | |
117 ASSERT_TRUE(pipe_opened_); | |
118 ASSERT_TRUE(event_handler_ != nullptr); | |
119 pipe_opened_ = false; | |
120 event_handler_->OnMessagePipeClosed(); | |
121 } | |
122 | |
123 } // namespace protocol | |
124 } // namespace remoting | |
OLD | NEW |