void BluetoothDevice::Notify(const BluetoothSignal& aData) { if (aData.name().EqualsLiteral("PropertyChanged")) { NS_ASSERTION(aData.value().type() == BluetoothValue::TArrayOfBluetoothNamedValue, "PropertyChanged: Invalid value type"); InfallibleTArray<BluetoothNamedValue> arr = aData.value().get_ArrayOfBluetoothNamedValue(); NS_ASSERTION(arr.Length() == 1, "Got more than one property in a change message!"); BluetoothNamedValue v = arr[0]; nsString name = v.name(); SetPropertyByValue(v); if (name.EqualsLiteral("Connected")) { DispatchTrustedEvent(mConnected ? NS_LITERAL_STRING("connected") : NS_LITERAL_STRING("disconnected")); } else { nsRefPtr<BluetoothPropertyEvent> e = BluetoothPropertyEvent::Create(name); e->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("propertychanged")); } } else { #ifdef DEBUG nsCString warningMsg; warningMsg.AssignLiteral("Not handling device signal: "); warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name())); NS_WARNING(warningMsg.get()); #endif } }
void BluetoothAdapter::SetPropertyByValue(const BluetoothNamedValue& aValue) { const nsString& name = aValue.name(); const BluetoothValue& value = aValue.value(); if (name.EqualsLiteral("Name")) { mName = value.get_nsString(); } else if (name.EqualsLiteral("Address")) { mAddress = value.get_nsString(); } else if (name.EqualsLiteral("Path")) { mPath = value.get_nsString(); } else if (name.EqualsLiteral("Discoverable")) { mDiscoverable = value.get_bool(); } else if (name.EqualsLiteral("Discovering")) { mDiscovering = value.get_bool(); } else if (name.EqualsLiteral("Pairable")) { mPairable = value.get_bool(); } else if (name.EqualsLiteral("Powered")) { mPowered = value.get_bool(); } else if (name.EqualsLiteral("PairableTimeout")) { mPairableTimeout = value.get_uint32_t(); } else if (name.EqualsLiteral("DiscoverableTimeout")) { mDiscoverableTimeout = value.get_uint32_t(); } else if (name.EqualsLiteral("Class")) { mClass = value.get_uint32_t(); } else if (name.EqualsLiteral("UUIDs")) { mUuids = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS_VOID(rv); if (NS_FAILED(nsTArrayToJSArray(sc->GetNativeContext(), mUuids, &mJsUuids))) { NS_WARNING("Cannot set JS UUIDs object!"); return; } Root(); } else if (name.EqualsLiteral("Devices")) { mDeviceAddresses = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS_VOID(rv); if (NS_FAILED(nsTArrayToJSArray(sc->GetNativeContext(), mDeviceAddresses, &mJsDeviceAddresses))) { NS_WARNING("Cannot set JS Devices object!"); return; } Root(); } else { #ifdef DEBUG nsCString warningMsg; warningMsg.AssignLiteral("Not handling adapter property: "); warningMsg.Append(NS_ConvertUTF16toUTF8(name)); NS_WARNING(warningMsg.get()); #endif } }
void BluetoothDevice::SetPropertyByValue(const BluetoothNamedValue& aValue) { const nsString& name = aValue.name(); const BluetoothValue& value = aValue.value(); if (name.EqualsLiteral("Name")) { mName = value.get_nsString(); } else if (name.EqualsLiteral("Path")) { MOZ_ASSERT(value.get_nsString().Length() > 0); mPath = value.get_nsString(); } else if (name.EqualsLiteral("Address")) { mAddress = value.get_nsString(); } else if (name.EqualsLiteral("Class")) { mClass = value.get_uint32_t(); } else if (name.EqualsLiteral("Icon")) { mIcon = value.get_nsString(); } else if (name.EqualsLiteral("Connected")) { mConnected = value.get_bool(); } else if (name.EqualsLiteral("Paired")) { mPaired = value.get_bool(); } else if (name.EqualsLiteral("UUIDs")) { mUuids = value.get_ArrayOfnsString(); AutoJSAPI jsapi; if (!jsapi.Init(GetOwner())) { BT_WARNING("Failed to initialise AutoJSAPI!"); return; } JSContext* cx = jsapi.cx(); JS::Rooted<JSObject*> uuids(cx); if (NS_FAILED(nsTArrayToJSArray(cx, mUuids, &uuids))) { BT_WARNING("Cannot set JS UUIDs object!"); return; } mJsUuids = uuids; Root(); } else if (name.EqualsLiteral("Services")) { mServices = value.get_ArrayOfnsString(); AutoJSAPI jsapi; if (!jsapi.Init(GetOwner())) { BT_WARNING("Failed to initialise AutoJSAPI!"); return; } JSContext* cx = jsapi.cx(); JS::Rooted<JSObject*> services(cx); if (NS_FAILED(nsTArrayToJSArray(cx, mServices, &services))) { BT_WARNING("Cannot set JS Services object!"); return; } mJsServices = services; Root(); } else { nsCString warningMsg; warningMsg.AssignLiteral("Not handling device property: "); warningMsg.Append(NS_ConvertUTF16toUTF8(name)); BT_WARNING(warningMsg.get()); } }
void BluetoothDevice::SetPropertyByValue(const BluetoothNamedValue& aValue) { const nsString& name = aValue.name(); const BluetoothValue& value = aValue.value(); if (name.EqualsLiteral("Name")) { mName = value.get_nsString(); } else if (name.EqualsLiteral("Path")) { MOZ_ASSERT(value.get_nsString().Length() > 0); mPath = value.get_nsString(); } else if (name.EqualsLiteral("Address")) { mAddress = value.get_nsString(); } else if (name.EqualsLiteral("Class")) { mClass = value.get_uint32_t(); } else if (name.EqualsLiteral("Icon")) { mIcon = value.get_nsString(); } else if (name.EqualsLiteral("Connected")) { mConnected = value.get_bool(); } else if (name.EqualsLiteral("Paired")) { mPaired = value.get_bool(); } else if (name.EqualsLiteral("UUIDs")) { mUuids = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS_VOID(rv); NS_ENSURE_TRUE_VOID(sc); AutoPushJSContext cx(sc->GetNativeContext()); JS::Rooted<JSObject*> uuids(cx); if (NS_FAILED(nsTArrayToJSArray(cx, mUuids, uuids.address()))) { BT_WARNING("Cannot set JS UUIDs object!"); return; } mJsUuids = uuids; Root(); } else if (name.EqualsLiteral("Services")) { mServices = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS_VOID(rv); NS_ENSURE_TRUE_VOID(sc); AutoPushJSContext cx(sc->GetNativeContext()); JS::Rooted<JSObject*> services(cx); if (NS_FAILED(nsTArrayToJSArray(cx, mServices, services.address()))) { BT_WARNING("Cannot set JS Services object!"); return; } mJsServices = services; Root(); } else { nsCString warningMsg; warningMsg.AssignLiteral("Not handling device property: "); warningMsg.Append(NS_ConvertUTF16toUTF8(name)); BT_WARNING(warningMsg.get()); } }
void BluetoothDevice::SetPropertyByValue(const BluetoothNamedValue& aValue) { const nsString& name = aValue.name(); const BluetoothValue& value = aValue.value(); if (name.EqualsLiteral("Name")) { mName = value.get_nsString(); } else if (name.EqualsLiteral("Path")) { MOZ_ASSERT(value.get_nsString().Length() > 0); mPath = value.get_nsString(); } else if (name.EqualsLiteral("Address")) { mAddress = value.get_nsString(); } else if (name.EqualsLiteral("Class")) { mClass = value.get_uint32_t(); } else if (name.EqualsLiteral("Icon")) { mIcon = value.get_nsString(); } else if (name.EqualsLiteral("Connected")) { mConnected = value.get_bool(); } else if (name.EqualsLiteral("Paired")) { mPaired = value.get_bool(); } else if (name.EqualsLiteral("UUIDs")) { mUuids = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); if (sc) { rv = nsTArrayToJSArray(sc->GetNativeContext(), mUuids, &mJsUuids); if (NS_FAILED(rv)) { NS_WARNING("Cannot set JS UUIDs object!"); return; } Root(); } else { NS_WARNING("Could not get context!"); } } else if (name.EqualsLiteral("Services")) { mServices = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); if (sc) { rv = nsTArrayToJSArray(sc->GetNativeContext(), mServices, &mJsServices); if (NS_FAILED(rv)) { NS_WARNING("Cannot set JS Services object!"); return; } Root(); } else { NS_WARNING("Could not get context!"); } #ifdef DEBUG } else { nsCString warningMsg; warningMsg.AssignLiteral("Not handling device property: "); warningMsg.Append(NS_ConvertUTF16toUTF8(name)); NS_WARNING(warningMsg.get()); #endif } }
void BluetoothAdapter::Notify(const BluetoothSignal& aData) { InfallibleTArray<BluetoothNamedValue> arr; if (aData.name().EqualsLiteral("DeviceFound")) { nsRefPtr<BluetoothDevice> device = BluetoothDevice::Create(GetOwner(), mPath, aData.value()); nsCOMPtr<nsIDOMEvent> event; NS_NewDOMBluetoothDeviceEvent(getter_AddRefs(event), nullptr, nullptr); nsCOMPtr<nsIDOMBluetoothDeviceEvent> e = do_QueryInterface(event); e->InitBluetoothDeviceEvent(NS_LITERAL_STRING("devicefound"), false, false, device); DispatchTrustedEvent(event); } else if (aData.name().EqualsLiteral("DeviceDisappeared")) { const nsAString& deviceAddress = aData.value().get_nsString(); nsCOMPtr<nsIDOMEvent> event; NS_NewDOMBluetoothDeviceAddressEvent(getter_AddRefs(event), nullptr, nullptr); nsCOMPtr<nsIDOMBluetoothDeviceAddressEvent> e = do_QueryInterface(event); e->InitBluetoothDeviceAddressEvent(NS_LITERAL_STRING("devicedisappeared"), false, false, deviceAddress); DispatchTrustedEvent(e); } else if (aData.name().EqualsLiteral("DeviceCreated")) { NS_ASSERTION(aData.value().type() == BluetoothValue::TArrayOfBluetoothNamedValue, "DeviceCreated: Invalid value type"); nsRefPtr<BluetoothDevice> device = BluetoothDevice::Create(GetOwner(), GetPath(), aData.value()); nsCOMPtr<nsIDOMEvent> event; NS_NewDOMBluetoothDeviceEvent(getter_AddRefs(event), nullptr, nullptr); nsCOMPtr<nsIDOMBluetoothDeviceEvent> e = do_QueryInterface(event); e->InitBluetoothDeviceEvent(NS_LITERAL_STRING("devicecreated"), false, false, device); DispatchTrustedEvent(e); } else if (aData.name().EqualsLiteral("PropertyChanged")) { NS_ASSERTION(aData.value().type() == BluetoothValue::TArrayOfBluetoothNamedValue, "PropertyChanged: Invalid value type"); arr = aData.value().get_ArrayOfBluetoothNamedValue(); NS_ASSERTION(arr.Length() == 1, "Got more than one property in a change message!"); BluetoothNamedValue v = arr[0]; SetPropertyByValue(v); nsRefPtr<BluetoothPropertyEvent> e = BluetoothPropertyEvent::Create(v.name()); e->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("propertychanged")); } else { #ifdef DEBUG nsCString warningMsg; warningMsg.AssignLiteral("Not handling adapter signal: "); warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name())); NS_WARNING(warningMsg.get()); #endif } }
void BluetoothAdapter::SetPropertyByValue(const BluetoothNamedValue& aValue) { const nsString& name = aValue.name(); const BluetoothValue& value = aValue.value(); if (name.EqualsLiteral("State")) { mState = value.get_bool() ? BluetoothAdapterState::Enabled : BluetoothAdapterState::Disabled; // Clear saved devices when state changes to disabled if (mState == BluetoothAdapterState::Disabled) { mDevices.Clear(); } } else if (name.EqualsLiteral("Name")) { mName = value.get_nsString(); } else if (name.EqualsLiteral("Address")) { mAddress = value.get_nsString(); } else if (name.EqualsLiteral("Discoverable")) { mDiscoverable = value.get_bool(); } else if (name.EqualsLiteral("Discovering")) { mDiscovering = value.get_bool(); if (!mDiscovering) { // Reset discovery handle in use to nullptr SetDiscoveryHandleInUse(nullptr); } } else if (name.EqualsLiteral("PairedDevices")) { const InfallibleTArray<nsString>& pairedDeviceAddresses = value.get_ArrayOfnsString(); for (uint32_t i = 0; i < pairedDeviceAddresses.Length(); i++) { // Check whether or not the address exists in mDevices. if (mDevices.Contains(pairedDeviceAddresses[i])) { // If the paired device exists in mDevices, it would handle // 'PropertyChanged' signal in BluetoothDevice::Notify(). continue; } InfallibleTArray<BluetoothNamedValue> props; BT_APPEND_NAMED_VALUE(props, "Address", pairedDeviceAddresses[i]); BT_APPEND_NAMED_VALUE(props, "Paired", true); // Create paired device with 'address' and 'paired' attributes nsRefPtr<BluetoothDevice> pairedDevice = BluetoothDevice::Create(GetOwner(), BluetoothValue(props)); // Append to adapter's device array mDevices.AppendElement(pairedDevice); } // Retrieve device properties, result will be handled by device objects. GetPairedDeviceProperties(pairedDeviceAddresses); } else { BT_WARNING("Not handling adapter property: %s", NS_ConvertUTF16toUTF8(name).get()); } }
nsresult BluetoothServiceBluedroid::SetProperty(BluetoothObjectType aType, const BluetoothNamedValue& aValue, BluetoothReplyRunnable* aRunnable) { MOZ_ASSERT(NS_IsMainThread()); if (!IsReady()) { NS_NAMED_LITERAL_STRING(errorStr, "Bluetooth service is not ready yet!"); DispatchBluetoothReply(aRunnable, BluetoothValue(), errorStr); return NS_OK; } const nsString propName = aValue.name(); bt_property_t prop; bt_scan_mode_t scanMode; nsCString str; // For Bluedroid, it's necessary to check property name for SetProperty if (propName.EqualsLiteral("Name")) { prop.type = BT_PROPERTY_BDNAME; } else if (propName.EqualsLiteral("Discoverable")) { prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE; } else if (propName.EqualsLiteral("DiscoverableTimeout")) { prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT; } else { BT_LOGR("Warning: Property type is not supported yet, type: %d", prop.type); } if (aValue.value().type() == BluetoothValue::Tuint32_t) { // Set discoverable timeout prop.val = (void*)aValue.value().get_uint32_t(); } else if (aValue.value().type() == BluetoothValue::TnsString) { // Set name str = NS_ConvertUTF16toUTF8(aValue.value().get_nsString()); const char* name = str.get(); prop.val = (void*)name; prop.len = strlen(name); } else if (aValue.value().type() == BluetoothValue::Tbool) { scanMode = aValue.value().get_bool() ? BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE : BT_SCAN_MODE_CONNECTABLE; prop.val = (void*)&scanMode; prop.len = sizeof(scanMode); } else { BT_LOGR("SetProperty but the property cannot be recognized correctly."); return NS_OK; } sSetPropertyRunnableArray.AppendElement(aRunnable); int ret = sBtInterface->set_adapter_property(&prop); if (ret != BT_STATUS_SUCCESS) { ReplyStatusError(aRunnable, ret, NS_LITERAL_STRING("SetProperty")); } return NS_OK; }
nsresult PackPDU(const BluetoothNamedValue& aIn, BluetoothDaemonPDU& aPDU) { nsresult rv = PackPDU( PackConversion<nsString, BluetoothPropertyType>(aIn.name()), aPDU); if (NS_FAILED(rv)) { return rv; } if (aIn.value().type() == BluetoothValue::Tuint32_t) { // Set discoverable timeout rv = PackPDU(static_cast<uint16_t>(sizeof(uint32_t)), aIn.value().get_uint32_t(), aPDU); } else if (aIn.value().type() == BluetoothValue::TnsString) { // Set name const nsCString value = NS_ConvertUTF16toUTF8(aIn.value().get_nsString()); rv = PackPDU(PackConversion<size_t, uint16_t>(value.Length()), PackArray<uint8_t>( reinterpret_cast<const uint8_t*>(value.get()), value.Length()), aPDU); } else if (aIn.value().type() == BluetoothValue::Tbool) { // Set scan mode bool value = aIn.value().get_bool(); rv = PackPDU(static_cast<uint16_t>(sizeof(int32_t)), PackConversion<bool, BluetoothScanMode>(value), aPDU); } else { BT_LOGR("Invalid property value type"); rv = NS_ERROR_ILLEGAL_VALUE; } return rv; }
void BluetoothManager::SetPropertyByValue(const BluetoothNamedValue& aValue) { #ifdef DEBUG const nsString& name = aValue.name(); nsCString warningMsg; warningMsg.AssignLiteral("Not handling manager property: "); warningMsg.Append(NS_ConvertUTF16toUTF8(name)); NS_WARNING(warningMsg.get()); #endif }
nsresult NamedValueToProperty(const BluetoothNamedValue& aValue, BluetoothProperty& aProperty) { nsresult rv = StringToPropertyType(aValue.name(), aProperty.mType); if (NS_FAILED(rv)) { return rv; } switch (aProperty.mType) { case PROPERTY_BDNAME: if (aValue.value().type() != BluetoothValue::TnsString) { BT_LOGR("Bluetooth property value is not a string"); return NS_ERROR_ILLEGAL_VALUE; } // Set name aProperty.mString = aValue.value().get_nsString(); break; case PROPERTY_ADAPTER_SCAN_MODE: if (aValue.value().type() != BluetoothValue::Tbool) { BT_LOGR("Bluetooth property value is not a boolean"); return NS_ERROR_ILLEGAL_VALUE; } // Set scan mode if (aValue.value().get_bool()) { aProperty.mScanMode = SCAN_MODE_CONNECTABLE_DISCOVERABLE; } else { aProperty.mScanMode = SCAN_MODE_CONNECTABLE; } break; case PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: if (aValue.value().type() != BluetoothValue::Tuint32_t) { BT_LOGR("Bluetooth property value is not an unsigned integer"); return NS_ERROR_ILLEGAL_VALUE; } // Set discoverable timeout aProperty.mUint32 = aValue.value().get_uint32_t(); break; default: BT_LOGR("Invalid property value type"); return NS_ERROR_ILLEGAL_VALUE; } return NS_OK; }
void BluetoothAdapter::Notify(const BluetoothSignal& aData) { InfallibleTArray<BluetoothNamedValue> arr; if (aData.name().EqualsLiteral("DeviceFound")) { nsRefPtr<BluetoothDevice> device = BluetoothDevice::Create(GetOwner(), mPath, aData.value()); nsCOMPtr<nsIDOMEvent> event; NS_NewDOMBluetoothDeviceEvent(getter_AddRefs(event), nullptr, nullptr); nsCOMPtr<nsIDOMBluetoothDeviceEvent> e = do_QueryInterface(event); e->InitBluetoothDeviceEvent(NS_LITERAL_STRING("devicefound"), false, false, device); e->SetTrusted(true); bool dummy; DispatchEvent(event, &dummy); } else if (aData.name().EqualsLiteral("DeviceDisappeared")) { const nsAString& deviceAddress = aData.value().get_nsString(); nsCOMPtr<nsIDOMEvent> event; NS_NewDOMBluetoothDeviceAddressEvent(getter_AddRefs(event), nullptr, nullptr); nsCOMPtr<nsIDOMBluetoothDeviceAddressEvent> e = do_QueryInterface(event); e->InitBluetoothDeviceAddressEvent(NS_LITERAL_STRING("devicedisappeared"), false, false, deviceAddress); e->SetTrusted(true); bool dummy; DispatchEvent(event, &dummy); } else if (aData.name().EqualsLiteral("PropertyChanged")) { // Get BluetoothNamedValue, make sure array length is 1 arr = aData.value().get_ArrayOfBluetoothNamedValue(); NS_ASSERTION(arr.Length() == 1, "Got more than one property in a change message!"); NS_ASSERTION(arr[0].value().type() == BluetoothValue::TArrayOfBluetoothNamedValue, "PropertyChanged: Invalid value type"); BluetoothNamedValue v = arr[0]; SetPropertyByValue(v); nsRefPtr<BluetoothPropertyEvent> e = BluetoothPropertyEvent::Create(v.name()); e->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("propertychanged")); } else if (aData.name().EqualsLiteral("RequestConfirmation")) { arr = aData.value().get_ArrayOfBluetoothNamedValue(); NS_ASSERTION(arr.Length() == 2, "RequestConfirmation: Wrong length of parameters"); NS_ASSERTION(arr[0].value().type() == BluetoothValue::TnsString, "RequestConfirmation: Invalid value type"); NS_ASSERTION(arr[1].value().type() == BluetoothValue::Tuint32_t, "RequestConfirmation: Invalid value type"); nsCOMPtr<nsIDOMEvent> event; NS_NewDOMBluetoothPairingEvent(getter_AddRefs(event), nullptr, nullptr); nsCOMPtr<nsIDOMBluetoothPairingEvent> e = do_QueryInterface(event); e->InitBluetoothPairingEvent(NS_LITERAL_STRING("requestconfirmation"), false, false, arr[0].value().get_nsString(), arr[1].value().get_uint32_t()); e->SetTrusted(true); bool dummy; DispatchEvent(event, &dummy); } else if (aData.name().EqualsLiteral("RequestPinCode")) { arr = aData.value().get_ArrayOfBluetoothNamedValue(); NS_ASSERTION(arr.Length() == 1, "RequestPinCode: Wrong length of parameters"); NS_ASSERTION(arr[0].value().type() == BluetoothValue::TnsString, "RequestPinCode: Invalid value type"); nsCOMPtr<nsIDOMEvent> event; NS_NewDOMBluetoothDeviceAddressEvent(getter_AddRefs(event), nullptr, nullptr); nsCOMPtr<nsIDOMBluetoothDeviceAddressEvent> e = do_QueryInterface(event); e->InitBluetoothDeviceAddressEvent(NS_LITERAL_STRING("requestpincode"), false, false, arr[0].value().get_nsString()); e->SetTrusted(true); bool dummy; DispatchEvent(event, &dummy); } else if (aData.name().EqualsLiteral("RequestPasskey")) { arr = aData.value().get_ArrayOfBluetoothNamedValue(); NS_ASSERTION(arr.Length() == 1, "RequestPasskey: Wrong length of parameters"); NS_ASSERTION(arr[0].value().type() == BluetoothValue::TnsString, "RequestPasskey: Invalid value type"); nsCOMPtr<nsIDOMEvent> event; NS_NewDOMBluetoothDeviceAddressEvent(getter_AddRefs(event), nullptr, nullptr); nsCOMPtr<nsIDOMBluetoothDeviceAddressEvent> e = do_QueryInterface(event); e->InitBluetoothDeviceAddressEvent(NS_LITERAL_STRING("requestpasskey"), false, false, arr[0].value().get_nsString()); e->SetTrusted(true); bool dummy; DispatchEvent(event, &dummy); } else if (aData.name().EqualsLiteral("Authorize")) { arr = aData.value().get_ArrayOfBluetoothNamedValue(); NS_ASSERTION(arr.Length() == 2, "Authorize: Wrong length of parameters"); NS_ASSERTION(arr[0].value().type() == BluetoothValue::TnsString, "Authorize: Invalid value type"); NS_ASSERTION(arr[1].value().type() == BluetoothValue::TnsString, "Authorize: Invalid value type"); nsCOMPtr<nsIDOMEvent> event; NS_NewDOMBluetoothAuthorizeEvent(getter_AddRefs(event), nullptr, nullptr); nsCOMPtr<nsIDOMBluetoothAuthorizeEvent> e = do_QueryInterface(event); e->InitBluetoothAuthorizeEvent(NS_LITERAL_STRING("authorize"), false, false, arr[0].value().get_nsString(), arr[1].value().get_nsString()); e->SetTrusted(true); bool dummy; DispatchEvent(event, &dummy); } else if (aData.name().EqualsLiteral("Cancel")) { nsCOMPtr<nsIDOMEvent> event; NS_NewDOMBluetoothDeviceAddressEvent(getter_AddRefs(event), nullptr, nullptr); nsCOMPtr<nsIDOMBluetoothDeviceAddressEvent> e = do_QueryInterface(event); // Just send a null nsString, won't be used e->InitBluetoothDeviceAddressEvent(NS_LITERAL_STRING("cancel"), false, false, EmptyString()); e->SetTrusted(true); bool dummy; DispatchEvent(event, &dummy); } else { #ifdef DEBUG nsCString warningMsg; warningMsg.AssignLiteral("Not handling adapter signal: "); warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name())); NS_WARNING(warningMsg.get()); #endif } }
void BluetoothAdapter::SetPropertyByValue(const BluetoothNamedValue& aValue) { const nsString& name = aValue.name(); const BluetoothValue& value = aValue.value(); if (name.EqualsLiteral("Name")) { mName = value.get_nsString(); } else if (name.EqualsLiteral("Address")) { mAddress = value.get_nsString(); } else if (name.EqualsLiteral("Path")) { mPath = value.get_nsString(); } else if (name.EqualsLiteral("Discoverable")) { mDiscoverable = value.get_bool(); } else if (name.EqualsLiteral("Discovering")) { mDiscovering = value.get_bool(); } else if (name.EqualsLiteral("Pairable")) { mPairable = value.get_bool(); } else if (name.EqualsLiteral("Powered")) { mPowered = value.get_bool(); } else if (name.EqualsLiteral("PairableTimeout")) { mPairableTimeout = value.get_uint32_t(); } else if (name.EqualsLiteral("DiscoverableTimeout")) { mDiscoverableTimeout = value.get_uint32_t(); } else if (name.EqualsLiteral("Class")) { mClass = value.get_uint32_t(); } else if (name.EqualsLiteral("UUIDs")) { mUuids = value.get_ArrayOfnsString(); AutoJSAPI jsapi; if (!jsapi.Init(GetOwner())) { BT_WARNING("Failed to initialise AutoJSAPI!"); return; } JSContext* cx = jsapi.cx(); JS::Rooted<JSObject*> uuids(cx); if (NS_FAILED(nsTArrayToJSArray(cx, mUuids, &uuids))) { BT_WARNING("Cannot set JS UUIDs object!"); return; } mJsUuids = uuids; Root(); } else if (name.EqualsLiteral("Devices")) { mDeviceAddresses = value.get_ArrayOfnsString(); AutoJSAPI jsapi; if (!jsapi.Init(GetOwner())) { BT_WARNING("Failed to initialise AutoJSAPI!"); return; } JSContext* cx = jsapi.cx(); JS::Rooted<JSObject*> deviceAddresses(cx); if (NS_FAILED(nsTArrayToJSArray(cx, mDeviceAddresses, &deviceAddresses))) { BT_WARNING("Cannot set JS Devices object!"); return; } mJsDeviceAddresses = deviceAddresses; Root(); } else { #ifdef DEBUG nsCString warningMsg; warningMsg.AssignLiteral("Not handling adapter property: "); warningMsg.Append(NS_ConvertUTF16toUTF8(name)); BT_WARNING(warningMsg.get()); #endif } }
void BluetoothAdapter::SetPropertyByValue(const BluetoothNamedValue& aValue) { const nsString& name = aValue.name(); const BluetoothValue& value = aValue.value(); if (name.EqualsLiteral("Name")) { mName = value.get_nsString(); } else if (name.EqualsLiteral("Address")) { mAddress = value.get_nsString(); } else if (name.EqualsLiteral("Path")) { mPath = value.get_nsString(); } else if (name.EqualsLiteral("Discoverable")) { mDiscoverable = value.get_bool(); } else if (name.EqualsLiteral("Discovering")) { mDiscovering = value.get_bool(); } else if (name.EqualsLiteral("Pairable")) { mPairable = value.get_bool(); } else if (name.EqualsLiteral("Powered")) { mPowered = value.get_bool(); } else if (name.EqualsLiteral("PairableTimeout")) { mPairableTimeout = value.get_uint32_t(); } else if (name.EqualsLiteral("DiscoverableTimeout")) { mDiscoverableTimeout = value.get_uint32_t(); } else if (name.EqualsLiteral("Class")) { mClass = value.get_uint32_t(); } else if (name.EqualsLiteral("UUIDs")) { mUuids = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS_VOID(rv); AutoPushJSContext cx(sc->GetNativeContext()); JS::Rooted<JSObject*> uuids(cx); if (NS_FAILED(nsTArrayToJSArray(cx, mUuids, uuids.address()))) { NS_WARNING("Cannot set JS UUIDs object!"); return; } mJsUuids = uuids; Root(); } else if (name.EqualsLiteral("Devices")) { mDeviceAddresses = value.get_ArrayOfnsString(); uint32_t length = mDeviceAddresses.Length(); for (int i = 0; i < length; i++) { mDeviceAddresses[i] = GetAddressFromObjectPath(mDeviceAddresses[i]); } nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS_VOID(rv); AutoPushJSContext cx(sc->GetNativeContext()); JS::Rooted<JSObject*> deviceAddresses(cx); if (NS_FAILED(nsTArrayToJSArray(cx, mDeviceAddresses, deviceAddresses.address()))) { NS_WARNING("Cannot set JS Devices object!"); return; } mJsDeviceAddresses = deviceAddresses; Root(); } else { #ifdef DEBUG nsCString warningMsg; warningMsg.AssignLiteral("Not handling adapter property: "); warningMsg.Append(NS_ConvertUTF16toUTF8(name)); NS_WARNING(warningMsg.get()); #endif } }