| 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 "extensions/browser/api/sockets_tcp_server/sockets_tcp_server_api.h" | 5 #include "extensions/browser/api/sockets_tcp_server/sockets_tcp_server_api.h" |
| 6 | 6 |
| 7 #include "content/public/common/socket_permission_request.h" | 7 #include "content/public/common/socket_permission_request.h" |
| 8 #include "extensions/browser/api/socket/tcp_socket.h" | 8 #include "extensions/browser/api/socket/tcp_socket.h" |
| 9 #include "extensions/browser/api/sockets_tcp_server/tcp_server_socket_event_disp
atcher.h" | 9 #include "extensions/browser/api/sockets_tcp_server/tcp_server_socket_event_disp
atcher.h" |
| 10 #include "extensions/common/api/sockets/sockets_manifest_data.h" | 10 #include "extensions/common/api/sockets/sockets_manifest_data.h" |
| 11 #include "extensions/common/permissions/permissions_data.h" | 11 #include "extensions/common/permissions/permissions_data.h" |
| 12 #include "extensions/common/permissions/socket_permission.h" | 12 #include "extensions/common/permissions/socket_permission.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 | 14 |
| 15 using content::SocketPermissionRequest; | 15 using content::SocketPermissionRequest; |
| 16 using extensions::ResumableTCPServerSocket; | 16 using extensions::ResumableTCPServerSocket; |
| 17 using extensions::api::sockets_tcp_server::SocketInfo; | 17 using extensions::api::sockets_tcp_server::SocketInfo; |
| 18 using extensions::api::sockets_tcp_server::SocketProperties; | 18 using extensions::api::sockets_tcp_server::SocketProperties; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const char kSocketNotFoundError[] = "Socket not found"; | 22 const char kSocketNotFoundError[] = "Socket not found"; |
| 23 const char kPermissionError[] = "Does not have permission"; | 23 const char kPermissionError[] = "Does not have permission"; |
| 24 const int kDefaultListenBacklog = SOMAXCONN; | 24 const int kDefaultListenBacklog = SOMAXCONN; |
| 25 | 25 |
| 26 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, | 26 SocketInfo CreateSocketInfo(int socket_id, ResumableTCPServerSocket* socket) { |
| 27 ResumableTCPServerSocket* socket) { | 27 SocketInfo socket_info; |
| 28 linked_ptr<SocketInfo> socket_info(new SocketInfo()); | |
| 29 // This represents what we know about the socket, and does not call through | 28 // This represents what we know about the socket, and does not call through |
| 30 // to the system. | 29 // to the system. |
| 31 socket_info->socket_id = socket_id; | 30 socket_info.socket_id = socket_id; |
| 32 if (!socket->name().empty()) { | 31 if (!socket->name().empty()) { |
| 33 socket_info->name.reset(new std::string(socket->name())); | 32 socket_info.name.reset(new std::string(socket->name())); |
| 34 } | 33 } |
| 35 socket_info->persistent = socket->persistent(); | 34 socket_info.persistent = socket->persistent(); |
| 36 socket_info->paused = socket->paused(); | 35 socket_info.paused = socket->paused(); |
| 37 | 36 |
| 38 // Grab the local address as known by the OS. | 37 // Grab the local address as known by the OS. |
| 39 net::IPEndPoint localAddress; | 38 net::IPEndPoint localAddress; |
| 40 if (socket->GetLocalAddress(&localAddress)) { | 39 if (socket->GetLocalAddress(&localAddress)) { |
| 41 socket_info->local_address.reset( | 40 socket_info.local_address.reset( |
| 42 new std::string(localAddress.ToStringWithoutPort())); | 41 new std::string(localAddress.ToStringWithoutPort())); |
| 43 socket_info->local_port.reset(new int(localAddress.port())); | 42 socket_info.local_port.reset(new int(localAddress.port())); |
| 44 } | 43 } |
| 45 | 44 |
| 46 return socket_info; | 45 return socket_info; |
| 47 } | 46 } |
| 48 | 47 |
| 49 void SetSocketProperties(ResumableTCPServerSocket* socket, | 48 void SetSocketProperties(ResumableTCPServerSocket* socket, |
| 50 SocketProperties* properties) { | 49 SocketProperties* properties) { |
| 51 if (properties->name.get()) { | 50 if (properties->name.get()) { |
| 52 socket->set_name(*properties->name.get()); | 51 socket->set_name(*properties->name.get()); |
| 53 } | 52 } |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 return true; | 261 return true; |
| 263 } | 262 } |
| 264 | 263 |
| 265 void SocketsTcpServerGetInfoFunction::Work() { | 264 void SocketsTcpServerGetInfoFunction::Work() { |
| 266 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); | 265 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); |
| 267 if (!socket) { | 266 if (!socket) { |
| 268 error_ = kSocketNotFoundError; | 267 error_ = kSocketNotFoundError; |
| 269 return; | 268 return; |
| 270 } | 269 } |
| 271 | 270 |
| 272 linked_ptr<sockets_tcp_server::SocketInfo> socket_info = | 271 sockets_tcp_server::SocketInfo socket_info = |
| 273 CreateSocketInfo(params_->socket_id, socket); | 272 CreateSocketInfo(params_->socket_id, socket); |
| 274 results_ = sockets_tcp_server::GetInfo::Results::Create(*socket_info); | 273 results_ = sockets_tcp_server::GetInfo::Results::Create(socket_info); |
| 275 } | 274 } |
| 276 | 275 |
| 277 SocketsTcpServerGetSocketsFunction::SocketsTcpServerGetSocketsFunction() {} | 276 SocketsTcpServerGetSocketsFunction::SocketsTcpServerGetSocketsFunction() {} |
| 278 | 277 |
| 279 SocketsTcpServerGetSocketsFunction::~SocketsTcpServerGetSocketsFunction() {} | 278 SocketsTcpServerGetSocketsFunction::~SocketsTcpServerGetSocketsFunction() {} |
| 280 | 279 |
| 281 bool SocketsTcpServerGetSocketsFunction::Prepare() { return true; } | 280 bool SocketsTcpServerGetSocketsFunction::Prepare() { return true; } |
| 282 | 281 |
| 283 void SocketsTcpServerGetSocketsFunction::Work() { | 282 void SocketsTcpServerGetSocketsFunction::Work() { |
| 284 std::vector<linked_ptr<sockets_tcp_server::SocketInfo> > socket_infos; | 283 std::vector<sockets_tcp_server::SocketInfo> socket_infos; |
| 285 base::hash_set<int>* resource_ids = GetSocketIds(); | 284 base::hash_set<int>* resource_ids = GetSocketIds(); |
| 286 if (resource_ids != NULL) { | 285 if (resource_ids != NULL) { |
| 287 for (base::hash_set<int>::iterator it = resource_ids->begin(); | 286 for (int socket_id : *resource_ids) { |
| 288 it != resource_ids->end(); | |
| 289 ++it) { | |
| 290 int socket_id = *it; | |
| 291 ResumableTCPServerSocket* socket = GetTcpSocket(socket_id); | 287 ResumableTCPServerSocket* socket = GetTcpSocket(socket_id); |
| 292 if (socket) { | 288 if (socket) { |
| 293 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); | 289 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); |
| 294 } | 290 } |
| 295 } | 291 } |
| 296 } | 292 } |
| 297 results_ = sockets_tcp_server::GetSockets::Results::Create(socket_infos); | 293 results_ = sockets_tcp_server::GetSockets::Results::Create(socket_infos); |
| 298 } | 294 } |
| 299 | 295 |
| 300 } // namespace api | 296 } // namespace api |
| 301 } // namespace extensions | 297 } // namespace extensions |
| OLD | NEW |