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 "device/bluetooth/bluetooth_adapter_factory.h" | 5 #include "device/bluetooth/bluetooth_adapter_factory.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "device/bluetooth/bluetooth_adapter.h" | 10 #include "device/bluetooth/bluetooth_adapter.h" |
11 | 11 |
12 #if defined(OS_CHROMEOS) | 12 #if defined(OS_CHROMEOS) |
13 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 13 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
14 #endif | 14 #endif |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // Shared default adapter instance, we don't want to keep this class around | 18 // Shared default adapter instance, we don't want to keep this class around |
19 // if nobody is using it so use a WeakPtr and create the object when needed; | 19 // if nobody is using it so use a WeakPtr and create the object when needed; |
20 // since Google C++ Style (and clang's static analyzer) forbids us having | 20 // since Google C++ Style (and clang's static analyzer) forbids us having |
21 // exit-time destructors we use a leaky lazy instance for it. | 21 // exit-time destructors we use a leaky lazy instance for it. |
22 base::LazyInstance<base::WeakPtr<device::BluetoothAdapter> >::Leaky | 22 base::LazyInstance<base::WeakPtr<device::BluetoothAdapter> >::Leaky |
23 default_adapter = LAZY_INSTANCE_INITIALIZER; | 23 default_adapter = LAZY_INSTANCE_INITIALIZER; |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 namespace device { | 27 namespace device { |
28 | 28 |
29 // static | 29 // static |
30 scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::DefaultAdapter() { | 30 bool BluetoothAdapterFactory::IsBluetoothSupported() { |
| 31 #if defined(OS_CHROMEOS) |
| 32 return true; |
| 33 #endif |
| 34 return false; |
| 35 } |
| 36 |
| 37 // static |
| 38 void BluetoothAdapterFactory::RunCallbackOnAdapterReady( |
| 39 const AdapterCallback& callback) { |
31 if (!default_adapter.Get().get()) { | 40 if (!default_adapter.Get().get()) { |
32 #if defined(OS_CHROMEOS) | 41 #if defined(OS_CHROMEOS) |
33 chromeos::BluetoothAdapterChromeOs* new_adapter = | 42 chromeos::BluetoothAdapterChromeOs* new_adapter = |
34 new chromeos::BluetoothAdapterChromeOs; | 43 new chromeos::BluetoothAdapterChromeOs; |
35 new_adapter->TrackDefaultAdapter(); | 44 new_adapter->TrackDefaultAdapter(); |
36 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); | 45 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); |
37 #endif | 46 #endif |
38 } | 47 } |
39 | 48 |
| 49 callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get())); |
| 50 } |
| 51 |
| 52 // static |
| 53 scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::GetAdapter() { |
40 return scoped_refptr<BluetoothAdapter>(default_adapter.Get()); | 54 return scoped_refptr<BluetoothAdapter>(default_adapter.Get()); |
41 } | 55 } |
42 | 56 |
43 // static | 57 // static |
44 BluetoothAdapter* BluetoothAdapterFactory::Create(const std::string& address) { | 58 BluetoothAdapter* BluetoothAdapterFactory::Create(const std::string& address) { |
45 BluetoothAdapter* adapter = NULL; | 59 BluetoothAdapter* adapter = NULL; |
46 #if defined(OS_CHROMEOS) | 60 #if defined(OS_CHROMEOS) |
47 chromeos::BluetoothAdapterChromeOs* adapter_chromeos = | 61 chromeos::BluetoothAdapterChromeOs* adapter_chromeos = |
48 new chromeos::BluetoothAdapterChromeOs; | 62 new chromeos::BluetoothAdapterChromeOs; |
49 adapter_chromeos->FindAdapter(address); | 63 adapter_chromeos->FindAdapter(address); |
50 adapter = adapter_chromeos; | 64 adapter = adapter_chromeos; |
51 #endif | 65 #endif |
52 return adapter; | 66 return adapter; |
53 } | 67 } |
54 | 68 |
55 } // namespace device | 69 } // namespace device |
OLD | NEW |