Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: chrome/browser/extensions/api/socket/socket_api.cc

Issue 10827390: Implement chrome.socket.bind/listen/accept for TCP server socket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Revert net::socket change, check permissions for listen. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/permissions/socket_permission.h" 8 #include "chrome/common/extensions/permissions/socket_permission.h"
9 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h"
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 error_ = kPermissionError; 268 error_ = kPermissionError;
269 SetResult(Value::CreateIntegerValue(result)); 269 SetResult(Value::CreateIntegerValue(result));
270 return; 270 return;
271 } 271 }
272 } 272 }
273 273
274 result = socket->Bind(address_, port_); 274 result = socket->Bind(address_, port_);
275 SetResult(Value::CreateIntegerValue(result)); 275 SetResult(Value::CreateIntegerValue(result));
276 } 276 }
277 277
278 SocketListenFunction::SocketListenFunction()
279 : params_(NULL) {
280 }
281
282 SocketListenFunction::~SocketListenFunction() {}
283
284 bool SocketListenFunction::Prepare() {
285 params_ = api::experimental_socket::Listen::Params::Create(*args_);
286 EXTENSION_FUNCTION_VALIDATE(params_.get());
287 return true;
288 }
289
290 void SocketListenFunction::Work() {
291 int result = -1;
292 net::IPEndPoint bindAddress;
293
294 Socket* socket = GetSocket(params_->socket_id);
295 if (socket && socket->GetBindAddress(&bindAddress)) {
Peng 2012/09/14 16:27:16 The permissions for bind and listen are udp-bind:i
296 std::string address;
297 int port;
298 Socket::IPEndPointToStringAndPort(bindAddress, &address, &port);
299 SocketPermission::CheckParam param(
300 SocketPermissionData::TCP_LISTEN, address, port);
301 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket,
302 &param)) {
303 error_ = kPermissionError;
304 SetResult(Value::CreateIntegerValue(result));
305 return;
306 }
307
308 result = socket->Listen(params_->backlog, &error_);
309 } else {
310 error_ = kSocketNotFoundError;
311 }
312
313 SetResult(Value::CreateIntegerValue(result));
314 }
315
316 SocketAcceptFunction::SocketAcceptFunction()
317 : params_(NULL) {
318 }
319
320 SocketAcceptFunction::~SocketAcceptFunction() {}
321
322 bool SocketAcceptFunction::Prepare() {
323 params_ = api::experimental_socket::Accept::Params::Create(*args_);
324 EXTENSION_FUNCTION_VALIDATE(params_.get());
325 return true;
326 }
327
328 void SocketAcceptFunction::AsyncWorkStart() {
329 Socket* socket = GetSocket(params_->socket_id);
330 if (socket) {
331 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this));
332 } else {
333 error_ = kSocketNotFoundError;
334 OnAccept(-1, NULL);
335 }
336 }
337
338 void SocketAcceptFunction::OnAccept(int result_code,
339 net::TCPClientSocket *socket) {
340 DCHECK(socket);
341 // TODO(justinlin): This socket won't have an event notifier, but it's not
342 // used for anything right now.
343 Socket *client_socket = new TCPSocket(socket, extension_id(), NULL, true);
344
345 DictionaryValue* result = new DictionaryValue();
346 result->SetInteger(kResultCodeKey, result_code);
347 result->SetInteger(kSocketIdKey, manager_->Add(client_socket));
348 SetResult(result);
349
350 AsyncWorkCompleted();
351 }
352
278 SocketReadFunction::SocketReadFunction() 353 SocketReadFunction::SocketReadFunction()
279 : params_(NULL) { 354 : params_(NULL) {
280 } 355 }
281 356
282 SocketReadFunction::~SocketReadFunction() {} 357 SocketReadFunction::~SocketReadFunction() {}
283 358
284 bool SocketReadFunction::Prepare() { 359 bool SocketReadFunction::Prepare() {
285 params_ = api::socket::Read::Params::Create(*args_); 360 params_ = api::socket::Read::Params::Create(*args_);
286 EXTENSION_FUNCTION_VALIDATE(params_.get()); 361 EXTENSION_FUNCTION_VALIDATE(params_.get());
287 return true; 362 return true;
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 info->name = i->name; 687 info->name = i->name;
613 info->address = net::IPAddressToString(i->address); 688 info->address = net::IPAddressToString(i->address);
614 create_arg.push_back(info); 689 create_arg.push_back(info);
615 } 690 }
616 691
617 results_ = api::socket::GetNetworkList::Results::Create(create_arg); 692 results_ = api::socket::GetNetworkList::Results::Create(create_arg);
618 SendResponse(true); 693 SendResponse(true);
619 } 694 }
620 695
621 } // namespace extensions 696 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/socket/socket_api.h ('k') | chrome/browser/extensions/api/socket/socket_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698