Files
palemoon27/dom/bluetooth/BluetoothUtils.cpp
T
roytam1 3909bb5fb8 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1110446 P2 Cleanup stale caches/bodies if last session didn't shutdown cleanly. r=ehsan (7925cf5fa)
- Bug 1110446 P3 Add a test that forces a Cache object to be orphaned and reclaimed. r=ehsan (c61409240)
- Bug 1110446 P4 Add a test that orphanes Cache API body files. r=ehsan (97e0a6f73)
- Bug 1157670 - Fixing an incorrect assertion in QuotaManager.cpp leads to an assertion failure; r=bent (0a19eac66)
- Bug 1165119 Remove corrupt morgue directories polluting nightly profiles. r=janv (d148170d8)
- Bug 1165119 - Follow-up to address review feedback accidentally left out of last push. r=me (f7ef96873)
- Fixup to make bug 1165119 ride the trains properly. r=trivial,DONTBUILD (87d186da4)
- Bug 1162624 - Add support for restoring corrupted or missing metadata files; r=bent (57e4341e6)
- Bug 1174113 - QuotaManager: Origin initialization fails on moz-safe-about+++home; r=bent (b7673128c)
- Bug 1142694 - QuotaManager default/temporary initialization fails on some profiles; r=bent (29a286137)
- Bug 1166871 - Always force a repaint before handling a wheel event so that we don't untransform it into some other scrollframe. r=botond (28e56646d)
- Don't vertically scroll APZCs that have less than one pixel of vertical scroll range. (bug 1154134, r=kats) (1bac9c054)
- Bug 1166871 - Add a test. r=botond (45d398bb6)
- Bug 1164557 - Do not start an overscroll animation if one is already running. r=kats (287a27910)
- Bug 1163832 - Add an API to flush pending APZ repaint requests and dispatch a notification upon completion. r=botond (8b3f9e06f)
- Bug 858680 - Part 1: Perform incremental_vacuum on open databases while idle, r=janv. (715f77ad6)
- Bug 858680 - Part 2: Add idle notifications to QuotaClient, r=janv. (9f245b1bb)
- Bug 1135166 - Initialize Telemetry histogram id cache early to avoid races. r=froydnj,vladan (f0bd8278c)
- Bug 1162176, Part 1. r=mak. (f92ba4061)
- Bug 1162176, Part 2. r=janv. (f313e1cf3)
- Bug 1155634 - Move ConnectionPool creation closer to where we actually use it and at a point guaranteed to be after QuotaManager has been started. r=khuey relanding CLOSED TREE (ce489e8f4)
- Bug 1155652 - Fix two incorrect assertions r=janv (2417d91ed)
- Bug 1156063 - Intermittent application crashed [@ mozilla::dom::indexedDB::::ConnectionPool::Start] in various tests. r=janv (b1126ac71)
- Bug 1157029 - More changes to bulletproof shutdown of failed connections, r=janv. (93a425abb)
- Bug 858680 - Part 4: Perform maintenance on databases while idle, r=janv. (017d536fe)
- Bug 1130775 - Convert synchronized ops and storage registration into unified directory locks; r=bent (300f635f7)
- Bug 1130775 followup: Add missing 'override' keyword to SendResults() methods in QuotaManager.cpp. rs=ehsan (397338f5b)
- Bug 1170021 - Part 1: Merge QuotaManager with QuotaObject; r=bent (168264350)
- Bug 1170021 - Part 2: Move DirectoryLock out of QuotaManager class; r=bent (278964f88)
- pointer style (99453953c)
- Bug 1171931 - Refactor duplicated code using XRE_IsParent/ContentProcess. r=froydnj (6d1ddbff1)
2021-02-23 12:01:40 +08:00

381 lines
11 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
ReversedUuidToString(const BluetoothUuid& aUuid, nsAString& aString)
{
BluetoothUuid uuid;
for (uint8_t i = 0; i < 16; i++) {
uuid.mUuid[i] = aUuid.mUuid[15 - i];
}
UuidToString(uuid, aString);
}
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
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,
nsAString& aUuidStr)
{
ReversedUuidToString(aId.mUuid, aUuidStr);
aPath.Assign(aUuidStr);
aPath.AppendLiteral("_");
aPath.AppendInt(aId.mInstanceId);
}
void
GeneratePathFromGattId(const BluetoothGattId& aId,
nsAString& aPath)
{
nsString uuidStr;
GeneratePathFromGattId(aId, aPath, uuidStr);
}
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 = INT_TO_JSVAL(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;
}
systemMessenger->BroadcastMessage(aType, value,
JS::UndefinedHandleValue);
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));
systemMessenger->BroadcastMessage(aType, value,
JS::UndefinedHandleValue);
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
#if MOZ_B2G_BT_API_V2
BluetoothReply* reply =
new BluetoothReply(BluetoothReplyError(STATUS_FAIL, nsString(aErrorStr)));
#else
BluetoothReply* reply =
new BluetoothReply(BluetoothReplyError(nsString(aErrorStr)));
#endif
aRunnable->SetReply(reply); // runnable will delete reply after Run()
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
#if MOZ_B2G_BT_API_V2
BluetoothReply* reply =
new BluetoothReply(BluetoothReplyError(aStatus, EmptyString()));
#else
BluetoothReply* reply =
new BluetoothReply(
BluetoothReplyError(NS_LITERAL_STRING("Internal error")));
#endif
aRunnable->SetReply(reply); // runnable will delete reply after Run()
NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(aRunnable)));
}
#if MOZ_B2G_BT_API_V2
// TODO: remove with bluetooth1
#else
void
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue,
const nsAString& aErrorStr)
{
// Reply will be deleted by the runnable after running on main thread
BluetoothReply* reply;
if (!aErrorStr.IsEmpty()) {
nsString err(aErrorStr);
reply = new BluetoothReply(BluetoothReplyError(err));
} else {
MOZ_ASSERT(aValue.type() != BluetoothValue::T__None);
reply = new BluetoothReply(BluetoothReplySuccess(aValue));
}
aRunnable->SetReply(reply);
if (NS_FAILED(NS_DispatchToMainThread(aRunnable))) {
BT_WARNING("Failed to dispatch to main thread!");
}
}
#endif
void
DispatchStatusChangedEvent(const nsAString& aType,
const nsAString& aAddress,
bool aStatus)
{
MOZ_ASSERT(NS_IsMainThread());
InfallibleTArray<BluetoothNamedValue> data;
BT_APPEND_NAMED_VALUE(data, "address", nsString(aAddress));
BT_APPEND_NAMED_VALUE(data, "status", aStatus);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
#ifdef MOZ_B2G_BT_API_V2
bs->DistributeSignal(aType, NS_LITERAL_STRING(KEY_ADAPTER), data);
#else
BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data);
bs->DistributeSignal(signal);
#endif
}
END_BLUETOOTH_NAMESPACE