| 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 #if defined(OS_WIN) |
| 20 #include "base/win/windows_version.h" |
| 21 #endif |
| 19 | 22 |
| 20 namespace device { | 23 namespace device { |
| 21 | 24 |
| 22 namespace { | 25 namespace { |
| 23 | 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 = |
| (...skipping 18 matching lines...) Expand all Loading... |
| 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 |
| 56 // static | 59 // static |
| 57 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() { | 60 bool BluetoothAdapterFactory::IsBluetoothSupported() { |
| 58 // SetAdapterForTesting() may be used to provide a test or mock adapter | 61 // SetAdapterForTesting() may be used to provide a test or mock adapter |
| 59 // instance even on platforms that would otherwise not support it. | 62 // instance even on platforms that would otherwise not support it. |
| 60 if (default_adapter.Get()) | 63 if (default_adapter.Get()) |
| 61 return true; | 64 return true; |
| 62 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || \ | 65 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || \ |
| 63 defined(OS_LINUX) || defined(OS_MACOSX) | 66 defined(OS_LINUX) || defined(OS_MACOSX) |
| 64 return true; | 67 return true; |
| 65 #else | 68 #else |
| 66 return false; | 69 return false; |
| 67 #endif | 70 #endif |
| 68 } | 71 } |
| 69 | 72 |
| 70 // static | 73 // static |
| 71 bool BluetoothAdapterFactory::IsLowEnergyAvailable() { | 74 bool BluetoothAdapterFactory::IsLowEnergySupported() { |
| 72 DCHECK(IsBluetoothAdapterAvailable()); | |
| 73 | |
| 74 // SetAdapterForTesting() may be used to provide a test or mock adapter | 75 // SetAdapterForTesting() may be used to provide a test or mock adapter |
| 75 // instance even on platforms that would otherwise not support it. | 76 // instance even on platforms that would otherwise not support it. |
| 76 if (default_adapter.Get()) | 77 if (default_adapter.Get()) |
| 77 return true; | 78 return true; |
| 78 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || \ | 79 |
| 79 defined(OS_LINUX) | 80 #if defined(OS_ANDROID) && __ANDROID_API__ >= 23 |
| 80 return true; | 81 return true; |
| 82 #elif defined(OS_WIN) |
| 83 // Windows 8 supports Low Energy GATT operations but it does not support |
| 84 // scanning, initiating connections and GATT Server. To keep the API |
| 85 // consistent we consider Windows 8 as lacking Low Energy support. |
| 86 return base::win::GetVersion() >= base::win::VERSION_WIN10; |
| 81 #elif defined(OS_MACOSX) | 87 #elif defined(OS_MACOSX) |
| 82 return base::mac::IsAtLeastOS10_10(); | 88 return base::mac::IsAtLeastOS10_10(); |
| 89 #elif defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 90 return true; |
| 83 #else | 91 #else |
| 84 return false; | 92 return false; |
| 85 #endif // defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || | 93 #endif |
| 86 // defined(OS_LINUX) | |
| 87 } | 94 } |
| 88 | 95 |
| 89 // static | 96 // static |
| 90 void BluetoothAdapterFactory::GetAdapter(const AdapterCallback& callback) { | 97 void BluetoothAdapterFactory::GetAdapter(const AdapterCallback& callback) { |
| 91 DCHECK(IsBluetoothAdapterAvailable()); | 98 DCHECK(IsBluetoothSupported()); |
| 92 | 99 |
| 93 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_CHROMEOS) | 100 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 94 if (!default_adapter.Get()) { | 101 if (!default_adapter.Get()) { |
| 95 default_adapter.Get() = | 102 default_adapter.Get() = |
| 96 BluetoothAdapter::CreateAdapter(base::Bind(&RunAdapterCallbacks)); | 103 BluetoothAdapter::CreateAdapter(base::Bind(&RunAdapterCallbacks)); |
| 97 DCHECK(!default_adapter.Get()->IsInitialized()); | 104 DCHECK(!default_adapter.Get()->IsInitialized()); |
| 98 } | 105 } |
| 99 | 106 |
| 100 if (!default_adapter.Get()->IsInitialized()) | 107 if (!default_adapter.Get()->IsInitialized()) |
| 101 adapter_callbacks.Get().push_back(callback); | 108 adapter_callbacks.Get().push_back(callback); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 125 scoped_refptr<BluetoothAdapter> adapter) { | 132 scoped_refptr<BluetoothAdapter> adapter) { |
| 126 default_adapter.Get() = adapter->GetWeakPtrForTesting(); | 133 default_adapter.Get() = adapter->GetWeakPtrForTesting(); |
| 127 } | 134 } |
| 128 | 135 |
| 129 // static | 136 // static |
| 130 bool BluetoothAdapterFactory::HasSharedInstanceForTesting() { | 137 bool BluetoothAdapterFactory::HasSharedInstanceForTesting() { |
| 131 return default_adapter.Get() != nullptr; | 138 return default_adapter.Get() != nullptr; |
| 132 } | 139 } |
| 133 | 140 |
| 134 } // namespace device | 141 } // namespace device |
| OLD | NEW |