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 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" | 5 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "content/common/bluetooth/bluetooth_messages.h" | 8 #include "content/common/bluetooth/bluetooth_messages.h" |
9 #include "device/bluetooth/bluetooth_adapter.h" | 9 #include "device/bluetooth/bluetooth_adapter.h" |
10 #include "device/bluetooth/bluetooth_adapter_factory.h" | 10 #include "device/bluetooth/bluetooth_adapter_factory.h" |
11 #include "device/bluetooth/bluetooth_device.h" | 11 #include "device/bluetooth/bluetooth_device.h" |
12 #include "device/bluetooth/bluetooth_discovery_session.h" | 12 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 13 #include "device/bluetooth/bluetooth_gatt_service.h" |
13 | 14 |
14 using device::BluetoothAdapter; | 15 using device::BluetoothAdapter; |
15 using device::BluetoothAdapterFactory; | 16 using device::BluetoothAdapterFactory; |
| 17 using device::BluetoothGattService; |
16 | 18 |
17 namespace content { | 19 namespace content { |
18 | 20 |
19 const int kScanTime = 5; // 5 seconds of scan time | 21 // TODO(ortuno): Once we have a chooser for scanning and the right |
20 const int kTestingScanTime = 0; // No need to scan for tests | 22 // callback for discovered services we should delete these constants. |
| 23 // https://crbug.com/436280 and https://crbug.com/484504 |
| 24 const int kDelayTime = 5; // 5 seconds for scanning and discovering |
| 25 const int kTestingDelayTime = 0; // No need to wait during tests |
21 | 26 |
22 BluetoothDispatcherHost::BluetoothDispatcherHost() | 27 BluetoothDispatcherHost::BluetoothDispatcherHost() |
23 : BrowserMessageFilter(BluetoothMsgStart), | 28 : BrowserMessageFilter(BluetoothMsgStart), |
24 weak_ptr_factory_(this) { | 29 weak_ptr_factory_(this) { |
25 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 30 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
26 current_scan_time_ = kScanTime; | 31 current_delay_time_ = kDelayTime; |
27 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | 32 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) |
28 BluetoothAdapterFactory::GetAdapter( | 33 BluetoothAdapterFactory::GetAdapter( |
29 base::Bind(&BluetoothDispatcherHost::set_adapter, | 34 base::Bind(&BluetoothDispatcherHost::set_adapter, |
30 weak_ptr_factory_.GetWeakPtr())); | 35 weak_ptr_factory_.GetWeakPtr())); |
31 } | 36 } |
32 | 37 |
33 void BluetoothDispatcherHost::OnDestruct() const { | 38 void BluetoothDispatcherHost::OnDestruct() const { |
34 // See class comment: UI Thread Note. | 39 // See class comment: UI Thread Note. |
35 BrowserThread::DeleteOnUIThread::Destruct(this); | 40 BrowserThread::DeleteOnUIThread::Destruct(this); |
36 } | 41 } |
37 | 42 |
38 void BluetoothDispatcherHost::OverrideThreadForMessage( | 43 void BluetoothDispatcherHost::OverrideThreadForMessage( |
39 const IPC::Message& message, | 44 const IPC::Message& message, |
40 content::BrowserThread::ID* thread) { | 45 content::BrowserThread::ID* thread) { |
41 // See class comment: UI Thread Note. | 46 // See class comment: UI Thread Note. |
42 *thread = BrowserThread::UI; | 47 *thread = BrowserThread::UI; |
43 } | 48 } |
44 | 49 |
45 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 50 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
46 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 51 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
47 bool handled = true; | 52 bool handled = true; |
48 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 53 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) |
49 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 54 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) |
50 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) | 55 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) |
| 56 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) |
51 IPC_MESSAGE_UNHANDLED(handled = false) | 57 IPC_MESSAGE_UNHANDLED(handled = false) |
52 IPC_END_MESSAGE_MAP() | 58 IPC_END_MESSAGE_MAP() |
53 return handled; | 59 return handled; |
54 } | 60 } |
55 | 61 |
56 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( | 62 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( |
57 scoped_refptr<device::BluetoothAdapter> mock_adapter) { | 63 scoped_refptr<device::BluetoothAdapter> mock_adapter) { |
58 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
59 current_scan_time_ = kTestingScanTime; | 65 current_delay_time_ = kTestingDelayTime; |
60 set_adapter(mock_adapter.Pass()); | 66 set_adapter(mock_adapter.Pass()); |
61 } | 67 } |
62 | 68 |
63 BluetoothDispatcherHost::~BluetoothDispatcherHost() { | 69 BluetoothDispatcherHost::~BluetoothDispatcherHost() { |
64 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
65 // Clear adapter, releasing observer references. | 71 // Clear adapter, releasing observer references. |
66 set_adapter(scoped_refptr<device::BluetoothAdapter>()); | 72 set_adapter(scoped_refptr<device::BluetoothAdapter>()); |
67 } | 73 } |
68 | 74 |
69 void BluetoothDispatcherHost::set_adapter( | 75 void BluetoothDispatcherHost::set_adapter( |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 } | 122 } |
117 device->CreateGattConnection( | 123 device->CreateGattConnection( |
118 base::Bind(&BluetoothDispatcherHost::OnGATTConnectionCreated, | 124 base::Bind(&BluetoothDispatcherHost::OnGATTConnectionCreated, |
119 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 125 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
120 device_instance_id), | 126 device_instance_id), |
121 base::Bind(&BluetoothDispatcherHost::OnCreateGATTConnectionError, | 127 base::Bind(&BluetoothDispatcherHost::OnCreateGATTConnectionError, |
122 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 128 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
123 device_instance_id)); | 129 device_instance_id)); |
124 } | 130 } |
125 | 131 |
| 132 void BluetoothDispatcherHost::OnGetPrimaryService( |
| 133 int thread_id, |
| 134 int request_id, |
| 135 const std::string& device_instance_id, |
| 136 const std::string& service_uuid) { |
| 137 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 138 // TODO(ortuno): Right now it's pointless to check if the domain has access to |
| 139 // the device, because any domain can connect to any device. But once |
| 140 // permissions are implemented we should check that the domain has access to |
| 141 // the device. https://crbug.com/484745 |
| 142 // For now just wait two seconds and call OnServiceDiscovered. |
| 143 // TODO(ortuno): Use callback once it's implemented http://crbug.com/484504 |
| 144 BrowserThread::PostDelayedTask( |
| 145 BrowserThread::UI, FROM_HERE, |
| 146 base::Bind(&BluetoothDispatcherHost::OnServicesDiscovered, |
| 147 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
| 148 device_instance_id, service_uuid), |
| 149 base::TimeDelta::FromSeconds(current_delay_time_)); |
| 150 } |
| 151 |
126 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 152 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
127 int thread_id, | 153 int thread_id, |
128 int request_id, | 154 int request_id, |
129 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 155 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
130 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 156 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
131 BrowserThread::PostDelayedTask( | 157 BrowserThread::PostDelayedTask( |
132 BrowserThread::UI, FROM_HERE, | 158 BrowserThread::UI, FROM_HERE, |
133 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 159 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
134 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 160 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
135 base::Passed(&discovery_session)), | 161 base::Passed(&discovery_session)), |
136 base::TimeDelta::FromSeconds(current_scan_time_)); | 162 base::TimeDelta::FromSeconds(current_delay_time_)); |
137 } | 163 } |
138 | 164 |
139 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, | 165 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, |
140 int request_id) { | 166 int request_id) { |
141 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 167 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
142 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; | 168 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; |
143 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 169 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
144 BluetoothError::NOT_FOUND)); | 170 BluetoothError::NOT_FOUND)); |
145 } | 171 } |
146 | 172 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 int request_id, | 231 int request_id, |
206 const std::string& device_instance_id, | 232 const std::string& device_instance_id, |
207 device::BluetoothDevice::ConnectErrorCode error_code) { | 233 device::BluetoothDevice::ConnectErrorCode error_code) { |
208 // There was an error creating the ATT Bearer so we reject with | 234 // There was an error creating the ATT Bearer so we reject with |
209 // NetworkError. | 235 // NetworkError. |
210 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-connect
gatt | 236 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-connect
gatt |
211 Send(new BluetoothMsg_ConnectGATTError(thread_id, request_id, | 237 Send(new BluetoothMsg_ConnectGATTError(thread_id, request_id, |
212 BluetoothError::NETWORK_ERROR)); | 238 BluetoothError::NETWORK_ERROR)); |
213 } | 239 } |
214 | 240 |
| 241 void BluetoothDispatcherHost::OnServicesDiscovered( |
| 242 int thread_id, |
| 243 int request_id, |
| 244 const std::string& device_instance_id, |
| 245 const std::string& service_uuid) { |
| 246 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 247 |
| 248 device::BluetoothDevice* device = adapter_->GetDevice(device_instance_id); |
| 249 if (device == NULL) { |
| 250 Send(new BluetoothMsg_GetPrimaryServiceError( |
| 251 thread_id, request_id, BluetoothError::NETWORK_ERROR)); |
| 252 return; |
| 253 } |
| 254 for (BluetoothGattService* service : device->GetGattServices()) { |
| 255 if (service->GetUUID().canonical_value() == service_uuid) { |
| 256 Send(new BluetoothMsg_GetPrimaryServiceSuccess( |
| 257 thread_id, request_id, device_instance_id, service->GetIdentifier(), |
| 258 service->GetUUID().canonical_value())); |
| 259 return; |
| 260 } |
| 261 } |
| 262 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, |
| 263 BluetoothError::NOT_FOUND)); |
| 264 } |
| 265 |
215 } // namespace content | 266 } // namespace content |
OLD | NEW |