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 "base/time/time.h" | |
12 #include "content/common/content_export.h" | |
13 #include "device/bluetooth/bluetooth_adapter.h" | |
14 | |
15 namespace content { | |
16 | |
17 class WebBluetoothServiceImpl; | |
18 | |
19 // Wrapper around BluetoothAdapterFactory that allows us to change | |
20 // the underlying BluetoothAdapter object and have the observers | |
21 // observe the new instance of the object. | |
22 // 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. | |
24 // http://crbug.com/603291 | |
25 class CONTENT_EXPORT BluetoothAdapterFactoryWrapper final { | |
26 public: | |
27 typedef base::Callback<void(device::BluetoothAdapter*)> | |
28 AcquireAdapterCallback; | |
29 | |
30 BluetoothAdapterFactoryWrapper(); | |
31 ~BluetoothAdapterFactoryWrapper(); | |
32 | |
33 // Returns true if the platform supports Bluetooth or if | |
34 // SetBluetoothAdapterForTesting has been called. | |
35 bool IsBluetoothAdapterAvailable(); | |
36 | |
37 // 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 | |
39 // adapter, otherwise it gets a new adapter and adds |observer| to it. Runs | |
40 // |callback| with the adapter |observer| has been added to. | |
41 void AcquireAdapter(device::BluetoothAdapter::Observer* observer, | |
42 const AcquireAdapterCallback& callback); | |
43 // Removes |observer| from the list of adapter observers if |observer| | |
44 // has acquired the adapter in the past. If there are no more observers | |
45 // it deletes the reference to the adapter. | |
46 void ReleaseAdapter(device::BluetoothAdapter::Observer* observer); | |
47 | |
48 // 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. | |
50 device::BluetoothAdapter* GetAdapter( | |
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 | |
57 // 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 | |
59 // to |mock_adapter|. | |
60 void SetBluetoothAdapterForTesting( | |
61 scoped_refptr<device::BluetoothAdapter> mock_adapter); | |
62 | |
63 private: | |
64 void OnGetAdapter(const AcquireAdapterCallback& continuation, | |
65 scoped_refptr<device::BluetoothAdapter> adapter); | |
66 | |
67 bool HasAdapter(device::BluetoothAdapter::Observer* observer); | |
68 void AddAdapterObserver(device::BluetoothAdapter::Observer* observer); | |
69 void RemoveAdapterObserver(device::BluetoothAdapter::Observer* observer); | |
70 | |
71 // Sets |adapter_| to a BluetoothAdapter instance and register observers, | |
72 // releasing references to previous |adapter_|. | |
73 void set_adapter(scoped_refptr<device::BluetoothAdapter> adapter); | |
74 | |
75 // A BluetoothAdapter instance representing an adapter of the system. | |
76 scoped_refptr<device::BluetoothAdapter> adapter_; | |
77 | |
78 // 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 | |
80 // new adapter. | |
81 std::unordered_set<device::BluetoothAdapter::Observer*> adapter_observers_; | |
82 | |
83 // This is 0 if SetBluetoothAdapterForTesting has been called. | |
84 base::TimeDelta scan_duration_; | |
85 | |
86 bool testing_; | |
87 | |
88 // Weak pointer factory for generating 'this' pointers that might live longer | |
89 // than we do. | |
90 // Note: This should remain the last member so it'll be destroyed and | |
91 // invalidate its weak pointers before any other members are destroyed. | |
92 base::WeakPtrFactory<BluetoothAdapterFactoryWrapper> weak_ptr_factory_; | |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterFactoryWrapper); | |
95 }; | |
96 | |
97 } // namespace content | |
98 | |
99 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ | |
OLD | NEW |