Chromium Code Reviews| Index: third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTService.cpp |
| diff --git a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTService.cpp b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTService.cpp |
| index 84d915fd495db3acdbbf855b6811c19d87763f45..fc44ffc4a56393e6e0121b8e8232a9b2980cf32c 100644 |
| --- a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTService.cpp |
| +++ b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTService.cpp |
| @@ -9,11 +9,11 @@ |
| #include "core/dom/DOMException.h" |
| #include "core/dom/ExceptionCode.h" |
| #include "core/inspector/ConsoleMessage.h" |
| +#include "modules/bluetooth/Bluetooth.h" |
| #include "modules/bluetooth/BluetoothError.h" |
| #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" |
| -#include "modules/bluetooth/BluetoothSupplement.h" |
| #include "modules/bluetooth/BluetoothUUID.h" |
| -#include "public/platform/modules/bluetooth/WebBluetooth.h" |
| +#include "modules/bluetooth/BluetoothUtils.h" |
| #include "wtf/PtrUtil.h" |
| #include <memory> |
| #include <utility> |
| @@ -33,93 +33,77 @@ const char kInvalidService[] = |
| } // namespace |
| BluetoothRemoteGATTService::BluetoothRemoteGATTService( |
| - std::unique_ptr<WebBluetoothRemoteGATTService> webService, |
| + const String& serviceInstanceId, |
| + const String& uuid, |
| + bool isPrimary, |
| + const String& deviceInstanceId, |
| BluetoothDevice* device) |
| - : m_webService(std::move(webService)), m_device(device) { |
| - DCHECK(m_webService); |
| -} |
| + : m_serviceInstanceId(serviceInstanceId), |
| + m_uuid(uuid), |
| + m_isPrimary(isPrimary), |
| + m_deviceInstanceId(deviceInstanceId), |
| + m_device(device) {} |
| DEFINE_TRACE(BluetoothRemoteGATTService) { |
| visitor->trace(m_device); |
| } |
| -// Class that allows us to resolve the promise with a single Characteristic or |
| -// with a vector owning the characteristics. |
| -class GetCharacteristicsCallback |
| - : public WebBluetoothGetCharacteristicsCallbacks { |
| - public: |
| - GetCharacteristicsCallback( |
| - BluetoothRemoteGATTService* service, |
| - mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| - ScriptPromiseResolver* resolver) |
| - : m_service(service), m_quantity(quantity), m_resolver(resolver) { |
| - // We always check that the device is connected before constructing this |
| - // object. |
| - CHECK(m_service->device()->gatt()->connected()); |
| - m_service->device()->gatt()->AddToActiveAlgorithms(m_resolver.get()); |
| - } |
| - |
| - void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& |
| - webCharacteristics) override { |
| - if (!m_resolver->getExecutionContext() || |
| - m_resolver->getExecutionContext()->isContextDestroyed()) |
| - return; |
| - |
| +// Callback that allows us to resolve the promise with a single Characteristic |
| +// or with a vector owning the characteristics. |
| +void BluetoothRemoteGATTService::GetCharacteristicsCallback( |
| + const String& serviceInstanceId, |
| + mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| + ScriptPromiseResolver* resolver, |
| + mojom::blink::WebBluetoothResult result, |
| + Optional<Vector<mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr>> |
| + characteristics) { |
| + if (!resolver->getExecutionContext() || |
| + resolver->getExecutionContext()->isContextDestroyed()) |
| + return; |
|
Reilly Grant (use Gerrit)
2016/12/16 01:41:29
This check is unnecessary. It is performed by
Scri
juncai
2016/12/17 01:18:52
Keep this check if the callbacks do more than Scri
|
| + |
| + if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| // If the resolver is not in the set of ActiveAlgorithms then the frame |
| // disconnected so we reject. |
| - if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( |
| - m_resolver.get())) { |
| - m_resolver->reject( |
| + if (!device()->gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| + resolver->reject( |
| DOMException::create(NetworkError, kGATTServerDisconnected)); |
| return; |
| } |
| - if (m_quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
| - DCHECK_EQ(1u, webCharacteristics.size()); |
| - m_resolver->resolve( |
| - m_service->device()->getOrCreateBluetoothRemoteGATTCharacteristic( |
| - m_resolver->getExecutionContext(), |
| - WTF::wrapUnique(webCharacteristics[0]), m_service)); |
| + DCHECK(characteristics); |
|
dougt
2016/12/16 00:11:45
I don't understand this check. characteristics is
juncai
2016/12/17 01:18:52
This DCHECK() is done inside the above if statemen
|
| + |
| + if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
| + DCHECK_EQ(1u, characteristics->size()); |
| + resolver->resolve(device()->getOrCreateBluetoothRemoteGATTCharacteristic( |
| + resolver->getExecutionContext(), |
| + characteristics.value()[0]->instance_id, serviceInstanceId, |
| + characteristics.value()[0]->uuid, |
| + characteristics.value()[0]->properties, this)); |
| return; |
| } |
| - HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; |
| - characteristics.reserveInitialCapacity(webCharacteristics.size()); |
| - for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : |
| - webCharacteristics) { |
| - characteristics.append( |
| - m_service->device()->getOrCreateBluetoothRemoteGATTCharacteristic( |
| - m_resolver->getExecutionContext(), |
| - WTF::wrapUnique(webCharacteristic), m_service)); |
| + HeapVector<Member<BluetoothRemoteGATTCharacteristic>> gattCharacteristics; |
| + gattCharacteristics.reserveInitialCapacity(characteristics->size()); |
| + for (const auto& characteristic : characteristics.value()) { |
| + gattCharacteristics.append( |
| + device()->getOrCreateBluetoothRemoteGATTCharacteristic( |
| + resolver->getExecutionContext(), characteristic->instance_id, |
| + serviceInstanceId, characteristic->uuid, |
| + characteristic->properties, this)); |
| } |
| - m_resolver->resolve(characteristics); |
| - } |
| - |
| - void onError( |
| - int32_t |
| - error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) |
| - override { |
| - if (!m_resolver->getExecutionContext() || |
| - m_resolver->getExecutionContext()->isContextDestroyed()) |
| - return; |
| - |
| + resolver->resolve(gattCharacteristics); |
| + } else { |
| // If the resolver is not in the set of ActiveAlgorithms then the frame |
| // disconnected so we reject. |
| - if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( |
| - m_resolver.get())) { |
| - m_resolver->reject( |
| + if (!device()->gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| + resolver->reject( |
| DOMException::create(NetworkError, kGATTServerDisconnected)); |
| return; |
| } |
| - m_resolver->reject(BluetoothError::take(m_resolver, error)); |
| + resolver->reject(BluetoothError::take(resolver, static_cast<int>(result))); |
| } |
| - |
| - private: |
| - Persistent<BluetoothRemoteGATTService> m_service; |
| - mojom::blink::WebBluetoothGATTQueryQuantity m_quantity; |
| - const Persistent<ScriptPromiseResolver> m_resolver; |
| -}; |
| +} |
| ScriptPromise BluetoothRemoteGATTService::getCharacteristic( |
| ScriptState* scriptState, |
| @@ -166,7 +150,7 @@ ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( |
| DOMException::create(NetworkError, kGATTServerNotConnected)); |
| } |
| - if (!device()->isValidService(m_webService->serviceInstanceID)) { |
| + if (!device()->isValidService(m_serviceInstanceId)) { |
| return ScriptPromise::rejectWithDOMException( |
| scriptState, DOMException::create(InvalidStateError, kInvalidService)); |
| } |
| @@ -174,12 +158,20 @@ ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - WebBluetooth* webbluetooth = |
| - BluetoothSupplement::fromScriptState(scriptState); |
| - webbluetooth->getCharacteristics( |
| - m_webService->serviceInstanceID, static_cast<int32_t>(quantity), |
| - characteristicsUUID, |
| - new GetCharacteristicsCallback(this, quantity, resolver)); |
| + // We always check that the device is connected. |
| + CHECK(device()->gatt()->connected()); |
| + device()->gatt()->AddToActiveAlgorithms(resolver); |
| + |
| + mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| + bluetooth::mojom::blink::UUIDPtr characteristicsUUIDPtr = nullptr; |
| + if (!characteristicsUUID.isEmpty()) |
| + characteristicsUUIDPtr = createUUIDPtr(characteristicsUUID); |
| + service->RemoteServiceGetCharacteristics( |
| + m_serviceInstanceId, quantity, std::move(characteristicsUUIDPtr), |
| + convertToBaseCallback( |
| + WTF::bind(&BluetoothRemoteGATTService::GetCharacteristicsCallback, |
| + wrapPersistent(this), m_serviceInstanceId, quantity, |
| + wrapPersistent(resolver)))); |
| return promise; |
| } |