| 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) |
| 17 #include "base/mac/mac_util.h" |
| 18 #endif |
| 19 |
| 16 namespace device { | 20 namespace device { |
| 17 | 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 // Shared default adapter instance. We don't want to keep this class around | 24 // 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. | 25 // 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 | 26 // Since Google C++ Style (and clang's static analyzer) forbids us having |
| 23 // exit-time destructors, we use a leaky lazy instance for it. | 27 // exit-time destructors, we use a leaky lazy instance for it. |
| 24 base::LazyInstance<base::WeakPtr<BluetoothAdapter> >::Leaky default_adapter = | 28 base::LazyInstance<base::WeakPtr<BluetoothAdapter> >::Leaky default_adapter = |
| 25 LAZY_INSTANCE_INITIALIZER; | 29 LAZY_INSTANCE_INITIALIZER; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 return true; | 61 return true; |
| 58 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || \ | 62 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || \ |
| 59 defined(OS_LINUX) || defined(OS_MACOSX) | 63 defined(OS_LINUX) || defined(OS_MACOSX) |
| 60 return true; | 64 return true; |
| 61 #else | 65 #else |
| 62 return false; | 66 return false; |
| 63 #endif | 67 #endif |
| 64 } | 68 } |
| 65 | 69 |
| 66 // static | 70 // static |
| 71 bool BluetoothAdapterFactory::IsLowEnergyAvailable() { |
| 72 DCHECK(IsBluetoothAdapterAvailable()); |
| 73 |
| 74 // SetAdapterForTesting() may be used to provide a test or mock adapter |
| 75 // instance even on platforms that would otherwise not support it. |
| 76 if (default_adapter.Get()) |
| 77 return true; |
| 78 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || \ |
| 79 defined(OS_LINUX) |
| 80 return true; |
| 81 #elif defined(OS_MACOSX) |
| 82 return base::mac::IsAtLeastOS10_10(); |
| 83 #else |
| 84 return false; |
| 85 #endif // defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN) || |
| 86 // defined(OS_LINUX) |
| 87 } |
| 88 |
| 89 // static |
| 67 void BluetoothAdapterFactory::GetAdapter(const AdapterCallback& callback) { | 90 void BluetoothAdapterFactory::GetAdapter(const AdapterCallback& callback) { |
| 68 DCHECK(IsBluetoothAdapterAvailable()); | 91 DCHECK(IsBluetoothAdapterAvailable()); |
| 69 | 92 |
| 70 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_CHROMEOS) | 93 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 71 if (!default_adapter.Get()) { | 94 if (!default_adapter.Get()) { |
| 72 default_adapter.Get() = | 95 default_adapter.Get() = |
| 73 BluetoothAdapter::CreateAdapter(base::Bind(&RunAdapterCallbacks)); | 96 BluetoothAdapter::CreateAdapter(base::Bind(&RunAdapterCallbacks)); |
| 74 DCHECK(!default_adapter.Get()->IsInitialized()); | 97 DCHECK(!default_adapter.Get()->IsInitialized()); |
| 75 } | 98 } |
| 76 | 99 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 102 scoped_refptr<BluetoothAdapter> adapter) { | 125 scoped_refptr<BluetoothAdapter> adapter) { |
| 103 default_adapter.Get() = adapter->GetWeakPtrForTesting(); | 126 default_adapter.Get() = adapter->GetWeakPtrForTesting(); |
| 104 } | 127 } |
| 105 | 128 |
| 106 // static | 129 // static |
| 107 bool BluetoothAdapterFactory::HasSharedInstanceForTesting() { | 130 bool BluetoothAdapterFactory::HasSharedInstanceForTesting() { |
| 108 return default_adapter.Get() != nullptr; | 131 return default_adapter.Get() != nullptr; |
| 109 } | 132 } |
| 110 | 133 |
| 111 } // namespace device | 134 } // namespace device |
| OLD | NEW |