Files
palemoon27/dom/bluetooth/common/BluetoothUtils.cpp
T
roytam1 fe0509a62e import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 904479 - Added createPromiseWithId() that returns id of resolver r=kanru,nsm (2ac672d882)
- Bug 1166580 - Disable mozHasPendingMessage tests on non-browser platform. r=me (03c689964b)
- Bug 1162281 - Invalid system message handler in an App Manifest can break the entire system. r=fabrice (e192a95f9c)
- Bug 1198988 - Turn off some useless dump() calls r=ferjm (34fc83b236)
- Bug 1164498: Remove |DispatchBluetoothReply|, r=btian (6143335efa)
- Bug 1001757 - Add ability to store core apps outside of profile on desktop b2g; r=fabrice (f6b605e7aa)
- Bug 1155245 - Set the app status correctly for hosted certified apps in developer mode. r=fabrice (131178b80e)
- Bug 1179052 - Add some raptor markers to b2g gecko startup r=gwagner (222256fad8)
- Bug 1163904 - handle -url command line argument. r=fabrice (ee61af1ff9)
- Bug 1167275 - JS error in shell.js handleCmdLine() r=me (32e75c604f)
- Bug 1167197 - Fix GMPProvider on Android r=cpearce Bug 1181209 - Make changes to Gecko needed for b2gdroid to boot. r=fabrice (b35d3a372f)
- Bug 1158544 - Remove FTPChannelChild::mWasOpened and make the base class mWasOpened protected; r=mcmanus (9111e1bc00)
- Bug 1171716 - Part 2: Use NS_ReleaseOnMainThread in nsBaseChannel. r=froydnj (f138124f14)
- partial of Bug 1177175 - Add a UITour target inside the TP panel. (603cc719b3)
- Bug 1175545 - Dont process alt-svc on 421 r=hurley (ad0f2f6e91)
- Bug 1191291 - convert nsHttpChannel::RetargetDeliveryTo warning to log r=michal.novotny (b9c6003df8)
- Bug 1182487 - Don't try to write to HTTP cache entry in nsHttpChannel when entry is open for reading only. r=michal (b36d7014a0)
- Bug 1173069 - Don't accumulate the cache hit telemetry for intercepted channels; r=mayhemer,jdm (aaed79183d)
- Bug 1208755 HttpBaseChannel::ShouldIntercept() should not assume every channel has a LoadInfo. r=ckerschb (d55be94901)
- Bug 1201229 - Return an empty string for a header when an error occurs; r=dragana (256d0462c8)
- Bug 1048048 - add preload content policy types - web platform test updates (r=dveditz) (baa1004dd6)
- Bug 1048048 - add preload content policy types - csp changes (r=dveditz) (17914dadba)
- Bug 1048048 - add preload content policy types for stylesheets (r=cam) (29af13263a)
- Bug 1048048 - add preload content policy types (r=ehsan) (f58a32d51b)
- Bug 1201747 - Don't inspect the subject principal in StorageAllowedForPrincipal. r=mystor (4f2c100882)
- Bug 1176829 - Remove custom elements base element queue. r=smaug (03a520c13d)
- Bug 1176829 follow-up, finish removing unused member to fix bustage. CLOSED TREE (29c6150af8)
- Bug 1179909: Build fix. r=me CLOSED TREE (40e3bdb971)
- Bug 1188932 - Allow the User-Agent header to be explicitly set by requests, r=bkelly, r=jgraham (37aacbd37d)
2022-04-01 23:06:55 +08:00

352 lines
10 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "BluetoothUtils.h"
#include "BluetoothReplyRunnable.h"
#include "BluetoothService.h"
#include "jsapi.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
#include "nsContentUtils.h"
#include "nsISystemMessagesInternal.h"
#include "nsIUUIDGenerator.h"
#include "nsServiceManagerUtils.h"
#include "nsXULAppAPI.h"
BEGIN_BLUETOOTH_NAMESPACE
void
UuidToString(const BluetoothUuid& aUuid, nsAString& aString)
{
char uuidStr[37];
uint32_t uuid0, uuid4;
uint16_t uuid1, uuid2, uuid3, uuid5;
memcpy(&uuid0, &aUuid.mUuid[0], sizeof(uint32_t));
memcpy(&uuid1, &aUuid.mUuid[4], sizeof(uint16_t));
memcpy(&uuid2, &aUuid.mUuid[6], sizeof(uint16_t));
memcpy(&uuid3, &aUuid.mUuid[8], sizeof(uint16_t));
memcpy(&uuid4, &aUuid.mUuid[10], sizeof(uint32_t));
memcpy(&uuid5, &aUuid.mUuid[14], sizeof(uint16_t));
snprintf(uuidStr, sizeof(uuidStr),
"%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
ntohl(uuid0), ntohs(uuid1),
ntohs(uuid2), ntohs(uuid3),
ntohl(uuid4), ntohs(uuid5));
aString.Truncate();
aString.AssignLiteral(uuidStr);
}
void
StringToUuid(const char* aString, BluetoothUuid& aUuid)
{
uint32_t uuid0, uuid4;
uint16_t uuid1, uuid2, uuid3, uuid5;
sscanf(aString, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
&uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5);
uuid0 = htonl(uuid0);
uuid1 = htons(uuid1);
uuid2 = htons(uuid2);
uuid3 = htons(uuid3);
uuid4 = htonl(uuid4);
uuid5 = htons(uuid5);
memcpy(&aUuid.mUuid[0], &uuid0, sizeof(uint32_t));
memcpy(&aUuid.mUuid[4], &uuid1, sizeof(uint16_t));
memcpy(&aUuid.mUuid[6], &uuid2, sizeof(uint16_t));
memcpy(&aUuid.mUuid[8], &uuid3, sizeof(uint16_t));
memcpy(&aUuid.mUuid[10], &uuid4, sizeof(uint32_t));
memcpy(&aUuid.mUuid[14], &uuid5, sizeof(uint16_t));
}
void
StringToUuid(const nsAString& aString, BluetoothUuid& aUuid)
{
StringToUuid(NS_ConvertUTF16toUTF8(aString).get(), aUuid);
}
void
GenerateUuid(nsAString &aUuidString)
{
nsresult rv;
nsCOMPtr<nsIUUIDGenerator> uuidGenerator =
do_GetService("@mozilla.org/uuid-generator;1", &rv);
NS_ENSURE_SUCCESS_VOID(rv);
nsID uuid;
rv = uuidGenerator->GenerateUUIDInPlace(&uuid);
NS_ENSURE_SUCCESS_VOID(rv);
// Build a string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} format
char uuidBuffer[NSID_LENGTH];
uuid.ToProvidedString(uuidBuffer);
NS_ConvertASCIItoUTF16 uuidString(uuidBuffer);
// Remove {} and the null terminator
aUuidString.Assign(Substring(uuidString, 1, NSID_LENGTH - 3));
}
void
GeneratePathFromGattId(const BluetoothGattId& aId,
nsAString& aPath)
{
nsString uuidStr;
UuidToString(aId.mUuid, uuidStr);
aPath.Assign(uuidStr);
aPath.AppendLiteral("_");
aPath.AppendInt(aId.mInstanceId);
}
void
RegisterBluetoothSignalHandler(const nsAString& aPath,
BluetoothSignalObserver* aHandler)
{
MOZ_ASSERT(!aPath.IsEmpty());
MOZ_ASSERT(aHandler);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
bs->RegisterBluetoothSignalHandler(aPath, aHandler);
aHandler->SetSignalRegistered(true);
}
void
UnregisterBluetoothSignalHandler(const nsAString& aPath,
BluetoothSignalObserver* aHandler)
{
MOZ_ASSERT(!aPath.IsEmpty());
MOZ_ASSERT(aHandler);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
bs->UnregisterBluetoothSignalHandler(aPath, aHandler);
aHandler->SetSignalRegistered(false);
}
/**
* |SetJsObject| is an internal function used by |BroadcastSystemMessage| only
*/
static bool
SetJsObject(JSContext* aContext,
const BluetoothValue& aValue,
JS::Handle<JSObject*> aObj)
{
MOZ_ASSERT(aContext && aObj);
if (aValue.type() != BluetoothValue::TArrayOfBluetoothNamedValue) {
BT_WARNING("SetJsObject: Invalid parameter type");
return false;
}
const nsTArray<BluetoothNamedValue>& arr =
aValue.get_ArrayOfBluetoothNamedValue();
for (uint32_t i = 0; i < arr.Length(); i++) {
JS::Rooted<JS::Value> val(aContext);
const BluetoothValue& v = arr[i].value();
switch(v.type()) {
case BluetoothValue::TnsString: {
JSString* jsData = JS_NewUCStringCopyN(aContext,
v.get_nsString().BeginReading(),
v.get_nsString().Length());
NS_ENSURE_TRUE(jsData, false);
val.setString(jsData);
break;
}
case BluetoothValue::Tuint32_t:
val.setInt32(v.get_uint32_t());
break;
case BluetoothValue::Tbool:
val.setBoolean(v.get_bool());
break;
default:
BT_WARNING("SetJsObject: Parameter is not handled");
break;
}
if (!JS_SetProperty(aContext, aObj,
NS_ConvertUTF16toUTF8(arr[i].name()).get(),
val)) {
BT_WARNING("Failed to set property");
return false;
}
}
return true;
}
bool
BroadcastSystemMessage(const nsAString& aType,
const BluetoothValue& aData)
{
mozilla::AutoSafeJSContext cx;
MOZ_ASSERT(!::JS_IsExceptionPending(cx),
"Shouldn't get here when an exception is pending!");
nsCOMPtr<nsISystemMessagesInternal> systemMessenger =
do_GetService("@mozilla.org/system-message-internal;1");
NS_ENSURE_TRUE(systemMessenger, false);
JS::Rooted<JS::Value> value(cx);
if (aData.type() == BluetoothValue::TnsString) {
JSString* jsData = JS_NewUCStringCopyN(cx,
aData.get_nsString().BeginReading(),
aData.get_nsString().Length());
value.setString(jsData);
} else if (aData.type() == BluetoothValue::TArrayOfBluetoothNamedValue) {
JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
if (!obj) {
BT_WARNING("Failed to new JSObject for system message!");
return false;
}
if (!SetJsObject(cx, aData, obj)) {
BT_WARNING("Failed to set properties of system message!");
return false;
}
value = JS::ObjectValue(*obj);
} else {
BT_WARNING("Not support the unknown BluetoothValue type");
return false;
}
nsCOMPtr<nsISupports> promise;
systemMessenger->BroadcastMessage(aType, value,
JS::UndefinedHandleValue,
getter_AddRefs(promise));
return true;
}
bool
BroadcastSystemMessage(const nsAString& aType,
const InfallibleTArray<BluetoothNamedValue>& aData)
{
mozilla::AutoSafeJSContext cx;
MOZ_ASSERT(!::JS_IsExceptionPending(cx),
"Shouldn't get here when an exception is pending!");
JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
if (!obj) {
BT_WARNING("Failed to new JSObject for system message!");
return false;
}
if (!SetJsObject(cx, aData, obj)) {
BT_WARNING("Failed to set properties of system message!");
return false;
}
nsCOMPtr<nsISystemMessagesInternal> systemMessenger =
do_GetService("@mozilla.org/system-message-internal;1");
NS_ENSURE_TRUE(systemMessenger, false);
JS::Rooted<JS::Value> value(cx, JS::ObjectValue(*obj));
nsCOMPtr<nsISupports> promise;
systemMessenger->BroadcastMessage(aType, value,
JS::UndefinedHandleValue,
getter_AddRefs(promise));
return true;
}
void
DispatchReplySuccess(BluetoothReplyRunnable* aRunnable)
{
DispatchReplySuccess(aRunnable, BluetoothValue(true));
}
void
DispatchReplySuccess(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue)
{
MOZ_ASSERT(aRunnable);
MOZ_ASSERT(aValue.type() != BluetoothValue::T__None);
BluetoothReply* reply = new BluetoothReply(BluetoothReplySuccess(aValue));
aRunnable->SetReply(reply); // runnable will delete reply after Run()
NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(aRunnable)));
}
void
DispatchReplyError(BluetoothReplyRunnable* aRunnable,
const nsAString& aErrorStr)
{
MOZ_ASSERT(aRunnable);
MOZ_ASSERT(!aErrorStr.IsEmpty());
// Reply will be deleted by the runnable after running on main thread
BluetoothReply* reply =
new BluetoothReply(BluetoothReplyError(STATUS_FAIL, nsString(aErrorStr)));
aRunnable->SetReply(reply);
NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(aRunnable)));
}
void
DispatchReplyError(BluetoothReplyRunnable* aRunnable,
const enum BluetoothStatus aStatus)
{
MOZ_ASSERT(aRunnable);
MOZ_ASSERT(aStatus != STATUS_SUCCESS);
// Reply will be deleted by the runnable after running on main thread
BluetoothReply* reply =
new BluetoothReply(BluetoothReplyError(aStatus, EmptyString()));
aRunnable->SetReply(reply);
NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(aRunnable)));
}
void
DispatchStatusChangedEvent(const nsAString& aType,
const nsAString& aAddress,
bool aStatus)
{
MOZ_ASSERT(NS_IsMainThread());
InfallibleTArray<BluetoothNamedValue> data;
AppendNamedValue(data, "address", nsString(aAddress));
AppendNamedValue(data, "status", aStatus);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
bs->DistributeSignal(aType, NS_LITERAL_STRING(KEY_ADAPTER), data);
}
void
AppendNamedValue(InfallibleTArray<BluetoothNamedValue>& aArray,
const char* aName, const BluetoothValue& aValue)
{
nsString name;
name.AssignASCII(aName);
aArray.AppendElement(BluetoothNamedValue(name, aValue));
}
void
InsertNamedValue(InfallibleTArray<BluetoothNamedValue>& aArray,
uint8_t aIndex, const char* aName,
const BluetoothValue& aValue)
{
nsString name;
name.AssignASCII(aName);
aArray.InsertElementAt(aIndex, BluetoothNamedValue(name, aValue));
}
END_BLUETOOTH_NAMESPACE