OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "blimp/net/thread_pipe_manager.h" | 5 #include "blimp/net/thread_pipe_manager.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/sequenced_task_runner.h" | 8 #include "base/sequenced_task_runner.h" |
9 #include "blimp/net/blimp_message_processor.h" | 9 #include "blimp/net/blimp_message_processor.h" |
10 #include "blimp/net/blimp_message_thread_pipe.h" | 10 #include "blimp/net/blimp_message_thread_pipe.h" |
11 #include "blimp/net/browser_connection_handler.h" | 11 #include "blimp/net/browser_connection_handler.h" |
12 | 12 |
13 namespace blimp { | 13 namespace blimp { |
14 | 14 |
15 // IoThreadPipeManager is created on the UI thread, and then used and destroyed | 15 // IoThreadPipeManager is created on the UI thread, and then used and destroyed |
16 // on the IO thread. | 16 // on the IO thread. |
17 // It works with |connection_handler| to register features on the IO thread, | 17 // It works with |connection_handler| to register features on the IO thread, |
18 // and manages IO-thread-side BlimpMessageThreadPipes. | 18 // and manages IO-thread-side BlimpMessageThreadPipes. |
19 class IoThreadPipeManager { | 19 class IoThreadPipeManager { |
20 public: | 20 public: |
21 explicit IoThreadPipeManager(BrowserConnectionHandler* connection_handler); | 21 explicit IoThreadPipeManager(BrowserConnectionHandler* connection_handler); |
22 virtual ~IoThreadPipeManager(); | 22 virtual ~IoThreadPipeManager(); |
23 | 23 |
24 // Connects message pipes between the specified feature and the network layer, | 24 // Connects message pipes between the specified feature and the network layer, |
25 // using |incoming_proxy| as the incoming message processor, and connecting | 25 // using |incoming_proxy| as the incoming message processor, and connecting |
26 // |outgoing_pipe| to the actual message sender. | 26 // |outgoing_pipe| to the actual message sender. |
27 void RegisterFeature(BlimpMessage::Type type, | 27 void RegisterFeature(BlimpMessage::FeatureCase feature_case, |
28 std::unique_ptr<BlimpMessageThreadPipe> outgoing_pipe, | 28 std::unique_ptr<BlimpMessageThreadPipe> outgoing_pipe, |
29 std::unique_ptr<BlimpMessageProcessor> incoming_proxy); | 29 std::unique_ptr<BlimpMessageProcessor> incoming_proxy); |
30 | 30 |
31 private: | 31 private: |
32 BrowserConnectionHandler* connection_handler_; | 32 BrowserConnectionHandler* connection_handler_; |
33 | 33 |
34 // Container for the feature-specific MessageProcessors. | 34 // Container for the feature-specific MessageProcessors. |
35 // IO-side proxy for sending messages to UI thread. | 35 // IO-side proxy for sending messages to UI thread. |
36 std::vector<std::unique_ptr<BlimpMessageProcessor>> incoming_proxies_; | 36 std::vector<std::unique_ptr<BlimpMessageProcessor>> incoming_proxies_; |
37 | 37 |
38 // Containers for the MessageProcessors used to write feature-specific | 38 // Containers for the MessageProcessors used to write feature-specific |
39 // messages to the network, and the thread-pipe endpoints through which | 39 // messages to the network, and the thread-pipe endpoints through which |
40 // they are used from the UI thread. | 40 // they are used from the UI thread. |
41 std::vector<std::unique_ptr<BlimpMessageProcessor>> | 41 std::vector<std::unique_ptr<BlimpMessageProcessor>> |
42 outgoing_message_processors_; | 42 outgoing_message_processors_; |
43 std::vector<std::unique_ptr<BlimpMessageThreadPipe>> outgoing_pipes_; | 43 std::vector<std::unique_ptr<BlimpMessageThreadPipe>> outgoing_pipes_; |
44 | 44 |
45 DISALLOW_COPY_AND_ASSIGN(IoThreadPipeManager); | 45 DISALLOW_COPY_AND_ASSIGN(IoThreadPipeManager); |
46 }; | 46 }; |
47 | 47 |
48 IoThreadPipeManager::IoThreadPipeManager( | 48 IoThreadPipeManager::IoThreadPipeManager( |
49 BrowserConnectionHandler* connection_handler) | 49 BrowserConnectionHandler* connection_handler) |
50 : connection_handler_(connection_handler) { | 50 : connection_handler_(connection_handler) { |
51 DCHECK(connection_handler_); | 51 DCHECK(connection_handler_); |
52 } | 52 } |
53 | 53 |
54 IoThreadPipeManager::~IoThreadPipeManager() {} | 54 IoThreadPipeManager::~IoThreadPipeManager() {} |
55 | 55 |
56 void IoThreadPipeManager::RegisterFeature( | 56 void IoThreadPipeManager::RegisterFeature( |
57 BlimpMessage::Type type, | 57 BlimpMessage::FeatureCase feature_case, |
58 std::unique_ptr<BlimpMessageThreadPipe> outgoing_pipe, | 58 std::unique_ptr<BlimpMessageThreadPipe> outgoing_pipe, |
59 std::unique_ptr<BlimpMessageProcessor> incoming_proxy) { | 59 std::unique_ptr<BlimpMessageProcessor> incoming_proxy) { |
60 // Registers |incoming_proxy| as the message processor for incoming | 60 // Registers |incoming_proxy| as the message processor for incoming |
61 // messages with |type|. Sets the returned outgoing message processor as the | 61 // messages with |feature_case|. Sets the returned outgoing message processor |
62 // target of the |outgoing_pipe|. | 62 // as the target of the |outgoing_pipe|. |
63 std::unique_ptr<BlimpMessageProcessor> outgoing_message_processor = | 63 std::unique_ptr<BlimpMessageProcessor> outgoing_message_processor = |
64 connection_handler_->RegisterFeature(type, incoming_proxy.get()); | 64 connection_handler_->RegisterFeature(feature_case, incoming_proxy.get()); |
65 outgoing_pipe->set_target_processor(outgoing_message_processor.get()); | 65 outgoing_pipe->set_target_processor(outgoing_message_processor.get()); |
66 | 66 |
67 // This object manages the lifetimes of the pipe, proxy and target processor. | 67 // This object manages the lifetimes of the pipe, proxy and target processor. |
68 incoming_proxies_.push_back(std::move(incoming_proxy)); | 68 incoming_proxies_.push_back(std::move(incoming_proxy)); |
69 outgoing_pipes_.push_back(std::move(outgoing_pipe)); | 69 outgoing_pipes_.push_back(std::move(outgoing_pipe)); |
70 outgoing_message_processors_.push_back(std::move(outgoing_message_processor)); | 70 outgoing_message_processors_.push_back(std::move(outgoing_message_processor)); |
71 } | 71 } |
72 | 72 |
73 ThreadPipeManager::ThreadPipeManager( | 73 ThreadPipeManager::ThreadPipeManager( |
74 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | 74 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, |
75 const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner, | 75 const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner, |
76 BrowserConnectionHandler* connection_handler) | 76 BrowserConnectionHandler* connection_handler) |
77 : io_task_runner_(io_task_runner), | 77 : io_task_runner_(io_task_runner), |
78 ui_task_runner_(ui_task_runner), | 78 ui_task_runner_(ui_task_runner), |
79 io_pipe_manager_(new IoThreadPipeManager(connection_handler)) {} | 79 io_pipe_manager_(new IoThreadPipeManager(connection_handler)) {} |
80 | 80 |
81 ThreadPipeManager::~ThreadPipeManager() { | 81 ThreadPipeManager::~ThreadPipeManager() { |
82 io_task_runner_->DeleteSoon(FROM_HERE, io_pipe_manager_.release()); | 82 io_task_runner_->DeleteSoon(FROM_HERE, io_pipe_manager_.release()); |
83 } | 83 } |
84 | 84 |
85 std::unique_ptr<BlimpMessageProcessor> ThreadPipeManager::RegisterFeature( | 85 std::unique_ptr<BlimpMessageProcessor> ThreadPipeManager::RegisterFeature( |
86 BlimpMessage::Type type, | 86 BlimpMessage::FeatureCase feature_case, |
87 BlimpMessageProcessor* incoming_processor) { | 87 BlimpMessageProcessor* incoming_processor) { |
88 // Creates an outgoing pipe and a proxy for forwarding messages | 88 // Creates an outgoing pipe and a proxy for forwarding messages |
89 // from features on the UI thread to network components on the IO thread. | 89 // from features on the UI thread to network components on the IO thread. |
90 std::unique_ptr<BlimpMessageThreadPipe> outgoing_pipe( | 90 std::unique_ptr<BlimpMessageThreadPipe> outgoing_pipe( |
91 new BlimpMessageThreadPipe(io_task_runner_)); | 91 new BlimpMessageThreadPipe(io_task_runner_)); |
92 std::unique_ptr<BlimpMessageProcessor> outgoing_proxy = | 92 std::unique_ptr<BlimpMessageProcessor> outgoing_proxy = |
93 outgoing_pipe->CreateProxy(); | 93 outgoing_pipe->CreateProxy(); |
94 | 94 |
95 // Creates an incoming pipe and a proxy for receiving messages | 95 // Creates an incoming pipe and a proxy for receiving messages |
96 // from network components on the IO thread. | 96 // from network components on the IO thread. |
97 std::unique_ptr<BlimpMessageThreadPipe> incoming_pipe( | 97 std::unique_ptr<BlimpMessageThreadPipe> incoming_pipe( |
98 new BlimpMessageThreadPipe(ui_task_runner_)); | 98 new BlimpMessageThreadPipe(ui_task_runner_)); |
99 incoming_pipe->set_target_processor(incoming_processor); | 99 incoming_pipe->set_target_processor(incoming_processor); |
100 std::unique_ptr<BlimpMessageProcessor> incoming_proxy = | 100 std::unique_ptr<BlimpMessageProcessor> incoming_proxy = |
101 incoming_pipe->CreateProxy(); | 101 incoming_pipe->CreateProxy(); |
102 | 102 |
103 // Finishes registration on IO thread. | 103 // Finishes registration on IO thread. |
104 io_task_runner_->PostTask( | 104 io_task_runner_->PostTask( |
105 FROM_HERE, base::Bind(&IoThreadPipeManager::RegisterFeature, | 105 FROM_HERE, |
106 base::Unretained(io_pipe_manager_.get()), type, | 106 base::Bind(&IoThreadPipeManager::RegisterFeature, |
107 base::Passed(std::move(outgoing_pipe)), | 107 base::Unretained(io_pipe_manager_.get()), feature_case, |
108 base::Passed(std::move(incoming_proxy)))); | 108 base::Passed(std::move(outgoing_pipe)), |
| 109 base::Passed(std::move(incoming_proxy)))); |
109 | 110 |
110 incoming_pipes_.push_back(std::move(incoming_pipe)); | 111 incoming_pipes_.push_back(std::move(incoming_pipe)); |
111 return outgoing_proxy; | 112 return outgoing_proxy; |
112 } | 113 } |
113 | 114 |
114 } // namespace blimp | 115 } // namespace blimp |
OLD | NEW |