Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "base/basictypes.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "BluetoothReplyRunnable.h" |
michael@0 | 10 | #include "BluetoothService.h" |
michael@0 | 11 | #include "BluetoothUtils.h" |
michael@0 | 12 | #include "jsapi.h" |
michael@0 | 13 | #include "mozilla/Scoped.h" |
michael@0 | 14 | #include "mozilla/dom/bluetooth/BluetoothTypes.h" |
michael@0 | 15 | #include "nsContentUtils.h" |
michael@0 | 16 | #include "nsCxPusher.h" |
michael@0 | 17 | #include "nsIScriptContext.h" |
michael@0 | 18 | #include "nsISystemMessagesInternal.h" |
michael@0 | 19 | #include "nsString.h" |
michael@0 | 20 | #include "nsTArray.h" |
michael@0 | 21 | #include "nsServiceManagerUtils.h" |
michael@0 | 22 | |
michael@0 | 23 | BEGIN_BLUETOOTH_NAMESPACE |
michael@0 | 24 | |
michael@0 | 25 | bool |
michael@0 | 26 | SetJsObject(JSContext* aContext, |
michael@0 | 27 | const BluetoothValue& aValue, |
michael@0 | 28 | JS::Handle<JSObject*> aObj) |
michael@0 | 29 | { |
michael@0 | 30 | MOZ_ASSERT(aContext && aObj); |
michael@0 | 31 | |
michael@0 | 32 | if (aValue.type() != BluetoothValue::TArrayOfBluetoothNamedValue) { |
michael@0 | 33 | BT_WARNING("SetJsObject: Invalid parameter type"); |
michael@0 | 34 | return false; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | const nsTArray<BluetoothNamedValue>& arr = |
michael@0 | 38 | aValue.get_ArrayOfBluetoothNamedValue(); |
michael@0 | 39 | |
michael@0 | 40 | for (uint32_t i = 0; i < arr.Length(); i++) { |
michael@0 | 41 | JS::Rooted<JS::Value> val(aContext); |
michael@0 | 42 | const BluetoothValue& v = arr[i].value(); |
michael@0 | 43 | |
michael@0 | 44 | switch(v.type()) { |
michael@0 | 45 | case BluetoothValue::TnsString: { |
michael@0 | 46 | JSString* jsData = JS_NewUCStringCopyN(aContext, |
michael@0 | 47 | v.get_nsString().BeginReading(), |
michael@0 | 48 | v.get_nsString().Length()); |
michael@0 | 49 | NS_ENSURE_TRUE(jsData, false); |
michael@0 | 50 | val = STRING_TO_JSVAL(jsData); |
michael@0 | 51 | break; |
michael@0 | 52 | } |
michael@0 | 53 | case BluetoothValue::Tuint32_t: |
michael@0 | 54 | val = INT_TO_JSVAL(v.get_uint32_t()); |
michael@0 | 55 | break; |
michael@0 | 56 | case BluetoothValue::Tbool: |
michael@0 | 57 | val = BOOLEAN_TO_JSVAL(v.get_bool()); |
michael@0 | 58 | break; |
michael@0 | 59 | default: |
michael@0 | 60 | BT_WARNING("SetJsObject: Parameter is not handled"); |
michael@0 | 61 | break; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | if (!JS_SetProperty(aContext, aObj, |
michael@0 | 65 | NS_ConvertUTF16toUTF8(arr[i].name()).get(), |
michael@0 | 66 | val)) { |
michael@0 | 67 | BT_WARNING("Failed to set property"); |
michael@0 | 68 | return false; |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | return true; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | nsString |
michael@0 | 76 | GetObjectPathFromAddress(const nsAString& aAdapterPath, |
michael@0 | 77 | const nsAString& aDeviceAddress) |
michael@0 | 78 | { |
michael@0 | 79 | // The object path would be like /org/bluez/2906/hci0/dev_00_23_7F_CB_B4_F1, |
michael@0 | 80 | // and the adapter path would be the first part of the object path, according |
michael@0 | 81 | // to the example above, it's /org/bluez/2906/hci0. |
michael@0 | 82 | nsString devicePath(aAdapterPath); |
michael@0 | 83 | devicePath.AppendLiteral("/dev_"); |
michael@0 | 84 | devicePath.Append(aDeviceAddress); |
michael@0 | 85 | devicePath.ReplaceChar(':', '_'); |
michael@0 | 86 | return devicePath; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | nsString |
michael@0 | 90 | GetAddressFromObjectPath(const nsAString& aObjectPath) |
michael@0 | 91 | { |
michael@0 | 92 | // The object path would be like /org/bluez/2906/hci0/dev_00_23_7F_CB_B4_F1, |
michael@0 | 93 | // and the adapter path would be the first part of the object path, according |
michael@0 | 94 | // to the example above, it's /org/bluez/2906/hci0. |
michael@0 | 95 | nsString address(aObjectPath); |
michael@0 | 96 | int addressHead = address.RFind("/") + 5; |
michael@0 | 97 | |
michael@0 | 98 | MOZ_ASSERT(addressHead + BLUETOOTH_ADDRESS_LENGTH == (int)address.Length()); |
michael@0 | 99 | |
michael@0 | 100 | address.Cut(0, addressHead); |
michael@0 | 101 | address.ReplaceChar('_', ':'); |
michael@0 | 102 | |
michael@0 | 103 | return address; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | bool |
michael@0 | 107 | BroadcastSystemMessage(const nsAString& aType, |
michael@0 | 108 | const InfallibleTArray<BluetoothNamedValue>& aData) |
michael@0 | 109 | { |
michael@0 | 110 | mozilla::AutoSafeJSContext cx; |
michael@0 | 111 | NS_ASSERTION(!::JS_IsExceptionPending(cx), |
michael@0 | 112 | "Shouldn't get here when an exception is pending!"); |
michael@0 | 113 | |
michael@0 | 114 | JS::Rooted<JSObject*> obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), |
michael@0 | 115 | JS::NullPtr())); |
michael@0 | 116 | if (!obj) { |
michael@0 | 117 | BT_WARNING("Failed to new JSObject for system message!"); |
michael@0 | 118 | return false; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | if (!SetJsObject(cx, aData, obj)) { |
michael@0 | 122 | BT_WARNING("Failed to set properties of system message!"); |
michael@0 | 123 | return false; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | nsCOMPtr<nsISystemMessagesInternal> systemMessenger = |
michael@0 | 127 | do_GetService("@mozilla.org/system-message-internal;1"); |
michael@0 | 128 | NS_ENSURE_TRUE(systemMessenger, false); |
michael@0 | 129 | |
michael@0 | 130 | JS::Rooted<JS::Value> value(cx, JS::ObjectValue(*obj)); |
michael@0 | 131 | systemMessenger->BroadcastMessage(aType, value, |
michael@0 | 132 | JS::UndefinedHandleValue); |
michael@0 | 133 | |
michael@0 | 134 | return true; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | void |
michael@0 | 138 | DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable, |
michael@0 | 139 | const BluetoothValue& aValue, |
michael@0 | 140 | const nsAString& aErrorStr) |
michael@0 | 141 | { |
michael@0 | 142 | // Reply will be deleted by the runnable after running on main thread |
michael@0 | 143 | BluetoothReply* reply; |
michael@0 | 144 | if (!aErrorStr.IsEmpty()) { |
michael@0 | 145 | nsString err(aErrorStr); |
michael@0 | 146 | reply = new BluetoothReply(BluetoothReplyError(err)); |
michael@0 | 147 | } else { |
michael@0 | 148 | MOZ_ASSERT(aValue.type() != BluetoothValue::T__None); |
michael@0 | 149 | reply = new BluetoothReply(BluetoothReplySuccess(aValue)); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | aRunnable->SetReply(reply); |
michael@0 | 153 | if (NS_FAILED(NS_DispatchToMainThread(aRunnable))) { |
michael@0 | 154 | BT_WARNING("Failed to dispatch to main thread!"); |
michael@0 | 155 | } |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | void |
michael@0 | 159 | ParseAtCommand(const nsACString& aAtCommand, const int aStart, |
michael@0 | 160 | nsTArray<nsCString>& aRetValues) |
michael@0 | 161 | { |
michael@0 | 162 | int length = aAtCommand.Length(); |
michael@0 | 163 | int begin = aStart; |
michael@0 | 164 | |
michael@0 | 165 | for (int i = aStart; i < length; ++i) { |
michael@0 | 166 | // Use ',' as separator |
michael@0 | 167 | if (aAtCommand[i] == ',') { |
michael@0 | 168 | nsCString tmp(nsDependentCSubstring(aAtCommand, begin, i - begin)); |
michael@0 | 169 | aRetValues.AppendElement(tmp); |
michael@0 | 170 | |
michael@0 | 171 | begin = i + 1; |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | nsCString tmp(nsDependentCSubstring(aAtCommand, begin)); |
michael@0 | 176 | aRetValues.AppendElement(tmp); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | void |
michael@0 | 180 | DispatchStatusChangedEvent(const nsAString& aType, |
michael@0 | 181 | const nsAString& aAddress, |
michael@0 | 182 | bool aStatus) |
michael@0 | 183 | { |
michael@0 | 184 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 185 | |
michael@0 | 186 | InfallibleTArray<BluetoothNamedValue> data; |
michael@0 | 187 | data.AppendElement( |
michael@0 | 188 | BluetoothNamedValue(NS_LITERAL_STRING("address"), nsString(aAddress))); |
michael@0 | 189 | data.AppendElement( |
michael@0 | 190 | BluetoothNamedValue(NS_LITERAL_STRING("status"), aStatus)); |
michael@0 | 191 | |
michael@0 | 192 | BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data); |
michael@0 | 193 | |
michael@0 | 194 | BluetoothService* bs = BluetoothService::Get(); |
michael@0 | 195 | NS_ENSURE_TRUE_VOID(bs); |
michael@0 | 196 | bs->DistributeSignal(signal); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | END_BLUETOOTH_NAMESPACE |