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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 const std::string& response) { | 45 const std::string& response) { |
46 response_message_loop->PostTask(FROM_HERE, | 46 response_message_loop->PostTask(FROM_HERE, |
47 base::Bind(callback, result, response)); | 47 base::Bind(callback, result, response)); |
48 } | 48 } |
49 | 49 |
50 static void PostHttpUpgradeCallback( | 50 static void PostHttpUpgradeCallback( |
51 scoped_refptr<base::MessageLoopProxy> response_message_loop, | 51 scoped_refptr<base::MessageLoopProxy> response_message_loop, |
52 const AndroidDeviceManager::HttpUpgradeCallback& callback, | 52 const AndroidDeviceManager::HttpUpgradeCallback& callback, |
53 int result, | 53 int result, |
54 const std::string& extensions, | 54 const std::string& extensions, |
| 55 const std::string& leading_bytes, |
55 scoped_ptr<net::StreamSocket> socket) { | 56 scoped_ptr<net::StreamSocket> socket) { |
56 response_message_loop->PostTask( | 57 response_message_loop->PostTask( |
57 FROM_HERE, | 58 FROM_HERE, base::Bind(callback, result, extensions, leading_bytes, |
58 base::Bind(callback, result, extensions, base::Passed(&socket))); | 59 base::Passed(&socket))); |
59 } | 60 } |
60 | 61 |
61 class HttpRequest { | 62 class HttpRequest { |
62 public: | 63 public: |
63 typedef AndroidDeviceManager::CommandCallback CommandCallback; | 64 typedef AndroidDeviceManager::CommandCallback CommandCallback; |
64 typedef AndroidDeviceManager::HttpUpgradeCallback HttpUpgradeCallback; | 65 typedef AndroidDeviceManager::HttpUpgradeCallback HttpUpgradeCallback; |
65 | 66 |
66 static void CommandRequest(const std::string& request, | 67 static void CommandRequest(const std::string& request, |
67 const CommandCallback& callback, | 68 const CommandCallback& callback, |
68 int result, | 69 int result, |
69 scoped_ptr<net::StreamSocket> socket) { | 70 scoped_ptr<net::StreamSocket> socket) { |
70 if (result != net::OK) { | 71 if (result != net::OK) { |
71 callback.Run(result, std::string()); | 72 callback.Run(result, std::string()); |
72 return; | 73 return; |
73 } | 74 } |
74 new HttpRequest(socket.Pass(), request, callback); | 75 new HttpRequest(socket.Pass(), request, callback); |
75 } | 76 } |
76 | 77 |
77 static void HttpUpgradeRequest(const std::string& request, | 78 static void HttpUpgradeRequest(const std::string& request, |
78 const HttpUpgradeCallback& callback, | 79 const HttpUpgradeCallback& callback, |
79 int result, | 80 int result, |
80 scoped_ptr<net::StreamSocket> socket) { | 81 scoped_ptr<net::StreamSocket> socket) { |
81 if (result != net::OK) { | 82 if (result != net::OK) { |
82 callback.Run(result, "", make_scoped_ptr<net::StreamSocket>(nullptr)); | 83 callback.Run(result, "", "", make_scoped_ptr<net::StreamSocket>(nullptr)); |
83 return; | 84 return; |
84 } | 85 } |
85 new HttpRequest(socket.Pass(), request, callback); | 86 new HttpRequest(socket.Pass(), request, callback); |
86 } | 87 } |
87 | 88 |
88 private: | 89 private: |
89 HttpRequest(scoped_ptr<net::StreamSocket> socket, | 90 HttpRequest(scoped_ptr<net::StreamSocket> socket, |
90 const std::string& request, | 91 const std::string& request, |
91 const CommandCallback& callback) | 92 const CommandCallback& callback) |
92 : socket_(socket.Pass()), | 93 : socket_(socket.Pass()), |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 } | 158 } |
158 } | 159 } |
159 | 160 |
160 body_pos_ = response_.find("\r\n\r\n"); | 161 body_pos_ = response_.find("\r\n\r\n"); |
161 if (body_pos_ != std::string::npos) { | 162 if (body_pos_ != std::string::npos) { |
162 body_pos_ += 4; | 163 body_pos_ += 4; |
163 bytes_total = body_pos_ + expected_length; | 164 bytes_total = body_pos_ + expected_length; |
164 } | 165 } |
165 } | 166 } |
166 | 167 |
167 if (bytes_total == static_cast<int>(response_.length())) { | 168 if (!command_callback_.is_null() && |
168 if (!command_callback_.is_null()) { | 169 bytes_total == static_cast<int>(response_.length())) { |
169 command_callback_.Run(net::OK, response_.substr(body_pos_)); | 170 command_callback_.Run(net::OK, response_.substr(body_pos_)); |
170 } else { | |
171 http_upgrade_callback_.Run(net::OK, | |
172 ExtractHeader("Sec-WebSocket-Extensions:"), socket_.Pass()); | |
173 } | |
174 delete this; | 171 delete this; |
175 return; | 172 return; |
176 } | 173 } |
| 174 |
| 175 if (!http_upgrade_callback_.is_null() && body_pos_ != std::string::npos && |
| 176 body_pos_ > 0) { |
| 177 http_upgrade_callback_.Run(net::OK, |
| 178 ExtractHeader("Sec-WebSocket-Extensions:"), |
| 179 response_.substr(body_pos_), socket_.Pass()); |
| 180 delete this; |
| 181 return; |
| 182 } |
177 | 183 |
178 result = socket_->Read( | 184 result = socket_->Read( |
179 response_buffer.get(), | 185 response_buffer.get(), |
180 kBufferSize, | 186 kBufferSize, |
181 base::Bind(&HttpRequest::OnResponseData, | 187 base::Bind(&HttpRequest::OnResponseData, |
182 base::Unretained(this), | 188 base::Unretained(this), |
183 response_buffer, | 189 response_buffer, |
184 bytes_total)); | 190 bytes_total)); |
185 if (result != net::ERR_IO_PENDING) | 191 if (result != net::ERR_IO_PENDING) |
186 OnResponseData(response_buffer, bytes_total, result); | 192 OnResponseData(response_buffer, bytes_total, result); |
(...skipping 13 matching lines...) Expand all Loading... |
200 base::TrimWhitespace(value, base::TRIM_ALL, &value); | 206 base::TrimWhitespace(value, base::TRIM_ALL, &value); |
201 return value; | 207 return value; |
202 } | 208 } |
203 | 209 |
204 bool CheckNetResultOrDie(int result) { | 210 bool CheckNetResultOrDie(int result) { |
205 if (result >= 0) | 211 if (result >= 0) |
206 return true; | 212 return true; |
207 if (!command_callback_.is_null()) { | 213 if (!command_callback_.is_null()) { |
208 command_callback_.Run(result, std::string()); | 214 command_callback_.Run(result, std::string()); |
209 } else { | 215 } else { |
210 http_upgrade_callback_.Run( | 216 http_upgrade_callback_.Run(result, "", "", |
211 result, "", make_scoped_ptr<net::StreamSocket>(nullptr)); | 217 make_scoped_ptr<net::StreamSocket>(nullptr)); |
212 } | 218 } |
213 delete this; | 219 delete this; |
214 return false; | 220 return false; |
215 } | 221 } |
216 | 222 |
217 scoped_ptr<net::StreamSocket> socket_; | 223 scoped_ptr<net::StreamSocket> socket_; |
218 std::string response_; | 224 std::string response_; |
219 CommandCallback command_callback_; | 225 CommandCallback command_callback_; |
220 HttpUpgradeCallback http_upgrade_callback_; | 226 HttpUpgradeCallback http_upgrade_callback_; |
221 size_t body_pos_; | 227 size_t body_pos_; |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 it->provider, it->serial); | 529 it->provider, it->serial); |
524 } else { | 530 } else { |
525 device = found->second.get(); | 531 device = found->second.get(); |
526 } | 532 } |
527 response.push_back(device); | 533 response.push_back(device); |
528 new_devices[it->serial] = device->weak_factory_.GetWeakPtr(); | 534 new_devices[it->serial] = device->weak_factory_.GetWeakPtr(); |
529 } | 535 } |
530 devices_.swap(new_devices); | 536 devices_.swap(new_devices); |
531 callback.Run(response); | 537 callback.Run(response); |
532 } | 538 } |
OLD | NEW |