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 bool is_bluetooth_supported = false; | |
32 #if defined(OS_CHROMEOS) | |
33 is_bluetooth_supported = true; | |
34 #endif | |
35 return is_bluetooth_supported; | |
bryeung
2013/01/08 22:24:27
any reason not to write this as:
#if defined(OS_C
youngki
2013/01/09 15:12:03
Done.
| |
36 } | |
37 | |
38 // static | |
39 void BluetoothAdapterFactory::RunCallbackOnAdapterReady( | |
40 const AdapterCallback& callback) { | |
31 if (!default_adapter.Get().get()) { | 41 if (!default_adapter.Get().get()) { |
32 #if defined(OS_CHROMEOS) | 42 #if defined(OS_CHROMEOS) |
33 chromeos::BluetoothAdapterChromeOs* new_adapter = | 43 chromeos::BluetoothAdapterChromeOs* new_adapter = |
34 new chromeos::BluetoothAdapterChromeOs; | 44 new chromeos::BluetoothAdapterChromeOs; |
35 new_adapter->TrackDefaultAdapter(); | 45 new_adapter->TrackDefaultAdapter(); |
36 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); | 46 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); |
37 #endif | 47 #endif |
38 } | 48 } |
39 | 49 |
50 if (default_adapter.Get().get()) | |
51 callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get())); | |
52 } | |
53 | |
54 // static | |
55 scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::GetAdapter() { | |
40 return scoped_refptr<BluetoothAdapter>(default_adapter.Get()); | 56 return scoped_refptr<BluetoothAdapter>(default_adapter.Get()); |
41 } | 57 } |
42 | 58 |
43 // static | 59 // static |
44 BluetoothAdapter* BluetoothAdapterFactory::Create(const std::string& address) { | 60 BluetoothAdapter* BluetoothAdapterFactory::Create(const std::string& address) { |
45 BluetoothAdapter* adapter = NULL; | 61 BluetoothAdapter* adapter = NULL; |
46 #if defined(OS_CHROMEOS) | 62 #if defined(OS_CHROMEOS) |
47 chromeos::BluetoothAdapterChromeOs* adapter_chromeos = | 63 chromeos::BluetoothAdapterChromeOs* adapter_chromeos = |
48 new chromeos::BluetoothAdapterChromeOs; | 64 new chromeos::BluetoothAdapterChromeOs; |
49 adapter_chromeos->FindAdapter(address); | 65 adapter_chromeos->FindAdapter(address); |
50 adapter = adapter_chromeos; | 66 adapter = adapter_chromeos; |
51 #endif | 67 #endif |
52 return adapter; | 68 return adapter; |
53 } | 69 } |
54 | 70 |
55 } // namespace device | 71 } // namespace device |
OLD | NEW |