| 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 "content/browser/devtools/protocol/tethering_handler.h" | 5 #include "content/browser/devtools/protocol/tethering_handler.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 private: | 149 private: |
| 150 scoped_ptr<net::StreamSocket> client_socket_; | 150 scoped_ptr<net::StreamSocket> client_socket_; |
| 151 scoped_ptr<net::ServerSocket> server_socket_; | 151 scoped_ptr<net::ServerSocket> server_socket_; |
| 152 scoped_ptr<net::StreamSocket> accepted_socket_; | 152 scoped_ptr<net::StreamSocket> accepted_socket_; |
| 153 int pending_writes_; | 153 int pending_writes_; |
| 154 bool pending_destruction_; | 154 bool pending_destruction_; |
| 155 }; | 155 }; |
| 156 | 156 |
| 157 class BoundSocket { | 157 class BoundSocket { |
| 158 public: | 158 public: |
| 159 typedef base::Callback<void(uint16, const std::string&)> AcceptedCallback; | 159 typedef base::Callback<void(uint16_t, const std::string&)> AcceptedCallback; |
| 160 | 160 |
| 161 BoundSocket(AcceptedCallback accepted_callback, | 161 BoundSocket(AcceptedCallback accepted_callback, |
| 162 const CreateServerSocketCallback& socket_callback) | 162 const CreateServerSocketCallback& socket_callback) |
| 163 : accepted_callback_(accepted_callback), | 163 : accepted_callback_(accepted_callback), |
| 164 socket_callback_(socket_callback), | 164 socket_callback_(socket_callback), |
| 165 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), | 165 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), |
| 166 port_(0) { | 166 port_(0) { |
| 167 } | 167 } |
| 168 | 168 |
| 169 virtual ~BoundSocket() { | 169 virtual ~BoundSocket() { |
| 170 } | 170 } |
| 171 | 171 |
| 172 bool Listen(uint16 port) { | 172 bool Listen(uint16_t port) { |
| 173 port_ = port; | 173 port_ = port; |
| 174 net::IPAddressNumber ip_number; | 174 net::IPAddressNumber ip_number; |
| 175 if (!net::ParseIPLiteralToNumber(kLocalhost, &ip_number)) | 175 if (!net::ParseIPLiteralToNumber(kLocalhost, &ip_number)) |
| 176 return false; | 176 return false; |
| 177 | 177 |
| 178 net::IPEndPoint end_point(ip_number, port); | 178 net::IPEndPoint end_point(ip_number, port); |
| 179 int result = socket_->Listen(end_point, kListenBacklog); | 179 int result = socket_->Listen(end_point, kListenBacklog); |
| 180 if (result < 0) | 180 if (result < 0) |
| 181 return false; | 181 return false; |
| 182 | 182 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 SocketPump* pump = new SocketPump(accept_socket_.release()); | 217 SocketPump* pump = new SocketPump(accept_socket_.release()); |
| 218 std::string name = pump->Init(socket_callback_); | 218 std::string name = pump->Init(socket_callback_); |
| 219 if (!name.empty()) | 219 if (!name.empty()) |
| 220 accepted_callback_.Run(port_, name); | 220 accepted_callback_.Run(port_, name); |
| 221 } | 221 } |
| 222 | 222 |
| 223 AcceptedCallback accepted_callback_; | 223 AcceptedCallback accepted_callback_; |
| 224 CreateServerSocketCallback socket_callback_; | 224 CreateServerSocketCallback socket_callback_; |
| 225 scoped_ptr<net::ServerSocket> socket_; | 225 scoped_ptr<net::ServerSocket> socket_; |
| 226 scoped_ptr<net::StreamSocket> accept_socket_; | 226 scoped_ptr<net::StreamSocket> accept_socket_; |
| 227 uint16 port_; | 227 uint16_t port_; |
| 228 }; | 228 }; |
| 229 | 229 |
| 230 } // namespace | 230 } // namespace |
| 231 | 231 |
| 232 // TetheringHandler::TetheringImpl ------------------------------------------- | 232 // TetheringHandler::TetheringImpl ------------------------------------------- |
| 233 | 233 |
| 234 class TetheringHandler::TetheringImpl { | 234 class TetheringHandler::TetheringImpl { |
| 235 public: | 235 public: |
| 236 TetheringImpl( | 236 TetheringImpl( |
| 237 base::WeakPtr<TetheringHandler> handler, | 237 base::WeakPtr<TetheringHandler> handler, |
| 238 const CreateServerSocketCallback& socket_callback); | 238 const CreateServerSocketCallback& socket_callback); |
| 239 ~TetheringImpl(); | 239 ~TetheringImpl(); |
| 240 | 240 |
| 241 void Bind(DevToolsCommandId command_id, uint16 port); | 241 void Bind(DevToolsCommandId command_id, uint16_t port); |
| 242 void Unbind(DevToolsCommandId command_id, uint16 port); | 242 void Unbind(DevToolsCommandId command_id, uint16_t port); |
| 243 void Accepted(uint16 port, const std::string& name); | 243 void Accepted(uint16_t port, const std::string& name); |
| 244 | 244 |
| 245 private: | 245 private: |
| 246 void SendInternalError(DevToolsCommandId command_id, | 246 void SendInternalError(DevToolsCommandId command_id, |
| 247 const std::string& message); | 247 const std::string& message); |
| 248 | 248 |
| 249 base::WeakPtr<TetheringHandler> handler_; | 249 base::WeakPtr<TetheringHandler> handler_; |
| 250 CreateServerSocketCallback socket_callback_; | 250 CreateServerSocketCallback socket_callback_; |
| 251 | 251 |
| 252 typedef std::map<uint16, BoundSocket*> BoundSockets; | 252 typedef std::map<uint16_t, BoundSocket*> BoundSockets; |
| 253 BoundSockets bound_sockets_; | 253 BoundSockets bound_sockets_; |
| 254 }; | 254 }; |
| 255 | 255 |
| 256 TetheringHandler::TetheringImpl::TetheringImpl( | 256 TetheringHandler::TetheringImpl::TetheringImpl( |
| 257 base::WeakPtr<TetheringHandler> handler, | 257 base::WeakPtr<TetheringHandler> handler, |
| 258 const CreateServerSocketCallback& socket_callback) | 258 const CreateServerSocketCallback& socket_callback) |
| 259 : handler_(handler), | 259 : handler_(handler), |
| 260 socket_callback_(socket_callback) { | 260 socket_callback_(socket_callback) { |
| 261 } | 261 } |
| 262 | 262 |
| 263 TetheringHandler::TetheringImpl::~TetheringImpl() { | 263 TetheringHandler::TetheringImpl::~TetheringImpl() { |
| 264 STLDeleteValues(&bound_sockets_); | 264 STLDeleteValues(&bound_sockets_); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void TetheringHandler::TetheringImpl::Bind( | 267 void TetheringHandler::TetheringImpl::Bind(DevToolsCommandId command_id, |
| 268 DevToolsCommandId command_id, uint16 port) { | 268 uint16_t port) { |
| 269 if (bound_sockets_.find(port) != bound_sockets_.end()) { | 269 if (bound_sockets_.find(port) != bound_sockets_.end()) { |
| 270 SendInternalError(command_id, "Port already bound"); | 270 SendInternalError(command_id, "Port already bound"); |
| 271 return; | 271 return; |
| 272 } | 272 } |
| 273 | 273 |
| 274 BoundSocket::AcceptedCallback callback = base::Bind( | 274 BoundSocket::AcceptedCallback callback = base::Bind( |
| 275 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); | 275 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); |
| 276 scoped_ptr<BoundSocket> bound_socket( | 276 scoped_ptr<BoundSocket> bound_socket( |
| 277 new BoundSocket(callback, socket_callback_)); | 277 new BoundSocket(callback, socket_callback_)); |
| 278 if (!bound_socket->Listen(port)) { | 278 if (!bound_socket->Listen(port)) { |
| 279 SendInternalError(command_id, "Could not bind port"); | 279 SendInternalError(command_id, "Could not bind port"); |
| 280 return; | 280 return; |
| 281 } | 281 } |
| 282 | 282 |
| 283 bound_sockets_[port] = bound_socket.release(); | 283 bound_sockets_[port] = bound_socket.release(); |
| 284 BrowserThread::PostTask( | 284 BrowserThread::PostTask( |
| 285 BrowserThread::UI, | 285 BrowserThread::UI, |
| 286 FROM_HERE, | 286 FROM_HERE, |
| 287 base::Bind(&TetheringHandler::SendBindSuccess, handler_, command_id)); | 287 base::Bind(&TetheringHandler::SendBindSuccess, handler_, command_id)); |
| 288 } | 288 } |
| 289 | 289 |
| 290 void TetheringHandler::TetheringImpl::Unbind( | 290 void TetheringHandler::TetheringImpl::Unbind(DevToolsCommandId command_id, |
| 291 DevToolsCommandId command_id, uint16 port) { | 291 uint16_t port) { |
| 292 | |
| 293 BoundSockets::iterator it = bound_sockets_.find(port); | 292 BoundSockets::iterator it = bound_sockets_.find(port); |
| 294 if (it == bound_sockets_.end()) { | 293 if (it == bound_sockets_.end()) { |
| 295 SendInternalError(command_id, "Port is not bound"); | 294 SendInternalError(command_id, "Port is not bound"); |
| 296 return; | 295 return; |
| 297 } | 296 } |
| 298 | 297 |
| 299 delete it->second; | 298 delete it->second; |
| 300 bound_sockets_.erase(it); | 299 bound_sockets_.erase(it); |
| 301 BrowserThread::PostTask( | 300 BrowserThread::PostTask( |
| 302 BrowserThread::UI, | 301 BrowserThread::UI, |
| 303 FROM_HERE, | 302 FROM_HERE, |
| 304 base::Bind(&TetheringHandler::SendUnbindSuccess, handler_, command_id)); | 303 base::Bind(&TetheringHandler::SendUnbindSuccess, handler_, command_id)); |
| 305 } | 304 } |
| 306 | 305 |
| 307 void TetheringHandler::TetheringImpl::Accepted( | 306 void TetheringHandler::TetheringImpl::Accepted(uint16_t port, |
| 308 uint16 port, const std::string& name) { | 307 const std::string& name) { |
| 309 BrowserThread::PostTask( | 308 BrowserThread::PostTask( |
| 310 BrowserThread::UI, | 309 BrowserThread::UI, |
| 311 FROM_HERE, | 310 FROM_HERE, |
| 312 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); | 311 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); |
| 313 } | 312 } |
| 314 | 313 |
| 315 void TetheringHandler::TetheringImpl::SendInternalError( | 314 void TetheringHandler::TetheringImpl::SendInternalError( |
| 316 DevToolsCommandId command_id, | 315 DevToolsCommandId command_id, |
| 317 const std::string& message) { | 316 const std::string& message) { |
| 318 BrowserThread::PostTask( | 317 BrowserThread::PostTask( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 341 if (is_active_) { | 340 if (is_active_) { |
| 342 task_runner_->DeleteSoon(FROM_HERE, impl_); | 341 task_runner_->DeleteSoon(FROM_HERE, impl_); |
| 343 impl_ = nullptr; | 342 impl_ = nullptr; |
| 344 } | 343 } |
| 345 } | 344 } |
| 346 | 345 |
| 347 void TetheringHandler::SetClient(scoped_ptr<Client> client) { | 346 void TetheringHandler::SetClient(scoped_ptr<Client> client) { |
| 348 client_.swap(client); | 347 client_.swap(client); |
| 349 } | 348 } |
| 350 | 349 |
| 351 void TetheringHandler::Accepted(uint16 port, const std::string& name) { | 350 void TetheringHandler::Accepted(uint16_t port, const std::string& name) { |
| 352 client_->Accepted(AcceptedParams::Create()->set_port(port) | 351 client_->Accepted(AcceptedParams::Create()->set_port(port) |
| 353 ->set_connection_id(name)); | 352 ->set_connection_id(name)); |
| 354 } | 353 } |
| 355 | 354 |
| 356 bool TetheringHandler::Activate() { | 355 bool TetheringHandler::Activate() { |
| 357 if (is_active_) | 356 if (is_active_) |
| 358 return true; | 357 return true; |
| 359 if (impl_) | 358 if (impl_) |
| 360 return false; | 359 return false; |
| 361 is_active_ = true; | 360 is_active_ = true; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 } | 396 } |
| 398 | 397 |
| 399 void TetheringHandler::SendInternalError(DevToolsCommandId command_id, | 398 void TetheringHandler::SendInternalError(DevToolsCommandId command_id, |
| 400 const std::string& message) { | 399 const std::string& message) { |
| 401 client_->SendError(command_id, Response::InternalError(message)); | 400 client_->SendError(command_id, Response::InternalError(message)); |
| 402 } | 401 } |
| 403 | 402 |
| 404 } // namespace tethering | 403 } // namespace tethering |
| 405 } // namespace devtools | 404 } // namespace devtools |
| 406 } // namespace content | 405 } // namespace content |
| OLD | NEW |