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); | |
joedow
2017/05/30 16:24:18
You should avoid adding ASSERT statements in class
Hzj_jie
2017/05/31 00:11:53
But this class itself is for testing purpose only.
| |
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 ASSERT_TRUE(pipe_opened_); | |
36 if (asynchronous_) { | |
37 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, done); | |
38 return; | |
39 } | |
40 done.Run(); | |
41 } | |
42 | |
43 void FakeMessagePipe::Receive(std::unique_ptr<CompoundBuffer> message) { | |
44 ASSERT_TRUE(pipe_opened_); | |
45 ASSERT_TRUE(event_handler_ != nullptr); | |
46 if (asynchronous_) { | |
joedow
2017/05/30 16:24:18
Can you move the duplicated logic (sync vs. async)
Hzj_jie
2017/05/31 00:11:53
Done.
| |
47 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::Bind( | |
48 [](FakeMessagePipe* me, | |
49 std::unique_ptr<CompoundBuffer> message) { | |
50 ASSERT_TRUE(me->pipe_opened_); | |
51 ASSERT_TRUE(me->event_handler_ != nullptr); | |
52 if (message) { | |
53 message->Lock(); | |
54 } | |
55 me->event_handler_->OnMessageReceived(std::move(message)); | |
56 }, | |
57 base::Unretained(this), | |
58 base::Passed(std::move(message)))); | |
59 return; | |
60 } | |
61 | |
62 if (message) { | |
63 message->Lock(); | |
64 } | |
65 event_handler_->OnMessageReceived(std::move(message)); | |
66 } | |
67 | |
68 void FakeMessagePipe::OpenPipe() { | |
69 ASSERT_FALSE(pipe_opened_); | |
70 ASSERT_TRUE(event_handler_ != nullptr); | |
71 if (asynchronous_) { | |
72 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::Bind( | |
73 [](FakeMessagePipe* me) { | |
74 ASSERT_FALSE(me->pipe_opened_); | |
75 ASSERT_TRUE(me->event_handler_ != nullptr); | |
76 me->pipe_opened_ = true; | |
77 me->event_handler_->OnMessagePipeOpen(); | |
78 }, | |
79 base::Unretained(this))); | |
80 return; | |
81 } | |
82 | |
83 pipe_opened_ = true; | |
84 event_handler_->OnMessagePipeOpen(); | |
85 } | |
86 | |
87 void FakeMessagePipe::ClosePipe() { | |
88 ASSERT_TRUE(pipe_opened_); | |
89 ASSERT_TRUE(event_handler_ != nullptr); | |
90 if (asynchronous_) { | |
91 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::Bind( | |
92 [](FakeMessagePipe* me) { | |
93 ASSERT_TRUE(me->pipe_opened_); | |
94 ASSERT_TRUE(me->event_handler_ != nullptr); | |
95 me->pipe_opened_ = false; | |
96 me->event_handler_->OnMessagePipeClosed(); | |
97 }, | |
98 base::Unretained(this))); | |
99 return; | |
100 } | |
101 | |
102 pipe_opened_ = false; | |
103 event_handler_->OnMessagePipeClosed(); | |
104 } | |
105 | |
106 } // namespace protocol | |
107 } // namespace remoting | |
OLD | NEW |