OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ |
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ |
7 | 7 |
8 #include <unordered_set> | 8 #include <unordered_set> |
9 | 9 |
| 10 #include "base/lazy_instance.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/time/time.h" | 12 #include "base/threading/thread_checker.h" |
12 #include "content/common/content_export.h" | |
13 #include "device/bluetooth/bluetooth_adapter.h" | 13 #include "device/bluetooth/bluetooth_adapter.h" |
| 14 #include "device/bluetooth/bluetooth_export.h" |
14 | 15 |
15 namespace content { | 16 namespace device { |
16 | |
17 class WebBluetoothServiceImpl; | |
18 | 17 |
19 // Wrapper around BluetoothAdapterFactory that allows us to change | 18 // Wrapper around BluetoothAdapterFactory that allows us to change |
20 // the underlying BluetoothAdapter object and have the observers | 19 // the underlying BluetoothAdapter object and have the observers |
21 // observe the new instance of the object. | 20 // observe the new instance of the object. |
22 // TODO(ortuno): Once there is no need to swap the adapter to change its | 21 // TODO(ortuno): Once there is no need to swap the adapter to change its |
23 // behavior observers should add/remove themselves to/from the adapter. | 22 // behavior observers should add/remove themselves to/from the adapter. |
24 // http://crbug.com/603291 | 23 // http://crbug.com/603291 |
25 class CONTENT_EXPORT BluetoothAdapterFactoryWrapper final { | 24 class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterFactoryWrapper { |
26 public: | 25 public: |
27 typedef base::Callback<void(device::BluetoothAdapter*)> | 26 typedef base::Callback<void(BluetoothAdapter*)> AcquireAdapterCallback; |
28 AcquireAdapterCallback; | |
29 | 27 |
30 BluetoothAdapterFactoryWrapper(); | |
31 ~BluetoothAdapterFactoryWrapper(); | 28 ~BluetoothAdapterFactoryWrapper(); |
32 | 29 |
| 30 static BluetoothAdapterFactoryWrapper& Get(); |
| 31 |
33 // Returns true if the platform supports Bluetooth or if | 32 // Returns true if the platform supports Bluetooth or if |
34 // SetBluetoothAdapterForTesting has been called. | 33 // SetBluetoothAdapterForTesting has been called. |
35 bool IsBluetoothAdapterAvailable(); | 34 bool IsBluetoothAdapterAvailable(); |
36 | 35 |
37 // Adds |observer| to the set of adapter observers. If another observer has | 36 // Adds |observer| to the set of adapter observers. If another observer has |
38 // acquired the adapter in the past it adds |observer| as an observer to that | 37 // acquired the adapter in the past it adds |observer| as an observer to that |
39 // adapter, otherwise it gets a new adapter and adds |observer| to it. Runs | 38 // adapter, otherwise it gets a new adapter and adds |observer| to it. Runs |
40 // |callback| with the adapter |observer| has been added to. | 39 // |callback| with the adapter |observer| has been added to. |
41 void AcquireAdapter(device::BluetoothAdapter::Observer* observer, | 40 void AcquireAdapter(BluetoothAdapter::Observer* observer, |
42 const AcquireAdapterCallback& callback); | 41 const AcquireAdapterCallback& callback); |
43 // Removes |observer| from the list of adapter observers if |observer| | 42 // Removes |observer| from the list of adapter observers if |observer| |
44 // has acquired the adapter in the past. If there are no more observers | 43 // has acquired the adapter in the past. If there are no more observers |
45 // it deletes the reference to the adapter. | 44 // it deletes the reference to the adapter. |
46 void ReleaseAdapter(device::BluetoothAdapter::Observer* observer); | 45 void ReleaseAdapter(BluetoothAdapter::Observer* observer); |
47 | 46 |
48 // Returns an adapter if |observer| has acquired an adapter in the past and | 47 // Returns an adapter if |observer| has acquired an adapter in the past and |
49 // this instance holds a reference to an adapter. Otherwise returns nullptr. | 48 // this instance holds a reference to an adapter. Otherwise returns nullptr. |
50 device::BluetoothAdapter* GetAdapter( | 49 BluetoothAdapter* GetAdapter(BluetoothAdapter::Observer* observer); |
51 device::BluetoothAdapter::Observer* observer); | |
52 | |
53 // The period of time a device discovery session should be active for. | |
54 // Returns 0 if SetBluetoothAdapterForTesting has been called. | |
55 base::TimeDelta GetScanDuration() { return scan_duration_; } | |
56 | 50 |
57 // Sets a new BluetoothAdapter to be returned by GetAdapter. When setting | 51 // Sets a new BluetoothAdapter to be returned by GetAdapter. When setting |
58 // a new adapter all observers from the old adapter are removed and added | 52 // a new adapter all observers from the old adapter are removed and added |
59 // to |mock_adapter|. | 53 // to |mock_adapter|. |
60 void SetBluetoothAdapterForTesting( | 54 void SetBluetoothAdapterForTesting( |
61 scoped_refptr<device::BluetoothAdapter> mock_adapter); | 55 scoped_refptr<BluetoothAdapter> mock_adapter); |
62 | 56 |
63 private: | 57 private: |
| 58 // friend LazyInstance to permit access to private constructor. |
| 59 friend base::DefaultLazyInstanceTraits<BluetoothAdapterFactoryWrapper>; |
| 60 |
| 61 BluetoothAdapterFactoryWrapper(); |
| 62 |
64 void OnGetAdapter(const AcquireAdapterCallback& continuation, | 63 void OnGetAdapter(const AcquireAdapterCallback& continuation, |
65 scoped_refptr<device::BluetoothAdapter> adapter); | 64 scoped_refptr<BluetoothAdapter> adapter); |
66 | 65 |
67 bool HasAdapter(device::BluetoothAdapter::Observer* observer); | 66 bool HasAdapter(BluetoothAdapter::Observer* observer); |
68 void AddAdapterObserver(device::BluetoothAdapter::Observer* observer); | 67 void AddAdapterObserver(BluetoothAdapter::Observer* observer); |
69 void RemoveAdapterObserver(device::BluetoothAdapter::Observer* observer); | 68 void RemoveAdapterObserver(BluetoothAdapter::Observer* observer); |
70 | 69 |
71 // Sets |adapter_| to a BluetoothAdapter instance and register observers, | 70 // Sets |adapter_| to a BluetoothAdapter instance and register observers, |
72 // releasing references to previous |adapter_|. | 71 // releasing references to previous |adapter_|. |
73 void set_adapter(scoped_refptr<device::BluetoothAdapter> adapter); | 72 void set_adapter(scoped_refptr<BluetoothAdapter> adapter); |
74 | 73 |
75 // A BluetoothAdapter instance representing an adapter of the system. | 74 // A BluetoothAdapter instance representing an adapter of the system. |
76 scoped_refptr<device::BluetoothAdapter> adapter_; | 75 scoped_refptr<BluetoothAdapter> adapter_; |
77 | 76 |
78 // We keep a list of all observers so that when the adapter gets swapped, | 77 // We keep a list of all observers so that when the adapter gets swapped, |
79 // we can remove all observers from the old adapter and add them to the | 78 // we can remove all observers from the old adapter and add them to the |
80 // new adapter. | 79 // new adapter. |
81 std::unordered_set<device::BluetoothAdapter::Observer*> adapter_observers_; | 80 std::unordered_set<BluetoothAdapter::Observer*> adapter_observers_; |
82 | 81 |
83 // This is 0 if SetBluetoothAdapterForTesting has been called. | 82 // Should only be called on the UI thread. |
84 base::TimeDelta scan_duration_; | 83 base::ThreadChecker thread_checker_; |
85 | |
86 bool testing_; | |
87 | 84 |
88 // Weak pointer factory for generating 'this' pointers that might live longer | 85 // Weak pointer factory for generating 'this' pointers that might live longer |
89 // than we do. | 86 // than we do. |
90 // Note: This should remain the last member so it'll be destroyed and | 87 // Note: This should remain the last member so it'll be destroyed and |
91 // invalidate its weak pointers before any other members are destroyed. | 88 // invalidate its weak pointers before any other members are destroyed. |
92 base::WeakPtrFactory<BluetoothAdapterFactoryWrapper> weak_ptr_factory_; | 89 base::WeakPtrFactory<BluetoothAdapterFactoryWrapper> weak_ptr_factory_; |
93 | 90 |
94 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterFactoryWrapper); | 91 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterFactoryWrapper); |
95 }; | 92 }; |
96 | 93 |
97 } // namespace content | 94 } // namespace device |
98 | 95 |
99 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ | 96 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ |
OLD | NEW |