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