gecko-dev/dom/mobilemessage/ipc/SmsIPCService.cpp

364 строки
10 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/ContentChild.h"
#include "SmsIPCService.h"
#include "nsXULAppAPI.h"
Bug 844429 - B2G SMS & MMS: move SMS codes into dom/mobilemessage to make it generic for MMS. r=vicamo --HG-- rename : dom/sms/interfaces/nsIDOMMozSmsEvent.idl => dom/mobilemessage/interfaces/nsIDOMMozSmsEvent.idl rename : dom/sms/interfaces/nsIDOMMozSmsMessage.idl => dom/mobilemessage/interfaces/nsIDOMMozSmsMessage.idl rename : dom/sms/interfaces/nsIDOMNavigatorSms.idl => dom/mobilemessage/interfaces/nsIDOMNavigatorSms.idl rename : dom/sms/interfaces/nsIDOMSmsCursor.idl => dom/mobilemessage/interfaces/nsIDOMSmsCursor.idl rename : dom/sms/interfaces/nsIDOMSmsFilter.idl => dom/mobilemessage/interfaces/nsIDOMSmsFilter.idl rename : dom/sms/interfaces/nsIDOMSmsManager.idl => dom/mobilemessage/interfaces/nsIDOMSmsManager.idl rename : dom/sms/interfaces/nsIDOMSmsRequest.idl => dom/mobilemessage/interfaces/nsIDOMSmsRequest.idl rename : dom/sms/interfaces/nsIDOMSmsSegmentInfo.idl => dom/mobilemessage/interfaces/nsIDOMSmsSegmentInfo.idl rename : dom/sms/interfaces/nsISmsRequest.idl => dom/mobilemessage/interfaces/nsISmsRequest.idl rename : dom/sms/interfaces/nsISmsService.idl => dom/mobilemessage/interfaces/nsISmsService.idl rename : dom/sms/src/Constants.cpp => dom/mobilemessage/src/Constants.cpp rename : dom/sms/src/Constants.h => dom/mobilemessage/src/Constants.h rename : dom/sms/src/SmsCursor.cpp => dom/mobilemessage/src/SmsCursor.cpp rename : dom/sms/src/SmsCursor.h => dom/mobilemessage/src/SmsCursor.h rename : dom/sms/src/SmsFilter.cpp => dom/mobilemessage/src/SmsFilter.cpp rename : dom/sms/src/SmsFilter.h => dom/mobilemessage/src/SmsFilter.h rename : dom/sms/src/SmsManager.cpp => dom/mobilemessage/src/SmsManager.cpp rename : dom/sms/src/SmsManager.h => dom/mobilemessage/src/SmsManager.h rename : dom/sms/src/SmsMessage.cpp => dom/mobilemessage/src/SmsMessage.cpp rename : dom/sms/src/SmsMessage.h => dom/mobilemessage/src/SmsMessage.h rename : dom/sms/src/SmsRequest.cpp => dom/mobilemessage/src/SmsRequest.cpp rename : dom/sms/src/SmsRequest.h => dom/mobilemessage/src/SmsRequest.h rename : dom/sms/src/SmsSegmentInfo.cpp => dom/mobilemessage/src/SmsSegmentInfo.cpp rename : dom/sms/src/SmsSegmentInfo.h => dom/mobilemessage/src/SmsSegmentInfo.h rename : dom/sms/src/SmsServicesFactory.cpp => dom/mobilemessage/src/SmsServicesFactory.cpp rename : dom/sms/src/SmsServicesFactory.h => dom/mobilemessage/src/SmsServicesFactory.h rename : dom/sms/src/Types.h => dom/mobilemessage/src/Types.h rename : dom/sms/src/android/SmsService.cpp => dom/mobilemessage/src/android/SmsService.cpp rename : dom/sms/src/android/SmsService.h => dom/mobilemessage/src/android/SmsService.h rename : dom/sms/src/fallback/SmsService.cpp => dom/mobilemessage/src/fallback/SmsService.cpp rename : dom/sms/src/fallback/SmsService.h => dom/mobilemessage/src/fallback/SmsService.h rename : dom/sms/src/ipc/PSms.ipdl => dom/mobilemessage/src/ipc/PSms.ipdl rename : dom/sms/src/ipc/PSmsRequest.ipdl => dom/mobilemessage/src/ipc/PSmsRequest.ipdl rename : dom/sms/src/ipc/SmsChild.cpp => dom/mobilemessage/src/ipc/SmsChild.cpp rename : dom/sms/src/ipc/SmsChild.h => dom/mobilemessage/src/ipc/SmsChild.h rename : dom/sms/src/ipc/SmsIPCService.cpp => dom/mobilemessage/src/ipc/SmsIPCService.cpp rename : dom/sms/src/ipc/SmsIPCService.h => dom/mobilemessage/src/ipc/SmsIPCService.h rename : dom/sms/src/ipc/SmsParent.cpp => dom/mobilemessage/src/ipc/SmsParent.cpp rename : dom/sms/src/ipc/SmsParent.h => dom/mobilemessage/src/ipc/SmsParent.h rename : dom/sms/src/ipc/SmsTypes.ipdlh => dom/mobilemessage/src/ipc/SmsTypes.ipdlh rename : dom/sms/src/ipc/ipdl.mk => dom/mobilemessage/src/ipc/ipdl.mk rename : dom/sms/src/ril/SmsService.cpp => dom/mobilemessage/src/ril/SmsService.cpp rename : dom/sms/src/ril/SmsService.h => dom/mobilemessage/src/ril/SmsService.h rename : dom/sms/tests/Makefile.in => dom/mobilemessage/tests/Makefile.in rename : dom/sms/tests/marionette/manifest.ini => dom/mobilemessage/tests/marionette/manifest.ini rename : dom/sms/tests/marionette/test_between_emulators.py => dom/mobilemessage/tests/marionette/test_between_emulators.py rename : dom/sms/tests/marionette/test_bug814761.js => dom/mobilemessage/tests/marionette/test_bug814761.js rename : dom/sms/tests/marionette/test_emulator_loopback.js => dom/mobilemessage/tests/marionette/test_emulator_loopback.js rename : dom/sms/tests/marionette/test_filter_date.js => dom/mobilemessage/tests/marionette/test_filter_date.js rename : dom/sms/tests/marionette/test_filter_date_notfound.js => dom/mobilemessage/tests/marionette/test_filter_date_notfound.js rename : dom/sms/tests/marionette/test_filter_mixed.js => dom/mobilemessage/tests/marionette/test_filter_mixed.js rename : dom/sms/tests/marionette/test_filter_number_multiple.js => dom/mobilemessage/tests/marionette/test_filter_number_multiple.js rename : dom/sms/tests/marionette/test_filter_number_single.js => dom/mobilemessage/tests/marionette/test_filter_number_single.js rename : dom/sms/tests/marionette/test_filter_read.js => dom/mobilemessage/tests/marionette/test_filter_read.js rename : dom/sms/tests/marionette/test_filter_received.js => dom/mobilemessage/tests/marionette/test_filter_received.js rename : dom/sms/tests/marionette/test_filter_sent.js => dom/mobilemessage/tests/marionette/test_filter_sent.js rename : dom/sms/tests/marionette/test_filter_unread.js => dom/mobilemessage/tests/marionette/test_filter_unread.js rename : dom/sms/tests/marionette/test_getmessage.js => dom/mobilemessage/tests/marionette/test_getmessage.js rename : dom/sms/tests/marionette/test_getmessage_notfound.js => dom/mobilemessage/tests/marionette/test_getmessage_notfound.js rename : dom/sms/tests/marionette/test_getmessages.js => dom/mobilemessage/tests/marionette/test_getmessages.js rename : dom/sms/tests/marionette/test_incoming.js => dom/mobilemessage/tests/marionette/test_incoming.js rename : dom/sms/tests/marionette/test_incoming_delete.js => dom/mobilemessage/tests/marionette/test_incoming_delete.js rename : dom/sms/tests/marionette/test_incoming_max_segments.js => dom/mobilemessage/tests/marionette/test_incoming_max_segments.js rename : dom/sms/tests/marionette/test_incoming_multipart.js => dom/mobilemessage/tests/marionette/test_incoming_multipart.js rename : dom/sms/tests/marionette/test_mark_msg_read.js => dom/mobilemessage/tests/marionette/test_mark_msg_read.js rename : dom/sms/tests/marionette/test_mark_msg_read_error.js => dom/mobilemessage/tests/marionette/test_mark_msg_read_error.js rename : dom/sms/tests/marionette/test_message_classes.js => dom/mobilemessage/tests/marionette/test_message_classes.js rename : dom/sms/tests/marionette/test_outgoing.js => dom/mobilemessage/tests/marionette/test_outgoing.js rename : dom/sms/tests/marionette/test_outgoing_delete.js => dom/mobilemessage/tests/marionette/test_outgoing_delete.js rename : dom/sms/tests/marionette/test_outgoing_max_segments.js => dom/mobilemessage/tests/marionette/test_outgoing_max_segments.js rename : dom/sms/tests/marionette/test_segment_info.js => dom/mobilemessage/tests/marionette/test_segment_info.js rename : dom/sms/tests/marionette/test_strict_7bit_encoding.js => dom/mobilemessage/tests/marionette/test_strict_7bit_encoding.js rename : dom/sms/tests/moz.build => dom/mobilemessage/tests/moz.build rename : dom/sms/tests/test_sms_basics.html => dom/mobilemessage/tests/test_sms_basics.html rename : dom/sms/tests/test_smsdatabaseservice.xul => dom/mobilemessage/tests/test_smsdatabaseservice.xul rename : dom/sms/tests/test_smsfilter.html => dom/mobilemessage/tests/test_smsfilter.html rename : dom/sms/tests/test_smsservice_createsmsmessage.js => dom/mobilemessage/tests/test_smsservice_createsmsmessage.js rename : dom/sms/tests/xpcshell.ini => dom/mobilemessage/tests/xpcshell.ini
2013-03-01 12:38:47 +04:00
#include "mozilla/dom/mobilemessage/SmsChild.h"
#include "SmsMessage.h"
#include "nsJSUtils.h"
#include "mozilla/dom/MozMobileMessageManagerBinding.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/Preferences.h"
#include "nsString.h"
2013-04-09 20:36:04 +04:00
using namespace mozilla::dom;
using namespace mozilla::dom::mobilemessage;
2013-04-09 20:36:04 +04:00
namespace {
const char* kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces";
#define kPrefMmsDefaultServiceId "dom.mms.defaultServiceId"
#define kPrefSmsDefaultServiceId "dom.sms.defaultServiceId"
const char* kObservedPrefs[] = {
kPrefMmsDefaultServiceId,
kPrefSmsDefaultServiceId,
nullptr
};
2013-04-09 20:36:04 +04:00
// TODO: Bug 767082 - WebSMS: sSmsChild leaks at shutdown
PSmsChild* gSmsChild;
2013-04-09 20:36:04 +04:00
PSmsChild*
GetSmsChild()
{
MOZ_ASSERT(NS_IsMainThread());
2013-04-09 20:36:04 +04:00
if (!gSmsChild) {
gSmsChild = ContentChild::GetSingleton()->SendPSmsConstructor();
2013-04-09 20:36:04 +04:00
NS_WARN_IF_FALSE(gSmsChild,
"Calling methods on SmsIPCService during shutdown!");
}
2013-04-09 20:36:04 +04:00
return gSmsChild;
}
2013-04-09 20:36:04 +04:00
nsresult
SendRequest(const IPCSmsRequest& aRequest,
nsIMobileMessageCallback* aRequestReply)
2013-04-09 06:21:35 +04:00
{
2013-04-09 20:36:04 +04:00
PSmsChild* smsChild = GetSmsChild();
NS_ENSURE_TRUE(smsChild, NS_ERROR_FAILURE);
2013-04-09 06:21:35 +04:00
2013-04-09 20:36:04 +04:00
SmsRequestChild* actor = new SmsRequestChild(aRequestReply);
smsChild->SendPSmsRequestConstructor(actor, aRequest);
return NS_OK;
}
nsresult
2013-04-09 20:37:58 +04:00
SendCursorRequest(const IPCMobileMessageCursor& aRequest,
2013-04-09 20:36:04 +04:00
nsIMobileMessageCursorCallback* aRequestReply,
nsICursorContinueCallback** aResult)
{
PSmsChild* smsChild = GetSmsChild();
NS_ENSURE_TRUE(smsChild, NS_ERROR_FAILURE);
nsRefPtr<MobileMessageCursorChild> actor =
new MobileMessageCursorChild(aRequestReply);
// Add an extra ref for IPDL. Will be released in
// SmsChild::DeallocPMobileMessageCursor().
actor->AddRef();
smsChild->SendPMobileMessageCursorConstructor(actor, aRequest);
actor.forget(aResult);
return NS_OK;
}
uint32_t
getDefaultServiceId(const char* aPrefKey)
{
int32_t id = mozilla::Preferences::GetInt(aPrefKey, 0);
int32_t numRil = mozilla::Preferences::GetInt(kPrefRilNumRadioInterfaces, 1);
if (id >= numRil || id < 0) {
id = 0;
}
return id;
}
2013-04-09 20:36:04 +04:00
} // anonymous namespace
NS_IMPL_ISUPPORTS(SmsIPCService,
nsISmsService,
nsIMmsService,
nsIMobileMessageDatabaseService,
nsIObserver)
SmsIPCService::SmsIPCService()
{
Preferences::AddStrongObservers(this, kObservedPrefs);
mMmsDefaultServiceId = getDefaultServiceId(kPrefMmsDefaultServiceId);
mSmsDefaultServiceId = getDefaultServiceId(kPrefSmsDefaultServiceId);
}
/*
* Implementation of nsIObserver.
*/
NS_IMETHODIMP
SmsIPCService::Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aData)
{
if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
nsDependentString data(aData);
if (data.EqualsLiteral(kPrefMmsDefaultServiceId)) {
mMmsDefaultServiceId = getDefaultServiceId(kPrefMmsDefaultServiceId);
} else if (data.EqualsLiteral(kPrefSmsDefaultServiceId)) {
mSmsDefaultServiceId = getDefaultServiceId(kPrefSmsDefaultServiceId);
}
return NS_OK;
}
MOZ_ASSERT(false, "SmsIPCService got unexpected topic!");
return NS_ERROR_UNEXPECTED;
}
2013-04-09 20:36:04 +04:00
/*
* Implementation of nsISmsService.
*/
NS_IMETHODIMP
SmsIPCService::GetSmsDefaultServiceId(uint32_t* aServiceId)
{
*aServiceId = mSmsDefaultServiceId;
return NS_OK;
}
NS_IMETHODIMP
SmsIPCService::GetSegmentInfoForText(const nsAString& aText,
nsIMobileMessageCallback* aRequest)
{
return SendRequest(GetSegmentInfoForTextRequest(nsString(aText)),
aRequest);
}
NS_IMETHODIMP
SmsIPCService::GetSmscAddress(uint32_t aServiceId,
nsIMobileMessageCallback* aRequest)
{
return SendRequest(GetSmscAddressRequest(aServiceId), aRequest);
}
NS_IMETHODIMP
SmsIPCService::Send(uint32_t aServiceId,
const nsAString& aNumber,
const nsAString& aMessage,
bool aSilent,
nsIMobileMessageCallback* aRequest)
{
return SendRequest(SendMessageRequest(SendSmsMessageRequest(aServiceId,
nsString(aNumber),
nsString(aMessage),
aSilent)),
aRequest);
}
NS_IMETHODIMP
SmsIPCService::IsSilentNumber(const nsAString& aNumber,
bool* aIsSilent)
{
NS_ERROR("We should not be here!");
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
SmsIPCService::AddSilentNumber(const nsAString& aNumber)
{
PSmsChild* smsChild = GetSmsChild();
NS_ENSURE_TRUE(smsChild, NS_ERROR_FAILURE);
smsChild->SendAddSilentNumber(nsString(aNumber));
return NS_OK;
}
NS_IMETHODIMP
SmsIPCService::RemoveSilentNumber(const nsAString& aNumber)
{
PSmsChild* smsChild = GetSmsChild();
NS_ENSURE_TRUE(smsChild, NS_ERROR_FAILURE);
smsChild->SendRemoveSilentNumber(nsString(aNumber));
return NS_OK;
}
/*
* Implementation of nsIMobileMessageDatabaseService.
*/
NS_IMETHODIMP
SmsIPCService::GetMessageMoz(int32_t aMessageId,
nsIMobileMessageCallback* aRequest)
{
2013-04-09 20:36:04 +04:00
return SendRequest(GetMessageRequest(aMessageId), aRequest);
}
NS_IMETHODIMP
SmsIPCService::DeleteMessage(int32_t *aMessageIds, uint32_t aSize,
nsIMobileMessageCallback* aRequest)
{
DeleteMessageRequest data;
data.messageIds().AppendElements(aMessageIds, aSize);
return SendRequest(data, aRequest);
}
NS_IMETHODIMP
SmsIPCService::CreateMessageCursor(bool aHasStartDate,
uint64_t aStartDate,
bool aHasEndDate,
uint64_t aEndDate,
const char16_t** aNumbers,
uint32_t aNumbersCount,
const nsAString& aDelivery,
bool aHasRead,
bool aRead,
uint64_t aThreadId,
2013-04-09 20:36:04 +04:00
bool aReverse,
nsIMobileMessageCursorCallback* aCursorCallback,
nsICursorContinueCallback** aResult)
{
SmsFilterData data;
data.hasStartDate() = aHasStartDate;
data.startDate() = aStartDate;
data.hasEndDate() = aHasEndDate;
data.startDate() = aEndDate;
if (aNumbersCount && aNumbers) {
nsTArray<nsString>& numbers = data.numbers();
uint32_t index;
for (index = 0; index < aNumbersCount; index++) {
numbers.AppendElement(aNumbers[index]);
}
}
data.delivery() = aDelivery;
data.hasRead() = aHasRead;
data.read() = aRead;
data.threadId() = aThreadId;
2013-04-09 20:36:04 +04:00
return SendCursorRequest(CreateMessageCursorRequest(data, aReverse),
aCursorCallback, aResult);
}
NS_IMETHODIMP
SmsIPCService::MarkMessageRead(int32_t aMessageId,
bool aValue,
bool aSendReadReport,
nsIMobileMessageCallback* aRequest)
{
return SendRequest(MarkMessageReadRequest(aMessageId, aValue, aSendReadReport), aRequest);
}
NS_IMETHODIMP
2013-04-09 20:37:58 +04:00
SmsIPCService::CreateThreadCursor(nsIMobileMessageCursorCallback* aCursorCallback,
nsICursorContinueCallback** aResult)
{
2013-04-09 20:37:58 +04:00
return SendCursorRequest(CreateThreadCursorRequest(), aCursorCallback,
aResult);
}
bool
GetSendMmsMessageRequestFromParams(uint32_t aServiceId,
const JS::Value& aParam,
SendMmsMessageRequest& request) {
if (aParam.isUndefined() || aParam.isNull() || !aParam.isObject()) {
return false;
}
mozilla::AutoJSContext cx;
JS::Rooted<JS::Value> param(cx, aParam);
RootedDictionary<MmsParameters> params(cx);
if (!params.Init(cx, param)) {
return false;
}
// SendMobileMessageRequest.receivers
if (!params.mReceivers.WasPassed()) {
return false;
}
request.receivers().AppendElements(params.mReceivers.Value());
// SendMobileMessageRequest.attachments
mozilla::dom::ContentChild* cc = mozilla::dom::ContentChild::GetSingleton();
if (!params.mAttachments.WasPassed()) {
return false;
}
for (uint32_t i = 0; i < params.mAttachments.Value().Length(); i++) {
mozilla::dom::MmsAttachment& attachment = params.mAttachments.Value()[i];
MmsAttachmentData mmsAttachment;
mmsAttachment.id().Assign(attachment.mId);
mmsAttachment.location().Assign(attachment.mLocation);
mmsAttachment.contentChild() = cc->GetOrCreateActorForBlob(attachment.mContent);
if (!mmsAttachment.contentChild()) {
return false;
}
request.attachments().AppendElement(mmsAttachment);
}
request.smil() = params.mSmil;
request.subject() = params.mSubject;
// Set service ID.
request.serviceId() = aServiceId;
return true;
}
/*
* Implementation of nsIMmsService.
*/
NS_IMETHODIMP
SmsIPCService::GetMmsDefaultServiceId(uint32_t* aServiceId)
{
*aServiceId = mMmsDefaultServiceId;
return NS_OK;
}
NS_IMETHODIMP
SmsIPCService::Send(uint32_t aServiceId,
JS::Handle<JS::Value> aParameters,
nsIMobileMessageCallback *aRequest)
{
SendMmsMessageRequest req;
if (!GetSendMmsMessageRequestFromParams(aServiceId, aParameters, req)) {
return NS_ERROR_INVALID_ARG;
}
return SendRequest(SendMessageRequest(req), aRequest);
}
NS_IMETHODIMP
SmsIPCService::Retrieve(int32_t aId, nsIMobileMessageCallback *aRequest)
{
return SendRequest(RetrieveMessageRequest(aId), aRequest);
}
NS_IMETHODIMP
SmsIPCService::SendReadReport(const nsAString & messageID,
const nsAString & toAddress,
const nsAString & iccId)
{
NS_ERROR("We should not be here!");
return NS_OK;
}