OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/client/session/blimp_client_session.h" | 5 #include "blimp/client/session/blimp_client_session.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
| 9 #include "base/bind.h" |
9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/location.h" |
| 12 #include "base/logging.h" |
10 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
11 #include "base/numerics/safe_conversions.h" | |
12 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
13 #include "base/strings/string_number_conversions.h" | |
14 #include "base/threading/sequenced_task_runner_handle.h" | 15 #include "base/threading/sequenced_task_runner_handle.h" |
| 16 #include "base/threading/thread.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
16 #include "blimp/client/core/blimp_client_switches.h" | 18 #include "blimp/client/core/blimp_client_switches.h" |
| 19 #include "blimp/client/core/session/client_network_components.h" |
| 20 #include "blimp/client/core/session/cross_thread_network_event_observer.h" |
17 #include "blimp/client/feature/ime_feature.h" | 21 #include "blimp/client/feature/ime_feature.h" |
18 #include "blimp/client/feature/navigation_feature.h" | 22 #include "blimp/client/feature/navigation_feature.h" |
19 #include "blimp/client/feature/render_widget_feature.h" | 23 #include "blimp/client/feature/render_widget_feature.h" |
20 #include "blimp/client/feature/settings_feature.h" | 24 #include "blimp/client/feature/settings_feature.h" |
21 #include "blimp/client/feature/tab_control_feature.h" | 25 #include "blimp/client/feature/tab_control_feature.h" |
22 #include "blimp/common/blob_cache/in_memory_blob_cache.h" | 26 #include "blimp/common/blob_cache/in_memory_blob_cache.h" |
23 #include "blimp/net/blimp_connection.h" | |
24 #include "blimp/net/blimp_message_processor.h" | |
25 #include "blimp/net/blimp_message_thread_pipe.h" | 27 #include "blimp/net/blimp_message_thread_pipe.h" |
26 #include "blimp/net/blob_channel/blob_channel_receiver.h" | 28 #include "blimp/net/blob_channel/blob_channel_receiver.h" |
27 #include "blimp/net/blob_channel/helium_blob_receiver_delegate.h" | 29 #include "blimp/net/blob_channel/helium_blob_receiver_delegate.h" |
28 #include "blimp/net/browser_connection_handler.h" | |
29 #include "blimp/net/client_connection_manager.h" | |
30 #include "blimp/net/common.h" | |
31 #include "blimp/net/connection_handler.h" | |
32 #include "blimp/net/null_blimp_message_processor.h" | |
33 #include "blimp/net/ssl_client_transport.h" | |
34 #include "blimp/net/tcp_client_transport.h" | |
35 #include "blimp/net/thread_pipe_manager.h" | 30 #include "blimp/net/thread_pipe_manager.h" |
36 #include "net/base/address_list.h" | |
37 #include "net/base/ip_address.h" | |
38 #include "net/base/ip_endpoint.h" | |
39 #include "url/gurl.h" | 31 #include "url/gurl.h" |
40 | 32 |
41 namespace blimp { | 33 namespace blimp { |
42 namespace client { | 34 namespace client { |
43 namespace { | |
44 | |
45 // Posts network events to an observer across the IO/UI thread boundary. | |
46 class CrossThreadNetworkEventObserver : public NetworkEventObserver { | |
47 public: | |
48 CrossThreadNetworkEventObserver( | |
49 const base::WeakPtr<NetworkEventObserver>& target, | |
50 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
51 : target_(target), task_runner_(task_runner) {} | |
52 | |
53 ~CrossThreadNetworkEventObserver() override {} | |
54 | |
55 void OnConnected() override { | |
56 task_runner_->PostTask( | |
57 FROM_HERE, base::Bind(&NetworkEventObserver::OnConnected, target_)); | |
58 } | |
59 | |
60 void OnDisconnected(int result) override { | |
61 task_runner_->PostTask( | |
62 FROM_HERE, | |
63 base::Bind(&NetworkEventObserver::OnDisconnected, target_, result)); | |
64 } | |
65 | |
66 private: | |
67 base::WeakPtr<NetworkEventObserver> target_; | |
68 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(CrossThreadNetworkEventObserver); | |
71 }; | |
72 | |
73 } // namespace | |
74 | |
75 // This class's functions and destruction are all invoked on the IO thread by | |
76 // the BlimpClientSession. | |
77 class ClientNetworkComponents : public ConnectionHandler, | |
78 public ConnectionErrorObserver { | |
79 public: | |
80 // Can be created on any thread. | |
81 ClientNetworkComponents( | |
82 std::unique_ptr<NetworkEventObserver> observer, | |
83 std::unique_ptr<BlimpConnectionStatistics> blimp_connection_statistics); | |
84 ~ClientNetworkComponents() override; | |
85 | |
86 // Sets up network components. | |
87 void Initialize(); | |
88 | |
89 // Starts the connection to the engine using the given |assignment|. | |
90 // It is required to first call Initialize. | |
91 void ConnectWithAssignment(const Assignment& assignment); | |
92 | |
93 BrowserConnectionHandler* GetBrowserConnectionHandler(); | |
94 | |
95 void DropCurrentConnection(); | |
96 | |
97 private: | |
98 // ConnectionHandler implementation. | |
99 void HandleConnection(std::unique_ptr<BlimpConnection> connection) override; | |
100 | |
101 // ConnectionErrorObserver implementation. | |
102 void OnConnectionError(int error) override; | |
103 | |
104 std::unique_ptr<BrowserConnectionHandler> connection_handler_; | |
105 std::unique_ptr<ClientConnectionManager> connection_manager_; | |
106 std::unique_ptr<NetworkEventObserver> network_observer_; | |
107 std::unique_ptr<BlimpConnectionStatistics> connection_statistics_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(ClientNetworkComponents); | |
110 }; | |
111 | |
112 ClientNetworkComponents::ClientNetworkComponents( | |
113 std::unique_ptr<NetworkEventObserver> network_observer, | |
114 std::unique_ptr<BlimpConnectionStatistics> statistics) | |
115 : connection_handler_(new BrowserConnectionHandler), | |
116 network_observer_(std::move(network_observer)), | |
117 connection_statistics_(std::move(statistics)) { | |
118 DCHECK(connection_statistics_); | |
119 } | |
120 | |
121 ClientNetworkComponents::~ClientNetworkComponents() {} | |
122 | |
123 void ClientNetworkComponents::Initialize() { | |
124 DCHECK(!connection_manager_); | |
125 connection_manager_ = base::WrapUnique(new ClientConnectionManager(this)); | |
126 } | |
127 | |
128 void ClientNetworkComponents::ConnectWithAssignment( | |
129 const Assignment& assignment) { | |
130 DCHECK(connection_manager_); | |
131 | |
132 connection_manager_->set_client_token(assignment.client_token); | |
133 const char* transport_type = "UNKNOWN"; | |
134 switch (assignment.transport_protocol) { | |
135 case Assignment::SSL: | |
136 DCHECK(assignment.cert); | |
137 connection_manager_->AddTransport(base::WrapUnique(new SSLClientTransport( | |
138 assignment.engine_endpoint, std::move(assignment.cert), | |
139 connection_statistics_.get(), nullptr))); | |
140 transport_type = "SSL"; | |
141 break; | |
142 case Assignment::TCP: | |
143 connection_manager_->AddTransport(base::WrapUnique(new TCPClientTransport( | |
144 assignment.engine_endpoint, connection_statistics_.get(), nullptr))); | |
145 transport_type = "TCP"; | |
146 break; | |
147 case Assignment::UNKNOWN: | |
148 LOG(FATAL) << "Unknown transport type."; | |
149 break; | |
150 } | |
151 | |
152 VLOG(1) << "Connecting to " << assignment.engine_endpoint.ToString() << " (" | |
153 << transport_type << ")"; | |
154 | |
155 connection_manager_->Connect(); | |
156 } | |
157 | |
158 BrowserConnectionHandler* | |
159 ClientNetworkComponents::GetBrowserConnectionHandler() { | |
160 return connection_handler_.get(); | |
161 } | |
162 | |
163 void ClientNetworkComponents::DropCurrentConnection() { | |
164 connection_handler_->DropCurrentConnection(); | |
165 } | |
166 | |
167 void ClientNetworkComponents::HandleConnection( | |
168 std::unique_ptr<BlimpConnection> connection) { | |
169 VLOG(1) << "Connection established."; | |
170 connection->AddConnectionErrorObserver(this); | |
171 network_observer_->OnConnected(); | |
172 connection_handler_->HandleConnection(std::move(connection)); | |
173 } | |
174 | |
175 void ClientNetworkComponents::OnConnectionError(int result) { | |
176 if (result >= 0) { | |
177 VLOG(1) << "Disconnected with reason: " << result; | |
178 } else { | |
179 VLOG(1) << "Connection error: " << net::ErrorToString(result); | |
180 } | |
181 network_observer_->OnDisconnected(result); | |
182 } | |
183 | 35 |
184 BlimpClientSession::BlimpClientSession(const GURL& assigner_endpoint) | 36 BlimpClientSession::BlimpClientSession(const GURL& assigner_endpoint) |
185 : io_thread_("BlimpIOThread"), | 37 : io_thread_("BlimpIOThread"), |
186 tab_control_feature_(new TabControlFeature), | 38 tab_control_feature_(new TabControlFeature), |
187 navigation_feature_(new NavigationFeature), | 39 navigation_feature_(new NavigationFeature), |
188 ime_feature_(new ImeFeature), | 40 ime_feature_(new ImeFeature), |
189 render_widget_feature_(new RenderWidgetFeature), | 41 render_widget_feature_(new RenderWidgetFeature), |
190 settings_feature_(new SettingsFeature), | 42 settings_feature_(new SettingsFeature), |
191 weak_factory_(this) { | 43 weak_factory_(this) { |
192 base::Thread::Options options; | 44 base::Thread::Options options; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 switches::kDownloadWholeDocument)) | 140 switches::kDownloadWholeDocument)) |
289 settings_feature_->SetRecordWholeDocument(true); | 141 settings_feature_->SetRecordWholeDocument(true); |
290 } | 142 } |
291 | 143 |
292 void BlimpClientSession::OnConnected() {} | 144 void BlimpClientSession::OnConnected() {} |
293 | 145 |
294 void BlimpClientSession::OnDisconnected(int result) {} | 146 void BlimpClientSession::OnDisconnected(int result) {} |
295 | 147 |
296 void BlimpClientSession::OnImageDecodeError() { | 148 void BlimpClientSession::OnImageDecodeError() { |
297 io_thread_.task_runner()->PostTask( | 149 io_thread_.task_runner()->PostTask( |
298 FROM_HERE, base::Bind(&ClientNetworkComponents::DropCurrentConnection, | 150 FROM_HERE, |
299 base::Unretained(net_components_.get()))); | 151 base::Bind( |
| 152 &BrowserConnectionHandler::DropCurrentConnection, |
| 153 base::Unretained(net_components_->GetBrowserConnectionHandler()))); |
300 } | 154 } |
301 | 155 |
302 TabControlFeature* BlimpClientSession::GetTabControlFeature() const { | 156 TabControlFeature* BlimpClientSession::GetTabControlFeature() const { |
303 return tab_control_feature_.get(); | 157 return tab_control_feature_.get(); |
304 } | 158 } |
305 | 159 |
306 NavigationFeature* BlimpClientSession::GetNavigationFeature() const { | 160 NavigationFeature* BlimpClientSession::GetNavigationFeature() const { |
307 return navigation_feature_.get(); | 161 return navigation_feature_.get(); |
308 } | 162 } |
309 | 163 |
310 ImeFeature* BlimpClientSession::GetImeFeature() const { | 164 ImeFeature* BlimpClientSession::GetImeFeature() const { |
311 return ime_feature_.get(); | 165 return ime_feature_.get(); |
312 } | 166 } |
313 | 167 |
314 RenderWidgetFeature* BlimpClientSession::GetRenderWidgetFeature() const { | 168 RenderWidgetFeature* BlimpClientSession::GetRenderWidgetFeature() const { |
315 return render_widget_feature_.get(); | 169 return render_widget_feature_.get(); |
316 } | 170 } |
317 | 171 |
318 SettingsFeature* BlimpClientSession::GetSettingsFeature() const { | 172 SettingsFeature* BlimpClientSession::GetSettingsFeature() const { |
319 return settings_feature_.get(); | 173 return settings_feature_.get(); |
320 } | 174 } |
321 | 175 |
322 BlimpConnectionStatistics* BlimpClientSession::GetBlimpConnectionStatistics() | 176 BlimpConnectionStatistics* BlimpClientSession::GetBlimpConnectionStatistics() |
323 const { | 177 const { |
324 return blimp_connection_statistics_; | 178 return blimp_connection_statistics_; |
325 } | 179 } |
326 | 180 |
327 } // namespace client | 181 } // namespace client |
328 } // namespace blimp | 182 } // namespace blimp |
OLD | NEW |