 Chromium Code Reviews
 Chromium Code Reviews Issue 1382743002:
  bluetooth: Add characteristicvaluechanged event  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-notifications-1
    
  
    Issue 1382743002:
  bluetooth: Add characteristicvaluechanged event  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-notifications-1| 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 // NETWORK_ERROR Note: | 5 // NETWORK_ERROR Note: | 
| 6 // When a device can't be found in the BluetoothAdapter, that generally | 6 // When a device can't be found in the BluetoothAdapter, that generally | 
| 7 // indicates that it's gone out of range. We reject with a NetworkError in that | 7 // indicates that it's gone out of range. We reject with a NetworkError in that | 
| 8 // case. | 8 // case. | 
| 9 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothdevice-conne ctgatt | 9 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothdevice-conne ctgatt | 
| 10 | 10 | 
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 bool handled = true; | 206 bool handled = true; | 
| 207 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 207 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 
| 208 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 208 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 
| 209 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) | 209 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) | 
| 210 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) | 210 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) | 
| 211 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) | 211 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) | 
| 212 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ReadValue, OnReadValue) | 212 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ReadValue, OnReadValue) | 
| 213 IPC_MESSAGE_HANDLER(BluetoothHostMsg_WriteValue, OnWriteValue) | 213 IPC_MESSAGE_HANDLER(BluetoothHostMsg_WriteValue, OnWriteValue) | 
| 214 IPC_MESSAGE_HANDLER(BluetoothHostMsg_StartNotifications, OnStartNotifications) | 214 IPC_MESSAGE_HANDLER(BluetoothHostMsg_StartNotifications, OnStartNotifications) | 
| 215 IPC_MESSAGE_HANDLER(BluetoothHostMsg_StopNotifications, OnStopNotifications) | 215 IPC_MESSAGE_HANDLER(BluetoothHostMsg_StopNotifications, OnStopNotifications) | 
| 216 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RegisterCharacteristic, | |
| 217 OnRegisterCharacteristicObject); | |
| 218 IPC_MESSAGE_HANDLER(BluetoothHostMsg_UnregisterCharacteristic, | |
| 219 OnUnregisterCharacteristicObject); | |
| 216 IPC_MESSAGE_UNHANDLED(handled = false) | 220 IPC_MESSAGE_UNHANDLED(handled = false) | 
| 217 IPC_END_MESSAGE_MAP() | 221 IPC_END_MESSAGE_MAP() | 
| 218 return handled; | 222 return handled; | 
| 219 } | 223 } | 
| 220 | 224 | 
| 221 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( | 225 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( | 
| 222 scoped_refptr<device::BluetoothAdapter> mock_adapter) { | 226 scoped_refptr<device::BluetoothAdapter> mock_adapter) { | 
| 223 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 227 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 224 current_delay_time_ = kTestingDelayTime; | 228 current_delay_time_ = kTestingDelayTime; | 
| 225 // Reset the discovery session timer to use the new delay time. | 229 // Reset the discovery session timer to use the new delay time. | 
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 if (session->chooser) { | 368 if (session->chooser) { | 
| 365 session->chooser->RemoveDevice(device->GetAddress()); | 369 session->chooser->RemoveDevice(device->GetAddress()); | 
| 366 } | 370 } | 
| 367 } | 371 } | 
| 368 } | 372 } | 
| 369 | 373 | 
| 370 void BluetoothDispatcherHost::GattCharacteristicValueChanged( | 374 void BluetoothDispatcherHost::GattCharacteristicValueChanged( | 
| 371 device::BluetoothAdapter* adapter, | 375 device::BluetoothAdapter* adapter, | 
| 372 device::BluetoothGattCharacteristic* characteristic, | 376 device::BluetoothGattCharacteristic* characteristic, | 
| 373 const std::vector<uint8>& value) { | 377 const std::vector<uint8>& value) { | 
| 374 // TODO(ortuno): Notify renderer the characteristic changed. | 378 VLOG(1) << "Characteristic updated: " << characteristic->GetIdentifier(); | 
| 375 // http://crbug.com/529560 | 379 auto iter = | 
| 376 VLOG(1) << "Characteristic updated."; | 380 active_characteristic_threads_.find(characteristic->GetIdentifier()); | 
| 381 | |
| 382 if (iter == active_characteristic_threads_.end()) { | |
| 383 return; | |
| 384 } | |
| 385 | |
| 386 for (int thread_id : iter->second) { | |
| 387 // Yield to the event loop so that the event gets dispatched after the | |
| 388 // readValue promise resolves. | |
| 389 // TODO(ortuno): Make sure the order of fulfulling promises and triggering | |
| 390 // events matches the spec and that events don't get lost. | |
| 391 // https://crbug.com/543882 | |
| 392 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 393 FROM_HERE, | |
| 394 base::Bind(&BluetoothDispatcherHost::NotifyActiveCharacteristic, | |
| 395 weak_ptr_on_ui_thread_, thread_id, | |
| 396 characteristic->GetIdentifier(), value))) { | |
| 397 LOG(WARNING) << "No TaskRunner."; | |
| 398 } | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 void BluetoothDispatcherHost::NotifyActiveCharacteristic( | |
| 403 int thread_id, | |
| 404 const std::string& characteristic_instance_id, | |
| 405 const std::vector<uint8>& value) { | |
| 406 Send(new BluetoothMsg_CharacteristicValueChanged( | |
| 407 thread_id, characteristic_instance_id, value)); | |
| 377 } | 408 } | 
| 378 | 409 | 
| 379 void BluetoothDispatcherHost::OnRequestDevice( | 410 void BluetoothDispatcherHost::OnRequestDevice( | 
| 380 int thread_id, | 411 int thread_id, | 
| 381 int request_id, | 412 int request_id, | 
| 382 int frame_routing_id, | 413 int frame_routing_id, | 
| 383 const std::vector<BluetoothScanFilter>& filters, | 414 const std::vector<BluetoothScanFilter>& filters, | 
| 384 const std::vector<BluetoothUUID>& optional_services) { | 415 const std::vector<BluetoothUUID>& optional_services) { | 
| 385 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 416 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 
| 386 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); | 417 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); | 
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 798 if (notify_session_iter == characteristic_id_to_notify_session_.end()) { | 829 if (notify_session_iter == characteristic_id_to_notify_session_.end()) { | 
| 799 Send(new BluetoothMsg_StopNotificationsSuccess(thread_id, request_id)); | 830 Send(new BluetoothMsg_StopNotificationsSuccess(thread_id, request_id)); | 
| 800 return; | 831 return; | 
| 801 } | 832 } | 
| 802 notify_session_iter->second->Stop( | 833 notify_session_iter->second->Stop( | 
| 803 base::Bind(&BluetoothDispatcherHost::OnStopNotifySession, | 834 base::Bind(&BluetoothDispatcherHost::OnStopNotifySession, | 
| 804 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 835 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 
| 805 characteristic_instance_id)); | 836 characteristic_instance_id)); | 
| 806 } | 837 } | 
| 807 | 838 | 
| 839 void BluetoothDispatcherHost::OnRegisterCharacteristicObject( | |
| 840 int thread_id, | |
| 841 const std::string& characteristic_instance_id) { | |
| 842 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 843 active_characteristic_threads_[characteristic_instance_id].insert(thread_id); | |
| 844 } | |
| 845 | |
| 846 void BluetoothDispatcherHost::OnUnregisterCharacteristicObject( | |
| 847 int thread_id, | |
| 848 const std::string& characteristic_instance_id) { | |
| 849 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 850 auto active_iter = | |
| 851 active_characteristic_threads_.find(characteristic_instance_id); | |
| 852 if (active_iter != active_characteristic_threads_.end()) { | |
| 
palmer
2015/10/19 19:23:33
Nit: Could save a level of indentation here by ret
 
ortuno
2015/10/19 20:09:18
Done.
 | |
| 853 std::set<int>& thread_ids_set = active_iter->second; | |
| 854 thread_ids_set.erase(thread_id); | |
| 855 if (thread_ids_set.empty()) { | |
| 856 active_characteristic_threads_.erase(active_iter); | |
| 857 } | |
| 858 } | |
| 859 } | |
| 860 | |
| 808 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 861 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 
| 809 int chooser_id, | 862 int chooser_id, | 
| 810 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 863 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 
| 811 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 864 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 812 VLOG(1) << "Started discovery session for " << chooser_id; | 865 VLOG(1) << "Started discovery session for " << chooser_id; | 
| 813 if (RequestDeviceSession* session = | 866 if (RequestDeviceSession* session = | 
| 814 request_device_sessions_.Lookup(chooser_id)) { | 867 request_device_sessions_.Lookup(chooser_id)) { | 
| 815 session->discovery_session = discovery_session.Pass(); | 868 session->discovery_session = discovery_session.Pass(); | 
| 816 | 869 | 
| 817 // Arrange to stop discovery later. | 870 // Arrange to stop discovery later. | 
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1084 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1137 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 1085 NOTIMPLEMENTED(); | 1138 NOTIMPLEMENTED(); | 
| 1086 } | 1139 } | 
| 1087 | 1140 | 
| 1088 void BluetoothDispatcherHost::ShowBluetoothAdapterOffLink() { | 1141 void BluetoothDispatcherHost::ShowBluetoothAdapterOffLink() { | 
| 1089 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1142 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 1090 NOTIMPLEMENTED(); | 1143 NOTIMPLEMENTED(); | 
| 1091 } | 1144 } | 
| 1092 | 1145 | 
| 1093 } // namespace content | 1146 } // namespace content | 
| OLD | NEW |