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 "mozilla/dom/ContentChild.h" |
michael@0 | 7 | #include "SmsIPCService.h" |
michael@0 | 8 | #include "nsXULAppAPI.h" |
michael@0 | 9 | #include "mozilla/dom/mobilemessage/SmsChild.h" |
michael@0 | 10 | #include "SmsMessage.h" |
michael@0 | 11 | #include "SmsFilter.h" |
michael@0 | 12 | #include "SmsSegmentInfo.h" |
michael@0 | 13 | #include "nsJSUtils.h" |
michael@0 | 14 | #include "nsCxPusher.h" |
michael@0 | 15 | #include "mozilla/dom/MobileMessageManagerBinding.h" |
michael@0 | 16 | #include "mozilla/dom/MozMmsMessageBinding.h" |
michael@0 | 17 | #include "mozilla/dom/BindingUtils.h" |
michael@0 | 18 | #include "mozilla/Preferences.h" |
michael@0 | 19 | #include "nsString.h" |
michael@0 | 20 | |
michael@0 | 21 | using namespace mozilla::dom; |
michael@0 | 22 | using namespace mozilla::dom::mobilemessage; |
michael@0 | 23 | |
michael@0 | 24 | namespace { |
michael@0 | 25 | |
michael@0 | 26 | const char* kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces"; |
michael@0 | 27 | #define kPrefMmsDefaultServiceId "dom.mms.defaultServiceId" |
michael@0 | 28 | #define kPrefSmsDefaultServiceId "dom.sms.defaultServiceId" |
michael@0 | 29 | const char* kObservedPrefs[] = { |
michael@0 | 30 | kPrefMmsDefaultServiceId, |
michael@0 | 31 | kPrefSmsDefaultServiceId, |
michael@0 | 32 | nullptr |
michael@0 | 33 | }; |
michael@0 | 34 | |
michael@0 | 35 | // TODO: Bug 767082 - WebSMS: sSmsChild leaks at shutdown |
michael@0 | 36 | PSmsChild* gSmsChild; |
michael@0 | 37 | |
michael@0 | 38 | PSmsChild* |
michael@0 | 39 | GetSmsChild() |
michael@0 | 40 | { |
michael@0 | 41 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 42 | |
michael@0 | 43 | if (!gSmsChild) { |
michael@0 | 44 | gSmsChild = ContentChild::GetSingleton()->SendPSmsConstructor(); |
michael@0 | 45 | |
michael@0 | 46 | NS_WARN_IF_FALSE(gSmsChild, |
michael@0 | 47 | "Calling methods on SmsIPCService during shutdown!"); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | return gSmsChild; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | nsresult |
michael@0 | 54 | SendRequest(const IPCSmsRequest& aRequest, |
michael@0 | 55 | nsIMobileMessageCallback* aRequestReply) |
michael@0 | 56 | { |
michael@0 | 57 | PSmsChild* smsChild = GetSmsChild(); |
michael@0 | 58 | NS_ENSURE_TRUE(smsChild, NS_ERROR_FAILURE); |
michael@0 | 59 | |
michael@0 | 60 | SmsRequestChild* actor = new SmsRequestChild(aRequestReply); |
michael@0 | 61 | smsChild->SendPSmsRequestConstructor(actor, aRequest); |
michael@0 | 62 | |
michael@0 | 63 | return NS_OK; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | nsresult |
michael@0 | 67 | SendCursorRequest(const IPCMobileMessageCursor& aRequest, |
michael@0 | 68 | nsIMobileMessageCursorCallback* aRequestReply, |
michael@0 | 69 | nsICursorContinueCallback** aResult) |
michael@0 | 70 | { |
michael@0 | 71 | PSmsChild* smsChild = GetSmsChild(); |
michael@0 | 72 | NS_ENSURE_TRUE(smsChild, NS_ERROR_FAILURE); |
michael@0 | 73 | |
michael@0 | 74 | nsRefPtr<MobileMessageCursorChild> actor = |
michael@0 | 75 | new MobileMessageCursorChild(aRequestReply); |
michael@0 | 76 | |
michael@0 | 77 | // Add an extra ref for IPDL. Will be released in |
michael@0 | 78 | // SmsChild::DeallocPMobileMessageCursor(). |
michael@0 | 79 | actor->AddRef(); |
michael@0 | 80 | |
michael@0 | 81 | smsChild->SendPMobileMessageCursorConstructor(actor, aRequest); |
michael@0 | 82 | |
michael@0 | 83 | actor.forget(aResult); |
michael@0 | 84 | return NS_OK; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | uint32_t |
michael@0 | 88 | getDefaultServiceId(const char* aPrefKey) |
michael@0 | 89 | { |
michael@0 | 90 | int32_t id = mozilla::Preferences::GetInt(aPrefKey, 0); |
michael@0 | 91 | int32_t numRil = mozilla::Preferences::GetInt(kPrefRilNumRadioInterfaces, 1); |
michael@0 | 92 | |
michael@0 | 93 | if (id >= numRil || id < 0) { |
michael@0 | 94 | id = 0; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | return id; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | } // anonymous namespace |
michael@0 | 101 | |
michael@0 | 102 | NS_IMPL_ISUPPORTS(SmsIPCService, |
michael@0 | 103 | nsISmsService, |
michael@0 | 104 | nsIMmsService, |
michael@0 | 105 | nsIMobileMessageDatabaseService, |
michael@0 | 106 | nsIObserver) |
michael@0 | 107 | |
michael@0 | 108 | SmsIPCService::SmsIPCService() |
michael@0 | 109 | { |
michael@0 | 110 | Preferences::AddStrongObservers(this, kObservedPrefs); |
michael@0 | 111 | mMmsDefaultServiceId = getDefaultServiceId(kPrefMmsDefaultServiceId); |
michael@0 | 112 | mSmsDefaultServiceId = getDefaultServiceId(kPrefSmsDefaultServiceId); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | /* |
michael@0 | 116 | * Implementation of nsIObserver. |
michael@0 | 117 | */ |
michael@0 | 118 | |
michael@0 | 119 | NS_IMETHODIMP |
michael@0 | 120 | SmsIPCService::Observe(nsISupports* aSubject, |
michael@0 | 121 | const char* aTopic, |
michael@0 | 122 | const char16_t* aData) |
michael@0 | 123 | { |
michael@0 | 124 | if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) { |
michael@0 | 125 | nsDependentString data(aData); |
michael@0 | 126 | if (data.EqualsLiteral(kPrefMmsDefaultServiceId)) { |
michael@0 | 127 | mMmsDefaultServiceId = getDefaultServiceId(kPrefMmsDefaultServiceId); |
michael@0 | 128 | } else if (data.EqualsLiteral(kPrefSmsDefaultServiceId)) { |
michael@0 | 129 | mSmsDefaultServiceId = getDefaultServiceId(kPrefSmsDefaultServiceId); |
michael@0 | 130 | } |
michael@0 | 131 | return NS_OK; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | MOZ_ASSERT(false, "SmsIPCService got unexpected topic!"); |
michael@0 | 135 | return NS_ERROR_UNEXPECTED; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | /* |
michael@0 | 139 | * Implementation of nsISmsService. |
michael@0 | 140 | */ |
michael@0 | 141 | |
michael@0 | 142 | NS_IMETHODIMP |
michael@0 | 143 | SmsIPCService::GetSmsDefaultServiceId(uint32_t* aServiceId) |
michael@0 | 144 | { |
michael@0 | 145 | *aServiceId = mSmsDefaultServiceId; |
michael@0 | 146 | return NS_OK; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | NS_IMETHODIMP |
michael@0 | 150 | SmsIPCService::GetSegmentInfoForText(const nsAString& aText, |
michael@0 | 151 | nsIMobileMessageCallback* aRequest) |
michael@0 | 152 | { |
michael@0 | 153 | return SendRequest(GetSegmentInfoForTextRequest(nsString(aText)), |
michael@0 | 154 | aRequest); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | NS_IMETHODIMP |
michael@0 | 158 | SmsIPCService::GetSmscAddress(uint32_t aServiceId, |
michael@0 | 159 | nsIMobileMessageCallback* aRequest) |
michael@0 | 160 | { |
michael@0 | 161 | return SendRequest(GetSmscAddressRequest(aServiceId), aRequest); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | NS_IMETHODIMP |
michael@0 | 165 | SmsIPCService::Send(uint32_t aServiceId, |
michael@0 | 166 | const nsAString& aNumber, |
michael@0 | 167 | const nsAString& aMessage, |
michael@0 | 168 | bool aSilent, |
michael@0 | 169 | nsIMobileMessageCallback* aRequest) |
michael@0 | 170 | { |
michael@0 | 171 | return SendRequest(SendMessageRequest(SendSmsMessageRequest(aServiceId, |
michael@0 | 172 | nsString(aNumber), |
michael@0 | 173 | nsString(aMessage), |
michael@0 | 174 | aSilent)), |
michael@0 | 175 | aRequest); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | NS_IMETHODIMP |
michael@0 | 179 | SmsIPCService::IsSilentNumber(const nsAString& aNumber, |
michael@0 | 180 | bool* aIsSilent) |
michael@0 | 181 | { |
michael@0 | 182 | NS_ERROR("We should not be here!"); |
michael@0 | 183 | return NS_ERROR_FAILURE; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | NS_IMETHODIMP |
michael@0 | 187 | SmsIPCService::AddSilentNumber(const nsAString& aNumber) |
michael@0 | 188 | { |
michael@0 | 189 | PSmsChild* smsChild = GetSmsChild(); |
michael@0 | 190 | NS_ENSURE_TRUE(smsChild, NS_ERROR_FAILURE); |
michael@0 | 191 | |
michael@0 | 192 | smsChild->SendAddSilentNumber(nsString(aNumber)); |
michael@0 | 193 | return NS_OK; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | NS_IMETHODIMP |
michael@0 | 197 | SmsIPCService::RemoveSilentNumber(const nsAString& aNumber) |
michael@0 | 198 | { |
michael@0 | 199 | PSmsChild* smsChild = GetSmsChild(); |
michael@0 | 200 | NS_ENSURE_TRUE(smsChild, NS_ERROR_FAILURE); |
michael@0 | 201 | |
michael@0 | 202 | smsChild->SendRemoveSilentNumber(nsString(aNumber)); |
michael@0 | 203 | return NS_OK; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | /* |
michael@0 | 207 | * Implementation of nsIMobileMessageDatabaseService. |
michael@0 | 208 | */ |
michael@0 | 209 | NS_IMETHODIMP |
michael@0 | 210 | SmsIPCService::GetMessageMoz(int32_t aMessageId, |
michael@0 | 211 | nsIMobileMessageCallback* aRequest) |
michael@0 | 212 | { |
michael@0 | 213 | return SendRequest(GetMessageRequest(aMessageId), aRequest); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | NS_IMETHODIMP |
michael@0 | 217 | SmsIPCService::DeleteMessage(int32_t *aMessageIds, uint32_t aSize, |
michael@0 | 218 | nsIMobileMessageCallback* aRequest) |
michael@0 | 219 | { |
michael@0 | 220 | DeleteMessageRequest data; |
michael@0 | 221 | data.messageIds().AppendElements(aMessageIds, aSize); |
michael@0 | 222 | return SendRequest(data, aRequest); |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | NS_IMETHODIMP |
michael@0 | 226 | SmsIPCService::CreateMessageCursor(nsIDOMMozSmsFilter* aFilter, |
michael@0 | 227 | bool aReverse, |
michael@0 | 228 | nsIMobileMessageCursorCallback* aCursorCallback, |
michael@0 | 229 | nsICursorContinueCallback** aResult) |
michael@0 | 230 | { |
michael@0 | 231 | const SmsFilterData& data = |
michael@0 | 232 | SmsFilterData(static_cast<SmsFilter*>(aFilter)->GetData()); |
michael@0 | 233 | |
michael@0 | 234 | return SendCursorRequest(CreateMessageCursorRequest(data, aReverse), |
michael@0 | 235 | aCursorCallback, aResult); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | NS_IMETHODIMP |
michael@0 | 239 | SmsIPCService::MarkMessageRead(int32_t aMessageId, |
michael@0 | 240 | bool aValue, |
michael@0 | 241 | bool aSendReadReport, |
michael@0 | 242 | nsIMobileMessageCallback* aRequest) |
michael@0 | 243 | { |
michael@0 | 244 | return SendRequest(MarkMessageReadRequest(aMessageId, aValue, aSendReadReport), aRequest); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | NS_IMETHODIMP |
michael@0 | 248 | SmsIPCService::CreateThreadCursor(nsIMobileMessageCursorCallback* aCursorCallback, |
michael@0 | 249 | nsICursorContinueCallback** aResult) |
michael@0 | 250 | { |
michael@0 | 251 | return SendCursorRequest(CreateThreadCursorRequest(), aCursorCallback, |
michael@0 | 252 | aResult); |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | bool |
michael@0 | 256 | GetSendMmsMessageRequestFromParams(uint32_t aServiceId, |
michael@0 | 257 | const JS::Value& aParam, |
michael@0 | 258 | SendMmsMessageRequest& request) { |
michael@0 | 259 | if (aParam.isUndefined() || aParam.isNull() || !aParam.isObject()) { |
michael@0 | 260 | return false; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | mozilla::AutoJSContext cx; |
michael@0 | 264 | JS::Rooted<JS::Value> param(cx, aParam); |
michael@0 | 265 | RootedDictionary<MmsParameters> params(cx); |
michael@0 | 266 | if (!params.Init(cx, param)) { |
michael@0 | 267 | return false; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | // SendMobileMessageRequest.receivers |
michael@0 | 271 | if (!params.mReceivers.WasPassed()) { |
michael@0 | 272 | return false; |
michael@0 | 273 | } |
michael@0 | 274 | request.receivers().AppendElements(params.mReceivers.Value()); |
michael@0 | 275 | |
michael@0 | 276 | // SendMobileMessageRequest.attachments |
michael@0 | 277 | mozilla::dom::ContentChild* cc = mozilla::dom::ContentChild::GetSingleton(); |
michael@0 | 278 | |
michael@0 | 279 | if (!params.mAttachments.WasPassed()) { |
michael@0 | 280 | return false; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | for (uint32_t i = 0; i < params.mAttachments.Value().Length(); i++) { |
michael@0 | 284 | mozilla::dom::MmsAttachment& attachment = params.mAttachments.Value()[i]; |
michael@0 | 285 | MmsAttachmentData mmsAttachment; |
michael@0 | 286 | mmsAttachment.id().Assign(attachment.mId); |
michael@0 | 287 | mmsAttachment.location().Assign(attachment.mLocation); |
michael@0 | 288 | mmsAttachment.contentChild() = cc->GetOrCreateActorForBlob(attachment.mContent); |
michael@0 | 289 | if (!mmsAttachment.contentChild()) { |
michael@0 | 290 | return false; |
michael@0 | 291 | } |
michael@0 | 292 | request.attachments().AppendElement(mmsAttachment); |
michael@0 | 293 | } |
michael@0 | 294 | |
michael@0 | 295 | request.smil() = params.mSmil; |
michael@0 | 296 | request.subject() = params.mSubject; |
michael@0 | 297 | |
michael@0 | 298 | // Set service ID. |
michael@0 | 299 | request.serviceId() = aServiceId; |
michael@0 | 300 | |
michael@0 | 301 | return true; |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | /* |
michael@0 | 305 | * Implementation of nsIMmsService. |
michael@0 | 306 | */ |
michael@0 | 307 | |
michael@0 | 308 | NS_IMETHODIMP |
michael@0 | 309 | SmsIPCService::GetMmsDefaultServiceId(uint32_t* aServiceId) |
michael@0 | 310 | { |
michael@0 | 311 | *aServiceId = mMmsDefaultServiceId; |
michael@0 | 312 | return NS_OK; |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | NS_IMETHODIMP |
michael@0 | 316 | SmsIPCService::Send(uint32_t aServiceId, |
michael@0 | 317 | JS::Handle<JS::Value> aParameters, |
michael@0 | 318 | nsIMobileMessageCallback *aRequest) |
michael@0 | 319 | { |
michael@0 | 320 | SendMmsMessageRequest req; |
michael@0 | 321 | if (!GetSendMmsMessageRequestFromParams(aServiceId, aParameters, req)) { |
michael@0 | 322 | return NS_ERROR_INVALID_ARG; |
michael@0 | 323 | } |
michael@0 | 324 | return SendRequest(SendMessageRequest(req), aRequest); |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | NS_IMETHODIMP |
michael@0 | 328 | SmsIPCService::Retrieve(int32_t aId, nsIMobileMessageCallback *aRequest) |
michael@0 | 329 | { |
michael@0 | 330 | return SendRequest(RetrieveMessageRequest(aId), aRequest); |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | NS_IMETHODIMP |
michael@0 | 334 | SmsIPCService::SendReadReport(const nsAString & messageID, |
michael@0 | 335 | const nsAString & toAddress, |
michael@0 | 336 | const nsAString & iccId) |
michael@0 | 337 | { |
michael@0 | 338 | NS_ERROR("We should not be here!"); |
michael@0 | 339 | return NS_OK; |
michael@0 | 340 | } |