| 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 "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "core/dom/DOMDataView.h" | 10 #include "core/dom/DOMDataView.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 class ReadValueCallback : public WebBluetoothReadValueCallbacks { | 118 class ReadValueCallback : public WebBluetoothReadValueCallbacks { |
| 119 public: | 119 public: |
| 120 ReadValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 120 ReadValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, |
| 121 ScriptPromiseResolver* resolver) | 121 ScriptPromiseResolver* resolver) |
| 122 : m_webCharacteristic(characteristic), m_resolver(resolver) {} | 122 : m_webCharacteristic(characteristic), m_resolver(resolver) {} |
| 123 | 123 |
| 124 void onSuccess(const WebVector<uint8_t>& value) override { | 124 void onSuccess(const WebVector<uint8_t>& value) override { |
| 125 if (!m_resolver->getExecutionContext() || | 125 if (!m_resolver->getExecutionContext()) |
| 126 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | |
| 127 return; | 126 return; |
| 128 | 127 |
| 129 DOMDataView* domDataView = ConvertWebVectorToDataView(value); | 128 DOMDataView* domDataView = ConvertWebVectorToDataView(value); |
| 130 if (m_webCharacteristic) | 129 if (m_webCharacteristic) |
| 131 m_webCharacteristic->setValue(domDataView); | 130 m_webCharacteristic->setValue(domDataView); |
| 132 | 131 |
| 133 m_resolver->resolve(domDataView); | 132 m_resolver->resolve(domDataView); |
| 134 } | 133 } |
| 135 | 134 |
| 136 void onError( | 135 void onError( |
| 137 int32_t | 136 int32_t |
| 138 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | 137 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) |
| 139 override { | 138 override { |
| 140 if (!m_resolver->getExecutionContext() || | 139 if (!m_resolver->getExecutionContext()) |
| 141 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | |
| 142 return; | 140 return; |
| 143 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 141 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
| 144 } | 142 } |
| 145 | 143 |
| 146 private: | 144 private: |
| 147 WeakPersistent<BluetoothRemoteGATTCharacteristic> m_webCharacteristic; | 145 WeakPersistent<BluetoothRemoteGATTCharacteristic> m_webCharacteristic; |
| 148 Persistent<ScriptPromiseResolver> m_resolver; | 146 Persistent<ScriptPromiseResolver> m_resolver; |
| 149 }; | 147 }; |
| 150 | 148 |
| 151 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( | 149 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( |
| 152 ScriptState* scriptState) { | 150 ScriptState* scriptState) { |
| 153 WebBluetooth* webbluetooth = | 151 WebBluetooth* webbluetooth = |
| 154 BluetoothSupplement::fromScriptState(scriptState); | 152 BluetoothSupplement::fromScriptState(scriptState); |
| 155 | 153 |
| 156 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 154 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 157 ScriptPromise promise = resolver->promise(); | 155 ScriptPromise promise = resolver->promise(); |
| 158 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, | 156 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, |
| 159 new ReadValueCallback(this, resolver)); | 157 new ReadValueCallback(this, resolver)); |
| 160 | 158 |
| 161 return promise; | 159 return promise; |
| 162 } | 160 } |
| 163 | 161 |
| 164 class WriteValueCallback : public WebBluetoothWriteValueCallbacks { | 162 class WriteValueCallback : public WebBluetoothWriteValueCallbacks { |
| 165 public: | 163 public: |
| 166 WriteValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 164 WriteValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, |
| 167 ScriptPromiseResolver* resolver) | 165 ScriptPromiseResolver* resolver) |
| 168 : m_webCharacteristic(characteristic), m_resolver(resolver) {} | 166 : m_webCharacteristic(characteristic), m_resolver(resolver) {} |
| 169 | 167 |
| 170 void onSuccess(const WebVector<uint8_t>& value) override { | 168 void onSuccess(const WebVector<uint8_t>& value) override { |
| 171 if (!m_resolver->getExecutionContext() || | 169 if (!m_resolver->getExecutionContext()) |
| 172 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | |
| 173 return; | 170 return; |
| 174 | 171 |
| 175 if (m_webCharacteristic) { | 172 if (m_webCharacteristic) { |
| 176 m_webCharacteristic->setValue(ConvertWebVectorToDataView(value)); | 173 m_webCharacteristic->setValue(ConvertWebVectorToDataView(value)); |
| 177 } | 174 } |
| 178 m_resolver->resolve(); | 175 m_resolver->resolve(); |
| 179 } | 176 } |
| 180 | 177 |
| 181 void onError( | 178 void onError( |
| 182 int32_t | 179 int32_t |
| 183 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | 180 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) |
| 184 override { | 181 override { |
| 185 if (!m_resolver->getExecutionContext() || | 182 if (!m_resolver->getExecutionContext()) |
| 186 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | |
| 187 return; | 183 return; |
| 188 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 184 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
| 189 } | 185 } |
| 190 | 186 |
| 191 private: | 187 private: |
| 192 WeakPersistent<BluetoothRemoteGATTCharacteristic> m_webCharacteristic; | 188 WeakPersistent<BluetoothRemoteGATTCharacteristic> m_webCharacteristic; |
| 193 Persistent<ScriptPromiseResolver> m_resolver; | 189 Persistent<ScriptPromiseResolver> m_resolver; |
| 194 }; | 190 }; |
| 195 | 191 |
| 196 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( | 192 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 221 return promise; | 217 return promise; |
| 222 } | 218 } |
| 223 | 219 |
| 224 class NotificationsCallback : public WebBluetoothNotificationsCallbacks { | 220 class NotificationsCallback : public WebBluetoothNotificationsCallbacks { |
| 225 public: | 221 public: |
| 226 NotificationsCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 222 NotificationsCallback(BluetoothRemoteGATTCharacteristic* characteristic, |
| 227 ScriptPromiseResolver* resolver) | 223 ScriptPromiseResolver* resolver) |
| 228 : m_webCharacteristic(characteristic), m_resolver(resolver) {} | 224 : m_webCharacteristic(characteristic), m_resolver(resolver) {} |
| 229 | 225 |
| 230 void onSuccess() override { | 226 void onSuccess() override { |
| 231 if (!m_resolver->getExecutionContext() || | 227 if (!m_resolver->getExecutionContext()) |
| 232 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | |
| 233 return; | 228 return; |
| 234 | 229 |
| 235 m_resolver->resolve(m_webCharacteristic); | 230 m_resolver->resolve(m_webCharacteristic); |
| 236 } | 231 } |
| 237 | 232 |
| 238 void onError( | 233 void onError( |
| 239 int32_t | 234 int32_t |
| 240 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | 235 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) |
| 241 override { | 236 override { |
| 242 if (!m_resolver->getExecutionContext() || | 237 if (!m_resolver->getExecutionContext()) |
| 243 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | |
| 244 return; | 238 return; |
| 245 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 239 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
| 246 } | 240 } |
| 247 | 241 |
| 248 private: | 242 private: |
| 249 Persistent<BluetoothRemoteGATTCharacteristic> m_webCharacteristic; | 243 Persistent<BluetoothRemoteGATTCharacteristic> m_webCharacteristic; |
| 250 Persistent<ScriptPromiseResolver> m_resolver; | 244 Persistent<ScriptPromiseResolver> m_resolver; |
| 251 }; | 245 }; |
| 252 | 246 |
| 253 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( | 247 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( |
| (...skipping 29 matching lines...) Expand all Loading... |
| 283 | 277 |
| 284 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 278 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 285 visitor->trace(m_service); | 279 visitor->trace(m_service); |
| 286 visitor->trace(m_properties); | 280 visitor->trace(m_properties); |
| 287 visitor->trace(m_value); | 281 visitor->trace(m_value); |
| 288 EventTargetWithInlineData::trace(visitor); | 282 EventTargetWithInlineData::trace(visitor); |
| 289 ActiveDOMObject::trace(visitor); | 283 ActiveDOMObject::trace(visitor); |
| 290 } | 284 } |
| 291 | 285 |
| 292 } // namespace blink | 286 } // namespace blink |
| OLD | NEW |