Files
palemoon27/dom/bluetooth/common/webapi/BluetoothManager.cpp
T
roytam1 19b122873d import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1240985 - Always enqueue OnMaybeDequeueOne task when receiving a message (r=dvander) (a5056d5942)
- Bug 1240985 - Null out mRecvd if message it corresponds to is cancelled (r=dvander) (3eb16e50b4)
- Bug 1245649: Merge browser and toolkit eslint rule settings. r=felipe (fb8837edb9)
- Bug 1189799 - Make sure that about:performance displays each add-on only once (more tests);r=felipe (67cc74db67)
- Bug 1230471 - Basic eslint fixes in places. r=mconley (1460c46edc)
- Bug 1142734: Allow unloading the loader sandbox module. r=jsantell (052f483a6d)
- Bug 1047595 - make picking colors work in HCM / when author colors are disabled, r=jaws (728163434b)
- Bug 1244647 fix typeof check, r=mikedeboer (72da15da21)
- Bug 1229240 - make applyConstraints() reject instead of crash on non-gUM tracks. r=jesup (93d2abf43a)
- Bug 1239893 - Add gonk SidebandStream handling to DOMHwMediaStream r=roc (86739bc7a4)
- Bug 1189162 - Clamp gamepad.timestamp and VideoPlaybackQuality.creationTime. r=bz (daaa3cc0db)
- Bug 1232348 - adjust nested-frame checking condition. r=baku. (e4d1930bef)
- Bug 1166556 - Don't start geolocation provider if the cached position is being used. r=jdm (3ad2c5dfa9)
- Bug 858827 - [Geolocation] Add a handling for getCurrentLocation when timeout is zero. r=jdm (63dd4ccaf3)
- Bug 1216148 - Handle how geolocation acts when the app's visibility changes. r=kchen. (dbeb9ef51c)
- Bug 1230209 - Add more telemetry for Geolocation usage f=bsmedberg r=tanvi,rbarnes,jdm (c32b195d25)
- Bug 1238825: Add "dom.bluetooth.webbluetooth.enabled" preference for WebBluetooth API development. r=btian, r=bz (3298be0b87)
- Bug 1223722: Transfer Bluetooth addresses in |BluetoothValue|, r=brsun (1cc507823b)
- Bug 1223722: Transfer Bluetooth remote names in |BluetoothValue|, r=brsun (73a008b991)
- Bug 789315, notify MutationObservers before running <script>, r=khuey (d2df797c54)
- Bug 1214495 - Move CallerSubsumes checks in nsLocation into the WebIDL entry points. r=bz (69a2086836)
- Bug 1239509: Move VisibilityChangeListener out of nsContentPermissionHelper's header. r=wchen (bb2e33a40c)
- Bug 1183845 - Only send activation event if document is non-null. r=bz (4b81c3295d)
- Bug 1208789, clear starting node before traversing backwards by document, r=smaug (94a6cdd891)
- Bug 1240908 - Make the window reference in nsContentPermissionRequester weak. r=smaug (b5982ecb9e)
- Bug 1234735 - Remove redundant spaces in nsGlobalWindow. r=baku (5f55b37486)
- Bug 1180525. Run callbacks whose window is no longer current as long as its document is the active document in the browsing context. r=bholley (02e6d97a7b)
2023-11-08 16:38:54 +08:00

295 lines
8.5 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 "base/basictypes.h"
#include "BluetoothReplyRunnable.h"
#include "BluetoothService.h"
#include "BluetoothUtils.h"
#include "mozilla/dom/bluetooth/BluetoothAdapter.h"
#include "mozilla/dom/bluetooth/BluetoothManager.h"
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
#include "mozilla/dom/BluetoothManagerBinding.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "nsContentUtils.h"
#include "nsDOMClassInfo.h"
#include "nsIPermissionManager.h"
#include "nsThreadUtils.h"
using namespace mozilla;
USING_BLUETOOTH_NAMESPACE
NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothManager)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothManager,
DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mAdapters)
/**
* Unregister the bluetooth signal handler after unlinked.
*
* This is needed to avoid ending up with exposing a deleted object to JS or
* accessing deleted objects while receiving signals from parent process
* after unlinked. Please see Bug 1138267 for detail informations.
*/
UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), tmp);
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothManager,
DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAdapters)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothManager)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(BluetoothManager, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(BluetoothManager, DOMEventTargetHelper)
class GetAdaptersTask : public BluetoothReplyRunnable
{
public:
GetAdaptersTask(BluetoothManager* aManager)
: BluetoothReplyRunnable(nullptr)
, mManager(aManager)
{ }
bool
ParseSuccessfulReply(JS::MutableHandle<JS::Value> aValue)
{
/**
* Unwrap BluetoothReply.BluetoothReplySuccess.BluetoothValue =
* BluetoothNamedValue[]
* |
* |__ BluetoothNamedValue =
* | {"Adapter", BluetoothValue = BluetoothNamedValue[]}
* |
* |__ BluetoothNamedValue =
* | {"Adapter", BluetoothValue = BluetoothNamedValue[]}
* ...
*/
// Extract the array of all adapters' properties
const BluetoothValue& adaptersProperties =
mReply->get_BluetoothReplySuccess().value();
NS_ENSURE_TRUE(adaptersProperties.type() ==
BluetoothValue::TArrayOfBluetoothNamedValue, false);
const InfallibleTArray<BluetoothNamedValue>& adaptersPropertiesArray =
adaptersProperties.get_ArrayOfBluetoothNamedValue();
// Append a BluetoothAdapter into adapters array for each properties array
uint32_t numAdapters = adaptersPropertiesArray.Length();
for (uint32_t i = 0; i < numAdapters; i++) {
MOZ_ASSERT(adaptersPropertiesArray[i].name().EqualsLiteral("Adapter"));
const BluetoothValue& properties = adaptersPropertiesArray[i].value();
mManager->AppendAdapter(properties);
}
aValue.setUndefined();
return true;
}
virtual void
ReleaseMembers() override
{
BluetoothReplyRunnable::ReleaseMembers();
mManager = nullptr;
}
private:
RefPtr<BluetoothManager> mManager;
};
BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow)
: DOMEventTargetHelper(aWindow)
, mDefaultAdapterIndex(-1)
{
MOZ_ASSERT(aWindow);
RegisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
// Query adapters list from bluetooth backend
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
RefPtr<BluetoothReplyRunnable> result = new GetAdaptersTask(this);
NS_ENSURE_SUCCESS_VOID(bs->GetAdaptersInternal(result));
}
BluetoothManager::~BluetoothManager()
{
UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
}
void
BluetoothManager::DisconnectFromOwner()
{
DOMEventTargetHelper::DisconnectFromOwner();
UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
}
BluetoothAdapter*
BluetoothManager::GetDefaultAdapter()
{
if (!DefaultAdapterExists()) {
return nullptr;
}
return mAdapters[mDefaultAdapterIndex];
}
void
BluetoothManager::AppendAdapter(const BluetoothValue& aValue)
{
MOZ_ASSERT(aValue.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
// Create a new BluetoothAdapter and append it to adapters array
const InfallibleTArray<BluetoothNamedValue>& values =
aValue.get_ArrayOfBluetoothNamedValue();
RefPtr<BluetoothAdapter> adapter =
BluetoothAdapter::Create(GetOwner(), values);
mAdapters.AppendElement(adapter);
// Set this adapter as default adapter if no adapter exists
if (!DefaultAdapterExists()) {
MOZ_ASSERT(mAdapters.Length() == 1);
ReselectDefaultAdapter();
}
}
void
BluetoothManager::GetAdapters(nsTArray<RefPtr<BluetoothAdapter> >& aAdapters)
{
aAdapters = mAdapters;
}
// static
already_AddRefed<BluetoothManager>
BluetoothManager::Create(nsPIDOMWindow* aWindow)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aWindow);
RefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
return manager.forget();
}
void
BluetoothManager::HandleAdapterAdded(const BluetoothValue& aValue)
{
MOZ_ASSERT(aValue.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
AppendAdapter(aValue);
// Notify application of added adapter
BluetoothAdapterEventInit init;
init.mAdapter = mAdapters.LastElement();
DispatchAdapterEvent(NS_LITERAL_STRING("adapteradded"), init);
}
void
BluetoothManager::HandleAdapterRemoved(const BluetoothValue& aValue)
{
MOZ_ASSERT(aValue.type() == BluetoothValue::TBluetoothAddress);
MOZ_ASSERT(DefaultAdapterExists());
// Remove the adapter of given address from adapters array
nsString addressToRemove;
AddressToString(aValue.get_BluetoothAddress(), addressToRemove);
uint32_t i;
for (i = 0; i < mAdapters.Length(); i++) {
nsString address;
mAdapters[i]->GetAddress(address);
if (address.Equals(addressToRemove)) {
mAdapters.RemoveElementAt(i);
break;
}
}
// Notify application of removed adapter
BluetoothAdapterEventInit init;
init.mAddress = addressToRemove;
DispatchAdapterEvent(NS_LITERAL_STRING("adapterremoved"), init);
// Reselect default adapter if it's removed
if (mDefaultAdapterIndex == (int)i) {
ReselectDefaultAdapter();
}
}
void
BluetoothManager::ReselectDefaultAdapter()
{
// Select the first of existing/remaining adapters as default adapter
mDefaultAdapterIndex = mAdapters.IsEmpty() ? -1 : 0;
// Notify application of default adapter change
DispatchAttributeEvent();
}
void
BluetoothManager::DispatchAdapterEvent(const nsAString& aType,
const BluetoothAdapterEventInit& aInit)
{
RefPtr<BluetoothAdapterEvent> event =
BluetoothAdapterEvent::Constructor(this, aType, aInit);
DispatchTrustedEvent(event);
}
void
BluetoothManager::DispatchAttributeEvent()
{
MOZ_ASSERT(NS_IsMainThread());
Sequence<nsString> types;
BT_APPEND_ENUM_STRING_FALLIBLE(types,
BluetoothManagerAttribute,
BluetoothManagerAttribute::DefaultAdapter);
// Notify application of default adapter change
BluetoothAttributeEventInit init;
init.mAttrs = types;
RefPtr<BluetoothAttributeEvent> event =
BluetoothAttributeEvent::Constructor(
this, NS_LITERAL_STRING(ATTRIBUTE_CHANGED_ID), init);
DispatchTrustedEvent(event);
}
void
BluetoothManager::Notify(const BluetoothSignal& aData)
{
BT_LOGD("[M] %s", NS_ConvertUTF16toUTF8(aData.name()).get());
NS_ENSURE_TRUE_VOID(mSignalRegistered);
if (aData.name().EqualsLiteral("AdapterAdded")) {
HandleAdapterAdded(aData.value());
} else if (aData.name().EqualsLiteral("AdapterRemoved")) {
HandleAdapterRemoved(aData.value());
} else {
BT_WARNING("Not handling manager signal: %s",
NS_ConvertUTF16toUTF8(aData.name()).get());
}
}
JSObject*
BluetoothManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return BluetoothManagerBinding::Wrap(aCx, this, aGivenProto);
}
// static
bool
BluetoothManager::B2GGattClientEnabled(JSContext* cx, JSObject* aGlobal)
{
return !Preferences::GetBool("dom.bluetooth.webbluetooth.enabled");
}