diff --git a/dom/base/nsDOMClassInfo.cpp b/dom/base/nsDOMClassInfo.cpp index 164b2d183e5..08288e4afcb 100644 --- a/dom/base/nsDOMClassInfo.cpp +++ b/dom/base/nsDOMClassInfo.cpp @@ -514,6 +514,7 @@ #include "nsIDOMSmsManager.h" #include "nsIDOMSmsMessage.h" #include "nsIDOMSmsEvent.h" +#include "nsIDOMSmsRequest.h" #include "nsIPrivateDOMEvent.h" #ifdef MOZ_B2G_RIL @@ -1411,6 +1412,9 @@ static nsDOMClassInfoData sClassInfoData[] = { NS_DEFINE_CLASSINFO_DATA(MozSmsEvent, nsDOMGenericSH, DOM_DEFAULT_SCRIPTABLE_FLAGS) + NS_DEFINE_CLASSINFO_DATA(MozSmsRequest, nsDOMGenericSH, + DOM_DEFAULT_SCRIPTABLE_FLAGS) + NS_DEFINE_CLASSINFO_DATA(CSSFontFaceRule, nsDOMGenericSH, DOM_DEFAULT_SCRIPTABLE_FLAGS) NS_DEFINE_CLASSINFO_DATA(CSSFontFaceStyleDecl, nsCSSStyleDeclSH, @@ -3986,6 +3990,11 @@ nsDOMClassInfo::Init() DOM_CLASSINFO_EVENT_MAP_ENTRIES DOM_CLASSINFO_MAP_END + DOM_CLASSINFO_MAP_BEGIN(MozSmsRequest, nsIDOMMozSmsRequest) + DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozSmsRequest) + DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget) + DOM_CLASSINFO_MAP_END + DOM_CLASSINFO_MAP_BEGIN(CSSFontFaceRule, nsIDOMCSSFontFaceRule) DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSFontFaceRule) DOM_CLASSINFO_MAP_END diff --git a/dom/base/nsDOMClassInfoClasses.h b/dom/base/nsDOMClassInfoClasses.h index 54356f7b5aa..a7be70081f5 100644 --- a/dom/base/nsDOMClassInfoClasses.h +++ b/dom/base/nsDOMClassInfoClasses.h @@ -433,6 +433,7 @@ DOMCI_CLASS(MozBatteryManager) DOMCI_CLASS(MozSmsManager) DOMCI_CLASS(MozSmsMessage) DOMCI_CLASS(MozSmsEvent) +DOMCI_CLASS(MozSmsRequest) // @font-face in CSS DOMCI_CLASS(CSSFontFaceRule) diff --git a/dom/sms/interfaces/Makefile.in b/dom/sms/interfaces/Makefile.in index a76ed8cbb1c..b4563843aee 100644 --- a/dom/sms/interfaces/Makefile.in +++ b/dom/sms/interfaces/Makefile.in @@ -52,6 +52,7 @@ XPIDLSRCS = \ nsIDOMSmsMessage.idl \ nsIDOMSmsEvent.idl \ nsISmsDatabaseService.idl \ + nsIDOMSmsRequest.idl \ $(NULL) include $(topsrcdir)/config/rules.mk diff --git a/dom/sms/interfaces/nsIDOMSmsRequest.idl b/dom/sms/interfaces/nsIDOMSmsRequest.idl new file mode 100644 index 00000000000..dedb4aea576 --- /dev/null +++ b/dom/sms/interfaces/nsIDOMSmsRequest.idl @@ -0,0 +1,53 @@ +/* ***** 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 (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 "nsIDOMEventTarget.idl" + +interface nsIDOMEventListener; + +[scriptable, function, uuid(1b24469d-cfb7-4667-aaf0-c1d17289ae7c)] +interface nsIDOMMozSmsRequest : nsIDOMEventTarget +{ + // Returns whether "processing" or "done". + readonly attribute DOMString readyState; + // Can be null. + readonly attribute DOMString error; + // Can be bool, nsIDOMSmsMessage, nsIDOMSmsIterator or null. + readonly attribute jsval result; + + attribute nsIDOMEventListener onsuccess; + attribute nsIDOMEventListener onerror; +}; diff --git a/dom/sms/src/Makefile.in b/dom/sms/src/Makefile.in index 3f0cfe474e9..e170b8bf324 100644 --- a/dom/sms/src/Makefile.in +++ b/dom/sms/src/Makefile.in @@ -80,6 +80,7 @@ CPPSRCS = \ Constants.cpp \ SmsChild.cpp \ SmsDatabaseService.cpp \ + SmsRequest.cpp \ $(NULL) LOCAL_INCLUDES = \ diff --git a/dom/sms/src/SmsRequest.cpp b/dom/sms/src/SmsRequest.cpp new file mode 100644 index 00000000000..f588dbf647f --- /dev/null +++ b/dom/sms/src/SmsRequest.cpp @@ -0,0 +1,220 @@ +/* -*- 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 (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 "SmsRequest.h" +#include "nsIDOMClassInfo.h" +#include "nsDOMString.h" +#include "nsContentUtils.h" +#include "nsIDOMSmsMessage.h" + +DOMCI_DATA(MozSmsRequest, mozilla::dom::sms::SmsRequest) + +namespace mozilla { +namespace dom { +namespace sms { + +NS_IMPL_CYCLE_COLLECTION_CLASS(SmsRequest) + +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SmsRequest, + nsDOMEventTargetWrapperCache) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS + NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(success) + NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(error) +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END + +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SmsRequest, + nsDOMEventTargetWrapperCache) + if (tmp->mResultRooted) { + tmp->mResult = JSVAL_NULL; + tmp->UnrootResult(); + } + NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(success) + NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(error) +NS_IMPL_CYCLE_COLLECTION_UNLINK_END + +NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(SmsRequest, + nsDOMEventTargetWrapperCache) + if (JSVAL_IS_GCTHING(tmp->mResult)) { + void *gcThing = JSVAL_TO_GCTHING(tmp->mResult); + NS_IMPL_CYCLE_COLLECTION_TRACE_JS_CALLBACK(gcThing, "mResult") + } +NS_IMPL_CYCLE_COLLECTION_TRACE_END + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(SmsRequest) + NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsRequest) + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMMozSmsRequest) + NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsRequest) +NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetWrapperCache) + +NS_IMPL_ADDREF_INHERITED(SmsRequest, nsDOMEventTargetWrapperCache) +NS_IMPL_RELEASE_INHERITED(SmsRequest, nsDOMEventTargetWrapperCache) + +NS_IMPL_EVENT_HANDLER(SmsRequest, success) +NS_IMPL_EVENT_HANDLER(SmsRequest, error) + +SmsRequest::SmsRequest(nsPIDOMWindow* aWindow, nsIScriptContext* aScriptContext) + : mResult(JSVAL_NULL) + , mResultRooted(false) + , mError(eNoError) + , mDone(false) +{ + // Those vars come from nsDOMEventTargetHelper. + mOwner = aWindow; + mScriptContext = aScriptContext; +} + +SmsRequest::~SmsRequest() +{ + if (mResultRooted) { + UnrootResult(); + } +} + +void +SmsRequest::RootResult() +{ + NS_ASSERTION(!mResultRooted, "Don't call RootResult() if already rooted!"); + NS_HOLD_JS_OBJECTS(this, SmsRequest); + mResultRooted = true; +} + +void +SmsRequest::UnrootResult() +{ + NS_ASSERTION(mResultRooted, "Don't call UnrotResult() if not rooted!"); + NS_DROP_JS_OBJECTS(this, SmsRequest); + mResultRooted = false; +} + +void +SmsRequest::SetSuccess(nsIDOMMozSmsMessage* aMessage) +{ + NS_PRECONDITION(!mDone, "mDone shouldn't have been set to true already!"); + NS_PRECONDITION(mError == eNoError, "mError shouldn't have been set!"); + NS_PRECONDITION(mResult == JSVAL_NULL, "mResult shouldn't have been set!"); + + JSContext* cx = mScriptContext->GetNativeContext(); + NS_ASSERTION(cx, "Failed to get a context!"); + + JSObject* global = mScriptContext->GetNativeGlobal(); + NS_ASSERTION(global, "Failed to get global object!"); + + JSAutoRequest ar(cx); + JSAutoEnterCompartment ac; + if (!ac.enter(cx, global)) { + SetError(eInternalError); + return; + } + + RootResult(); + + if (NS_FAILED(nsContentUtils::WrapNative(cx, global, aMessage, &mResult))) { + UnrootResult(); + mResult = JSVAL_NULL; + SetError(eInternalError); + } + + mDone = true; +} + +void +SmsRequest::SetError(ErrorType aError) +{ + NS_PRECONDITION(!mDone, "mDone shouldn't have been set to true already!"); + NS_PRECONDITION(mError == eNoError, "mError shouldn't have been set!"); + NS_PRECONDITION(mResult == JSVAL_NULL, "mResult shouldn't have been set!"); + + mDone = true; + mError = aError; +} + +NS_IMETHODIMP +SmsRequest::GetReadyState(nsAString& aReadyState) +{ + if (mDone) { + aReadyState.AssignLiteral("done"); + } else { + aReadyState.AssignLiteral("processing"); + } + + return NS_OK; +} + +NS_IMETHODIMP +SmsRequest::GetError(nsAString& aError) +{ + if (!mDone) { + NS_ASSERTION(mError == eNoError, + "There should be no error if the request is still processing!"); + + SetDOMStringToNull(aError); + return NS_OK; + } + + NS_ASSERTION(mError == eNoError || mResult == JSVAL_NULL, + "mResult should be void when there is an error!"); + + switch (mError) { + case eNoError: + SetDOMStringToNull(aError); + break; + case eInternalError: + aError.AssignLiteral("InternalError"); + break; + } + + return NS_OK; +} + +NS_IMETHODIMP +SmsRequest::GetResult(jsval* aResult) +{ + if (!mDone) { + NS_ASSERTION(mResult == JSVAL_NULL, + "When not done, result should be null!"); + + *aResult = JSVAL_NULL; + return NS_OK; + } + + *aResult = mResult; + return NS_OK; +} + +} // namespace sms +} // namespace dom +} // namespace mozilla diff --git a/dom/sms/src/SmsRequest.h b/dom/sms/src/SmsRequest.h new file mode 100644 index 00000000000..baf10b11efe --- /dev/null +++ b/dom/sms/src/SmsRequest.h @@ -0,0 +1,106 @@ +/* -*- 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 (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_SmsRequest_h +#define mozilla_dom_sms_SmsRequest_h + +#include "nsIDOMSmsRequest.h" +#include "nsDOMEventTargetWrapperCache.h" + +class nsIDOMMozSmsMessage; + +namespace mozilla { +namespace dom { +namespace sms { + +class SmsRequest : public nsIDOMMozSmsRequest + , public nsDOMEventTargetWrapperCache +{ +public: + enum ErrorType { + eNoError = 0, + eInternalError, + }; + + NS_DECL_ISUPPORTS + NS_DECL_NSIDOMMOZSMSREQUEST + + NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetWrapperCache::) + + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(SmsRequest, + nsDOMEventTargetWrapperCache) + +private: + SmsRequest() MOZ_DELETE; + + SmsRequest(nsPIDOMWindow* aWindow, nsIScriptContext* aScriptContext); + ~SmsRequest(); + + /** + * Root mResult (jsval) to prevent garbage collection. + */ + void RootResult(); + + /** + * Unroot mResult (jsval) to allow garbage collection. + */ + void UnrootResult(); + + /** + * Set the object in a success state with the result being aMessage. + */ + void SetSuccess(nsIDOMMozSmsMessage* aMessage); + + /** + * Set the object in an error state with the error type being aError. + */ + void SetError(ErrorType aError); + + jsval mResult; + bool mResultRooted; + ErrorType mError; + bool mDone; + + NS_DECL_EVENT_HANDLER(success) + NS_DECL_EVENT_HANDLER(error) +}; + +} // namespace sms +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_sms_SmsRequest_h diff --git a/dom/sms/tests/test_sms_basics.html b/dom/sms/tests/test_sms_basics.html index c193bbacfbd..4b7aff39afd 100644 --- a/dom/sms/tests/test_sms_basics.html +++ b/dom/sms/tests/test_sms_basics.html @@ -45,6 +45,7 @@ function test() { checkInterface("NavigatorSms"); checkInterface("SmsMessage"); checkInterface("SmsEvent"); + checkInterface("SmsRequest"); // If sms are disabled and whitelist is empty, sms is disabled. SpecialPowers.setBoolPref("dom.sms.enabled", false);