Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "config.h" | 5 #include "config.h" |
| 6 #include "modules/bluetooth/BluetoothGATTCharacteristic.h" | 6 #include "modules/bluetooth/BluetoothGATTCharacteristic.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "core/events/Event.h" | |
| 13 #include "modules/bluetooth/BluetoothError.h" | 14 #include "modules/bluetooth/BluetoothError.h" |
| 14 #include "modules/bluetooth/BluetoothSupplement.h" | 15 #include "modules/bluetooth/BluetoothSupplement.h" |
| 15 #include "modules/bluetooth/ConvertWebVectorToArrayBuffer.h" | 16 #include "modules/bluetooth/ConvertWebVectorToArrayBuffer.h" |
| 16 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 17 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 17 | 18 |
| 18 namespace blink { | 19 namespace blink { |
| 19 | 20 |
| 20 BluetoothGATTCharacteristic::BluetoothGATTCharacteristic(ExecutionContext* conte xt, PassOwnPtr<WebBluetoothGATTCharacteristicInit> webCharacteristic) | 21 BluetoothGATTCharacteristic::BluetoothGATTCharacteristic(ExecutionContext* conte xt, PassOwnPtr<WebBluetoothGATTCharacteristicInit> webCharacteristic) |
| 21 : ActiveDOMObject(context) | 22 : ActiveDOMObject(context) |
| 22 , m_webCharacteristic(webCharacteristic) | 23 , m_webCharacteristic(webCharacteristic) |
| 23 , m_stopped(false) | 24 , m_stopped(false) |
| 24 { | 25 { |
| 25 // See example in Source/platform/heap/ThreadState.h | 26 // See example in Source/platform/heap/ThreadState.h |
| 26 ThreadState::current()->registerPreFinalizer(this); | 27 ThreadState::current()->registerPreFinalizer(this); |
| 27 } | 28 } |
| 28 | 29 |
| 29 BluetoothGATTCharacteristic* BluetoothGATTCharacteristic::take(ScriptPromiseReso lver* resolver, PassOwnPtr<WebBluetoothGATTCharacteristicInit> webCharacteristic ) | 30 BluetoothGATTCharacteristic* BluetoothGATTCharacteristic::take(ScriptPromiseReso lver* resolver, PassOwnPtr<WebBluetoothGATTCharacteristicInit> webCharacteristic ) |
| 30 { | 31 { |
| 31 if (!webCharacteristic) { | 32 if (!webCharacteristic) { |
| 32 return nullptr; | 33 return nullptr; |
| 33 } | 34 } |
| 34 BluetoothGATTCharacteristic* characteristic = new BluetoothGATTCharacteristi c(resolver->executionContext(), webCharacteristic); | 35 BluetoothGATTCharacteristic* characteristic = new BluetoothGATTCharacteristi c(resolver->executionContext(), webCharacteristic); |
| 35 // See note in ActiveDOMObject about suspendIfNeeded. | 36 // See note in ActiveDOMObject about suspendIfNeeded. |
| 36 characteristic->suspendIfNeeded(); | 37 characteristic->suspendIfNeeded(); |
| 37 return characteristic; | 38 return characteristic; |
| 38 } | 39 } |
| 39 | 40 |
| 41 void BluetoothGATTCharacteristic::dispatchCharacteristicValueChanged( | |
| 42 const WebVector<uint8_t>& value) | |
| 43 { | |
| 44 static_assert(sizeof(*value.data()) == 1, "uint8_t should be a single byte") ; | |
|
palmer
2015/10/19 19:23:33
This doesn't necessarily mean what you think it me
ortuno
2015/10/19 20:09:18
That assert is there to make sure that the byteLen
| |
| 45 | |
| 46 m_value = DOMArrayBuffer::create(value.data(), value.size()); | |
| 47 | |
| 48 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); | |
| 49 } | |
| 50 | |
| 40 void BluetoothGATTCharacteristic::stop() | 51 void BluetoothGATTCharacteristic::stop() |
| 41 { | 52 { |
| 42 notifyCharacteristicObjectRemoved(); | 53 notifyCharacteristicObjectRemoved(); |
| 43 } | 54 } |
| 44 | 55 |
| 45 void BluetoothGATTCharacteristic::dispose() | 56 void BluetoothGATTCharacteristic::dispose() |
| 46 { | 57 { |
| 47 notifyCharacteristicObjectRemoved(); | 58 notifyCharacteristicObjectRemoved(); |
| 48 } | 59 } |
| 49 | 60 |
| 50 void BluetoothGATTCharacteristic::notifyCharacteristicObjectRemoved() | 61 void BluetoothGATTCharacteristic::notifyCharacteristicObjectRemoved() |
| 51 { | 62 { |
| 52 if (!m_stopped) { | 63 if (!m_stopped) { |
| 53 m_stopped = true; | 64 m_stopped = true; |
| 54 WebBluetooth* webbluetooth = BluetoothSupplement::fromExecutionContext(A ctiveDOMObject::executionContext()); | 65 WebBluetooth* webbluetooth = BluetoothSupplement::fromExecutionContext(A ctiveDOMObject::executionContext()); |
| 55 webbluetooth->characteristicObjectRemoved(m_webCharacteristic->character isticInstanceID, this); | 66 webbluetooth->characteristicObjectRemoved(m_webCharacteristic->character isticInstanceID, this); |
| 56 } | 67 } |
| 57 } | 68 } |
| 58 | 69 |
| 70 const WTF::AtomicString& BluetoothGATTCharacteristic::interfaceName() const | |
| 71 { | |
| 72 return EventTargetNames::BluetoothGATTCharacteristic; | |
| 73 } | |
| 74 | |
| 75 ExecutionContext* BluetoothGATTCharacteristic::executionContext() const | |
| 76 { | |
| 77 return ActiveDOMObject::executionContext(); | |
| 78 } | |
| 79 | |
| 80 bool BluetoothGATTCharacteristic::addEventListener(const AtomicString& eventType , PassRefPtrWillBeRawPtr<EventListener> listener, bool useCapture) | |
| 81 { | |
| 82 // We will also need to unregister a characteristic once all the event | |
| 83 // listeners have been removed. See http://crbug.com/541390 | |
| 84 if (eventType == EventTypeNames::characteristicvaluechanged) { | |
| 85 WebBluetooth* webbluetooth = BluetoothSupplement::fromExecutionContext(e xecutionContext()); | |
| 86 webbluetooth->registerCharacteristicObject(m_webCharacteristic->characte risticInstanceID, this); | |
| 87 } | |
| 88 return EventTarget::addEventListener(eventType, listener, useCapture); | |
| 89 } | |
| 90 | |
| 59 ScriptPromise BluetoothGATTCharacteristic::readValue(ScriptState* scriptState) | 91 ScriptPromise BluetoothGATTCharacteristic::readValue(ScriptState* scriptState) |
| 60 { | 92 { |
| 61 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e); | 93 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e); |
| 62 | 94 |
| 63 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 95 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 64 ScriptPromise promise = resolver->promise(); | 96 ScriptPromise promise = resolver->promise(); |
| 65 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, new C allbackPromiseAdapter<ConvertWebVectorToArrayBuffer, BluetoothError>(resolver)); | 97 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, new C allbackPromiseAdapter<ConvertWebVectorToArrayBuffer, BluetoothError>(resolver)); |
| 66 | 98 |
| 67 return promise; | 99 return promise; |
| 68 } | 100 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 { | 134 { |
| 103 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e); | 135 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e); |
| 104 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 136 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 105 ScriptPromise promise = resolver->promise(); | 137 ScriptPromise promise = resolver->promise(); |
| 106 webbluetooth->stopNotifications(m_webCharacteristic->characteristicInstanceI D, this, new CallbackPromiseAdapter<void, BluetoothError>(resolver)); | 138 webbluetooth->stopNotifications(m_webCharacteristic->characteristicInstanceI D, this, new CallbackPromiseAdapter<void, BluetoothError>(resolver)); |
| 107 return promise; | 139 return promise; |
| 108 } | 140 } |
| 109 | 141 |
| 110 DEFINE_TRACE(BluetoothGATTCharacteristic) | 142 DEFINE_TRACE(BluetoothGATTCharacteristic) |
| 111 { | 143 { |
| 144 RefCountedGarbageCollectedEventTargetWithInlineData<BluetoothGATTCharacteris tic>::trace(visitor); | |
| 112 ActiveDOMObject::trace(visitor); | 145 ActiveDOMObject::trace(visitor); |
| 113 } | 146 } |
| 114 | 147 |
| 115 } // namespace blink | 148 } // namespace blink |
| OLD | NEW |