From 87b1c5736f239959c4d996e8509a6e203470f82a Mon Sep 17 00:00:00 2001 From: Thomas Zimmermann Date: Thu, 16 Apr 2015 10:48:21 +0200 Subject: [PATCH] Bug 1154235: Share BluetoothUtils.{cpp,h} between Bluetooth v1 and v2, r=btian With this patch, Bluetooth v1 and v2 share the same files for BluetoothUtils.{cpp,h}. Some of the functions are still version specific and cannot be shared. The v1 variants can be removed after switching to v2. --- CLOBBER | 6 +- dom/bluetooth/BluetoothUtils.cpp | 45 ++++- dom/bluetooth/BluetoothUtils.h | 14 ++ dom/bluetooth/bluetooth1/BluetoothUtils.cpp | 178 -------------------- dom/bluetooth/bluetooth1/BluetoothUtils.h | 44 ----- dom/bluetooth/moz.build | 5 +- 6 files changed, 65 insertions(+), 227 deletions(-) delete mode 100644 dom/bluetooth/bluetooth1/BluetoothUtils.cpp delete mode 100644 dom/bluetooth/bluetooth1/BluetoothUtils.h diff --git a/CLOBBER b/CLOBBER index cb3fc6c4c7c63..670615407d498 100644 --- a/CLOBBER +++ b/CLOBBER @@ -22,4 +22,8 @@ # changes to stick? As of bug 928195, this shouldn't be necessary! Please # don't change CLOBBER for WebIDL changes any more. -Bug 1153796: Merge Bluetooth backend interfaces +Bug 1154235: Merge BluetoothUtils.{cpp,h} variants into single file + +This patch set renames and removes source files. This requires updating +the build system's dependency information from scratch. The issue has +been reported in bug 1154232. diff --git a/dom/bluetooth/BluetoothUtils.cpp b/dom/bluetooth/BluetoothUtils.cpp index e1e0994b60157..be41257fa732a 100644 --- a/dom/bluetooth/BluetoothUtils.cpp +++ b/dom/bluetooth/BluetoothUtils.cpp @@ -98,7 +98,7 @@ GeneratePathFromGattId(const BluetoothGattId& aId, /** * |SetJsObject| is an internal function used by |BroadcastSystemMessage| only */ -bool +static bool SetJsObject(JSContext* aContext, const BluetoothValue& aValue, JS::Handle aObj) @@ -219,6 +219,7 @@ BroadcastSystemMessage(const nsAString& aType, return true; } +#ifdef MOZ_B2G_BT_API_V2 void DispatchReplySuccess(BluetoothReplyRunnable* aRunnable) { @@ -281,6 +282,48 @@ DispatchStatusChangedEvent(const nsAString& aType, NS_ENSURE_TRUE_VOID(bs); bs->DistributeSignal(aType, NS_LITERAL_STRING(KEY_ADAPTER), data); } +#else +// TODO: remove with bluetooth1 +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!"); + } +} + +// TODO: remove with bluetooth1 +void +DispatchStatusChangedEvent(const nsAString& aType, + const nsAString& aAddress, + bool aStatus) +{ + MOZ_ASSERT(NS_IsMainThread()); + + InfallibleTArray data; + BT_APPEND_NAMED_VALUE(data, "address", nsString(aAddress)); + BT_APPEND_NAMED_VALUE(data, "status", aStatus); + + BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data); + + BluetoothService* bs = BluetoothService::Get(); + NS_ENSURE_TRUE_VOID(bs); + bs->DistributeSignal(signal); +} +#endif bool IsMainProcess() diff --git a/dom/bluetooth/BluetoothUtils.h b/dom/bluetooth/BluetoothUtils.h index 9a0e24bbedef3..917a476eb1a3a 100644 --- a/dom/bluetooth/BluetoothUtils.h +++ b/dom/bluetooth/BluetoothUtils.h @@ -93,6 +93,7 @@ BroadcastSystemMessage(const nsAString& aType, // Dispatch bluetooth reply to main thread // +#ifdef MOZ_B2G_BT_API_V2 /** * Dispatch successful bluetooth reply with NO value to reply request. * @@ -142,6 +143,19 @@ DispatchReplyError(BluetoothReplyRunnable* aRunnable, void DispatchReplyError(BluetoothReplyRunnable* aRunnable, const enum BluetoothStatus aStatus); +#else +// TODO: remove with bluetooth1 +void +DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable, + const BluetoothValue& aValue, + const nsAString& aErrorStr); + +// TODO: remove with bluetooth1 +void +DispatchStatusChangedEvent(const nsAString& aType, + const nsAString& aDeviceAddress, + bool aStatus); +#endif void DispatchStatusChangedEvent(const nsAString& aType, diff --git a/dom/bluetooth/bluetooth1/BluetoothUtils.cpp b/dom/bluetooth/bluetooth1/BluetoothUtils.cpp deleted file mode 100644 index f4066c97bbada..0000000000000 --- a/dom/bluetooth/bluetooth1/BluetoothUtils.cpp +++ /dev/null @@ -1,178 +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 "BluetoothUtils.h" -#include "BluetoothReplyRunnable.h" -#include "BluetoothService.h" -#include "jsapi.h" -#include "mozilla/dom/bluetooth/BluetoothTypes.h" -#include "nsContentUtils.h" -#include "nsISystemMessagesInternal.h" -#include "nsServiceManagerUtils.h" - -BEGIN_BLUETOOTH_NAMESPACE - -bool -SetJsObject(JSContext* aContext, - const BluetoothValue& aValue, - JS::Handle aObj) -{ - MOZ_ASSERT(aContext && aObj); - - if (aValue.type() != BluetoothValue::TArrayOfBluetoothNamedValue) { - BT_WARNING("SetJsObject: Invalid parameter type"); - return false; - } - - const nsTArray& arr = - aValue.get_ArrayOfBluetoothNamedValue(); - - for (uint32_t i = 0; i < arr.Length(); i++) { - JS::Rooted 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 = STRING_TO_JSVAL(jsData); - break; - } - case BluetoothValue::Tuint32_t: - val = INT_TO_JSVAL(v.get_uint32_t()); - break; - case BluetoothValue::Tbool: - val = BOOLEAN_TO_JSVAL(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; - NS_ASSERTION(!::JS_IsExceptionPending(cx), - "Shouldn't get here when an exception is pending!"); - - nsCOMPtr systemMessenger = - do_GetService("@mozilla.org/system-message-internal;1"); - NS_ENSURE_TRUE(systemMessenger, false); - - JS::Rooted value(cx); - if (aData.type() == BluetoothValue::TnsString) { - JSString* jsData = JS_NewUCStringCopyN(cx, - aData.get_nsString().BeginReading(), - aData.get_nsString().Length()); - value = STRING_TO_JSVAL(jsData); - } else if (aData.type() == BluetoothValue::TArrayOfBluetoothNamedValue) { - JS::Rooted 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& aData) -{ - mozilla::AutoSafeJSContext cx; - NS_ASSERTION(!::JS_IsExceptionPending(cx), - "Shouldn't get here when an exception is pending!"); - - JS::Rooted 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 systemMessenger = - do_GetService("@mozilla.org/system-message-internal;1"); - NS_ENSURE_TRUE(systemMessenger, false); - - JS::Rooted value(cx, JS::ObjectValue(*obj)); - systemMessenger->BroadcastMessage(aType, value, - JS::UndefinedHandleValue); - - return true; -} - -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!"); - } -} - -void -DispatchStatusChangedEvent(const nsAString& aType, - const nsAString& aAddress, - bool aStatus) -{ - MOZ_ASSERT(NS_IsMainThread()); - - InfallibleTArray data; - BT_APPEND_NAMED_VALUE(data, "address", nsString(aAddress)); - BT_APPEND_NAMED_VALUE(data, "status", aStatus); - - BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data); - - BluetoothService* bs = BluetoothService::Get(); - NS_ENSURE_TRUE_VOID(bs); - bs->DistributeSignal(signal); -} - -END_BLUETOOTH_NAMESPACE diff --git a/dom/bluetooth/bluetooth1/BluetoothUtils.h b/dom/bluetooth/bluetooth1/BluetoothUtils.h deleted file mode 100644 index 41449762e5510..0000000000000 --- a/dom/bluetooth/bluetooth1/BluetoothUtils.h +++ /dev/null @@ -1,44 +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_bluetoothutils_h -#define mozilla_dom_bluetooth_bluetoothutils_h - -#include "BluetoothCommon.h" -#include "js/TypeDecls.h" - -BEGIN_BLUETOOTH_NAMESPACE - -class BluetoothNamedValue; -class BluetoothValue; -class BluetoothReplyRunnable; - -bool -SetJsObject(JSContext* aContext, - const BluetoothValue& aValue, - JS::Handle aObj); - -bool -BroadcastSystemMessage(const nsAString& aType, - const BluetoothValue& aData); - -bool -BroadcastSystemMessage(const nsAString& aType, - const InfallibleTArray& aData); - -void -DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable, - const BluetoothValue& aValue, - const nsAString& aErrorStr); - -void -DispatchStatusChangedEvent(const nsAString& aType, - const nsAString& aDeviceAddress, - bool aStatus); - -END_BLUETOOTH_NAMESPACE - -#endif diff --git a/dom/bluetooth/moz.build b/dom/bluetooth/moz.build index 6a75242a50f60..16e4c5976a256 100644 --- a/dom/bluetooth/moz.build +++ b/dom/bluetooth/moz.build @@ -12,7 +12,8 @@ if CONFIG['MOZ_B2G_BT']: SOURCES += [ 'BluetoothInterface.cpp', - 'BluetoothInterfaceHelpers.cpp' + 'BluetoothInterfaceHelpers.cpp', + 'BluetoothUtils.cpp' ] if CONFIG['MOZ_B2G_BT_API_V2']: @@ -32,7 +33,6 @@ if CONFIG['MOZ_B2G_BT']: 'bluetooth2/BluetoothProfileController.cpp', 'bluetooth2/BluetoothReplyRunnable.cpp', 'bluetooth2/BluetoothService.cpp', - 'bluetooth2/BluetoothUtils.cpp', 'bluetooth2/BluetoothUuid.cpp', 'bluetooth2/ipc/BluetoothChild.cpp', 'bluetooth2/ipc/BluetoothParent.cpp', @@ -58,7 +58,6 @@ if CONFIG['MOZ_B2G_BT']: 'bluetooth1/BluetoothPropertyContainer.cpp', 'bluetooth1/BluetoothReplyRunnable.cpp', 'bluetooth1/BluetoothService.cpp', - 'bluetooth1/BluetoothUtils.cpp', 'bluetooth1/BluetoothUuid.cpp', 'bluetooth1/ipc/BluetoothChild.cpp', 'bluetooth1/ipc/BluetoothParent.cpp',