| 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_extension_function.h
" | 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h
" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" | 8 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
| 9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "device/bluetooth/bluetooth_adapter.h" | 11 #include "device/bluetooth/bluetooth_adapter.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_factory.h" | 12 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 const char kPlatformNotSupported[] = | 16 const char kPlatformNotSupported[] = |
| 17 "This operation is not supported on your platform"; | 17 "This operation is not supported on your platform"; |
| 18 | 18 |
| 19 scoped_refptr<device::BluetoothAdapter> GetAdapter(Profile* profile) { | 19 void RunCallbackOnAdapterReady( |
| 20 const device::BluetoothAdapterFactory::AdapterCallback callback, |
| 21 Profile* profile) { |
| 20 extensions::ExtensionBluetoothEventRouter* event_router = | 22 extensions::ExtensionBluetoothEventRouter* event_router = |
| 21 extensions::BluetoothAPI::Get(profile)->bluetooth_event_router(); | 23 extensions::BluetoothAPI::Get(profile)->bluetooth_event_router(); |
| 22 return event_router->GetAdapter(); | 24 event_router->RunCallbackOnAdapterReady(callback); |
| 23 } | 25 } |
| 24 | 26 |
| 25 } // namespace | 27 } // namespace |
| 26 | 28 |
| 27 namespace extensions { | 29 namespace extensions { |
| 28 | 30 |
| 29 namespace api { | 31 namespace api { |
| 30 | 32 |
| 31 BluetoothExtensionFunction::BluetoothExtensionFunction() { | 33 BluetoothExtensionFunction::BluetoothExtensionFunction() |
| 34 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 32 } | 35 } |
| 33 | 36 |
| 34 BluetoothExtensionFunction::~BluetoothExtensionFunction() { | 37 BluetoothExtensionFunction::~BluetoothExtensionFunction() { |
| 35 } | 38 } |
| 36 | 39 |
| 37 bool BluetoothExtensionFunction::RunImpl() { | 40 bool BluetoothExtensionFunction::RunImpl() { |
| 38 scoped_refptr<device::BluetoothAdapter> adapter = GetAdapter(profile()); | 41 if (!device::BluetoothAdapterFactory::IsBluetoothSupported()) { |
| 39 if (!adapter) { | |
| 40 SetError(kPlatformNotSupported); | 42 SetError(kPlatformNotSupported); |
| 41 return false; | 43 return false; |
| 42 } | 44 } |
| 45 RunCallbackOnAdapterReady( |
| 46 base::Bind(&BluetoothExtensionFunction::RunOnAdapterReady, |
| 47 weak_ptr_factory_.GetWeakPtr()), |
| 48 profile()); |
| 49 return true; |
| 50 } |
| 51 |
| 52 void BluetoothExtensionFunction::RunOnAdapterReady( |
| 53 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 43 DoWork(adapter); | 54 DoWork(adapter); |
| 44 return true; | |
| 45 } | 55 } |
| 46 | 56 |
| 47 } // namespace api | 57 } // namespace api |
| 48 | 58 |
| 49 } // namespace extensions | 59 } // namespace extensions |
| OLD | NEW |