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" |
18 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
20 #include "net/base/net_log.h" | 21 #include "net/base/net_log.h" |
21 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
22 | 23 |
23 namespace extensions { | 24 namespace extensions { |
24 | 25 |
25 const char kAddressKey[] = "address"; | 26 const char kAddressKey[] = "address"; |
26 const char kPortKey[] = "port"; | 27 const char kPortKey[] = "port"; |
27 const char kBytesWrittenKey[] = "bytesWritten"; | 28 const char kBytesWrittenKey[] = "bytesWritten"; |
28 const char kDataKey[] = "data"; | 29 const char kDataKey[] = "data"; |
29 const char kResultCodeKey[] = "resultCode"; | 30 const char kResultCodeKey[] = "resultCode"; |
30 const char kSocketIdKey[] = "socketId"; | 31 const char kSocketIdKey[] = "socketId"; |
31 | 32 |
32 const char kSocketNotFoundError[] = "Socket not found"; | 33 const char kSocketNotFoundError[] = "Socket not found"; |
33 const char kSocketTypeInvalidError[] = "Socket type is not supported"; | 34 const char kSocketTypeInvalidError[] = "Socket type is not supported"; |
34 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 35 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
35 const char kPermissionError[] = "Caller does not have permission"; | 36 const char kPermissionError[] = "App does not have permission"; |
| 37 const char kExperimentalPermissionError[] = |
| 38 "App does not have permission for experimental API"; |
36 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 39 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
| 40 const char kTCPSocketBindError[] = |
| 41 "TCP socket does not support bind. For TCP server please use listen."; |
37 | 42 |
38 SocketAsyncApiFunction::SocketAsyncApiFunction() | 43 SocketAsyncApiFunction::SocketAsyncApiFunction() |
39 : manager_(NULL) { | 44 : manager_(NULL) { |
40 } | 45 } |
41 | 46 |
42 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 47 SocketAsyncApiFunction::~SocketAsyncApiFunction() { |
43 } | 48 } |
44 | 49 |
45 bool SocketAsyncApiFunction::PrePrepare() { | 50 bool SocketAsyncApiFunction::PrePrepare() { |
46 manager_ = ExtensionSystem::Get(profile())->socket_manager(); | 51 manager_ = ExtensionSystem::Get(profile())->socket_manager(); |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 | 268 |
264 if (socket->GetSocketType() == Socket::TYPE_UDP) { | 269 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
265 SocketPermission::CheckParam param( | 270 SocketPermission::CheckParam param( |
266 SocketPermissionData::UDP_BIND, address_, port_); | 271 SocketPermissionData::UDP_BIND, address_, port_); |
267 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | 272 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
268 ¶m)) { | 273 ¶m)) { |
269 error_ = kPermissionError; | 274 error_ = kPermissionError; |
270 SetResult(Value::CreateIntegerValue(result)); | 275 SetResult(Value::CreateIntegerValue(result)); |
271 return; | 276 return; |
272 } | 277 } |
| 278 } else if (socket->GetSocketType() == Socket::TYPE_TCP) { |
| 279 error_ = kTCPSocketBindError; |
| 280 SetResult(Value::CreateIntegerValue(result)); |
| 281 return; |
273 } | 282 } |
274 | 283 |
275 result = socket->Bind(address_, port_); | 284 result = socket->Bind(address_, port_); |
276 SetResult(Value::CreateIntegerValue(result)); | 285 SetResult(Value::CreateIntegerValue(result)); |
277 } | 286 } |
278 | 287 |
| 288 SocketListenFunction::SocketListenFunction() |
| 289 : params_(NULL) { |
| 290 } |
| 291 |
| 292 SocketListenFunction::~SocketListenFunction() {} |
| 293 |
| 294 bool SocketListenFunction::Prepare() { |
| 295 if (!GetExtension()->HasAPIPermission(APIPermission::kExperimental)) { |
| 296 error_ = kExperimentalPermissionError; |
| 297 return false; |
| 298 } |
| 299 |
| 300 params_ = api::socket::Listen::Params::Create(*args_); |
| 301 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 302 return true; |
| 303 } |
| 304 |
| 305 void SocketListenFunction::Work() { |
| 306 int result = -1; |
| 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 if (!GetExtension()->HasAPIPermission(APIPermission::kExperimental)) { |
| 339 error_ = kExperimentalPermissionError; |
| 340 return false; |
| 341 } |
| 342 |
| 343 params_ = api::socket::Accept::Params::Create(*args_); |
| 344 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 345 return true; |
| 346 } |
| 347 |
| 348 void SocketAcceptFunction::AsyncWorkStart() { |
| 349 Socket* socket = GetSocket(params_->socket_id); |
| 350 if (socket) { |
| 351 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); |
| 352 } else { |
| 353 error_ = kSocketNotFoundError; |
| 354 OnAccept(-1, NULL); |
| 355 } |
| 356 } |
| 357 |
| 358 void SocketAcceptFunction::OnAccept(int result_code, |
| 359 net::TCPClientSocket *socket) { |
| 360 DictionaryValue* result = new DictionaryValue(); |
| 361 result->SetInteger(kResultCodeKey, result_code); |
| 362 if (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 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); |
| 367 } |
| 368 SetResult(result); |
| 369 |
| 370 AsyncWorkCompleted(); |
| 371 } |
| 372 |
279 SocketReadFunction::SocketReadFunction() | 373 SocketReadFunction::SocketReadFunction() |
280 : params_(NULL) { | 374 : params_(NULL) { |
281 } | 375 } |
282 | 376 |
283 SocketReadFunction::~SocketReadFunction() {} | 377 SocketReadFunction::~SocketReadFunction() {} |
284 | 378 |
285 bool SocketReadFunction::Prepare() { | 379 bool SocketReadFunction::Prepare() { |
286 params_ = api::socket::Read::Params::Create(*args_); | 380 params_ = api::socket::Read::Params::Create(*args_); |
287 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 381 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
288 return true; | 382 return true; |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 info->name = i->name; | 699 info->name = i->name; |
606 info->address = net::IPAddressToString(i->address); | 700 info->address = net::IPAddressToString(i->address); |
607 create_arg.push_back(info); | 701 create_arg.push_back(info); |
608 } | 702 } |
609 | 703 |
610 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 704 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
611 SendResponse(true); | 705 SendResponse(true); |
612 } | 706 } |
613 | 707 |
614 } // namespace extensions | 708 } // namespace extensions |
OLD | NEW |