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

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

Issue 1991083002: Remove ExtensionFunction::SetResult(T*) overload. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: IWYU Created 4 years, 7 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/browser/api/socket/socket_api.h" 5 #include "extensions/browser/api/socket/socket_api.h"
6 6
7 #include <memory>
7 #include <utility> 8 #include <utility>
8 #include <vector> 9 #include <vector>
9 10
10 #include "base/bind.h" 11 #include "base/bind.h"
11 #include "base/containers/hash_tables.h" 12 #include "base/containers/hash_tables.h"
13 #include "base/memory/ptr_util.h"
12 #include "build/build_config.h" 14 #include "build/build_config.h"
13 #include "content/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/resource_context.h" 16 #include "content/public/browser/resource_context.h"
15 #include "content/public/browser/storage_partition.h" 17 #include "content/public/browser/storage_partition.h"
16 #include "extensions/browser/api/dns/host_resolver_wrapper.h" 18 #include "extensions/browser/api/dns/host_resolver_wrapper.h"
17 #include "extensions/browser/api/socket/socket.h" 19 #include "extensions/browser/api/socket/socket.h"
18 #include "extensions/browser/api/socket/tcp_socket.h" 20 #include "extensions/browser/api/socket/tcp_socket.h"
19 #include "extensions/browser/api/socket/tls_socket.h" 21 #include "extensions/browser/api/socket/tls_socket.h"
20 #include "extensions/browser/api/socket/udp_socket.h" 22 #include "extensions/browser/api/socket/udp_socket.h"
21 #include "extensions/browser/extension_system.h" 23 #include "extensions/browser/extension_system.h"
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 106
105 void SocketAsyncApiFunction::OpenFirewallHole(const std::string& address, 107 void SocketAsyncApiFunction::OpenFirewallHole(const std::string& address,
106 int socket_id, 108 int socket_id,
107 Socket* socket) { 109 Socket* socket) {
108 #if defined(OS_CHROMEOS) 110 #if defined(OS_CHROMEOS)
109 if (!net::IsLocalhost(address)) { 111 if (!net::IsLocalhost(address)) {
110 net::IPEndPoint local_address; 112 net::IPEndPoint local_address;
111 if (!socket->GetLocalAddress(&local_address)) { 113 if (!socket->GetLocalAddress(&local_address)) {
112 NOTREACHED() << "Cannot get address of recently bound socket."; 114 NOTREACHED() << "Cannot get address of recently bound socket.";
113 error_ = kFirewallFailure; 115 error_ = kFirewallFailure;
114 SetResult(new base::FundamentalValue(-1)); 116 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
115 AsyncWorkCompleted(); 117 AsyncWorkCompleted();
116 return; 118 return;
117 } 119 }
118 120
119 AppFirewallHole::PortType type = socket->GetSocketType() == Socket::TYPE_TCP 121 AppFirewallHole::PortType type = socket->GetSocketType() == Socket::TYPE_TCP
120 ? AppFirewallHole::PortType::TCP 122 ? AppFirewallHole::PortType::TCP
121 : AppFirewallHole::PortType::UDP; 123 : AppFirewallHole::PortType::UDP;
122 124
123 BrowserThread::PostTask( 125 BrowserThread::PostTask(
124 BrowserThread::UI, FROM_HERE, 126 BrowserThread::UI, FROM_HERE,
(...skipping 21 matching lines...) Expand all
146 base::Bind(&SocketAsyncApiFunction::OnFirewallHoleOpened, this, socket_id, 148 base::Bind(&SocketAsyncApiFunction::OnFirewallHoleOpened, this, socket_id,
147 base::Passed(&hole))); 149 base::Passed(&hole)));
148 } 150 }
149 151
150 void SocketAsyncApiFunction::OnFirewallHoleOpened( 152 void SocketAsyncApiFunction::OnFirewallHoleOpened(
151 int socket_id, 153 int socket_id,
152 std::unique_ptr<AppFirewallHole, BrowserThread::DeleteOnUIThread> hole) { 154 std::unique_ptr<AppFirewallHole, BrowserThread::DeleteOnUIThread> hole) {
153 DCHECK_CURRENTLY_ON(BrowserThread::IO); 155 DCHECK_CURRENTLY_ON(BrowserThread::IO);
154 if (!hole) { 156 if (!hole) {
155 error_ = kFirewallFailure; 157 error_ = kFirewallFailure;
156 SetResult(new base::FundamentalValue(-1)); 158 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
157 AsyncWorkCompleted(); 159 AsyncWorkCompleted();
158 return; 160 return;
159 } 161 }
160 162
161 Socket* socket = GetSocket(socket_id); 163 Socket* socket = GetSocket(socket_id);
162 if (!socket) { 164 if (!socket) {
163 error_ = kSocketNotFoundError; 165 error_ = kSocketNotFoundError;
164 SetResult(new base::FundamentalValue(-1)); 166 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
165 AsyncWorkCompleted(); 167 AsyncWorkCompleted();
166 return; 168 return;
167 } 169 }
168 170
169 socket->set_firewall_hole(std::move(hole)); 171 socket->set_firewall_hole(std::move(hole));
170 AsyncWorkCompleted(); 172 AsyncWorkCompleted();
171 } 173 }
172 174
173 #endif // OS_CHROMEOS 175 #endif // OS_CHROMEOS
174 176
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 242
241 void SocketCreateFunction::Work() { 243 void SocketCreateFunction::Work() {
242 Socket* socket = NULL; 244 Socket* socket = NULL;
243 if (socket_type_ == kSocketTypeTCP) { 245 if (socket_type_ == kSocketTypeTCP) {
244 socket = new TCPSocket(extension_->id()); 246 socket = new TCPSocket(extension_->id());
245 } else if (socket_type_ == kSocketTypeUDP) { 247 } else if (socket_type_ == kSocketTypeUDP) {
246 socket = new UDPSocket(extension_->id()); 248 socket = new UDPSocket(extension_->id());
247 } 249 }
248 DCHECK(socket); 250 DCHECK(socket);
249 251
250 base::DictionaryValue* result = new base::DictionaryValue(); 252 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
251 result->SetInteger(kSocketIdKey, AddSocket(socket)); 253 result->SetInteger(kSocketIdKey, AddSocket(socket));
252 SetResult(result); 254 SetResult(std::move(result));
253 } 255 }
254 256
255 bool SocketDestroyFunction::Prepare() { 257 bool SocketDestroyFunction::Prepare() {
256 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 258 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
257 return true; 259 return true;
258 } 260 }
259 261
260 void SocketDestroyFunction::Work() { RemoveSocket(socket_id_); } 262 void SocketDestroyFunction::Work() { RemoveSocket(socket_id_); }
261 263
262 SocketConnectFunction::SocketConnectFunction() 264 SocketConnectFunction::SocketConnectFunction()
263 : socket_id_(0), hostname_(), port_(0) { 265 : socket_id_(0), hostname_(), port_(0) {
264 } 266 }
265 267
266 SocketConnectFunction::~SocketConnectFunction() {} 268 SocketConnectFunction::~SocketConnectFunction() {}
267 269
268 bool SocketConnectFunction::Prepare() { 270 bool SocketConnectFunction::Prepare() {
269 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 271 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
270 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); 272 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_));
271 int port; 273 int port;
272 EXTENSION_FUNCTION_VALIDATE( 274 EXTENSION_FUNCTION_VALIDATE(
273 args_->GetInteger(2, &port) && port >= 0 && port <= 65535); 275 args_->GetInteger(2, &port) && port >= 0 && port <= 65535);
274 port_ = static_cast<uint16_t>(port); 276 port_ = static_cast<uint16_t>(port);
275 return true; 277 return true;
276 } 278 }
277 279
278 void SocketConnectFunction::AsyncWorkStart() { 280 void SocketConnectFunction::AsyncWorkStart() {
279 Socket* socket = GetSocket(socket_id_); 281 Socket* socket = GetSocket(socket_id_);
280 if (!socket) { 282 if (!socket) {
281 error_ = kSocketNotFoundError; 283 error_ = kSocketNotFoundError;
282 SetResult(new base::FundamentalValue(-1)); 284 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
283 AsyncWorkCompleted(); 285 AsyncWorkCompleted();
284 return; 286 return;
285 } 287 }
286 288
287 socket->set_hostname(hostname_); 289 socket->set_hostname(hostname_);
288 290
289 SocketPermissionRequest::OperationType operation_type; 291 SocketPermissionRequest::OperationType operation_type;
290 switch (socket->GetSocketType()) { 292 switch (socket->GetSocketType()) {
291 case Socket::TYPE_TCP: 293 case Socket::TYPE_TCP:
292 operation_type = SocketPermissionRequest::TCP_CONNECT; 294 operation_type = SocketPermissionRequest::TCP_CONNECT;
293 break; 295 break;
294 case Socket::TYPE_UDP: 296 case Socket::TYPE_UDP:
295 operation_type = SocketPermissionRequest::UDP_SEND_TO; 297 operation_type = SocketPermissionRequest::UDP_SEND_TO;
296 break; 298 break;
297 default: 299 default:
298 NOTREACHED() << "Unknown socket type."; 300 NOTREACHED() << "Unknown socket type.";
299 operation_type = SocketPermissionRequest::NONE; 301 operation_type = SocketPermissionRequest::NONE;
300 break; 302 break;
301 } 303 }
302 304
303 SocketPermission::CheckParam param(operation_type, hostname_, port_); 305 SocketPermission::CheckParam param(operation_type, hostname_, port_);
304 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( 306 if (!extension()->permissions_data()->CheckAPIPermissionWithParam(
305 APIPermission::kSocket, &param)) { 307 APIPermission::kSocket, &param)) {
306 error_ = kPermissionError; 308 error_ = kPermissionError;
307 SetResult(new base::FundamentalValue(-1)); 309 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
308 AsyncWorkCompleted(); 310 AsyncWorkCompleted();
309 return; 311 return;
310 } 312 }
311 313
312 StartDnsLookup(net::HostPortPair(hostname_, port_)); 314 StartDnsLookup(net::HostPortPair(hostname_, port_));
313 } 315 }
314 316
315 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { 317 void SocketConnectFunction::AfterDnsLookup(int lookup_result) {
316 if (lookup_result == net::OK) { 318 if (lookup_result == net::OK) {
317 StartConnect(); 319 StartConnect();
318 } else { 320 } else {
319 SetResult(new base::FundamentalValue(lookup_result)); 321 SetResult(base::MakeUnique<base::FundamentalValue>(lookup_result));
320 AsyncWorkCompleted(); 322 AsyncWorkCompleted();
321 } 323 }
322 } 324 }
323 325
324 void SocketConnectFunction::StartConnect() { 326 void SocketConnectFunction::StartConnect() {
325 Socket* socket = GetSocket(socket_id_); 327 Socket* socket = GetSocket(socket_id_);
326 if (!socket) { 328 if (!socket) {
327 error_ = kSocketNotFoundError; 329 error_ = kSocketNotFoundError;
328 SetResult(new base::FundamentalValue(-1)); 330 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
329 AsyncWorkCompleted(); 331 AsyncWorkCompleted();
330 return; 332 return;
331 } 333 }
332 334
333 socket->Connect(addresses_, 335 socket->Connect(addresses_,
334 base::Bind(&SocketConnectFunction::OnConnect, this)); 336 base::Bind(&SocketConnectFunction::OnConnect, this));
335 } 337 }
336 338
337 void SocketConnectFunction::OnConnect(int result) { 339 void SocketConnectFunction::OnConnect(int result) {
338 SetResult(new base::FundamentalValue(result)); 340 SetResult(base::MakeUnique<base::FundamentalValue>(result));
339 AsyncWorkCompleted(); 341 AsyncWorkCompleted();
340 } 342 }
341 343
342 bool SocketDisconnectFunction::Prepare() { 344 bool SocketDisconnectFunction::Prepare() {
343 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 345 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
344 return true; 346 return true;
345 } 347 }
346 348
347 void SocketDisconnectFunction::Work() { 349 void SocketDisconnectFunction::Work() {
348 Socket* socket = GetSocket(socket_id_); 350 Socket* socket = GetSocket(socket_id_);
(...skipping 11 matching lines...) Expand all
360 EXTENSION_FUNCTION_VALIDATE( 362 EXTENSION_FUNCTION_VALIDATE(
361 args_->GetInteger(2, &port) && port >= 0 && port <= 65535); 363 args_->GetInteger(2, &port) && port >= 0 && port <= 65535);
362 port_ = static_cast<uint16_t>(port); 364 port_ = static_cast<uint16_t>(port);
363 return true; 365 return true;
364 } 366 }
365 367
366 void SocketBindFunction::AsyncWorkStart() { 368 void SocketBindFunction::AsyncWorkStart() {
367 Socket* socket = GetSocket(socket_id_); 369 Socket* socket = GetSocket(socket_id_);
368 if (!socket) { 370 if (!socket) {
369 error_ = kSocketNotFoundError; 371 error_ = kSocketNotFoundError;
370 SetResult(new base::FundamentalValue(-1)); 372 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
371 AsyncWorkCompleted(); 373 AsyncWorkCompleted();
372 return; 374 return;
373 } 375 }
374 376
375 if (socket->GetSocketType() == Socket::TYPE_TCP) { 377 if (socket->GetSocketType() == Socket::TYPE_TCP) {
376 error_ = kTCPSocketBindError; 378 error_ = kTCPSocketBindError;
377 SetResult(new base::FundamentalValue(-1)); 379 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
378 AsyncWorkCompleted(); 380 AsyncWorkCompleted();
379 return; 381 return;
380 } 382 }
381 383
382 CHECK(socket->GetSocketType() == Socket::TYPE_UDP); 384 CHECK(socket->GetSocketType() == Socket::TYPE_UDP);
383 SocketPermission::CheckParam param(SocketPermissionRequest::UDP_BIND, 385 SocketPermission::CheckParam param(SocketPermissionRequest::UDP_BIND,
384 address_, port_); 386 address_, port_);
385 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( 387 if (!extension()->permissions_data()->CheckAPIPermissionWithParam(
386 APIPermission::kSocket, &param)) { 388 APIPermission::kSocket, &param)) {
387 error_ = kPermissionError; 389 error_ = kPermissionError;
388 SetResult(new base::FundamentalValue(-1)); 390 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
389 AsyncWorkCompleted(); 391 AsyncWorkCompleted();
390 return; 392 return;
391 } 393 }
392 394
393 int result = socket->Bind(address_, port_); 395 int result = socket->Bind(address_, port_);
394 SetResult(new base::FundamentalValue(result)); 396 SetResult(base::MakeUnique<base::FundamentalValue>(result));
395 if (result != net::OK) { 397 if (result != net::OK) {
396 AsyncWorkCompleted(); 398 AsyncWorkCompleted();
397 return; 399 return;
398 } 400 }
399 401
400 OpenFirewallHole(address_, socket_id_, socket); 402 OpenFirewallHole(address_, socket_id_, socket);
401 } 403 }
402 404
403 SocketListenFunction::SocketListenFunction() {} 405 SocketListenFunction::SocketListenFunction() {}
404 406
405 SocketListenFunction::~SocketListenFunction() {} 407 SocketListenFunction::~SocketListenFunction() {}
406 408
407 bool SocketListenFunction::Prepare() { 409 bool SocketListenFunction::Prepare() {
408 params_ = api::socket::Listen::Params::Create(*args_); 410 params_ = api::socket::Listen::Params::Create(*args_);
409 EXTENSION_FUNCTION_VALIDATE(params_.get()); 411 EXTENSION_FUNCTION_VALIDATE(params_.get());
410 return true; 412 return true;
411 } 413 }
412 414
413 void SocketListenFunction::AsyncWorkStart() { 415 void SocketListenFunction::AsyncWorkStart() {
414 Socket* socket = GetSocket(params_->socket_id); 416 Socket* socket = GetSocket(params_->socket_id);
415 if (!socket) { 417 if (!socket) {
416 error_ = kSocketNotFoundError; 418 error_ = kSocketNotFoundError;
417 SetResult(new base::FundamentalValue(-1)); 419 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
418 AsyncWorkCompleted(); 420 AsyncWorkCompleted();
419 return; 421 return;
420 } 422 }
421 423
422 SocketPermission::CheckParam param(SocketPermissionRequest::TCP_LISTEN, 424 SocketPermission::CheckParam param(SocketPermissionRequest::TCP_LISTEN,
423 params_->address, params_->port); 425 params_->address, params_->port);
424 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( 426 if (!extension()->permissions_data()->CheckAPIPermissionWithParam(
425 APIPermission::kSocket, &param)) { 427 APIPermission::kSocket, &param)) {
426 error_ = kPermissionError; 428 error_ = kPermissionError;
427 SetResult(new base::FundamentalValue(-1)); 429 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
428 AsyncWorkCompleted(); 430 AsyncWorkCompleted();
429 return; 431 return;
430 } 432 }
431 433
432 int result = socket->Listen( 434 int result = socket->Listen(
433 params_->address, params_->port, 435 params_->address, params_->port,
434 params_->backlog.get() ? *params_->backlog.get() : 5, &error_); 436 params_->backlog.get() ? *params_->backlog.get() : 5, &error_);
435 SetResult(new base::FundamentalValue(result)); 437 SetResult(base::MakeUnique<base::FundamentalValue>(result));
436 if (result != net::OK) { 438 if (result != net::OK) {
437 AsyncWorkCompleted(); 439 AsyncWorkCompleted();
438 return; 440 return;
439 } 441 }
440 442
441 OpenFirewallHole(params_->address, params_->socket_id, socket); 443 OpenFirewallHole(params_->address, params_->socket_id, socket);
442 } 444 }
443 445
444 SocketAcceptFunction::SocketAcceptFunction() {} 446 SocketAcceptFunction::SocketAcceptFunction() {}
445 447
(...skipping 11 matching lines...) Expand all
457 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); 459 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this));
458 } else { 460 } else {
459 error_ = kSocketNotFoundError; 461 error_ = kSocketNotFoundError;
460 OnAccept(-1, NULL); 462 OnAccept(-1, NULL);
461 } 463 }
462 } 464 }
463 465
464 void SocketAcceptFunction::OnAccept( 466 void SocketAcceptFunction::OnAccept(
465 int result_code, 467 int result_code,
466 std::unique_ptr<net::TCPClientSocket> socket) { 468 std::unique_ptr<net::TCPClientSocket> socket) {
467 base::DictionaryValue* result = new base::DictionaryValue(); 469 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
468 result->SetInteger(kResultCodeKey, result_code); 470 result->SetInteger(kResultCodeKey, result_code);
469 if (socket) { 471 if (socket) {
470 Socket* client_socket = 472 Socket* client_socket =
471 new TCPSocket(std::move(socket), extension_id(), true); 473 new TCPSocket(std::move(socket), extension_id(), true);
472 result->SetInteger(kSocketIdKey, AddSocket(client_socket)); 474 result->SetInteger(kSocketIdKey, AddSocket(client_socket));
473 } 475 }
474 SetResult(result); 476 SetResult(std::move(result));
475 477
476 AsyncWorkCompleted(); 478 AsyncWorkCompleted();
477 } 479 }
478 480
479 SocketReadFunction::SocketReadFunction() {} 481 SocketReadFunction::SocketReadFunction() {}
480 482
481 SocketReadFunction::~SocketReadFunction() {} 483 SocketReadFunction::~SocketReadFunction() {}
482 484
483 bool SocketReadFunction::Prepare() { 485 bool SocketReadFunction::Prepare() {
484 params_ = api::socket::Read::Params::Create(*args_); 486 params_ = api::socket::Read::Params::Create(*args_);
485 EXTENSION_FUNCTION_VALIDATE(params_.get()); 487 EXTENSION_FUNCTION_VALIDATE(params_.get());
486 return true; 488 return true;
487 } 489 }
488 490
489 void SocketReadFunction::AsyncWorkStart() { 491 void SocketReadFunction::AsyncWorkStart() {
490 Socket* socket = GetSocket(params_->socket_id); 492 Socket* socket = GetSocket(params_->socket_id);
491 if (!socket) { 493 if (!socket) {
492 error_ = kSocketNotFoundError; 494 error_ = kSocketNotFoundError;
493 OnCompleted(-1, NULL); 495 OnCompleted(-1, NULL);
494 return; 496 return;
495 } 497 }
496 498
497 socket->Read(params_->buffer_size.get() ? *params_->buffer_size.get() : 4096, 499 socket->Read(params_->buffer_size.get() ? *params_->buffer_size.get() : 4096,
498 base::Bind(&SocketReadFunction::OnCompleted, this)); 500 base::Bind(&SocketReadFunction::OnCompleted, this));
499 } 501 }
500 502
501 void SocketReadFunction::OnCompleted(int bytes_read, 503 void SocketReadFunction::OnCompleted(int bytes_read,
502 scoped_refptr<net::IOBuffer> io_buffer) { 504 scoped_refptr<net::IOBuffer> io_buffer) {
503 base::DictionaryValue* result = new base::DictionaryValue(); 505 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
504 result->SetInteger(kResultCodeKey, bytes_read); 506 result->SetInteger(kResultCodeKey, bytes_read);
505 if (bytes_read > 0) { 507 if (bytes_read > 0) {
506 result->Set(kDataKey, 508 result->Set(kDataKey,
507 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), 509 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(),
508 bytes_read)); 510 bytes_read));
509 } else { 511 } else {
510 result->Set(kDataKey, new base::BinaryValue()); 512 result->Set(kDataKey, new base::BinaryValue());
511 } 513 }
512 SetResult(result); 514 SetResult(std::move(result));
513 515
514 AsyncWorkCompleted(); 516 AsyncWorkCompleted();
515 } 517 }
516 518
517 SocketWriteFunction::SocketWriteFunction() 519 SocketWriteFunction::SocketWriteFunction()
518 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0) {} 520 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0) {}
519 521
520 SocketWriteFunction::~SocketWriteFunction() {} 522 SocketWriteFunction::~SocketWriteFunction() {}
521 523
522 bool SocketWriteFunction::Prepare() { 524 bool SocketWriteFunction::Prepare() {
(...skipping 14 matching lines...) Expand all
537 OnCompleted(-1); 539 OnCompleted(-1);
538 return; 540 return;
539 } 541 }
540 542
541 socket->Write(io_buffer_, 543 socket->Write(io_buffer_,
542 io_buffer_size_, 544 io_buffer_size_,
543 base::Bind(&SocketWriteFunction::OnCompleted, this)); 545 base::Bind(&SocketWriteFunction::OnCompleted, this));
544 } 546 }
545 547
546 void SocketWriteFunction::OnCompleted(int bytes_written) { 548 void SocketWriteFunction::OnCompleted(int bytes_written) {
547 base::DictionaryValue* result = new base::DictionaryValue(); 549 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
548 result->SetInteger(kBytesWrittenKey, bytes_written); 550 result->SetInteger(kBytesWrittenKey, bytes_written);
549 SetResult(result); 551 SetResult(std::move(result));
550 552
551 AsyncWorkCompleted(); 553 AsyncWorkCompleted();
552 } 554 }
553 555
554 SocketRecvFromFunction::SocketRecvFromFunction() {} 556 SocketRecvFromFunction::SocketRecvFromFunction() {}
555 557
556 SocketRecvFromFunction::~SocketRecvFromFunction() {} 558 SocketRecvFromFunction::~SocketRecvFromFunction() {}
557 559
558 bool SocketRecvFromFunction::Prepare() { 560 bool SocketRecvFromFunction::Prepare() {
559 params_ = api::socket::RecvFrom::Params::Create(*args_); 561 params_ = api::socket::RecvFrom::Params::Create(*args_);
(...skipping 10 matching lines...) Expand all
570 } 572 }
571 573
572 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, 574 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096,
573 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); 575 base::Bind(&SocketRecvFromFunction::OnCompleted, this));
574 } 576 }
575 577
576 void SocketRecvFromFunction::OnCompleted(int bytes_read, 578 void SocketRecvFromFunction::OnCompleted(int bytes_read,
577 scoped_refptr<net::IOBuffer> io_buffer, 579 scoped_refptr<net::IOBuffer> io_buffer,
578 const std::string& address, 580 const std::string& address,
579 uint16_t port) { 581 uint16_t port) {
580 base::DictionaryValue* result = new base::DictionaryValue(); 582 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
581 result->SetInteger(kResultCodeKey, bytes_read); 583 result->SetInteger(kResultCodeKey, bytes_read);
582 if (bytes_read > 0) { 584 if (bytes_read > 0) {
583 result->Set(kDataKey, 585 result->Set(kDataKey,
584 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), 586 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(),
585 bytes_read)); 587 bytes_read));
586 } else { 588 } else {
587 result->Set(kDataKey, new base::BinaryValue()); 589 result->Set(kDataKey, new base::BinaryValue());
588 } 590 }
589 result->SetString(kAddressKey, address); 591 result->SetString(kAddressKey, address);
590 result->SetInteger(kPortKey, port); 592 result->SetInteger(kPortKey, port);
591 SetResult(result); 593 SetResult(std::move(result));
592 594
593 AsyncWorkCompleted(); 595 AsyncWorkCompleted();
594 } 596 }
595 597
596 SocketSendToFunction::SocketSendToFunction() 598 SocketSendToFunction::SocketSendToFunction()
597 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0), port_(0) { 599 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0), port_(0) {
598 } 600 }
599 601
600 SocketSendToFunction::~SocketSendToFunction() {} 602 SocketSendToFunction::~SocketSendToFunction() {}
601 603
602 bool SocketSendToFunction::Prepare() { 604 bool SocketSendToFunction::Prepare() {
603 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 605 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
604 base::BinaryValue* data = NULL; 606 base::BinaryValue* data = NULL;
605 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); 607 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data));
606 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); 608 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_));
607 int port; 609 int port;
608 EXTENSION_FUNCTION_VALIDATE( 610 EXTENSION_FUNCTION_VALIDATE(
609 args_->GetInteger(3, &port) && port >= 0 && port <= 65535); 611 args_->GetInteger(3, &port) && port >= 0 && port <= 65535);
610 port_ = static_cast<uint16_t>(port); 612 port_ = static_cast<uint16_t>(port);
611 613
612 io_buffer_size_ = data->GetSize(); 614 io_buffer_size_ = data->GetSize();
613 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); 615 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer());
614 return true; 616 return true;
615 } 617 }
616 618
617 void SocketSendToFunction::AsyncWorkStart() { 619 void SocketSendToFunction::AsyncWorkStart() {
618 Socket* socket = GetSocket(socket_id_); 620 Socket* socket = GetSocket(socket_id_);
619 if (!socket) { 621 if (!socket) {
620 error_ = kSocketNotFoundError; 622 error_ = kSocketNotFoundError;
621 SetResult(new base::FundamentalValue(-1)); 623 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
622 AsyncWorkCompleted(); 624 AsyncWorkCompleted();
623 return; 625 return;
624 } 626 }
625 627
626 if (socket->GetSocketType() == Socket::TYPE_UDP) { 628 if (socket->GetSocketType() == Socket::TYPE_UDP) {
627 SocketPermission::CheckParam param( 629 SocketPermission::CheckParam param(
628 SocketPermissionRequest::UDP_SEND_TO, hostname_, port_); 630 SocketPermissionRequest::UDP_SEND_TO, hostname_, port_);
629 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( 631 if (!extension()->permissions_data()->CheckAPIPermissionWithParam(
630 APIPermission::kSocket, &param)) { 632 APIPermission::kSocket, &param)) {
631 error_ = kPermissionError; 633 error_ = kPermissionError;
632 SetResult(new base::FundamentalValue(-1)); 634 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
633 AsyncWorkCompleted(); 635 AsyncWorkCompleted();
634 return; 636 return;
635 } 637 }
636 } 638 }
637 639
638 StartDnsLookup(net::HostPortPair(hostname_, port_)); 640 StartDnsLookup(net::HostPortPair(hostname_, port_));
639 } 641 }
640 642
641 void SocketSendToFunction::AfterDnsLookup(int lookup_result) { 643 void SocketSendToFunction::AfterDnsLookup(int lookup_result) {
642 if (lookup_result == net::OK) { 644 if (lookup_result == net::OK) {
643 StartSendTo(); 645 StartSendTo();
644 } else { 646 } else {
645 SetResult(new base::FundamentalValue(lookup_result)); 647 SetResult(base::MakeUnique<base::FundamentalValue>(lookup_result));
646 AsyncWorkCompleted(); 648 AsyncWorkCompleted();
647 } 649 }
648 } 650 }
649 651
650 void SocketSendToFunction::StartSendTo() { 652 void SocketSendToFunction::StartSendTo() {
651 Socket* socket = GetSocket(socket_id_); 653 Socket* socket = GetSocket(socket_id_);
652 if (!socket) { 654 if (!socket) {
653 error_ = kSocketNotFoundError; 655 error_ = kSocketNotFoundError;
654 SetResult(new base::FundamentalValue(-1)); 656 SetResult(base::MakeUnique<base::FundamentalValue>(-1));
655 AsyncWorkCompleted(); 657 AsyncWorkCompleted();
656 return; 658 return;
657 } 659 }
658 660
659 socket->SendTo(io_buffer_, io_buffer_size_, addresses_.front(), 661 socket->SendTo(io_buffer_, io_buffer_size_, addresses_.front(),
660 base::Bind(&SocketSendToFunction::OnCompleted, this)); 662 base::Bind(&SocketSendToFunction::OnCompleted, this));
661 } 663 }
662 664
663 void SocketSendToFunction::OnCompleted(int bytes_written) { 665 void SocketSendToFunction::OnCompleted(int bytes_written) {
664 base::DictionaryValue* result = new base::DictionaryValue(); 666 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
665 result->SetInteger(kBytesWrittenKey, bytes_written); 667 result->SetInteger(kBytesWrittenKey, bytes_written);
666 SetResult(result); 668 SetResult(std::move(result));
667 669
668 AsyncWorkCompleted(); 670 AsyncWorkCompleted();
669 } 671 }
670 672
671 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() {} 673 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() {}
672 674
673 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} 675 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {}
674 676
675 bool SocketSetKeepAliveFunction::Prepare() { 677 bool SocketSetKeepAliveFunction::Prepare() {
676 params_ = api::socket::SetKeepAlive::Params::Create(*args_); 678 params_ = api::socket::SetKeepAlive::Params::Create(*args_);
677 EXTENSION_FUNCTION_VALIDATE(params_.get()); 679 EXTENSION_FUNCTION_VALIDATE(params_.get());
678 return true; 680 return true;
679 } 681 }
680 682
681 void SocketSetKeepAliveFunction::Work() { 683 void SocketSetKeepAliveFunction::Work() {
682 bool result = false; 684 bool result = false;
683 Socket* socket = GetSocket(params_->socket_id); 685 Socket* socket = GetSocket(params_->socket_id);
684 if (socket) { 686 if (socket) {
685 int delay = 0; 687 int delay = 0;
686 if (params_->delay.get()) 688 if (params_->delay.get())
687 delay = *params_->delay; 689 delay = *params_->delay;
688 result = socket->SetKeepAlive(params_->enable, delay); 690 result = socket->SetKeepAlive(params_->enable, delay);
689 } else { 691 } else {
690 error_ = kSocketNotFoundError; 692 error_ = kSocketNotFoundError;
691 } 693 }
692 SetResult(new base::FundamentalValue(result)); 694 SetResult(base::MakeUnique<base::FundamentalValue>(result));
693 } 695 }
694 696
695 SocketSetNoDelayFunction::SocketSetNoDelayFunction() {} 697 SocketSetNoDelayFunction::SocketSetNoDelayFunction() {}
696 698
697 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} 699 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {}
698 700
699 bool SocketSetNoDelayFunction::Prepare() { 701 bool SocketSetNoDelayFunction::Prepare() {
700 params_ = api::socket::SetNoDelay::Params::Create(*args_); 702 params_ = api::socket::SetNoDelay::Params::Create(*args_);
701 EXTENSION_FUNCTION_VALIDATE(params_.get()); 703 EXTENSION_FUNCTION_VALIDATE(params_.get());
702 return true; 704 return true;
703 } 705 }
704 706
705 void SocketSetNoDelayFunction::Work() { 707 void SocketSetNoDelayFunction::Work() {
706 bool result = false; 708 bool result = false;
707 Socket* socket = GetSocket(params_->socket_id); 709 Socket* socket = GetSocket(params_->socket_id);
708 if (socket) 710 if (socket)
709 result = socket->SetNoDelay(params_->no_delay); 711 result = socket->SetNoDelay(params_->no_delay);
710 else 712 else
711 error_ = kSocketNotFoundError; 713 error_ = kSocketNotFoundError;
712 SetResult(new base::FundamentalValue(result)); 714 SetResult(base::MakeUnique<base::FundamentalValue>(result));
713 } 715 }
714 716
715 SocketGetInfoFunction::SocketGetInfoFunction() {} 717 SocketGetInfoFunction::SocketGetInfoFunction() {}
716 718
717 SocketGetInfoFunction::~SocketGetInfoFunction() {} 719 SocketGetInfoFunction::~SocketGetInfoFunction() {}
718 720
719 bool SocketGetInfoFunction::Prepare() { 721 bool SocketGetInfoFunction::Prepare() {
720 params_ = api::socket::GetInfo::Params::Create(*args_); 722 params_ = api::socket::GetInfo::Params::Create(*args_);
721 EXTENSION_FUNCTION_VALIDATE(params_.get()); 723 EXTENSION_FUNCTION_VALIDATE(params_.get());
722 return true; 724 return true;
(...skipping 26 matching lines...) Expand all
749 } 751 }
750 752
751 // Grab the local address as known by the OS. 753 // Grab the local address as known by the OS.
752 net::IPEndPoint localAddress; 754 net::IPEndPoint localAddress;
753 if (socket->GetLocalAddress(&localAddress)) { 755 if (socket->GetLocalAddress(&localAddress)) {
754 info.local_address.reset( 756 info.local_address.reset(
755 new std::string(localAddress.ToStringWithoutPort())); 757 new std::string(localAddress.ToStringWithoutPort()));
756 info.local_port.reset(new int(localAddress.port())); 758 info.local_port.reset(new int(localAddress.port()));
757 } 759 }
758 760
759 SetResult(info.ToValue().release()); 761 SetResult(info.ToValue());
760 } 762 }
761 763
762 bool SocketGetNetworkListFunction::RunAsync() { 764 bool SocketGetNetworkListFunction::RunAsync() {
763 BrowserThread::PostTask( 765 BrowserThread::PostTask(
764 BrowserThread::FILE, FROM_HERE, 766 BrowserThread::FILE, FROM_HERE,
765 base::Bind(&SocketGetNetworkListFunction::GetNetworkListOnFileThread, 767 base::Bind(&SocketGetNetworkListFunction::GetNetworkListOnFileThread,
766 this)); 768 this));
767 return true; 769 return true;
768 } 770 }
769 771
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 params_ = api::socket::JoinGroup::Params::Create(*args_); 818 params_ = api::socket::JoinGroup::Params::Create(*args_);
817 EXTENSION_FUNCTION_VALIDATE(params_.get()); 819 EXTENSION_FUNCTION_VALIDATE(params_.get());
818 return true; 820 return true;
819 } 821 }
820 822
821 void SocketJoinGroupFunction::Work() { 823 void SocketJoinGroupFunction::Work() {
822 int result = -1; 824 int result = -1;
823 Socket* socket = GetSocket(params_->socket_id); 825 Socket* socket = GetSocket(params_->socket_id);
824 if (!socket) { 826 if (!socket) {
825 error_ = kSocketNotFoundError; 827 error_ = kSocketNotFoundError;
826 SetResult(new base::FundamentalValue(result)); 828 SetResult(base::MakeUnique<base::FundamentalValue>(result));
827 return; 829 return;
828 } 830 }
829 831
830 if (socket->GetSocketType() != Socket::TYPE_UDP) { 832 if (socket->GetSocketType() != Socket::TYPE_UDP) {
831 error_ = kMulticastSocketTypeError; 833 error_ = kMulticastSocketTypeError;
832 SetResult(new base::FundamentalValue(result)); 834 SetResult(base::MakeUnique<base::FundamentalValue>(result));
833 return; 835 return;
834 } 836 }
835 837
836 SocketPermission::CheckParam param( 838 SocketPermission::CheckParam param(
837 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, 839 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP,
838 kWildcardAddress, 840 kWildcardAddress,
839 kWildcardPort); 841 kWildcardPort);
840 842
841 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( 843 if (!extension()->permissions_data()->CheckAPIPermissionWithParam(
842 APIPermission::kSocket, &param)) { 844 APIPermission::kSocket, &param)) {
843 error_ = kPermissionError; 845 error_ = kPermissionError;
844 SetResult(new base::FundamentalValue(result)); 846 SetResult(base::MakeUnique<base::FundamentalValue>(result));
845 return; 847 return;
846 } 848 }
847 849
848 result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address); 850 result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address);
849 if (result != 0) { 851 if (result != 0) {
850 error_ = net::ErrorToString(result); 852 error_ = net::ErrorToString(result);
851 } 853 }
852 SetResult(new base::FundamentalValue(result)); 854 SetResult(base::MakeUnique<base::FundamentalValue>(result));
853 } 855 }
854 856
855 SocketLeaveGroupFunction::SocketLeaveGroupFunction() {} 857 SocketLeaveGroupFunction::SocketLeaveGroupFunction() {}
856 858
857 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} 859 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {}
858 860
859 bool SocketLeaveGroupFunction::Prepare() { 861 bool SocketLeaveGroupFunction::Prepare() {
860 params_ = api::socket::LeaveGroup::Params::Create(*args_); 862 params_ = api::socket::LeaveGroup::Params::Create(*args_);
861 EXTENSION_FUNCTION_VALIDATE(params_.get()); 863 EXTENSION_FUNCTION_VALIDATE(params_.get());
862 return true; 864 return true;
863 } 865 }
864 866
865 void SocketLeaveGroupFunction::Work() { 867 void SocketLeaveGroupFunction::Work() {
866 int result = -1; 868 int result = -1;
867 Socket* socket = GetSocket(params_->socket_id); 869 Socket* socket = GetSocket(params_->socket_id);
868 870
869 if (!socket) { 871 if (!socket) {
870 error_ = kSocketNotFoundError; 872 error_ = kSocketNotFoundError;
871 SetResult(new base::FundamentalValue(result)); 873 SetResult(base::MakeUnique<base::FundamentalValue>(result));
872 return; 874 return;
873 } 875 }
874 876
875 if (socket->GetSocketType() != Socket::TYPE_UDP) { 877 if (socket->GetSocketType() != Socket::TYPE_UDP) {
876 error_ = kMulticastSocketTypeError; 878 error_ = kMulticastSocketTypeError;
877 SetResult(new base::FundamentalValue(result)); 879 SetResult(base::MakeUnique<base::FundamentalValue>(result));
878 return; 880 return;
879 } 881 }
880 882
881 SocketPermission::CheckParam param( 883 SocketPermission::CheckParam param(
882 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, 884 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP,
883 kWildcardAddress, 885 kWildcardAddress,
884 kWildcardPort); 886 kWildcardPort);
885 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( 887 if (!extension()->permissions_data()->CheckAPIPermissionWithParam(
886 APIPermission::kSocket, &param)) { 888 APIPermission::kSocket, &param)) {
887 error_ = kPermissionError; 889 error_ = kPermissionError;
888 SetResult(new base::FundamentalValue(result)); 890 SetResult(base::MakeUnique<base::FundamentalValue>(result));
889 return; 891 return;
890 } 892 }
891 893
892 result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address); 894 result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address);
893 if (result != 0) 895 if (result != 0)
894 error_ = net::ErrorToString(result); 896 error_ = net::ErrorToString(result);
895 SetResult(new base::FundamentalValue(result)); 897 SetResult(base::MakeUnique<base::FundamentalValue>(result));
896 } 898 }
897 899
898 SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() {} 900 SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() {}
899 901
900 SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {} 902 SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {}
901 903
902 bool SocketSetMulticastTimeToLiveFunction::Prepare() { 904 bool SocketSetMulticastTimeToLiveFunction::Prepare() {
903 params_ = api::socket::SetMulticastTimeToLive::Params::Create(*args_); 905 params_ = api::socket::SetMulticastTimeToLive::Params::Create(*args_);
904 EXTENSION_FUNCTION_VALIDATE(params_.get()); 906 EXTENSION_FUNCTION_VALIDATE(params_.get());
905 return true; 907 return true;
906 } 908 }
907 void SocketSetMulticastTimeToLiveFunction::Work() { 909 void SocketSetMulticastTimeToLiveFunction::Work() {
908 int result = -1; 910 int result = -1;
909 Socket* socket = GetSocket(params_->socket_id); 911 Socket* socket = GetSocket(params_->socket_id);
910 if (!socket) { 912 if (!socket) {
911 error_ = kSocketNotFoundError; 913 error_ = kSocketNotFoundError;
912 SetResult(new base::FundamentalValue(result)); 914 SetResult(base::MakeUnique<base::FundamentalValue>(result));
913 return; 915 return;
914 } 916 }
915 917
916 if (socket->GetSocketType() != Socket::TYPE_UDP) { 918 if (socket->GetSocketType() != Socket::TYPE_UDP) {
917 error_ = kMulticastSocketTypeError; 919 error_ = kMulticastSocketTypeError;
918 SetResult(new base::FundamentalValue(result)); 920 SetResult(base::MakeUnique<base::FundamentalValue>(result));
919 return; 921 return;
920 } 922 }
921 923
922 result = 924 result =
923 static_cast<UDPSocket*>(socket)->SetMulticastTimeToLive(params_->ttl); 925 static_cast<UDPSocket*>(socket)->SetMulticastTimeToLive(params_->ttl);
924 if (result != 0) 926 if (result != 0)
925 error_ = net::ErrorToString(result); 927 error_ = net::ErrorToString(result);
926 SetResult(new base::FundamentalValue(result)); 928 SetResult(base::MakeUnique<base::FundamentalValue>(result));
927 } 929 }
928 930
929 SocketSetMulticastLoopbackModeFunction:: 931 SocketSetMulticastLoopbackModeFunction::
930 SocketSetMulticastLoopbackModeFunction() {} 932 SocketSetMulticastLoopbackModeFunction() {}
931 933
932 SocketSetMulticastLoopbackModeFunction:: 934 SocketSetMulticastLoopbackModeFunction::
933 ~SocketSetMulticastLoopbackModeFunction() {} 935 ~SocketSetMulticastLoopbackModeFunction() {}
934 936
935 bool SocketSetMulticastLoopbackModeFunction::Prepare() { 937 bool SocketSetMulticastLoopbackModeFunction::Prepare() {
936 params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_); 938 params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_);
937 EXTENSION_FUNCTION_VALIDATE(params_.get()); 939 EXTENSION_FUNCTION_VALIDATE(params_.get());
938 return true; 940 return true;
939 } 941 }
940 942
941 void SocketSetMulticastLoopbackModeFunction::Work() { 943 void SocketSetMulticastLoopbackModeFunction::Work() {
942 int result = -1; 944 int result = -1;
943 Socket* socket = GetSocket(params_->socket_id); 945 Socket* socket = GetSocket(params_->socket_id);
944 if (!socket) { 946 if (!socket) {
945 error_ = kSocketNotFoundError; 947 error_ = kSocketNotFoundError;
946 SetResult(new base::FundamentalValue(result)); 948 SetResult(base::MakeUnique<base::FundamentalValue>(result));
947 return; 949 return;
948 } 950 }
949 951
950 if (socket->GetSocketType() != Socket::TYPE_UDP) { 952 if (socket->GetSocketType() != Socket::TYPE_UDP) {
951 error_ = kMulticastSocketTypeError; 953 error_ = kMulticastSocketTypeError;
952 SetResult(new base::FundamentalValue(result)); 954 SetResult(base::MakeUnique<base::FundamentalValue>(result));
953 return; 955 return;
954 } 956 }
955 957
956 result = static_cast<UDPSocket*>(socket) 958 result = static_cast<UDPSocket*>(socket)
957 ->SetMulticastLoopbackMode(params_->enabled); 959 ->SetMulticastLoopbackMode(params_->enabled);
958 if (result != 0) 960 if (result != 0)
959 error_ = net::ErrorToString(result); 961 error_ = net::ErrorToString(result);
960 SetResult(new base::FundamentalValue(result)); 962 SetResult(base::MakeUnique<base::FundamentalValue>(result));
961 } 963 }
962 964
963 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() {} 965 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() {}
964 966
965 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {} 967 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {}
966 968
967 bool SocketGetJoinedGroupsFunction::Prepare() { 969 bool SocketGetJoinedGroupsFunction::Prepare() {
968 params_ = api::socket::GetJoinedGroups::Params::Create(*args_); 970 params_ = api::socket::GetJoinedGroups::Params::Create(*args_);
969 EXTENSION_FUNCTION_VALIDATE(params_.get()); 971 EXTENSION_FUNCTION_VALIDATE(params_.get());
970 return true; 972 return true;
971 } 973 }
972 974
973 void SocketGetJoinedGroupsFunction::Work() { 975 void SocketGetJoinedGroupsFunction::Work() {
974 int result = -1; 976 int result = -1;
975 Socket* socket = GetSocket(params_->socket_id); 977 Socket* socket = GetSocket(params_->socket_id);
976 if (!socket) { 978 if (!socket) {
977 error_ = kSocketNotFoundError; 979 error_ = kSocketNotFoundError;
978 SetResult(new base::FundamentalValue(result)); 980 SetResult(base::MakeUnique<base::FundamentalValue>(result));
979 return; 981 return;
980 } 982 }
981 983
982 if (socket->GetSocketType() != Socket::TYPE_UDP) { 984 if (socket->GetSocketType() != Socket::TYPE_UDP) {
983 error_ = kMulticastSocketTypeError; 985 error_ = kMulticastSocketTypeError;
984 SetResult(new base::FundamentalValue(result)); 986 SetResult(base::MakeUnique<base::FundamentalValue>(result));
985 return; 987 return;
986 } 988 }
987 989
988 SocketPermission::CheckParam param( 990 SocketPermission::CheckParam param(
989 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, 991 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP,
990 kWildcardAddress, 992 kWildcardAddress,
991 kWildcardPort); 993 kWildcardPort);
992 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( 994 if (!extension()->permissions_data()->CheckAPIPermissionWithParam(
993 APIPermission::kSocket, &param)) { 995 APIPermission::kSocket, &param)) {
994 error_ = kPermissionError; 996 error_ = kPermissionError;
995 SetResult(new base::FundamentalValue(result)); 997 SetResult(base::MakeUnique<base::FundamentalValue>(result));
996 return; 998 return;
997 } 999 }
998 1000
999 base::ListValue* values = new base::ListValue(); 1001 std::unique_ptr<base::ListValue> values(new base::ListValue());
1000 values->AppendStrings((std::vector<std::string>&)static_cast<UDPSocket*>( 1002 values->AppendStrings((std::vector<std::string>&)static_cast<UDPSocket*>(
1001 socket)->GetJoinedGroups()); 1003 socket)->GetJoinedGroups());
1002 SetResult(values); 1004 SetResult(std::move(values));
1003 } 1005 }
1004 1006
1005 SocketSecureFunction::SocketSecureFunction() { 1007 SocketSecureFunction::SocketSecureFunction() {
1006 } 1008 }
1007 1009
1008 SocketSecureFunction::~SocketSecureFunction() { 1010 SocketSecureFunction::~SocketSecureFunction() {
1009 } 1011 }
1010 1012
1011 bool SocketSecureFunction::Prepare() { 1013 bool SocketSecureFunction::Prepare() {
1012 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1014 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1013 params_ = api::socket::Secure::Params::Create(*args_); 1015 params_ = api::socket::Secure::Params::Create(*args_);
1014 EXTENSION_FUNCTION_VALIDATE(params_.get()); 1016 EXTENSION_FUNCTION_VALIDATE(params_.get());
1015 url_request_getter_ = content::BrowserContext::GetDefaultStoragePartition( 1017 url_request_getter_ = content::BrowserContext::GetDefaultStoragePartition(
1016 browser_context())->GetURLRequestContext(); 1018 browser_context())->GetURLRequestContext();
1017 return true; 1019 return true;
1018 } 1020 }
1019 1021
1020 // Override the regular implementation, which would call AsyncWorkCompleted 1022 // Override the regular implementation, which would call AsyncWorkCompleted
1021 // immediately after Work(). 1023 // immediately after Work().
1022 void SocketSecureFunction::AsyncWorkStart() { 1024 void SocketSecureFunction::AsyncWorkStart() {
1023 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1025 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1024 1026
1025 Socket* socket = GetSocket(params_->socket_id); 1027 Socket* socket = GetSocket(params_->socket_id);
1026 if (!socket) { 1028 if (!socket) {
1027 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); 1029 SetResult(
1030 base::MakeUnique<base::FundamentalValue>(net::ERR_INVALID_ARGUMENT));
1028 error_ = kSocketNotFoundError; 1031 error_ = kSocketNotFoundError;
1029 AsyncWorkCompleted(); 1032 AsyncWorkCompleted();
1030 return; 1033 return;
1031 } 1034 }
1032 1035
1033 // Make sure that the socket is a TCP client socket. 1036 // Make sure that the socket is a TCP client socket.
1034 if (socket->GetSocketType() != Socket::TYPE_TCP || 1037 if (socket->GetSocketType() != Socket::TYPE_TCP ||
1035 static_cast<TCPSocket*>(socket)->ClientStream() == NULL) { 1038 static_cast<TCPSocket*>(socket)->ClientStream() == NULL) {
1036 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); 1039 SetResult(
1040 base::MakeUnique<base::FundamentalValue>(net::ERR_INVALID_ARGUMENT));
1037 error_ = kSecureSocketTypeError; 1041 error_ = kSecureSocketTypeError;
1038 AsyncWorkCompleted(); 1042 AsyncWorkCompleted();
1039 return; 1043 return;
1040 } 1044 }
1041 1045
1042 if (!socket->IsConnected()) { 1046 if (!socket->IsConnected()) {
1043 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); 1047 SetResult(
1048 base::MakeUnique<base::FundamentalValue>(net::ERR_INVALID_ARGUMENT));
1044 error_ = kSocketNotConnectedError; 1049 error_ = kSocketNotConnectedError;
1045 AsyncWorkCompleted(); 1050 AsyncWorkCompleted();
1046 return; 1051 return;
1047 } 1052 }
1048 1053
1049 net::URLRequestContext* url_request_context = 1054 net::URLRequestContext* url_request_context =
1050 url_request_getter_->GetURLRequestContext(); 1055 url_request_getter_->GetURLRequestContext();
1051 1056
1052 TLSSocket::UpgradeSocketToTLS( 1057 TLSSocket::UpgradeSocketToTLS(
1053 socket, 1058 socket,
(...skipping 15 matching lines...) Expand all
1069 } else { 1074 } else {
1070 RemoveSocket(params_->socket_id); 1075 RemoveSocket(params_->socket_id);
1071 error_ = net::ErrorToString(result); 1076 error_ = net::ErrorToString(result);
1072 } 1077 }
1073 1078
1074 results_ = api::socket::Secure::Results::Create(result); 1079 results_ = api::socket::Secure::Results::Create(result);
1075 AsyncWorkCompleted(); 1080 AsyncWorkCompleted();
1076 } 1081 }
1077 1082
1078 } // namespace extensions 1083 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698