dom/mobilemessage/src/MobileMessageThread.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 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "MobileMessageThread.h"
     7 #include "nsIDOMClassInfo.h"
     8 #include "jsapi.h"           // For OBJECT_TO_JSVAL and JS_NewDateObjectMsec
     9 #include "jsfriendapi.h"     // For js_DateGetMsecSinceEpoch
    10 #include "nsJSUtils.h"       // For nsDependentJSString
    11 #include "nsTArrayHelpers.h" // For nsTArrayToJSArray
    12 #include "mozilla/dom/mobilemessage/Constants.h" // For MessageType
    14 using namespace mozilla::dom::mobilemessage;
    16 DOMCI_DATA(MozMobileMessageThread, mozilla::dom::MobileMessageThread)
    18 namespace mozilla {
    19 namespace dom {
    21 NS_INTERFACE_MAP_BEGIN(MobileMessageThread)
    22   NS_INTERFACE_MAP_ENTRY(nsIDOMMozMobileMessageThread)
    23   NS_INTERFACE_MAP_ENTRY(nsISupports)
    24   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozMobileMessageThread)
    25 NS_INTERFACE_MAP_END
    27 NS_IMPL_ADDREF(MobileMessageThread)
    28 NS_IMPL_RELEASE(MobileMessageThread)
    30 /* static */ nsresult
    31 MobileMessageThread::Create(uint64_t aId,
    32                             const JS::Value& aParticipants,
    33                             uint64_t aTimestamp,
    34                             const nsAString& aLastMessageSubject,
    35                             const nsAString& aBody,
    36                             uint64_t aUnreadCount,
    37                             const nsAString& aLastMessageType,
    38                             JSContext* aCx,
    39                             nsIDOMMozMobileMessageThread** aThread)
    40 {
    41   *aThread = nullptr;
    43   // ThreadData exposes these as references, so we can simply assign
    44   // to them.
    45   ThreadData data;
    46   data.id() = aId;
    47   data.lastMessageSubject().Assign(aLastMessageSubject);
    48   data.body().Assign(aBody);
    49   data.unreadCount() = aUnreadCount;
    51   // Participants.
    52   {
    53     if (!aParticipants.isObject()) {
    54       return NS_ERROR_INVALID_ARG;
    55     }
    57     JS::Rooted<JSObject*> obj(aCx, &aParticipants.toObject());
    58     if (!JS_IsArrayObject(aCx, obj)) {
    59       return NS_ERROR_INVALID_ARG;
    60     }
    62     uint32_t length;
    63     MOZ_ALWAYS_TRUE(JS_GetArrayLength(aCx, obj, &length));
    64     NS_ENSURE_TRUE(length, NS_ERROR_INVALID_ARG);
    66     for (uint32_t i = 0; i < length; ++i) {
    67       JS::Rooted<JS::Value> val(aCx);
    69       if (!JS_GetElement(aCx, obj, i, &val) || !val.isString()) {
    70         return NS_ERROR_INVALID_ARG;
    71       }
    73       nsDependentJSString str;
    74       str.init(aCx, val.toString());
    75       data.participants().AppendElement(str);
    76     }
    77   }
    79   // Set |timestamp|;
    80   data.timestamp() = aTimestamp;
    82   // Set |lastMessageType|.
    83   {
    84     MessageType lastMessageType;
    85     if (aLastMessageType.Equals(MESSAGE_TYPE_SMS)) {
    86       lastMessageType = eMessageType_SMS;
    87     } else if (aLastMessageType.Equals(MESSAGE_TYPE_MMS)) {
    88       lastMessageType = eMessageType_MMS;
    89     } else {
    90       return NS_ERROR_INVALID_ARG;
    91     }
    92     data.lastMessageType() = lastMessageType;
    93   }
    95   nsCOMPtr<nsIDOMMozMobileMessageThread> thread = new MobileMessageThread(data);
    96   thread.forget(aThread);
    97   return NS_OK;
    98 }
   100 MobileMessageThread::MobileMessageThread(uint64_t aId,
   101                                          const nsTArray<nsString>& aParticipants,
   102                                          uint64_t aTimestamp,
   103                                          const nsString& aLastMessageSubject,
   104                                          const nsString& aBody,
   105                                          uint64_t aUnreadCount,
   106                                          MessageType aLastMessageType)
   107   : mData(aId, aParticipants, aTimestamp, aLastMessageSubject, aBody,
   108           aUnreadCount, aLastMessageType)
   109 {
   110   MOZ_ASSERT(aParticipants.Length());
   111 }
   113 MobileMessageThread::MobileMessageThread(const ThreadData& aData)
   114   : mData(aData)
   115 {
   116   MOZ_ASSERT(aData.participants().Length());
   117 }
   119 NS_IMETHODIMP
   120 MobileMessageThread::GetId(uint64_t* aId)
   121 {
   122   *aId = mData.id();
   123   return NS_OK;
   124 }
   126 NS_IMETHODIMP
   127 MobileMessageThread::GetLastMessageSubject(nsAString& aLastMessageSubject)
   128 {
   129   aLastMessageSubject = mData.lastMessageSubject();
   130   return NS_OK;
   131 }
   133 NS_IMETHODIMP
   134 MobileMessageThread::GetBody(nsAString& aBody)
   135 {
   136   aBody = mData.body();
   137   return NS_OK;
   138 }
   140 NS_IMETHODIMP
   141 MobileMessageThread::GetUnreadCount(uint64_t* aUnreadCount)
   142 {
   143   *aUnreadCount = mData.unreadCount();
   144   return NS_OK;
   145 }
   147 NS_IMETHODIMP
   148 MobileMessageThread::GetParticipants(JSContext* aCx,
   149                                      JS::MutableHandle<JS::Value> aParticipants)
   150 {
   151   JS::Rooted<JSObject*> obj(aCx);
   153   nsresult rv = nsTArrayToJSArray(aCx, mData.participants(), obj.address());
   154   NS_ENSURE_SUCCESS(rv, rv);
   156   aParticipants.setObject(*obj);
   157   return NS_OK;
   158 }
   160 NS_IMETHODIMP
   161 MobileMessageThread::GetTimestamp(DOMTimeStamp* aDate)
   162 {
   163   *aDate = mData.timestamp();
   164   return NS_OK;
   165 }
   167 NS_IMETHODIMP
   168 MobileMessageThread::GetLastMessageType(nsAString& aLastMessageType)
   169 {
   170   switch (mData.lastMessageType()) {
   171     case eMessageType_SMS:
   172       aLastMessageType = MESSAGE_TYPE_SMS;
   173       break;
   174     case eMessageType_MMS:
   175       aLastMessageType = MESSAGE_TYPE_MMS;
   176       break;
   177     case eMessageType_EndGuard:
   178     default:
   179       MOZ_CRASH("We shouldn't get any other message type!");
   180   }
   182   return NS_OK;
   183 }
   185 } // namespace dom
   186 } // namespace mozilla

mercurial