diff options
author | scheib <scheib@chromium.org> | 2016-03-25 09:25:08 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-25 16:26:37 +0000 |
commit | fc2e764e46a19ec3ef3fce0deffb261424cd69f3 (patch) | |
tree | 8a6414a47ecb00f68fa444843c702f590fc13af9 /device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc | |
parent | ddda942e328de57ff530a68391208bd3046b1cb4 (diff) | |
download | chromium_src-fc2e764e46a19ec3ef3fce0deffb261424cd69f3.zip chromium_src-fc2e764e46a19ec3ef3fce0deffb261424cd69f3.tar.gz chromium_src-fc2e764e46a19ec3ef3fce0deffb261424cd69f3.tar.bz2 |
bluetooth: Test & make StartNotifySession reentrant.
Follow up work from "bluetooth: android: Confirm the notify session after
the descriptor has been written." https://crrev.com/1712593002.
o Protects against reentrancy issue in OnStartNotifySessionSuccess
and OnStartNotifySessionError by swapping callbacks container.
o Adds unit test for StartNotifySession failure condition.
o Adds unit test for StartNotifySession after GATT objects deleted.
BUG=584369
Review URL: https://codereview.chromium.org/1779083002
Cr-Commit-Position: refs/heads/master@{#383285}
Diffstat (limited to 'device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc')
-rw-r--r-- | device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc index c4a73ff..6ad56f6 100644 --- a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc @@ -48,6 +48,10 @@ BluetoothRemoteGattCharacteristicAndroid:: ~BluetoothRemoteGattCharacteristicAndroid() { Java_ChromeBluetoothRemoteGattCharacteristic_onBluetoothRemoteGattCharacteristicAndroidDestruction( AttachCurrentThread(), j_characteristic_.obj()); + + if (pending_start_notify_calls_.size()) { + OnStartNotifySessionError(device::BluetoothGattService::GATT_ERROR_FAILED); + } } // static @@ -137,7 +141,8 @@ void BluetoothRemoteGattCharacteristicAndroid::StartNotifySession( const NotifySessionCallback& callback, const ErrorCallback& error_callback) { if (!pending_start_notify_calls_.empty()) { - pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); + pending_start_notify_calls_.push_back( + std::make_pair(callback, error_callback)); return; } @@ -149,8 +154,8 @@ void BluetoothRemoteGattCharacteristicAndroid::StartNotifySession( if (!hasNotify && !hasIndicate) { LOG(ERROR) << "Characteristic needs NOTIFY or INDICATE"; base::MessageLoop::current()->PostTask( - FROM_HERE, - base::Bind(error_callback, BluetoothGattService::GATT_ERROR_FAILED)); + FROM_HERE, base::Bind(error_callback, + BluetoothGattService::GATT_ERROR_NOT_SUPPORTED)); return; } @@ -178,11 +183,11 @@ void BluetoothRemoteGattCharacteristicAndroid::StartNotifySession( return; } - std::vector<uint8_t> value; - value.push_back(hasNotify ? 1 : 2); - value.push_back(0); + std::vector<uint8_t> value(2); + value[0] = hasNotify ? 1 : 2; - pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); + pending_start_notify_calls_.push_back( + std::make_pair(callback, error_callback)); ccc_descriptor[0]->WriteRemoteDescriptor( value, base::Bind(&BluetoothRemoteGattCharacteristicAndroid:: OnStartNotifySessionSuccess, @@ -250,21 +255,23 @@ void BluetoothRemoteGattCharacteristicAndroid::OnChanged( } void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionSuccess() { - while (!pending_start_notify_calls_.empty()) { - PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); - pending_start_notify_calls_.pop(); + std::vector<PendingStartNotifyCall> reentrant_safe_callbacks; + reentrant_safe_callbacks.swap(pending_start_notify_calls_); + + for (const auto& callback_pair : reentrant_safe_callbacks) { scoped_ptr<device::BluetoothGattNotifySession> notify_session( new BluetoothGattNotifySessionAndroid(instance_id_)); - callbacks.first.Run(std::move(notify_session)); + callback_pair.first.Run(std::move(notify_session)); } } void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError( BluetoothGattService::GattErrorCode error) { - while (!pending_start_notify_calls_.empty()) { - PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); - pending_start_notify_calls_.pop(); - callbacks.second.Run(error); + std::vector<PendingStartNotifyCall> reentrant_safe_callbacks; + reentrant_safe_callbacks.swap(pending_start_notify_calls_); + + for (auto const& callback_pair : reentrant_safe_callbacks) { + callback_pair.second.Run(error); } } |