| 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 "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/id_map.h" |
| 9 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "content/public/browser/bluetooth_chooser.h" |
| 10 #include "content/public/browser/browser_message_filter.h" | 12 #include "content/public/browser/browser_message_filter.h" |
| 11 #include "device/bluetooth/bluetooth_adapter.h" | 13 #include "device/bluetooth/bluetooth_adapter.h" |
| 12 #include "device/bluetooth/bluetooth_gatt_connection.h" | 14 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 13 #include "device/bluetooth/bluetooth_gatt_service.h" | 15 #include "device/bluetooth/bluetooth_gatt_service.h" |
| 14 | 16 |
| 15 namespace device { | 17 namespace device { |
| 16 class BluetoothUUID; | 18 class BluetoothUUID; |
| 17 } | 19 } |
| 18 | 20 |
| 19 namespace content { | 21 namespace content { |
| 20 | 22 |
| 21 struct BluetoothScanFilter; | 23 struct BluetoothScanFilter; |
| 22 | 24 |
| 23 // Dispatches and sends bluetooth related messages sent to/from a child | 25 // Dispatches and sends bluetooth related messages sent to/from a child |
| 24 // process BluetoothDispatcher from/to the main browser process. | 26 // process BluetoothDispatcher from/to the main browser process. |
| 25 // | 27 // |
| 26 // Intended to be instantiated by the RenderProcessHost and installed as | 28 // Intended to be instantiated by the RenderProcessHost and installed as |
| 27 // a filter on the channel. BrowserMessageFilter is refcounted and typically | 29 // a filter on the channel. BrowserMessageFilter is refcounted and typically |
| 28 // lives as long as it is installed on a channel. | 30 // lives as long as it is installed on a channel. |
| 29 // | 31 // |
| 30 // UI Thread Note: | 32 // UI Thread Note: |
| 31 // BluetoothDispatcherHost is constructed, operates, and destroyed on the UI | 33 // BluetoothDispatcherHost is constructed, operates, and destroyed on the UI |
| 32 // thread because BluetoothAdapter and related objects live there. | 34 // thread because BluetoothAdapter and related objects live there. |
| 33 class CONTENT_EXPORT BluetoothDispatcherHost final | 35 class CONTENT_EXPORT BluetoothDispatcherHost final |
| 34 : public BrowserMessageFilter, | 36 : public BrowserMessageFilter, |
| 37 public BluetoothChooser::Observer, |
| 35 public device::BluetoothAdapter::Observer { | 38 public device::BluetoothAdapter::Observer { |
| 36 public: | 39 public: |
| 37 BluetoothDispatcherHost(int render_process_id); | 40 BluetoothDispatcherHost(int render_process_id); |
| 38 // BrowserMessageFilter: | 41 // BrowserMessageFilter: |
| 39 void OnDestruct() const override; | 42 void OnDestruct() const override; |
| 40 void OverrideThreadForMessage(const IPC::Message& message, | 43 void OverrideThreadForMessage(const IPC::Message& message, |
| 41 BrowserThread::ID* thread) override; | 44 BrowserThread::ID* thread) override; |
| 42 bool OnMessageReceived(const IPC::Message& message) override; | 45 bool OnMessageReceived(const IPC::Message& message) override; |
| 43 | 46 |
| 44 void SetBluetoothAdapterForTesting( | 47 void SetBluetoothAdapterForTesting( |
| 45 scoped_refptr<device::BluetoothAdapter> mock_adapter); | 48 scoped_refptr<device::BluetoothAdapter> mock_adapter); |
| 46 | 49 |
| 47 protected: | 50 protected: |
| 48 ~BluetoothDispatcherHost() override; | 51 ~BluetoothDispatcherHost() override; |
| 49 | 52 |
| 50 private: | 53 private: |
| 51 friend class base::DeleteHelper<BluetoothDispatcherHost>; | 54 friend class base::DeleteHelper<BluetoothDispatcherHost>; |
| 52 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 55 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
| 53 | 56 |
| 54 struct RequestDeviceSession; | 57 struct RequestDeviceSession; |
| 55 | 58 |
| 56 // Set |adapter_| to a BluetoothAdapter instance and register observers, | 59 // Set |adapter_| to a BluetoothAdapter instance and register observers, |
| 57 // releasing references to previous |adapter_|. | 60 // releasing references to previous |adapter_|. |
| 58 void set_adapter(scoped_refptr<device::BluetoothAdapter> adapter); | 61 void set_adapter(scoped_refptr<device::BluetoothAdapter> adapter); |
| 59 | 62 |
| 63 // Stops all BluetoothDiscoverySessions being run for requestDevice() |
| 64 // choosers. |
| 65 void StopDeviceDiscovery(); |
| 66 |
| 67 // BluetoothAdapter::Observer implementation: |
| 68 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, |
| 69 bool powered) override; |
| 70 void DeviceAdded(device::BluetoothAdapter* adapter, |
| 71 device::BluetoothDevice* device) override; |
| 72 |
| 60 // IPC Handlers, see definitions in bluetooth_messages.h. | 73 // IPC Handlers, see definitions in bluetooth_messages.h. |
| 61 void OnRequestDevice( | 74 void OnRequestDevice( |
| 62 int thread_id, | 75 int thread_id, |
| 63 int request_id, | 76 int request_id, |
| 64 int frame_routing_id, | 77 int frame_routing_id, |
| 65 const std::vector<content::BluetoothScanFilter>& filters, | 78 const std::vector<content::BluetoothScanFilter>& filters, |
| 66 const std::vector<device::BluetoothUUID>& optional_services); | 79 const std::vector<device::BluetoothUUID>& optional_services); |
| 67 void OnConnectGATT(int thread_id, int request_id, | 80 void OnConnectGATT(int thread_id, int request_id, |
| 68 const std::string& device_instance_id); | 81 const std::string& device_instance_id); |
| 69 void OnGetPrimaryService(int thread_id, | 82 void OnGetPrimaryService(int thread_id, |
| 70 int request_id, | 83 int request_id, |
| 71 const std::string& device_instance_id, | 84 const std::string& device_instance_id, |
| 72 const std::string& service_uuid); | 85 const std::string& service_uuid); |
| 73 void OnGetCharacteristic(int thread_id, | 86 void OnGetCharacteristic(int thread_id, |
| 74 int request_id, | 87 int request_id, |
| 75 const std::string& service_instance_id, | 88 const std::string& service_instance_id, |
| 76 const std::string& characteristic_uuid); | 89 const std::string& characteristic_uuid); |
| 77 void OnReadValue(int thread_id, | 90 void OnReadValue(int thread_id, |
| 78 int request_id, | 91 int request_id, |
| 79 const std::string& characteristic_instance_id); | 92 const std::string& characteristic_instance_id); |
| 80 void OnWriteValue(int thread_id, | 93 void OnWriteValue(int thread_id, |
| 81 int request_id, | 94 int request_id, |
| 82 const std::string& characteristic_instance_id, | 95 const std::string& characteristic_instance_id, |
| 83 const std::vector<uint8_t>& value); | 96 const std::vector<uint8_t>& value); |
| 84 | 97 |
| 85 // Callbacks for BluetoothAdapter::StartDiscoverySession. | 98 // Callbacks for BluetoothAdapter::StartDiscoverySession. |
| 86 void OnDiscoverySessionStarted( | 99 void OnDiscoverySessionStarted( |
| 87 int thread_id, | 100 int chooser_id, |
| 88 int request_id, | |
| 89 scoped_ptr<device::BluetoothDiscoverySession> discovery_session); | 101 scoped_ptr<device::BluetoothDiscoverySession> discovery_session); |
| 90 void OnDiscoverySessionStartedError(int thread_id, int request_id); | 102 void OnDiscoverySessionStartedError(int chooser_id); |
| 91 | 103 |
| 92 // Stop in progress discovery session. | 104 // BluetoothChooser::Observer implementation: |
| 93 void StopDiscoverySession( | 105 void DialogCancelled(int chooser_id) override; |
| 94 int thread_id, | 106 void DeviceSelected(int chooser_id, const std::string& device_id) override; |
| 95 int request_id, | |
| 96 scoped_ptr<device::BluetoothDiscoverySession> discovery_session); | |
| 97 | 107 |
| 98 // Callbacks for BluetoothDiscoverySession::Stop. | 108 // Helper function for the above two functions: |
| 99 void OnDiscoverySessionStopped(int thread_id, int request_id); | 109 void CloseDialog(int chooser_id, const base::Closure& callback); |
| 100 void OnDiscoverySessionStoppedError(int thread_id, int request_id); | 110 |
| 111 // The chooser implementation yields to the event loop to avoid re-entering |
| 112 // code that's still using the RequestDeviceSession. These are the |
| 113 // continuation functions. |
| 114 void OnRequestDeviceCancelled(int chooser_id); |
| 115 void OnRequestDeviceSelected(int chooser_id, const std::string& device_id); |
| 101 | 116 |
| 102 // Callbacks for BluetoothDevice::CreateGattConnection. | 117 // Callbacks for BluetoothDevice::CreateGattConnection. |
| 103 void OnGATTConnectionCreated( | 118 void OnGATTConnectionCreated( |
| 104 int thread_id, | 119 int thread_id, |
| 105 int request_id, | 120 int request_id, |
| 106 const std::string& device_instance_id, | 121 const std::string& device_instance_id, |
| 107 base::TimeTicks start_time, | 122 base::TimeTicks start_time, |
| 108 scoped_ptr<device::BluetoothGattConnection> connection); | 123 scoped_ptr<device::BluetoothGattConnection> connection); |
| 109 void OnCreateGATTConnectionError( | 124 void OnCreateGATTConnectionError( |
| 110 int thread_id, | 125 int thread_id, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 135 void OnWriteValueFailed(int thread_id, | 150 void OnWriteValueFailed(int thread_id, |
| 136 int request_id, | 151 int request_id, |
| 137 device::BluetoothGattService::GattErrorCode); | 152 device::BluetoothGattService::GattErrorCode); |
| 138 | 153 |
| 139 int render_process_id_; | 154 int render_process_id_; |
| 140 | 155 |
| 141 // Maps a (thread_id,request_id) to information about its requestDevice call, | 156 // Maps a (thread_id,request_id) to information about its requestDevice call, |
| 142 // including the chooser dialog. | 157 // including the chooser dialog. |
| 143 // An entry is added to this map in OnRequestDevice, and should be removed | 158 // An entry is added to this map in OnRequestDevice, and should be removed |
| 144 // again everywhere a requestDevice() reply is sent. | 159 // again everywhere a requestDevice() reply is sent. |
| 145 std::map<std::pair<int, int>, RequestDeviceSession> request_device_sessions_; | 160 IDMap<RequestDeviceSession, IDMapOwnPointer> request_device_sessions_; |
| 146 | 161 |
| 147 // Maps to get the object's parent based on it's instanceID | 162 // Maps to get the object's parent based on it's instanceID |
| 148 // Map of service_instance_id to device_instance_id. | 163 // Map of service_instance_id to device_instance_id. |
| 149 std::map<std::string, std::string> service_to_device_; | 164 std::map<std::string, std::string> service_to_device_; |
| 150 // Map of characteristic_instance_id to service_instance_id. | 165 // Map of characteristic_instance_id to service_instance_id. |
| 151 std::map<std::string, std::string> characteristic_to_service_; | 166 std::map<std::string, std::string> characteristic_to_service_; |
| 152 | 167 |
| 153 // Defines how long to scan for and how long to discover services for. | 168 // Defines how long to scan for and how long to discover services for. |
| 154 int current_delay_time_; | 169 int current_delay_time_; |
| 155 | 170 |
| 156 // A BluetoothAdapter instance representing an adapter of the system. | 171 // A BluetoothAdapter instance representing an adapter of the system. |
| 157 scoped_refptr<device::BluetoothAdapter> adapter_; | 172 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 158 | 173 |
| 174 // Automatically stops Bluetooth discovery a set amount of time after it was |
| 175 // started. We have a single timer for all of Web Bluetooth because it's |
| 176 // simpler than tracking timeouts for each RequestDeviceSession individually, |
| 177 // and because there's no harm in extending the length of a few discovery |
| 178 // sessions when other sessions are active. |
| 179 base::Timer discovery_session_timer_; |
| 180 |
| 159 // Must be last member, see base/memory/weak_ptr.h documentation | 181 // Must be last member, see base/memory/weak_ptr.h documentation |
| 160 base::WeakPtrFactory<BluetoothDispatcherHost> weak_ptr_factory_; | 182 base::WeakPtrFactory<BluetoothDispatcherHost> weak_ptr_factory_; |
| 161 | 183 |
| 162 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost); | 184 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost); |
| 163 }; | 185 }; |
| 164 | 186 |
| 165 } // namespace content | 187 } // namespace content |
| 166 | 188 |
| 167 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ | 189 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ |
| OLD | NEW |