OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/devtools/device/android_device_manager.h" | 5 #include "chrome/browser/devtools/device/android_device_manager.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 static const char kModelOffline[] = "Offline"; | 22 static const char kModelOffline[] = "Offline"; |
23 | 23 |
24 static const char kHttpGetRequest[] = "GET %s HTTP/1.1\r\n\r\n"; | 24 static const char kHttpGetRequest[] = "GET %s HTTP/1.1\r\n\r\n"; |
25 | 25 |
26 static const char kWebSocketUpgradeRequest[] = "GET %s HTTP/1.1\r\n" | 26 static const char kWebSocketUpgradeRequest[] = "GET %s HTTP/1.1\r\n" |
27 "Upgrade: WebSocket\r\n" | 27 "Upgrade: WebSocket\r\n" |
28 "Connection: Upgrade\r\n" | 28 "Connection: Upgrade\r\n" |
29 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" | 29 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
30 "Sec-WebSocket-Version: 13\r\n" | 30 "Sec-WebSocket-Version: 13\r\n" |
| 31 "%s" |
31 "\r\n"; | 32 "\r\n"; |
32 | 33 |
33 static void PostDeviceInfoCallback( | 34 static void PostDeviceInfoCallback( |
34 scoped_refptr<base::MessageLoopProxy> response_message_loop, | 35 scoped_refptr<base::MessageLoopProxy> response_message_loop, |
35 const AndroidDeviceManager::DeviceInfoCallback& callback, | 36 const AndroidDeviceManager::DeviceInfoCallback& callback, |
36 const AndroidDeviceManager::DeviceInfo& device_info) { | 37 const AndroidDeviceManager::DeviceInfo& device_info) { |
37 response_message_loop->PostTask(FROM_HERE, base::Bind(callback, device_info)); | 38 response_message_loop->PostTask(FROM_HERE, base::Bind(callback, device_info)); |
38 } | 39 } |
39 | 40 |
40 static void PostCommandCallback( | 41 static void PostCommandCallback( |
41 scoped_refptr<base::MessageLoopProxy> response_message_loop, | 42 scoped_refptr<base::MessageLoopProxy> response_message_loop, |
42 const AndroidDeviceManager::CommandCallback& callback, | 43 const AndroidDeviceManager::CommandCallback& callback, |
43 int result, | 44 int result, |
44 const std::string& response) { | 45 const std::string& response) { |
45 response_message_loop->PostTask(FROM_HERE, | 46 response_message_loop->PostTask(FROM_HERE, |
46 base::Bind(callback, result, response)); | 47 base::Bind(callback, result, response)); |
47 } | 48 } |
48 | 49 |
49 static void PostSocketCallback( | 50 static void PostHttpUpgradeCallback( |
50 scoped_refptr<base::MessageLoopProxy> response_message_loop, | 51 scoped_refptr<base::MessageLoopProxy> response_message_loop, |
51 const AndroidDeviceManager::SocketCallback& callback, | 52 const AndroidDeviceManager::HttpUpgradeCallback& callback, |
52 int result, | 53 int result, |
| 54 const std::string& extensions, |
53 scoped_ptr<net::StreamSocket> socket) { | 55 scoped_ptr<net::StreamSocket> socket) { |
54 response_message_loop->PostTask( | 56 response_message_loop->PostTask( |
55 FROM_HERE, base::Bind(callback, result, base::Passed(&socket))); | 57 FROM_HERE, |
| 58 base::Bind(callback, result, extensions, base::Passed(&socket))); |
56 } | 59 } |
57 | 60 |
58 class HttpRequest { | 61 class HttpRequest { |
59 public: | 62 public: |
60 typedef AndroidDeviceManager::CommandCallback CommandCallback; | 63 typedef AndroidDeviceManager::CommandCallback CommandCallback; |
61 typedef AndroidDeviceManager::SocketCallback SocketCallback; | 64 typedef AndroidDeviceManager::HttpUpgradeCallback HttpUpgradeCallback; |
62 | 65 |
63 static void CommandRequest(const std::string& request, | 66 static void CommandRequest(const std::string& request, |
64 const CommandCallback& callback, | 67 const CommandCallback& callback, |
65 int result, | 68 int result, |
66 scoped_ptr<net::StreamSocket> socket) { | 69 scoped_ptr<net::StreamSocket> socket) { |
67 if (result != net::OK) { | 70 if (result != net::OK) { |
68 callback.Run(result, std::string()); | 71 callback.Run(result, std::string()); |
69 return; | 72 return; |
70 } | 73 } |
71 new HttpRequest(socket.Pass(), request, callback); | 74 new HttpRequest(socket.Pass(), request, callback); |
72 } | 75 } |
73 | 76 |
74 static void SocketRequest(const std::string& request, | 77 static void HttpUpgradeRequest(const std::string& request, |
75 const SocketCallback& callback, | 78 const HttpUpgradeCallback& callback, |
76 int result, | 79 int result, |
77 scoped_ptr<net::StreamSocket> socket) { | 80 scoped_ptr<net::StreamSocket> socket) { |
78 if (result != net::OK) { | 81 if (result != net::OK) { |
79 callback.Run(result, make_scoped_ptr<net::StreamSocket>(NULL)); | 82 callback.Run(result, "", make_scoped_ptr<net::StreamSocket>(nullptr)); |
80 return; | 83 return; |
81 } | 84 } |
82 new HttpRequest(socket.Pass(), request, callback); | 85 new HttpRequest(socket.Pass(), request, callback); |
83 } | 86 } |
84 | 87 |
85 private: | 88 private: |
86 HttpRequest(scoped_ptr<net::StreamSocket> socket, | 89 HttpRequest(scoped_ptr<net::StreamSocket> socket, |
87 const std::string& request, | 90 const std::string& request, |
88 const CommandCallback& callback) | 91 const CommandCallback& callback) |
89 : socket_(socket.Pass()), | 92 : socket_(socket.Pass()), |
90 command_callback_(callback), | 93 command_callback_(callback), |
91 body_pos_(0) { | 94 body_pos_(0) { |
92 SendRequest(request); | 95 SendRequest(request); |
93 } | 96 } |
94 | 97 |
95 HttpRequest(scoped_ptr<net::StreamSocket> socket, | 98 HttpRequest(scoped_ptr<net::StreamSocket> socket, |
96 const std::string& request, | 99 const std::string& request, |
97 const SocketCallback& callback) | 100 const HttpUpgradeCallback& callback) |
98 : socket_(socket.Pass()), | 101 : socket_(socket.Pass()), |
99 socket_callback_(callback), | 102 http_upgrade_callback_(callback), |
100 body_pos_(0) { | 103 body_pos_(0) { |
101 SendRequest(request); | 104 SendRequest(request); |
102 } | 105 } |
103 | 106 |
104 ~HttpRequest() { | 107 ~HttpRequest() { |
105 } | 108 } |
106 | 109 |
107 void SendRequest(const std::string& request) { | 110 void SendRequest(const std::string& request) { |
108 scoped_refptr<net::StringIOBuffer> request_buffer = | 111 scoped_refptr<net::StringIOBuffer> request_buffer = |
109 new net::StringIOBuffer(request); | 112 new net::StringIOBuffer(request); |
(...skipping 29 matching lines...) Expand all Loading... |
139 return; | 142 return; |
140 if (result == 0) { | 143 if (result == 0) { |
141 CheckNetResultOrDie(net::ERR_CONNECTION_CLOSED); | 144 CheckNetResultOrDie(net::ERR_CONNECTION_CLOSED); |
142 return; | 145 return; |
143 } | 146 } |
144 | 147 |
145 response_ += std::string(response_buffer->data(), result); | 148 response_ += std::string(response_buffer->data(), result); |
146 int expected_length = 0; | 149 int expected_length = 0; |
147 if (bytes_total < 0) { | 150 if (bytes_total < 0) { |
148 // TODO(kaznacheev): Use net::HttpResponseHeader to parse the header. | 151 // TODO(kaznacheev): Use net::HttpResponseHeader to parse the header. |
149 size_t content_pos = response_.find("Content-Length:"); | 152 std::string content_length = ExtractHeader("Content-Length:"); |
150 if (content_pos != std::string::npos) { | 153 if (!content_length.empty()) { |
151 size_t endline_pos = response_.find("\n", content_pos); | 154 if (!base::StringToInt(content_length, &expected_length)) { |
152 if (endline_pos != std::string::npos) { | 155 CheckNetResultOrDie(net::ERR_FAILED); |
153 std::string len = response_.substr(content_pos + 15, | 156 return; |
154 endline_pos - content_pos - 15); | |
155 base::TrimWhitespace(len, base::TRIM_ALL, &len); | |
156 if (!base::StringToInt(len, &expected_length)) { | |
157 CheckNetResultOrDie(net::ERR_FAILED); | |
158 return; | |
159 } | |
160 } | 157 } |
161 } | 158 } |
162 | 159 |
163 body_pos_ = response_.find("\r\n\r\n"); | 160 body_pos_ = response_.find("\r\n\r\n"); |
164 if (body_pos_ != std::string::npos) { | 161 if (body_pos_ != std::string::npos) { |
165 body_pos_ += 4; | 162 body_pos_ += 4; |
166 bytes_total = body_pos_ + expected_length; | 163 bytes_total = body_pos_ + expected_length; |
167 } | 164 } |
168 } | 165 } |
169 | 166 |
170 if (bytes_total == static_cast<int>(response_.length())) { | 167 if (bytes_total == static_cast<int>(response_.length())) { |
171 if (!command_callback_.is_null()) | 168 if (!command_callback_.is_null()) { |
172 command_callback_.Run(net::OK, response_.substr(body_pos_)); | 169 command_callback_.Run(net::OK, response_.substr(body_pos_)); |
173 else | 170 } else { |
174 socket_callback_.Run(net::OK, socket_.Pass()); | 171 http_upgrade_callback_.Run(net::OK, |
| 172 ExtractHeader("Sec-WebSocket-Extensions:"), socket_.Pass()); |
| 173 } |
175 delete this; | 174 delete this; |
176 return; | 175 return; |
177 } | 176 } |
178 | 177 |
179 result = socket_->Read( | 178 result = socket_->Read( |
180 response_buffer.get(), | 179 response_buffer.get(), |
181 kBufferSize, | 180 kBufferSize, |
182 base::Bind(&HttpRequest::OnResponseData, | 181 base::Bind(&HttpRequest::OnResponseData, |
183 base::Unretained(this), | 182 base::Unretained(this), |
184 response_buffer, | 183 response_buffer, |
185 bytes_total)); | 184 bytes_total)); |
186 if (result != net::ERR_IO_PENDING) | 185 if (result != net::ERR_IO_PENDING) |
187 OnResponseData(response_buffer, bytes_total, result); | 186 OnResponseData(response_buffer, bytes_total, result); |
188 } | 187 } |
189 | 188 |
| 189 std::string ExtractHeader(const std::string& header) { |
| 190 size_t start_pos = response_.find(header); |
| 191 if (start_pos == std::string::npos) |
| 192 return std::string(); |
| 193 |
| 194 size_t endline_pos = response_.find("\n", start_pos); |
| 195 if (endline_pos == std::string::npos) |
| 196 return std::string(); |
| 197 |
| 198 std::string value = response_.substr( |
| 199 start_pos + header.length(), endline_pos - start_pos - header.length()); |
| 200 base::TrimWhitespace(value, base::TRIM_ALL, &value); |
| 201 return value; |
| 202 } |
| 203 |
190 bool CheckNetResultOrDie(int result) { | 204 bool CheckNetResultOrDie(int result) { |
191 if (result >= 0) | 205 if (result >= 0) |
192 return true; | 206 return true; |
193 if (!command_callback_.is_null()) | 207 if (!command_callback_.is_null()) { |
194 command_callback_.Run(result, std::string()); | 208 command_callback_.Run(result, std::string()); |
195 else | 209 } else { |
196 socket_callback_.Run(result, make_scoped_ptr<net::StreamSocket>(NULL)); | 210 http_upgrade_callback_.Run( |
| 211 result, "", make_scoped_ptr<net::StreamSocket>(nullptr)); |
| 212 } |
197 delete this; | 213 delete this; |
198 return false; | 214 return false; |
199 } | 215 } |
200 | 216 |
201 scoped_ptr<net::StreamSocket> socket_; | 217 scoped_ptr<net::StreamSocket> socket_; |
202 std::string response_; | 218 std::string response_; |
203 AndroidDeviceManager::CommandCallback command_callback_; | 219 CommandCallback command_callback_; |
204 AndroidDeviceManager::SocketCallback socket_callback_; | 220 HttpUpgradeCallback http_upgrade_callback_; |
205 size_t body_pos_; | 221 size_t body_pos_; |
206 }; | 222 }; |
207 | 223 |
208 class DevicesRequest : public base::RefCountedThreadSafe<DevicesRequest> { | 224 class DevicesRequest : public base::RefCountedThreadSafe<DevicesRequest> { |
209 public: | 225 public: |
210 typedef AndroidDeviceManager::DeviceInfo DeviceInfo; | 226 typedef AndroidDeviceManager::DeviceInfo DeviceInfo; |
211 typedef AndroidDeviceManager::DeviceProvider DeviceProvider; | 227 typedef AndroidDeviceManager::DeviceProvider DeviceProvider; |
212 typedef AndroidDeviceManager::DeviceProviders DeviceProviders; | 228 typedef AndroidDeviceManager::DeviceProviders DeviceProviders; |
213 typedef AndroidDeviceManager::DeviceDescriptors DeviceDescriptors; | 229 typedef AndroidDeviceManager::DeviceDescriptors DeviceDescriptors; |
214 typedef base::Callback<void(scoped_ptr<DeviceDescriptors>)> | 230 typedef base::Callback<void(scoped_ptr<DeviceDescriptors>)> |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 socket_name, | 314 socket_name, |
299 base::Bind(&HttpRequest::CommandRequest, | 315 base::Bind(&HttpRequest::CommandRequest, |
300 base::StringPrintf(kHttpGetRequest, request.c_str()), | 316 base::StringPrintf(kHttpGetRequest, request.c_str()), |
301 callback)); | 317 callback)); |
302 } | 318 } |
303 | 319 |
304 void AndroidDeviceManager::DeviceProvider::HttpUpgrade( | 320 void AndroidDeviceManager::DeviceProvider::HttpUpgrade( |
305 const std::string& serial, | 321 const std::string& serial, |
306 const std::string& socket_name, | 322 const std::string& socket_name, |
307 const std::string& url, | 323 const std::string& url, |
308 const SocketCallback& callback) { | 324 const std::string& extensions, |
| 325 const HttpUpgradeCallback& callback) { |
| 326 std::string extensions_with_new_line = |
| 327 extensions.empty() ? std::string() : extensions + "\r\n"; |
309 OpenSocket( | 328 OpenSocket( |
310 serial, | 329 serial, |
311 socket_name, | 330 socket_name, |
312 base::Bind(&HttpRequest::SocketRequest, | 331 base::Bind(&HttpRequest::HttpUpgradeRequest, |
313 base::StringPrintf(kWebSocketUpgradeRequest, url.c_str()), | 332 base::StringPrintf(kWebSocketUpgradeRequest, |
| 333 url.c_str(), |
| 334 extensions_with_new_line.c_str()), |
314 callback)); | 335 callback)); |
315 } | 336 } |
316 | 337 |
317 void AndroidDeviceManager::DeviceProvider::ReleaseDevice( | 338 void AndroidDeviceManager::DeviceProvider::ReleaseDevice( |
318 const std::string& serial) { | 339 const std::string& serial) { |
319 } | 340 } |
320 | 341 |
321 AndroidDeviceManager::DeviceProvider::DeviceProvider() { | 342 AndroidDeviceManager::DeviceProvider::DeviceProvider() { |
322 } | 343 } |
323 | 344 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 base::Bind(&DeviceProvider::SendJsonRequest, | 377 base::Bind(&DeviceProvider::SendJsonRequest, |
357 provider_, | 378 provider_, |
358 serial_, | 379 serial_, |
359 socket_name, | 380 socket_name, |
360 request, | 381 request, |
361 base::Bind(&PostCommandCallback, | 382 base::Bind(&PostCommandCallback, |
362 base::MessageLoopProxy::current(), | 383 base::MessageLoopProxy::current(), |
363 callback))); | 384 callback))); |
364 } | 385 } |
365 | 386 |
366 void AndroidDeviceManager::Device::HttpUpgrade(const std::string& socket_name, | 387 void AndroidDeviceManager::Device::HttpUpgrade( |
367 const std::string& url, | 388 const std::string& socket_name, |
368 const SocketCallback& callback) { | 389 const std::string& url, |
| 390 const std::string& extensions, |
| 391 const HttpUpgradeCallback& callback) { |
369 message_loop_proxy_->PostTask( | 392 message_loop_proxy_->PostTask( |
370 FROM_HERE, | 393 FROM_HERE, |
371 base::Bind(&DeviceProvider::HttpUpgrade, | 394 base::Bind(&DeviceProvider::HttpUpgrade, |
372 provider_, | 395 provider_, |
373 serial_, | 396 serial_, |
374 socket_name, | 397 socket_name, |
375 url, | 398 url, |
376 base::Bind(&PostSocketCallback, | 399 extensions, |
| 400 base::Bind(&PostHttpUpgradeCallback, |
377 base::MessageLoopProxy::current(), | 401 base::MessageLoopProxy::current(), |
378 callback))); | 402 callback))); |
379 } | 403 } |
380 | 404 |
381 AndroidDeviceManager::Device::Device( | 405 AndroidDeviceManager::Device::Device( |
382 scoped_refptr<base::MessageLoopProxy> device_message_loop, | 406 scoped_refptr<base::MessageLoopProxy> device_message_loop, |
383 scoped_refptr<DeviceProvider> provider, | 407 scoped_refptr<DeviceProvider> provider, |
384 const std::string& serial) | 408 const std::string& serial) |
385 : message_loop_proxy_(device_message_loop), | 409 : message_loop_proxy_(device_message_loop), |
386 provider_(provider), | 410 provider_(provider), |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 it->provider, it->serial); | 523 it->provider, it->serial); |
500 } else { | 524 } else { |
501 device = found->second.get(); | 525 device = found->second.get(); |
502 } | 526 } |
503 response.push_back(device); | 527 response.push_back(device); |
504 new_devices[it->serial] = device->weak_factory_.GetWeakPtr(); | 528 new_devices[it->serial] = device->weak_factory_.GetWeakPtr(); |
505 } | 529 } |
506 devices_.swap(new_devices); | 530 devices_.swap(new_devices); |
507 callback.Run(response); | 531 callback.Run(response); |
508 } | 532 } |
OLD | NEW |