Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/socket/socket_api.h" | 5 #include "chrome/browser/extensions/api/socket/socket_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/common/extensions/extension.h" | |
| 8 #include "chrome/common/extensions/permissions/socket_permission.h" | 9 #include "chrome/common/extensions/permissions/socket_permission.h" |
| 9 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 11 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
| 11 #include "chrome/browser/extensions/api/socket/socket.h" | 12 #include "chrome/browser/extensions/api/socket/socket.h" |
| 12 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | 13 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
| 13 #include "chrome/browser/extensions/api/socket/udp_socket.h" | 14 #include "chrome/browser/extensions/api/socket/udp_socket.h" |
| 14 #include "chrome/browser/extensions/extension_system.h" | 15 #include "chrome/browser/extensions/extension_system.h" |
| 15 #include "chrome/browser/io_thread.h" | 16 #include "chrome/browser/io_thread.h" |
| 16 #include "net/base/host_port_pair.h" | 17 #include "net/base/host_port_pair.h" |
| 17 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 29 const char kResultCodeKey[] = "resultCode"; | 30 const char kResultCodeKey[] = "resultCode"; |
| 30 const char kSocketIdKey[] = "socketId"; | 31 const char kSocketIdKey[] = "socketId"; |
| 31 const char kTCPOption[] = "tcp"; | 32 const char kTCPOption[] = "tcp"; |
| 32 const char kUDPOption[] = "udp"; | 33 const char kUDPOption[] = "udp"; |
| 33 const char kUnknown[] = "unknown"; | 34 const char kUnknown[] = "unknown"; |
| 34 | 35 |
| 35 const char kSocketNotFoundError[] = "Socket not found"; | 36 const char kSocketNotFoundError[] = "Socket not found"; |
| 36 const char kSocketTypeInvalidError[] = "Socket type is not supported"; | 37 const char kSocketTypeInvalidError[] = "Socket type is not supported"; |
| 37 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 38 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
| 38 const char kPermissionError[] = "Caller does not have permission"; | 39 const char kPermissionError[] = "Caller does not have permission"; |
| 40 const char kExperimentalPermissionError[] = | |
| 41 "Caller does not have permission for experimental api"; | |
| 39 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 42 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
| 43 const char kTCPSocketBindError[] = | |
| 44 "TCP socket does not support bind. For TCP server please use listen."; | |
| 40 | 45 |
| 41 SocketAsyncApiFunction::SocketAsyncApiFunction() | 46 SocketAsyncApiFunction::SocketAsyncApiFunction() |
| 42 : manager_(NULL) { | 47 : manager_(NULL) { |
| 43 } | 48 } |
| 44 | 49 |
| 45 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 50 SocketAsyncApiFunction::~SocketAsyncApiFunction() { |
| 46 } | 51 } |
| 47 | 52 |
| 48 bool SocketAsyncApiFunction::PrePrepare() { | 53 bool SocketAsyncApiFunction::PrePrepare() { |
| 49 manager_ = ExtensionSystem::Get(profile())->socket_manager(); | 54 manager_ = ExtensionSystem::Get(profile())->socket_manager(); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 | 267 |
| 263 if (socket->GetSocketType() == Socket::TYPE_UDP) { | 268 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 264 SocketPermission::CheckParam param( | 269 SocketPermission::CheckParam param( |
| 265 SocketPermissionData::UDP_BIND, address_, port_); | 270 SocketPermissionData::UDP_BIND, address_, port_); |
| 266 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | 271 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 267 ¶m)) { | 272 ¶m)) { |
| 268 error_ = kPermissionError; | 273 error_ = kPermissionError; |
| 269 SetResult(Value::CreateIntegerValue(result)); | 274 SetResult(Value::CreateIntegerValue(result)); |
| 270 return; | 275 return; |
| 271 } | 276 } |
| 277 } else if (socket->GetSocketType() == Socket::TYPE_TCP) { | |
| 278 error_ = kTCPSocketBindError; | |
| 279 SetResult(Value::CreateIntegerValue(result)); | |
| 280 return; | |
| 272 } | 281 } |
| 273 | 282 |
| 274 result = socket->Bind(address_, port_); | 283 result = socket->Bind(address_, port_); |
| 275 SetResult(Value::CreateIntegerValue(result)); | 284 SetResult(Value::CreateIntegerValue(result)); |
| 276 } | 285 } |
| 277 | 286 |
| 287 SocketListenFunction::SocketListenFunction() | |
| 288 : params_(NULL) { | |
| 289 } | |
| 290 | |
| 291 SocketListenFunction::~SocketListenFunction() {} | |
| 292 | |
| 293 bool SocketListenFunction::Prepare() { | |
| 294 params_ = api::socket::Listen::Params::Create(*args_); | |
| 295 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
| 296 return true; | |
| 297 } | |
| 298 | |
| 299 void SocketListenFunction::Work() { | |
| 300 int result = -1; | |
| 301 | |
| 302 if (!GetExtension()->HasAPIPermission(APIPermission::kExperimental)) { | |
| 303 error_ = kExperimentalPermissionError; | |
| 304 SetResult(Value::CreateIntegerValue(result)); | |
|
miket_OOO
2012/09/26 17:17:55
Excellent. Could this happen in Prepare()? Not a b
justinlin
2012/09/26 20:53:12
Done.
| |
| 305 return; | |
| 306 } | |
| 307 | |
| 308 Socket* socket = GetSocket(params_->socket_id); | |
| 309 if (socket) { | |
| 310 SocketPermission::CheckParam param( | |
| 311 SocketPermissionData::TCP_LISTEN, params_->address, params_->port); | |
| 312 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | |
| 313 ¶m)) { | |
| 314 error_ = kPermissionError; | |
| 315 SetResult(Value::CreateIntegerValue(result)); | |
| 316 return; | |
| 317 } | |
| 318 | |
| 319 result = socket->Listen( | |
| 320 params_->address, | |
| 321 params_->port, | |
| 322 params_->backlog.get() ? *params_->backlog.get() : 5, | |
| 323 &error_); | |
| 324 } else { | |
| 325 error_ = kSocketNotFoundError; | |
| 326 } | |
| 327 | |
| 328 SetResult(Value::CreateIntegerValue(result)); | |
| 329 } | |
| 330 | |
| 331 SocketAcceptFunction::SocketAcceptFunction() | |
| 332 : params_(NULL) { | |
| 333 } | |
| 334 | |
| 335 SocketAcceptFunction::~SocketAcceptFunction() {} | |
| 336 | |
| 337 bool SocketAcceptFunction::Prepare() { | |
| 338 params_ = api::socket::Accept::Params::Create(*args_); | |
| 339 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
| 340 return true; | |
| 341 } | |
| 342 | |
| 343 void SocketAcceptFunction::AsyncWorkStart() { | |
| 344 if (!GetExtension()->HasAPIPermission(APIPermission::kExperimental)) { | |
| 345 error_ = kExperimentalPermissionError; | |
|
miket_OOO
2012/09/26 17:17:55
Same.
justinlin
2012/09/26 20:53:12
Done.
| |
| 346 SetResult(Value::CreateIntegerValue(-1)); | |
| 347 AsyncWorkCompleted(); | |
| 348 return; | |
| 349 } | |
| 350 | |
| 351 Socket* socket = GetSocket(params_->socket_id); | |
| 352 if (socket) { | |
| 353 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); | |
| 354 } else { | |
| 355 error_ = kSocketNotFoundError; | |
| 356 OnAccept(-1, NULL); | |
| 357 } | |
| 358 } | |
| 359 | |
| 360 void SocketAcceptFunction::OnAccept(int result_code, | |
| 361 net::TCPClientSocket *socket) { | |
| 362 DCHECK(socket); | |
| 363 // TODO(justinlin): This socket won't have an event notifier, but it's not | |
| 364 // used for anything right now. | |
| 365 Socket *client_socket = new TCPSocket(socket, extension_id(), NULL, true); | |
| 366 | |
| 367 DictionaryValue* result = new DictionaryValue(); | |
| 368 result->SetInteger(kResultCodeKey, result_code); | |
| 369 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); | |
| 370 SetResult(result); | |
| 371 | |
| 372 AsyncWorkCompleted(); | |
| 373 } | |
| 374 | |
| 278 SocketReadFunction::SocketReadFunction() | 375 SocketReadFunction::SocketReadFunction() |
| 279 : params_(NULL) { | 376 : params_(NULL) { |
| 280 } | 377 } |
| 281 | 378 |
| 282 SocketReadFunction::~SocketReadFunction() {} | 379 SocketReadFunction::~SocketReadFunction() {} |
| 283 | 380 |
| 284 bool SocketReadFunction::Prepare() { | 381 bool SocketReadFunction::Prepare() { |
| 285 params_ = api::socket::Read::Params::Create(*args_); | 382 params_ = api::socket::Read::Params::Create(*args_); |
| 286 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 383 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 287 return true; | 384 return true; |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 612 info->name = i->name; | 709 info->name = i->name; |
| 613 info->address = net::IPAddressToString(i->address); | 710 info->address = net::IPAddressToString(i->address); |
| 614 create_arg.push_back(info); | 711 create_arg.push_back(info); |
| 615 } | 712 } |
| 616 | 713 |
| 617 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 714 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
| 618 SendResponse(true); | 715 SendResponse(true); |
| 619 } | 716 } |
| 620 | 717 |
| 621 } // namespace extensions | 718 } // namespace extensions |
| OLD | NEW |