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 "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "core/events/Event.h" | 9 #include "core/events/Event.h" |
| 10 #include "core/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 namespace blink { | 23 namespace blink { |
| 24 | 24 |
| 25 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic( | 25 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic( |
| 26 ExecutionContext* context, | 26 ExecutionContext* context, |
| 27 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, | 27 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, |
| 28 BluetoothRemoteGATTService* service, | 28 BluetoothRemoteGATTService* service, |
| 29 BluetoothDevice* device) | 29 BluetoothDevice* device) |
| 30 : ContextLifecycleObserver(context), | 30 : ContextLifecycleObserver(context), |
| 31 m_characteristic(std::move(characteristic)), | 31 m_characteristic(std::move(characteristic)), |
| 32 m_service(service), | 32 m_service(service), |
| 33 m_clientBinding(this), | |
| 33 m_stopped(false), | 34 m_stopped(false), |
| 34 m_device(device) { | 35 m_device(device) { |
| 35 m_properties = | 36 m_properties = |
| 36 BluetoothCharacteristicProperties::create(m_characteristic->properties); | 37 BluetoothCharacteristicProperties::create(m_characteristic->properties); |
| 37 } | 38 } |
| 38 | 39 |
| 39 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::create( | 40 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::create( |
| 40 ExecutionContext* context, | 41 ExecutionContext* context, |
| 41 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, | 42 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, |
| 42 BluetoothRemoteGATTService* service, | 43 BluetoothRemoteGATTService* service, |
| 43 BluetoothDevice* device) { | 44 BluetoothDevice* device) { |
| 44 return new BluetoothRemoteGATTCharacteristic( | 45 return new BluetoothRemoteGATTCharacteristic( |
| 45 context, std::move(characteristic), service, device); | 46 context, std::move(characteristic), service, device); |
| 46 } | 47 } |
| 47 | 48 |
| 48 void BluetoothRemoteGATTCharacteristic::setValue(DOMDataView* domDataView) { | 49 void BluetoothRemoteGATTCharacteristic::setValue(DOMDataView* domDataView) { |
| 49 m_value = domDataView; | 50 m_value = domDataView; |
| 50 } | 51 } |
| 51 | 52 |
| 52 void BluetoothRemoteGATTCharacteristic::dispatchCharacteristicValueChanged( | 53 void BluetoothRemoteGATTCharacteristic::contextDestroyed(ExecutionContext*) { |
| 53 const Vector<uint8_t>& value) { | 54 notifyCharacteristicObjectRemoved(); |
| 55 } | |
| 56 | |
| 57 void BluetoothRemoteGATTCharacteristic::RemoteCharacteristicValueChanged( | |
| 58 const WTF::Vector<uint8_t>& value) { | |
| 54 if (!getGatt()->connected()) | 59 if (!getGatt()->connected()) |
| 55 return; | 60 return; |
| 56 this->setValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); | 61 this->setValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); |
| 57 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); | 62 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); |
| 58 } | 63 } |
| 59 | 64 |
| 60 void BluetoothRemoteGATTCharacteristic::contextDestroyed(ExecutionContext*) { | |
| 61 notifyCharacteristicObjectRemoved(); | |
| 62 } | |
| 63 | |
| 64 void BluetoothRemoteGATTCharacteristic::dispose() { | 65 void BluetoothRemoteGATTCharacteristic::dispose() { |
| 66 // The pipe to this object must be closed when is marked unreachable to | |
|
ortuno
2017/02/24 03:28:52
notifyCharacteristicObjectRemoved is used so that
juncai
2017/03/01 02:04:12
Done.
| |
| 67 // prevent messages from being dispatched before lazy sweeping. | |
| 68 if (m_clientBinding.is_bound()) | |
| 69 m_clientBinding.Close(); | |
| 65 notifyCharacteristicObjectRemoved(); | 70 notifyCharacteristicObjectRemoved(); |
| 66 } | 71 } |
| 67 | 72 |
| 68 void BluetoothRemoteGATTCharacteristic::notifyCharacteristicObjectRemoved() { | 73 void BluetoothRemoteGATTCharacteristic::notifyCharacteristicObjectRemoved() { |
| 69 if (!m_stopped) { | 74 if (!m_stopped) { |
| 70 m_stopped = true; | 75 m_stopped = true; |
| 71 m_device->bluetooth()->characteristicObjectRemoved( | |
| 72 m_characteristic->instance_id); | |
| 73 } | 76 } |
| 74 } | 77 } |
| 75 | 78 |
| 76 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() | 79 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() |
| 77 const { | 80 const { |
| 78 return EventTargetNames::BluetoothRemoteGATTCharacteristic; | 81 return EventTargetNames::BluetoothRemoteGATTCharacteristic; |
| 79 } | 82 } |
| 80 | 83 |
| 81 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() | 84 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() |
| 82 const { | 85 const { |
| 83 return ContextLifecycleObserver::getExecutionContext(); | 86 return ContextLifecycleObserver::getExecutionContext(); |
| 84 } | 87 } |
| 85 | 88 |
| 86 void BluetoothRemoteGATTCharacteristic::addedEventListener( | 89 void BluetoothRemoteGATTCharacteristic::addedEventListener( |
| 87 const AtomicString& eventType, | 90 const AtomicString& eventType, |
| 88 RegisteredEventListener& registeredListener) { | 91 RegisteredEventListener& registeredListener) { |
| 89 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); | 92 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); |
| 90 // We will also need to unregister a characteristic once all the event | |
| 91 // listeners have been removed. See http://crbug.com/541390 | |
| 92 if (eventType == EventTypeNames::characteristicvaluechanged) { | |
| 93 m_device->bluetooth()->registerCharacteristicObject( | |
| 94 m_characteristic->instance_id, this); | |
| 95 } | |
| 96 } | 93 } |
| 97 | 94 |
| 98 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( | 95 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( |
| 99 ScriptPromiseResolver* resolver, | 96 ScriptPromiseResolver* resolver, |
| 100 mojom::blink::WebBluetoothResult result, | 97 mojom::blink::WebBluetoothResult result, |
| 101 const Optional<Vector<uint8_t>>& value) { | 98 const Optional<Vector<uint8_t>>& value) { |
| 102 if (!resolver->getExecutionContext() || | 99 if (!resolver->getExecutionContext() || |
| 103 resolver->getExecutionContext()->isContextDestroyed()) | 100 resolver->getExecutionContext()->isContextDestroyed()) |
| 104 return; | 101 return; |
| 105 | 102 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 m_characteristic->instance_id, valueVector, | 214 m_characteristic->instance_id, valueVector, |
| 218 convertToBaseCallback(WTF::bind( | 215 convertToBaseCallback(WTF::bind( |
| 219 &BluetoothRemoteGATTCharacteristic::WriteValueCallback, | 216 &BluetoothRemoteGATTCharacteristic::WriteValueCallback, |
| 220 wrapPersistent(this), wrapPersistent(resolver), valueVector))); | 217 wrapPersistent(this), wrapPersistent(resolver), valueVector))); |
| 221 | 218 |
| 222 return promise; | 219 return promise; |
| 223 } | 220 } |
| 224 | 221 |
| 225 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( | 222 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( |
| 226 ScriptPromiseResolver* resolver, | 223 ScriptPromiseResolver* resolver, |
| 227 mojom::blink::WebBluetoothResult result) { | 224 mojom::blink::WebBluetoothResult result, |
| 225 mojom::blink::WebBluetoothCharacteristicClientAssociatedRequest request) { | |
| 228 if (!resolver->getExecutionContext() || | 226 if (!resolver->getExecutionContext() || |
| 229 resolver->getExecutionContext()->isContextDestroyed()) | 227 resolver->getExecutionContext()->isContextDestroyed()) |
| 230 return; | 228 return; |
| 231 | 229 |
| 232 // If the device is disconnected, reject. | 230 // If the device is disconnected, reject. |
| 233 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { | 231 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 234 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( | 232 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( |
| 235 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); | 233 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); |
| 236 return; | 234 return; |
| 237 } | 235 } |
| 238 | 236 |
| 237 m_clientBinding.Bind(std::move(request)); | |
| 238 | |
| 239 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 239 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 240 resolver->resolve(this); | 240 resolver->resolve(this); |
| 241 } else { | 241 } else { |
| 242 resolver->reject(BluetoothError::take(resolver, result)); | 242 resolver->reject(BluetoothError::take(resolver, result)); |
| 243 } | 243 } |
| 244 } | 244 } |
| 245 | 245 |
| 246 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( | 246 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( |
| 247 ScriptState* scriptState) { | 247 ScriptState* scriptState) { |
| 248 if (!getGatt()->connected()) { | 248 if (!getGatt()->connected()) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 294 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 295 ScriptPromise promise = resolver->promise(); | 295 ScriptPromise promise = resolver->promise(); |
| 296 getGatt()->AddToActiveAlgorithms(resolver); | 296 getGatt()->AddToActiveAlgorithms(resolver); |
| 297 | 297 |
| 298 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 298 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 299 service->RemoteCharacteristicStopNotifications( | 299 service->RemoteCharacteristicStopNotifications( |
| 300 m_characteristic->instance_id, | 300 m_characteristic->instance_id, |
| 301 convertToBaseCallback( | 301 convertToBaseCallback( |
| 302 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | 302 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, |
| 303 wrapPersistent(this), wrapPersistent(resolver), | 303 wrapPersistent(this), wrapPersistent(resolver), |
| 304 mojom::blink::WebBluetoothResult::SUCCESS))); | 304 mojom::blink::WebBluetoothResult::SUCCESS, nullptr))); |
| 305 return promise; | 305 return promise; |
| 306 } | 306 } |
| 307 | 307 |
| 308 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptor( | 308 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptor( |
| 309 ScriptState* scriptState, | 309 ScriptState* scriptState, |
| 310 const StringOrUnsignedLong& descriptorUUID, | 310 const StringOrUnsignedLong& descriptorUUID, |
| 311 ExceptionState& exceptionState) { | 311 ExceptionState& exceptionState) { |
| 312 String descriptor = | 312 String descriptor = |
| 313 BluetoothUUID::getDescriptor(descriptorUUID, exceptionState); | 313 BluetoothUUID::getDescriptor(descriptorUUID, exceptionState); |
| 314 if (exceptionState.hadException()) | 314 if (exceptionState.hadException()) |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 421 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 421 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 422 visitor->trace(m_service); | 422 visitor->trace(m_service); |
| 423 visitor->trace(m_properties); | 423 visitor->trace(m_properties); |
| 424 visitor->trace(m_value); | 424 visitor->trace(m_value); |
| 425 visitor->trace(m_device); | 425 visitor->trace(m_device); |
| 426 EventTargetWithInlineData::trace(visitor); | 426 EventTargetWithInlineData::trace(visitor); |
| 427 ContextLifecycleObserver::trace(visitor); | 427 ContextLifecycleObserver::trace(visitor); |
| 428 } | 428 } |
| 429 | 429 |
| 430 } // namespace blink | 430 } // namespace blink |
| OLD | NEW |