Bug 674725 - Part AI - Implement SmsFilter. r=smaug,mrbkap

This commit is contained in:
Mounir Lamouri 2012-01-17 19:48:58 +01:00
Родитель 8ffb491577
Коммит a5083056ec
13 изменённых файлов: 500 добавлений и 7 удалений

Просмотреть файл

@ -39,6 +39,11 @@
* ***** END LICENSE BLOCK ***** */
#include "mozilla/Util.h"
#include "SmsFilter.h" // On top because it includes basictypes.h.
#ifdef XP_WIN
#undef GetClassName
#endif
// JavaScript includes
#include "jsapi.h"
@ -164,11 +169,6 @@
#include "nsIObjectLoadingContent.h"
#include "nsIPluginHost.h"
// Oh, did I mention that I hate Microsoft for doing this to me?
#ifdef XP_WIN
#undef GetClassName
#endif
// HTMLOptionsCollection includes
#include "nsIDOMHTMLOptionElement.h"
#include "nsIDOMHTMLOptionsCollection.h"
@ -515,6 +515,7 @@
#include "nsIDOMSmsMessage.h"
#include "nsIDOMSmsEvent.h"
#include "nsIDOMSmsRequest.h"
#include "nsIDOMSmsFilter.h"
#include "nsIPrivateDOMEvent.h"
#ifdef MOZ_B2G_RIL
@ -1415,6 +1416,9 @@ static nsDOMClassInfoData sClassInfoData[] = {
NS_DEFINE_CLASSINFO_DATA(MozSmsRequest, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
NS_DEFINE_CLASSINFO_DATA(MozSmsFilter, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
NS_DEFINE_CLASSINFO_DATA(CSSFontFaceRule, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
NS_DEFINE_CLASSINFO_DATA(CSSFontFaceStyleDecl, nsCSSStyleDeclSH,
@ -1641,6 +1645,7 @@ static const nsConstructorFuncMapData kConstructorFuncMap[] =
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(CloseEvent)
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(UIEvent)
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(MouseEvent)
NS_DEFINE_CONSTRUCTOR_FUNC_DATA(MozSmsFilter, sms::SmsFilter::NewSmsFilter)
};
nsIXPConnect *nsDOMClassInfo::sXPConnect = nsnull;
@ -3995,6 +4000,10 @@ nsDOMClassInfo::Init()
DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget)
DOM_CLASSINFO_MAP_END
DOM_CLASSINFO_MAP_BEGIN(MozSmsFilter, nsIDOMMozSmsFilter)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozSmsFilter)
DOM_CLASSINFO_MAP_END
DOM_CLASSINFO_MAP_BEGIN(CSSFontFaceRule, nsIDOMCSSFontFaceRule)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSFontFaceRule)
DOM_CLASSINFO_MAP_END

Просмотреть файл

@ -434,6 +434,7 @@ DOMCI_CLASS(MozSmsManager)
DOMCI_CLASS(MozSmsMessage)
DOMCI_CLASS(MozSmsEvent)
DOMCI_CLASS(MozSmsRequest)
DOMCI_CLASS(MozSmsFilter)
// @font-face in CSS
DOMCI_CLASS(CSSFontFaceRule)

Просмотреть файл

@ -53,6 +53,7 @@ XPIDLSRCS = \
nsIDOMSmsEvent.idl \
nsISmsDatabaseService.idl \
nsIDOMSmsRequest.idl \
nsIDOMSmsFilter.idl \
$(NULL)
include $(topsrcdir)/config/rules.mk

Просмотреть файл

@ -0,0 +1,57 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org.
*
* The Initial Developer of the Original Code is Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
[scriptable, builtinclass, uuid(7da08e45-ee81-4293-912b-2f2fea5b6935)]
interface nsIDOMMozSmsFilter : nsISupports
{
// A date that can return null.
[implicit_jscontext]
attribute jsval startDate;
// A date that can return null.
[implicit_jscontext]
attribute jsval endDate;
// An array of DOMString that can return null.
[implicit_jscontext]
attribute jsval numbers;
// A DOMString that can return and be set to "sent", "received" or null.
[Null(Empty)]
attribute DOMString delivery;
};

Просмотреть файл

@ -84,6 +84,7 @@ CPPSRCS = \
SmsDatabaseService.cpp \
SmsRequest.cpp \
SmsRequestManager.cpp \
SmsFilter.cpp \
$(NULL)
LOCAL_INCLUDES = \

264
dom/sms/src/SmsFilter.cpp Normal file
Просмотреть файл

@ -0,0 +1,264 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "SmsFilter.h"
#include "nsIDOMClassInfo.h"
#include "Constants.h"
#include "nsError.h"
#include "Constants.h"
#include "jsapi.h"
#include "jsdate.h" // For js_DateGetMsecSinceEpoch.
#include "js/Utility.h"
#include "nsJSUtils.h"
#include "nsDOMString.h"
DOMCI_DATA(MozSmsFilter, mozilla::dom::sms::SmsFilter)
namespace mozilla {
namespace dom {
namespace sms {
NS_INTERFACE_MAP_BEGIN(SmsFilter)
NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsFilter)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsFilter)
NS_INTERFACE_MAP_END
NS_IMPL_ADDREF(SmsFilter)
NS_IMPL_RELEASE(SmsFilter)
SmsFilter::SmsFilter()
{
mData.startDate() = 0;
mData.endDate() = 0;
mData.delivery() = eDeliveryState_Unknown;
}
SmsFilter::SmsFilter(const SmsFilterData& aData)
: mData(aData)
{
}
/* static */ nsresult
SmsFilter::NewSmsFilter(nsISupports** aSmsFilter)
{
NS_ADDREF(*aSmsFilter = new SmsFilter());
return NS_OK;
}
NS_IMETHODIMP
SmsFilter::GetStartDate(JSContext* aCx, jsval* aStartDate)
{
if (mData.startDate() == 0) {
*aStartDate = JSVAL_NULL;
return NS_OK;
}
aStartDate->setObjectOrNull(JS_NewDateObjectMsec(aCx, mData.startDate()));
NS_ENSURE_TRUE(aStartDate->isObject(), NS_ERROR_FAILURE);
return NS_OK;
}
NS_IMETHODIMP
SmsFilter::SetStartDate(JSContext* aCx, const jsval& aStartDate)
{
if (aStartDate == JSVAL_NULL) {
mData.startDate() = 0;
return NS_OK;
}
if (!aStartDate.isObject()) {
return NS_ERROR_INVALID_ARG;
}
JSObject& obj = aStartDate.toObject();
if (!JS_ObjectIsDate(aCx, &obj)) {
return NS_ERROR_INVALID_ARG;
}
mData.startDate() = js_DateGetMsecSinceEpoch(aCx, &obj);
return NS_OK;
}
NS_IMETHODIMP
SmsFilter::GetEndDate(JSContext* aCx, jsval* aEndDate)
{
if (mData.endDate() == 0) {
*aEndDate = JSVAL_NULL;
return NS_OK;
}
aEndDate->setObjectOrNull(JS_NewDateObjectMsec(aCx, mData.endDate()));
NS_ENSURE_TRUE(aEndDate->isObject(), NS_ERROR_FAILURE);
return NS_OK;
}
NS_IMETHODIMP
SmsFilter::SetEndDate(JSContext* aCx, const jsval& aEndDate)
{
if (aEndDate == JSVAL_NULL) {
mData.endDate() = 0;
return NS_OK;
}
if (!aEndDate.isObject()) {
return NS_ERROR_INVALID_ARG;
}
JSObject& obj = aEndDate.toObject();
if (!JS_ObjectIsDate(aCx, &obj)) {
return NS_ERROR_INVALID_ARG;
}
mData.endDate() = js_DateGetMsecSinceEpoch(aCx, &obj);
return NS_OK;
}
NS_IMETHODIMP
SmsFilter::GetNumbers(JSContext* aCx, jsval* aNumbers)
{
PRUint32 length = mData.numbers().Length();
if (length == 0) {
*aNumbers = JSVAL_NULL;
return NS_OK;
}
jsval* numbers = new jsval[length];
for (PRUint32 i=0; i<length; ++i) {
numbers[i].setString(JS_NewUCStringCopyN(aCx, mData.numbers()[i].get(),
mData.numbers()[i].Length()));
}
aNumbers->setObjectOrNull(JS_NewArrayObject(aCx, length, numbers));
NS_ENSURE_TRUE(aNumbers->isObject(), NS_ERROR_FAILURE);
return NS_OK;
}
NS_IMETHODIMP
SmsFilter::SetNumbers(JSContext* aCx, const jsval& aNumbers)
{
if (aNumbers == JSVAL_NULL) {
mData.numbers().Clear();
return NS_OK;
}
if (!aNumbers.isObject()) {
return NS_ERROR_INVALID_ARG;
}
JSObject& obj = aNumbers.toObject();
if (!JS_IsArrayObject(aCx, &obj)) {
return NS_ERROR_INVALID_ARG;
}
jsuint size;
JS_ALWAYS_TRUE(JS_GetArrayLength(aCx, &obj, &size));
nsTArray<nsString> numbers;
for (jsuint i=0; i<size; ++i) {
jsval jsNumber;
if (!JS_GetElement(aCx, &obj, i, &jsNumber)) {
return NS_ERROR_INVALID_ARG;
}
if (!jsNumber.isString()) {
return NS_ERROR_INVALID_ARG;
}
nsDependentJSString number;
number.init(aCx, jsNumber.toString());
numbers.AppendElement(number);
}
mData.numbers().Clear();
mData.numbers().AppendElements(numbers);
return NS_OK;
}
NS_IMETHODIMP
SmsFilter::GetDelivery(nsAString& aDelivery)
{
switch (mData.delivery()) {
case eDeliveryState_Received:
aDelivery = DELIVERY_RECEIVED;
break;
case eDeliveryState_Sent:
aDelivery = DELIVERY_SENT;
break;
case eDeliveryState_Unknown:
SetDOMStringToNull(aDelivery);
break;
default:
NS_ASSERTION(false, "We shouldn't get another delivery state!");
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
NS_IMETHODIMP
SmsFilter::SetDelivery(const nsAString& aDelivery)
{
if (aDelivery.IsEmpty()) {
mData.delivery() = eDeliveryState_Unknown;
return NS_OK;
}
if (aDelivery.Equals(DELIVERY_RECEIVED)) {
mData.delivery() = eDeliveryState_Received;
return NS_OK;
}
if (aDelivery.Equals(DELIVERY_SENT)) {
mData.delivery() = eDeliveryState_Sent;
return NS_OK;
}
return NS_ERROR_INVALID_ARG;
}
} // namespace sms
} // namespace dom
} // namespace mozilla

75
dom/sms/src/SmsFilter.h Normal file
Просмотреть файл

@ -0,0 +1,75 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef mozilla_dom_sms_SmsFilter_h
#define mozilla_dom_sms_SmsFilter_h
#include "mozilla/dom/sms/PSms.h"
#include "nsIDOMSmsFilter.h"
#include "Types.h"
namespace mozilla {
namespace dom {
namespace sms {
class SmsFilter : public nsIDOMMozSmsFilter
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMMOZSMSFILTER
SmsFilter();
SmsFilter(const SmsFilterData& aData);
const SmsFilterData& GetData() const;
static nsresult NewSmsFilter(nsISupports** aSmsFilter);
private:
SmsFilterData mData;
};
inline const SmsFilterData&
SmsFilter::GetData() const {
return mData;
}
} // namespace sms
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_sms_SmsFilter_h

Просмотреть файл

@ -138,10 +138,10 @@ SmsMessage::GetDelivery(nsAString& aDelivery)
{
switch (mData.delivery()) {
case eDeliveryState_Received:
aDelivery.AssignLiteral("received");
aDelivery = DELIVERY_RECEIVED;
break;
case eDeliveryState_Sent:
aDelivery.AssignLiteral("sent");
aDelivery = DELIVERY_SENT;
break;
case eDeliveryState_Unknown:
default:

Просмотреть файл

@ -38,6 +38,8 @@
#ifndef mozilla_dom_sms_Types_h
#define mozilla_dom_sms_Types_h
#include "IPCMessageUtils.h"
namespace mozilla {
namespace dom {
namespace sms {

Просмотреть файл

@ -55,6 +55,13 @@ struct SmsMessageData {
PRUint64 timestamp; // ms since epoch.
};
struct SmsFilterData {
PRUint64 startDate;
PRUint64 endDate;
nsString[] numbers;
DeliveryState delivery;
};
sync protocol PSms {
manager PContent;

Просмотреть файл

@ -50,6 +50,7 @@ include $(topsrcdir)/config/rules.mk
_TEST_FILES = \
test_sms_basics.html \
test_smsfilter.html \
$(NULL)
_CHROME_TEST_FILES = \

Просмотреть файл

@ -46,6 +46,7 @@ function test() {
checkInterface("SmsMessage");
checkInterface("SmsEvent");
checkInterface("SmsRequest");
checkInterface("SmsFilter");
// If sms are disabled and whitelist is empty, sms is disabled.
SpecialPowers.setBoolPref("dom.sms.enabled", false);

Просмотреть файл

@ -0,0 +1,74 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test SmsFilter</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
<iframe></iframe>
</div>
<pre id="test">
<script type="application/javascript">
/** Test SmsFilter **/
function throwingCheck(aFilter, aProperty, disallowedValue)
{
disallowedValue.forEach(function(v) {
var exception = false;
try {
aFilter[aProperty] = v;
} catch (e) {
exception = true;
}
ok(exception, "An exception should have been thrown");
});
}
var filter = new MozSmsFilter();
isnot(filter, null, "filter should be set");
is(filter.startDate, null, "Parameters shouldn't be initialized");
is(filter.endDate, null, "Parameters shouldn't be initialized");
is(filter.numbers, null, "Parameters shouldn't be initialized");
is(filter.delivery, null, "Parameters shouldn't be initialized");
var date = new Date();
filter.startDate = date;
is(filter.startDate, "" + date, "Setters and getters should work!");
filter.startDate = null;
is(filter.startDate, null, "null should revert the value to default");
throwingCheck(filter, 'startDate', [ "foo", 42, [1, 2], function () {}, undefined ]);
filter.endDate = date;
is(filter.endDate, "" + date, "Setters and getters should work!");
filter.endDate = null;
is(filter.endDate, null, "null should revert the value to default");
throwingCheck(filter, 'endDate', [ "foo", 42, [1, 2], function () {}, undefined ]);
var numbers = [ "foo", "bar" ];
filter.numbers = numbers;
is(filter.numbers, "" + numbers, "Setters and getters should work!");
filter.numbers = null;
is(filter.numbers, null, "null should revert the value to default");
throwingCheck(filter, 'numbers', [ "foo", 42, function () {}, new Date(), undefined ]);
filter.delivery = "sent";
is(filter.delivery, "sent", "Setters and getters should work!");
filter.delivery = "received";
is(filter.delivery, "received", "Setters and getters should work!");
filter.delivery = "";
is(filter.delivery, null, "The empty string should revert the value to default.");
filter.delivery = null;
is(filter.delivery, null, "'null' should revert the value to default.");
throwingCheck(filter, 'delivery', [ "foo", 42, [1, 2], function () {}, new Date(), undefined ]);
</script>
</pre>
</body>
</html>