Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: device/bluetooth/bluetooth_adapter_factory_wrapper.h

Issue 2059543002: bluetooth: Move FactoryWrapper to device and expose a function for testing in chooser controller (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@my-origin
Patch Set: Fix typo Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/common/content_export.h"
13 #include "device/bluetooth/bluetooth_adapter.h" 12 #include "device/bluetooth/bluetooth_adapter.h"
13 #include "device/bluetooth/bluetooth_export.h"
14 14
15 namespace content { 15 namespace device {
16
17 class WebBluetoothServiceImpl;
18 16
19 // Wrapper around BluetoothAdapterFactory that allows us to change 17 // Wrapper around BluetoothAdapterFactory that allows us to change
20 // the underlying BluetoothAdapter object and have the observers 18 // the underlying BluetoothAdapter object and have the observers
21 // observe the new instance of the object. 19 // observe the new instance of the object.
22 // TODO(ortuno): Once there is no need to swap the adapter to change its 20 // 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. 21 // behavior observers should add/remove themselves to/from the adapter.
24 // http://crbug.com/603291 22 // http://crbug.com/603291
25 class CONTENT_EXPORT BluetoothAdapterFactoryWrapper final { 23 class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterFactoryWrapper {
26 public: 24 public:
27 typedef base::Callback<void(device::BluetoothAdapter*)> 25 typedef base::Callback<void(BluetoothAdapter*)> AcquireAdapterCallback;
28 AcquireAdapterCallback;
29 26
30 BluetoothAdapterFactoryWrapper();
31 ~BluetoothAdapterFactoryWrapper(); 27 ~BluetoothAdapterFactoryWrapper();
32 28
29 static BluetoothAdapterFactoryWrapper& Get();
30
33 // Returns true if the platform supports Bluetooth or if 31 // Returns true if the platform supports Bluetooth or if
34 // SetBluetoothAdapterForTesting has been called. 32 // SetBluetoothAdapterForTesting has been called.
35 bool IsBluetoothAdapterAvailable(); 33 bool IsBluetoothAdapterAvailable();
36 34
37 // Adds |observer| to the set of adapter observers. If another observer has 35 // 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 36 // 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 37 // adapter, otherwise it gets a new adapter and adds |observer| to it. Runs
40 // |callback| with the adapter |observer| has been added to. 38 // |callback| with the adapter |observer| has been added to.
41 void AcquireAdapter(device::BluetoothAdapter::Observer* observer, 39 void AcquireAdapter(BluetoothAdapter::Observer* observer,
42 const AcquireAdapterCallback& callback); 40 const AcquireAdapterCallback& callback);
43 // Removes |observer| from the list of adapter observers if |observer| 41 // Removes |observer| from the list of adapter observers if |observer|
44 // has acquired the adapter in the past. If there are no more observers 42 // has acquired the adapter in the past. If there are no more observers
45 // it deletes the reference to the adapter. 43 // it deletes the reference to the adapter.
46 void ReleaseAdapter(device::BluetoothAdapter::Observer* observer); 44 void ReleaseAdapter(BluetoothAdapter::Observer* observer);
47 45
48 // Returns an adapter if |observer| has acquired an adapter in the past and 46 // 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. 47 // this instance holds a reference to an adapter. Otherwise returns nullptr.
50 device::BluetoothAdapter* GetAdapter( 48 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 49
57 // Sets a new BluetoothAdapter to be returned by GetAdapter. When setting 50 // 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 51 // a new adapter all observers from the old adapter are removed and added
59 // to |mock_adapter|. 52 // to |mock_adapter|.
60 void SetBluetoothAdapterForTesting( 53 void SetBluetoothAdapterForTesting(
61 scoped_refptr<device::BluetoothAdapter> mock_adapter); 54 scoped_refptr<BluetoothAdapter> mock_adapter);
62 55
63 private: 56 private:
57 // friend LazyInstance to permit access to private constructor.
58 friend base::DefaultLazyInstanceTraits<BluetoothAdapterFactoryWrapper>;
59
60 BluetoothAdapterFactoryWrapper();
61
64 void OnGetAdapter(const AcquireAdapterCallback& continuation, 62 void OnGetAdapter(const AcquireAdapterCallback& continuation,
65 scoped_refptr<device::BluetoothAdapter> adapter); 63 scoped_refptr<BluetoothAdapter> adapter);
66 64
67 bool HasAdapter(device::BluetoothAdapter::Observer* observer); 65 bool HasAdapter(BluetoothAdapter::Observer* observer);
68 void AddAdapterObserver(device::BluetoothAdapter::Observer* observer); 66 void AddAdapterObserver(BluetoothAdapter::Observer* observer);
69 void RemoveAdapterObserver(device::BluetoothAdapter::Observer* observer); 67 void RemoveAdapterObserver(BluetoothAdapter::Observer* observer);
70 68
71 // Sets |adapter_| to a BluetoothAdapter instance and register observers, 69 // Sets |adapter_| to a BluetoothAdapter instance and register observers,
72 // releasing references to previous |adapter_|. 70 // releasing references to previous |adapter_|.
73 void set_adapter(scoped_refptr<device::BluetoothAdapter> adapter); 71 void set_adapter(scoped_refptr<BluetoothAdapter> adapter);
74 72
75 // A BluetoothAdapter instance representing an adapter of the system. 73 // A BluetoothAdapter instance representing an adapter of the system.
76 scoped_refptr<device::BluetoothAdapter> adapter_; 74 scoped_refptr<BluetoothAdapter> adapter_;
77 75
78 // We keep a list of all observers so that when the adapter gets swapped, 76 // 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 77 // we can remove all observers from the old adapter and add them to the
80 // new adapter. 78 // new adapter.
81 std::unordered_set<device::BluetoothAdapter::Observer*> adapter_observers_; 79 std::unordered_set<BluetoothAdapter::Observer*> adapter_observers_;
82
83 // This is 0 if SetBluetoothAdapterForTesting has been called.
84 base::TimeDelta scan_duration_;
85
86 bool testing_;
87 80
88 // Weak pointer factory for generating 'this' pointers that might live longer 81 // Weak pointer factory for generating 'this' pointers that might live longer
89 // than we do. 82 // than we do.
90 // Note: This should remain the last member so it'll be destroyed and 83 // Note: This should remain the last member so it'll be destroyed and
91 // invalidate its weak pointers before any other members are destroyed. 84 // invalidate its weak pointers before any other members are destroyed.
92 base::WeakPtrFactory<BluetoothAdapterFactoryWrapper> weak_ptr_factory_; 85 base::WeakPtrFactory<BluetoothAdapterFactoryWrapper> weak_ptr_factory_;
93 86
94 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterFactoryWrapper); 87 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterFactoryWrapper);
95 }; 88 };
96 89
97 } // namespace content 90 } // namespace device
98 91
99 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_ 92 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698