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 |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "MobileMessageCallback.h" |
michael@0 | 7 | #include "nsContentUtils.h" |
michael@0 | 8 | #include "nsCxPusher.h" |
michael@0 | 9 | #include "nsIDOMMozSmsMessage.h" |
michael@0 | 10 | #include "nsIDOMMozMmsMessage.h" |
michael@0 | 11 | #include "nsIDOMSmsSegmentInfo.h" |
michael@0 | 12 | #include "nsIScriptGlobalObject.h" |
michael@0 | 13 | #include "nsPIDOMWindow.h" |
michael@0 | 14 | #include "MmsMessage.h" |
michael@0 | 15 | #include "jsapi.h" |
michael@0 | 16 | #include "xpcpublic.h" |
michael@0 | 17 | #include "nsServiceManagerUtils.h" |
michael@0 | 18 | #include "nsTArrayHelpers.h" |
michael@0 | 19 | |
michael@0 | 20 | namespace mozilla { |
michael@0 | 21 | namespace dom { |
michael@0 | 22 | namespace mobilemessage { |
michael@0 | 23 | |
michael@0 | 24 | NS_IMPL_ADDREF(MobileMessageCallback) |
michael@0 | 25 | NS_IMPL_RELEASE(MobileMessageCallback) |
michael@0 | 26 | |
michael@0 | 27 | NS_INTERFACE_MAP_BEGIN(MobileMessageCallback) |
michael@0 | 28 | NS_INTERFACE_MAP_ENTRY(nsIMobileMessageCallback) |
michael@0 | 29 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 30 | NS_INTERFACE_MAP_END |
michael@0 | 31 | |
michael@0 | 32 | MobileMessageCallback::MobileMessageCallback(DOMRequest* aDOMRequest) |
michael@0 | 33 | : mDOMRequest(aDOMRequest) |
michael@0 | 34 | { |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | MobileMessageCallback::~MobileMessageCallback() |
michael@0 | 38 | { |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | |
michael@0 | 42 | nsresult |
michael@0 | 43 | MobileMessageCallback::NotifySuccess(JS::Handle<JS::Value> aResult, bool aAsync) |
michael@0 | 44 | { |
michael@0 | 45 | if (aAsync) { |
michael@0 | 46 | nsCOMPtr<nsIDOMRequestService> rs = |
michael@0 | 47 | do_GetService(DOMREQUEST_SERVICE_CONTRACTID); |
michael@0 | 48 | NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); |
michael@0 | 49 | |
michael@0 | 50 | return rs->FireSuccessAsync(mDOMRequest, aResult); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | mDOMRequest->FireSuccess(aResult); |
michael@0 | 54 | return NS_OK; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | nsresult |
michael@0 | 58 | MobileMessageCallback::NotifySuccess(nsISupports *aMessage, bool aAsync) |
michael@0 | 59 | { |
michael@0 | 60 | nsresult rv; |
michael@0 | 61 | nsIScriptContext* scriptContext = mDOMRequest->GetContextForEventHandlers(&rv); |
michael@0 | 62 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 63 | NS_ENSURE_TRUE(scriptContext, NS_ERROR_FAILURE); |
michael@0 | 64 | |
michael@0 | 65 | AutoPushJSContext cx(scriptContext->GetNativeContext()); |
michael@0 | 66 | NS_ENSURE_TRUE(cx, NS_ERROR_FAILURE); |
michael@0 | 67 | |
michael@0 | 68 | JS::Rooted<JSObject*> global(cx, scriptContext->GetWindowProxy()); |
michael@0 | 69 | NS_ENSURE_TRUE(global, NS_ERROR_FAILURE); |
michael@0 | 70 | |
michael@0 | 71 | JSAutoCompartment ac(cx, global); |
michael@0 | 72 | |
michael@0 | 73 | JS::Rooted<JS::Value> wrappedMessage(cx); |
michael@0 | 74 | rv = nsContentUtils::WrapNative(cx, aMessage, &wrappedMessage); |
michael@0 | 75 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 76 | |
michael@0 | 77 | return NotifySuccess(wrappedMessage, aAsync); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | nsresult |
michael@0 | 81 | MobileMessageCallback::NotifyError(int32_t aError, bool aAsync) |
michael@0 | 82 | { |
michael@0 | 83 | nsAutoString errorStr; |
michael@0 | 84 | switch (aError) { |
michael@0 | 85 | case nsIMobileMessageCallback::NO_SIGNAL_ERROR: |
michael@0 | 86 | errorStr = NS_LITERAL_STRING("NoSignalError"); |
michael@0 | 87 | break; |
michael@0 | 88 | case nsIMobileMessageCallback::NOT_FOUND_ERROR: |
michael@0 | 89 | errorStr = NS_LITERAL_STRING("NotFoundError"); |
michael@0 | 90 | break; |
michael@0 | 91 | case nsIMobileMessageCallback::UNKNOWN_ERROR: |
michael@0 | 92 | errorStr = NS_LITERAL_STRING("UnknownError"); |
michael@0 | 93 | break; |
michael@0 | 94 | case nsIMobileMessageCallback::INTERNAL_ERROR: |
michael@0 | 95 | errorStr = NS_LITERAL_STRING("InternalError"); |
michael@0 | 96 | break; |
michael@0 | 97 | case nsIMobileMessageCallback::NO_SIM_CARD_ERROR: |
michael@0 | 98 | errorStr = NS_LITERAL_STRING("NoSimCardError"); |
michael@0 | 99 | break; |
michael@0 | 100 | case nsIMobileMessageCallback::RADIO_DISABLED_ERROR: |
michael@0 | 101 | errorStr = NS_LITERAL_STRING("RadioDisabledError"); |
michael@0 | 102 | break; |
michael@0 | 103 | case nsIMobileMessageCallback::INVALID_ADDRESS_ERROR: |
michael@0 | 104 | errorStr = NS_LITERAL_STRING("InvalidAddressError"); |
michael@0 | 105 | break; |
michael@0 | 106 | case nsIMobileMessageCallback::FDN_CHECK_ERROR: |
michael@0 | 107 | errorStr = NS_LITERAL_STRING("FdnCheckError"); |
michael@0 | 108 | break; |
michael@0 | 109 | case nsIMobileMessageCallback::NON_ACTIVE_SIM_CARD_ERROR: |
michael@0 | 110 | errorStr = NS_LITERAL_STRING("NonActiveSimCardError"); |
michael@0 | 111 | break; |
michael@0 | 112 | case nsIMobileMessageCallback::STORAGE_FULL_ERROR: |
michael@0 | 113 | errorStr = NS_LITERAL_STRING("StorageFullError"); |
michael@0 | 114 | break; |
michael@0 | 115 | case nsIMobileMessageCallback::SIM_NOT_MATCHED_ERROR: |
michael@0 | 116 | errorStr = NS_LITERAL_STRING("SimNotMatchedError"); |
michael@0 | 117 | break; |
michael@0 | 118 | default: // SUCCESS_NO_ERROR is handled above. |
michael@0 | 119 | MOZ_CRASH("Should never get here!"); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | if (aAsync) { |
michael@0 | 123 | nsCOMPtr<nsIDOMRequestService> rs = |
michael@0 | 124 | do_GetService(DOMREQUEST_SERVICE_CONTRACTID); |
michael@0 | 125 | NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); |
michael@0 | 126 | |
michael@0 | 127 | return rs->FireErrorAsync(mDOMRequest, errorStr); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | mDOMRequest->FireError(errorStr); |
michael@0 | 131 | return NS_OK; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | NS_IMETHODIMP |
michael@0 | 135 | MobileMessageCallback::NotifyMessageSent(nsISupports *aMessage) |
michael@0 | 136 | { |
michael@0 | 137 | return NotifySuccess(aMessage); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | NS_IMETHODIMP |
michael@0 | 141 | MobileMessageCallback::NotifySendMessageFailed(int32_t aError) |
michael@0 | 142 | { |
michael@0 | 143 | return NotifyError(aError); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | NS_IMETHODIMP |
michael@0 | 147 | MobileMessageCallback::NotifyMessageGot(nsISupports *aMessage) |
michael@0 | 148 | { |
michael@0 | 149 | return NotifySuccess(aMessage); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | NS_IMETHODIMP |
michael@0 | 153 | MobileMessageCallback::NotifyGetMessageFailed(int32_t aError) |
michael@0 | 154 | { |
michael@0 | 155 | return NotifyError(aError); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | NS_IMETHODIMP |
michael@0 | 159 | MobileMessageCallback::NotifyMessageDeleted(bool *aDeleted, uint32_t aSize) |
michael@0 | 160 | { |
michael@0 | 161 | if (aSize == 1) { |
michael@0 | 162 | AutoJSContext cx; |
michael@0 | 163 | JS::Rooted<JS::Value> val(cx, aDeleted[0] ? JSVAL_TRUE : JSVAL_FALSE); |
michael@0 | 164 | return NotifySuccess(val); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | nsresult rv; |
michael@0 | 168 | nsIScriptContext* sc = mDOMRequest->GetContextForEventHandlers(&rv); |
michael@0 | 169 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 170 | NS_ENSURE_TRUE(sc, NS_ERROR_FAILURE); |
michael@0 | 171 | |
michael@0 | 172 | AutoPushJSContext cx(sc->GetNativeContext()); |
michael@0 | 173 | NS_ENSURE_TRUE(cx, NS_ERROR_FAILURE); |
michael@0 | 174 | |
michael@0 | 175 | JS::Rooted<JSObject*> deleteArrayObj(cx, |
michael@0 | 176 | JS_NewArrayObject(cx, aSize)); |
michael@0 | 177 | for (uint32_t i = 0; i < aSize; i++) { |
michael@0 | 178 | JS_SetElement(cx, deleteArrayObj, i, aDeleted[i]); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | JS::Rooted<JS::Value> deleteArrayVal(cx, JS::ObjectValue(*deleteArrayObj)); |
michael@0 | 182 | return NotifySuccess(deleteArrayVal); |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | NS_IMETHODIMP |
michael@0 | 186 | MobileMessageCallback::NotifyDeleteMessageFailed(int32_t aError) |
michael@0 | 187 | { |
michael@0 | 188 | return NotifyError(aError); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | NS_IMETHODIMP |
michael@0 | 192 | MobileMessageCallback::NotifyMessageMarkedRead(bool aRead) |
michael@0 | 193 | { |
michael@0 | 194 | AutoJSContext cx; |
michael@0 | 195 | JS::Rooted<JS::Value> val(cx, aRead ? JSVAL_TRUE : JSVAL_FALSE); |
michael@0 | 196 | return NotifySuccess(val); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | NS_IMETHODIMP |
michael@0 | 200 | MobileMessageCallback::NotifyMarkMessageReadFailed(int32_t aError) |
michael@0 | 201 | { |
michael@0 | 202 | return NotifyError(aError); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | NS_IMETHODIMP |
michael@0 | 206 | MobileMessageCallback::NotifySegmentInfoForTextGot(nsIDOMMozSmsSegmentInfo *aInfo) |
michael@0 | 207 | { |
michael@0 | 208 | return NotifySuccess(aInfo, true); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | NS_IMETHODIMP |
michael@0 | 212 | MobileMessageCallback::NotifyGetSegmentInfoForTextFailed(int32_t aError) |
michael@0 | 213 | { |
michael@0 | 214 | return NotifyError(aError, true); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | NS_IMETHODIMP |
michael@0 | 218 | MobileMessageCallback::NotifyGetSmscAddress(const nsAString& aSmscAddress) |
michael@0 | 219 | { |
michael@0 | 220 | AutoJSContext cx; |
michael@0 | 221 | JSString* smsc = JS_NewUCStringCopyN(cx, |
michael@0 | 222 | static_cast<const jschar *>(aSmscAddress.BeginReading()), |
michael@0 | 223 | aSmscAddress.Length()); |
michael@0 | 224 | |
michael@0 | 225 | if (!smsc) { |
michael@0 | 226 | return NotifyError(nsIMobileMessageCallback::INTERNAL_ERROR); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | JS::Rooted<JS::Value> val(cx, STRING_TO_JSVAL(smsc)); |
michael@0 | 230 | return NotifySuccess(val); |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | NS_IMETHODIMP |
michael@0 | 234 | MobileMessageCallback::NotifyGetSmscAddressFailed(int32_t aError) |
michael@0 | 235 | { |
michael@0 | 236 | return NotifyError(aError); |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | } // namesapce mobilemessage |
michael@0 | 240 | } // namespace dom |
michael@0 | 241 | } // namespace mozilla |