| 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 extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { |
| 20 extensions::ExtensionBluetoothEventRouter* event_router = | 20 return extensions::BluetoothAPI::Get(profile)->bluetooth_event_router(); |
| 21 extensions::BluetoothAPI::Get(profile)->bluetooth_event_router(); | 21 } |
| 22 return event_router->GetAdapter(); | 22 |
| 23 bool IsBluetoothSupported(Profile* profile) { |
| 24 return GetEventRouter(profile)->IsBluetoothSupported(); |
| 25 } |
| 26 |
| 27 void RunCallbackOnAdapterReady( |
| 28 const device::BluetoothAdapterFactory::AdapterCallback callback, |
| 29 Profile* profile) { |
| 30 GetEventRouter(profile)->RunCallbackOnAdapterReady(callback); |
| 23 } | 31 } |
| 24 | 32 |
| 25 } // namespace | 33 } // namespace |
| 26 | 34 |
| 27 namespace extensions { | 35 namespace extensions { |
| 28 | 36 |
| 29 namespace api { | 37 namespace api { |
| 30 | 38 |
| 31 BluetoothExtensionFunction::BluetoothExtensionFunction() { | 39 BluetoothExtensionFunction::BluetoothExtensionFunction() |
| 40 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 32 } | 41 } |
| 33 | 42 |
| 34 BluetoothExtensionFunction::~BluetoothExtensionFunction() { | 43 BluetoothExtensionFunction::~BluetoothExtensionFunction() { |
| 35 } | 44 } |
| 36 | 45 |
| 37 bool BluetoothExtensionFunction::RunImpl() { | 46 bool BluetoothExtensionFunction::RunImpl() { |
| 38 scoped_refptr<device::BluetoothAdapter> adapter = GetAdapter(profile()); | 47 if (!IsBluetoothSupported(profile())) { |
| 39 if (!adapter) { | |
| 40 SetError(kPlatformNotSupported); | 48 SetError(kPlatformNotSupported); |
| 41 return false; | 49 return false; |
| 42 } | 50 } |
| 51 RunCallbackOnAdapterReady( |
| 52 base::Bind(&BluetoothExtensionFunction::RunOnAdapterReady, |
| 53 weak_ptr_factory_.GetWeakPtr()), |
| 54 profile()); |
| 55 return true; |
| 56 } |
| 57 |
| 58 void BluetoothExtensionFunction::RunOnAdapterReady( |
| 59 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 43 DoWork(adapter); | 60 DoWork(adapter); |
| 44 return true; | |
| 45 } | 61 } |
| 46 | 62 |
| 47 } // namespace api | 63 } // namespace api |
| 48 | 64 |
| 49 } // namespace extensions | 65 } // namespace extensions |
| OLD | NEW |