From 21d8fc32e7379b0bae6bdcd0ef5709c22794640c Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Mon, 30 Jun 2014 16:02:04 -0700 Subject: [PATCH] Bug 1031051: Part 17 - Manually implement CustomEvent. r=smaug CustomEvent can't be generated because we still need the XPCOM interface which is used by a11y among others. --- dom/base/nsGlobalWindow.cpp | 2 +- dom/browser-element/BrowserElementParent.cpp | 3 +- dom/events/CustomEvent.cpp | 117 +++++++++++++++++++ dom/events/CustomEvent.h | 59 ++++++++++ dom/events/moz.build | 2 + dom/interfaces/events/nsIDOMEvent.idl | 5 + dom/webidl/CustomEvent.webidl | 3 +- js/xpconnect/src/event_impl_gen.conf.in | 1 - 8 files changed, 186 insertions(+), 6 deletions(-) create mode 100644 dom/events/CustomEvent.cpp create mode 100644 dom/events/CustomEvent.h diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index 7c75db16e370..53b01d088826 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -143,7 +143,7 @@ #include "nsIDOMXULControlElement.h" #include "nsMenuPopupFrame.h" #endif -#include "nsIDOMCustomEvent.h" +#include "mozilla/dom/CustomEvent.h" #include "nsIFrameRequestCallback.h" #include "nsIJARChannel.h" diff --git a/dom/browser-element/BrowserElementParent.cpp b/dom/browser-element/BrowserElementParent.cpp index 4a4ded818d2d..d2a123a4cea7 100644 --- a/dom/browser-element/BrowserElementParent.cpp +++ b/dom/browser-element/BrowserElementParent.cpp @@ -16,12 +16,11 @@ #include "mozilla/EventDispatcher.h" #include "mozilla/dom/HTMLIFrameElement.h" #include "mozilla/dom/ToJSValue.h" -#include "nsIDOMCustomEvent.h" #include "nsIInterfaceRequestorUtils.h" #include "nsVariant.h" #include "mozilla/dom/BrowserElementDictionariesBinding.h" #include "nsCxPusher.h" -#include "GeneratedEventClasses.h" +#include "mozilla/dom/CustomEvent.h" using namespace mozilla; using namespace mozilla::dom; diff --git a/dom/events/CustomEvent.cpp b/dom/events/CustomEvent.cpp new file mode 100644 index 000000000000..4f12187fd439 --- /dev/null +++ b/dom/events/CustomEvent.cpp @@ -0,0 +1,117 @@ +/* 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 "CustomEvent.h" +#include "mozilla/dom/CustomEventBinding.h" + +#include "mozilla/dom/BindingUtils.h" +#include "nsContentUtils.h" +#include "nsIXPConnect.h" + +using namespace mozilla; +using namespace mozilla::dom; + +CustomEvent::CustomEvent(mozilla::dom::EventTarget* aOwner, + nsPresContext* aPresContext, + mozilla::WidgetEvent* aEvent) +: Event(aOwner, aPresContext, aEvent) +{ +} + +CustomEvent::~CustomEvent() {} + +NS_IMPL_CYCLE_COLLECTION_CLASS(CustomEvent) +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(CustomEvent, Event) + NS_IMPL_CYCLE_COLLECTION_UNLINK(mDetail) +NS_IMPL_CYCLE_COLLECTION_UNLINK_END + +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CustomEvent, Event) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDetail) +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END + +NS_IMPL_ADDREF_INHERITED(CustomEvent, Event) +NS_IMPL_RELEASE_INHERITED(CustomEvent, Event) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(CustomEvent) + NS_INTERFACE_MAP_ENTRY(nsIDOMCustomEvent) +NS_INTERFACE_MAP_END_INHERITING(Event) + +already_AddRefed +CustomEvent::Constructor(const GlobalObject& aGlobal, + const nsAString& aType, + const CustomEventInit& aParam, + ErrorResult& aRv) +{ + nsCOMPtr t = do_QueryInterface(aGlobal.GetAsSupports()); + nsRefPtr e = new CustomEvent(t, nullptr, nullptr); + bool trusted = e->Init(t); + JS::Rooted detail(aGlobal.Context(), aParam.mDetail); + e->InitCustomEvent(aGlobal.Context(), aType, aParam.mBubbles, aParam.mCancelable, detail, aRv); + e->SetTrusted(trusted); + return e.forget(); +} + +JSObject* +CustomEvent::WrapObject(JSContext* aCx) +{ + return mozilla::dom::CustomEventBinding::Wrap(aCx, this); +} + +NS_IMETHODIMP +CustomEvent::InitCustomEvent(const nsAString& aType, + bool aCanBubble, + bool aCancelable, + nsIVariant* aDetail) +{ + nsresult rv = Event::InitEvent(aType, aCanBubble, aCancelable); + NS_ENSURE_SUCCESS(rv, rv); + mDetail = aDetail; + return NS_OK; +} + +void +CustomEvent::InitCustomEvent(JSContext* aCx, + const nsAString& aType, + bool aCanBubble, + bool aCancelable, + JS::Handle aDetail, + ErrorResult& aRv) +{ + nsCOMPtr detail; + if (nsIXPConnect* xpc = nsContentUtils::XPConnect()) { + xpc->JSToVariant(aCx, aDetail, getter_AddRefs(detail)); + } + + if (!detail) { + aRv.Throw(NS_ERROR_FAILURE); + return; + } + aRv = InitCustomEvent(aType, aCanBubble, aCancelable, detail); +} + +NS_IMETHODIMP +CustomEvent::GetDetail(nsIVariant** aDetail) +{ + NS_IF_ADDREF(*aDetail = mDetail); + return NS_OK; +} + +void +CustomEvent::GetDetail(JSContext* aCx, + JS::MutableHandle aRetval) +{ + VariantToJsval(aCx, mDetail, aRetval); +} + +nsresult +NS_NewDOMCustomEvent(nsIDOMEvent** aInstancePtrResult, + mozilla::dom::EventTarget* aOwner, + nsPresContext* aPresContext, + mozilla::WidgetEvent* aEvent) +{ + CustomEvent* it = new CustomEvent(aOwner, aPresContext, aEvent); + NS_ADDREF(it); + *aInstancePtrResult = static_cast(it); + return NS_OK; +} diff --git a/dom/events/CustomEvent.h b/dom/events/CustomEvent.h new file mode 100644 index 000000000000..f3bbff5a8899 --- /dev/null +++ b/dom/events/CustomEvent.h @@ -0,0 +1,59 @@ +/* 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 CustomEvent_h__ +#define CustomEvent_h__ + +#include "mozilla/dom/Event.h" +#include "nsIDOMCustomEvent.h" + +namespace mozilla { +namespace dom { + +struct CustomEventInit; + +class CustomEvent MOZ_FINAL : public Event, + public nsIDOMCustomEvent +{ +private: + virtual ~CustomEvent(); + + nsCOMPtr mDetail; + +public: + CustomEvent(mozilla::dom::EventTarget* aOwner, + nsPresContext* aPresContext = nullptr, + mozilla::WidgetEvent* aEvent = nullptr); + + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CustomEvent, Event) + NS_FORWARD_TO_EVENT + NS_DECL_NSIDOMCUSTOMEVENT + + static already_AddRefed + Constructor(const GlobalObject& aGlobal, + const nsAString& aType, + const CustomEventInit& aParam, + ErrorResult& aRv); + + virtual JSObject* + WrapObject(JSContext* aCx) MOZ_OVERRIDE; + + void + GetDetail(JSContext* aCx, + JS::MutableHandle aRetval); + + void + InitCustomEvent(JSContext* aCx, + const nsAString& aType, + bool aCanBubble, + bool aCancelable, + JS::Handle aDetail, + ErrorResult& aRv); +}; + +} // namespace dom +} // namespace mozilla + +#endif // CustomEvent_h__ diff --git a/dom/events/moz.build b/dom/events/moz.build index 729db7e0eb8a..7e9fc46b1869 100644 --- a/dom/events/moz.build +++ b/dom/events/moz.build @@ -36,6 +36,7 @@ EXPORTS.mozilla.dom += [ 'ClipboardEvent.h', 'CommandEvent.h', 'CompositionEvent.h', + 'CustomEvent.h', 'DataContainerEvent.h', 'DataTransfer.h', 'DeviceMotionEvent.h', @@ -112,6 +113,7 @@ UNIFIED_SOURCES += [ # nsEventStateManager.cpp should be built separately because of Mac OS X headers. SOURCES += [ + 'CustomEvent.cpp', 'EventStateManager.cpp', ] diff --git a/dom/interfaces/events/nsIDOMEvent.idl b/dom/interfaces/events/nsIDOMEvent.idl index dc7e8e5883d7..ca02589b8463 100644 --- a/dom/interfaces/events/nsIDOMEvent.idl +++ b/dom/interfaces/events/nsIDOMEvent.idl @@ -365,4 +365,9 @@ NS_NewDOMTouchEvent(nsIDOMEvent** aInstancePtrResult, mozilla::dom::EventTarget* aOwner, nsPresContext* aPresContext, mozilla::WidgetTouchEvent* aEvent); +nsresult +NS_NewDOMCustomEvent(nsIDOMEvent** aInstancePtrResult, + mozilla::dom::EventTarget* aOwner, + nsPresContext* aPresContext, + mozilla::WidgetEvent* aEvent); %} diff --git a/dom/webidl/CustomEvent.webidl b/dom/webidl/CustomEvent.webidl index c4a2eedc37ff..180159b33ae6 100644 --- a/dom/webidl/CustomEvent.webidl +++ b/dom/webidl/CustomEvent.webidl @@ -10,10 +10,9 @@ * liability, trademark and document use rules apply. */ -[Constructor(DOMString type, optional CustomEventInit eventInitDict), HeaderFile="GeneratedEventClasses.h"] +[Constructor(DOMString type, optional CustomEventInit eventInitDict)] interface CustomEvent : Event { - [Throws] readonly attribute any detail; // initCustomEvent is a Gecko specific deprecated method. diff --git a/js/xpconnect/src/event_impl_gen.conf.in b/js/xpconnect/src/event_impl_gen.conf.in index b4f5527cc6e7..12c1073ad4f5 100644 --- a/js/xpconnect/src/event_impl_gen.conf.in +++ b/js/xpconnect/src/event_impl_gen.conf.in @@ -8,7 +8,6 @@ Init dictionary for the event constructor. """ simple_events = [ - 'CustomEvent', ] """ include file names """