Index: content/browser/bluetooth/bluetooth_dispatcher_host.cc |
diff --git a/content/browser/bluetooth/bluetooth_dispatcher_host.cc b/content/browser/bluetooth/bluetooth_dispatcher_host.cc |
index 75556c0b319d3f4b7bc0656813429e7fd04545a1..008eba24f246737faa3815d2b165730079e10f3d 100644 |
--- a/content/browser/bluetooth/bluetooth_dispatcher_host.cc |
+++ b/content/browser/bluetooth/bluetooth_dispatcher_host.cc |
@@ -199,6 +199,8 @@ bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) |
IPC_MESSAGE_HANDLER(BluetoothHostMsg_ReadValue, OnReadValue) |
IPC_MESSAGE_HANDLER(BluetoothHostMsg_WriteValue, OnWriteValue) |
+ IPC_MESSAGE_HANDLER(BluetoothHostMsg_StartNotifications, OnStartNotifications) |
+ IPC_MESSAGE_HANDLER(BluetoothHostMsg_StopNotifications, OnStopNotifications) |
IPC_MESSAGE_UNHANDLED(handled = false) |
IPC_END_MESSAGE_MAP() |
return handled; |
@@ -347,6 +349,15 @@ void BluetoothDispatcherHost::DeviceRemoved(device::BluetoothAdapter* adapter, |
} |
} |
+void BluetoothDispatcherHost::GattCharacteristicValueChanged( |
+ device::BluetoothAdapter* adapter, |
+ device::BluetoothGattCharacteristic* characteristic, |
+ const std::vector<uint8>& value) { |
+ // TODO(ortuno): Notify renderer the characteristic changed. |
+ // http://crbug.com/529560 |
+ VLOG(1) << "Characteristic updated."; |
+} |
+ |
void BluetoothDispatcherHost::OnRequestDevice( |
int thread_id, |
int request_id, |
@@ -694,6 +705,92 @@ void BluetoothDispatcherHost::OnWriteValue( |
weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
} |
+void BluetoothDispatcherHost::OnStartNotifications( |
+ int thread_id, |
+ int request_id, |
+ const std::string& characteristic_instance_id) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ RecordWebBluetoothFunctionCall( |
+ UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); |
+ |
+ // Check if already subscribed to notifications. |
+ auto notify_session_iter = |
+ characteristic_id_to_notify_session_.find(characteristic_instance_id); |
+ if (notify_session_iter != characteristic_id_to_notify_session_.end()) { |
+ Send(new BluetoothMsg_StartNotificationsSuccess(thread_id, request_id)); |
Jeffrey Yasskin
2015/09/25 23:03:19
This is vulnerable to races if a second BluetoothH
ortuno
2015/09/29 22:47:36
This code was wrote before I implemented the queue
|
+ return; |
+ } |
+ |
+ // TODO(ortuno): Check if notify/indicate bit is set. |
Jeffrey Yasskin
2015/09/25 23:03:19
Add a blank line below this, since it's not connec
ortuno
2015/09/29 22:47:36
Done.
|
+ auto characteristic_iter = |
+ characteristic_to_service_.find(characteristic_instance_id); |
+ // A characteristic_instance_id not in the map implies a hostile renderer |
+ // because a renderer obtains the characteristic id from this class and |
+ // it will be added to the map at that time. |
+ if (characteristic_iter == characteristic_to_service_.end()) { |
+ // Kill the renderer |
+ bad_message::ReceivedBadMessage(this, |
+ bad_message::BDH_INVALID_CHARACTERISTIC_ID); |
+ return; |
+ } |
+ |
+ const std::string& service_instance_id = characteristic_iter->second; |
+ auto device_iter = service_to_device_.find(service_instance_id); |
+ CHECK(device_iter != service_to_device_.end()); |
+ |
+ device::BluetoothDevice* device = |
+ adapter_->GetDevice(device_iter->second /* device_instance_id */); |
+ if (device == nullptr) { // See "NETWORK_ERROR Note" above. |
+ RecordStartNotificationsOutcome(UMAGATTOperationOutcome::NO_DEVICE); |
+ Send(new BluetoothMsg_StartNotificationsError( |
+ thread_id, request_id, WebBluetoothError::DeviceNoLongerInRange)); |
+ return; |
+ } |
+ |
+ BluetoothGattService* service = device->GetGattService(service_instance_id); |
+ if (service == nullptr) { |
+ RecordStartNotificationsOutcome(UMAGATTOperationOutcome::NO_SERVICE); |
+ Send(new BluetoothMsg_StartNotificationsError( |
+ thread_id, request_id, WebBluetoothError::ServiceNoLongerExists)); |
+ return; |
+ } |
+ |
+ BluetoothGattCharacteristic* characteristic = |
+ service->GetCharacteristic(characteristic_instance_id); |
+ if (characteristic == nullptr) { |
+ RecordStartNotificationsOutcome(UMAGATTOperationOutcome::NO_CHARACTERISTIC); |
+ Send(new BluetoothMsg_StartNotificationsError( |
+ thread_id, request_id, |
+ WebBluetoothError::CharacteristicNoLongerExists)); |
+ return; |
+ } |
+ |
+ characteristic->StartNotifySession( |
+ base::Bind(&BluetoothDispatcherHost::OnStartNotifySessionSuccess, |
+ weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
+ base::Bind(&BluetoothDispatcherHost::OnStartNotifySessionFailed, |
+ weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
+} |
+void BluetoothDispatcherHost::OnStopNotifications( |
+ int thread_id, |
+ int request_id, |
+ const std::string& characteristic_instance_id) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ RecordWebBluetoothFunctionCall( |
+ UMAWebBluetoothFunction::CHARACTERISTIC_STOP_NOTIFICATIONS); |
+ |
+ auto notify_session_iter = |
+ characteristic_id_to_notify_session_.find(characteristic_instance_id); |
+ if (notify_session_iter == characteristic_id_to_notify_session_.end()) { |
+ Send(new BluetoothMsg_StopNotificationsSuccess(thread_id, request_id)); |
+ return; |
+ } |
+ notify_session_iter->second->Stop( |
+ base::Bind(&BluetoothDispatcherHost::OnStopNotifySession, |
+ weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
+ characteristic_instance_id)); |
+} |
+ |
void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
int chooser_id, |
scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
@@ -923,6 +1020,33 @@ void BluetoothDispatcherHost::OnWriteValueFailed( |
TranslateGATTError(error_code, UMAGATTOperation::CHARACTERISTIC_WRITE))); |
} |
+void BluetoothDispatcherHost::OnStartNotifySessionSuccess( |
+ int thread_id, |
+ int request_id, |
+ scoped_ptr<device::BluetoothGattNotifySession> notify_session) { |
+ characteristic_id_to_notify_session_.insert( |
+ notify_session->GetCharacteristicIdentifier(), notify_session.Pass()); |
+ Send(new BluetoothMsg_StartNotificationsSuccess(thread_id, request_id)); |
Jeffrey Yasskin
2015/09/25 23:03:19
Call RecordStartNotificationsOutcome() here, right
ortuno
2015/09/29 22:47:36
Done.
|
+} |
+ |
+void BluetoothDispatcherHost::OnStartNotifySessionFailed( |
+ int thread_id, |
+ int request_id, |
+ device::BluetoothGattService::GattErrorCode error_code) { |
+ // TranslateGATTError calls RecordGATTOperationOutcome. |
+ Send(new BluetoothMsg_StartNotificationsError( |
+ thread_id, request_id, |
+ TranslateGATTError(error_code, UMAGATTOperation::START_NOTIFICATIONS))); |
+} |
+ |
+void BluetoothDispatcherHost::OnStopNotifySession( |
+ int thread_id, |
+ int request_id, |
+ const std::string& characteristic_instance_id) { |
+ characteristic_id_to_notify_session_.erase(characteristic_instance_id); |
+ Send(new BluetoothMsg_StopNotificationsSuccess(thread_id, request_id)); |
+} |
+ |
void BluetoothDispatcherHost::ShowBluetoothOverviewLink() { |
NOTIMPLEMENTED(); |
} |