Bug 744714 - Part 4 : DOMEvent. r=smaug

This commit is contained in:
Yoshi Huang 2012-06-11 19:28:16 +08:00
parent d6d8a831ba
commit 34c48a72ba
7 changed files with 209 additions and 0 deletions

View File

@ -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

View File

@ -522,6 +522,7 @@ DOMCI_CLASS(CallEvent)
DOMCI_CLASS(MozVoicemail)
DOMCI_CLASS(MozVoicemailEvent)
DOMCI_CLASS(MozIccManager)
DOMCI_CLASS(MozStkCommandEvent)
#endif
#ifdef MOZ_B2G_BT

View File

@ -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<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
@ -67,6 +93,26 @@ 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;
}
@ -94,6 +140,24 @@ IccManager::SendStkMenuSelection(PRUint16 aItemIdentifier, bool 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)

View File

@ -37,6 +37,15 @@ public:
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)
};

View File

@ -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

View File

@ -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;
}
}
}
}

View File

@ -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