From 9fe80b713bf344cfec8b2d3b4a11e2ce002d08e6 Mon Sep 17 00:00:00 2001 From: Neil Deakin Date: Mon, 11 Mar 2013 11:49:56 -0400 Subject: [PATCH] Bug 407983, add clipboard events constructor, r=smaug --- content/events/src/nsDOMClipboardEvent.cpp | 40 +++++++ content/events/src/nsDOMClipboardEvent.h | 4 + content/events/src/nsEventDispatcher.cpp | 2 +- dom/base/nsDOMClassInfo.cpp | 2 + .../events/nsIDOMClipboardEvent.idl | 12 ++ .../general/test_clipboard_events.html | 108 ++++++++++++++++-- js/xpconnect/src/dictionary_helper_gen.conf | 1 + 7 files changed, 157 insertions(+), 12 deletions(-) diff --git a/content/events/src/nsDOMClipboardEvent.cpp b/content/events/src/nsDOMClipboardEvent.cpp index dfdeb0a941cd..0aa8cfbf738b 100644 --- a/content/events/src/nsDOMClipboardEvent.cpp +++ b/content/events/src/nsDOMClipboardEvent.cpp @@ -7,6 +7,7 @@ #include "nsContentUtils.h" #include "nsClientRect.h" #include "nsDOMDataTransfer.h" +#include "DictionaryHelpers.h" #include "nsDOMClassInfoID.h" nsDOMClipboardEvent::nsDOMClipboardEvent(mozilla::dom::EventTarget* aOwner, @@ -41,6 +42,45 @@ NS_INTERFACE_MAP_END_INHERITING(nsDOMEvent) NS_IMPL_ADDREF_INHERITED(nsDOMClipboardEvent, nsDOMEvent) NS_IMPL_RELEASE_INHERITED(nsDOMClipboardEvent, nsDOMEvent) +nsresult +nsDOMClipboardEvent::InitClipboardEvent(const nsAString & aType, bool aCanBubble, bool aCancelable, + nsIDOMDataTransfer* clipboardData) +{ + nsresult rv = nsDOMEvent::InitEvent(aType, aCanBubble, aCancelable); + NS_ENSURE_SUCCESS(rv, rv); + + nsClipboardEvent* event = static_cast(mEvent); + event->clipboardData = clipboardData; + + return NS_OK; +} + +nsresult +nsDOMClipboardEvent::InitFromCtor(const nsAString& aType, + JSContext* aCx, jsval* aVal) +{ + mozilla::idl::ClipboardEventInit d; + nsresult rv = d.Init(aCx, aVal); + NS_ENSURE_SUCCESS(rv, rv); + + nsRefPtr clipboardData; + if (mEventIsInternal) { + nsClipboardEvent* event = static_cast(mEvent); + if (event) { + // Always create a clipboardData for the copy event. If this is changed to + // support other types of events, make sure that read/write privileges are + // checked properly within nsDOMDataTransfer. + clipboardData = new nsDOMDataTransfer(NS_COPY, false); + clipboardData->SetData(d.dataType, d.data); + } + } + + rv = InitClipboardEvent(aType, d.bubbles, d.cancelable, clipboardData); + NS_ENSURE_SUCCESS(rv, rv); + + return NS_OK; +} + NS_IMETHODIMP nsDOMClipboardEvent::GetClipboardData(nsIDOMDataTransfer** aClipboardData) { diff --git a/content/events/src/nsDOMClipboardEvent.h b/content/events/src/nsDOMClipboardEvent.h index 498f4bac03ac..39b0f98d4c35 100644 --- a/content/events/src/nsDOMClipboardEvent.h +++ b/content/events/src/nsDOMClipboardEvent.h @@ -24,6 +24,10 @@ public: // Forward to base class NS_FORWARD_TO_NSDOMEVENT + + nsresult InitFromCtor(const nsAString& aType, + JSContext* aCx, jsval* aVal); + }; #endif // nsDOMClipboardEvent_h_ diff --git a/content/events/src/nsEventDispatcher.cpp b/content/events/src/nsEventDispatcher.cpp index 7359b7680a86..863800a5385d 100644 --- a/content/events/src/nsEventDispatcher.cpp +++ b/content/events/src/nsEventDispatcher.cpp @@ -742,7 +742,7 @@ nsEventDispatcher::CreateEvent(mozilla::dom::EventTarget* aOwner, return NS_NewDOMTextEvent(aDOMEvent, aOwner, aPresContext, static_cast(aEvent)); case NS_CLIPBOARD_EVENT: - return NS_NewDOMClipboardEvent(aDOMEvent, aPresContext, + return NS_NewDOMClipboardEvent(aDOMEvent, aOwner, aPresContext, static_cast(aEvent)); case NS_SVG_EVENT: return NS_NewDOMSVGEvent(aDOMEvent, aOwner, aPresContext, diff --git a/dom/base/nsDOMClassInfo.cpp b/dom/base/nsDOMClassInfo.cpp index 0a2ef4b7e324..a85a24dd8307 100644 --- a/dom/base/nsDOMClassInfo.cpp +++ b/dom/base/nsDOMClassInfo.cpp @@ -1196,6 +1196,7 @@ NS_DEFINE_CONTRACT_CTOR(MozActivity, NS_DOMACTIVITY_CONTRACTID) NS_DEFINE_EVENT_CTOR(UIEvent) NS_DEFINE_EVENT_CTOR(MouseEvent) NS_DEFINE_EVENT_CTOR(WheelEvent) +NS_DEFINE_EVENT_CTOR(ClipboardEvent) #define MOZ_GENERATED_EVENT_LIST #define MOZ_GENERATED_EVENT(_event_interface) \ @@ -1222,6 +1223,7 @@ static const nsConstructorFuncMapData kConstructorFuncMap[] = NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(UIEvent) NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(MouseEvent) NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(WheelEvent) + NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(ClipboardEvent) #ifdef MOZ_B2G_RIL NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(MozWifiStatusChangeEvent) NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(MozWifiConnectionInfoEvent) diff --git a/dom/interfaces/events/nsIDOMClipboardEvent.idl b/dom/interfaces/events/nsIDOMClipboardEvent.idl index b5ce7b03bc5f..1e38befc35c0 100644 --- a/dom/interfaces/events/nsIDOMClipboardEvent.idl +++ b/dom/interfaces/events/nsIDOMClipboardEvent.idl @@ -13,4 +13,16 @@ interface nsIDOMClipboardEvent : nsIDOMEvent { readonly attribute nsIDOMDataTransfer clipboardData; + // The constructor must be used from script to initialize + // clipboard events. + [noscript] void initClipboardEvent(in DOMString typeArg, + in boolean canBubbleArg, + in boolean cancelableArg, + in nsIDOMDataTransfer clipboardData); +}; + +dictionary ClipboardEventInit : EventInit +{ + DOMString data; + DOMString dataType; }; diff --git a/dom/tests/mochitest/general/test_clipboard_events.html b/dom/tests/mochitest/general/test_clipboard_events.html index 9bf4dcf0b17f..684ed6f74b5c 100644 --- a/dom/tests/mochitest/general/test_clipboard_events.html +++ b/dom/tests/mochitest/general/test_clipboard_events.html @@ -4,7 +4,7 @@ Test for Clipboard Events - + @@ -12,6 +12,10 @@
CONTENT TEXT
+
Spot
+