Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 "BluetoothServiceBluedroid.h" |
michael@0 | 12 | #include "BluetoothUtils.h" |
michael@0 | 13 | #include "jsapi.h" |
michael@0 | 14 | #include "mozilla/Scoped.h" |
michael@0 | 15 | #include "mozilla/dom/bluetooth/BluetoothTypes.h" |
michael@0 | 16 | #include "nsContentUtils.h" |
michael@0 | 17 | #include "nsCxPusher.h" |
michael@0 | 18 | #include "nsIScriptContext.h" |
michael@0 | 19 | #include "nsISystemMessagesInternal.h" |
michael@0 | 20 | #include "nsString.h" |
michael@0 | 21 | #include "nsTArray.h" |
michael@0 | 22 | #include "nsServiceManagerUtils.h" |
michael@0 | 23 | |
michael@0 | 24 | BEGIN_BLUETOOTH_NAMESPACE |
michael@0 | 25 | |
michael@0 | 26 | const bt_interface_t* |
michael@0 | 27 | GetBluetoothInterface() |
michael@0 | 28 | { |
michael@0 | 29 | return BluetoothServiceBluedroid::GetBluetoothInterface(); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | void |
michael@0 | 33 | StringToBdAddressType(const nsAString& aBdAddress, |
michael@0 | 34 | bt_bdaddr_t *aRetBdAddressType) |
michael@0 | 35 | { |
michael@0 | 36 | NS_ConvertUTF16toUTF8 bdAddressUTF8(aBdAddress); |
michael@0 | 37 | const char* str = bdAddressUTF8.get(); |
michael@0 | 38 | |
michael@0 | 39 | for (int i = 0; i < 6; i++) { |
michael@0 | 40 | aRetBdAddressType->address[i] = (uint8_t) strtoul(str, (char **)&str, 16); |
michael@0 | 41 | str++; |
michael@0 | 42 | } |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | void |
michael@0 | 46 | BdAddressTypeToString(bt_bdaddr_t* aBdAddressType, nsAString& aRetBdAddress) |
michael@0 | 47 | { |
michael@0 | 48 | uint8_t* addr = aBdAddressType->address; |
michael@0 | 49 | char bdstr[18]; |
michael@0 | 50 | |
michael@0 | 51 | sprintf(bdstr, "%02x:%02x:%02x:%02x:%02x:%02x", |
michael@0 | 52 | (int)addr[0],(int)addr[1],(int)addr[2], |
michael@0 | 53 | (int)addr[3],(int)addr[4],(int)addr[5]); |
michael@0 | 54 | |
michael@0 | 55 | aRetBdAddress = NS_ConvertUTF8toUTF16(bdstr); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | bool |
michael@0 | 59 | SetJsObject(JSContext* aContext, |
michael@0 | 60 | const BluetoothValue& aValue, |
michael@0 | 61 | JS::Handle<JSObject*> aObj) |
michael@0 | 62 | { |
michael@0 | 63 | MOZ_ASSERT(aContext && aObj); |
michael@0 | 64 | |
michael@0 | 65 | if (aValue.type() != BluetoothValue::TArrayOfBluetoothNamedValue) { |
michael@0 | 66 | BT_WARNING("SetJsObject: Invalid parameter type"); |
michael@0 | 67 | return false; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | const nsTArray<BluetoothNamedValue>& arr = |
michael@0 | 71 | aValue.get_ArrayOfBluetoothNamedValue(); |
michael@0 | 72 | |
michael@0 | 73 | for (uint32_t i = 0; i < arr.Length(); i++) { |
michael@0 | 74 | JS::Rooted<JS::Value> val(aContext); |
michael@0 | 75 | const BluetoothValue& v = arr[i].value(); |
michael@0 | 76 | |
michael@0 | 77 | switch(v.type()) { |
michael@0 | 78 | case BluetoothValue::TnsString: { |
michael@0 | 79 | JSString* jsData = JS_NewUCStringCopyN(aContext, |
michael@0 | 80 | v.get_nsString().BeginReading(), |
michael@0 | 81 | v.get_nsString().Length()); |
michael@0 | 82 | NS_ENSURE_TRUE(jsData, false); |
michael@0 | 83 | val = STRING_TO_JSVAL(jsData); |
michael@0 | 84 | break; |
michael@0 | 85 | } |
michael@0 | 86 | case BluetoothValue::Tuint32_t: |
michael@0 | 87 | val = INT_TO_JSVAL(v.get_uint32_t()); |
michael@0 | 88 | break; |
michael@0 | 89 | case BluetoothValue::Tbool: |
michael@0 | 90 | val = BOOLEAN_TO_JSVAL(v.get_bool()); |
michael@0 | 91 | break; |
michael@0 | 92 | default: |
michael@0 | 93 | BT_WARNING("SetJsObject: Parameter is not handled"); |
michael@0 | 94 | break; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | if (!JS_SetProperty(aContext, aObj, |
michael@0 | 98 | NS_ConvertUTF16toUTF8(arr[i].name()).get(), |
michael@0 | 99 | val)) { |
michael@0 | 100 | BT_WARNING("Failed to set property"); |
michael@0 | 101 | return false; |
michael@0 | 102 | } |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | return true; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | bool |
michael@0 | 109 | BroadcastSystemMessage(const nsAString& aType, |
michael@0 | 110 | const BluetoothValue& aData) |
michael@0 | 111 | { |
michael@0 | 112 | mozilla::AutoSafeJSContext cx; |
michael@0 | 113 | NS_ASSERTION(!::JS_IsExceptionPending(cx), |
michael@0 | 114 | "Shouldn't get here when an exception is pending!"); |
michael@0 | 115 | |
michael@0 | 116 | nsCOMPtr<nsISystemMessagesInternal> systemMessenger = |
michael@0 | 117 | do_GetService("@mozilla.org/system-message-internal;1"); |
michael@0 | 118 | NS_ENSURE_TRUE(systemMessenger, false); |
michael@0 | 119 | |
michael@0 | 120 | JS::Rooted<JS::Value> value(cx); |
michael@0 | 121 | if (aData.type() == BluetoothValue::TnsString) { |
michael@0 | 122 | JSString* jsData = JS_NewUCStringCopyN(cx, |
michael@0 | 123 | aData.get_nsString().BeginReading(), |
michael@0 | 124 | aData.get_nsString().Length()); |
michael@0 | 125 | value = STRING_TO_JSVAL(jsData); |
michael@0 | 126 | } else if (aData.type() == BluetoothValue::TArrayOfBluetoothNamedValue) { |
michael@0 | 127 | JS::Rooted<JSObject*> obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), |
michael@0 | 128 | JS::NullPtr())); |
michael@0 | 129 | if (!obj) { |
michael@0 | 130 | BT_WARNING("Failed to new JSObject for system message!"); |
michael@0 | 131 | return false; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | if (!SetJsObject(cx, aData, obj)) { |
michael@0 | 135 | BT_WARNING("Failed to set properties of system message!"); |
michael@0 | 136 | return false; |
michael@0 | 137 | } |
michael@0 | 138 | value = JS::ObjectValue(*obj); |
michael@0 | 139 | } else { |
michael@0 | 140 | BT_WARNING("Not support the unknown BluetoothValue type"); |
michael@0 | 141 | return false; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | systemMessenger->BroadcastMessage(aType, value, |
michael@0 | 145 | JS::UndefinedHandleValue); |
michael@0 | 146 | |
michael@0 | 147 | return true; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | void |
michael@0 | 151 | DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable, |
michael@0 | 152 | const BluetoothValue& aValue, |
michael@0 | 153 | const nsAString& aErrorStr) |
michael@0 | 154 | { |
michael@0 | 155 | // Reply will be deleted by the runnable after running on main thread |
michael@0 | 156 | BluetoothReply* reply; |
michael@0 | 157 | if (!aErrorStr.IsEmpty()) { |
michael@0 | 158 | nsString err(aErrorStr); |
michael@0 | 159 | reply = new BluetoothReply(BluetoothReplyError(err)); |
michael@0 | 160 | } else { |
michael@0 | 161 | MOZ_ASSERT(aValue.type() != BluetoothValue::T__None); |
michael@0 | 162 | reply = new BluetoothReply(BluetoothReplySuccess(aValue)); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | aRunnable->SetReply(reply); |
michael@0 | 166 | if (NS_FAILED(NS_DispatchToMainThread(aRunnable))) { |
michael@0 | 167 | BT_WARNING("Failed to dispatch to main thread!"); |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | void |
michael@0 | 172 | DispatchStatusChangedEvent(const nsAString& aType, |
michael@0 | 173 | const nsAString& aAddress, |
michael@0 | 174 | bool aStatus) |
michael@0 | 175 | { |
michael@0 | 176 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 177 | |
michael@0 | 178 | InfallibleTArray<BluetoothNamedValue> data; |
michael@0 | 179 | BT_APPEND_NAMED_VALUE(data, "address", nsString(aAddress)); |
michael@0 | 180 | BT_APPEND_NAMED_VALUE(data, "status", aStatus); |
michael@0 | 181 | |
michael@0 | 182 | BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data); |
michael@0 | 183 | |
michael@0 | 184 | BluetoothService* bs = BluetoothService::Get(); |
michael@0 | 185 | NS_ENSURE_TRUE_VOID(bs); |
michael@0 | 186 | bs->DistributeSignal(signal); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | END_BLUETOOTH_NAMESPACE |