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++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "SmsFilter.h" |
michael@0 | 7 | #include "jsapi.h" |
michael@0 | 8 | #include "jsfriendapi.h" // For js_DateGetMsecSinceEpoch. |
michael@0 | 9 | #include "js/Utility.h" |
michael@0 | 10 | #include "mozilla/dom/mobilemessage/Constants.h" // For MessageType |
michael@0 | 11 | #include "mozilla/dom/ToJSValue.h" |
michael@0 | 12 | #include "nsDOMString.h" |
michael@0 | 13 | #include "nsError.h" |
michael@0 | 14 | #include "nsIDOMClassInfo.h" |
michael@0 | 15 | #include "nsJSUtils.h" |
michael@0 | 16 | |
michael@0 | 17 | using namespace mozilla::dom::mobilemessage; |
michael@0 | 18 | |
michael@0 | 19 | DOMCI_DATA(MozSmsFilter, mozilla::dom::SmsFilter) |
michael@0 | 20 | |
michael@0 | 21 | namespace mozilla { |
michael@0 | 22 | namespace dom { |
michael@0 | 23 | |
michael@0 | 24 | NS_INTERFACE_MAP_BEGIN(SmsFilter) |
michael@0 | 25 | NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsFilter) |
michael@0 | 26 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 27 | NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsFilter) |
michael@0 | 28 | NS_INTERFACE_MAP_END |
michael@0 | 29 | |
michael@0 | 30 | NS_IMPL_ADDREF(SmsFilter) |
michael@0 | 31 | NS_IMPL_RELEASE(SmsFilter) |
michael@0 | 32 | |
michael@0 | 33 | SmsFilter::SmsFilter() |
michael@0 | 34 | { |
michael@0 | 35 | mData.startDate() = 0; |
michael@0 | 36 | mData.endDate() = 0; |
michael@0 | 37 | mData.delivery() = eDeliveryState_Unknown; |
michael@0 | 38 | mData.read() = eReadState_Unknown; |
michael@0 | 39 | mData.threadId() = 0; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | SmsFilter::SmsFilter(const SmsFilterData& aData) |
michael@0 | 43 | : mData(aData) |
michael@0 | 44 | { |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | /* static */ nsresult |
michael@0 | 48 | SmsFilter::NewSmsFilter(nsISupports** aSmsFilter) |
michael@0 | 49 | { |
michael@0 | 50 | NS_ADDREF(*aSmsFilter = new SmsFilter()); |
michael@0 | 51 | return NS_OK; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | NS_IMETHODIMP |
michael@0 | 55 | SmsFilter::GetStartDate(JSContext* aCx, JS::MutableHandle<JS::Value> aStartDate) |
michael@0 | 56 | { |
michael@0 | 57 | if (mData.startDate() == 0) { |
michael@0 | 58 | aStartDate.setNull(); |
michael@0 | 59 | return NS_OK; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | aStartDate.setObjectOrNull(JS_NewDateObjectMsec(aCx, mData.startDate())); |
michael@0 | 63 | NS_ENSURE_TRUE(aStartDate.isObject(), NS_ERROR_FAILURE); |
michael@0 | 64 | |
michael@0 | 65 | return NS_OK; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | NS_IMETHODIMP |
michael@0 | 69 | SmsFilter::SetStartDate(JSContext* aCx, JS::Handle<JS::Value> aStartDate) |
michael@0 | 70 | { |
michael@0 | 71 | if (aStartDate.isNull()) { |
michael@0 | 72 | mData.startDate() = 0; |
michael@0 | 73 | return NS_OK; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | if (!aStartDate.isObject()) { |
michael@0 | 77 | return NS_ERROR_INVALID_ARG; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | JS::Rooted<JSObject*> obj(aCx, &aStartDate.toObject()); |
michael@0 | 81 | if (!JS_ObjectIsDate(aCx, obj)) { |
michael@0 | 82 | return NS_ERROR_INVALID_ARG; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | mData.startDate() = js_DateGetMsecSinceEpoch(obj); |
michael@0 | 86 | return NS_OK; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | NS_IMETHODIMP |
michael@0 | 90 | SmsFilter::GetEndDate(JSContext* aCx, JS::MutableHandle<JS::Value> aEndDate) |
michael@0 | 91 | { |
michael@0 | 92 | if (mData.endDate() == 0) { |
michael@0 | 93 | aEndDate.setNull(); |
michael@0 | 94 | return NS_OK; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | aEndDate.setObjectOrNull(JS_NewDateObjectMsec(aCx, mData.endDate())); |
michael@0 | 98 | NS_ENSURE_TRUE(aEndDate.isObject(), NS_ERROR_FAILURE); |
michael@0 | 99 | |
michael@0 | 100 | return NS_OK; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | NS_IMETHODIMP |
michael@0 | 104 | SmsFilter::SetEndDate(JSContext* aCx, JS::Handle<JS::Value> aEndDate) |
michael@0 | 105 | { |
michael@0 | 106 | if (aEndDate.isNull()) { |
michael@0 | 107 | mData.endDate() = 0; |
michael@0 | 108 | return NS_OK; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | if (!aEndDate.isObject()) { |
michael@0 | 112 | return NS_ERROR_INVALID_ARG; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | JS::Rooted<JSObject*> obj(aCx, &aEndDate.toObject()); |
michael@0 | 116 | if (!JS_ObjectIsDate(aCx, obj)) { |
michael@0 | 117 | return NS_ERROR_INVALID_ARG; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | mData.endDate() = js_DateGetMsecSinceEpoch(obj); |
michael@0 | 121 | return NS_OK; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | NS_IMETHODIMP |
michael@0 | 125 | SmsFilter::GetNumbers(JSContext* aCx, JS::MutableHandle<JS::Value> aNumbers) |
michael@0 | 126 | { |
michael@0 | 127 | uint32_t length = mData.numbers().Length(); |
michael@0 | 128 | |
michael@0 | 129 | if (length == 0) { |
michael@0 | 130 | aNumbers.setNull(); |
michael@0 | 131 | return NS_OK; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | if (!ToJSValue(aCx, mData.numbers(), aNumbers)) { |
michael@0 | 135 | return NS_ERROR_FAILURE; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | return NS_OK; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | NS_IMETHODIMP |
michael@0 | 142 | SmsFilter::SetNumbers(JSContext* aCx, JS::Handle<JS::Value> aNumbers) |
michael@0 | 143 | { |
michael@0 | 144 | if (aNumbers.isNull()) { |
michael@0 | 145 | mData.numbers().Clear(); |
michael@0 | 146 | return NS_OK; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | if (!aNumbers.isObject()) { |
michael@0 | 150 | return NS_ERROR_INVALID_ARG; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | JS::Rooted<JSObject*> obj(aCx, &aNumbers.toObject()); |
michael@0 | 154 | if (!JS_IsArrayObject(aCx, obj)) { |
michael@0 | 155 | return NS_ERROR_INVALID_ARG; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | uint32_t size; |
michael@0 | 159 | MOZ_ALWAYS_TRUE(JS_GetArrayLength(aCx, obj, &size)); |
michael@0 | 160 | |
michael@0 | 161 | nsTArray<nsString> numbers; |
michael@0 | 162 | |
michael@0 | 163 | for (uint32_t i=0; i<size; ++i) { |
michael@0 | 164 | JS::Rooted<JS::Value> jsNumber(aCx); |
michael@0 | 165 | if (!JS_GetElement(aCx, obj, i, &jsNumber)) { |
michael@0 | 166 | return NS_ERROR_INVALID_ARG; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | if (!jsNumber.isString()) { |
michael@0 | 170 | return NS_ERROR_INVALID_ARG; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | nsDependentJSString number; |
michael@0 | 174 | number.init(aCx, jsNumber.toString()); |
michael@0 | 175 | |
michael@0 | 176 | numbers.AppendElement(number); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | mData.numbers().Clear(); |
michael@0 | 180 | mData.numbers().AppendElements(numbers); |
michael@0 | 181 | |
michael@0 | 182 | return NS_OK; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | NS_IMETHODIMP |
michael@0 | 186 | SmsFilter::GetDelivery(nsAString& aDelivery) |
michael@0 | 187 | { |
michael@0 | 188 | switch (mData.delivery()) { |
michael@0 | 189 | case eDeliveryState_Received: |
michael@0 | 190 | aDelivery = DELIVERY_RECEIVED; |
michael@0 | 191 | break; |
michael@0 | 192 | case eDeliveryState_Sent: |
michael@0 | 193 | aDelivery = DELIVERY_SENT; |
michael@0 | 194 | break; |
michael@0 | 195 | case eDeliveryState_Unknown: |
michael@0 | 196 | SetDOMStringToNull(aDelivery); |
michael@0 | 197 | break; |
michael@0 | 198 | default: |
michael@0 | 199 | NS_ASSERTION(false, "We shouldn't get another delivery state!"); |
michael@0 | 200 | return NS_ERROR_UNEXPECTED; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | return NS_OK; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | NS_IMETHODIMP |
michael@0 | 207 | SmsFilter::SetDelivery(const nsAString& aDelivery) |
michael@0 | 208 | { |
michael@0 | 209 | if (aDelivery.IsEmpty()) { |
michael@0 | 210 | mData.delivery() = eDeliveryState_Unknown; |
michael@0 | 211 | return NS_OK; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | if (aDelivery.Equals(DELIVERY_RECEIVED)) { |
michael@0 | 215 | mData.delivery() = eDeliveryState_Received; |
michael@0 | 216 | return NS_OK; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | if (aDelivery.Equals(DELIVERY_SENT)) { |
michael@0 | 220 | mData.delivery() = eDeliveryState_Sent; |
michael@0 | 221 | return NS_OK; |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | return NS_ERROR_INVALID_ARG; |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | NS_IMETHODIMP |
michael@0 | 228 | SmsFilter::GetRead(JSContext* aCx, JS::MutableHandle<JS::Value> aRead) |
michael@0 | 229 | { |
michael@0 | 230 | if (mData.read() == eReadState_Unknown) { |
michael@0 | 231 | aRead.setNull(); |
michael@0 | 232 | return NS_OK; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | aRead.setBoolean(mData.read()); |
michael@0 | 236 | return NS_OK; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | NS_IMETHODIMP |
michael@0 | 240 | SmsFilter::SetRead(JSContext* aCx, JS::Handle<JS::Value> aRead) |
michael@0 | 241 | { |
michael@0 | 242 | if (aRead.isNull()) { |
michael@0 | 243 | mData.read() = eReadState_Unknown; |
michael@0 | 244 | return NS_OK; |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | if (!aRead.isBoolean()) { |
michael@0 | 248 | return NS_ERROR_INVALID_ARG; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | mData.read() = aRead.toBoolean() ? eReadState_Read : eReadState_Unread; |
michael@0 | 252 | return NS_OK; |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | NS_IMETHODIMP |
michael@0 | 256 | SmsFilter::GetThreadId(JSContext* aCx, JS::MutableHandle<JS::Value> aThreadId) |
michael@0 | 257 | { |
michael@0 | 258 | if (!mData.threadId()) { |
michael@0 | 259 | aThreadId.setNull(); |
michael@0 | 260 | return NS_OK; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | aThreadId.setNumber(static_cast<double>(mData.threadId())); |
michael@0 | 264 | return NS_OK; |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | NS_IMETHODIMP |
michael@0 | 268 | SmsFilter::SetThreadId(JSContext* aCx, JS::Handle<JS::Value> aThreadId) |
michael@0 | 269 | { |
michael@0 | 270 | if (aThreadId.isNull()) { |
michael@0 | 271 | mData.threadId() = 0; |
michael@0 | 272 | return NS_OK; |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | if (!aThreadId.isNumber()) { |
michael@0 | 276 | return NS_ERROR_INVALID_ARG; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | double number = aThreadId.toNumber(); |
michael@0 | 280 | uint64_t integer = static_cast<uint64_t>(number); |
michael@0 | 281 | if (integer == 0 || integer != number) { |
michael@0 | 282 | return NS_ERROR_INVALID_ARG; |
michael@0 | 283 | } |
michael@0 | 284 | mData.threadId() = integer; |
michael@0 | 285 | |
michael@0 | 286 | return NS_OK; |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | } // namespace dom |
michael@0 | 290 | } // namespace mozilla |