michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: #include "BluetoothReplyRunnable.h" michael@0: #include "BluetoothService.h" michael@0: #include "BluetoothUtils.h" michael@0: #include "jsapi.h" michael@0: #include "mozilla/Scoped.h" michael@0: #include "mozilla/dom/bluetooth/BluetoothTypes.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsCxPusher.h" michael@0: #include "nsIScriptContext.h" michael@0: #include "nsISystemMessagesInternal.h" michael@0: #include "nsString.h" michael@0: #include "nsTArray.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: michael@0: BEGIN_BLUETOOTH_NAMESPACE michael@0: michael@0: bool michael@0: SetJsObject(JSContext* aContext, michael@0: const BluetoothValue& aValue, michael@0: JS::Handle aObj) michael@0: { michael@0: MOZ_ASSERT(aContext && aObj); michael@0: michael@0: if (aValue.type() != BluetoothValue::TArrayOfBluetoothNamedValue) { michael@0: BT_WARNING("SetJsObject: Invalid parameter type"); michael@0: return false; michael@0: } michael@0: michael@0: const nsTArray& arr = michael@0: aValue.get_ArrayOfBluetoothNamedValue(); michael@0: michael@0: for (uint32_t i = 0; i < arr.Length(); i++) { michael@0: JS::Rooted val(aContext); michael@0: const BluetoothValue& v = arr[i].value(); michael@0: michael@0: switch(v.type()) { michael@0: case BluetoothValue::TnsString: { michael@0: JSString* jsData = JS_NewUCStringCopyN(aContext, michael@0: v.get_nsString().BeginReading(), michael@0: v.get_nsString().Length()); michael@0: NS_ENSURE_TRUE(jsData, false); michael@0: val = STRING_TO_JSVAL(jsData); michael@0: break; michael@0: } michael@0: case BluetoothValue::Tuint32_t: michael@0: val = INT_TO_JSVAL(v.get_uint32_t()); michael@0: break; michael@0: case BluetoothValue::Tbool: michael@0: val = BOOLEAN_TO_JSVAL(v.get_bool()); michael@0: break; michael@0: default: michael@0: BT_WARNING("SetJsObject: Parameter is not handled"); michael@0: break; michael@0: } michael@0: michael@0: if (!JS_SetProperty(aContext, aObj, michael@0: NS_ConvertUTF16toUTF8(arr[i].name()).get(), michael@0: val)) { michael@0: BT_WARNING("Failed to set property"); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: nsString michael@0: GetObjectPathFromAddress(const nsAString& aAdapterPath, michael@0: const nsAString& aDeviceAddress) michael@0: { michael@0: // The object path would be like /org/bluez/2906/hci0/dev_00_23_7F_CB_B4_F1, michael@0: // and the adapter path would be the first part of the object path, according michael@0: // to the example above, it's /org/bluez/2906/hci0. michael@0: nsString devicePath(aAdapterPath); michael@0: devicePath.AppendLiteral("/dev_"); michael@0: devicePath.Append(aDeviceAddress); michael@0: devicePath.ReplaceChar(':', '_'); michael@0: return devicePath; michael@0: } michael@0: michael@0: nsString michael@0: GetAddressFromObjectPath(const nsAString& aObjectPath) michael@0: { michael@0: // The object path would be like /org/bluez/2906/hci0/dev_00_23_7F_CB_B4_F1, michael@0: // and the adapter path would be the first part of the object path, according michael@0: // to the example above, it's /org/bluez/2906/hci0. michael@0: nsString address(aObjectPath); michael@0: int addressHead = address.RFind("/") + 5; michael@0: michael@0: MOZ_ASSERT(addressHead + BLUETOOTH_ADDRESS_LENGTH == (int)address.Length()); michael@0: michael@0: address.Cut(0, addressHead); michael@0: address.ReplaceChar('_', ':'); michael@0: michael@0: return address; michael@0: } michael@0: michael@0: bool michael@0: BroadcastSystemMessage(const nsAString& aType, michael@0: const InfallibleTArray& aData) michael@0: { michael@0: mozilla::AutoSafeJSContext cx; michael@0: NS_ASSERTION(!::JS_IsExceptionPending(cx), michael@0: "Shouldn't get here when an exception is pending!"); michael@0: michael@0: JS::Rooted obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), michael@0: JS::NullPtr())); michael@0: if (!obj) { michael@0: BT_WARNING("Failed to new JSObject for system message!"); michael@0: return false; michael@0: } michael@0: michael@0: if (!SetJsObject(cx, aData, obj)) { michael@0: BT_WARNING("Failed to set properties of system message!"); michael@0: return false; michael@0: } michael@0: michael@0: nsCOMPtr systemMessenger = michael@0: do_GetService("@mozilla.org/system-message-internal;1"); michael@0: NS_ENSURE_TRUE(systemMessenger, false); michael@0: michael@0: JS::Rooted value(cx, JS::ObjectValue(*obj)); michael@0: systemMessenger->BroadcastMessage(aType, value, michael@0: JS::UndefinedHandleValue); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable, michael@0: const BluetoothValue& aValue, michael@0: const nsAString& aErrorStr) michael@0: { michael@0: // Reply will be deleted by the runnable after running on main thread michael@0: BluetoothReply* reply; michael@0: if (!aErrorStr.IsEmpty()) { michael@0: nsString err(aErrorStr); michael@0: reply = new BluetoothReply(BluetoothReplyError(err)); michael@0: } else { michael@0: MOZ_ASSERT(aValue.type() != BluetoothValue::T__None); michael@0: reply = new BluetoothReply(BluetoothReplySuccess(aValue)); michael@0: } michael@0: michael@0: aRunnable->SetReply(reply); michael@0: if (NS_FAILED(NS_DispatchToMainThread(aRunnable))) { michael@0: BT_WARNING("Failed to dispatch to main thread!"); michael@0: } michael@0: } michael@0: michael@0: void michael@0: ParseAtCommand(const nsACString& aAtCommand, const int aStart, michael@0: nsTArray& aRetValues) michael@0: { michael@0: int length = aAtCommand.Length(); michael@0: int begin = aStart; michael@0: michael@0: for (int i = aStart; i < length; ++i) { michael@0: // Use ',' as separator michael@0: if (aAtCommand[i] == ',') { michael@0: nsCString tmp(nsDependentCSubstring(aAtCommand, begin, i - begin)); michael@0: aRetValues.AppendElement(tmp); michael@0: michael@0: begin = i + 1; michael@0: } michael@0: } michael@0: michael@0: nsCString tmp(nsDependentCSubstring(aAtCommand, begin)); michael@0: aRetValues.AppendElement(tmp); michael@0: } michael@0: michael@0: void michael@0: DispatchStatusChangedEvent(const nsAString& aType, michael@0: const nsAString& aAddress, michael@0: bool aStatus) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: InfallibleTArray data; michael@0: data.AppendElement( michael@0: BluetoothNamedValue(NS_LITERAL_STRING("address"), nsString(aAddress))); michael@0: data.AppendElement( michael@0: BluetoothNamedValue(NS_LITERAL_STRING("status"), aStatus)); michael@0: michael@0: BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data); michael@0: michael@0: BluetoothService* bs = BluetoothService::Get(); michael@0: NS_ENSURE_TRUE_VOID(bs); michael@0: bs->DistributeSignal(signal); michael@0: } michael@0: michael@0: END_BLUETOOTH_NAMESPACE