diff --git a/dom/bluetooth/BluetoothManager.cpp b/dom/bluetooth/BluetoothManager.cpp index f5dd76f7d584..b5923a0a87c7 100644 --- a/dom/bluetooth/BluetoothManager.cpp +++ b/dom/bluetooth/BluetoothManager.cpp @@ -11,13 +11,11 @@ #include "BluetoothService.h" #include "BluetoothReplyRunnable.h" +#include "DOMRequest.h" #include "nsContentUtils.h" #include "nsDOMClassInfo.h" -#include "nsDOMEvent.h" -#include "nsIDOMDOMRequest.h" #include "nsIPermissionManager.h" #include "nsThreadUtils.h" -#include "nsXPCOMCIDInternal.h" #include "mozilla/Util.h" #include "mozilla/dom/bluetooth/BluetoothTypes.h" @@ -58,12 +56,15 @@ public: bool ParseSuccessfulReply(jsval* aValue) { - nsCOMPtr adapter; *aValue = JSVAL_VOID; - const InfallibleTArray& v = - mReply->get_BluetoothReplySuccess().value().get_ArrayOfBluetoothNamedValue(); - adapter = BluetoothAdapter::Create(mManagerPtr->GetOwner(), v); + const BluetoothValue& v = mReply->get_BluetoothReplySuccess().value(); + + MOZ_ASSERT(v.type() == BluetoothValue::TArrayOfBluetoothNamedValue); + const InfallibleTArray& values = + v.get_ArrayOfBluetoothNamedValue(); + nsCOMPtr adapter; + adapter = BluetoothAdapter::Create(mManagerPtr->GetOwner(), values); nsresult rv; nsIScriptContext* sc = mManagerPtr->GetContextForEventHandlers(&rv); @@ -77,13 +78,13 @@ public: sc->GetNativeGlobal(), adapter, aValue); - bool result = NS_SUCCEEDED(rv); - if (!result) { + if (NS_FAILED(rv)) { NS_WARNING("Cannot create native object!"); SetError(NS_LITERAL_STRING("BluetoothNativeObjectError")); + return false; } - return result; + return true; } void @@ -97,33 +98,6 @@ private: nsRefPtr mManagerPtr; }; -class ToggleBtResultTask : public nsRunnable -{ -public: - ToggleBtResultTask(BluetoothManager* aManager, bool aEnabled) - : mManagerPtr(aManager), - mEnabled(aEnabled) - { - } - - NS_IMETHOD Run() - { - MOZ_ASSERT(NS_IsMainThread()); - - mManagerPtr->FireEnabledDisabledEvent(mEnabled); - - // mManagerPtr must be null before returning to prevent the background - // thread from racing to release it during the destruction of this runnable. - mManagerPtr = nullptr; - - return NS_OK; - } - -private: - nsRefPtr mManagerPtr; - bool mEnabled; -}; - nsresult BluetoothManager::FireEnabledDisabledEvent(bool aEnabled) { @@ -164,10 +138,7 @@ NS_IMETHODIMP BluetoothManager::GetEnabled(bool* aEnabled) { BluetoothService* bs = BluetoothService::Get(); - if (!bs) { - NS_WARNING("BluetoothService not available!"); - return NS_ERROR_FAILURE; - } + NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); *aEnabled = bs->IsEnabled(); return NS_OK; @@ -176,31 +147,22 @@ BluetoothManager::GetEnabled(bool* aEnabled) NS_IMETHODIMP BluetoothManager::GetDefaultAdapter(nsIDOMDOMRequest** aAdapter) { - BluetoothService* bs = BluetoothService::Get(); - if (!bs) { - NS_WARNING("BluetoothService not available!"); - return NS_ERROR_FAILURE; - } - - nsCOMPtr rs = do_GetService("@mozilla.org/dom/dom-request-service;1"); - - if (!rs) { - NS_WARNING("No DOMRequest Service!"); - return NS_ERROR_FAILURE; - } + nsCOMPtr rs = + do_GetService(DOMREQUEST_SERVICE_CONTRACTID); + NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); nsCOMPtr request; nsresult rv = rs->CreateRequest(GetOwner(), getter_AddRefs(request)); - if (NS_FAILED(rv)) { - NS_WARNING("Can't create DOMRequest!"); - return NS_ERROR_FAILURE; - } + NS_ENSURE_SUCCESS(rv, rv); nsRefPtr results = new GetAdapterTask(this, request); + BluetoothService* bs = BluetoothService::Get(); + NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE); if (NS_FAILED(bs->GetDefaultAdapterPathInternal(results))) { return NS_ERROR_FAILURE; } + request.forget(aAdapter); return NS_OK; } @@ -212,14 +174,10 @@ BluetoothManager::Create(nsPIDOMWindow* aWindow) MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(aWindow); - BluetoothService* bs = BluetoothService::Get(); - if (!bs) { - NS_WARNING("BluetoothService not available!"); - return nullptr; - } - nsRefPtr manager = new BluetoothManager(aWindow); + BluetoothService* bs = BluetoothService::Get(); + NS_ENSURE_TRUE(bs, nullptr); bs->RegisterManager(manager); return manager.forget(); diff --git a/dom/bluetooth/BluetoothService.cpp b/dom/bluetooth/BluetoothService.cpp index 93bbd1077be3..5e420af4b98e 100644 --- a/dom/bluetooth/BluetoothService.cpp +++ b/dom/bluetooth/BluetoothService.cpp @@ -12,6 +12,7 @@ #include "BluetoothParent.h" #include "BluetoothReplyRunnable.h" #include "BluetoothServiceChildProcess.h" +#include "BluetoothUtils.h" #include "jsapi.h" #include "mozilla/Services.h" @@ -22,15 +23,12 @@ #include "mozilla/dom/bluetooth/BluetoothTypes.h" #include "mozilla/ipc/UnixSocket.h" #include "nsContentUtils.h" -#include "nsIDOMDOMRequest.h" #include "nsIObserverService.h" #include "nsISettingsService.h" #include "nsISystemMessagesInternal.h" #include "nsITimer.h" #include "nsThreadUtils.h" #include "nsXPCOM.h" -#include "nsXPCOMCIDInternal.h" -#include "nsXULAppAPI.h" #if defined(MOZ_B2G_BT) # if defined(MOZ_BLUETOOTH_GONK) @@ -63,15 +61,6 @@ IsMainProcess() return XRE_GetProcessType() == GeckoProcessType_Default; } -PLDHashOperator -RemoveAllSignalHandlers(const nsAString& aKey, - nsAutoPtr& aData, - void* aUserArg) -{ - aData->RemoveObserver(static_cast(aUserArg)); - return aData->Length() ? PL_DHASH_NEXT : PL_DHASH_REMOVE; -} - void ShutdownTimeExceeded(nsITimer* aTimer, void* aClosure) { @@ -269,8 +258,6 @@ BluetoothService::Init() return false; } - mRegisteredForLocalAgent = true; - return true; } @@ -279,11 +266,6 @@ BluetoothService::Cleanup() { MOZ_ASSERT(NS_IsMainThread()); - if (mRegisteredForLocalAgent) { - UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(LOCAL_AGENT_PATH), this); - UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(REMOTE_AGENT_PATH), this); - mRegisteredForLocalAgent = false; - } nsCOMPtr obs = services::GetObserverService(); if (obs && @@ -333,14 +315,22 @@ BluetoothService::UnregisterAllSignalHandlers(BluetoothSignalObserver* aHandler) MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(aHandler); - mBluetoothSignalObserverTable.Enumerate(RemoveAllSignalHandlers, aHandler); + mBluetoothSignalObserverTable.Clear(); } void BluetoothService::DistributeSignal(const BluetoothSignal& aSignal) { MOZ_ASSERT(NS_IsMainThread()); - // Notify observers that a message has been sent + + if (aSignal.path().EqualsLiteral(LOCAL_AGENT_PATH)) { + Notify(aSignal); + return; + } else if (aSignal.path().EqualsLiteral(REMOTE_AGENT_PATH)) { + Notify(aSignal); + return; + } + BluetoothSignalObserverList* ol; if (!mBluetoothSignalObserverTable.Get(aSignal.path(), &ol)) { #if DEBUG @@ -383,11 +373,6 @@ BluetoothService::StartStopBluetooth(bool aStart) NS_ENSURE_SUCCESS(rv, rv); } - if (aStart) { - RegisterBluetoothSignalHandler(NS_LITERAL_STRING(LOCAL_AGENT_PATH), this); - RegisterBluetoothSignalHandler(NS_LITERAL_STRING(REMOTE_AGENT_PATH), this); - } - nsCOMPtr runnable = new ToggleBtTask(aStart); rv = mBluetoothCommandThread->Dispatch(runnable, NS_DISPATCH_NORMAL); NS_ENSURE_SUCCESS(rv, rv); @@ -419,12 +404,9 @@ BluetoothService::SetEnabled(bool aEnabled) BluetoothSignalObserverList* ol; nsString managerPath = NS_LITERAL_STRING("/"); - // Skip when BluetoothManager has been registered in constructor // Re-register here after toggling due to table mBluetoothSignalObserverTable was cleared - if (!mBluetoothSignalObserverTable.Get(managerPath, &ol)) { - while (iter.HasMore()) { - RegisterBluetoothSignalHandler(managerPath, (BluetoothSignalObserver*)iter.GetNext()); - } + while (iter.HasMore()) { + RegisterBluetoothSignalHandler(managerPath, (BluetoothSignalObserver*)iter.GetNext()); } } else { mBluetoothSignalObserverTable.Clear(); @@ -636,7 +618,6 @@ BluetoothService::RegisterManager(BluetoothManager* aManager) MOZ_ASSERT(!mLiveManagers.Contains(aManager)); mLiveManagers.AppendElement(aManager); - RegisterBluetoothSignalHandler(aManager->GetPath(), aManager); } void @@ -646,7 +627,6 @@ BluetoothService::UnregisterManager(BluetoothManager* aManager) MOZ_ASSERT(aManager); MOZ_ASSERT(mLiveManagers.Contains(aManager)); - UnregisterBluetoothSignalHandler(aManager->GetPath(), aManager); mLiveManagers.RemoveElement(aManager); } @@ -702,39 +682,6 @@ BluetoothService::Observe(nsISupports* aSubject, const char* aTopic, return NS_ERROR_UNEXPECTED; } -bool -SetJsObject(JSContext* aContext, - JSObject* aObj, - const InfallibleTArray& aData) -{ - for (uint32_t i = 0; i < aData.Length(); i++) { - jsval v; - if (aData[i].value().type() == BluetoothValue::TnsString) { - nsString data = aData[i].value().get_nsString(); - JSString* JsData = JS_NewStringCopyN(aContext, - NS_ConvertUTF16toUTF8(data).get(), - data.Length()); - NS_ENSURE_TRUE(JsData, false); - v = STRING_TO_JSVAL(JsData); - } else if (aData[i].value().type() == BluetoothValue::Tuint32_t) { - int data = aData[i].value().get_uint32_t(); - v = INT_TO_JSVAL(data); - } else if (aData[i].value().type() == BluetoothValue::Tbool) { - bool data = aData[i].value().get_bool(); - v = BOOLEAN_TO_JSVAL(data); - } else { - NS_WARNING("SetJsObject: Parameter is not handled"); - } - - if (!JS_SetProperty(aContext, aObj, - NS_ConvertUTF16toUTF8(aData[i].name()).get(), - &v)) { - return false; - } - } - return true; -} - void BluetoothService::Notify(const BluetoothSignal& aData) { @@ -787,10 +734,7 @@ BluetoothService::Notify(const BluetoothSignal& aData) nsCOMPtr systemMessenger = do_GetService("@mozilla.org/system-message-internal;1"); + NS_ENSURE_TRUE_VOID(systemMessenger); - if (!systemMessenger) { - NS_WARNING("Failed to get SystemMessenger service!"); - return; - } systemMessenger->BroadcastMessage(type, OBJECT_TO_JSVAL(obj)); } diff --git a/dom/bluetooth/BluetoothService.h b/dom/bluetooth/BluetoothService.h index 9cd50a381df1..cb06e71158d7 100644 --- a/dom/bluetooth/BluetoothService.h +++ b/dom/bluetooth/BluetoothService.h @@ -305,8 +305,8 @@ public: protected: BluetoothService() - : mEnabled(false), mSettingsCheckInProgress(false), - mRegisteredForLocalAgent(false) + : mEnabled(false) + , mSettingsCheckInProgress(false) #ifdef DEBUG , mLastRequestedEnable(false) #endif @@ -399,7 +399,6 @@ protected: bool mEnabled; bool mSettingsCheckInProgress; - bool mRegisteredForLocalAgent; #ifdef DEBUG bool mLastRequestedEnable;