| 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/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 | 629 |
| 630 return true; | 630 return true; |
| 631 } | 631 } |
| 632 | 632 |
| 633 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { | 633 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { |
| 634 SendResponse(true); | 634 SendResponse(true); |
| 635 } | 635 } |
| 636 | 636 |
| 637 void BluetoothStartDiscoveryFunction::OnErrorCallback() { | 637 void BluetoothStartDiscoveryFunction::OnErrorCallback() { |
| 638 SetError(kStartDiscoveryFailed); | 638 SetError(kStartDiscoveryFailed); |
| 639 GetEventRouter(browser_context())->SetResponsibleForDiscovery(false); |
| 639 SendResponse(false); | 640 SendResponse(false); |
| 640 GetEventRouter(browser_context())->OnListenerRemoved(); | 641 GetEventRouter(browser_context())->OnListenerRemoved(); |
| 641 } | 642 } |
| 642 | 643 |
| 643 bool BluetoothStartDiscoveryFunction::DoWork( | 644 bool BluetoothStartDiscoveryFunction::DoWork( |
| 644 scoped_refptr<BluetoothAdapter> adapter) { | 645 scoped_refptr<BluetoothAdapter> adapter) { |
| 645 GetEventRouter(browser_context())->OnListenerAdded(); | 646 GetEventRouter(browser_context())->SetSendDiscoveryEvents(true); |
| 646 GetEventRouter(browser_context())->StartDiscoverySession( | 647 |
| 647 adapter, | 648 // If this profile is already discovering devices, there should be nothing |
| 648 extension_id(), | 649 // else to do. |
| 649 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this), | 650 if (!GetEventRouter(browser_context())->IsResponsibleForDiscovery()) { |
| 650 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this)); | 651 GetEventRouter(browser_context())->SetResponsibleForDiscovery(true); |
| 652 GetEventRouter(browser_context())->OnListenerAdded(); |
| 653 adapter->StartDiscovering( |
| 654 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this), |
| 655 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this)); |
| 656 } |
| 651 | 657 |
| 652 return true; | 658 return true; |
| 653 } | 659 } |
| 654 | 660 |
| 655 void BluetoothStopDiscoveryFunction::OnSuccessCallback() { | 661 void BluetoothStopDiscoveryFunction::OnSuccessCallback() { |
| 656 SendResponse(true); | 662 SendResponse(true); |
| 657 GetEventRouter(browser_context())->OnListenerRemoved(); | 663 GetEventRouter(browser_context())->OnListenerRemoved(); |
| 658 } | 664 } |
| 659 | 665 |
| 660 void BluetoothStopDiscoveryFunction::OnErrorCallback() { | 666 void BluetoothStopDiscoveryFunction::OnErrorCallback() { |
| 661 SetError(kStopDiscoveryFailed); | 667 SetError(kStopDiscoveryFailed); |
| 668 GetEventRouter(browser_context())->SetResponsibleForDiscovery(true); |
| 662 SendResponse(false); | 669 SendResponse(false); |
| 663 GetEventRouter(browser_context())->OnListenerRemoved(); | 670 GetEventRouter(browser_context())->OnListenerRemoved(); |
| 664 } | 671 } |
| 665 | 672 |
| 666 bool BluetoothStopDiscoveryFunction::DoWork( | 673 bool BluetoothStopDiscoveryFunction::DoWork( |
| 667 scoped_refptr<BluetoothAdapter> adapter) { | 674 scoped_refptr<BluetoothAdapter> adapter) { |
| 668 GetEventRouter(browser_context())->StopDiscoverySession( | 675 GetEventRouter(browser_context())->SetSendDiscoveryEvents(false); |
| 669 adapter, | 676 if (GetEventRouter(browser_context())->IsResponsibleForDiscovery()) { |
| 670 extension_id(), | 677 adapter->StopDiscovering( |
| 671 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), | 678 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
| 672 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); | 679 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
| 680 } |
| 673 | 681 |
| 674 return true; | 682 return true; |
| 675 } | 683 } |
| 676 | 684 |
| 677 } // namespace api | 685 } // namespace api |
| 678 } // namespace extensions | 686 } // namespace extensions |
| OLD | NEW |