From e66199f9503844e3c76502d3688446e9abdf0bd5 Mon Sep 17 00:00:00 2001 From: Thomas Zimmermann Date: Mon, 12 Aug 2013 10:32:53 +0200 Subject: [PATCH] Bug 888595 - Convert BluetoothAdapter to WebIDL. r=bz, r=echou In addition to converting BluetoothAdapter, this patch also converts MediaMetaData and MediaPlayStatus to WebIDL. The old XPIDL-based bindings have been removed. --- dom/base/nsDOMClassInfo.cpp | 7 - dom/base/nsDOMClassInfoClasses.h | 1 - dom/bindings/Bindings.conf | 7 + dom/bluetooth/BluetoothAdapter.cpp | 714 ++++++++++--------- dom/bluetooth/BluetoothAdapter.h | 143 +++- dom/bluetooth/BluetoothManager.cpp | 4 +- dom/bluetooth/BluetoothPropertyContainer.cpp | 52 +- dom/bluetooth/BluetoothPropertyContainer.h | 17 +- dom/bluetooth/MediaMetaData.cpp | 83 --- dom/bluetooth/MediaMetaData.h | 32 - dom/bluetooth/MediaPlayStatus.cpp | 62 -- dom/bluetooth/MediaPlayStatus.h | 29 - dom/bluetooth/moz.build | 3 - dom/bluetooth/nsIDOMBluetoothAdapter.idl | 117 --- dom/webidl/BluetoothAdapter.webidl | 131 ++++ dom/webidl/WebIDL.mk | 1 + 16 files changed, 659 insertions(+), 744 deletions(-) delete mode 100644 dom/bluetooth/MediaMetaData.cpp delete mode 100644 dom/bluetooth/MediaMetaData.h delete mode 100644 dom/bluetooth/MediaPlayStatus.cpp delete mode 100644 dom/bluetooth/MediaPlayStatus.h delete mode 100644 dom/bluetooth/nsIDOMBluetoothAdapter.idl create mode 100644 dom/webidl/BluetoothAdapter.webidl diff --git a/dom/base/nsDOMClassInfo.cpp b/dom/base/nsDOMClassInfo.cpp index df22aec9fe25..71f36f6007c8 100644 --- a/dom/base/nsDOMClassInfo.cpp +++ b/dom/base/nsDOMClassInfo.cpp @@ -232,7 +232,6 @@ using mozilla::dom::workers::ResolveWorkerClasses; #endif #ifdef MOZ_B2G_BT -#include "BluetoothAdapter.h" #include "BluetoothDevice.h" #endif @@ -632,8 +631,6 @@ static nsDOMClassInfoData sClassInfoData[] = { #endif #ifdef MOZ_B2G_BT - NS_DEFINE_CLASSINFO_DATA(BluetoothAdapter, nsEventTargetSH, - EVENTTARGET_SCRIPTABLE_FLAGS) NS_DEFINE_CLASSINFO_DATA(BluetoothDevice, nsEventTargetSH, EVENTTARGET_SCRIPTABLE_FLAGS) #endif @@ -1524,10 +1521,6 @@ nsDOMClassInfo::Init() #endif #ifdef MOZ_B2G_BT - DOM_CLASSINFO_MAP_BEGIN(BluetoothAdapter, nsIDOMBluetoothAdapter) - DOM_CLASSINFO_MAP_ENTRY(nsIDOMBluetoothAdapter) - DOM_CLASSINFO_MAP_END - DOM_CLASSINFO_MAP_BEGIN(BluetoothDevice, nsIDOMBluetoothDevice) DOM_CLASSINFO_MAP_ENTRY(nsIDOMBluetoothDevice) DOM_CLASSINFO_MAP_END diff --git a/dom/base/nsDOMClassInfoClasses.h b/dom/base/nsDOMClassInfoClasses.h index 0b58a6564394..effc66496dd6 100644 --- a/dom/base/nsDOMClassInfoClasses.h +++ b/dom/base/nsDOMClassInfoClasses.h @@ -136,7 +136,6 @@ DOMCI_CLASS(FMRadio) #endif #ifdef MOZ_B2G_BT -DOMCI_CLASS(BluetoothAdapter) DOMCI_CLASS(BluetoothDevice) #endif diff --git a/dom/bindings/Bindings.conf b/dom/bindings/Bindings.conf index 586a8b515a27..ed257114f119 100644 --- a/dom/bindings/Bindings.conf +++ b/dom/bindings/Bindings.conf @@ -150,6 +150,11 @@ DOMInterfaces = { 'headerFile': 'BatteryManager.h' }, +'BluetoothAdapter': { + 'nativeType': 'mozilla::dom::bluetooth::BluetoothAdapter', + 'headerFile': 'BluetoothAdapter.h' +}, + 'BluetoothManager': { 'nativeType': 'mozilla::dom::bluetooth::BluetoothManager', 'headerFile': 'BluetoothManager.h' @@ -1732,6 +1737,8 @@ def addExternalIface(iface, nativeType=None, headerFile=None, addExternalIface('ActivityOptions', nativeType='nsIDOMMozActivityOptions', headerFile='nsIDOMActivityOptions.h') +addExternalIface('BluetoothDevice', nativeType='nsIDOMBluetoothDevice', + headerFile='nsIDOMBluetoothDevice.h') addExternalIface('Counter') addExternalIface('CSSRule') addExternalIface('DeviceAcceleration', headerFile='nsIDOMDeviceMotionEvent.h', notflattened=True) diff --git a/dom/bluetooth/BluetoothAdapter.cpp b/dom/bluetooth/BluetoothAdapter.cpp index a87d9b2bd2ff..1cb8100c099c 100644 --- a/dom/bluetooth/BluetoothAdapter.cpp +++ b/dom/bluetooth/BluetoothAdapter.cpp @@ -16,6 +16,7 @@ #include "nsThreadUtils.h" #include "mozilla/dom/bluetooth/BluetoothTypes.h" +#include "mozilla/dom/BluetoothAdapterBinding.h" #include "mozilla/dom/ContentChild.h" #include "mozilla/LazyIdleThread.h" #include "mozilla/Util.h" @@ -25,15 +26,12 @@ #include "BluetoothReplyRunnable.h" #include "BluetoothService.h" #include "BluetoothUtils.h" -#include "MediaMetaData.h" -#include "MediaPlayStatus.h" using namespace mozilla; +using namespace mozilla::dom; USING_BLUETOOTH_NAMESPACE -DOMCI_DATA(BluetoothAdapter, BluetoothAdapter) - NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothAdapter) NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(BluetoothAdapter, @@ -42,19 +40,18 @@ NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(BluetoothAdapter, NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mJsDeviceAddresses) NS_IMPL_CYCLE_COLLECTION_TRACE_END -NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothAdapter, +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END -NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothAdapter, +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper) tmp->Unroot(); NS_IMPL_CYCLE_COLLECTION_UNLINK_END +// QueryInterface implementation for BluetoothAdapter NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothAdapter) - NS_INTERFACE_MAP_ENTRY(nsIDOMBluetoothAdapter) - NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BluetoothAdapter) NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper) NS_IMPL_ADDREF_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper) @@ -164,33 +161,20 @@ public: static int kCreatePairedDeviceTimeout = 50000; // unit: msec -nsresult -PrepareDOMRequest(nsIDOMWindow* aWindow, nsIDOMDOMRequest** aRequest) -{ - MOZ_ASSERT(aWindow); - - nsCOMPtr rs = - do_GetService(DOMREQUEST_SERVICE_CONTRACTID); - NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); - - nsresult rv = rs->CreateRequest(aWindow, aRequest); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - BluetoothAdapter::BluetoothAdapter(nsPIDOMWindow* aWindow, const BluetoothValue& aValue) - : BluetoothPropertyContainer(BluetoothObjectType::TYPE_ADAPTER) + : nsDOMEventTargetHelper(aWindow) + , BluetoothPropertyContainer(BluetoothObjectType::TYPE_ADAPTER) + , mJsUuids(nullptr) + , mJsDeviceAddresses(nullptr) , mDiscoverable(false) , mDiscovering(false) , mPairable(false) , mPowered(false) - , mJsUuids(nullptr) - , mJsDeviceAddresses(nullptr) , mIsRooted(false) { MOZ_ASSERT(aWindow); + MOZ_ASSERT(IsDOMBinding()); BindToOwner(aWindow); const InfallibleTArray& values = @@ -372,210 +356,191 @@ BluetoothAdapter::Notify(const BluetoothSignal& aData) } } -nsresult -BluetoothAdapter::StartStopDiscovery(bool aStart, nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::StartStopDiscovery(bool aStart, ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv; - rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsresult rv; if (aStart) { rv = bs->StartDiscoveryInternal(results); } else { rv = bs->StopDiscoveryInternal(results); } - if(NS_FAILED(rv)) { + if (NS_FAILED(rv)) { NS_WARNING("Start/Stop Discovery failed!"); - return NS_ERROR_FAILURE; + aRv.Throw(rv); + return nullptr; } // mDiscovering is not set here, we'll get a Property update from our external // protocol to tell us that it's been set. - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMETHODIMP -BluetoothAdapter::StartDiscovery(nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::StartDiscovery(ErrorResult& aRv) { - return StartStopDiscovery(true, aRequest); + return StartStopDiscovery(true, aRv); } -NS_IMETHODIMP -BluetoothAdapter::StopDiscovery(nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::StopDiscovery(ErrorResult& aRv) { - return StartStopDiscovery(false, aRequest); + return StartStopDiscovery(false, aRv); } -NS_IMETHODIMP -BluetoothAdapter::GetAddress(nsAString& aAddress) +JS::Value +BluetoothAdapter::GetDevices(JSContext* aContext, ErrorResult& aRv) { - aAddress = mAddress; - return NS_OK; -} - -NS_IMETHODIMP -BluetoothAdapter::GetAdapterClass(uint32_t* aClass) -{ - *aClass = mClass; - return NS_OK; -} - -NS_IMETHODIMP -BluetoothAdapter::GetDiscovering(bool* aDiscovering) -{ - *aDiscovering = mDiscovering; - return NS_OK; -} - -NS_IMETHODIMP -BluetoothAdapter::GetName(nsAString& aName) -{ - aName = mName; - return NS_OK; -} - -NS_IMETHODIMP -BluetoothAdapter::GetDiscoverable(bool* aDiscoverable) -{ - *aDiscoverable = mDiscoverable; - return NS_OK; -} - -NS_IMETHODIMP -BluetoothAdapter::GetDiscoverableTimeout(uint32_t* aDiscoverableTimeout) -{ - *aDiscoverableTimeout = mDiscoverableTimeout; - return NS_OK; -} - -NS_IMETHODIMP -BluetoothAdapter::GetDevices(JSContext* aCx, JS::Value* aDevices) -{ - if (mJsDeviceAddresses) { - aDevices->setObject(*mJsDeviceAddresses); - } - else { + if (!mJsDeviceAddresses) { NS_WARNING("Devices not yet set!\n"); - return NS_ERROR_FAILURE; + aRv.Throw(NS_ERROR_FAILURE); + return JS::NullValue(); } - return NS_OK; + + return JS::ObjectValue(*xpc_UnmarkGrayObject(mJsDeviceAddresses)); } -NS_IMETHODIMP -BluetoothAdapter::GetUuids(JSContext* aCx, JS::Value* aValue) +JS::Value +BluetoothAdapter::GetUuids(JSContext* aContext, ErrorResult& aRv) { - if (mJsUuids) { - aValue->setObject(*mJsUuids); - } - else { + if (!mJsUuids) { NS_WARNING("UUIDs not yet set!\n"); - return NS_ERROR_FAILURE; - } - return NS_OK; + aRv.Throw(NS_ERROR_FAILURE); + return JS::NullValue(); + } + + return JS::ObjectValue(*xpc_UnmarkGrayObject(mJsUuids)); } -NS_IMETHODIMP -BluetoothAdapter::SetName(const nsAString& aName, - nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::SetName(const nsAString& aName, ErrorResult& aRv) { if (mName.Equals(aName)) { - return FirePropertyAlreadySet(GetOwner(), aRequest); + return FirePropertyAlreadySet(GetOwner(), aRv); } nsString name(aName); BluetoothValue value(name); BluetoothNamedValue property(NS_LITERAL_STRING("Name"), value); - return SetProperty(GetOwner(), property, aRequest); + return SetProperty(GetOwner(), property, aRv); } -NS_IMETHODIMP -BluetoothAdapter::SetDiscoverable(const bool aDiscoverable, - nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::SetDiscoverable(bool aDiscoverable, ErrorResult& aRv) { if (aDiscoverable == mDiscoverable) { - return FirePropertyAlreadySet(GetOwner(), aRequest); + return FirePropertyAlreadySet(GetOwner(), aRv); } BluetoothValue value(aDiscoverable); BluetoothNamedValue property(NS_LITERAL_STRING("Discoverable"), value); - return SetProperty(GetOwner(), property, aRequest); + return SetProperty(GetOwner(), property, aRv); } - -NS_IMETHODIMP -BluetoothAdapter::SetDiscoverableTimeout(const uint32_t aDiscoverableTimeout, - nsIDOMDOMRequest** aRequest) + +already_AddRefed +BluetoothAdapter::SetDiscoverableTimeout(uint32_t aDiscoverableTimeout, ErrorResult& aRv) { if (aDiscoverableTimeout == mDiscoverableTimeout) { - return FirePropertyAlreadySet(GetOwner(), aRequest); + return FirePropertyAlreadySet(GetOwner(), aRv); } BluetoothValue value(aDiscoverableTimeout); BluetoothNamedValue property(NS_LITERAL_STRING("DiscoverableTimeout"), value); - return SetProperty(GetOwner(), property, aRequest); + return SetProperty(GetOwner(), property, aRv); } -NS_IMETHODIMP -BluetoothAdapter::GetConnectedDevices(uint16_t aProfileId, - nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::GetConnectedDevices(uint16_t aProfileId, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new GetDevicesTask(this, req); + new GetDevicesTask(this, request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); - rv = bs->GetConnectedDevicePropertiesInternal(aProfileId, results); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsresult rv = bs->GetConnectedDevicePropertiesInternal(aProfileId, results); + if (NS_FAILED(rv)) { + aRv.Throw(rv); + return nullptr; + } - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMETHODIMP -BluetoothAdapter::GetPairedDevices(nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::GetPairedDevices(ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new GetDevicesTask(this, req); + new GetDevicesTask(this, request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); - rv = bs->GetPairedDevicePropertiesInternal(mDeviceAddresses, results); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsresult rv = bs->GetPairedDevicePropertiesInternal(mDeviceAddresses, results); + if (NS_FAILED(rv)) { + aRv.Throw(rv); + return nullptr; + } - req.forget(aRequest); - return NS_OK; + return request.forget(); } -nsresult -BluetoothAdapter::PairUnpair(bool aPair, - nsIDOMBluetoothDevice* aDevice, - nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::PairUnpair(bool aPair, nsIDOMBluetoothDevice* aDevice, + ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); nsAutoString addr; aDevice->GetAddress(addr); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsresult rv; if (aPair) { rv = bs->CreatePairedDeviceInternal(addr, kCreatePairedDeviceTimeout, @@ -583,350 +548,393 @@ BluetoothAdapter::PairUnpair(bool aPair, } else { rv = bs->RemoveDeviceInternal(addr, results); } - if (NS_FAILED(rv)) { NS_WARNING("Pair/Unpair failed!"); - return NS_ERROR_FAILURE; + aRv.Throw(rv); + return nullptr; } - req.forget(aRequest); - return NS_OK; + return request.forget(); } -nsresult -BluetoothAdapter::Pair(nsIDOMBluetoothDevice* aDevice, - nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::Pair(nsIDOMBluetoothDevice* aDevice, ErrorResult& aRv) { - return PairUnpair(true, aDevice, aRequest); + return PairUnpair(true, aDevice, aRv); } -nsresult -BluetoothAdapter::Unpair(nsIDOMBluetoothDevice* aDevice, - nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::Unpair(nsIDOMBluetoothDevice* aDevice, ErrorResult& aRv) { - return PairUnpair(false, aDevice, aRequest); + return PairUnpair(false, aDevice, aRv); } -nsresult +already_AddRefed BluetoothAdapter::SetPinCode(const nsAString& aDeviceAddress, - const nsAString& aPinCode, - nsIDOMDOMRequest** aRequest) + const nsAString& aPinCode, ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); - if(!bs->SetPinCodeInternal(aDeviceAddress, aPinCode, results)) { + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + if (!bs->SetPinCodeInternal(aDeviceAddress, aPinCode, results)) { NS_WARNING("SetPinCode failed!"); - return NS_ERROR_FAILURE; + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; } - req.forget(aRequest); - return NS_OK; + return request.forget(); } -nsresult +already_AddRefed BluetoothAdapter::SetPasskey(const nsAString& aDeviceAddress, uint32_t aPasskey, - nsIDOMDOMRequest** aRequest) + ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); - if(bs->SetPasskeyInternal(aDeviceAddress, aPasskey, results)) { + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + if (bs->SetPasskeyInternal(aDeviceAddress, aPasskey, results)) { NS_WARNING("SetPasskeyInternal failed!"); - return NS_ERROR_FAILURE; + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; } - req.forget(aRequest); - return NS_OK; + return request.forget(); } -nsresult +already_AddRefed BluetoothAdapter::SetPairingConfirmation(const nsAString& aDeviceAddress, - bool aConfirmation, - nsIDOMDOMRequest** aRequest) + bool aConfirmation, ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); - if(!bs->SetPairingConfirmationInternal(aDeviceAddress, - aConfirmation, - results)) { + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + if (!bs->SetPairingConfirmationInternal(aDeviceAddress, + aConfirmation, + results)) { NS_WARNING("SetPairingConfirmation failed!"); - return NS_ERROR_FAILURE; + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; } - req.forget(aRequest); - return NS_OK; + return request.forget(); } -nsresult +already_AddRefed BluetoothAdapter::SetAuthorization(const nsAString& aDeviceAddress, bool aAllow, - nsIDOMDOMRequest** aRequest) + ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); - - nsRefPtr results = - new BluetoothVoidReplyRunnable(req); - - BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); - if(!bs->SetAuthorizationInternal(aDeviceAddress, aAllow, results)) { - NS_WARNING("SetAuthorization failed!"); - return NS_ERROR_FAILURE; + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; } - req.forget(aRequest); - return NS_OK; -} - -NS_IMETHODIMP -BluetoothAdapter::Connect(const nsAString& aDeviceAddress, - uint16_t aProfileId, - nsIDOMDOMRequest** aRequest) -{ - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsRefPtr request = new DOMRequest(win); + nsRefPtr results = + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + if (!bs->SetAuthorizationInternal(aDeviceAddress, aAllow, results)) { + NS_WARNING("SetAuthorization failed!"); + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + return request.forget(); +} + +already_AddRefed +BluetoothAdapter::Connect(const nsAString& aDeviceAddress, + uint16_t aProfileId, ErrorResult& aRv) +{ + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); + + BluetoothService* bs = BluetoothService::Get(); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } bs->Connect(aDeviceAddress, aProfileId, results); - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMETHODIMP -BluetoothAdapter::Disconnect(uint16_t aProfileId, - nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::Disconnect(uint16_t aProfileId, ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } bs->Disconnect(aProfileId, results); - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMETHODIMP +already_AddRefed BluetoothAdapter::SendFile(const nsAString& aDeviceAddress, - nsIDOMBlob* aBlob, - nsIDOMDOMRequest** aRequest) + nsIDOMBlob* aBlob, ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BlobChild* actor = - mozilla::dom::ContentChild::GetSingleton()->GetOrCreateActorForBlob(aBlob); + ContentChild::GetSingleton()->GetOrCreateActorForBlob(aBlob); if (!actor) { NS_WARNING("Can't create actor"); - return NS_ERROR_FAILURE; + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; } BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } bs->SendFile(aDeviceAddress, nullptr, actor, results); - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMETHODIMP -BluetoothAdapter::StopSendingFile(const nsAString& aDeviceAddress, - nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::StopSendingFile(const nsAString& aDeviceAddress, ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } bs->StopSendingFile(aDeviceAddress, results); - req.forget(aRequest); - return NS_OK; + return request.forget(); } -nsresult +already_AddRefed BluetoothAdapter::ConfirmReceivingFile(const nsAString& aDeviceAddress, - bool aConfirmation, - nsIDOMDOMRequest** aRequest) + bool aConfirmation, ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } bs->ConfirmReceivingFile(aDeviceAddress, aConfirmation, results); - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMETHODIMP -BluetoothAdapter::ConnectSco(nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::ConnectSco(ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } bs->ConnectSco(results); - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMETHODIMP -BluetoothAdapter::DisconnectSco(nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::DisconnectSco(ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } bs->DisconnectSco(results); - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMETHODIMP -BluetoothAdapter::IsScoConnected(nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::IsScoConnected(ErrorResult& aRv) { - nsCOMPtr req; - nsresult rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new GetScoConnectionStatusTask(req); + new GetScoConnectionStatusTask(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } bs->IsScoConnected(results); - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMETHODIMP -BluetoothAdapter::SendMediaMetaData(const JS::Value& aOptions, - nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::SendMediaMetaData(const MediaMetaData& aMediaMetaData, ErrorResult& aRv) { - MediaMetaData metadata; - - nsresult rv; - nsIScriptContext* sc = GetContextForEventHandlers(&rv); - NS_ENSURE_SUCCESS(rv, rv); - - AutoPushJSContext cx(sc->GetNativeContext()); - rv = metadata.Init(cx, &aOptions); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr req; - rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); - bs->SendMetaData(metadata.mTitle, - metadata.mArtist, - metadata.mAlbum, - metadata.mMediaNumber, - metadata.mTotalMediaCount, - metadata.mDuration, + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + bs->SendMetaData(aMediaMetaData.mTitle, + aMediaMetaData.mArtist, + aMediaMetaData.mAlbum, + aMediaMetaData.mMediaNumber, + aMediaMetaData.mTotalMediaCount, + aMediaMetaData.mDuration, results); - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMETHODIMP -BluetoothAdapter::SendMediaPlayStatus(const JS::Value& aOptions, - nsIDOMDOMRequest** aRequest) +already_AddRefed +BluetoothAdapter::SendMediaPlayStatus(const MediaPlayStatus& aMediaPlayStatus, ErrorResult& aRv) { - MediaPlayStatus status; - - nsresult rv; - nsIScriptContext* sc = GetContextForEventHandlers(&rv); - NS_ENSURE_SUCCESS(rv, rv); - - AutoPushJSContext cx(sc->GetNativeContext()); - rv = status.Init(cx, &aOptions); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr req; - rv = PrepareDOMRequest(GetOwner(), getter_AddRefs(req)); - NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr win = GetOwner(); + if (!win) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + nsRefPtr request = new DOMRequest(win); nsRefPtr results = - new BluetoothVoidReplyRunnable(req); + new BluetoothVoidReplyRunnable(request); BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); - bs->SendPlayStatus(status.mDuration, - status.mPosition, - status.mPlayStatus, + if (!bs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + bs->SendPlayStatus(aMediaPlayStatus.mDuration, + aMediaPlayStatus.mPosition, + aMediaPlayStatus.mPlayStatus, results); - req.forget(aRequest); - return NS_OK; + return request.forget(); } -NS_IMPL_EVENT_HANDLER(BluetoothAdapter, devicefound) -NS_IMPL_EVENT_HANDLER(BluetoothAdapter, a2dpstatuschanged) -NS_IMPL_EVENT_HANDLER(BluetoothAdapter, hfpstatuschanged) -NS_IMPL_EVENT_HANDLER(BluetoothAdapter, pairedstatuschanged) -NS_IMPL_EVENT_HANDLER(BluetoothAdapter, scostatuschanged) +JSObject* +BluetoothAdapter::WrapObject(JSContext* aCx, JS::Handle aScope) +{ + return BluetoothAdapterBinding::Wrap(aCx, aScope, this); +} diff --git a/dom/bluetooth/BluetoothAdapter.h b/dom/bluetooth/BluetoothAdapter.h index 8eaee0f4ac67..216d93870a32 100644 --- a/dom/bluetooth/BluetoothAdapter.h +++ b/dom/bluetooth/BluetoothAdapter.h @@ -12,10 +12,15 @@ #include "BluetoothPropertyContainer.h" #include "nsCOMPtr.h" #include "nsDOMEventTargetHelper.h" -#include "nsIDOMBluetoothAdapter.h" +#include "nsIDOMBluetoothDevice.h" -class nsIEventTarget; -class nsIDOMDOMRequest; +namespace mozilla { +namespace dom { +class DOMRequest; +struct MediaMetaData; +struct MediaPlayStatus; +} +} BEGIN_BLUETOOTH_NAMESPACE @@ -24,15 +29,11 @@ class BluetoothNamedValue; class BluetoothValue; class BluetoothAdapter : public nsDOMEventTargetHelper - , public nsIDOMBluetoothAdapter , public BluetoothSignalObserver , public BluetoothPropertyContainer { public: NS_DECL_ISUPPORTS_INHERITED - NS_DECL_NSIDOMBLUETOOTHADAPTER - - NS_REALLY_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper) NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper) @@ -42,25 +43,129 @@ public: void Notify(const BluetoothSignal& aParam); - nsISupports* - ToISupports() + void Unroot(); + virtual void SetPropertyByValue(const BluetoothNamedValue& aValue) MOZ_OVERRIDE; + + void GetAddress(nsString& aAddress) const { - return static_cast(this); + aAddress = mAddress; } - void Unroot(); - virtual void SetPropertyByValue(const BluetoothNamedValue& aValue) MOZ_OVERRIDE; -private: + uint32_t + Class() const + { + return mClass; + } + void + GetName(nsString& aName) const + { + aName = mName; + } + + bool + Discovering() const + { + return mDiscovering; + } + + bool + Discoverable() const + { + return mDiscoverable; + } + + uint32_t + DiscoverableTimeout() const + { + return mDiscoverableTimeout; + } + + JS::Value GetDevices(JSContext* aContext, ErrorResult& aRv); + JS::Value GetUuids(JSContext* aContext, ErrorResult& aRv); + + already_AddRefed + SetName(const nsAString& aName, ErrorResult& aRv); + + already_AddRefed + SetDiscoverable(bool aDiscoverable, ErrorResult& aRv); + already_AddRefed + SetDiscoverableTimeout(uint32_t aTimeout, ErrorResult& aRv); + already_AddRefed StartDiscovery(ErrorResult& aRv); + already_AddRefed StopDiscovery(ErrorResult& aRv); + + already_AddRefed + Pair(nsIDOMBluetoothDevice* aDevice, ErrorResult& aRv); + already_AddRefed + Unpair(nsIDOMBluetoothDevice* aDevice, ErrorResult& aRv); + already_AddRefed + GetPairedDevices(ErrorResult& aRv); + already_AddRefed + SetPinCode(const nsAString& aDeviceAddress, const nsAString& aPinCode, + ErrorResult& aRv); + already_AddRefed + SetPasskey(const nsAString& aDeviceAddress, uint32_t aPasskey, + ErrorResult& aRv); + already_AddRefed + SetPairingConfirmation(const nsAString& aDeviceAddress, bool aConfirmation, + ErrorResult& aRv); + already_AddRefed + SetAuthorization(const nsAString& aDeviceAddress, bool aAllow, + ErrorResult& aRv); + + already_AddRefed + Connect(const nsAString& aDeviceAddress, uint16_t aProfile, + ErrorResult& aRv); + already_AddRefed + Disconnect(uint16_t aProfile, ErrorResult& aRv); + already_AddRefed + GetConnectedDevices(uint16_t aProfile, ErrorResult& aRv); + + already_AddRefed + SendFile(const nsAString& aDeviceAddress, nsIDOMBlob* aBlob, + ErrorResult& aRv); + already_AddRefed + StopSendingFile(const nsAString& aDeviceAddress, ErrorResult& aRv); + already_AddRefed + ConfirmReceivingFile(const nsAString& aDeviceAddress, bool aConfirmation, + ErrorResult& aRv); + + already_AddRefed ConnectSco(ErrorResult& aRv); + already_AddRefed DisconnectSco(ErrorResult& aRv); + already_AddRefed IsScoConnected(ErrorResult& aRv); + + already_AddRefed + SendMediaMetaData(const MediaMetaData& aMediaMetaData, ErrorResult& aRv); + already_AddRefed + SendMediaPlayStatus(const MediaPlayStatus& aMediaPlayStatus, ErrorResult& aRv); + + IMPL_EVENT_HANDLER(devicefound); + IMPL_EVENT_HANDLER(a2dpstatuschanged); + IMPL_EVENT_HANDLER(hfpstatuschanged); + IMPL_EVENT_HANDLER(pairedstatuschanged); + IMPL_EVENT_HANDLER(scostatuschanged); + + nsPIDOMWindow* GetParentObject() const + { + return GetOwner(); + } + + virtual JSObject* + WrapObject(JSContext* aCx, JS::Handle aScope) MOZ_OVERRIDE; + +private: BluetoothAdapter(nsPIDOMWindow* aOwner, const BluetoothValue& aValue); ~BluetoothAdapter(); void Root(); - nsresult StartStopDiscovery(bool aStart, nsIDOMDOMRequest** aRequest); - nsresult PairUnpair(bool aPair, - nsIDOMBluetoothDevice* aDevice, - nsIDOMDOMRequest** aRequest); - + + already_AddRefed + StartStopDiscovery(bool aStart, ErrorResult& aRv); + already_AddRefed + PairUnpair(bool aPair, nsIDOMBluetoothDevice* aDevice, ErrorResult& aRv); + + JS::Heap mJsUuids; + JS::Heap mJsDeviceAddresses; nsString mAddress; nsString mName; bool mDiscoverable; @@ -72,8 +177,6 @@ private: uint32_t mClass; nsTArray mDeviceAddresses; nsTArray mUuids; - JS::Heap mJsUuids; - JS::Heap mJsDeviceAddresses; bool mIsRooted; }; diff --git a/dom/bluetooth/BluetoothManager.cpp b/dom/bluetooth/BluetoothManager.cpp index 34a603d573bb..43118c15fa39 100644 --- a/dom/bluetooth/BluetoothManager.cpp +++ b/dom/bluetooth/BluetoothManager.cpp @@ -55,8 +55,8 @@ public: const InfallibleTArray& values = v.get_ArrayOfBluetoothNamedValue(); - nsCOMPtr adapter; - adapter = BluetoothAdapter::Create(mManagerPtr->GetOwner(), values); + nsRefPtr adapter = + BluetoothAdapter::Create(mManagerPtr->GetOwner(), values); nsresult rv; nsIScriptContext* sc = mManagerPtr->GetContextForEventHandlers(&rv); diff --git a/dom/bluetooth/BluetoothPropertyContainer.cpp b/dom/bluetooth/BluetoothPropertyContainer.cpp index 803f7f5edea3..5ae8ec74a49e 100644 --- a/dom/bluetooth/BluetoothPropertyContainer.cpp +++ b/dom/bluetooth/BluetoothPropertyContainer.cpp @@ -8,57 +8,49 @@ #include "BluetoothPropertyContainer.h" #include "BluetoothService.h" #include "DOMRequest.h" +#include "mozilla/ErrorResult.h" #include "mozilla/dom/bluetooth/BluetoothTypes.h" USING_BLUETOOTH_NAMESPACE -nsresult +already_AddRefed BluetoothPropertyContainer::FirePropertyAlreadySet(nsIDOMWindow* aOwner, - nsIDOMDOMRequest** aRequest) + ErrorResult& aRv) { nsCOMPtr rs = do_GetService(DOMREQUEST_SERVICE_CONTRACTID); - NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); - - nsCOMPtr req; - nsresult rv = rs->CreateRequest(aOwner, getter_AddRefs(req)); - if (NS_FAILED(rv)) { - NS_WARNING("Can't create DOMRequest!"); - return NS_ERROR_FAILURE; + if (!rs) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; } - rs->FireSuccess(req, JSVAL_VOID); - req.forget(aRequest); - return NS_OK; + nsRefPtr request = new DOMRequest(aOwner); + rs->FireSuccess(request, JS::UndefinedValue()); + + return request.forget(); } -nsresult +already_AddRefed BluetoothPropertyContainer::SetProperty(nsIDOMWindow* aOwner, const BluetoothNamedValue& aProperty, - nsIDOMDOMRequest** aRequest) + ErrorResult& aRv) { + nsRefPtr request = new DOMRequest(aOwner); + nsRefPtr task = + new BluetoothVoidReplyRunnable(request); + BluetoothService* bs = BluetoothService::Get(); if (!bs) { NS_WARNING("Bluetooth service not available!"); - return NS_ERROR_FAILURE; + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; } - nsCOMPtr rs = - do_GetService(DOMREQUEST_SERVICE_CONTRACTID); - NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); - - nsCOMPtr req; - nsresult rv = rs->CreateRequest(aOwner, getter_AddRefs(req)); + nsresult rv = bs->SetProperty(mObjectType, aProperty, task); if (NS_FAILED(rv)) { - NS_WARNING("Can't create DOMRequest!"); - return NS_ERROR_FAILURE; + aRv.Throw(rv); + return nullptr; } - nsRefPtr task = new BluetoothVoidReplyRunnable(req); - - rv = bs->SetProperty(mObjectType, aProperty, task); - NS_ENSURE_SUCCESS(rv, rv); - - req.forget(aRequest); - return NS_OK; + return request.forget(); } diff --git a/dom/bluetooth/BluetoothPropertyContainer.h b/dom/bluetooth/BluetoothPropertyContainer.h index 79badf2abf37..8e6509f20888 100644 --- a/dom/bluetooth/BluetoothPropertyContainer.h +++ b/dom/bluetooth/BluetoothPropertyContainer.h @@ -13,6 +13,13 @@ class nsIDOMDOMRequest; class nsIDOMWindow; +namespace mozilla { +class ErrorResult; +namespace dom { +class DOMRequest; +} +} + BEGIN_BLUETOOTH_NAMESPACE class BluetoothNamedValue; @@ -20,11 +27,11 @@ class BluetoothNamedValue; class BluetoothPropertyContainer { public: - nsresult FirePropertyAlreadySet(nsIDOMWindow* aOwner, - nsIDOMDOMRequest** aRequest); - nsresult SetProperty(nsIDOMWindow* aOwner, - const BluetoothNamedValue& aProperty, - nsIDOMDOMRequest** aRequest); + already_AddRefed + FirePropertyAlreadySet(nsIDOMWindow* aOwner, ErrorResult& aRv); + already_AddRefed + SetProperty(nsIDOMWindow* aOwner, const BluetoothNamedValue& aProperty, + ErrorResult& aRv); virtual void SetPropertyByValue(const BluetoothNamedValue& aValue) = 0; nsString GetPath() { diff --git a/dom/bluetooth/MediaMetaData.cpp b/dom/bluetooth/MediaMetaData.cpp deleted file mode 100644 index b2bf0bec9760..000000000000 --- a/dom/bluetooth/MediaMetaData.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ -/* vim: set ts=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 "BluetoothCommon.h" -#include "MediaMetaData.h" - -#include "nsCxPusher.h" -#include "nsContentUtils.h" -#include "nsJSUtils.h" -#include "nsThreadUtils.h" - -using namespace mozilla; -USING_BLUETOOTH_NAMESPACE - -MediaMetaData::MediaMetaData() : mDuration(-1) - , mMediaNumber(-1) - , mTotalMediaCount(-1) -{ -} - -nsresult -MediaMetaData::Init(JSContext* aCx, const jsval* aVal) -{ - MOZ_ASSERT(NS_IsMainThread()); - - if (!aCx || !aVal) { - return NS_OK; - } - - if (!aVal->isObject()) { - return aVal->isNullOrUndefined() ? NS_OK : NS_ERROR_TYPE_ERR; - } - - JS::RootedObject obj(aCx, &aVal->toObject()); - nsCxPusher pusher; - pusher.Push(aCx); - JSAutoCompartment ac(aCx, obj); - - JS::Rooted value(aCx); - NS_ENSURE_STATE(JS_GetProperty(aCx, obj, "mAlbum", &value)); - if (JSVAL_IS_STRING(value)) { - nsDependentJSString jsString; - NS_ENSURE_STATE(jsString.init(aCx, value.toString())); - mAlbum = jsString; - } - - NS_ENSURE_STATE(JS_GetProperty(aCx, obj, "mArtist", &value)); - if (JSVAL_IS_STRING(value)) { - nsDependentJSString jsString; - NS_ENSURE_STATE(JSVAL_IS_STRING(value)); - NS_ENSURE_STATE(jsString.init(aCx, value.toString())); - mArtist = jsString; - } - - NS_ENSURE_STATE(JS_GetProperty(aCx, obj, "mDuration", &value)); - if (JSVAL_IS_INT(value)) { - NS_ENSURE_STATE(JS_ValueToInt64(aCx, value, &mDuration)); - } - - NS_ENSURE_STATE(JS_GetProperty(aCx, obj, "mMediaNumber", &value)); - if (JSVAL_IS_INT(value)) { - NS_ENSURE_STATE(JS_ValueToInt64(aCx, value, &mMediaNumber)); - } - - NS_ENSURE_STATE(JS_GetProperty(aCx, obj, "mTitle", &value)); - if (JSVAL_IS_STRING(value)) { - nsDependentJSString jsString; - NS_ENSURE_STATE(JSVAL_IS_STRING(value)); - NS_ENSURE_STATE(jsString.init(aCx, value.toString())); - mTitle = jsString; - } - - NS_ENSURE_STATE(JS_GetProperty(aCx, obj, "mTotalMediaCount", &value)); - if (JSVAL_IS_INT(value)) { - NS_ENSURE_STATE(JS_ValueToInt64(aCx, value, &mTotalMediaCount)); - } - - return NS_OK; -} - diff --git a/dom/bluetooth/MediaMetaData.h b/dom/bluetooth/MediaMetaData.h deleted file mode 100644 index b59c11cb3282..000000000000 --- a/dom/bluetooth/MediaMetaData.h +++ /dev/null @@ -1,32 +0,0 @@ -/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ -/* vim: set ts=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/. */ - -#ifndef mozilla_dom_bluetooth_mediametadata_h__ -#define mozilla_dom_bluetooth_mediametadata_h__ - -#include "jsapi.h" -#include "nsString.h" - -BEGIN_BLUETOOTH_NAMESPACE - -class MediaMetaData -{ -public: - MediaMetaData(); - - nsresult Init(JSContext* aCx, const jsval* aVal); - - nsString mAlbum; - nsString mArtist; - int64_t mDuration; - int64_t mMediaNumber; - nsString mTitle; - int64_t mTotalMediaCount; -}; - -END_BLUETOOTH_NAMESPACE - -#endif diff --git a/dom/bluetooth/MediaPlayStatus.cpp b/dom/bluetooth/MediaPlayStatus.cpp deleted file mode 100644 index 9adc3c799362..000000000000 --- a/dom/bluetooth/MediaPlayStatus.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ -/* vim: set ts=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 "BluetoothCommon.h" -#include "MediaPlayStatus.h" - -#include "nsContentUtils.h" -#include "nsCxPusher.h" -#include "nsJSUtils.h" -#include "nsThreadUtils.h" - -using namespace mozilla; -USING_BLUETOOTH_NAMESPACE - -MediaPlayStatus::MediaPlayStatus() : mDuration(-1) - , mPosition(-1) -{ -} - -nsresult -MediaPlayStatus::Init(JSContext* aCx, const jsval* aVal) -{ - MOZ_ASSERT(NS_IsMainThread()); - - if (!aCx || !aVal) { - return NS_OK; - } - - if (!aVal->isObject()) { - return aVal->isNullOrUndefined() ? NS_OK : NS_ERROR_TYPE_ERR; - } - - JS::RootedObject obj(aCx, &aVal->toObject()); - nsCxPusher pusher; - pusher.Push(aCx); - JSAutoCompartment ac(aCx, obj); - - JS::Rooted value(aCx); - NS_ENSURE_STATE(JS_GetProperty(aCx, obj, "mDuration", &value)); - if (JSVAL_IS_INT(value)) { - NS_ENSURE_STATE(JS_ValueToInt64(aCx, value, &mDuration)); - } - - NS_ENSURE_STATE(JS_GetProperty(aCx, obj, "mPlayStatus", &value)); - if (JSVAL_IS_STRING(value)) { - nsDependentJSString jsString; - NS_ENSURE_STATE(JSVAL_IS_STRING(value)); - NS_ENSURE_STATE(jsString.init(aCx, value.toString())); - mPlayStatus = jsString; - } - - NS_ENSURE_STATE(JS_GetProperty(aCx, obj, "mPosition", &value)); - if (JSVAL_IS_INT(value)) { - NS_ENSURE_STATE(JS_ValueToInt64(aCx, value, &mPosition)); - } - - return NS_OK; -} - diff --git a/dom/bluetooth/MediaPlayStatus.h b/dom/bluetooth/MediaPlayStatus.h deleted file mode 100644 index c66870a37f0f..000000000000 --- a/dom/bluetooth/MediaPlayStatus.h +++ /dev/null @@ -1,29 +0,0 @@ -/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ -/* vim: set ts=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/. */ - -#ifndef mozilla_dom_bluetooth_mediaplaystatus_h__ -#define mozilla_dom_bluetooth_mediaplaystatus_h__ - -#include "jsapi.h" -#include "nsString.h" - -BEGIN_BLUETOOTH_NAMESPACE - -class MediaPlayStatus -{ -public: - MediaPlayStatus(); - - nsresult Init(JSContext* aCx, const jsval* aVal); - - int64_t mDuration; - nsString mPlayStatus; - int64_t mPosition; -}; - -END_BLUETOOTH_NAMESPACE - -#endif diff --git a/dom/bluetooth/moz.build b/dom/bluetooth/moz.build index a5858a58f234..6976b7b8f512 100644 --- a/dom/bluetooth/moz.build +++ b/dom/bluetooth/moz.build @@ -18,7 +18,6 @@ if CONFIG['MOZ_B2G_BT']: MODULE = 'dom' XPIDL_MODULE = 'dom_bluetooth' XPIDL_SOURCES += [ - 'nsIDOMBluetoothAdapter.idl', 'nsIDOMBluetoothDevice.idl', 'nsIDOMBluetoothDeviceEvent.idl', 'nsIDOMBluetoothStatusChangedEvent.idl', @@ -42,8 +41,6 @@ if CONFIG['MOZ_B2G_BT']: 'ObexBase.cpp', 'BluetoothUuid.cpp', 'BluetoothSocket.cpp', - 'MediaMetaData.cpp', - 'MediaPlayStatus.cpp' ] if CONFIG['MOZ_B2G_RIL']: diff --git a/dom/bluetooth/nsIDOMBluetoothAdapter.idl b/dom/bluetooth/nsIDOMBluetoothAdapter.idl deleted file mode 100644 index 5e6d5d9348b9..000000000000 --- a/dom/bluetooth/nsIDOMBluetoothAdapter.idl +++ /dev/null @@ -1,117 +0,0 @@ -/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ -/* vim: set ts=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 "nsIDOMEventTarget.idl" - -/** - * MediaMetadata and MediaPlayStatus are used to keep data from Applications. - * Please see specification of AVRCP 1.3 for more details. - * - * @title: track title - * @artist: artist name - * @album: album name - * @mediaNumber: track number - * @totalMediaCount: number of tracks in the album - * @duration: playing time (ms) - */ -dictionary MediaMetaData -{ - DOMString title; - DOMString artist; - DOMString album; - unsigned long mediaNumber; - unsigned long totalMediaCount; - unsigned long duration; -}; - -/** - * @duration: current track length (ms) - * @position: playing time (ms) - * @playStatus: STOPPED/PLAYING/PAUSED/FWD_SEEK/REV_SEEK/ERROR - */ -dictionary MediaPlayStatus -{ - unsigned long duration; - unsigned long position; - DOMString playStatus; -}; - -interface nsIDOMDOMRequest; -interface nsIDOMBlob; -interface nsIDOMBluetoothDevice; - -[scriptable, builtinclass, uuid(54bf9aa2-1208-47ab-ac96-c7df349fcf0e)] -interface nsIDOMBluetoothAdapter : nsIDOMEventTarget -{ - readonly attribute DOMString address; - [binaryname(AdapterClass)] readonly attribute unsigned long class; - readonly attribute bool discovering; - - [implicit_jscontext] - readonly attribute jsval devices; - - [implicit_jscontext] - readonly attribute jsval uuids; - - readonly attribute DOMString name; - readonly attribute bool discoverable; - // Unit: sec - readonly attribute unsigned long discoverableTimeout; - - nsIDOMDOMRequest setName(in DOMString name); - nsIDOMDOMRequest setDiscoverable(in bool discoverable); - nsIDOMDOMRequest setDiscoverableTimeout(in unsigned long timeout); - nsIDOMDOMRequest startDiscovery(); - nsIDOMDOMRequest stopDiscovery(); - nsIDOMDOMRequest pair(in nsIDOMBluetoothDevice aDevice); - nsIDOMDOMRequest unpair(in nsIDOMBluetoothDevice aDevice); - nsIDOMDOMRequest getPairedDevices(); - nsIDOMDOMRequest getConnectedDevices(in unsigned short aProfile); - nsIDOMDOMRequest setPinCode(in DOMString aDeviceAddress, in DOMString aPinCode); - nsIDOMDOMRequest setPasskey(in DOMString aDeviceAddress, in unsigned long aPasskey); - nsIDOMDOMRequest setPairingConfirmation(in DOMString aDeviceAddress, in bool aConfirmation); - nsIDOMDOMRequest setAuthorization(in DOMString aDeviceAddress, in bool aAllow); - - /** - * Connect/Disconnect to a specific service of a target remote device. - * To check the value of service UUIDs, please check "Bluetooth Assigned - * Numbers" / "Service Discovery Protocol" for more information. - * - * @param aDeviceAddress Remote device address - * @param aProfile 2-octets service UUID - */ - nsIDOMDOMRequest connect(in DOMString aDeviceAddress, in unsigned short aProfile); - nsIDOMDOMRequest disconnect(in unsigned short aProfile); - - // One device can only send one file at a time - nsIDOMDOMRequest sendFile(in DOMString aDeviceAddress, in nsIDOMBlob aBlob); - nsIDOMDOMRequest stopSendingFile(in DOMString aDeviceAddress); - nsIDOMDOMRequest confirmReceivingFile(in DOMString aDeviceAddress, in bool aConfirmation); - - // AVRCP 1.3 methods - nsIDOMDOMRequest sendMediaMetaData(in jsval aOptions); - nsIDOMDOMRequest sendMediaPlayStatus(in jsval aOptions); - - // Connect/Disconnect SCO (audio) connection - nsIDOMDOMRequest connectSco(); - nsIDOMDOMRequest disconnectSco(); - nsIDOMDOMRequest isScoConnected(); - - // Fired when discoverying and any device is discovered. - [implicit_jscontext] attribute jsval ondevicefound; - - // Fired when pairing process is completed - [implicit_jscontext] attribute jsval onpairedstatuschanged; - - // Fired when a2dp connection status changed - [implicit_jscontext] attribute jsval ona2dpstatuschanged; - - // Fired when handsfree connection status changed - [implicit_jscontext] attribute jsval onhfpstatuschanged; - - // Fired when sco connection status changed - [implicit_jscontext] attribute jsval onscostatuschanged; -}; diff --git a/dom/webidl/BluetoothAdapter.webidl b/dom/webidl/BluetoothAdapter.webidl new file mode 100644 index 000000000000..21fe733abe95 --- /dev/null +++ b/dom/webidl/BluetoothAdapter.webidl @@ -0,0 +1,131 @@ +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ +/* vim: set ts=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/. */ + +// MediaMetadata and MediaPlayStatus are used to keep data from Applications. +// Please see specification of AVRCP 1.3 for more details. +dictionary MediaMetaData +{ + // track title + DOMString title = ""; + // artist name + DOMString artist = ""; + // album name + DOMString album = ""; + // track number + long long mediaNumber = -1; + // number of tracks in the album + long long totalMediaCount = -1; + // playing time (ms) + long long duration = -1; +}; + +dictionary MediaPlayStatus +{ + // current track length (ms) + long long duration = -1; + // playing time (ms) + long long position = -1; + // one of 'STOPPED'/'PLAYING'/'PAUSED'/'FWD_SEEK'/'REV_SEEK'/'ERROR' + DOMString playStatus = ""; +}; + +interface BluetoothAdapter : EventTarget { + readonly attribute DOMString address; + readonly attribute unsigned long class; + readonly attribute boolean discovering; + readonly attribute DOMString name; + readonly attribute boolean discoverable; + readonly attribute unsigned long discoverableTimeout; // in seconds + + // array of type BluetoothDevice[] + [GetterThrows] + readonly attribute any devices; + + // array of type DOMString[] + [GetterThrows] + readonly attribute any uuids; + + [SetterThrows] + attribute EventHandler ondevicefound; + + // Fired when pairing process is completed + [SetterThrows] + attribute EventHandler onpairedstatuschanged; + + // Fired when a2dp connection status changed + [SetterThrows] + attribute EventHandler ona2dpstatuschanged; + + // Fired when handsfree connection status changed + [SetterThrows] + attribute EventHandler onhfpstatuschanged; + + // Fired when sco connection status changed + [SetterThrows] + attribute EventHandler onscostatuschanged; + + [Creator, Throws] + DOMRequest setName(DOMString name); + [Creator, Throws] + DOMRequest setDiscoverable(boolean discoverable); + [Creator, Throws] + DOMRequest setDiscoverableTimeout(unsigned long timeout); + [Creator, Throws] + DOMRequest startDiscovery(); + [Creator, Throws] + DOMRequest stopDiscovery(); + [Creator, Throws] + DOMRequest pair(BluetoothDevice device); + [Creator, Throws] + DOMRequest unpair(BluetoothDevice device); + [Creator, Throws] + DOMRequest getPairedDevices(); + [Creator, Throws] + DOMRequest getConnectedDevices(unsigned short profile); + [Creator, Throws] + DOMRequest setPinCode(DOMString deviceAddress, DOMString pinCode); + [Creator, Throws] + DOMRequest setPasskey(DOMString deviceAddress, unsigned long passkey); + [Creator, Throws] + DOMRequest setPairingConfirmation(DOMString deviceAddress, boolean confirmation); + [Creator, Throws] + DOMRequest setAuthorization(DOMString deviceAddress, boolean allow); + + /** + * Connect/Disconnect to a specific service of a target remote device. + * To check the value of service UUIDs, please check "Bluetooth Assigned + * Numbers" / "Service Discovery Protocol" for more information. + * + * @param deviceAddress Remote device address + * @param profile 2-octets service UUID + */ + [Creator, Throws] + DOMRequest connect(DOMString deviceAddress, unsigned short profile); + [Creator, Throws] + DOMRequest disconnect(unsigned short profile); + + // One device can only send one file at a time + [Creator, Throws] + DOMRequest sendFile(DOMString deviceAddress, Blob blob); + [Creator, Throws] + DOMRequest stopSendingFile(DOMString deviceAddress); + [Creator, Throws] + DOMRequest confirmReceivingFile(DOMString deviceAddress, boolean confirmation); + + // Connect/Disconnect SCO (audio) connection + [Creator, Throws] + DOMRequest connectSco(); + [Creator, Throws] + DOMRequest disconnectSco(); + [Creator, Throws] + DOMRequest isScoConnected(); + + // AVRCP 1.3 methods + [Creator,Throws] + DOMRequest sendMediaMetaData(optional MediaMetaData mediaMetaData); + [Creator,Throws] + DOMRequest sendMediaPlayStatus(optional MediaPlayStatus mediaPlayStatus); +}; diff --git a/dom/webidl/WebIDL.mk b/dom/webidl/WebIDL.mk index 9eb7158762d7..c89aafba4783 100644 --- a/dom/webidl/WebIDL.mk +++ b/dom/webidl/WebIDL.mk @@ -476,6 +476,7 @@ webidl_files += \ ifdef MOZ_B2G_BT webidl_files += \ + BluetoothAdapter.webidl \ BluetoothDeviceEvent.webidl \ BluetoothManager.webidl \ BluetoothStatusChangedEvent.webidl \