Commit f5ab6b86 authored by beaufort.francois's avatar beaufort.francois Committed by Commit bot

bluetooth: Update BluetoothCharacteristic.value on writeValue

BUG=507415

Review URL: https://codereview.chromium.org/1611443002

Cr-Commit-Position: refs/heads/master@{#370733}
parent 4ff9d853
......@@ -67,6 +67,17 @@ struct BluetoothCharacteristicRequest {
scoped_ptr<blink::WebBluetoothGetCharacteristicCallbacks> callbacks;
};
// Struct that holds a pending WriteValue request.
struct BluetoothWriteValueRequest {
BluetoothWriteValueRequest(const blink::WebVector<uint8_t>& value,
blink::WebBluetoothWriteValueCallbacks* callbacks)
: value(value), callbacks(callbacks) {}
~BluetoothWriteValueRequest() {}
const blink::WebVector<uint8_t> value;
scoped_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks;
};
// Struct that holds a pending Start/StopNotifications request.
struct BluetoothNotificationsRequest {
BluetoothNotificationsRequest(
......@@ -273,8 +284,8 @@ void BluetoothDispatcher::writeValue(
const blink::WebString& characteristic_instance_id,
const blink::WebVector<uint8_t>& value,
blink::WebBluetoothWriteValueCallbacks* callbacks) {
int request_id = pending_write_value_requests_.Add(callbacks);
int request_id = pending_write_value_requests_.Add(
new BluetoothWriteValueRequest(value, callbacks));
Send(new BluetoothHostMsg_WriteValue(
CurrentWorkerId(), request_id, frame_routing_id,
characteristic_instance_id.utf8(),
......@@ -695,7 +706,9 @@ void BluetoothDispatcher::OnReadValueError(int thread_id,
void BluetoothDispatcher::OnWriteValueSuccess(int thread_id, int request_id) {
DCHECK(pending_write_value_requests_.Lookup(request_id)) << request_id;
pending_write_value_requests_.Lookup(request_id)->onSuccess();
BluetoothWriteValueRequest* request =
pending_write_value_requests_.Lookup(request_id);
request->callbacks->onSuccess(request->value);
pending_write_value_requests_.Remove(request_id);
}
......@@ -705,8 +718,9 @@ void BluetoothDispatcher::OnWriteValueError(int thread_id,
WebBluetoothError error) {
DCHECK(pending_write_value_requests_.Lookup(request_id)) << request_id;
pending_write_value_requests_.Lookup(request_id)
->onError(WebBluetoothError(error));
BluetoothWriteValueRequest* request =
pending_write_value_requests_.Lookup(request_id);
request->callbacks->onError(WebBluetoothError(error));
pending_write_value_requests_.Remove(request_id);
}
......
......@@ -33,6 +33,7 @@ class Message;
struct BluetoothCharacteristicRequest;
struct BluetoothPrimaryServiceRequest;
struct BluetoothWriteValueRequest;
struct BluetoothNotificationsRequest;
namespace content {
......@@ -241,7 +242,7 @@ class BluetoothDispatcher : public WorkerThread::Observer {
// Tracks requests to read from a characteristics.
IDMap<blink::WebBluetoothReadValueCallbacks, IDMapOwnPointer>
pending_read_value_requests_;
IDMap<blink::WebBluetoothWriteValueCallbacks, IDMapOwnPointer>
IDMap<BluetoothWriteValueRequest, IDMapOwnPointer>
pending_write_value_requests_;
IDMap<BluetoothNotificationsRequest, IDMapOwnPointer>
pending_notifications_requests_;
......
......@@ -98,4 +98,20 @@ promise_test(() => {
characteristic.writeValue(new ArrayBuffer(1 /* length */)),
characteristic.writeValue(new DataView(new ArrayBuffer(1 /* length */)))]));
}, 'A regular write request to a writable characteristic should succeed.');
promise_test(() => {
testRunner.setBluetoothMockDataSet('HeartRateAdapter');
return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}]})
.then(device => device.connectGATT())
.then(gattServer => gattServer.getPrimaryService('generic_access'))
.then(service => service.getCharacteristic('gap.device_name'))
.then(characteristic => {
assert_equals(characteristic.value, null);
let textEncoder = new TextEncoder();
let newValue = textEncoder.encode('foo');
return characteristic.writeValue(newValue).then(() => {
assert_array_equals(characteristic.value.buffer, newValue.buffer);
});
});
}, 'A regular write request to a writable characteristic should update value.');
</script>
......@@ -145,6 +145,33 @@ ScriptPromise BluetoothGATTCharacteristic::readValue(ScriptState* scriptState)
return promise;
}
class WriteValueCallback : public WebBluetoothWriteValueCallbacks {
public:
WriteValueCallback(BluetoothGATTCharacteristic* characteristic, ScriptPromiseResolver* resolver) : m_webCharacteristic(characteristic), m_resolver(resolver) {}
void onSuccess(const WebVector<uint8_t>& value) override
{
if (!m_resolver->executionContext() || m_resolver->executionContext()->activeDOMObjectsAreStopped())
return;
if (m_webCharacteristic) {
m_webCharacteristic->setValue(ConvertWebVectorToDataView(value));
}
m_resolver->resolve();
}
void onError(const WebBluetoothError& e) override
{
if (!m_resolver->executionContext() || m_resolver->executionContext()->activeDOMObjectsAreStopped())
return;
m_resolver->reject(BluetoothError::take(m_resolver, e));
}
private:
WeakPersistent<BluetoothGATTCharacteristic> m_webCharacteristic;
Persistent<ScriptPromiseResolver> m_resolver;
};
ScriptPromise BluetoothGATTCharacteristic::writeValue(ScriptState* scriptState, const DOMArrayPiece& value)
{
WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptState);
......@@ -163,7 +190,7 @@ ScriptPromise BluetoothGATTCharacteristic::writeValue(ScriptState* scriptState,
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, valueVector, new CallbackPromiseAdapter<void, BluetoothError>(resolver));
webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, valueVector, new WriteValueCallback(this, resolver));
return promise;
}
......
......@@ -37,7 +37,7 @@ using WebBluetoothGetCharacteristicCallbacks = WebCallbacks<WebPassOwnPtr<WebBlu
using WebBluetoothReadValueCallbacks = WebCallbacks<const WebVector<uint8_t>&, const WebBluetoothError&>;
// Success and failure callbacks for writeValue.
using WebBluetoothWriteValueCallbacks = WebCallbacks<void, const WebBluetoothError&>;
using WebBluetoothWriteValueCallbacks = WebCallbacks<const WebVector<uint8_t>&, const WebBluetoothError&>;
// Success and failure callbacks for characteristic.startNotifications and
// characteristic.stopNotifications.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment