Chromium Code Reviews| 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 <vector> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 7 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 8 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 10 #include "device/bluetooth/bluetooth_adapter.h" | 13 #include "device/bluetooth/bluetooth_adapter.h" |
| 11 | 14 |
| 12 #if defined(OS_CHROMEOS) | 15 #if defined(OS_CHROMEOS) |
| 13 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 16 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| 14 #elif defined(OS_WIN) | 17 #elif defined(OS_WIN) |
| 15 #include "device/bluetooth/bluetooth_adapter_win.h" | 18 #include "device/bluetooth/bluetooth_adapter_win.h" |
| 16 #endif | 19 #endif |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 23 using device::BluetoothAdapter; | |
| 24 using device::BluetoothAdapterFactory; | |
| 25 | |
| 20 // Shared default adapter instance, we don't want to keep this class around | 26 // Shared default adapter instance, we don't want to keep this class around |
| 21 // if nobody is using it so use a WeakPtr and create the object when needed; | 27 // if nobody is using it so use a WeakPtr and create the object when needed; |
| 22 // since Google C++ Style (and clang's static analyzer) forbids us having | 28 // since Google C++ Style (and clang's static analyzer) forbids us having |
| 23 // exit-time destructors we use a leaky lazy instance for it. | 29 // exit-time destructors we use a leaky lazy instance for it. |
| 24 base::LazyInstance<base::WeakPtr<device::BluetoothAdapter> >::Leaky | 30 base::LazyInstance<base::WeakPtr<device::BluetoothAdapter> >::Leaky |
| 25 default_adapter = LAZY_INSTANCE_INITIALIZER; | 31 default_adapter = LAZY_INSTANCE_INITIALIZER; |
| 26 | 32 |
| 33 std::vector<BluetoothAdapterFactory::AdapterCallback> adapter_callbacks; | |
|
Ami GONE FROM CHROMIUM
2013/01/23 17:40:58
Post-commit note:
This violates the style guide w
| |
| 34 | |
| 35 void RunAdapterCallbacks() { | |
| 36 CHECK(default_adapter.Get().get()); | |
| 37 scoped_refptr<BluetoothAdapter> adapter(default_adapter.Get()); | |
| 38 for (std::vector<BluetoothAdapterFactory::AdapterCallback>::const_iterator | |
| 39 iter = adapter_callbacks.begin(); | |
| 40 iter != adapter_callbacks.end(); | |
| 41 ++iter) { | |
| 42 iter->Run(adapter); | |
| 43 } | |
| 44 adapter_callbacks.clear(); | |
| 45 } | |
| 46 | |
| 27 } // namespace | 47 } // namespace |
| 28 | 48 |
| 29 namespace device { | 49 namespace device { |
| 30 | 50 |
| 31 // static | 51 // static |
| 32 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() { | 52 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() { |
| 33 #if defined(OS_CHROMEOS) | 53 #if defined(OS_CHROMEOS) |
| 34 return true; | 54 return true; |
| 35 #elif defined(OS_WIN) | 55 #elif defined(OS_WIN) |
| 36 return true; | 56 return true; |
| 37 #endif | 57 #endif |
| 38 return false; | 58 return false; |
| 39 } | 59 } |
| 40 | 60 |
| 41 // static | 61 // static |
| 42 void BluetoothAdapterFactory::RunCallbackOnAdapterReady( | 62 void BluetoothAdapterFactory::GetAdapter(const AdapterCallback& callback) { |
| 43 const BluetoothAdapter::AdapterCallback& callback) { | |
| 44 if (!default_adapter.Get().get()) { | 63 if (!default_adapter.Get().get()) { |
| 45 #if defined(OS_CHROMEOS) | 64 #if defined(OS_CHROMEOS) |
| 46 chromeos::BluetoothAdapterChromeOs* new_adapter = | 65 chromeos::BluetoothAdapterChromeOs* new_adapter = |
| 47 new chromeos::BluetoothAdapterChromeOs; | 66 new chromeos::BluetoothAdapterChromeOs; |
| 48 new_adapter->TrackDefaultAdapter(); | 67 new_adapter->TrackDefaultAdapter(); |
| 49 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); | 68 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); |
| 50 #elif defined(OS_WIN) | 69 #elif defined(OS_WIN) |
| 51 BluetoothAdapterWin* new_adapter = new BluetoothAdapterWin(); | 70 BluetoothAdapterWin* new_adapter = new BluetoothAdapterWin( |
| 71 base::Bind(&RunAdapterCallbacks)); | |
| 52 new_adapter->TrackDefaultAdapter(); | 72 new_adapter->TrackDefaultAdapter(); |
| 53 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); | 73 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); |
| 54 #endif | 74 #endif |
| 55 } | 75 } |
| 56 | 76 |
| 57 if (default_adapter.Get()->IsInitialized()) { | 77 if (default_adapter.Get()->IsInitialized()) { |
| 58 callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get())); | 78 callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get())); |
| 59 } else { | 79 } else { |
| 60 default_adapter.Get()->QueueAdapterCallback(callback); | 80 adapter_callbacks.push_back(callback); |
| 61 } | 81 } |
| 62 } | 82 } |
| 63 | 83 |
| 64 // static | 84 // static |
| 65 scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::GetAdapter() { | 85 scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::MaybeGetAdapter() { |
| 66 return scoped_refptr<BluetoothAdapter>(default_adapter.Get()); | 86 return scoped_refptr<BluetoothAdapter>(default_adapter.Get()); |
| 67 } | 87 } |
| 68 | 88 |
| 69 // static | 89 // static |
| 70 BluetoothAdapter* BluetoothAdapterFactory::Create(const std::string& address) { | 90 BluetoothAdapter* BluetoothAdapterFactory::Create(const std::string& address) { |
| 71 BluetoothAdapter* adapter = NULL; | 91 BluetoothAdapter* adapter = NULL; |
| 72 #if defined(OS_CHROMEOS) | 92 #if defined(OS_CHROMEOS) |
| 73 chromeos::BluetoothAdapterChromeOs* adapter_chromeos = | 93 chromeos::BluetoothAdapterChromeOs* adapter_chromeos = |
| 74 new chromeos::BluetoothAdapterChromeOs; | 94 new chromeos::BluetoothAdapterChromeOs; |
| 75 adapter_chromeos->FindAdapter(address); | 95 adapter_chromeos->FindAdapter(address); |
| 76 adapter = adapter_chromeos; | 96 adapter = adapter_chromeos; |
| 77 #elif defined(OS_WIN) | 97 #elif defined(OS_WIN) |
| 78 adapter = new BluetoothAdapterWin(); | 98 adapter = new BluetoothAdapterWin(base::Bind(&RunAdapterCallbacks)); |
| 79 #endif | 99 #endif |
| 80 return adapter; | 100 return adapter; |
| 81 } | 101 } |
| 82 | 102 |
| 83 } // namespace device | 103 } // namespace device |
| OLD | NEW |