From ddd3985dc280f7855220b85e1de6887f627be9ee Mon Sep 17 00:00:00 2001 From: Yoshi Huang Date: Mon, 11 Jun 2012 19:28:16 +0800 Subject: [PATCH] Bug 744714 - Part 4 : DOMEvent. r=smaug --- dom/base/nsDOMClassInfo.cpp | 8 ++++ dom/base/nsDOMClassInfoClasses.h | 1 + dom/icc/src/IccManager.cpp | 64 ++++++++++++++++++++++++++++++++ dom/icc/src/IccManager.h | 9 +++++ dom/icc/src/Makefile.in | 8 ++++ dom/icc/src/StkCommandEvent.cpp | 55 +++++++++++++++++++++++++++ dom/icc/src/StkCommandEvent.h | 64 ++++++++++++++++++++++++++++++++ 7 files changed, 209 insertions(+) create mode 100644 dom/icc/src/StkCommandEvent.cpp create mode 100644 dom/icc/src/StkCommandEvent.h diff --git a/dom/base/nsDOMClassInfo.cpp b/dom/base/nsDOMClassInfo.cpp index 4a770848b2ce..c30728a4baec 100644 --- a/dom/base/nsDOMClassInfo.cpp +++ b/dom/base/nsDOMClassInfo.cpp @@ -514,6 +514,7 @@ using mozilla::dom::indexedDB::IDBWrapperCache; #include "nsIDOMVoicemail.h" #include "nsIDOMVoicemailEvent.h" #include "nsIDOMIccManager.h" +#include "StkCommandEvent.h" #endif #ifdef MOZ_B2G_BT @@ -1683,6 +1684,8 @@ static nsDOMClassInfoData sClassInfoData[] = { DOM_DEFAULT_SCRIPTABLE_FLAGS) NS_DEFINE_CLASSINFO_DATA(MozIccManager, nsDOMGenericSH, DOM_DEFAULT_SCRIPTABLE_FLAGS) + NS_DEFINE_CLASSINFO_DATA(MozStkCommandEvent, nsDOMGenericSH, + DOM_DEFAULT_SCRIPTABLE_FLAGS) #endif #ifdef MOZ_B2G_BT @@ -4495,6 +4498,11 @@ nsDOMClassInfo::Init() DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget) DOM_CLASSINFO_MAP_END + DOM_CLASSINFO_MAP_BEGIN(MozStkCommandEvent, nsIDOMMozStkCommandEvent) + DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozStkCommandEvent) + DOM_CLASSINFO_EVENT_MAP_ENTRIES + DOM_CLASSINFO_MAP_END + #endif #ifdef MOZ_B2G_BT diff --git a/dom/base/nsDOMClassInfoClasses.h b/dom/base/nsDOMClassInfoClasses.h index 1be6f8c61464..096b1a59a798 100644 --- a/dom/base/nsDOMClassInfoClasses.h +++ b/dom/base/nsDOMClassInfoClasses.h @@ -522,6 +522,7 @@ DOMCI_CLASS(CallEvent) DOMCI_CLASS(MozVoicemail) DOMCI_CLASS(MozVoicemailEvent) DOMCI_CLASS(MozIccManager) +DOMCI_CLASS(MozStkCommandEvent) #endif #ifdef MOZ_B2G_BT diff --git a/dom/icc/src/IccManager.cpp b/dom/icc/src/IccManager.cpp index 1650324f6884..c49cb384f084 100644 --- a/dom/icc/src/IccManager.cpp +++ b/dom/icc/src/IccManager.cpp @@ -2,18 +2,27 @@ * 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/Services.h" #include "nsIDOMClassInfo.h" +#include "nsIObserverService.h" #include "IccManager.h" #include "SimToolKit.h" +#include "StkCommandEvent.h" #define NS_RILCONTENTHELPER_CONTRACTID "@mozilla.org/ril/content-helper;1" +#define STKCOMMAND_EVENTNAME NS_LITERAL_STRING("stkcommand") +#define STKSESSIONEND_EVENTNAME NS_LITERAL_STRING("stksessionend") + DOMCI_DATA(MozIccManager, mozilla::dom::icc::IccManager) namespace mozilla { namespace dom { namespace icc { +const char* kStkCommandTopic = "icc-manager-stk-command"; +const char* kStkSessionEndTopic = "icc-manager-stk-session-end"; + NS_IMPL_CYCLE_COLLECTION_CLASS(IccManager) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IccManager, @@ -53,11 +62,28 @@ void IccManager::Init(nsPIDOMWindow* aWindow) { BindToOwner(aWindow); + + nsCOMPtr obs = services::GetObserverService(); + if (!obs) { + NS_WARNING("Could not acquire nsIObserverService!"); + return; + } + + obs->AddObserver(this, kStkCommandTopic, false); + obs->AddObserver(this, kStkSessionEndTopic, false); } void IccManager::Shutdown() { + nsCOMPtr obs = services::GetObserverService(); + if (!obs) { + NS_WARNING("Could not acquire nsIObserverService!"); + return; + } + + obs->RemoveObserver(this, kStkCommandTopic); + obs->RemoveObserver(this, kStkSessionEndTopic); } // nsIObserver @@ -67,6 +93,26 @@ IccManager::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* aData) { + if (!strcmp(aTopic, kStkCommandTopic)) { + nsString stkMsg; + stkMsg.Assign(aData); + nsRefPtr event = StkCommandEvent::Create(stkMsg); + + NS_ASSERTION(event, "This should never fail!"); + + nsresult rv = event->Dispatch(ToIDOMEventTarget(), STKCOMMAND_EVENTNAME); + NS_ENSURE_SUCCESS(rv, rv); + + return NS_OK; + } + + if (!strcmp(aTopic, kStkSessionEndTopic)) { + InternalDispatchEvent(STKSESSIONEND_EVENTNAME); + return NS_OK; + } + + MOZ_NOT_REACHED("Unknown observer topic!"); + return NS_OK; } @@ -94,6 +140,24 @@ IccManager::SendStkMenuSelection(PRUint16 aItemIdentifier, bool aHelpRequested) return NS_OK; } +nsresult +IccManager::InternalDispatchEvent(const nsAString& aType) +{ + nsRefPtr event = new nsDOMEvent(nullptr, nullptr); + nsresult rv = event->InitEvent(aType, false, false); + NS_ENSURE_SUCCESS(rv, rv); + + rv = event->SetTrusted(true); + NS_ENSURE_SUCCESS(rv, rv); + + bool dummy; + rv = DispatchEvent(event, &dummy); + NS_ENSURE_SUCCESS(rv, rv); + + return NS_OK; +} + + NS_IMPL_EVENT_HANDLER(IccManager, stkcommand) NS_IMPL_EVENT_HANDLER(IccManager, stksessionend) diff --git a/dom/icc/src/IccManager.h b/dom/icc/src/IccManager.h index dec357d89d1c..234cdfdd5a20 100644 --- a/dom/icc/src/IccManager.h +++ b/dom/icc/src/IccManager.h @@ -37,6 +37,15 @@ public: private: nsCOMPtr mProvider; + nsIDOMEventTarget* + ToIDOMEventTarget() const + { + return static_cast( + const_cast(this)); + } + + nsresult InternalDispatchEvent(const nsAString& aType); + NS_DECL_EVENT_HANDLER(stkcommand) NS_DECL_EVENT_HANDLER(stksessionend) }; diff --git a/dom/icc/src/Makefile.in b/dom/icc/src/Makefile.in index 2909ae2ee335..93e9aec373a6 100644 --- a/dom/icc/src/Makefile.in +++ b/dom/icc/src/Makefile.in @@ -15,11 +15,19 @@ FORCE_STATIC_LIB = 1 include $(topsrcdir)/dom/dom-config.mk +EXPORTS_NAMESPACES = mozilla/dom/icc + +EXPORTS_mozilla/dom/icc = \ + StkCommandEvent.h \ + $(NULL) + CPPSRCS = \ IccManager.cpp \ + StkCommandEvent.cpp \ $(NULL) LOCAL_INCLUDES = \ + -I$(topsrcdir)/content/events/src \ $(NULL) include $(topsrcdir)/config/config.mk diff --git a/dom/icc/src/StkCommandEvent.cpp b/dom/icc/src/StkCommandEvent.cpp new file mode 100644 index 000000000000..3448d735af21 --- /dev/null +++ b/dom/icc/src/StkCommandEvent.cpp @@ -0,0 +1,55 @@ +/* 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 "nsIDOMClassInfo.h" +#include "nsDOMClassInfoID.h" +#include "nsContentUtils.h" +#include "SimToolKit.h" +#include "StkCommandEvent.h" + +#include "nsJSON.h" +#include "jsapi.h" +#include "jsfriendapi.h" + +DOMCI_DATA(MozStkCommandEvent, mozilla::dom::icc::StkCommandEvent) + +namespace mozilla { +namespace dom { +namespace icc { + +already_AddRefed +StkCommandEvent::Create(nsAString& aMessage) +{ + nsRefPtr event = new StkCommandEvent(); + event->mCommand = aMessage; + return event.forget(); +} + +NS_IMPL_ADDREF_INHERITED(StkCommandEvent, nsDOMEvent) +NS_IMPL_RELEASE_INHERITED(StkCommandEvent, nsDOMEvent) + +NS_INTERFACE_MAP_BEGIN(StkCommandEvent) + NS_INTERFACE_MAP_ENTRY(nsIDOMMozStkCommandEvent) + NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozStkCommandEvent) +NS_INTERFACE_MAP_END_INHERITING(nsDOMEvent) + +NS_IMETHODIMP +StkCommandEvent::GetCommand(JSContext* aCx, jsval* aCommand) + +{ + nsCOMPtr json(new nsJSON()); + + if (!mCommand.IsEmpty()) { + nsresult rv = json->DecodeToJSVal(mCommand, aCx, aCommand); + NS_ENSURE_SUCCESS(rv, rv); + } else { + *aCommand = JSVAL_VOID; + } + + return NS_OK; +} + +} +} +} diff --git a/dom/icc/src/StkCommandEvent.h b/dom/icc/src/StkCommandEvent.h new file mode 100644 index 000000000000..921060855d63 --- /dev/null +++ b/dom/icc/src/StkCommandEvent.h @@ -0,0 +1,64 @@ +/* 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_icc_stkcommandevent_h +#define mozilla_dom_icc_stkcommandevent_h + +#include "nsDOMEvent.h" +#include "SimToolKit.h" + +namespace mozilla { +namespace dom { +namespace icc { + +class StkCommandEvent : public nsDOMEvent, + public nsIDOMMozStkCommandEvent +{ + nsString mCommand; + +public: + NS_DECL_ISUPPORTS_INHERITED + NS_FORWARD_TO_NSDOMEVENT + NS_DECL_NSIDOMMOZSTKCOMMANDEVENT + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(StkCommandEvent, nsDOMEvent) + + static already_AddRefed + Create(nsAString& aMessage); + + nsresult + Dispatch(nsIDOMEventTarget* aTarget, const nsAString& aEventType) + { + NS_ASSERTION(aTarget, "Null pointer!"); + NS_ASSERTION(!aEventType.IsEmpty(), "Empty event type!"); + + nsresult rv = InitEvent(aEventType, false, false); + NS_ENSURE_SUCCESS(rv, rv); + + rv = SetTrusted(true); + NS_ENSURE_SUCCESS(rv, rv); + + nsIDOMEvent* thisEvent = + static_cast(const_cast(this)); + + bool dummy; + rv = aTarget->DispatchEvent(thisEvent, &dummy); + NS_ENSURE_SUCCESS(rv, rv); + + return NS_OK; + } + +private: + StkCommandEvent() + : nsDOMEvent(nullptr, nullptr) + { } + + ~StkCommandEvent() + { } +}; + +} +} +} + +#endif // mozilla_dom_icc_stkcommandevent_h