OLD | NEW |
| (Empty) |
1 // Copyright 2016 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/thread_pipe_manager.h" | |
6 | |
7 #include "base/location.h" | |
8 #include "base/sequenced_task_runner.h" | |
9 #include "base/threading/sequenced_task_runner_handle.h" | |
10 #include "blimp/net/blimp_message_processor.h" | |
11 #include "blimp/net/blimp_message_thread_pipe.h" | |
12 #include "blimp/net/browser_connection_handler.h" | |
13 | |
14 namespace blimp { | |
15 | |
16 // ConnectionThreadPipeManager manages ThreadPipeManager resources used on the | |
17 // connection thread. It is created on the caller thread, and then has pipe | |
18 // and proxy pairs passed to it on the connection thread, to register with | |
19 // the supplied |connection_handler|. It is finally deleted on the connection | |
20 // thread. | |
21 class ConnectionThreadPipeManager { | |
22 public: | |
23 explicit ConnectionThreadPipeManager( | |
24 BrowserConnectionHandler* connection_handler); | |
25 virtual ~ConnectionThreadPipeManager(); | |
26 | |
27 // Connects message pipes between the specified feature and the network layer, | |
28 // using |incoming_proxy| as the incoming message processor, and connecting | |
29 // |outgoing_pipe| to the actual message sender. | |
30 void RegisterFeature(BlimpMessage::FeatureCase feature_case, | |
31 std::unique_ptr<BlimpMessageThreadPipe> outgoing_pipe, | |
32 std::unique_ptr<BlimpMessageProcessor> incoming_proxy); | |
33 | |
34 private: | |
35 BrowserConnectionHandler* connection_handler_; | |
36 | |
37 // Container for the feature-specific MessageProcessors. | |
38 // connection-side proxy for sending messages to caller thread. | |
39 std::vector<std::unique_ptr<BlimpMessageProcessor>> incoming_proxies_; | |
40 | |
41 // Containers for the MessageProcessors used to write feature-specific | |
42 // messages to the network, and the thread-pipe endpoints through which | |
43 // they are used from the caller thread. | |
44 std::vector<std::unique_ptr<BlimpMessageProcessor>> | |
45 outgoing_message_processors_; | |
46 std::vector<std::unique_ptr<BlimpMessageThreadPipe>> outgoing_pipes_; | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(ConnectionThreadPipeManager); | |
49 }; | |
50 | |
51 ConnectionThreadPipeManager::ConnectionThreadPipeManager( | |
52 BrowserConnectionHandler* connection_handler) | |
53 : connection_handler_(connection_handler) { | |
54 DCHECK(connection_handler_); | |
55 } | |
56 | |
57 ConnectionThreadPipeManager::~ConnectionThreadPipeManager() {} | |
58 | |
59 void ConnectionThreadPipeManager::RegisterFeature( | |
60 BlimpMessage::FeatureCase feature_case, | |
61 std::unique_ptr<BlimpMessageThreadPipe> outgoing_pipe, | |
62 std::unique_ptr<BlimpMessageProcessor> incoming_proxy) { | |
63 // Registers |incoming_proxy| as the message processor for incoming | |
64 // messages with |feature_case|. Sets the returned outgoing message processor | |
65 // as the target of the |outgoing_pipe|. | |
66 std::unique_ptr<BlimpMessageProcessor> outgoing_message_processor = | |
67 connection_handler_->RegisterFeature(feature_case, incoming_proxy.get()); | |
68 outgoing_pipe->set_target_processor(outgoing_message_processor.get()); | |
69 | |
70 // This object manages the lifetimes of the pipe, proxy and target processor. | |
71 incoming_proxies_.push_back(std::move(incoming_proxy)); | |
72 outgoing_pipes_.push_back(std::move(outgoing_pipe)); | |
73 outgoing_message_processors_.push_back(std::move(outgoing_message_processor)); | |
74 } | |
75 | |
76 ThreadPipeManager::ThreadPipeManager( | |
77 const scoped_refptr<base::SequencedTaskRunner>& connection_task_runner, | |
78 BrowserConnectionHandler* connection_handler) | |
79 : connection_task_runner_(connection_task_runner), | |
80 connection_pipe_manager_( | |
81 new ConnectionThreadPipeManager(connection_handler)) {} | |
82 | |
83 ThreadPipeManager::~ThreadPipeManager() { | |
84 DCHECK(sequence_checker_.CalledOnValidSequence()); | |
85 | |
86 connection_task_runner_->DeleteSoon(FROM_HERE, | |
87 connection_pipe_manager_.release()); | |
88 } | |
89 | |
90 std::unique_ptr<BlimpMessageProcessor> ThreadPipeManager::RegisterFeature( | |
91 BlimpMessage::FeatureCase feature_case, | |
92 BlimpMessageProcessor* incoming_processor) { | |
93 DCHECK(sequence_checker_.CalledOnValidSequence()); | |
94 | |
95 // Creates an outgoing pipe and a proxy for forwarding messages | |
96 // from features on the caller thread to network components on the | |
97 // connection thread. | |
98 std::unique_ptr<BlimpMessageThreadPipe> outgoing_pipe( | |
99 new BlimpMessageThreadPipe(connection_task_runner_)); | |
100 std::unique_ptr<BlimpMessageProcessor> outgoing_proxy = | |
101 outgoing_pipe->CreateProxy(); | |
102 | |
103 // Creates an incoming pipe and a proxy for receiving messages | |
104 // from network components on the connection thread. | |
105 std::unique_ptr<BlimpMessageThreadPipe> incoming_pipe( | |
106 new BlimpMessageThreadPipe(base::SequencedTaskRunnerHandle::Get())); | |
107 incoming_pipe->set_target_processor(incoming_processor); | |
108 std::unique_ptr<BlimpMessageProcessor> incoming_proxy = | |
109 incoming_pipe->CreateProxy(); | |
110 | |
111 // Finishes registration on connection thread. | |
112 connection_task_runner_->PostTask( | |
113 FROM_HERE, | |
114 base::Bind(&ConnectionThreadPipeManager::RegisterFeature, | |
115 base::Unretained(connection_pipe_manager_.get()), feature_case, | |
116 base::Passed(std::move(outgoing_pipe)), | |
117 base::Passed(std::move(incoming_proxy)))); | |
118 | |
119 incoming_pipes_.push_back(std::move(incoming_pipe)); | |
120 return outgoing_proxy; | |
121 } | |
122 | |
123 } // namespace blimp | |
OLD | NEW |