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

Side by Side Diff: content/browser/bluetooth/bluetooth_dispatcher_host.cc

Issue 1149883011: bluetooth: Browser-side implementation of readValue (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-get-characteristic-initial
Patch Set: Add unreadable characteristic. Created 5 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 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/browser/bad_message.h" 8 #include "content/browser/bad_message.h"
9 #include "content/common/bluetooth/bluetooth_messages.h" 9 #include "content/common/bluetooth/bluetooth_messages.h"
10 #include "device/bluetooth/bluetooth_adapter.h" 10 #include "device/bluetooth/bluetooth_adapter.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 } 51 }
52 52
53 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { 53 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) {
54 DCHECK_CURRENTLY_ON(BrowserThread::UI); 54 DCHECK_CURRENTLY_ON(BrowserThread::UI);
55 bool handled = true; 55 bool handled = true;
56 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) 56 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message)
57 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) 57 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice)
58 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) 58 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT)
59 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) 59 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService)
60 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) 60 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic)
61 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ReadValue, OnReadValue)
61 IPC_MESSAGE_UNHANDLED(handled = false) 62 IPC_MESSAGE_UNHANDLED(handled = false)
62 IPC_END_MESSAGE_MAP() 63 IPC_END_MESSAGE_MAP()
63 return handled; 64 return handled;
64 } 65 }
65 66
66 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( 67 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting(
67 scoped_refptr<device::BluetoothAdapter> mock_adapter) { 68 scoped_refptr<device::BluetoothAdapter> mock_adapter) {
68 DCHECK_CURRENTLY_ON(BrowserThread::UI); 69 DCHECK_CURRENTLY_ON(BrowserThread::UI);
69 current_delay_time_ = kTestingDelayTime; 70 current_delay_time_ = kTestingDelayTime;
70 set_adapter(mock_adapter.Pass()); 71 set_adapter(mock_adapter.Pass());
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 device->GetGattService(service_instance_id); 188 device->GetGattService(service_instance_id);
188 if (!service) { 189 if (!service) {
189 Send(new BluetoothMsg_GetCharacteristicError( 190 Send(new BluetoothMsg_GetCharacteristicError(
190 thread_id, request_id, BluetoothError::NETWORK_ERROR)); 191 thread_id, request_id, BluetoothError::NETWORK_ERROR));
191 return; 192 return;
192 } 193 }
193 194
194 for (BluetoothGattCharacteristic* characteristic : 195 for (BluetoothGattCharacteristic* characteristic :
195 service->GetCharacteristics()) { 196 service->GetCharacteristics()) {
196 if (characteristic->GetUUID().canonical_value() == characteristic_uuid) { 197 if (characteristic->GetUUID().canonical_value() == characteristic_uuid) {
198 const std::string& characteristic_instance_id =
199 characteristic->GetIdentifier();
200
201 auto insert_result = characteristic_to_service_.insert(
202 make_pair(characteristic_instance_id, service_instance_id));
203
204 // If the characteristic existed already check that service_instance_id is
205 // the same.
206 if (!insert_result.second)
scheib 2015/06/11 20:20:50 Reduce comment size, if (!insert_result.second) /
ortuno 2015/06/11 21:17:09 Done.
scheib 2015/06/11 21:51:57 You can do it on one line, """ if (!insert_result
ortuno 2015/06/11 22:04:38 That's longer that character limit.
207 DCHECK(insert_result.first->second == service_instance_id);
208
197 // TODO(ortuno): Use generated instance ID instead. 209 // TODO(ortuno): Use generated instance ID instead.
198 // https://crbug.com/495379 210 // https://crbug.com/495379
199 Send(new BluetoothMsg_GetCharacteristicSuccess( 211 Send(new BluetoothMsg_GetCharacteristicSuccess(
200 thread_id, request_id, characteristic->GetIdentifier())); 212 thread_id, request_id, characteristic_instance_id));
201 return; 213 return;
202 } 214 }
203 } 215 }
204 Send(new BluetoothMsg_GetCharacteristicError(thread_id, request_id, 216 Send(new BluetoothMsg_GetCharacteristicError(thread_id, request_id,
205 BluetoothError::NOT_FOUND)); 217 BluetoothError::NOT_FOUND));
206 } 218 }
207 219
220 void BluetoothDispatcherHost::OnReadValue(
221 int thread_id,
222 int request_id,
223 const std::string& characteristic_instance_id) {
224 DCHECK_CURRENTLY_ON(BrowserThread::UI);
225
226 auto characteristic_iter =
227 characteristic_to_service_.find(characteristic_instance_id);
228 // A characteristic_instance_id not in the map implies a hostile renderer
229 // because a renderer obtains the characteristic id from this class and
230 // it will be added to the map at that time.
231 if (characteristic_iter == characteristic_to_service_.end()) {
232 // Kill the renderer
233 bad_message::ReceivedBadMessage(this,
234 bad_message::BDH_INVALID_CHARACTERISTIC_ID);
235 return;
236 }
237 const std::string& service_instance_id = characteristic_iter->second;
238
239 auto device_iter = service_to_device_.find(service_instance_id);
240 // A service_instance_id not in the map implies a hostile renderer
scheib 2015/06/11 20:20:50 Isn't the first map check sufficient for bad rende
ortuno 2015/06/11 21:17:09 Done.
241 // because a renderer obtains the service id from this class and
242 // it will be added to the map at that time.
243 if (device_iter == service_to_device_.end()) {
244 // Kill the renderer
245 bad_message::ReceivedBadMessage(this, bad_message::BDH_INVALID_SERVICE_ID);
246 return;
247 }
248
249 device::BluetoothDevice* device =
250 adapter_->GetDevice(device_iter->second /* device_instance_id */);
251 if (device == nullptr) {
252 Send(new BluetoothMsg_ReadCharacteristicValueError(
253 thread_id, request_id, BluetoothError::NETWORK_ERROR));
254 return;
255 }
256
257 BluetoothGattService* service = device->GetGattService(service_instance_id);
258 if (service == nullptr) {
259 // TODO(ortuno): Change to InvalidStateError once implemented:
260 // http://crbug.com/499014
261 Send(new BluetoothMsg_ReadCharacteristicValueError(
262 thread_id, request_id, BluetoothError::NETWORK_ERROR));
263 return;
264 }
265
266 BluetoothGattCharacteristic* characteristic =
267 service->GetCharacteristic(characteristic_instance_id);
268 if (characteristic == nullptr) {
269 Send(new BluetoothMsg_ReadCharacteristicValueError(
270 thread_id, request_id, BluetoothError::NETWORK_ERROR));
271 return;
272 }
273
274 characteristic->ReadRemoteCharacteristic(
275 base::Bind(&BluetoothDispatcherHost::OnCharacteristicValueRead,
276 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id),
277 base::Bind(&BluetoothDispatcherHost::OnCharacteristicReadValueError,
278 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id));
279 }
280
208 void BluetoothDispatcherHost::OnDiscoverySessionStarted( 281 void BluetoothDispatcherHost::OnDiscoverySessionStarted(
209 int thread_id, 282 int thread_id,
210 int request_id, 283 int request_id,
211 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { 284 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) {
212 DCHECK_CURRENTLY_ON(BrowserThread::UI); 285 DCHECK_CURRENTLY_ON(BrowserThread::UI);
213 BrowserThread::PostDelayedTask( 286 BrowserThread::PostDelayedTask(
214 BrowserThread::UI, FROM_HERE, 287 BrowserThread::UI, FROM_HERE,
215 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, 288 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession,
216 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, 289 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id,
217 base::Passed(&discovery_session)), 290 base::Passed(&discovery_session)),
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 395
323 Send(new BluetoothMsg_GetPrimaryServiceSuccess(thread_id, request_id, 396 Send(new BluetoothMsg_GetPrimaryServiceSuccess(thread_id, request_id,
324 service_identifier)); 397 service_identifier));
325 return; 398 return;
326 } 399 }
327 } 400 }
328 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, 401 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id,
329 BluetoothError::NOT_FOUND)); 402 BluetoothError::NOT_FOUND));
330 } 403 }
331 404
405 void BluetoothDispatcherHost::OnCharacteristicValueRead(
406 int thread_id,
407 int request_id,
408 const std::vector<uint8>& value) {
409 Send(new BluetoothMsg_ReadCharacteristicValueSuccess(thread_id, request_id,
410 value));
411 }
412
413 void BluetoothDispatcherHost::OnCharacteristicReadValueError(
414 int thread_id,
415 int request_id,
416 device::BluetoothGattService::GattErrorCode) {
417 // TODO(ortuno): Send a different Error based on the GattErrorCode.
scheib 2015/06/11 20:20:49 Issue number for this.
ortuno 2015/06/11 21:17:09 Done.
418 Send(new BluetoothMsg_ReadCharacteristicValueError(
419 thread_id, request_id, BluetoothError::NETWORK_ERROR));
420 }
421
332 } // namespace content 422 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698