Bug 744714 - Part 4 : DOMEvent. r=smaug
authorYoshi Huang <yhuang@mozilla.com>
Mon, 11 Jun 2012 19:28:16 +0800
changeset 105808 1cf18897cdf418ccc8159e2de34302dc4ae92fff
parent 105807 a5611e4202ef91bc2b9df184f6df430e6f2db352
child 105809 430b14724dca7516a8141486e8a329f682cbd5b8
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewerssmaug
bugs744714
milestone18.0a1
Bug 744714 - Part 4 : DOMEvent. r=smaug
dom/base/nsDOMClassInfo.cpp
dom/base/nsDOMClassInfoClasses.h
dom/icc/src/IccManager.cpp
dom/icc/src/IccManager.h
dom/icc/src/Makefile.in
dom/icc/src/StkCommandEvent.cpp
dom/icc/src/StkCommandEvent.h
--- a/dom/base/nsDOMClassInfo.cpp
+++ b/dom/base/nsDOMClassInfo.cpp
@@ -509,16 +509,17 @@ using mozilla::dom::indexedDB::IDBWrappe
 
 #ifdef MOZ_B2G_RIL
 #include "Telephony.h"
 #include "TelephonyCall.h"
 #include "CallEvent.h"
 #include "nsIDOMVoicemail.h"
 #include "nsIDOMVoicemailEvent.h"
 #include "nsIDOMIccManager.h"
+#include "StkCommandEvent.h"
 #endif
 
 #ifdef MOZ_B2G_BT
 #include "BluetoothManager.h"
 #include "BluetoothAdapter.h"
 #include "BluetoothDevice.h"
 #include "BluetoothPropertyEvent.h"
 #endif
@@ -1678,16 +1679,18 @@ static nsDOMClassInfoData sClassInfoData
   NS_DEFINE_CLASSINFO_DATA(CallEvent, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
   NS_DEFINE_CLASSINFO_DATA(MozVoicemail, nsEventTargetSH,
                            EVENTTARGET_SCRIPTABLE_FLAGS)
   NS_DEFINE_CLASSINFO_DATA(MozVoicemailEvent, nsDOMGenericSH,
                            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
   NS_DEFINE_CLASSINFO_DATA(BluetoothManager, nsEventTargetSH,
                            EVENTTARGET_SCRIPTABLE_FLAGS)
   NS_DEFINE_CLASSINFO_DATA(BluetoothAdapter, nsEventTargetSH,
                            EVENTTARGET_SCRIPTABLE_FLAGS)  
   NS_DEFINE_CLASSINFO_DATA(BluetoothDevice, nsEventTargetSH,
@@ -4490,16 +4493,21 @@ nsDOMClassInfo::Init()
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMEvent)
   DOM_CLASSINFO_MAP_END
 
   DOM_CLASSINFO_MAP_BEGIN(MozIccManager, nsIDOMMozIccManager)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozIccManager)
     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
   DOM_CLASSINFO_MAP_BEGIN(BluetoothManager, nsIDOMBluetoothManager)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMBluetoothManager)
   DOM_CLASSINFO_MAP_END  
 
   DOM_CLASSINFO_MAP_BEGIN(BluetoothAdapter, nsIDOMBluetoothAdapter)
--- a/dom/base/nsDOMClassInfoClasses.h
+++ b/dom/base/nsDOMClassInfoClasses.h
@@ -517,16 +517,17 @@ DOMCI_CLASS(MutationRecord)
 DOMCI_CLASS(MozWifiStatusChangeEvent)
 DOMCI_CLASS(MozWifiConnectionInfoEvent)
 DOMCI_CLASS(Telephony)
 DOMCI_CLASS(TelephonyCall)
 DOMCI_CLASS(CallEvent)
 DOMCI_CLASS(MozVoicemail)
 DOMCI_CLASS(MozVoicemailEvent)
 DOMCI_CLASS(MozIccManager)
+DOMCI_CLASS(MozStkCommandEvent)
 #endif
 
 #ifdef MOZ_B2G_BT
 DOMCI_CLASS(BluetoothManager)
 DOMCI_CLASS(BluetoothAdapter)
 DOMCI_CLASS(BluetoothDevice)
 DOMCI_CLASS(BluetoothPropertyEvent)
 #endif
--- a/dom/icc/src/IccManager.cpp
+++ b/dom/icc/src/IccManager.cpp
@@ -1,24 +1,33 @@
 /* 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/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,
                                                   nsDOMEventTargetHelper)
   NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(stkcommand)
   NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(stksessionend)
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
@@ -48,30 +57,67 @@ IccManager::IccManager()
     NS_WARNING("Could not acquire nsIMobileConnectionProvider!");
   }
 }
 
 void
 IccManager::Init(nsPIDOMWindow* aWindow)
 {
   BindToOwner(aWindow);
+
+  nsCOMPtr<nsIObserverService> 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<nsIObserverService> obs = services::GetObserverService();
+  if (!obs) {
+    NS_WARNING("Could not acquire nsIObserverService!");
+    return;
+  }
+
+  obs->RemoveObserver(this, kStkCommandTopic);
+  obs->RemoveObserver(this, kStkSessionEndTopic);
 }
 
 // nsIObserver
 
 NS_IMETHODIMP
 IccManager::Observe(nsISupports* aSubject,
                     const char* aTopic,
                     const PRUnichar* aData)
 {
+  if (!strcmp(aTopic, kStkCommandTopic)) {
+    nsString stkMsg;
+    stkMsg.Assign(aData);
+    nsRefPtr<StkCommandEvent> 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;
 }
 
 // nsIDOMMozIccManager
 
 NS_IMETHODIMP
 IccManager::SendStkResponse(const JS::Value& aResponse)
 {
@@ -89,14 +135,32 @@ IccManager::SendStkMenuSelection(PRUint1
   if (!mProvider) {
     return NS_ERROR_FAILURE;
   }
 
   mProvider->SendStkMenuSelection(GetOwner(), aItemIdentifier, aHelpRequested);
   return NS_OK;
 }
 
+nsresult
+IccManager::InternalDispatchEvent(const nsAString& aType)
+{
+  nsRefPtr<nsDOMEvent> 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)
 
 } // namespace icc
 } // namespace dom
 } // namespace mozilla
--- a/dom/icc/src/IccManager.h
+++ b/dom/icc/src/IccManager.h
@@ -32,16 +32,25 @@ public:
   void Shutdown();
 
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IccManager,
                                            nsDOMEventTargetHelper)
 
 private:
   nsCOMPtr<nsIMobileConnectionProvider> mProvider;
 
+  nsIDOMEventTarget*
+  ToIDOMEventTarget() const
+  {
+    return static_cast<nsDOMEventTargetHelper*>(
+           const_cast<IccManager*>(this));
+  }
+
+  nsresult InternalDispatchEvent(const nsAString& aType);
+
   NS_DECL_EVENT_HANDLER(stkcommand)
   NS_DECL_EVENT_HANDLER(stksessionend)
 };
 
 } // namespace icc
 } // namespace dom
 } // namespace mozilla
 
--- a/dom/icc/src/Makefile.in
+++ b/dom/icc/src/Makefile.in
@@ -10,18 +10,26 @@ VPATH            = $(srcdir)
 include $(DEPTH)/config/autoconf.mk
 
 LIBRARY_NAME     = dom_icc_s
 LIBXUL_LIBRARY   = 1
 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
 include $(topsrcdir)/ipc/chromium/chromium-config.mk
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /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>
+StkCommandEvent::Create(nsAString& aMessage)
+{
+  nsRefPtr<StkCommandEvent> 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<nsIJSON> 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;
+}
+
+}
+}
+}
new file mode 100644
--- /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<StkCommandEvent>
+  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<nsDOMEvent*>(const_cast<StkCommandEvent*>(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