| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 int result, | 43 int result, |
| 44 const std::string& response) { | 44 const std::string& response) { |
| 45 response_message_loop->PostTask(FROM_HERE, | 45 response_message_loop->PostTask(FROM_HERE, |
| 46 base::Bind(callback, result, response)); | 46 base::Bind(callback, result, response)); |
| 47 } | 47 } |
| 48 | 48 |
| 49 static void PostSocketCallback( | 49 static void PostSocketCallback( |
| 50 scoped_refptr<base::MessageLoopProxy> response_message_loop, | 50 scoped_refptr<base::MessageLoopProxy> response_message_loop, |
| 51 const AndroidDeviceManager::SocketCallback& callback, | 51 const AndroidDeviceManager::SocketCallback& callback, |
| 52 int result, | 52 int result, |
| 53 scoped_ptr<net::StreamSocket> socket) { | 53 net::StreamSocket* socket) { |
| 54 response_message_loop->PostTask( | 54 response_message_loop->PostTask(FROM_HERE, |
| 55 FROM_HERE, base::Bind(callback, result, base::Passed(&socket))); | 55 base::Bind(callback, result, socket)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 class HttpRequest { | 58 class HttpRequest { |
| 59 public: | 59 public: |
| 60 typedef AndroidDeviceManager::CommandCallback CommandCallback; | 60 typedef AndroidDeviceManager::CommandCallback CommandCallback; |
| 61 typedef AndroidDeviceManager::SocketCallback SocketCallback; | 61 typedef AndroidDeviceManager::SocketCallback SocketCallback; |
| 62 | 62 |
| 63 static void CommandRequest(const std::string& request, | 63 static void CommandRequest(const std::string& request, |
| 64 const CommandCallback& callback, | 64 const CommandCallback& callback, |
| 65 int result, | 65 int result, |
| 66 scoped_ptr<net::StreamSocket> socket) { | 66 net::StreamSocket* socket) { |
| 67 if (result != net::OK) { | 67 if (result != net::OK) { |
| 68 callback.Run(result, std::string()); | 68 callback.Run(result, std::string()); |
| 69 return; | 69 return; |
| 70 } | 70 } |
| 71 new HttpRequest(socket.Pass(), request, callback); | 71 new HttpRequest(socket, request, callback); |
| 72 } | 72 } |
| 73 | 73 |
| 74 static void SocketRequest(const std::string& request, | 74 static void SocketRequest(const std::string& request, |
| 75 const SocketCallback& callback, | 75 const SocketCallback& callback, |
| 76 int result, | 76 int result, |
| 77 scoped_ptr<net::StreamSocket> socket) { | 77 net::StreamSocket* socket) { |
| 78 if (result != net::OK) { | 78 if (result != net::OK) { |
| 79 callback.Run(result, make_scoped_ptr<net::StreamSocket>(NULL)); | 79 callback.Run(result, NULL); |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 new HttpRequest(socket.Pass(), request, callback); | 82 new HttpRequest(socket, request, callback); |
| 83 } | 83 } |
| 84 | 84 |
| 85 private: | 85 private: |
| 86 HttpRequest(scoped_ptr<net::StreamSocket> socket, | 86 HttpRequest(net::StreamSocket* socket, |
| 87 const std::string& request, | 87 const std::string& request, |
| 88 const CommandCallback& callback) | 88 const CommandCallback& callback) |
| 89 : socket_(socket.Pass()), | 89 : socket_(socket), command_callback_(callback), body_pos_(0) { |
| 90 command_callback_(callback), | |
| 91 body_pos_(0) { | |
| 92 SendRequest(request); | 90 SendRequest(request); |
| 93 } | 91 } |
| 94 | 92 |
| 95 HttpRequest(scoped_ptr<net::StreamSocket> socket, | 93 HttpRequest(net::StreamSocket* socket, |
| 96 const std::string& request, | 94 const std::string& request, |
| 97 const SocketCallback& callback) | 95 const SocketCallback& callback) |
| 98 : socket_(socket.Pass()), | 96 : socket_(socket), |
| 99 socket_callback_(callback), | 97 socket_callback_(callback), |
| 100 body_pos_(0) { | 98 body_pos_(0) { |
| 101 SendRequest(request); | 99 SendRequest(request); |
| 102 } | 100 } |
| 103 | 101 |
| 104 ~HttpRequest() { | 102 ~HttpRequest() { |
| 105 } | 103 } |
| 106 | 104 |
| 107 void SendRequest(const std::string& request) { | 105 void SendRequest(const std::string& request) { |
| 108 scoped_refptr<net::StringIOBuffer> request_buffer = | 106 scoped_refptr<net::StringIOBuffer> request_buffer = |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 if (body_pos_ != std::string::npos) { | 162 if (body_pos_ != std::string::npos) { |
| 165 body_pos_ += 4; | 163 body_pos_ += 4; |
| 166 bytes_total = body_pos_ + expected_length; | 164 bytes_total = body_pos_ + expected_length; |
| 167 } | 165 } |
| 168 } | 166 } |
| 169 | 167 |
| 170 if (bytes_total == static_cast<int>(response_.length())) { | 168 if (bytes_total == static_cast<int>(response_.length())) { |
| 171 if (!command_callback_.is_null()) | 169 if (!command_callback_.is_null()) |
| 172 command_callback_.Run(net::OK, response_.substr(body_pos_)); | 170 command_callback_.Run(net::OK, response_.substr(body_pos_)); |
| 173 else | 171 else |
| 174 socket_callback_.Run(net::OK, socket_.Pass()); | 172 socket_callback_.Run(net::OK, socket_.release()); |
| 175 delete this; | 173 delete this; |
| 176 return; | 174 return; |
| 177 } | 175 } |
| 178 | 176 |
| 179 result = socket_->Read( | 177 result = socket_->Read( |
| 180 response_buffer.get(), | 178 response_buffer.get(), |
| 181 kBufferSize, | 179 kBufferSize, |
| 182 base::Bind(&HttpRequest::OnResponseData, | 180 base::Bind(&HttpRequest::OnResponseData, |
| 183 base::Unretained(this), | 181 base::Unretained(this), |
| 184 response_buffer, | 182 response_buffer, |
| 185 bytes_total)); | 183 bytes_total)); |
| 186 if (result != net::ERR_IO_PENDING) | 184 if (result != net::ERR_IO_PENDING) |
| 187 OnResponseData(response_buffer, bytes_total, result); | 185 OnResponseData(response_buffer, bytes_total, result); |
| 188 } | 186 } |
| 189 | 187 |
| 190 bool CheckNetResultOrDie(int result) { | 188 bool CheckNetResultOrDie(int result) { |
| 191 if (result >= 0) | 189 if (result >= 0) |
| 192 return true; | 190 return true; |
| 193 if (!command_callback_.is_null()) | 191 if (!command_callback_.is_null()) |
| 194 command_callback_.Run(result, std::string()); | 192 command_callback_.Run(result, std::string()); |
| 195 else | 193 else |
| 196 socket_callback_.Run(result, make_scoped_ptr<net::StreamSocket>(NULL)); | 194 socket_callback_.Run(result, NULL); |
| 197 delete this; | 195 delete this; |
| 198 return false; | 196 return false; |
| 199 } | 197 } |
| 200 | 198 |
| 201 scoped_ptr<net::StreamSocket> socket_; | 199 scoped_ptr<net::StreamSocket> socket_; |
| 202 std::string response_; | 200 std::string response_; |
| 203 AndroidDeviceManager::CommandCallback command_callback_; | 201 AndroidDeviceManager::CommandCallback command_callback_; |
| 204 AndroidDeviceManager::SocketCallback socket_callback_; | 202 AndroidDeviceManager::SocketCallback socket_callback_; |
| 205 size_t body_pos_; | 203 size_t body_pos_; |
| 206 }; | 204 }; |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 it->provider, it->serial); | 493 it->provider, it->serial); |
| 496 } else { | 494 } else { |
| 497 device = found->second.get(); | 495 device = found->second.get(); |
| 498 } | 496 } |
| 499 response.push_back(device); | 497 response.push_back(device); |
| 500 new_devices[it->serial] = device->weak_factory_.GetWeakPtr(); | 498 new_devices[it->serial] = device->weak_factory_.GetWeakPtr(); |
| 501 } | 499 } |
| 502 devices_.swap(new_devices); | 500 devices_.swap(new_devices); |
| 503 callback.Run(response); | 501 callback.Run(response); |
| 504 } | 502 } |
| OLD | NEW |