| 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> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "device/bluetooth/bluetooth_adapter.h" | 14 #include "device/bluetooth/bluetooth_adapter.h" |
| 15 | 15 |
| 16 #if defined(OS_MACOSX) | 16 #if defined(OS_MACOSX) |
| 17 #include "base/mac/mac_util.h" | 17 #include "base/mac/mac_util.h" |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 namespace device { | 20 namespace device { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 static base::LazyInstance<BluetoothAdapterFactory>::Leaky g_singleton = |
| 25 LAZY_INSTANCE_INITIALIZER; |
| 26 |
| 24 // Shared default adapter instance. We don't want to keep this class around | 27 // Shared default adapter instance. We don't want to keep this class around |
| 25 // if nobody is using it, so use a WeakPtr and create the object when needed. | 28 // if nobody is using it, so use a WeakPtr and create the object when needed. |
| 26 // Since Google C++ Style (and clang's static analyzer) forbids us having | 29 // Since Google C++ Style (and clang's static analyzer) forbids us having |
| 27 // exit-time destructors, we use a leaky lazy instance for it. | 30 // exit-time destructors, we use a leaky lazy instance for it. |
| 28 base::LazyInstance<base::WeakPtr<BluetoothAdapter>>::Leaky default_adapter = | 31 base::LazyInstance<base::WeakPtr<BluetoothAdapter>>::Leaky default_adapter = |
| 29 LAZY_INSTANCE_INITIALIZER; | 32 LAZY_INSTANCE_INITIALIZER; |
| 30 | 33 |
| 31 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_CHROMEOS) | 34 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 32 typedef std::vector<BluetoothAdapterFactory::AdapterCallback> | 35 typedef std::vector<BluetoothAdapterFactory::AdapterCallback> |
| 33 AdapterCallbackList; | 36 AdapterCallbackList; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 46 iter != adapter_callbacks.Get().end(); | 49 iter != adapter_callbacks.Get().end(); |
| 47 ++iter) { | 50 ++iter) { |
| 48 iter->Run(adapter); | 51 iter->Run(adapter); |
| 49 } | 52 } |
| 50 adapter_callbacks.Get().clear(); | 53 adapter_callbacks.Get().clear(); |
| 51 } | 54 } |
| 52 #endif // defined(OS_WIN) || defined(OS_LINUX) || defined(OS_CHROMEOS) | 55 #endif // defined(OS_WIN) || defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 53 | 56 |
| 54 } // namespace | 57 } // namespace |
| 55 | 58 |
| 59 BluetoothAdapterFactory::~BluetoothAdapterFactory() {} |
| 60 |
| 61 // static |
| 62 BluetoothAdapterFactory& BluetoothAdapterFactory::Get() { |
| 63 return g_singleton.Get(); |
| 64 } |
| 65 |
| 56 // static | 66 // static |
| 57 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() { | 67 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() { |
| 58 // SetAdapterForTesting() may be used to provide a test or mock adapter | 68 // SetAdapterForTesting() may be used to provide a test or mock adapter |
| 59 // instance even on platforms that would otherwise not support it. | 69 // instance even on platforms that would otherwise not support it. |
| 60 if (default_adapter.Get()) | 70 if (default_adapter.Get()) |
| 61 return true; | 71 return true; |
| 62 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || \ | 72 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || \ |
| 63 defined(OS_LINUX) || defined(OS_MACOSX) | 73 defined(OS_LINUX) || defined(OS_MACOSX) |
| 64 return true; | 74 return true; |
| 65 #else | 75 #else |
| 66 return false; | 76 return false; |
| 67 #endif | 77 #endif |
| 68 } | 78 } |
| 69 | 79 |
| 70 // static | |
| 71 bool BluetoothAdapterFactory::IsLowEnergyAvailable() { | 80 bool BluetoothAdapterFactory::IsLowEnergyAvailable() { |
| 81 if (le_available_for_testing_) { |
| 82 return le_available_for_testing_.value(); |
| 83 } |
| 84 |
| 72 DCHECK(IsBluetoothAdapterAvailable()); | 85 DCHECK(IsBluetoothAdapterAvailable()); |
| 73 | 86 |
| 74 // SetAdapterForTesting() may be used to provide a test or mock adapter | 87 // SetAdapterForTesting() may be used to provide a test or mock adapter |
| 75 // instance even on platforms that would otherwise not support it. | 88 // instance even on platforms that would otherwise not support it. |
| 76 if (default_adapter.Get()) | 89 if (default_adapter.Get()) |
| 77 return true; | 90 return true; |
| 78 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || \ | 91 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || \ |
| 79 defined(OS_LINUX) | 92 defined(OS_LINUX) |
| 80 return true; | 93 return true; |
| 81 #elif defined(OS_MACOSX) | 94 #elif defined(OS_MACOSX) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 void BluetoothAdapterFactory::SetAdapterForTesting( | 137 void BluetoothAdapterFactory::SetAdapterForTesting( |
| 125 scoped_refptr<BluetoothAdapter> adapter) { | 138 scoped_refptr<BluetoothAdapter> adapter) { |
| 126 default_adapter.Get() = adapter->GetWeakPtrForTesting(); | 139 default_adapter.Get() = adapter->GetWeakPtrForTesting(); |
| 127 } | 140 } |
| 128 | 141 |
| 129 // static | 142 // static |
| 130 bool BluetoothAdapterFactory::HasSharedInstanceForTesting() { | 143 bool BluetoothAdapterFactory::HasSharedInstanceForTesting() { |
| 131 return default_adapter.Get() != nullptr; | 144 return default_adapter.Get() != nullptr; |
| 132 } | 145 } |
| 133 | 146 |
| 147 void BluetoothAdapterFactory::SetLowEnergyAvailableForTesting(bool available) { |
| 148 le_available_for_testing_ = base::make_optional(available); |
| 149 } |
| 150 |
| 151 BluetoothAdapterFactory::BluetoothAdapterFactory() {} |
| 152 |
| 134 } // namespace device | 153 } // namespace device |
| OLD | NEW |