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/bluetooth/bluetooth_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
| 9 #include "base/bind_helpers.h" |
9 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
11 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
13 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
14 #include "chrome/common/extensions/api/bluetooth.h" | 15 #include "chrome/common/extensions/api/bluetooth.h" |
15 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" | 16 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "device/bluetooth/bluetooth_adapter.h" | 18 #include "device/bluetooth/bluetooth_adapter.h" |
18 #include "device/bluetooth/bluetooth_device.h" | 19 #include "device/bluetooth/bluetooth_device.h" |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 BluetoothProfile* bluetooth_profile = | 451 BluetoothProfile* bluetooth_profile = |
451 GetEventRouter(browser_context())->GetProfile(uuid); | 452 GetEventRouter(browser_context())->GetProfile(uuid); |
452 if (!bluetooth_profile) { | 453 if (!bluetooth_profile) { |
453 SetError(kProfileNotFound); | 454 SetError(kProfileNotFound); |
454 SendResponse(false); | 455 SendResponse(false); |
455 return false; | 456 return false; |
456 } | 457 } |
457 | 458 |
458 device->ConnectToProfile( | 459 device->ConnectToProfile( |
459 bluetooth_profile, | 460 bluetooth_profile, |
460 base::Bind(&BluetoothConnectFunction::OnSuccessCallback, this), | 461 base::Bind(&BluetoothConnectFunction::OnConnectedCallback, |
| 462 this, |
| 463 adapter, |
| 464 device->GetAddress()), |
461 base::Bind(&BluetoothConnectFunction::OnErrorCallback, this)); | 465 base::Bind(&BluetoothConnectFunction::OnErrorCallback, this)); |
462 | 466 |
463 return true; | 467 return true; |
464 } | 468 } |
465 | 469 |
466 void BluetoothConnectFunction::OnSuccessCallback() { | 470 void BluetoothConnectFunction::OnConnectedCallback( |
| 471 scoped_refptr<device::BluetoothAdapter> adapter, |
| 472 const std::string& device_address) { |
| 473 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 474 |
| 475 // TODO(tengs): Remove this once we have an API for starting the connection |
| 476 // monitor. |
| 477 BluetoothDevice* device = adapter->GetDevice(device_address); |
| 478 if (!device) { |
| 479 SetError(kInvalidDevice); |
| 480 SendResponse(false); |
| 481 return; |
| 482 } |
| 483 // Start the connection monitor, and return success even if this fails, |
| 484 // as the connection was still opened successfully. |
| 485 device->StartConnectionMonitor( |
| 486 base::Bind(&BluetoothConnectFunction::OnMonitorStartedCallback, this), |
| 487 base::Bind(&BluetoothConnectFunction::OnMonitorStartedCallback, this)); |
| 488 } |
| 489 |
| 490 void BluetoothConnectFunction::OnMonitorStartedCallback() { |
467 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 491 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
468 SendResponse(true); | 492 SendResponse(true); |
469 } | 493 } |
470 | 494 |
471 void BluetoothConnectFunction::OnErrorCallback(const std::string& error) { | 495 void BluetoothConnectFunction::OnErrorCallback(const std::string& error) { |
472 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 496 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
473 SetError(error); | 497 SetError(error); |
474 SendResponse(false); | 498 SendResponse(false); |
475 } | 499 } |
476 | 500 |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
644 adapter, | 668 adapter, |
645 extension_id(), | 669 extension_id(), |
646 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), | 670 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
647 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); | 671 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
648 | 672 |
649 return true; | 673 return true; |
650 } | 674 } |
651 | 675 |
652 } // namespace api | 676 } // namespace api |
653 } // namespace extensions | 677 } // namespace extensions |
OLD | NEW |