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