dom/mobilemessage/src/ipc/SmsChild.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/mobilemessage/src/ipc/SmsChild.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,317 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "SmsChild.h"
     1.9 +#include "SmsMessage.h"
    1.10 +#include "MmsMessage.h"
    1.11 +#include "SmsSegmentInfo.h"
    1.12 +#include "nsIObserverService.h"
    1.13 +#include "mozilla/Services.h"
    1.14 +#include "mozilla/dom/ContentChild.h"
    1.15 +#include "mozilla/dom/mobilemessage/Constants.h" // For MessageType
    1.16 +#include "MobileMessageThread.h"
    1.17 +#include "MainThreadUtils.h"
    1.18 +
    1.19 +using namespace mozilla;
    1.20 +using namespace mozilla::dom;
    1.21 +using namespace mozilla::dom::mobilemessage;
    1.22 +
    1.23 +namespace {
    1.24 +
    1.25 +already_AddRefed<nsISupports>
    1.26 +CreateMessageFromMessageData(const MobileMessageData& aData)
    1.27 +{
    1.28 +  nsCOMPtr<nsISupports> message;
    1.29 +
    1.30 +  switch(aData.type()) {
    1.31 +    case MobileMessageData::TMmsMessageData:
    1.32 +      message = new MmsMessage(aData.get_MmsMessageData());
    1.33 +      break;
    1.34 +    case MobileMessageData::TSmsMessageData:
    1.35 +      message = new SmsMessage(aData.get_SmsMessageData());
    1.36 +      break;
    1.37 +    default:
    1.38 +      MOZ_CRASH("Unexpected type of MobileMessageData");
    1.39 +  }
    1.40 +
    1.41 +  return message.forget();
    1.42 +}
    1.43 +
    1.44 +void
    1.45 +NotifyObserversWithMobileMessage(const char* aEventName,
    1.46 +                                 const MobileMessageData& aData)
    1.47 +{
    1.48 +  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
    1.49 +  if (!obs) {
    1.50 +    return;
    1.51 +  }
    1.52 +
    1.53 +  nsCOMPtr<nsISupports> msg = CreateMessageFromMessageData(aData);
    1.54 +  obs->NotifyObservers(msg, aEventName, nullptr);
    1.55 +}
    1.56 +
    1.57 +} // anonymous namespace
    1.58 +
    1.59 +namespace mozilla {
    1.60 +namespace dom {
    1.61 +namespace mobilemessage {
    1.62 +
    1.63 +void
    1.64 +SmsChild::ActorDestroy(ActorDestroyReason aWhy)
    1.65 +{
    1.66 +}
    1.67 +
    1.68 +bool
    1.69 +SmsChild::RecvNotifyReceivedMessage(const MobileMessageData& aData)
    1.70 +{
    1.71 +  NotifyObserversWithMobileMessage(kSmsReceivedObserverTopic, aData);
    1.72 +  return true;
    1.73 +}
    1.74 +
    1.75 +bool
    1.76 +SmsChild::RecvNotifyRetrievingMessage(const MobileMessageData& aData)
    1.77 +{
    1.78 +  NotifyObserversWithMobileMessage(kSmsRetrievingObserverTopic, aData);
    1.79 +  return true;
    1.80 +}
    1.81 +
    1.82 +bool
    1.83 +SmsChild::RecvNotifySendingMessage(const MobileMessageData& aData)
    1.84 +{
    1.85 +  NotifyObserversWithMobileMessage(kSmsSendingObserverTopic, aData);
    1.86 +  return true;
    1.87 +}
    1.88 +
    1.89 +bool
    1.90 +SmsChild::RecvNotifySentMessage(const MobileMessageData& aData)
    1.91 +{
    1.92 +  NotifyObserversWithMobileMessage(kSmsSentObserverTopic, aData);
    1.93 +  return true;
    1.94 +}
    1.95 +
    1.96 +bool
    1.97 +SmsChild::RecvNotifyFailedMessage(const MobileMessageData& aData)
    1.98 +{
    1.99 +  NotifyObserversWithMobileMessage(kSmsFailedObserverTopic, aData);
   1.100 +  return true;
   1.101 +}
   1.102 +
   1.103 +bool
   1.104 +SmsChild::RecvNotifyDeliverySuccessMessage(const MobileMessageData& aData)
   1.105 +{
   1.106 +  NotifyObserversWithMobileMessage(kSmsDeliverySuccessObserverTopic, aData);
   1.107 +  return true;
   1.108 +}
   1.109 +
   1.110 +bool
   1.111 +SmsChild::RecvNotifyDeliveryErrorMessage(const MobileMessageData& aData)
   1.112 +{
   1.113 +  NotifyObserversWithMobileMessage(kSmsDeliveryErrorObserverTopic, aData);
   1.114 +  return true;
   1.115 +}
   1.116 +
   1.117 +bool
   1.118 +SmsChild::RecvNotifyReceivedSilentMessage(const MobileMessageData& aData)
   1.119 +{
   1.120 +  NotifyObserversWithMobileMessage(kSilentSmsReceivedObserverTopic, aData);
   1.121 +  return true;
   1.122 +}
   1.123 +
   1.124 +bool
   1.125 +SmsChild::RecvNotifyReadSuccessMessage(const MobileMessageData& aData)
   1.126 +{
   1.127 +  NotifyObserversWithMobileMessage(kSmsReadSuccessObserverTopic, aData);
   1.128 +  return true;
   1.129 +}
   1.130 +
   1.131 +bool
   1.132 +SmsChild::RecvNotifyReadErrorMessage(const MobileMessageData& aData)
   1.133 +{
   1.134 +  NotifyObserversWithMobileMessage(kSmsReadErrorObserverTopic, aData);
   1.135 +  return true;
   1.136 +}
   1.137 +
   1.138 +PSmsRequestChild*
   1.139 +SmsChild::AllocPSmsRequestChild(const IPCSmsRequest& aRequest)
   1.140 +{
   1.141 +  MOZ_CRASH("Caller is supposed to manually construct a request!");
   1.142 +}
   1.143 +
   1.144 +bool
   1.145 +SmsChild::DeallocPSmsRequestChild(PSmsRequestChild* aActor)
   1.146 +{
   1.147 +  delete aActor;
   1.148 +  return true;
   1.149 +}
   1.150 +
   1.151 +PMobileMessageCursorChild*
   1.152 +SmsChild::AllocPMobileMessageCursorChild(const IPCMobileMessageCursor& aCursor)
   1.153 +{
   1.154 +  MOZ_CRASH("Caller is supposed to manually construct a cursor!");
   1.155 +}
   1.156 +
   1.157 +bool
   1.158 +SmsChild::DeallocPMobileMessageCursorChild(PMobileMessageCursorChild* aActor)
   1.159 +{
   1.160 +  // MobileMessageCursorChild is refcounted, must not be freed manually.
   1.161 +  // Originally AddRefed in SendCursorRequest() in SmsIPCService.cpp.
   1.162 +  static_cast<MobileMessageCursorChild*>(aActor)->Release();
   1.163 +  return true;
   1.164 +}
   1.165 +
   1.166 +/*******************************************************************************
   1.167 + * SmsRequestChild
   1.168 + ******************************************************************************/
   1.169 +
   1.170 +SmsRequestChild::SmsRequestChild(nsIMobileMessageCallback* aReplyRequest)
   1.171 +  : mReplyRequest(aReplyRequest)
   1.172 +{
   1.173 +  MOZ_COUNT_CTOR(SmsRequestChild);
   1.174 +  MOZ_ASSERT(aReplyRequest);
   1.175 +}
   1.176 +
   1.177 +void
   1.178 +SmsRequestChild::ActorDestroy(ActorDestroyReason aWhy)
   1.179 +{
   1.180 +  // Nothing needed here.
   1.181 +}
   1.182 +
   1.183 +bool
   1.184 +SmsRequestChild::Recv__delete__(const MessageReply& aReply)
   1.185 +{
   1.186 +  MOZ_ASSERT(NS_IsMainThread());
   1.187 +  MOZ_ASSERT(mReplyRequest);
   1.188 +  nsCOMPtr<SmsMessage> message;
   1.189 +  switch(aReply.type()) {
   1.190 +    case MessageReply::TReplyMessageSend: {
   1.191 +        const MobileMessageData& data =
   1.192 +          aReply.get_ReplyMessageSend().messageData();
   1.193 +        nsCOMPtr<nsISupports> msg = CreateMessageFromMessageData(data);
   1.194 +        mReplyRequest->NotifyMessageSent(msg);
   1.195 +      }
   1.196 +      break;
   1.197 +    case MessageReply::TReplyMessageSendFail:
   1.198 +      mReplyRequest->NotifySendMessageFailed(aReply.get_ReplyMessageSendFail().error());
   1.199 +      break;
   1.200 +    case MessageReply::TReplyGetMessage: {
   1.201 +        const MobileMessageData& data =
   1.202 +          aReply.get_ReplyGetMessage().messageData();
   1.203 +        nsCOMPtr<nsISupports> msg = CreateMessageFromMessageData(data);
   1.204 +        mReplyRequest->NotifyMessageGot(msg);
   1.205 +      }
   1.206 +      break;
   1.207 +    case MessageReply::TReplyGetMessageFail:
   1.208 +      mReplyRequest->NotifyGetMessageFailed(aReply.get_ReplyGetMessageFail().error());
   1.209 +      break;
   1.210 +    case MessageReply::TReplyMessageDelete: {
   1.211 +        const InfallibleTArray<bool>& deletedResult = aReply.get_ReplyMessageDelete().deleted();
   1.212 +        mReplyRequest->NotifyMessageDeleted(const_cast<bool *>(deletedResult.Elements()),
   1.213 +                                            deletedResult.Length());
   1.214 +      }
   1.215 +      break;
   1.216 +    case MessageReply::TReplyMessageDeleteFail:
   1.217 +      mReplyRequest->NotifyDeleteMessageFailed(aReply.get_ReplyMessageDeleteFail().error());
   1.218 +      break;
   1.219 +    case MessageReply::TReplyMarkeMessageRead:
   1.220 +      mReplyRequest->NotifyMessageMarkedRead(aReply.get_ReplyMarkeMessageRead().read());
   1.221 +      break;
   1.222 +    case MessageReply::TReplyMarkeMessageReadFail:
   1.223 +      mReplyRequest->NotifyMarkMessageReadFailed(aReply.get_ReplyMarkeMessageReadFail().error());
   1.224 +      break;
   1.225 +    case MessageReply::TReplyGetSegmentInfoForText: {
   1.226 +        const SmsSegmentInfoData& data =
   1.227 +          aReply.get_ReplyGetSegmentInfoForText().infoData();
   1.228 +        nsCOMPtr<nsIDOMMozSmsSegmentInfo> info = new SmsSegmentInfo(data);
   1.229 +        mReplyRequest->NotifySegmentInfoForTextGot(info);
   1.230 +      }
   1.231 +      break;
   1.232 +    case MessageReply::TReplyGetSegmentInfoForTextFail:
   1.233 +      mReplyRequest->NotifyGetSegmentInfoForTextFailed(
   1.234 +        aReply.get_ReplyGetSegmentInfoForTextFail().error());
   1.235 +      break;
   1.236 +    case MessageReply::TReplyGetSmscAddress:
   1.237 +      mReplyRequest->NotifyGetSmscAddress(aReply.get_ReplyGetSmscAddress().smscAddress());
   1.238 +      break;
   1.239 +    case MessageReply::TReplyGetSmscAddressFail:
   1.240 +      mReplyRequest->NotifyGetSmscAddressFailed(aReply.get_ReplyGetSmscAddressFail().error());
   1.241 +      break;
   1.242 +    default:
   1.243 +      MOZ_CRASH("Received invalid response parameters!");
   1.244 +  }
   1.245 +
   1.246 +  return true;
   1.247 +}
   1.248 +
   1.249 +/*******************************************************************************
   1.250 + * MobileMessageCursorChild
   1.251 + ******************************************************************************/
   1.252 +
   1.253 +NS_IMPL_ISUPPORTS(MobileMessageCursorChild, nsICursorContinueCallback)
   1.254 +
   1.255 +MobileMessageCursorChild::MobileMessageCursorChild(nsIMobileMessageCursorCallback* aCallback)
   1.256 +  : mCursorCallback(aCallback)
   1.257 +{
   1.258 +  MOZ_COUNT_CTOR(MobileMessageCursorChild);
   1.259 +  MOZ_ASSERT(aCallback);
   1.260 +}
   1.261 +
   1.262 +void
   1.263 +MobileMessageCursorChild::ActorDestroy(ActorDestroyReason aWhy)
   1.264 +{
   1.265 +  // Nothing needed here.
   1.266 +}
   1.267 +
   1.268 +bool
   1.269 +MobileMessageCursorChild::RecvNotifyResult(const MobileMessageCursorData& aData)
   1.270 +{
   1.271 +  MOZ_ASSERT(mCursorCallback);
   1.272 +
   1.273 +  nsCOMPtr<nsISupports> result;
   1.274 +  switch(aData.type()) {
   1.275 +    case MobileMessageCursorData::TMmsMessageData:
   1.276 +      result = new MmsMessage(aData.get_MmsMessageData());
   1.277 +      break;
   1.278 +    case MobileMessageCursorData::TSmsMessageData:
   1.279 +      result = new SmsMessage(aData.get_SmsMessageData());
   1.280 +      break;
   1.281 +    case MobileMessageCursorData::TThreadData:
   1.282 +      result = new MobileMessageThread(aData.get_ThreadData());
   1.283 +      break;
   1.284 +    default:
   1.285 +      MOZ_CRASH("Received invalid response parameters!");
   1.286 +  }
   1.287 +
   1.288 +  mCursorCallback->NotifyCursorResult(result);
   1.289 +  return true;
   1.290 +}
   1.291 +
   1.292 +bool
   1.293 +MobileMessageCursorChild::Recv__delete__(const int32_t& aError)
   1.294 +{
   1.295 +  MOZ_ASSERT(mCursorCallback);
   1.296 +
   1.297 +  if (aError != nsIMobileMessageCallback::SUCCESS_NO_ERROR) {
   1.298 +    mCursorCallback->NotifyCursorError(aError);
   1.299 +  } else {
   1.300 +    mCursorCallback->NotifyCursorDone();
   1.301 +  }
   1.302 +  mCursorCallback = nullptr;
   1.303 +
   1.304 +  return true;
   1.305 +}
   1.306 +
   1.307 +// nsICursorContinueCallback
   1.308 +
   1.309 +NS_IMETHODIMP
   1.310 +MobileMessageCursorChild::HandleContinue()
   1.311 +{
   1.312 +  MOZ_ASSERT(mCursorCallback);
   1.313 +
   1.314 +  SendContinue();
   1.315 +  return NS_OK;
   1.316 +}
   1.317 +
   1.318 +} // namespace mobilemessage
   1.319 +} // namespace dom
   1.320 +} // namespace mozilla

mercurial