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