| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_DISPATCHER_HOST_H_ | 5 #ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ |
| 6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ | 6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 | 11 |
| 12 #include "base/id_map.h" | 12 #include "base/id_map.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | 16 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" |
| 17 #include "content/public/browser/bluetooth_chooser.h" | 17 #include "content/public/browser/bluetooth_chooser.h" |
| 18 #include "content/public/browser/browser_message_filter.h" | 18 #include "content/public/browser/browser_message_filter.h" |
| 19 #include "device/bluetooth/bluetooth_adapter.h" | 19 #include "device/bluetooth/bluetooth_adapter.h" |
| 20 #include "device/bluetooth/bluetooth_gatt_connection.h" | |
| 21 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | 20 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
| 22 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 21 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
| 23 | 22 |
| 24 namespace device { | 23 namespace device { |
| 25 class BluetoothUUID; | 24 class BluetoothUUID; |
| 26 } | 25 } |
| 27 | 26 |
| 28 namespace content { | 27 namespace content { |
| 29 | 28 |
| 30 class WebBluetoothServiceImpl; | 29 class WebBluetoothServiceImpl; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 protected: | 66 protected: |
| 68 ~BluetoothDispatcherHost() override; | 67 ~BluetoothDispatcherHost() override; |
| 69 | 68 |
| 70 private: | 69 private: |
| 71 friend class WebBluetoothServiceImpl; | 70 friend class WebBluetoothServiceImpl; |
| 72 friend class base::DeleteHelper<BluetoothDispatcherHost>; | 71 friend class base::DeleteHelper<BluetoothDispatcherHost>; |
| 73 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 72 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
| 74 | 73 |
| 75 struct RequestDeviceSession; | 74 struct RequestDeviceSession; |
| 76 | 75 |
| 77 // Map to keep track of connections. Inserting and removing connections | |
| 78 // will update the Web Contents for the frame. Upon destruction | |
| 79 // the map will clear Web Contents of Bluetooth connections. | |
| 80 struct ConnectedDevicesMap { | |
| 81 ConnectedDevicesMap(int render_process_id); | |
| 82 ~ConnectedDevicesMap(); | |
| 83 bool HasActiveConnection(const std::string& device_id); | |
| 84 void InsertOrReplace( | |
| 85 int frame_routing_id, | |
| 86 const std::string& device_id, | |
| 87 std::unique_ptr<device::BluetoothGattConnection> connection); | |
| 88 void Remove(int frame_routing_id, const std::string& device_id); | |
| 89 void IncrementBluetoothConnectedDeviceCount(int frame_routing_id); | |
| 90 void DecrementBluetoothConnectedDeviceCount(int frame_routing_id); | |
| 91 | |
| 92 int render_process_id_; | |
| 93 std::unordered_map<std::string, | |
| 94 std::unique_ptr<device::BluetoothGattConnection>> | |
| 95 device_id_to_connection_map_; | |
| 96 // Keeps track of which frame is connected to which device so that | |
| 97 // we can clean up the WebContents in our destructor. | |
| 98 std::set<std::pair<int, std::string>> frame_ids_device_ids_; | |
| 99 }; | |
| 100 | |
| 101 // Set |adapter_| to a BluetoothAdapter instance and register observers, | 76 // Set |adapter_| to a BluetoothAdapter instance and register observers, |
| 102 // releasing references to previous |adapter_|. | 77 // releasing references to previous |adapter_|. |
| 103 // | 78 // |
| 104 // We currently keep track of observers that used BluetoothDispatcherHost | 79 // We currently keep track of observers that used BluetoothDispatcherHost |
| 105 // to register themselves on the adapter and remove them from |adapter_| and | 80 // to register themselves on the adapter and remove them from |adapter_| and |
| 106 // add them to |adapter| when this function is called. | 81 // add them to |adapter| when this function is called. |
| 107 // TODO(ortuno): Observers should add/remove themselves to/from the adapter. | 82 // TODO(ortuno): Observers should add/remove themselves to/from the adapter. |
| 108 // http://crbug.com/603291 | 83 // http://crbug.com/603291 |
| 109 void set_adapter(scoped_refptr<device::BluetoothAdapter> adapter); | 84 void set_adapter(scoped_refptr<device::BluetoothAdapter> adapter); |
| 110 | 85 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 124 void DeviceRemoved(device::BluetoothAdapter* adapter, | 99 void DeviceRemoved(device::BluetoothAdapter* adapter, |
| 125 device::BluetoothDevice* device) override; | 100 device::BluetoothDevice* device) override; |
| 126 | 101 |
| 127 // IPC Handlers, see definitions in bluetooth_messages.h. | 102 // IPC Handlers, see definitions in bluetooth_messages.h. |
| 128 void OnRequestDevice( | 103 void OnRequestDevice( |
| 129 int thread_id, | 104 int thread_id, |
| 130 int request_id, | 105 int request_id, |
| 131 int frame_routing_id, | 106 int frame_routing_id, |
| 132 const std::vector<content::BluetoothScanFilter>& filters, | 107 const std::vector<content::BluetoothScanFilter>& filters, |
| 133 const std::vector<device::BluetoothUUID>& optional_services); | 108 const std::vector<device::BluetoothUUID>& optional_services); |
| 134 void OnGATTServerConnect(int thread_id, | |
| 135 int request_id, | |
| 136 int frame_routing_id, | |
| 137 const std::string& device_id); | |
| 138 void OnGATTServerDisconnect(int thread_id, | |
| 139 int frame_routing_id, | |
| 140 const std::string& device_id); | |
| 141 | 109 |
| 142 // Callbacks for BluetoothDevice::OnRequestDevice. | 110 // Callbacks for BluetoothDevice::OnRequestDevice. |
| 143 // If necessary, the adapter must be obtained before continuing to Impl. | 111 // If necessary, the adapter must be obtained before continuing to Impl. |
| 144 void OnGetAdapter(base::Closure continuation, | 112 void OnGetAdapter(base::Closure continuation, |
| 145 scoped_refptr<device::BluetoothAdapter> adapter); | 113 scoped_refptr<device::BluetoothAdapter> adapter); |
| 146 void OnRequestDeviceImpl( | 114 void OnRequestDeviceImpl( |
| 147 int thread_id, | 115 int thread_id, |
| 148 int request_id, | 116 int request_id, |
| 149 int frame_routing_id, | 117 int frame_routing_id, |
| 150 const std::vector<content::BluetoothScanFilter>& filters, | 118 const std::vector<content::BluetoothScanFilter>& filters, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 161 BluetoothChooser::Event event, | 129 BluetoothChooser::Event event, |
| 162 const std::string& device_id); | 130 const std::string& device_id); |
| 163 | 131 |
| 164 // The chooser implementation yields to the event loop to avoid re-entering | 132 // The chooser implementation yields to the event loop to avoid re-entering |
| 165 // code that's still using the RequestDeviceSession, and continues with this | 133 // code that's still using the RequestDeviceSession, and continues with this |
| 166 // function. | 134 // function. |
| 167 void FinishClosingChooser(int chooser_id, | 135 void FinishClosingChooser(int chooser_id, |
| 168 BluetoothChooser::Event event, | 136 BluetoothChooser::Event event, |
| 169 const std::string& device_id); | 137 const std::string& device_id); |
| 170 | 138 |
| 171 // Callbacks for BluetoothDevice::CreateGattConnection. | |
| 172 void OnGATTConnectionCreated( | |
| 173 int thread_id, | |
| 174 int request_id, | |
| 175 int frame_routing_id, | |
| 176 const std::string& device_id, | |
| 177 base::TimeTicks start_time, | |
| 178 std::unique_ptr<device::BluetoothGattConnection> connection); | |
| 179 void OnCreateGATTConnectionError( | |
| 180 int thread_id, | |
| 181 int request_id, | |
| 182 const std::string& device_id, | |
| 183 base::TimeTicks start_time, | |
| 184 device::BluetoothDevice::ConnectErrorCode error_code); | |
| 185 | |
| 186 // Functions to query the platform cache for the bluetooth object. | 139 // Functions to query the platform cache for the bluetooth object. |
| 187 // result.outcome == CacheQueryOutcome::SUCCESS if the object was found in the | 140 // result.outcome == CacheQueryOutcome::SUCCESS if the object was found in the |
| 188 // cache. Otherwise result.outcome that can used to record the outcome and | 141 // cache. Otherwise result.outcome that can used to record the outcome and |
| 189 // result.error will contain the error that should be send to the renderer. | 142 // result.error will contain the error that should be send to the renderer. |
| 190 // One of the possible outcomes is BAD_RENDERER. In this case the outcome | 143 // One of the possible outcomes is BAD_RENDERER. In this case the outcome |
| 191 // was already recorded and since there renderer crashed there is no need to | 144 // was already recorded and since there renderer crashed there is no need to |
| 192 // send a response. | 145 // send a response. |
| 193 | |
| 194 // Queries the platform cache for a Device with |device_id| for |origin|. | 146 // Queries the platform cache for a Device with |device_id| for |origin|. |
| 195 // Fills in the |outcome| field and the |device| field if successful. | 147 // Fills in the |outcome| field and the |device| field if successful. |
| 196 CacheQueryResult QueryCacheForDevice(const url::Origin& origin, | 148 CacheQueryResult QueryCacheForDevice(const url::Origin& origin, |
| 197 const std::string& device_id); | 149 const std::string& device_id); |
| 198 | 150 |
| 199 // Returns the origin for the frame with "frame_routing_id" in | |
| 200 // render_process_id_. | |
| 201 url::Origin GetOrigin(int frame_routing_id); | |
| 202 | |
| 203 int render_process_id_; | 151 int render_process_id_; |
| 204 | 152 |
| 205 // Maps a (thread_id,request_id) to information about its requestDevice call, | 153 // Maps a (thread_id,request_id) to information about its requestDevice call, |
| 206 // including the chooser dialog. | 154 // including the chooser dialog. |
| 207 // An entry is added to this map in OnRequestDevice, and should be removed | 155 // An entry is added to this map in OnRequestDevice, and should be removed |
| 208 // again everywhere a requestDevice() reply is sent. | 156 // again everywhere a requestDevice() reply is sent. |
| 209 IDMap<RequestDeviceSession, IDMapOwnPointer> request_device_sessions_; | 157 IDMap<RequestDeviceSession, IDMapOwnPointer> request_device_sessions_; |
| 210 | 158 |
| 211 BluetoothAllowedDevicesMap allowed_devices_map_; | 159 BluetoothAllowedDevicesMap allowed_devices_map_; |
| 212 | 160 |
| 213 // Defines how long to scan for and how long to discover services for. | 161 // Defines how long to scan for and how long to discover services for. |
| 214 int current_delay_time_; | 162 int current_delay_time_; |
| 215 | 163 |
| 216 // A BluetoothAdapter instance representing an adapter of the system. | 164 // A BluetoothAdapter instance representing an adapter of the system. |
| 217 scoped_refptr<device::BluetoothAdapter> adapter_; | 165 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 218 | 166 |
| 219 std::unordered_set<device::BluetoothAdapter::Observer*> adapter_observers_; | 167 std::unordered_set<device::BluetoothAdapter::Observer*> adapter_observers_; |
| 220 | 168 |
| 221 // Automatically stops Bluetooth discovery a set amount of time after it was | 169 // Automatically stops Bluetooth discovery a set amount of time after it was |
| 222 // started. We have a single timer for all of Web Bluetooth because it's | 170 // started. We have a single timer for all of Web Bluetooth because it's |
| 223 // simpler than tracking timeouts for each RequestDeviceSession individually, | 171 // simpler than tracking timeouts for each RequestDeviceSession individually, |
| 224 // and because there's no harm in extending the length of a few discovery | 172 // and because there's no harm in extending the length of a few discovery |
| 225 // sessions when other sessions are active. | 173 // sessions when other sessions are active. |
| 226 base::Timer discovery_session_timer_; | 174 base::Timer discovery_session_timer_; |
| 227 | 175 |
| 228 // Retains BluetoothGattConnection objects to keep connections open. | |
| 229 std::unique_ptr<ConnectedDevicesMap> connected_devices_map_; | |
| 230 | |
| 231 // |weak_ptr_on_ui_thread_| provides weak pointers, e.g. for callbacks, and | 176 // |weak_ptr_on_ui_thread_| provides weak pointers, e.g. for callbacks, and |
| 232 // because it exists and has been bound to the UI thread enforces that all | 177 // because it exists and has been bound to the UI thread enforces that all |
| 233 // copies verify they are also used on the UI thread. | 178 // copies verify they are also used on the UI thread. |
| 234 base::WeakPtr<BluetoothDispatcherHost> weak_ptr_on_ui_thread_; | 179 base::WeakPtr<BluetoothDispatcherHost> weak_ptr_on_ui_thread_; |
| 235 base::WeakPtrFactory<BluetoothDispatcherHost> weak_ptr_factory_; | 180 base::WeakPtrFactory<BluetoothDispatcherHost> weak_ptr_factory_; |
| 236 | 181 |
| 237 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost); | 182 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost); |
| 238 }; | 183 }; |
| 239 | 184 |
| 240 } // namespace content | 185 } // namespace content |
| 241 | 186 |
| 242 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ | 187 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ |
| OLD | NEW |