1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/mobilemessage/src/MobileMessageThread.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,186 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "MobileMessageThread.h" 1.10 +#include "nsIDOMClassInfo.h" 1.11 +#include "jsapi.h" // For OBJECT_TO_JSVAL and JS_NewDateObjectMsec 1.12 +#include "jsfriendapi.h" // For js_DateGetMsecSinceEpoch 1.13 +#include "nsJSUtils.h" // For nsDependentJSString 1.14 +#include "nsTArrayHelpers.h" // For nsTArrayToJSArray 1.15 +#include "mozilla/dom/mobilemessage/Constants.h" // For MessageType 1.16 + 1.17 +using namespace mozilla::dom::mobilemessage; 1.18 + 1.19 +DOMCI_DATA(MozMobileMessageThread, mozilla::dom::MobileMessageThread) 1.20 + 1.21 +namespace mozilla { 1.22 +namespace dom { 1.23 + 1.24 +NS_INTERFACE_MAP_BEGIN(MobileMessageThread) 1.25 + NS_INTERFACE_MAP_ENTRY(nsIDOMMozMobileMessageThread) 1.26 + NS_INTERFACE_MAP_ENTRY(nsISupports) 1.27 + NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozMobileMessageThread) 1.28 +NS_INTERFACE_MAP_END 1.29 + 1.30 +NS_IMPL_ADDREF(MobileMessageThread) 1.31 +NS_IMPL_RELEASE(MobileMessageThread) 1.32 + 1.33 +/* static */ nsresult 1.34 +MobileMessageThread::Create(uint64_t aId, 1.35 + const JS::Value& aParticipants, 1.36 + uint64_t aTimestamp, 1.37 + const nsAString& aLastMessageSubject, 1.38 + const nsAString& aBody, 1.39 + uint64_t aUnreadCount, 1.40 + const nsAString& aLastMessageType, 1.41 + JSContext* aCx, 1.42 + nsIDOMMozMobileMessageThread** aThread) 1.43 +{ 1.44 + *aThread = nullptr; 1.45 + 1.46 + // ThreadData exposes these as references, so we can simply assign 1.47 + // to them. 1.48 + ThreadData data; 1.49 + data.id() = aId; 1.50 + data.lastMessageSubject().Assign(aLastMessageSubject); 1.51 + data.body().Assign(aBody); 1.52 + data.unreadCount() = aUnreadCount; 1.53 + 1.54 + // Participants. 1.55 + { 1.56 + if (!aParticipants.isObject()) { 1.57 + return NS_ERROR_INVALID_ARG; 1.58 + } 1.59 + 1.60 + JS::Rooted<JSObject*> obj(aCx, &aParticipants.toObject()); 1.61 + if (!JS_IsArrayObject(aCx, obj)) { 1.62 + return NS_ERROR_INVALID_ARG; 1.63 + } 1.64 + 1.65 + uint32_t length; 1.66 + MOZ_ALWAYS_TRUE(JS_GetArrayLength(aCx, obj, &length)); 1.67 + NS_ENSURE_TRUE(length, NS_ERROR_INVALID_ARG); 1.68 + 1.69 + for (uint32_t i = 0; i < length; ++i) { 1.70 + JS::Rooted<JS::Value> val(aCx); 1.71 + 1.72 + if (!JS_GetElement(aCx, obj, i, &val) || !val.isString()) { 1.73 + return NS_ERROR_INVALID_ARG; 1.74 + } 1.75 + 1.76 + nsDependentJSString str; 1.77 + str.init(aCx, val.toString()); 1.78 + data.participants().AppendElement(str); 1.79 + } 1.80 + } 1.81 + 1.82 + // Set |timestamp|; 1.83 + data.timestamp() = aTimestamp; 1.84 + 1.85 + // Set |lastMessageType|. 1.86 + { 1.87 + MessageType lastMessageType; 1.88 + if (aLastMessageType.Equals(MESSAGE_TYPE_SMS)) { 1.89 + lastMessageType = eMessageType_SMS; 1.90 + } else if (aLastMessageType.Equals(MESSAGE_TYPE_MMS)) { 1.91 + lastMessageType = eMessageType_MMS; 1.92 + } else { 1.93 + return NS_ERROR_INVALID_ARG; 1.94 + } 1.95 + data.lastMessageType() = lastMessageType; 1.96 + } 1.97 + 1.98 + nsCOMPtr<nsIDOMMozMobileMessageThread> thread = new MobileMessageThread(data); 1.99 + thread.forget(aThread); 1.100 + return NS_OK; 1.101 +} 1.102 + 1.103 +MobileMessageThread::MobileMessageThread(uint64_t aId, 1.104 + const nsTArray<nsString>& aParticipants, 1.105 + uint64_t aTimestamp, 1.106 + const nsString& aLastMessageSubject, 1.107 + const nsString& aBody, 1.108 + uint64_t aUnreadCount, 1.109 + MessageType aLastMessageType) 1.110 + : mData(aId, aParticipants, aTimestamp, aLastMessageSubject, aBody, 1.111 + aUnreadCount, aLastMessageType) 1.112 +{ 1.113 + MOZ_ASSERT(aParticipants.Length()); 1.114 +} 1.115 + 1.116 +MobileMessageThread::MobileMessageThread(const ThreadData& aData) 1.117 + : mData(aData) 1.118 +{ 1.119 + MOZ_ASSERT(aData.participants().Length()); 1.120 +} 1.121 + 1.122 +NS_IMETHODIMP 1.123 +MobileMessageThread::GetId(uint64_t* aId) 1.124 +{ 1.125 + *aId = mData.id(); 1.126 + return NS_OK; 1.127 +} 1.128 + 1.129 +NS_IMETHODIMP 1.130 +MobileMessageThread::GetLastMessageSubject(nsAString& aLastMessageSubject) 1.131 +{ 1.132 + aLastMessageSubject = mData.lastMessageSubject(); 1.133 + return NS_OK; 1.134 +} 1.135 + 1.136 +NS_IMETHODIMP 1.137 +MobileMessageThread::GetBody(nsAString& aBody) 1.138 +{ 1.139 + aBody = mData.body(); 1.140 + return NS_OK; 1.141 +} 1.142 + 1.143 +NS_IMETHODIMP 1.144 +MobileMessageThread::GetUnreadCount(uint64_t* aUnreadCount) 1.145 +{ 1.146 + *aUnreadCount = mData.unreadCount(); 1.147 + return NS_OK; 1.148 +} 1.149 + 1.150 +NS_IMETHODIMP 1.151 +MobileMessageThread::GetParticipants(JSContext* aCx, 1.152 + JS::MutableHandle<JS::Value> aParticipants) 1.153 +{ 1.154 + JS::Rooted<JSObject*> obj(aCx); 1.155 + 1.156 + nsresult rv = nsTArrayToJSArray(aCx, mData.participants(), obj.address()); 1.157 + NS_ENSURE_SUCCESS(rv, rv); 1.158 + 1.159 + aParticipants.setObject(*obj); 1.160 + return NS_OK; 1.161 +} 1.162 + 1.163 +NS_IMETHODIMP 1.164 +MobileMessageThread::GetTimestamp(DOMTimeStamp* aDate) 1.165 +{ 1.166 + *aDate = mData.timestamp(); 1.167 + return NS_OK; 1.168 +} 1.169 + 1.170 +NS_IMETHODIMP 1.171 +MobileMessageThread::GetLastMessageType(nsAString& aLastMessageType) 1.172 +{ 1.173 + switch (mData.lastMessageType()) { 1.174 + case eMessageType_SMS: 1.175 + aLastMessageType = MESSAGE_TYPE_SMS; 1.176 + break; 1.177 + case eMessageType_MMS: 1.178 + aLastMessageType = MESSAGE_TYPE_MMS; 1.179 + break; 1.180 + case eMessageType_EndGuard: 1.181 + default: 1.182 + MOZ_CRASH("We shouldn't get any other message type!"); 1.183 + } 1.184 + 1.185 + return NS_OK; 1.186 +} 1.187 + 1.188 +} // namespace dom 1.189 +} // namespace mozilla