Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: third_party/WebKit/Source/modules/bluetooth/BluetoothGATTCharacteristic.cpp

Issue 1382743002: bluetooth: Add characteristicvaluechanged event (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-notifications-1
Patch Set: Final clean up Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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") ;
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)
Jeffrey Yasskin 2015/10/15 23:00:51 Presumably we'll also need to unregister on remove
ortuno 2015/10/16 01:24:21 Done.
81 {
82 if (eventType == EventTypeNames::characteristicvaluechanged) {
83 WebBluetooth* webbluetooth = BluetoothSupplement::fromExecutionContext(e xecutionContext());
84 webbluetooth->registerCharacteristicObject(m_webCharacteristic->characte risticInstanceID, this);
85 }
86 return EventTarget::addEventListener(eventType, listener, useCapture);
87 }
88
59 ScriptPromise BluetoothGATTCharacteristic::readValue(ScriptState* scriptState) 89 ScriptPromise BluetoothGATTCharacteristic::readValue(ScriptState* scriptState)
60 { 90 {
61 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e); 91 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e);
62 92
63 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 93 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
64 ScriptPromise promise = resolver->promise(); 94 ScriptPromise promise = resolver->promise();
65 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, new C allbackPromiseAdapter<ConvertWebVectorToArrayBuffer, BluetoothError>(resolver)); 95 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, new C allbackPromiseAdapter<ConvertWebVectorToArrayBuffer, BluetoothError>(resolver));
66 96
67 return promise; 97 return promise;
68 } 98 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 { 132 {
103 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e); 133 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e);
104 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 134 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
105 ScriptPromise promise = resolver->promise(); 135 ScriptPromise promise = resolver->promise();
106 webbluetooth->stopNotifications(m_webCharacteristic->characteristicInstanceI D, this, new CallbackPromiseAdapter<void, BluetoothError>(resolver)); 136 webbluetooth->stopNotifications(m_webCharacteristic->characteristicInstanceI D, this, new CallbackPromiseAdapter<void, BluetoothError>(resolver));
107 return promise; 137 return promise;
108 } 138 }
109 139
110 DEFINE_TRACE(BluetoothGATTCharacteristic) 140 DEFINE_TRACE(BluetoothGATTCharacteristic)
111 { 141 {
142 RefCountedGarbageCollectedEventTargetWithInlineData<BluetoothGATTCharacteris tic>::trace(visitor);
112 ActiveDOMObject::trace(visitor); 143 ActiveDOMObject::trace(visitor);
113 } 144 }
114 145
115 } // namespace blink 146 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698