Chromium Code Reviews| Index: content/browser/bluetooth/bluetooth_adapter_factory_wrapper.h |
| diff --git a/content/browser/bluetooth/bluetooth_adapter_factory_wrapper.h b/content/browser/bluetooth/bluetooth_adapter_factory_wrapper.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aada2f84e1454b5d2cebbc6fc96d2fff792f2ae0 |
| --- /dev/null |
| +++ b/content/browser/bluetooth/bluetooth_adapter_factory_wrapper.h |
| @@ -0,0 +1,95 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ |
| +#define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ |
| + |
| +#include <unordered_set> |
| + |
| +#include "base/macros.h" |
| +#include "content/common/content_export.h" |
| +#include "device/bluetooth/bluetooth_adapter.h" |
| + |
| +namespace content { |
| + |
| +class WebBluetoothServiceImpl; |
| + |
| +// Wrapper around BluetoothAdapterFactory that allows us to change |
| +// the underlying BluetoothAdapter object and have the observers |
| +// observe the new instance of the object. |
| +// TODO(ortuno): Once there is no need to swap the adapter to change its |
| +// behavior observers should add/remove themselves to/from the adapter. |
| +// http://crbug.com/603291 |
| +class CONTENT_EXPORT BluetoothAdapterFactoryWrapper final { |
|
Jeffrey Yasskin
2016/05/13 04:41:58
"FactoryWrapper" is also an awkward suffix. Still
ortuno
2016/05/13 20:11:17
BluetoothAdapterProvider? BluetoothAdapterManager?
Jeffrey Yasskin
2016/05/13 21:36:55
It does actually wrap the factory. We could add th
ortuno
2016/05/13 22:14:38
Ack.
|
| + public: |
| + BluetoothAdapterFactoryWrapper(); |
| + ~BluetoothAdapterFactoryWrapper(); |
| + |
| + // Returns true if the platform supports Bluetooth or if |
| + // SetBluetoothAdapterForTesting has been called. |
| + bool IsBluetoothAdapterAvailable(); |
| + |
| + // Adds |observer| to the set of adapter observers. If another observer has |
| + // acquired the adapter in the past we add |observer| as an observer to that |
|
Jeffrey Yasskin
2016/05/13 04:41:58
Be consistent about "we" vs "it".
ortuno
2016/05/13 20:11:17
Done.
|
| + // adapter, otherwise it gets a new adapter and adds |observer| to it. Runs |
| + // |continuation| after adding |observer| to an adapter. |
| + void AcquireAdapter(device::BluetoothAdapter::Observer* observer, |
| + const base::Closure& continuation); |
| + // Removes |observer| from the list of adapter observers if |observer| |
| + // has acquired the adapter in the past. If there are no more observers |
| + // it deletes the reference to the adapter. |
| + void ReleaseAdapter(device::BluetoothAdapter::Observer* observer); |
| + |
| + // Returns an adapter if |observer| has acquired an adapter in the past and |
| + // we hold a reference to an adapter. Otherwise returns nullptr. |
| + device::BluetoothAdapter* GetAdapter( |
|
Jeffrey Yasskin
2016/05/13 04:41:58
Would it make sense to pass the adapter back in th
ortuno
2016/05/13 20:11:17
Done.
|
| + device::BluetoothAdapter::Observer* observer); |
| + |
| + // The period of time a device discovery session should be active for. |
| + // If SetBluetoothAdapterForTesting has been called this returns 0. |
| + int GetScanDuration() { return scan_duration_; } |
| + |
| + // Sets a new BluetoothAdapter to be returned by GetAdapter. When setting |
| + // a new adapter all observers from the old adapter are removed and added |
| + // to |mock_adapter|. |
| + void SetBluetoothAdapterForTesting( |
| + scoped_refptr<device::BluetoothAdapter> mock_adapter); |
| + |
| + private: |
| + void OnGetAdapter(const base::Closure& continuation, |
| + scoped_refptr<device::BluetoothAdapter> adapter); |
| + |
| + bool HasAdapter(device::BluetoothAdapter::Observer* observer); |
| + void AddAdapterObserver(device::BluetoothAdapter::Observer* observer); |
| + void RemoveAdapterObserver(device::BluetoothAdapter::Observer* observer); |
| + |
| + // Set |adapter_| to a BluetoothAdapter instance and register observers, |
| + // releasing references to previous |adapter_|. |
| + void set_adapter(scoped_refptr<device::BluetoothAdapter> adapter); |
| + |
| + // A BluetoothAdapter instance representing an adapter of the system. |
| + scoped_refptr<device::BluetoothAdapter> adapter_; |
| + |
| + // We keep a list of all observers so that when the adapter gets swapped, |
| + // we can remove all observers from the old adapter and add them to the |
| + // new adapter. |
| + std::unordered_set<device::BluetoothAdapter::Observer*> adapter_observers_; |
| + |
| + // This is 0 if SetBluetoothAdapterForTesting has been called. |
| + int scan_duration_; |
|
Jeffrey Yasskin
2016/05/13 04:41:58
This should be a base::TimeDelta.
ortuno
2016/05/13 20:11:17
Done.
|
| + |
| + bool testing_; |
| + |
| + // Weak pointer factory for generating 'this' pointers that might live longer |
| + // than we do. |
| + // Note: This should remain the last member so it'll be destroyed and |
| + // invalidate its weak pointers before any other members are destroyed. |
| + base::WeakPtrFactory<BluetoothAdapterFactoryWrapper> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterFactoryWrapper); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ |