OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_DISPATCHER_HOST_H_ | |
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <map> | |
11 | |
12 #include "base/id_map.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/scoped_vector.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | |
17 #include "content/public/browser/bluetooth_chooser.h" | |
18 #include "content/public/browser/browser_message_filter.h" | |
19 #include "device/bluetooth/bluetooth_adapter.h" | |
20 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | |
21 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | |
22 | |
23 namespace device { | |
24 class BluetoothUUID; | |
25 } | |
26 | |
27 namespace content { | |
28 | |
29 class WebBluetoothServiceImpl; | |
30 | |
31 struct BluetoothScanFilter; | |
32 struct CacheQueryResult; | |
33 | |
34 // Dispatches and sends bluetooth related messages sent to/from a child | |
35 // process BluetoothDispatcher from/to the main browser process. | |
36 // | |
37 // Intended to be instantiated by the RenderProcessHost and installed as | |
38 // a filter on the channel. BrowserMessageFilter is refcounted and typically | |
39 // lives as long as it is installed on a channel. | |
40 // | |
41 // UI Thread Note: | |
42 // BluetoothDispatcherHost is constructed, operates, and destroyed on the UI | |
43 // thread because BluetoothAdapter and related objects live there. | |
44 class CONTENT_EXPORT BluetoothDispatcherHost final | |
45 : public BrowserMessageFilter, | |
46 public device::BluetoothAdapter::Observer { | |
47 public: | |
48 BluetoothDispatcherHost(int render_process_id); | |
49 // BrowserMessageFilter: | |
50 void OnDestruct() const override; | |
51 void OverrideThreadForMessage(const IPC::Message& message, | |
52 BrowserThread::ID* thread) override; | |
53 bool OnMessageReceived(const IPC::Message& message) override; | |
54 | |
55 void SetBluetoothAdapterForTesting( | |
56 scoped_refptr<device::BluetoothAdapter> mock_adapter); | |
57 | |
58 // Temporary functions so that WebBluetoothServices can add themselves as | |
59 // observers of the Bluetooth Adapter without having to get an adapter for | |
60 // themselves. | |
61 // TODO(ortuno): Remove once WebBluetoothServiceImpl gets its own adapter. | |
62 // https://crbug.com/508771 | |
63 void AddAdapterObserver(device::BluetoothAdapter::Observer* observer); | |
64 void RemoveAdapterObserver(device::BluetoothAdapter::Observer* observer); | |
65 | |
66 protected: | |
67 ~BluetoothDispatcherHost() override; | |
68 | |
69 private: | |
70 friend class WebBluetoothServiceImpl; | |
71 friend class base::DeleteHelper<BluetoothDispatcherHost>; | |
72 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | |
73 | |
74 struct RequestDeviceSession; | |
75 | |
76 // Set |adapter_| to a BluetoothAdapter instance and register observers, | |
77 // releasing references to previous |adapter_|. | |
78 // | |
79 // We currently keep track of observers that used BluetoothDispatcherHost | |
80 // to register themselves on the adapter and remove them from |adapter_| and | |
81 // add them to |adapter| when this function is called. | |
82 // TODO(ortuno): Observers should add/remove themselves to/from the adapter. | |
83 // http://crbug.com/603291 | |
84 void set_adapter(scoped_refptr<device::BluetoothAdapter> adapter); | |
85 | |
86 // Makes sure a BluetoothDiscoverySession is active for |session|, and resets | |
87 // its timeout. | |
88 void StartDeviceDiscovery(RequestDeviceSession* session, int chooser_id); | |
89 | |
90 // Stops all BluetoothDiscoverySessions being run for requestDevice() | |
91 // choosers. | |
92 void StopDeviceDiscovery(); | |
93 | |
94 // BluetoothAdapter::Observer: | |
95 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, | |
96 bool powered) override; | |
97 void DeviceAdded(device::BluetoothAdapter* adapter, | |
98 device::BluetoothDevice* device) override; | |
99 void DeviceRemoved(device::BluetoothAdapter* adapter, | |
100 device::BluetoothDevice* device) override; | |
101 | |
102 // IPC Handlers, see definitions in bluetooth_messages.h. | |
103 void OnRequestDevice( | |
104 int thread_id, | |
105 int request_id, | |
106 int frame_routing_id, | |
107 const std::vector<content::BluetoothScanFilter>& filters, | |
108 const std::vector<device::BluetoothUUID>& optional_services); | |
109 | |
110 // Callbacks for BluetoothDevice::OnRequestDevice. | |
111 // If necessary, the adapter must be obtained before continuing to Impl. | |
112 void OnGetAdapter(base::Closure continuation, | |
113 scoped_refptr<device::BluetoothAdapter> adapter); | |
114 void OnRequestDeviceImpl( | |
115 int thread_id, | |
116 int request_id, | |
117 int frame_routing_id, | |
118 const std::vector<content::BluetoothScanFilter>& filters, | |
119 const std::vector<device::BluetoothUUID>& optional_services); | |
120 | |
121 // Callbacks for BluetoothAdapter::StartDiscoverySession. | |
122 void OnDiscoverySessionStarted( | |
123 int chooser_id, | |
124 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session); | |
125 void OnDiscoverySessionStartedError(int chooser_id); | |
126 | |
127 // BluetoothChooser::EventHandler: | |
128 void OnBluetoothChooserEvent(int chooser_id, | |
129 BluetoothChooser::Event event, | |
130 const std::string& device_id); | |
131 | |
132 // The chooser implementation yields to the event loop to avoid re-entering | |
133 // code that's still using the RequestDeviceSession, and continues with this | |
134 // function. | |
135 void FinishClosingChooser(int chooser_id, | |
136 BluetoothChooser::Event event, | |
137 const std::string& device_id); | |
138 | |
139 // Functions to query the platform cache for the bluetooth object. | |
140 // result.outcome == CacheQueryOutcome::SUCCESS if the object was found in the | |
141 // cache. Otherwise result.outcome that can used to record the outcome and | |
142 // result.error will contain the error that should be send to the renderer. | |
143 // One of the possible outcomes is BAD_RENDERER. In this case the outcome | |
144 // was already recorded and since there renderer crashed there is no need to | |
145 // send a response. | |
146 // Queries the platform cache for a Device with |device_id| for |origin|. | |
147 // Fills in the |outcome| field and the |device| field if successful. | |
148 CacheQueryResult QueryCacheForDevice(const url::Origin& origin, | |
149 const std::string& device_id); | |
150 | |
151 int render_process_id_; | |
152 | |
153 // Maps a (thread_id,request_id) to information about its requestDevice call, | |
154 // including the chooser dialog. | |
155 // An entry is added to this map in OnRequestDevice, and should be removed | |
156 // again everywhere a requestDevice() reply is sent. | |
157 IDMap<RequestDeviceSession, IDMapOwnPointer> request_device_sessions_; | |
158 | |
159 BluetoothAllowedDevicesMap allowed_devices_map_; | |
160 | |
161 // Defines how long to scan for and how long to discover services for. | |
162 int current_delay_time_; | |
163 | |
164 // A BluetoothAdapter instance representing an adapter of the system. | |
165 scoped_refptr<device::BluetoothAdapter> adapter_; | |
166 | |
167 std::unordered_set<device::BluetoothAdapter::Observer*> adapter_observers_; | |
168 | |
169 // Automatically stops Bluetooth discovery a set amount of time after it was | |
170 // started. We have a single timer for all of Web Bluetooth because it's | |
171 // simpler than tracking timeouts for each RequestDeviceSession individually, | |
172 // and because there's no harm in extending the length of a few discovery | |
173 // sessions when other sessions are active. | |
174 base::Timer discovery_session_timer_; | |
175 | |
176 // |weak_ptr_on_ui_thread_| provides weak pointers, e.g. for callbacks, and | |
177 // because it exists and has been bound to the UI thread enforces that all | |
178 // copies verify they are also used on the UI thread. | |
179 base::WeakPtr<BluetoothDispatcherHost> weak_ptr_on_ui_thread_; | |
180 base::WeakPtrFactory<BluetoothDispatcherHost> weak_ptr_factory_; | |
181 | |
182 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost); | |
183 }; | |
184 | |
185 } // namespace content | |
186 | |
187 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ | |
OLD | NEW |