LCOV - code coverage report
Current view: directory - dom/sms/src - SmsMessage.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 64 58 90.6 %
Date: 2012-06-02 Functions: 13 11 84.6 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is Mozilla Foundation
      18                 :  * Portions created by the Initial Developer are Copyright (C) 2011
      19                 :  * the Initial Developer. All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *   Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
      23                 :  *   Philipp von Weitershausen <philipp@weitershausen.de>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      27                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #include "SmsMessage.h"
      40                 : #include "nsIDOMClassInfo.h"
      41                 : #include "jsapi.h" // For OBJECT_TO_JSVAL and JS_NewDateObjectMsec
      42                 : #include "jsfriendapi.h" // For js_DateGetMsecSinceEpoch
      43                 : #include "Constants.h"
      44                 : 
      45                 : DOMCI_DATA(MozSmsMessage, mozilla::dom::sms::SmsMessage)
      46                 : 
      47                 : namespace mozilla {
      48                 : namespace dom {
      49                 : namespace sms {
      50                 : 
      51              28 : NS_INTERFACE_MAP_BEGIN(SmsMessage)
      52              28 :   NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsMessage)
      53              20 :   NS_INTERFACE_MAP_ENTRY(nsISupports)
      54              16 :   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsMessage)
      55              12 : NS_INTERFACE_MAP_END
      56                 : 
      57              16 : NS_IMPL_ADDREF(SmsMessage)
      58              20 : NS_IMPL_RELEASE(SmsMessage)
      59                 : 
      60               0 : SmsMessage::SmsMessage(PRInt32 aId, DeliveryState aDelivery,
      61                 :                        const nsString& aSender, const nsString& aReceiver,
      62                 :                        const nsString& aBody, PRUint64 aTimestamp)
      63               0 :   : mData(aId, aDelivery, aSender, aReceiver, aBody, aTimestamp)
      64                 : {
      65               0 : }
      66                 : 
      67               4 : SmsMessage::SmsMessage(const SmsMessageData& aData)
      68               4 :   : mData(aData)
      69                 : {
      70               4 : }
      71                 : 
      72                 : /* static */ nsresult
      73              10 : SmsMessage::Create(PRInt32 aId,
      74                 :                    const nsAString& aDelivery,
      75                 :                    const nsAString& aSender,
      76                 :                    const nsAString& aReceiver,
      77                 :                    const nsAString& aBody,
      78                 :                    const jsval& aTimestamp,
      79                 :                    JSContext* aCx,
      80                 :                    nsIDOMMozSmsMessage** aMessage)
      81                 : {
      82              10 :   *aMessage = nsnull;
      83                 : 
      84                 :   // SmsMessageData exposes these as references, so we can simply assign
      85                 :   // to them.
      86              20 :   SmsMessageData data;
      87              10 :   data.id() = aId;
      88              10 :   data.sender() = nsString(aSender);
      89              10 :   data.receiver() = nsString(aReceiver);
      90              10 :   data.body() = nsString(aBody);
      91                 : 
      92              10 :   if (aDelivery.Equals(DELIVERY_RECEIVED)) {
      93               1 :     data.delivery() = eDeliveryState_Received;
      94               9 :   } else if (aDelivery.Equals(DELIVERY_SENT)) {
      95               7 :     data.delivery() = eDeliveryState_Sent;
      96                 :   } else {
      97               2 :     return NS_ERROR_INVALID_ARG;
      98                 :   }
      99                 : 
     100                 :   // We support both a Date object and a millisecond timestamp as a number.
     101               8 :   if (aTimestamp.isObject()) {
     102               4 :     JSObject& obj = aTimestamp.toObject();
     103               4 :     if (!JS_ObjectIsDate(aCx, &obj)) {
     104               1 :       return NS_ERROR_INVALID_ARG;
     105                 :     }
     106               3 :     data.timestamp() = js_DateGetMsecSinceEpoch(aCx, &obj);
     107                 :   } else {
     108               4 :     if (!aTimestamp.isNumber()) {
     109               2 :       return NS_ERROR_INVALID_ARG;
     110                 :     }
     111               2 :     double number = aTimestamp.toNumber();
     112               2 :     if (static_cast<PRUint64>(number) != number) {
     113               1 :       return NS_ERROR_INVALID_ARG;
     114                 :     }
     115               1 :     data.timestamp() = static_cast<PRUint64>(number);
     116                 :   }
     117                 : 
     118               8 :   nsCOMPtr<nsIDOMMozSmsMessage> message = new SmsMessage(data);
     119               4 :   message.swap(*aMessage);
     120               4 :   return NS_OK;
     121                 : }
     122                 : 
     123                 : const SmsMessageData&
     124               0 : SmsMessage::GetData() const
     125                 : {
     126               0 :   return mData;
     127                 : }
     128                 : 
     129                 : NS_IMETHODIMP
     130               4 : SmsMessage::GetId(PRInt32* aId)
     131                 : {
     132               4 :   *aId = mData.id();
     133               4 :   return NS_OK;
     134                 : }
     135                 : 
     136                 : NS_IMETHODIMP
     137               4 : SmsMessage::GetDelivery(nsAString& aDelivery)
     138                 : {
     139               4 :   switch (mData.delivery()) {
     140                 :     case eDeliveryState_Received:
     141               1 :       aDelivery = DELIVERY_RECEIVED;
     142               1 :       break;
     143                 :     case eDeliveryState_Sent:
     144               3 :       aDelivery = DELIVERY_SENT;
     145               3 :       break;
     146                 :     case eDeliveryState_Unknown:
     147                 :     case eDeliveryState_EndGuard:
     148                 :     default:
     149                 :       NS_ASSERTION(true, "We shouldn't get any other delivery state!");
     150               0 :       return NS_ERROR_UNEXPECTED;
     151                 :   }
     152                 : 
     153               4 :   return NS_OK;
     154                 : }
     155                 : 
     156                 : NS_IMETHODIMP
     157               4 : SmsMessage::GetSender(nsAString& aSender)
     158                 : {
     159               4 :   aSender = mData.sender();
     160               4 :   return NS_OK;
     161                 : }
     162                 : 
     163                 : NS_IMETHODIMP
     164               4 : SmsMessage::GetReceiver(nsAString& aReceiver)
     165                 : {
     166               4 :   aReceiver = mData.receiver();
     167               4 :   return NS_OK;
     168                 : }
     169                 : 
     170                 : NS_IMETHODIMP
     171               4 : SmsMessage::GetBody(nsAString& aBody)
     172                 : {
     173               4 :   aBody = mData.body();
     174               4 :   return NS_OK;
     175                 : }
     176                 : 
     177                 : NS_IMETHODIMP
     178               7 : SmsMessage::GetTimestamp(JSContext* cx, jsval* aDate)
     179                 : {
     180               7 :   *aDate = OBJECT_TO_JSVAL(JS_NewDateObjectMsec(cx, mData.timestamp()));
     181               7 :   return NS_OK;
     182                 : }
     183                 : 
     184                 : } // namespace sms
     185                 : } // namespace dom
     186                 : } // namespace mozilla

Generated by: LCOV version 1.7