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"; | |
Mihai Parparita -not on Chrome
2012/09/26 21:50:33
Nit: Capitalize API.
Also, for both this and kPer
justinlin
2012/09/26 23:03:57
Done.
| |
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 if (!GetExtension()->HasAPIPermission(APIPermission::kExperimental)) { | |
295 error_ = kExperimentalPermissionError; | |
296 return false; | |
297 } | |
298 | |
299 params_ = api::socket::Listen::Params::Create(*args_); | |
300 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
301 return true; | |
302 } | |
303 | |
304 void SocketListenFunction::Work() { | |
305 int result = -1; | |
306 | |
307 Socket* socket = GetSocket(params_->socket_id); | |
308 if (socket) { | |
309 SocketPermission::CheckParam param( | |
310 SocketPermissionData::TCP_LISTEN, params_->address, params_->port); | |
311 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | |
312 ¶m)) { | |
313 error_ = kPermissionError; | |
314 SetResult(Value::CreateIntegerValue(result)); | |
315 return; | |
316 } | |
317 | |
318 result = socket->Listen( | |
319 params_->address, | |
320 params_->port, | |
321 params_->backlog.get() ? *params_->backlog.get() : 5, | |
322 &error_); | |
323 } else { | |
324 error_ = kSocketNotFoundError; | |
325 } | |
326 | |
327 SetResult(Value::CreateIntegerValue(result)); | |
328 } | |
329 | |
330 SocketAcceptFunction::SocketAcceptFunction() | |
331 : params_(NULL) { | |
332 } | |
333 | |
334 SocketAcceptFunction::~SocketAcceptFunction() {} | |
335 | |
336 bool SocketAcceptFunction::Prepare() { | |
337 if (!GetExtension()->HasAPIPermission(APIPermission::kExperimental)) { | |
338 error_ = kExperimentalPermissionError; | |
miket_OOO
2012/09/26 21:02:44
Nice.
| |
339 return false; | |
340 } | |
341 | |
342 params_ = api::socket::Accept::Params::Create(*args_); | |
343 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
344 return true; | |
345 } | |
346 | |
347 void SocketAcceptFunction::AsyncWorkStart() { | |
348 Socket* socket = GetSocket(params_->socket_id); | |
349 if (socket) { | |
350 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); | |
351 } else { | |
352 error_ = kSocketNotFoundError; | |
353 OnAccept(-1, NULL); | |
354 } | |
355 } | |
356 | |
357 void SocketAcceptFunction::OnAccept(int result_code, | |
358 net::TCPClientSocket *socket) { | |
359 DCHECK(socket); | |
360 | |
361 DictionaryValue* result = new DictionaryValue(); | |
362 result->SetInteger(kResultCodeKey, result_code); | |
363 if (socket) { | |
364 // TODO(justinlin): This socket won't have an event notifier, but it's not | |
365 // used for anything right now. | |
366 Socket *client_socket = new TCPSocket(socket, extension_id(), NULL, true); | |
367 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); | |
368 } | |
369 SetResult(result); | |
370 | |
371 AsyncWorkCompleted(); | |
372 } | |
373 | |
278 SocketReadFunction::SocketReadFunction() | 374 SocketReadFunction::SocketReadFunction() |
279 : params_(NULL) { | 375 : params_(NULL) { |
280 } | 376 } |
281 | 377 |
282 SocketReadFunction::~SocketReadFunction() {} | 378 SocketReadFunction::~SocketReadFunction() {} |
283 | 379 |
284 bool SocketReadFunction::Prepare() { | 380 bool SocketReadFunction::Prepare() { |
285 params_ = api::socket::Read::Params::Create(*args_); | 381 params_ = api::socket::Read::Params::Create(*args_); |
286 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 382 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
287 return true; | 383 return true; |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
612 info->name = i->name; | 708 info->name = i->name; |
613 info->address = net::IPAddressToString(i->address); | 709 info->address = net::IPAddressToString(i->address); |
614 create_arg.push_back(info); | 710 create_arg.push_back(info); |
615 } | 711 } |
616 | 712 |
617 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 713 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
618 SendResponse(true); | 714 SendResponse(true); |
619 } | 715 } |
620 | 716 |
621 } // namespace extensions | 717 } // namespace extensions |
OLD | NEW |