diff --git a/dom/bluetooth/BluetoothDevice.cpp b/dom/bluetooth/BluetoothDevice.cpp index 50da5b86e5c1..b1ecc56815f1 100644 --- a/dom/bluetooth/BluetoothDevice.cpp +++ b/dom/bluetooth/BluetoothDevice.cpp @@ -30,13 +30,17 @@ NS_IMPL_CYCLE_COLLECTION_TRACE_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothDevice, nsDOMEventTargetHelper) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS - NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(propertychanged) + NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(propertychanged) + NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(connected) + NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(disconnected) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothDevice, nsDOMEventTargetHelper) tmp->Unroot(); - NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(propertychanged) + NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(propertychanged) + NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(connected) + NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(disconnected) NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothDevice) @@ -170,9 +174,29 @@ BluetoothDevice::Notify(const BluetoothSignal& aData) // Get BluetoothNamedValue, make sure array length is 1 BluetoothNamedValue v = aData.value().get_ArrayOfBluetoothNamedValue()[0]; nsString name = v.name(); - SetPropertyByValue(v); - nsRefPtr e = BluetoothPropertyEvent::Create(name); - e->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("propertychanged")); + + if (name.EqualsLiteral("Connected")) { + bool isConnected = v.value(); + nsRefPtr event = new nsDOMEvent(nullptr, nullptr); + nsresult rv; + if (isConnected) { + rv = event->InitEvent(NS_LITERAL_STRING("connected"), false, false); + } else { + rv = event->InitEvent(NS_LITERAL_STRING("disconnected"), false, false); + } + if (NS_FAILED(rv)) { + NS_WARNING("Failed to init the connected/disconnected event!!!"); + return; + } + + event->SetTrusted(true); + bool dummy; + DispatchEvent(event, &dummy); + } else { + SetPropertyByValue(v); + nsRefPtr e = BluetoothPropertyEvent::Create(name); + e->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("propertychanged")); + } } else { #ifdef DEBUG nsCString warningMsg; @@ -231,3 +255,5 @@ BluetoothDevice::GetUuids(JSContext* aCx, jsval* aUuids) } NS_IMPL_EVENT_HANDLER(BluetoothDevice, propertychanged) +NS_IMPL_EVENT_HANDLER(BluetoothDevice, connected) +NS_IMPL_EVENT_HANDLER(BluetoothDevice, disconnected) diff --git a/dom/bluetooth/BluetoothDevice.h b/dom/bluetooth/BluetoothDevice.h index 5d4c275c451c..6add1139a748 100644 --- a/dom/bluetooth/BluetoothDevice.h +++ b/dom/bluetooth/BluetoothDevice.h @@ -75,6 +75,8 @@ private: nsTArray mUuids; NS_DECL_EVENT_HANDLER(propertychanged) + NS_DECL_EVENT_HANDLER(connected) + NS_DECL_EVENT_HANDLER(disconnected) }; END_BLUETOOTH_NAMESPACE diff --git a/dom/bluetooth/nsIDOMBluetoothDevice.idl b/dom/bluetooth/nsIDOMBluetoothDevice.idl index 718676a5b0a2..e792f7d2e471 100644 --- a/dom/bluetooth/nsIDOMBluetoothDevice.idl +++ b/dom/bluetooth/nsIDOMBluetoothDevice.idl @@ -6,7 +6,7 @@ #include "nsIDOMEventTarget.idl" -[scriptable, builtinclass, uuid(24c64513-9587-46c6-b718-bb9b9a754b0d)] +[scriptable, builtinclass, uuid(647bb64c-8d45-4642-b86b-b3b80d4c8c25)] interface nsIDOMBluetoothDevice : nsIDOMEventTarget { readonly attribute DOMString address; @@ -16,4 +16,6 @@ interface nsIDOMBluetoothDevice : nsIDOMEventTarget readonly attribute bool connected; readonly attribute bool paired; attribute nsIDOMEventListener onpropertychanged; + attribute nsIDOMEventListener onconnected; + attribute nsIDOMEventListener ondisconnected; };