OLD | NEW |
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 "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_api.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_api_socket.h" | 9 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_api_socket.h" |
10 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_
dispatcher.h" | 10 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_
dispatcher.h" |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 } | 429 } |
430 } | 430 } |
431 | 431 |
432 adapter->CreateL2capService(uuid, service_options, callback, error_callback); | 432 adapter->CreateL2capService(uuid, service_options, callback, error_callback); |
433 } | 433 } |
434 | 434 |
435 void BluetoothSocketListenUsingL2capFunction::CreateResults() { | 435 void BluetoothSocketListenUsingL2capFunction::CreateResults() { |
436 results_ = bluetooth_socket::ListenUsingL2cap::Results::Create(); | 436 results_ = bluetooth_socket::ListenUsingL2cap::Results::Create(); |
437 } | 437 } |
438 | 438 |
439 BluetoothSocketConnectFunction::BluetoothSocketConnectFunction() {} | 439 BluetoothSocketAbstractConnectFunction:: |
| 440 BluetoothSocketAbstractConnectFunction() {} |
440 | 441 |
441 BluetoothSocketConnectFunction::~BluetoothSocketConnectFunction() {} | 442 BluetoothSocketAbstractConnectFunction:: |
| 443 ~BluetoothSocketAbstractConnectFunction() {} |
442 | 444 |
443 bool BluetoothSocketConnectFunction::Prepare() { | 445 bool BluetoothSocketAbstractConnectFunction::Prepare() { |
444 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 446 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
445 params_ = bluetooth_socket::Connect::Params::Create(*args_); | 447 params_ = bluetooth_socket::Connect::Params::Create(*args_); |
446 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 448 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
447 | 449 |
448 socket_event_dispatcher_ = GetSocketEventDispatcher(browser_context()); | 450 socket_event_dispatcher_ = GetSocketEventDispatcher(browser_context()); |
449 return socket_event_dispatcher_ != NULL; | 451 return socket_event_dispatcher_ != NULL; |
450 } | 452 } |
451 | 453 |
452 void BluetoothSocketConnectFunction::AsyncWorkStart() { | 454 void BluetoothSocketAbstractConnectFunction::AsyncWorkStart() { |
453 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); | 455 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
454 device::BluetoothAdapterFactory::GetAdapter( | 456 device::BluetoothAdapterFactory::GetAdapter( |
455 base::Bind(&BluetoothSocketConnectFunction::OnGetAdapter, this)); | 457 base::Bind(&BluetoothSocketAbstractConnectFunction::OnGetAdapter, this)); |
456 } | 458 } |
457 | 459 |
458 void BluetoothSocketConnectFunction::OnGetAdapter( | 460 void BluetoothSocketAbstractConnectFunction::OnGetAdapter( |
459 scoped_refptr<device::BluetoothAdapter> adapter) { | 461 scoped_refptr<device::BluetoothAdapter> adapter) { |
460 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); | 462 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
461 BluetoothApiSocket* socket = GetSocket(params_->socket_id); | 463 BluetoothApiSocket* socket = GetSocket(params_->socket_id); |
462 if (!socket) { | 464 if (!socket) { |
463 error_ = kSocketNotFoundError; | 465 error_ = kSocketNotFoundError; |
464 AsyncWorkCompleted(); | 466 AsyncWorkCompleted(); |
465 return; | 467 return; |
466 } | 468 } |
467 | 469 |
468 device::BluetoothDevice* device = adapter->GetDevice(params_->address); | 470 device::BluetoothDevice* device = adapter->GetDevice(params_->address); |
(...skipping 10 matching lines...) Expand all Loading... |
479 return; | 481 return; |
480 } | 482 } |
481 | 483 |
482 BluetoothPermissionRequest param(params_->uuid); | 484 BluetoothPermissionRequest param(params_->uuid); |
483 if (!BluetoothManifestData::CheckRequest(extension(), param)) { | 485 if (!BluetoothManifestData::CheckRequest(extension(), param)) { |
484 error_ = kPermissionDeniedError; | 486 error_ = kPermissionDeniedError; |
485 AsyncWorkCompleted(); | 487 AsyncWorkCompleted(); |
486 return; | 488 return; |
487 } | 489 } |
488 | 490 |
489 device->ConnectToService( | 491 ConnectToService(device, uuid); |
490 uuid, | |
491 base::Bind(&BluetoothSocketConnectFunction::OnConnect, this), | |
492 base::Bind(&BluetoothSocketConnectFunction::OnConnectError, this)); | |
493 } | 492 } |
494 | 493 |
495 void BluetoothSocketConnectFunction::OnConnect( | 494 void BluetoothSocketAbstractConnectFunction::OnConnect( |
496 scoped_refptr<device::BluetoothSocket> socket) { | 495 scoped_refptr<device::BluetoothSocket> socket) { |
497 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); | 496 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
498 | 497 |
499 // Fetch the socket again since this is not a reference-counted object, and | 498 // Fetch the socket again since this is not a reference-counted object, and |
500 // it may have gone away in the meantime (we check earlier to avoid making | 499 // it may have gone away in the meantime (we check earlier to avoid making |
501 // a connection in the case of an obvious programming error). | 500 // a connection in the case of an obvious programming error). |
502 BluetoothApiSocket* api_socket = GetSocket(params_->socket_id); | 501 BluetoothApiSocket* api_socket = GetSocket(params_->socket_id); |
503 if (!api_socket) { | 502 if (!api_socket) { |
504 error_ = kSocketNotFoundError; | 503 error_ = kSocketNotFoundError; |
505 AsyncWorkCompleted(); | 504 AsyncWorkCompleted(); |
506 return; | 505 return; |
507 } | 506 } |
508 | 507 |
509 api_socket->AdoptConnectedSocket(socket, | 508 api_socket->AdoptConnectedSocket(socket, |
510 params_->address, | 509 params_->address, |
511 device::BluetoothUUID(params_->uuid)); | 510 device::BluetoothUUID(params_->uuid)); |
512 socket_event_dispatcher_->OnSocketConnect(extension_id(), | 511 socket_event_dispatcher_->OnSocketConnect(extension_id(), |
513 params_->socket_id); | 512 params_->socket_id); |
514 | 513 |
515 results_ = bluetooth_socket::Connect::Results::Create(); | 514 results_ = bluetooth_socket::Connect::Results::Create(); |
516 AsyncWorkCompleted(); | 515 AsyncWorkCompleted(); |
517 } | 516 } |
518 | 517 |
519 void BluetoothSocketConnectFunction::OnConnectError( | 518 void BluetoothSocketAbstractConnectFunction::OnConnectError( |
520 const std::string& message) { | 519 const std::string& message) { |
521 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); | 520 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
522 error_ = message; | 521 error_ = message; |
523 AsyncWorkCompleted(); | 522 AsyncWorkCompleted(); |
524 } | 523 } |
525 | 524 |
| 525 BluetoothSocketConnectFunction::BluetoothSocketConnectFunction() {} |
| 526 |
| 527 BluetoothSocketConnectFunction::~BluetoothSocketConnectFunction() {} |
| 528 |
| 529 void BluetoothSocketConnectFunction::ConnectToService( |
| 530 device::BluetoothDevice* device, |
| 531 const device::BluetoothUUID& uuid) { |
| 532 device->ConnectToService( |
| 533 uuid, |
| 534 base::Bind(&BluetoothSocketConnectFunction::OnConnect, this), |
| 535 base::Bind(&BluetoothSocketConnectFunction::OnConnectError, this)); |
| 536 } |
| 537 |
526 BluetoothSocketDisconnectFunction::BluetoothSocketDisconnectFunction() {} | 538 BluetoothSocketDisconnectFunction::BluetoothSocketDisconnectFunction() {} |
527 | 539 |
528 BluetoothSocketDisconnectFunction::~BluetoothSocketDisconnectFunction() {} | 540 BluetoothSocketDisconnectFunction::~BluetoothSocketDisconnectFunction() {} |
529 | 541 |
530 bool BluetoothSocketDisconnectFunction::Prepare() { | 542 bool BluetoothSocketDisconnectFunction::Prepare() { |
531 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 543 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
532 params_ = bluetooth_socket::Disconnect::Params::Create(*args_); | 544 params_ = bluetooth_socket::Disconnect::Params::Create(*args_); |
533 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 545 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
534 return true; | 546 return true; |
535 } | 547 } |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 if (socket) { | 669 if (socket) { |
658 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); | 670 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); |
659 } | 671 } |
660 } | 672 } |
661 } | 673 } |
662 results_ = bluetooth_socket::GetSockets::Results::Create(socket_infos); | 674 results_ = bluetooth_socket::GetSockets::Results::Create(socket_infos); |
663 } | 675 } |
664 | 676 |
665 } // namespace api | 677 } // namespace api |
666 } // namespace extensions | 678 } // namespace extensions |
OLD | NEW |