Bug 873351 - Part 1: Re-write SmsService in JavaScript. r=echen

This commit is contained in:
Bevis Tseng 2014-11-24 18:43:54 +08:00
parent 68f8cc7236
commit 95b39f7cac
11 changed files with 188 additions and 207 deletions

View File

@ -451,6 +451,8 @@
@BINPATH@/components/RILContentHelper.js
@BINPATH@/components/RILSystemMessengerHelper.js
@BINPATH@/components/RILSystemMessengerHelper.manifest
@BINPATH@/components/SmsService.js
@BINPATH@/components/SmsService.manifest
@BINPATH@/components/TelephonyAudioService.js
@BINPATH@/components/TelephonyAudioService.manifest
@BINPATH@/components/TelephonyService.js

View File

@ -36,7 +36,7 @@
#include "android/SmsService.h"
#elif defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
#include "nsIRilMobileMessageDatabaseService.h"
#include "gonk/SmsService.h"
#include "nsIGonkSmsService.h"
#endif
#include "nsXULAppAPI.h" // For XRE_GetProcessType()
@ -711,7 +711,7 @@ NS_CreateSmsService()
#ifdef MOZ_WIDGET_ANDROID
smsService = new SmsService();
#elif defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
smsService = new SmsService();
smsService = do_GetService(GONK_SMSSERVICE_CONTRACTID);
#endif
}

View File

@ -1,159 +0,0 @@
/* -*- 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 "SmsMessage.h"
#include "SmsService.h"
#include "mozilla/Preferences.h"
#include "nsServiceManagerUtils.h"
namespace {
#define kPrefDefaultServiceId "dom.sms.defaultServiceId"
uint32_t
getDefaultServiceId()
{
static const char* kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces";
int32_t id = mozilla::Preferences::GetInt(kPrefDefaultServiceId, 0);
int32_t numRil = mozilla::Preferences::GetInt(kPrefRilNumRadioInterfaces, 1);
if (id >= numRil || id < 0) {
id = 0;
}
return id;
}
} // Anonymous namespace
namespace mozilla {
namespace dom {
namespace mobilemessage {
NS_IMPL_ISUPPORTS(SmsService,
nsISmsService,
nsIObserver)
SmsService::SmsService()
{
mRil = do_GetService("@mozilla.org/ril;1");
NS_WARN_IF_FALSE(mRil, "This shouldn't fail!");
// Initialize observer.
static const char* kObservedPrefs[] = {
kPrefDefaultServiceId,
nullptr
};
Preferences::AddStrongObservers(this, kObservedPrefs);
mDefaultServiceId = getDefaultServiceId();
}
/*
* Implementation of nsIObserver.
*/
NS_IMETHODIMP
SmsService::Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aData)
{
if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
nsDependentString data(aData);
if (data.EqualsLiteral(kPrefDefaultServiceId)) {
mDefaultServiceId = getDefaultServiceId();
}
return NS_OK;
}
MOZ_ASSERT(false, "SmsService got unexpected topic!");
return NS_ERROR_UNEXPECTED;
}
/*
* Implementation of nsISmsService.
*/
NS_IMETHODIMP
SmsService::GetSmsDefaultServiceId(uint32_t* aServiceId)
{
*aServiceId = mDefaultServiceId;
return NS_OK;
}
NS_IMETHODIMP
SmsService::GetSegmentInfoForText(const nsAString& aText,
nsIMobileMessageCallback* aRequest)
{
nsCOMPtr<nsIRadioInterface> radioInterface;
if (mRil) {
mRil->GetRadioInterface(0, getter_AddRefs(radioInterface));
}
NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE);
return radioInterface->GetSegmentInfoForText(aText, aRequest);
}
NS_IMETHODIMP
SmsService::Send(uint32_t aServiceId,
const nsAString& aNumber,
const nsAString& aMessage,
bool aSilent,
nsIMobileMessageCallback* aRequest)
{
nsCOMPtr<nsIRadioInterface> radioInterface;
if (mRil) {
mRil->GetRadioInterface(aServiceId, getter_AddRefs(radioInterface));
}
NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE);
return radioInterface->SendSMS(aNumber, aMessage, aSilent, aRequest);
}
NS_IMETHODIMP
SmsService::IsSilentNumber(const nsAString& aNumber,
bool* aIsSilent)
{
*aIsSilent = mSilentNumbers.Contains(aNumber);
return NS_OK;
}
NS_IMETHODIMP
SmsService::AddSilentNumber(const nsAString& aNumber)
{
if (mSilentNumbers.Contains(aNumber)) {
return NS_ERROR_UNEXPECTED;
}
NS_ENSURE_TRUE(mSilentNumbers.AppendElement(aNumber), NS_ERROR_FAILURE);
return NS_OK;
}
NS_IMETHODIMP
SmsService::RemoveSilentNumber(const nsAString& aNumber)
{
if (!mSilentNumbers.Contains(aNumber)) {
return NS_ERROR_INVALID_ARG;
}
NS_ENSURE_TRUE(mSilentNumbers.RemoveElement(aNumber), NS_ERROR_FAILURE);
return NS_OK;
}
NS_IMETHODIMP
SmsService::GetSmscAddress(uint32_t aServiceId,
nsIMobileMessageCallback *aRequest)
{
nsCOMPtr<nsIRadioInterface> radioInterface;
if (mRil) {
mRil->GetRadioInterface(aServiceId, getter_AddRefs(radioInterface));
}
NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE);
return radioInterface->GetSmscAddress(aRequest);
}
} // namespace mobilemessage
} // namespace dom
} // namespace mozilla

View File

@ -1,40 +0,0 @@
/* 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/. */
#ifndef mozilla_dom_mobilemessage_SmsService_h
#define mozilla_dom_mobilemessage_SmsService_h
#include "nsISmsService.h"
#include "nsCOMPtr.h"
#include "nsIObserver.h"
#include "nsIRadioInterfaceLayer.h"
#include "nsTArray.h"
#include "nsString.h"
#include "mozilla/Attributes.h"
namespace mozilla {
namespace dom {
namespace mobilemessage {
class SmsService MOZ_FINAL : public nsISmsService
, public nsIObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSISMSSERVICE
NS_DECL_NSIOBSERVER
SmsService();
protected:
nsCOMPtr<nsIRadioInterfaceLayer> mRil;
nsTArray<nsString> mSilentNumbers;
uint32_t mDefaultServiceId;
};
} // namespace mobilemessage
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_mobilemessage_SmsService_h

View File

@ -0,0 +1,154 @@
/* 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/. */
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
let RIL = {};
Cu.import("resource://gre/modules/ril_consts.js", RIL);
const GONK_SMSSERVICE_CONTRACTID = "@mozilla.org/sms/gonksmsservice;1";
const GONK_SMSSERVICE_CID = Components.ID("{f9b9b5e2-73b4-11e4-83ff-a33e27428c86}");
const NS_XPCOM_SHUTDOWN_OBSERVER_ID = "xpcom-shutdown";
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
const kPrefDefaultServiceId = "dom.sms.defaultServiceId";
const kPrefRilDebuggingEnabled = "ril.debugging.enabled";
const kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces";
XPCOMUtils.defineLazyGetter(this, "gRadioInterfaces", function() {
let ril = Cc["@mozilla.org/ril;1"].getService(Ci.nsIRadioInterfaceLayer);
let interfaces = [];
for (let i = 0; i < ril.numRadioInterfaces; i++) {
interfaces.push(ril.getRadioInterface(i));
}
return interfaces;
});
let DEBUG = RIL.DEBUG_RIL;
function debug(s) {
dump("SmsService: " + s);
}
function SmsService() {
this._silentNumbers = [];
this.smsDefaultServiceId = this._getDefaultServiceId();
Services.prefs.addObserver(kPrefRilDebuggingEnabled, this, false);
Services.prefs.addObserver(kPrefDefaultServiceId, this, false);
Services.obs.addObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
}
SmsService.prototype = {
classID: GONK_SMSSERVICE_CID,
classInfo: XPCOMUtils.generateCI({classID: GONK_SMSSERVICE_CID,
contractID: GONK_SMSSERVICE_CONTRACTID,
classDescription: "SmsService",
interfaces: [Ci.nsISmsService,
Ci.nsIGonkSmsService],
flags: Ci.nsIClassInfo.SINGLETON}),
QueryInterface: XPCOMUtils.generateQI([Ci.nsISmsService,
Ci.nsIGonkSmsService,
Ci.nsIObserver]),
_updateDebugFlag: function() {
try {
DEBUG = RIL.DEBUG_RIL ||
Services.prefs.getBoolPref(kPrefRilDebuggingEnabled);
} catch (e) {}
},
_getDefaultServiceId: function() {
let id = Services.prefs.getIntPref(kPrefDefaultServiceId);
let numRil = Services.prefs.getIntPref(kPrefRilNumRadioInterfaces);
if (id >= numRil || id < 0) {
id = 0;
}
return id;
},
/**
* nsISmsService interface
*/
smsDefaultServiceId: 0,
getSegmentInfoForText: function(aText, aRequest) {
gRadioInterfaces[0].getSegmentInfoForText(aText, aRequest);
},
send: function(aServiceId, aNumber, aMessage, aSilent, aRequest) {
if (aServiceId > (gRadioInterfaces.length - 1)) {
throw Cr.NS_ERROR_INVALID_ARG;
}
gRadioInterfaces[aServiceId].sendSMS(aNumber, aMessage, aSilent, aRequest);
},
// An array of slient numbers.
_silentNumbers: null,
isSilentNumber: function(aNumber) {
return this._silentNumbers.indexOf(aNumber) >= 0;
},
addSilentNumber: function(aNumber) {
if (this.isSilentNumber(aNumber)) {
throw Cr.NS_ERROR_UNEXPECTED;
}
this._silentNumbers.push(aNumber);
},
removeSilentNumber: function(aNumber) {
let index = this._silentNumbers.indexOf(aNumber);
if (index < 0) {
throw Cr.NS_ERROR_INVALID_ARG;
}
this._silentNumbers.splice(index, 1);
},
getSmscAddress: function(aServiceId, aRequest) {
if (aServiceId > (gRadioInterfaces.length - 1)) {
throw Cr.NS_ERROR_INVALID_ARG;
}
gRadioInterfaces[aServiceId].getSmscAddress(aRequest);
},
/**
* TODO: nsIGonkSmsService interface
*/
/**
* nsIObserver interface.
*/
observe: function(aSubject, aTopic, aData) {
switch (aTopic) {
case NS_PREFBRANCH_PREFCHANGE_TOPIC_ID:
if (aData === kPrefRilDebuggingEnabled) {
this._updateDebugFlag();
}
else if (aData === kPrefDefaultServiceId) {
this.smsDefaultServiceId = this._getDefaultServiceId();
}
break;
case NS_XPCOM_SHUTDOWN_OBSERVER_ID:
Services.prefs.removeObserver(kPrefRilDebuggingEnabled, this);
Services.prefs.removeObserver(kPrefDefaultServiceId, this);
Services.obs.removeObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
break;
}
}
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SmsService]);

View File

@ -0,0 +1,6 @@
# 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/.
component {f9b9b5e2-73b4-11e4-83ff-a33e27428c86} SmsService.js
contract @mozilla.org/sms/gonksmsservice;1 {f9b9b5e2-73b4-11e4-83ff-a33e27428c86}

View File

@ -20,6 +20,7 @@ XPIDL_SOURCES += [
if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk' and CONFIG['MOZ_B2G_RIL']:
XPIDL_SOURCES += [
'nsIGonkSmsService.idl',
'nsIRilMobileMessageDatabaseService.idl',
'nsISmsMessenger.idl',
]

View File

@ -0,0 +1,18 @@
/* 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 "nsISmsService.idl"
%{C++
#define GONK_SMSSERVICE_CONTRACTID \
"@mozilla.org/sms/gonksmsservice;1"
%}
[scriptable, uuid(63fab75e-73b4-11e4-a10d-dbfa9d05a4f4)]
interface nsIGonkSmsService : nsISmsService
{
/**
* TODO: define callback to receive message from the network.
*/
};

View File

@ -12,7 +12,7 @@ interface nsIMobileMessageCallback;
#define SMS_SERVICE_CONTRACTID "@mozilla.org/sms/smsservice;1"
%}
[scriptable, builtinclass, uuid(8f86d068-698e-11e4-9470-8f75a088b84a)]
[scriptable, uuid(31626940-73b4-11e4-8b03-1724e1d8a6a1)]
interface nsISmsService : nsISupports
{
/**

View File

@ -34,9 +34,8 @@ elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk' and CONFIG['MOZ_B2G_RIL']:
'gonk/MmsService.manifest',
'gonk/MobileMessageDatabaseService.js',
'gonk/MobileMessageDatabaseService.manifest',
]
UNIFIED_SOURCES += [
'gonk/SmsService.cpp',
'gonk/SmsService.js',
'gonk/SmsService.manifest',
]
EXPORTS.mozilla.dom += [

View File

@ -145,8 +145,8 @@ XPCOMUtils.defineLazyServiceGetter(this, "gMobileMessageService",
"nsIMobileMessageService");
XPCOMUtils.defineLazyServiceGetter(this, "gSmsService",
"@mozilla.org/sms/smsservice;1",
"nsISmsService");
"@mozilla.org/sms/gonksmsservice;1",
"nsIGonkSmsService");
XPCOMUtils.defineLazyServiceGetter(this, "gMobileMessageDatabaseService",
"@mozilla.org/mobilemessage/rilmobilemessagedatabaseservice;1",