dom/mobilemessage/src/ipc/SmsChild.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "SmsChild.h"
     6 #include "SmsMessage.h"
     7 #include "MmsMessage.h"
     8 #include "SmsSegmentInfo.h"
     9 #include "nsIObserverService.h"
    10 #include "mozilla/Services.h"
    11 #include "mozilla/dom/ContentChild.h"
    12 #include "mozilla/dom/mobilemessage/Constants.h" // For MessageType
    13 #include "MobileMessageThread.h"
    14 #include "MainThreadUtils.h"
    16 using namespace mozilla;
    17 using namespace mozilla::dom;
    18 using namespace mozilla::dom::mobilemessage;
    20 namespace {
    22 already_AddRefed<nsISupports>
    23 CreateMessageFromMessageData(const MobileMessageData& aData)
    24 {
    25   nsCOMPtr<nsISupports> message;
    27   switch(aData.type()) {
    28     case MobileMessageData::TMmsMessageData:
    29       message = new MmsMessage(aData.get_MmsMessageData());
    30       break;
    31     case MobileMessageData::TSmsMessageData:
    32       message = new SmsMessage(aData.get_SmsMessageData());
    33       break;
    34     default:
    35       MOZ_CRASH("Unexpected type of MobileMessageData");
    36   }
    38   return message.forget();
    39 }
    41 void
    42 NotifyObserversWithMobileMessage(const char* aEventName,
    43                                  const MobileMessageData& aData)
    44 {
    45   nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
    46   if (!obs) {
    47     return;
    48   }
    50   nsCOMPtr<nsISupports> msg = CreateMessageFromMessageData(aData);
    51   obs->NotifyObservers(msg, aEventName, nullptr);
    52 }
    54 } // anonymous namespace
    56 namespace mozilla {
    57 namespace dom {
    58 namespace mobilemessage {
    60 void
    61 SmsChild::ActorDestroy(ActorDestroyReason aWhy)
    62 {
    63 }
    65 bool
    66 SmsChild::RecvNotifyReceivedMessage(const MobileMessageData& aData)
    67 {
    68   NotifyObserversWithMobileMessage(kSmsReceivedObserverTopic, aData);
    69   return true;
    70 }
    72 bool
    73 SmsChild::RecvNotifyRetrievingMessage(const MobileMessageData& aData)
    74 {
    75   NotifyObserversWithMobileMessage(kSmsRetrievingObserverTopic, aData);
    76   return true;
    77 }
    79 bool
    80 SmsChild::RecvNotifySendingMessage(const MobileMessageData& aData)
    81 {
    82   NotifyObserversWithMobileMessage(kSmsSendingObserverTopic, aData);
    83   return true;
    84 }
    86 bool
    87 SmsChild::RecvNotifySentMessage(const MobileMessageData& aData)
    88 {
    89   NotifyObserversWithMobileMessage(kSmsSentObserverTopic, aData);
    90   return true;
    91 }
    93 bool
    94 SmsChild::RecvNotifyFailedMessage(const MobileMessageData& aData)
    95 {
    96   NotifyObserversWithMobileMessage(kSmsFailedObserverTopic, aData);
    97   return true;
    98 }
   100 bool
   101 SmsChild::RecvNotifyDeliverySuccessMessage(const MobileMessageData& aData)
   102 {
   103   NotifyObserversWithMobileMessage(kSmsDeliverySuccessObserverTopic, aData);
   104   return true;
   105 }
   107 bool
   108 SmsChild::RecvNotifyDeliveryErrorMessage(const MobileMessageData& aData)
   109 {
   110   NotifyObserversWithMobileMessage(kSmsDeliveryErrorObserverTopic, aData);
   111   return true;
   112 }
   114 bool
   115 SmsChild::RecvNotifyReceivedSilentMessage(const MobileMessageData& aData)
   116 {
   117   NotifyObserversWithMobileMessage(kSilentSmsReceivedObserverTopic, aData);
   118   return true;
   119 }
   121 bool
   122 SmsChild::RecvNotifyReadSuccessMessage(const MobileMessageData& aData)
   123 {
   124   NotifyObserversWithMobileMessage(kSmsReadSuccessObserverTopic, aData);
   125   return true;
   126 }
   128 bool
   129 SmsChild::RecvNotifyReadErrorMessage(const MobileMessageData& aData)
   130 {
   131   NotifyObserversWithMobileMessage(kSmsReadErrorObserverTopic, aData);
   132   return true;
   133 }
   135 PSmsRequestChild*
   136 SmsChild::AllocPSmsRequestChild(const IPCSmsRequest& aRequest)
   137 {
   138   MOZ_CRASH("Caller is supposed to manually construct a request!");
   139 }
   141 bool
   142 SmsChild::DeallocPSmsRequestChild(PSmsRequestChild* aActor)
   143 {
   144   delete aActor;
   145   return true;
   146 }
   148 PMobileMessageCursorChild*
   149 SmsChild::AllocPMobileMessageCursorChild(const IPCMobileMessageCursor& aCursor)
   150 {
   151   MOZ_CRASH("Caller is supposed to manually construct a cursor!");
   152 }
   154 bool
   155 SmsChild::DeallocPMobileMessageCursorChild(PMobileMessageCursorChild* aActor)
   156 {
   157   // MobileMessageCursorChild is refcounted, must not be freed manually.
   158   // Originally AddRefed in SendCursorRequest() in SmsIPCService.cpp.
   159   static_cast<MobileMessageCursorChild*>(aActor)->Release();
   160   return true;
   161 }
   163 /*******************************************************************************
   164  * SmsRequestChild
   165  ******************************************************************************/
   167 SmsRequestChild::SmsRequestChild(nsIMobileMessageCallback* aReplyRequest)
   168   : mReplyRequest(aReplyRequest)
   169 {
   170   MOZ_COUNT_CTOR(SmsRequestChild);
   171   MOZ_ASSERT(aReplyRequest);
   172 }
   174 void
   175 SmsRequestChild::ActorDestroy(ActorDestroyReason aWhy)
   176 {
   177   // Nothing needed here.
   178 }
   180 bool
   181 SmsRequestChild::Recv__delete__(const MessageReply& aReply)
   182 {
   183   MOZ_ASSERT(NS_IsMainThread());
   184   MOZ_ASSERT(mReplyRequest);
   185   nsCOMPtr<SmsMessage> message;
   186   switch(aReply.type()) {
   187     case MessageReply::TReplyMessageSend: {
   188         const MobileMessageData& data =
   189           aReply.get_ReplyMessageSend().messageData();
   190         nsCOMPtr<nsISupports> msg = CreateMessageFromMessageData(data);
   191         mReplyRequest->NotifyMessageSent(msg);
   192       }
   193       break;
   194     case MessageReply::TReplyMessageSendFail:
   195       mReplyRequest->NotifySendMessageFailed(aReply.get_ReplyMessageSendFail().error());
   196       break;
   197     case MessageReply::TReplyGetMessage: {
   198         const MobileMessageData& data =
   199           aReply.get_ReplyGetMessage().messageData();
   200         nsCOMPtr<nsISupports> msg = CreateMessageFromMessageData(data);
   201         mReplyRequest->NotifyMessageGot(msg);
   202       }
   203       break;
   204     case MessageReply::TReplyGetMessageFail:
   205       mReplyRequest->NotifyGetMessageFailed(aReply.get_ReplyGetMessageFail().error());
   206       break;
   207     case MessageReply::TReplyMessageDelete: {
   208         const InfallibleTArray<bool>& deletedResult = aReply.get_ReplyMessageDelete().deleted();
   209         mReplyRequest->NotifyMessageDeleted(const_cast<bool *>(deletedResult.Elements()),
   210                                             deletedResult.Length());
   211       }
   212       break;
   213     case MessageReply::TReplyMessageDeleteFail:
   214       mReplyRequest->NotifyDeleteMessageFailed(aReply.get_ReplyMessageDeleteFail().error());
   215       break;
   216     case MessageReply::TReplyMarkeMessageRead:
   217       mReplyRequest->NotifyMessageMarkedRead(aReply.get_ReplyMarkeMessageRead().read());
   218       break;
   219     case MessageReply::TReplyMarkeMessageReadFail:
   220       mReplyRequest->NotifyMarkMessageReadFailed(aReply.get_ReplyMarkeMessageReadFail().error());
   221       break;
   222     case MessageReply::TReplyGetSegmentInfoForText: {
   223         const SmsSegmentInfoData& data =
   224           aReply.get_ReplyGetSegmentInfoForText().infoData();
   225         nsCOMPtr<nsIDOMMozSmsSegmentInfo> info = new SmsSegmentInfo(data);
   226         mReplyRequest->NotifySegmentInfoForTextGot(info);
   227       }
   228       break;
   229     case MessageReply::TReplyGetSegmentInfoForTextFail:
   230       mReplyRequest->NotifyGetSegmentInfoForTextFailed(
   231         aReply.get_ReplyGetSegmentInfoForTextFail().error());
   232       break;
   233     case MessageReply::TReplyGetSmscAddress:
   234       mReplyRequest->NotifyGetSmscAddress(aReply.get_ReplyGetSmscAddress().smscAddress());
   235       break;
   236     case MessageReply::TReplyGetSmscAddressFail:
   237       mReplyRequest->NotifyGetSmscAddressFailed(aReply.get_ReplyGetSmscAddressFail().error());
   238       break;
   239     default:
   240       MOZ_CRASH("Received invalid response parameters!");
   241   }
   243   return true;
   244 }
   246 /*******************************************************************************
   247  * MobileMessageCursorChild
   248  ******************************************************************************/
   250 NS_IMPL_ISUPPORTS(MobileMessageCursorChild, nsICursorContinueCallback)
   252 MobileMessageCursorChild::MobileMessageCursorChild(nsIMobileMessageCursorCallback* aCallback)
   253   : mCursorCallback(aCallback)
   254 {
   255   MOZ_COUNT_CTOR(MobileMessageCursorChild);
   256   MOZ_ASSERT(aCallback);
   257 }
   259 void
   260 MobileMessageCursorChild::ActorDestroy(ActorDestroyReason aWhy)
   261 {
   262   // Nothing needed here.
   263 }
   265 bool
   266 MobileMessageCursorChild::RecvNotifyResult(const MobileMessageCursorData& aData)
   267 {
   268   MOZ_ASSERT(mCursorCallback);
   270   nsCOMPtr<nsISupports> result;
   271   switch(aData.type()) {
   272     case MobileMessageCursorData::TMmsMessageData:
   273       result = new MmsMessage(aData.get_MmsMessageData());
   274       break;
   275     case MobileMessageCursorData::TSmsMessageData:
   276       result = new SmsMessage(aData.get_SmsMessageData());
   277       break;
   278     case MobileMessageCursorData::TThreadData:
   279       result = new MobileMessageThread(aData.get_ThreadData());
   280       break;
   281     default:
   282       MOZ_CRASH("Received invalid response parameters!");
   283   }
   285   mCursorCallback->NotifyCursorResult(result);
   286   return true;
   287 }
   289 bool
   290 MobileMessageCursorChild::Recv__delete__(const int32_t& aError)
   291 {
   292   MOZ_ASSERT(mCursorCallback);
   294   if (aError != nsIMobileMessageCallback::SUCCESS_NO_ERROR) {
   295     mCursorCallback->NotifyCursorError(aError);
   296   } else {
   297     mCursorCallback->NotifyCursorDone();
   298   }
   299   mCursorCallback = nullptr;
   301   return true;
   302 }
   304 // nsICursorContinueCallback
   306 NS_IMETHODIMP
   307 MobileMessageCursorChild::HandleContinue()
   308 {
   309   MOZ_ASSERT(mCursorCallback);
   311   SendContinue();
   312   return NS_OK;
   313 }
   315 } // namespace mobilemessage
   316 } // namespace dom
   317 } // namespace mozilla

mercurial