OLD | NEW |
| (Empty) |
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 #include "blimp/net/blimp_message_thread_pipe.h" | |
6 | |
7 #include "base/bind_helpers.h" | |
8 #include "base/location.h" | |
9 #include "base/logging.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/sequenced_task_runner.h" | |
12 #include "base/threading/sequenced_task_runner_handle.h" | |
13 #include "blimp/common/proto/blimp_message.pb.h" | |
14 #include "blimp/net/blimp_message_processor.h" | |
15 | |
16 namespace blimp { | |
17 | |
18 namespace { | |
19 | |
20 class BlimpMessageThreadProxy : public BlimpMessageProcessor { | |
21 public: | |
22 BlimpMessageThreadProxy( | |
23 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
24 const base::WeakPtr<BlimpMessageThreadPipe>& pipe); | |
25 ~BlimpMessageThreadProxy() override; | |
26 | |
27 // BlimpMessageProcessor implementation. | |
28 void ProcessMessage(std::unique_ptr<BlimpMessage> message, | |
29 const net::CompletionCallback& callback) override; | |
30 | |
31 private: | |
32 // Thread & pipe instance to route messages to/through. | |
33 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
34 base::WeakPtr<BlimpMessageThreadPipe> pipe_; | |
35 | |
36 // Used to correctly drop ProcessMessage callbacks if |this| is deleted. | |
37 base::WeakPtrFactory<BlimpMessageThreadProxy> weak_factory_; | |
38 | |
39 DISALLOW_COPY_AND_ASSIGN(BlimpMessageThreadProxy); | |
40 }; | |
41 | |
42 BlimpMessageThreadProxy::BlimpMessageThreadProxy( | |
43 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
44 const base::WeakPtr<BlimpMessageThreadPipe>& pipe) | |
45 : task_runner_(task_runner), pipe_(pipe), weak_factory_(this) {} | |
46 | |
47 BlimpMessageThreadProxy::~BlimpMessageThreadProxy() {} | |
48 | |
49 void DispatchProcessMessage(const base::WeakPtr<BlimpMessageThreadPipe> pipe, | |
50 std::unique_ptr<BlimpMessage> message, | |
51 const net::CompletionCallback& callback) { | |
52 // Process the message only if the pipe is still active. | |
53 if (pipe) { | |
54 pipe->target_processor()->ProcessMessage(std::move(message), callback); | |
55 } | |
56 } | |
57 | |
58 void DispatchProcessMessageCallback( | |
59 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
60 const base::WeakPtr<BlimpMessageThreadProxy> proxy, | |
61 const net::CompletionCallback& callback, | |
62 int result) { | |
63 if (!task_runner->RunsTasksOnCurrentThread()) { | |
64 // Bounce the completion to the thread from which ProcessMessage was called. | |
65 task_runner->PostTask( | |
66 FROM_HERE, base::Bind(&DispatchProcessMessageCallback, task_runner, | |
67 proxy, callback, result)); | |
68 return; | |
69 } | |
70 | |
71 // Only dispatch the completion callback if the |proxy| is still live. | |
72 if (proxy) { | |
73 callback.Run(result); | |
74 } | |
75 } | |
76 | |
77 void BlimpMessageThreadProxy::ProcessMessage( | |
78 std::unique_ptr<BlimpMessage> message, | |
79 const net::CompletionCallback& callback) { | |
80 // If |callback| is non-null then wrap it to be called on this thread, iff | |
81 // this proxy instance is still alive at the time. | |
82 net::CompletionCallback wrapped_callback; | |
83 if (!callback.is_null()) { | |
84 wrapped_callback = base::Bind(&DispatchProcessMessageCallback, | |
85 base::SequencedTaskRunnerHandle::Get(), | |
86 weak_factory_.GetWeakPtr(), callback); | |
87 } | |
88 | |
89 // Post |message| to be processed via |pipe_| on |task_runner_|. | |
90 task_runner_->PostTask(FROM_HERE, | |
91 base::Bind(&DispatchProcessMessage, pipe_, | |
92 base::Passed(&message), wrapped_callback)); | |
93 } | |
94 | |
95 } // namespace | |
96 | |
97 BlimpMessageThreadPipe::BlimpMessageThreadPipe( | |
98 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
99 : target_task_runner_(task_runner), weak_factory_(this) {} | |
100 | |
101 BlimpMessageThreadPipe::~BlimpMessageThreadPipe() { | |
102 DCHECK(target_task_runner_->RunsTasksOnCurrentThread()); | |
103 } | |
104 | |
105 std::unique_ptr<BlimpMessageProcessor> BlimpMessageThreadPipe::CreateProxy() { | |
106 return base::MakeUnique<BlimpMessageThreadProxy>(target_task_runner_, | |
107 weak_factory_.GetWeakPtr()); | |
108 } | |
109 | |
110 void BlimpMessageThreadPipe::set_target_processor( | |
111 BlimpMessageProcessor* processor) { | |
112 DCHECK(target_task_runner_->RunsTasksOnCurrentThread()); | |
113 target_processor_ = processor; | |
114 } | |
115 | |
116 BlimpMessageProcessor* BlimpMessageThreadPipe::target_processor() const { | |
117 DCHECK(target_task_runner_->RunsTasksOnCurrentThread()); | |
118 return target_processor_; | |
119 } | |
120 | |
121 } // namespace blimp | |
OLD | NEW |