| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/api/sockets_tcp_server/sockets_tcp_server_ap
i.h" | 5 #include "extensions/browser/api/sockets_tcp_server/sockets_tcp_server_api.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | |
| 8 #include "chrome/browser/extensions/api/sockets_tcp_server/tcp_server_socket_eve
nt_dispatcher.h" | |
| 9 #include "chrome/common/extensions/api/sockets/sockets_manifest_data.h" | 7 #include "chrome/common/extensions/api/sockets/sockets_manifest_data.h" |
| 10 #include "chrome/common/extensions/permissions/socket_permission.h" | 8 #include "chrome/common/extensions/permissions/socket_permission.h" |
| 11 #include "content/public/common/socket_permission_request.h" | 9 #include "content/public/common/socket_permission_request.h" |
| 10 #include "extensions/browser/api/socket/tcp_socket.h" |
| 11 #include "extensions/browser/api/sockets_tcp_server/tcp_server_socket_event_disp
atcher.h" |
| 12 #include "extensions/common/permissions/permissions_data.h" | 12 #include "extensions/common/permissions/permissions_data.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::core_api::sockets_tcp_server::SocketInfo; |
| 18 using extensions::api::sockets_tcp_server::SocketProperties; | 18 using extensions::core_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 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, |
| 27 ResumableTCPServerSocket* socket) { | 27 ResumableTCPServerSocket* socket) { |
| 28 linked_ptr<SocketInfo> socket_info(new SocketInfo()); | 28 linked_ptr<SocketInfo> socket_info(new SocketInfo()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 52 socket->set_name(*properties->name.get()); | 52 socket->set_name(*properties->name.get()); |
| 53 } | 53 } |
| 54 if (properties->persistent.get()) { | 54 if (properties->persistent.get()) { |
| 55 socket->set_persistent(*properties->persistent.get()); | 55 socket->set_persistent(*properties->persistent.get()); |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 } // namespace | 59 } // namespace |
| 60 | 60 |
| 61 namespace extensions { | 61 namespace extensions { |
| 62 namespace api { | 62 namespace core_api { |
| 63 | 63 |
| 64 TCPServerSocketAsyncApiFunction::~TCPServerSocketAsyncApiFunction() {} | 64 TCPServerSocketAsyncApiFunction::~TCPServerSocketAsyncApiFunction() {} |
| 65 | 65 |
| 66 scoped_ptr<SocketResourceManagerInterface> | 66 scoped_ptr<SocketResourceManagerInterface> |
| 67 TCPServerSocketAsyncApiFunction::CreateSocketResourceManager() { | 67 TCPServerSocketAsyncApiFunction::CreateSocketResourceManager() { |
| 68 return scoped_ptr<SocketResourceManagerInterface>( | 68 return scoped_ptr<SocketResourceManagerInterface>( |
| 69 new SocketResourceManager<ResumableTCPServerSocket>()).Pass(); | 69 new SocketResourceManager<ResumableTCPServerSocket>()).Pass(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 ResumableTCPServerSocket* TCPServerSocketAsyncApiFunction::GetTcpSocket( | 72 ResumableTCPServerSocket* TCPServerSocketAsyncApiFunction::GetTcpSocket( |
| 73 int socket_id) { | 73 int socket_id) { |
| 74 return static_cast<ResumableTCPServerSocket*>(GetSocket(socket_id)); | 74 return static_cast<ResumableTCPServerSocket*>(GetSocket(socket_id)); |
| 75 } | 75 } |
| 76 | 76 |
| 77 SocketsTcpServerCreateFunction::SocketsTcpServerCreateFunction() {} | 77 SocketsTcpServerCreateFunction::SocketsTcpServerCreateFunction() {} |
| 78 | 78 |
| 79 SocketsTcpServerCreateFunction::~SocketsTcpServerCreateFunction() {} | 79 SocketsTcpServerCreateFunction::~SocketsTcpServerCreateFunction() {} |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 SetSocketProperties(socket, ¶ms_.get()->properties); | 119 SetSocketProperties(socket, ¶ms_.get()->properties); |
| 120 results_ = sockets_tcp_server::Update::Results::Create(); | 120 results_ = sockets_tcp_server::Update::Results::Create(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 SocketsTcpServerSetPausedFunction::SocketsTcpServerSetPausedFunction() | 123 SocketsTcpServerSetPausedFunction::SocketsTcpServerSetPausedFunction() |
| 124 : socket_event_dispatcher_(NULL) {} | 124 : socket_event_dispatcher_(NULL) {} |
| 125 | 125 |
| 126 SocketsTcpServerSetPausedFunction::~SocketsTcpServerSetPausedFunction() {} | 126 SocketsTcpServerSetPausedFunction::~SocketsTcpServerSetPausedFunction() {} |
| 127 | 127 |
| 128 bool SocketsTcpServerSetPausedFunction::Prepare() { | 128 bool SocketsTcpServerSetPausedFunction::Prepare() { |
| 129 params_ = api::sockets_tcp_server::SetPaused::Params::Create(*args_); | 129 params_ = core_api::sockets_tcp_server::SetPaused::Params::Create(*args_); |
| 130 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 130 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 131 | 131 |
| 132 socket_event_dispatcher_ = | 132 socket_event_dispatcher_ = |
| 133 TCPServerSocketEventDispatcher::Get(browser_context()); | 133 TCPServerSocketEventDispatcher::Get(browser_context()); |
| 134 DCHECK(socket_event_dispatcher_) << "There is no socket event dispatcher. " | 134 DCHECK(socket_event_dispatcher_) |
| 135 "If this assertion is failing during a test, then it is likely that " | 135 << "There is no socket event dispatcher. " |
| 136 "TestExtensionSystem is failing to provide an instance of " | 136 "If this assertion is failing during a test, then it is likely that " |
| 137 "TCPServerSocketEventDispatcher."; | 137 "TestExtensionSystem is failing to provide an instance of " |
| 138 "TCPServerSocketEventDispatcher."; |
| 138 return socket_event_dispatcher_ != NULL; | 139 return socket_event_dispatcher_ != NULL; |
| 139 } | 140 } |
| 140 | 141 |
| 141 void SocketsTcpServerSetPausedFunction::Work() { | 142 void SocketsTcpServerSetPausedFunction::Work() { |
| 142 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); | 143 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); |
| 143 if (!socket) { | 144 if (!socket) { |
| 144 error_ = kSocketNotFoundError; | 145 error_ = kSocketNotFoundError; |
| 145 return; | 146 return; |
| 146 } | 147 } |
| 147 | 148 |
| 148 if (socket->paused() != params_->paused) { | 149 if (socket->paused() != params_->paused) { |
| 149 socket->set_paused(params_->paused); | 150 socket->set_paused(params_->paused); |
| 150 if (socket->IsConnected() && !params_->paused) { | 151 if (socket->IsConnected() && !params_->paused) { |
| 151 socket_event_dispatcher_->OnServerSocketResume(extension_->id(), | 152 socket_event_dispatcher_->OnServerSocketResume(extension_->id(), |
| 152 params_->socket_id); | 153 params_->socket_id); |
| 153 } | 154 } |
| 154 } | 155 } |
| 155 | 156 |
| 156 results_ = sockets_tcp_server::SetPaused::Results::Create(); | 157 results_ = sockets_tcp_server::SetPaused::Results::Create(); |
| 157 } | 158 } |
| 158 | 159 |
| 159 SocketsTcpServerListenFunction::SocketsTcpServerListenFunction() | 160 SocketsTcpServerListenFunction::SocketsTcpServerListenFunction() |
| 160 : socket_event_dispatcher_(NULL) {} | 161 : socket_event_dispatcher_(NULL) {} |
| 161 | 162 |
| 162 SocketsTcpServerListenFunction::~SocketsTcpServerListenFunction() {} | 163 SocketsTcpServerListenFunction::~SocketsTcpServerListenFunction() {} |
| 163 | 164 |
| 164 bool SocketsTcpServerListenFunction::Prepare() { | 165 bool SocketsTcpServerListenFunction::Prepare() { |
| 165 params_ = api::sockets_tcp_server::Listen::Params::Create(*args_); | 166 params_ = core_api::sockets_tcp_server::Listen::Params::Create(*args_); |
| 166 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 167 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 167 | 168 |
| 168 socket_event_dispatcher_ = | 169 socket_event_dispatcher_ = |
| 169 TCPServerSocketEventDispatcher::Get(browser_context()); | 170 TCPServerSocketEventDispatcher::Get(browser_context()); |
| 170 DCHECK(socket_event_dispatcher_) << "There is no socket event dispatcher. " | 171 DCHECK(socket_event_dispatcher_) |
| 171 "If this assertion is failing during a test, then it is likely that " | 172 << "There is no socket event dispatcher. " |
| 172 "TestExtensionSystem is failing to provide an instance of " | 173 "If this assertion is failing during a test, then it is likely that " |
| 173 "TCPServerSocketEventDispatcher."; | 174 "TestExtensionSystem is failing to provide an instance of " |
| 175 "TCPServerSocketEventDispatcher."; |
| 174 return socket_event_dispatcher_ != NULL; | 176 return socket_event_dispatcher_ != NULL; |
| 175 } | 177 } |
| 176 | 178 |
| 177 void SocketsTcpServerListenFunction::Work() { | 179 void SocketsTcpServerListenFunction::Work() { |
| 178 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); | 180 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); |
| 179 if (!socket) { | 181 if (!socket) { |
| 180 error_ = kSocketNotFoundError; | 182 error_ = kSocketNotFoundError; |
| 181 return; | 183 return; |
| 182 } | 184 } |
| 183 | 185 |
| 184 SocketPermissionRequest param( | 186 SocketPermissionRequest param( |
| 185 SocketPermissionRequest::TCP_LISTEN, | 187 SocketPermissionRequest::TCP_LISTEN, params_->address, params_->port); |
| 186 params_->address, | |
| 187 params_->port); | |
| 188 if (!SocketsManifestData::CheckRequest(GetExtension(), param)) { | 188 if (!SocketsManifestData::CheckRequest(GetExtension(), param)) { |
| 189 error_ = kPermissionError; | 189 error_ = kPermissionError; |
| 190 return; | 190 return; |
| 191 } | 191 } |
| 192 | 192 |
| 193 int net_result = socket->Listen( | 193 int net_result = socket->Listen( |
| 194 params_->address, | 194 params_->address, |
| 195 params_->port, | 195 params_->port, |
| 196 params_->backlog.get() ? *params_->backlog.get() : kDefaultListenBacklog, | 196 params_->backlog.get() ? *params_->backlog.get() : kDefaultListenBacklog, |
| 197 &error_); | 197 &error_); |
| 198 | 198 |
| 199 if (net_result != net::OK) | 199 if (net_result != net::OK) |
| 200 error_ = net::ErrorToString(net_result); | 200 error_ = net::ErrorToString(net_result); |
| 201 | 201 |
| 202 | |
| 203 if (net_result == net::OK) { | 202 if (net_result == net::OK) { |
| 204 socket_event_dispatcher_->OnServerSocketListen(extension_->id(), | 203 socket_event_dispatcher_->OnServerSocketListen(extension_->id(), |
| 205 params_->socket_id); | 204 params_->socket_id); |
| 206 } | 205 } |
| 207 | 206 |
| 208 results_ = sockets_tcp_server::Listen::Results::Create(net_result); | 207 results_ = sockets_tcp_server::Listen::Results::Create(net_result); |
| 209 } | 208 } |
| 210 | 209 |
| 211 SocketsTcpServerDisconnectFunction::SocketsTcpServerDisconnectFunction() {} | 210 SocketsTcpServerDisconnectFunction::SocketsTcpServerDisconnectFunction() {} |
| 212 | 211 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 | 268 |
| 270 linked_ptr<sockets_tcp_server::SocketInfo> socket_info = | 269 linked_ptr<sockets_tcp_server::SocketInfo> socket_info = |
| 271 CreateSocketInfo(params_->socket_id, socket); | 270 CreateSocketInfo(params_->socket_id, socket); |
| 272 results_ = sockets_tcp_server::GetInfo::Results::Create(*socket_info); | 271 results_ = sockets_tcp_server::GetInfo::Results::Create(*socket_info); |
| 273 } | 272 } |
| 274 | 273 |
| 275 SocketsTcpServerGetSocketsFunction::SocketsTcpServerGetSocketsFunction() {} | 274 SocketsTcpServerGetSocketsFunction::SocketsTcpServerGetSocketsFunction() {} |
| 276 | 275 |
| 277 SocketsTcpServerGetSocketsFunction::~SocketsTcpServerGetSocketsFunction() {} | 276 SocketsTcpServerGetSocketsFunction::~SocketsTcpServerGetSocketsFunction() {} |
| 278 | 277 |
| 279 bool SocketsTcpServerGetSocketsFunction::Prepare() { | 278 bool SocketsTcpServerGetSocketsFunction::Prepare() { return true; } |
| 280 return true; | |
| 281 } | |
| 282 | 279 |
| 283 void SocketsTcpServerGetSocketsFunction::Work() { | 280 void SocketsTcpServerGetSocketsFunction::Work() { |
| 284 std::vector<linked_ptr<sockets_tcp_server::SocketInfo> > socket_infos; | 281 std::vector<linked_ptr<sockets_tcp_server::SocketInfo> > socket_infos; |
| 285 base::hash_set<int>* resource_ids = GetSocketIds(); | 282 base::hash_set<int>* resource_ids = GetSocketIds(); |
| 286 if (resource_ids != NULL) { | 283 if (resource_ids != NULL) { |
| 287 for (base::hash_set<int>::iterator it = resource_ids->begin(); | 284 for (base::hash_set<int>::iterator it = resource_ids->begin(); |
| 288 it != resource_ids->end(); ++it) { | 285 it != resource_ids->end(); |
| 286 ++it) { |
| 289 int socket_id = *it; | 287 int socket_id = *it; |
| 290 ResumableTCPServerSocket* socket = GetTcpSocket(socket_id); | 288 ResumableTCPServerSocket* socket = GetTcpSocket(socket_id); |
| 291 if (socket) { | 289 if (socket) { |
| 292 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); | 290 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); |
| 293 } | 291 } |
| 294 } | 292 } |
| 295 } | 293 } |
| 296 results_ = sockets_tcp_server::GetSockets::Results::Create(socket_infos); | 294 results_ = sockets_tcp_server::GetSockets::Results::Create(socket_infos); |
| 297 } | 295 } |
| 298 | 296 |
| 299 } // namespace api | 297 } // namespace core_api |
| 300 } // namespace extensions | 298 } // namespace extensions |
| OLD | NEW |