Bug 874842 - Return Event instead of nsIDOMEvent
authorAryeh Gregor <ayg@aryeh.name>
Wed, 12 Aug 2015 14:39:31 +0300
changeset 257451 68f0b91431932cb255ae55da6ceb4a567dc0609e
parent 257450 93e1af0f8be18ea536caf00708e3db75cb27c73b
child 257452 05cba24084d0bb5039845ad1b72e80c5c94db0d5
push id29219
push userkwierso@gmail.com
push dateWed, 12 Aug 2015 22:16:22 +0000
treeherdermozilla-central@7649ffe28b67 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs874842
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 874842 - Return Event instead of nsIDOMEvent
dom/base/DOMRequest.cpp
dom/base/EventSource.cpp
dom/base/WebSocket.cpp
dom/base/nsDOMDataChannel.cpp
dom/base/nsDocument.cpp
dom/base/nsGlobalWindow.cpp
dom/base/nsPerformance.cpp
dom/base/nsXMLHttpRequest.cpp
dom/bluetooth/bluetooth1/BluetoothAdapter.cpp
dom/bluetooth/bluetooth2/BluetoothAdapter.cpp
dom/bluetooth/bluetooth2/BluetoothGatt.cpp
dom/browser-element/BrowserElementParent.cpp
dom/events/AnimationEvent.cpp
dom/events/AnimationEvent.h
dom/events/AsyncEventDispatcher.cpp
dom/events/BeforeAfterKeyboardEvent.cpp
dom/events/BeforeAfterKeyboardEvent.h
dom/events/BeforeUnloadEvent.cpp
dom/events/BeforeUnloadEvent.h
dom/events/ClipboardEvent.cpp
dom/events/ClipboardEvent.h
dom/events/CommandEvent.cpp
dom/events/CommandEvent.h
dom/events/CompositionEvent.cpp
dom/events/CompositionEvent.h
dom/events/CustomEvent.cpp
dom/events/CustomEvent.h
dom/events/DOMEventTargetHelper.cpp
dom/events/DataContainerEvent.cpp
dom/events/DataContainerEvent.h
dom/events/DeviceMotionEvent.cpp
dom/events/DeviceMotionEvent.h
dom/events/DragEvent.cpp
dom/events/DragEvent.h
dom/events/Event.cpp
dom/events/Event.h
dom/events/EventDispatcher.cpp
dom/events/EventDispatcher.h
dom/events/EventListenerManager.cpp
dom/events/EventStateManager.cpp
dom/events/FocusEvent.cpp
dom/events/FocusEvent.h
dom/events/InputEvent.cpp
dom/events/InputEvent.h
dom/events/KeyboardEvent.cpp
dom/events/KeyboardEvent.h
dom/events/MessageEvent.cpp
dom/events/MessageEvent.h
dom/events/MouseEvent.cpp
dom/events/MouseEvent.h
dom/events/MouseScrollEvent.cpp
dom/events/MouseScrollEvent.h
dom/events/MutationEvent.cpp
dom/events/MutationEvent.h
dom/events/NotifyPaintEvent.cpp
dom/events/NotifyPaintEvent.h
dom/events/PointerEvent.cpp
dom/events/PointerEvent.h
dom/events/ScrollAreaEvent.cpp
dom/events/ScrollAreaEvent.h
dom/events/SimpleGestureEvent.cpp
dom/events/SimpleGestureEvent.h
dom/events/StorageEvent.cpp
dom/events/StorageEvent.h
dom/events/TouchEvent.cpp
dom/events/TouchEvent.h
dom/events/TransitionEvent.cpp
dom/events/TransitionEvent.h
dom/events/UIEvent.cpp
dom/events/UIEvent.h
dom/events/WheelEvent.cpp
dom/events/WheelEvent.h
dom/events/XULCommandEvent.cpp
dom/events/XULCommandEvent.h
dom/events/moz.build
dom/html/HTMLInputElement.cpp
dom/interfaces/events/nsIDOMEvent.idl
dom/ipc/TabMessageUtils.cpp
dom/ipc/TabParent.cpp
dom/media/MediaRecorder.cpp
dom/media/TextTrackList.cpp
dom/notification/DesktopNotification.cpp
dom/notification/Notification.cpp
dom/offline/nsDOMOfflineResourceList.cpp
dom/presentation/PresentationSession.cpp
dom/smil/TimeEvent.cpp
dom/smil/TimeEvent.h
dom/speakermanager/SpeakerManager.cpp
dom/svg/SVGZoomEvent.cpp
dom/svg/SVGZoomEvent.h
dom/workers/ServiceWorkerManager.cpp
dom/workers/WorkerPrivate.cpp
dom/workers/XMLHttpRequest.cpp
dom/xml/nsXMLPrettyPrinter.cpp
layout/base/nsPresContext.cpp
layout/xul/nsMenuFrame.cpp
--- a/dom/base/DOMRequest.cpp
+++ b/dom/base/DOMRequest.cpp
@@ -4,18 +4,18 @@
  * 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 "DOMRequest.h"
 
 #include "DOMError.h"
 #include "nsThreadUtils.h"
 #include "DOMCursor.h"
-#include "nsIDOMEvent.h"
 #include "mozilla/ErrorResult.h"
+#include "mozilla/dom/Event.h"
 #include "mozilla/dom/Promise.h"
 #include "mozilla/dom/ScriptSettings.h"
 
 using mozilla::dom::AnyCallback;
 using mozilla::dom::DOMError;
 using mozilla::dom::DOMRequest;
 using mozilla::dom::DOMRequestService;
 using mozilla::dom::DOMCursor;
@@ -187,18 +187,17 @@ DOMRequest::FireDetailedError(DOMError* 
 
 void
 DOMRequest::FireEvent(const nsAString& aType, bool aBubble, bool aCancelable)
 {
   if (NS_FAILED(CheckInnerWindowCorrectness())) {
     return;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
   nsresult rv = event->InitEvent(aType, aBubble, aCancelable);
   if (NS_FAILED(rv)) {
     return;
   }
 
   event->SetTrusted(true);
 
   bool dummy;
--- a/dom/base/EventSource.cpp
+++ b/dom/base/EventSource.cpp
@@ -840,22 +840,17 @@ EventSource::AnnounceConnection()
 
   mReadyState = OPEN;
 
   nsresult rv = CheckInnerWindowCorrectness();
   if (NS_FAILED(rv)) {
     return;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  if (NS_FAILED(rv)) {
-    NS_WARNING("Failed to create the open event!!!");
-    return;
-  }
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
 
   // it doesn't bubble, and it isn't cancelable
   rv = event->InitEvent(NS_LITERAL_STRING("open"), false, false);
   if (NS_FAILED(rv)) {
     NS_WARNING("Failed to init the open event!!!");
     return;
   }
 
@@ -905,22 +900,17 @@ EventSource::ReestablishConnection()
     return;
   }
 
   rv = CheckInnerWindowCorrectness();
   if (NS_FAILED(rv)) {
     return;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  if (NS_FAILED(rv)) {
-    NS_WARNING("Failed to create the error event!!!");
-    return;
-  }
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
 
   // it doesn't bubble, and it isn't cancelable
   rv = event->InitEvent(NS_LITERAL_STRING("error"), false, false);
   if (NS_FAILED(rv)) {
     NS_WARNING("Failed to init the error event!!!");
     return;
   }
 
@@ -1061,22 +1051,17 @@ EventSource::FailConnection()
 
   Close(); // it sets mReadyState to CLOSED
 
   rv = CheckInnerWindowCorrectness();
   if (NS_FAILED(rv)) {
     return;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  if (NS_FAILED(rv)) {
-    NS_WARNING("Failed to create the error event!!!");
-    return;
-  }
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
 
   // it doesn't bubble, and it isn't cancelable
   rv = event->InitEvent(NS_LITERAL_STRING("error"), false, false);
   if (NS_FAILED(rv)) {
     NS_WARNING("Failed to init the error event!!!");
     return;
   }
 
@@ -1229,37 +1214,30 @@ EventSource::DispatchAllMessageEvents()
       NS_ENSURE_TRUE_VOID(jsString);
 
       jsData.setString(jsString);
     }
 
     // create an event that uses the MessageEvent interface,
     // which does not bubble, is not cancelable, and has no default action
 
-    nsCOMPtr<nsIDOMEvent> event;
-    rv = NS_NewDOMMessageEvent(getter_AddRefs(event), this, nullptr, nullptr);
-    if (NS_FAILED(rv)) {
-      NS_WARNING("Failed to create the message event!!!");
-      return;
-    }
+    nsRefPtr<MessageEvent> event =
+      NS_NewDOMMessageEvent(this, nullptr, nullptr);
 
-    nsCOMPtr<nsIDOMMessageEvent> messageEvent = do_QueryInterface(event);
-    rv = messageEvent->InitMessageEvent(message->mEventName,
-                                        false, false,
-                                        jsData,
-                                        mOrigin,
-                                        message->mLastEventID, nullptr);
+    rv = event->InitMessageEvent(message->mEventName, false, false, jsData,
+                                 mOrigin, message->mLastEventID, nullptr);
     if (NS_FAILED(rv)) {
       NS_WARNING("Failed to init the message event!!!");
       return;
     }
 
-    messageEvent->SetTrusted(true);
+    event->SetTrusted(true);
 
-    rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
+    rv = DispatchDOMEvent(nullptr, static_cast<Event*>(event), nullptr,
+                          nullptr);
     if (NS_FAILED(rv)) {
       NS_WARNING("Failed to dispatch the message event!!!");
       return;
     }
 
     mLastEventID.Assign(message->mLastEventID);
   }
 }
--- a/dom/base/WebSocket.cpp
+++ b/dom/base/WebSocket.cpp
@@ -8,16 +8,17 @@
 #include "mozilla/dom/WebSocketBinding.h"
 #include "mozilla/net/WebSocketChannel.h"
 
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "mozilla/DOMEventTargetHelper.h"
 #include "mozilla/net/WebSocketChannel.h"
 #include "mozilla/dom/File.h"
+#include "mozilla/dom/MessageEvent.h"
 #include "mozilla/dom/nsCSPContext.h"
 #include "mozilla/dom/nsCSPUtils.h"
 #include "mozilla/dom/ScriptSettings.h"
 #include "mozilla/dom/WorkerPrivate.h"
 #include "mozilla/dom/WorkerRunnable.h"
 #include "mozilla/dom/WorkerScope.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsIDOMWindow.h"
@@ -25,17 +26,16 @@
 #include "nsXPCOM.h"
 #include "nsIXPConnect.h"
 #include "nsContentUtils.h"
 #include "nsError.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsIURL.h"
 #include "nsIUnicodeEncoder.h"
 #include "nsThreadUtils.h"
-#include "nsIDOMMessageEvent.h"
 #include "nsIPromptFactory.h"
 #include "nsIWindowWatcher.h"
 #include "nsIPrompt.h"
 #include "nsIStringBundle.h"
 #include "nsIConsoleService.h"
 #include "mozilla/dom/CloseEvent.h"
 #include "nsICryptoHash.h"
 #include "nsJSUtils.h"
@@ -1732,19 +1732,17 @@ WebSocket::CreateAndDispatchSimpleEvent(
   MOZ_ASSERT(mImpl);
   AssertIsOnTargetThread();
 
   nsresult rv = CheckInnerWindowCorrectness();
   if (NS_FAILED(rv)) {
     return NS_OK;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
 
   // it doesn't bubble, and it isn't cancelable
   rv = event->InitEvent(aName, false, false);
   NS_ENSURE_SUCCESS(rv, rv);
 
   event->SetTrusted(true);
 
   return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
@@ -1812,31 +1810,27 @@ WebSocket::CreateAndDispatchMessageEvent
     NS_ENSURE_TRUE(jsString, NS_ERROR_FAILURE);
 
     jsData.setString(jsString);
   }
 
   // create an event that uses the MessageEvent interface,
   // which does not bubble, is not cancelable, and has no default action
 
-  nsCOMPtr<nsIDOMEvent> event;
-  rv = NS_NewDOMMessageEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  nsCOMPtr<nsIDOMMessageEvent> messageEvent = do_QueryInterface(event);
-  rv = messageEvent->InitMessageEvent(NS_LITERAL_STRING("message"),
-                                      false, false,
-                                      jsData,
-                                      mImpl->mUTF16Origin,
-                                      EmptyString(), nullptr);
+  nsRefPtr<MessageEvent> event = NS_NewDOMMessageEvent(this, nullptr, nullptr);
+
+  rv = event->InitMessageEvent(NS_LITERAL_STRING("message"), false, false,
+                               jsData, mImpl->mUTF16Origin, EmptyString(),
+                               nullptr);
   NS_ENSURE_SUCCESS(rv, rv);
 
   event->SetTrusted(true);
 
-  return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
+  return DispatchDOMEvent(nullptr, static_cast<Event*>(event), nullptr,
+                          nullptr);
 }
 
 nsresult
 WebSocket::CreateAndDispatchCloseEvent(bool aWasClean,
                                        uint16_t aCode,
                                        const nsAString &aReason)
 {
   MOZ_ASSERT(mImpl);
--- a/dom/base/nsDOMDataChannel.cpp
+++ b/dom/base/nsDOMDataChannel.cpp
@@ -12,19 +12,19 @@
 extern PRLogModuleInfo* GetDataChannelLog();
 #undef LOG
 #define LOG(args) MOZ_LOG(GetDataChannelLog(), mozilla::LogLevel::Debug, args)
 
 
 #include "nsDOMDataChannelDeclarations.h"
 #include "nsDOMDataChannel.h"
 #include "nsIDOMDataChannel.h"
-#include "nsIDOMMessageEvent.h"
 #include "mozilla/DOMEventTargetHelper.h"
 #include "mozilla/dom/File.h"
+#include "mozilla/dom/MessageEvent.h"
 #include "mozilla/dom/ScriptSettings.h"
 
 #include "nsError.h"
 #include "nsAutoPtr.h"
 #include "nsContentUtils.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsIScriptObjectPrincipal.h"
 
@@ -396,30 +396,25 @@ nsDOMDataChannel::DoOnMessageAvailable(c
   } else {
     NS_ConvertUTF8toUTF16 utf16data(aData);
     JSString* jsString = JS_NewUCStringCopyN(cx, utf16data.get(), utf16data.Length());
     NS_ENSURE_TRUE(jsString, NS_ERROR_FAILURE);
 
     jsData.setString(jsString);
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  rv = NS_NewDOMMessageEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  NS_ENSURE_SUCCESS(rv,rv);
+  nsRefPtr<MessageEvent> event = NS_NewDOMMessageEvent(this, nullptr, nullptr);
 
-  nsCOMPtr<nsIDOMMessageEvent> messageEvent = do_QueryInterface(event);
-  rv = messageEvent->InitMessageEvent(NS_LITERAL_STRING("message"),
-                                      false, false,
-                                      jsData, mOrigin, EmptyString(),
-                                      nullptr);
+  rv = event->InitMessageEvent(NS_LITERAL_STRING("message"), false, false,
+                               jsData, mOrigin, EmptyString(), nullptr);
   NS_ENSURE_SUCCESS(rv,rv);
   event->SetTrusted(true);
 
   LOG(("%p(%p): %s - Dispatching\n",this,(void*)mDataChannel,__FUNCTION__));
-  rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
+  rv = DispatchDOMEvent(nullptr, static_cast<Event*>(event), nullptr, nullptr);
   if (NS_FAILED(rv)) {
     NS_WARNING("Failed to dispatch the message event!!!");
   }
   return rv;
 }
 
 nsresult
 nsDOMDataChannel::OnMessageAvailable(nsISupports* aContext,
@@ -442,19 +437,17 @@ nsDOMDataChannel::OnSimpleEvent(nsISuppo
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   nsresult rv = CheckInnerWindowCorrectness();
   if (NS_FAILED(rv)) {
     return NS_OK;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  NS_ENSURE_SUCCESS(rv,rv);
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
 
   rv = event->InitEvent(aName, false, false);
   NS_ENSURE_SUCCESS(rv,rv);
 
   event->SetTrusted(true);
 
   return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
 }
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -8127,27 +8127,27 @@ nsIDocument::CreateEvent(const nsAString
   nsPresContext *presContext = nullptr;
 
   if (shell) {
     // Retrieve the context
     presContext = shell->GetPresContext();
   }
 
   // Create event even without presContext.
-  nsCOMPtr<nsIDOMEvent> ev;
-  rv = EventDispatcher::CreateEvent(const_cast<nsIDocument*>(this),
-                                    presContext, nullptr, aEventType,
-                                    getter_AddRefs(ev));
+  nsRefPtr<Event> ev =
+    EventDispatcher::CreateEvent(const_cast<nsIDocument*>(this), presContext,
+                                 nullptr, aEventType);
   if (!ev) {
+    rv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
     return nullptr;
   }
   WidgetEvent* e = ev->GetInternalNSEvent();
   e->mFlags.mBubbles = false;
   e->mFlags.mCancelable = false;
-  return dont_AddRef(ev.forget().take()->InternalDOMEvent());
+  return ev.forget();
 }
 
 void
 nsDocument::FlushPendingNotifications(mozFlushType aType)
 {
   nsDocumentOnStack dos(this);
 
   // We need to flush the sink for non-HTML documents (because the XML
@@ -9204,18 +9204,17 @@ NotifyPageHide(nsIDocument* aDocument, v
   aDocument->OnPageHide(*aPersistedPtr, nullptr);
   return true;
 }
 
 static void
 DispatchCustomEventWithFlush(nsINode* aTarget, const nsAString& aEventType,
                              bool aBubbles, bool aOnlyChromeDispatch)
 {
-  nsCOMPtr<nsIDOMEvent> event;
-  NS_NewDOMEvent(getter_AddRefs(event), aTarget, nullptr, nullptr);
+  nsRefPtr<Event> event = NS_NewDOMEvent(aTarget, nullptr, nullptr);
   nsresult rv = event->InitEvent(aEventType, aBubbles, false);
   if (NS_FAILED(rv)) {
     return;
   }
   event->SetTrusted(true);
   if (aOnlyChromeDispatch) {
     event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
   }
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -9096,18 +9096,17 @@ nsGlobalWindow::LeaveModalState()
   }
 
   // Remember the time of the last dialog quit.
   nsGlobalWindow *inner = topWin->GetCurrentInnerWindowInternal();
   if (inner)
     inner->mLastDialogQuitTime = TimeStamp::Now();
 
   if (topWin->mModalStateDepth == 0) {
-    nsCOMPtr<nsIDOMEvent> event;
-    NS_NewDOMEvent(getter_AddRefs(event), topWin, nullptr, nullptr);
+    nsRefPtr<Event> event = NS_NewDOMEvent(topWin, nullptr, nullptr);
     event->InitEvent(NS_LITERAL_STRING("endmodalstate"), true, false);
     event->SetTrusted(true);
     event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
     bool dummy;
     topWin->DispatchEvent(event, &dummy);
   }
 }
 
@@ -11885,18 +11884,17 @@ nsGlobalWindow::Observe(nsISupports* aSu
 #ifdef MOZ_B2G
   if (!nsCRT::strcmp(aTopic, NS_NETWORK_ACTIVITY_BLIP_UPLOAD_TOPIC) ||
       !nsCRT::strcmp(aTopic, NS_NETWORK_ACTIVITY_BLIP_DOWNLOAD_TOPIC)) {
     MOZ_ASSERT(IsInnerWindow());
     if (!IsCurrentInnerWindow()) {
       return NS_OK;
     }
 
-    nsCOMPtr<nsIDOMEvent> event;
-    NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
+    nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
     nsresult rv = event->InitEvent(
       !nsCRT::strcmp(aTopic, NS_NETWORK_ACTIVITY_BLIP_UPLOAD_TOPIC)
         ? NETWORK_UPLOAD_EVENT_NAME
         : NETWORK_DOWNLOAD_EVENT_NAME,
       false, false);
     NS_ENSURE_SUCCESS(rv, rv);
 
     event->SetTrusted(true);
@@ -11922,18 +11920,17 @@ nsGlobalWindow::Observe(nsISupports* aSu
       NavigatorBinding::ClearCachedLanguagesValue(mNavigator);
     }
 
     // The event has to be dispatched only to the current inner window.
     if (!IsCurrentInnerWindow()) {
       return NS_OK;
     }
 
-    nsCOMPtr<nsIDOMEvent> event;
-    NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
+    nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
     nsresult rv = event->InitEvent(NS_LITERAL_STRING("languagechange"), false, false);
     NS_ENSURE_SUCCESS(rv, rv);
 
     event->SetTrusted(true);
 
     bool dummy;
     return DispatchEvent(event, &dummy);
   }
--- a/dom/base/nsPerformance.cpp
+++ b/dom/base/nsPerformance.cpp
@@ -473,26 +473,25 @@ nsPerformance::Timing()
         mDOMTiming->GetNavigationStart());
   }
   return mTiming;
 }
 
 void
 nsPerformance::DispatchBufferFullEvent()
 {
-  nsCOMPtr<nsIDOMEvent> event;
-  nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  if (NS_SUCCEEDED(rv)) {
-    // it bubbles, and it isn't cancelable
-    rv = event->InitEvent(NS_LITERAL_STRING("resourcetimingbufferfull"), true, false);
-    if (NS_SUCCEEDED(rv)) {
-      event->SetTrusted(true);
-      DispatchDOMEvent(nullptr, event, nullptr, nullptr);
-    }
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
+  // it bubbles, and it isn't cancelable
+  nsresult rv = event->InitEvent(NS_LITERAL_STRING("resourcetimingbufferfull"),
+                                 true, false);
+  if (NS_FAILED(rv)) {
+    return;
   }
+  event->SetTrusted(true);
+  DispatchDOMEvent(nullptr, event, nullptr, nullptr);
 }
 
 nsPerformanceNavigation*
 nsPerformance::Navigation()
 {
   if (!mNavigation) {
     mNavigation = new nsPerformanceNavigation(this);
   }
--- a/dom/base/nsXMLHttpRequest.cpp
+++ b/dom/base/nsXMLHttpRequest.cpp
@@ -1462,22 +1462,18 @@ nsXMLHttpRequest::GetLoadGroup() const
   }
 
   return nullptr;
 }
 
 nsresult
 nsXMLHttpRequest::CreateReadystatechangeEvent(nsIDOMEvent** aDOMEvent)
 {
-  nsresult rv = EventDispatcher::CreateEvent(this, nullptr, nullptr,
-                                             NS_LITERAL_STRING("Events"),
-                                             aDOMEvent);
-  if (NS_FAILED(rv)) {
-    return rv;
-  }
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
+  event.forget(aDOMEvent);
 
   (*aDOMEvent)->InitEvent(NS_LITERAL_STRING(READYSTATE_STR),
                           false, false);
 
   // We assume anyone who managed to call CreateReadystatechangeEvent is trusted
   (*aDOMEvent)->SetTrusted(true);
 
   return NS_OK;
--- a/dom/bluetooth/bluetooth1/BluetoothAdapter.cpp
+++ b/dom/bluetooth/bluetooth1/BluetoothAdapter.cpp
@@ -11,16 +11,17 @@
 #include "nsThreadUtils.h"
 
 #include "mozilla/dom/bluetooth/BluetoothTypes.h"
 #include "mozilla/dom/BluetoothAdapterBinding.h"
 #include "mozilla/dom/BluetoothDeviceEvent.h"
 #include "mozilla/dom/BluetoothDiscoveryStateChangedEvent.h"
 #include "mozilla/dom/BluetoothStatusChangedEvent.h"
 #include "mozilla/dom/ContentChild.h"
+#include "mozilla/dom/Event.h"
 #include "mozilla/dom/File.h"
 #include "mozilla/dom/ScriptSettings.h"
 #include "mozilla/LazyIdleThread.h"
 
 #include "BluetoothAdapter.h"
 #include "BluetoothDevice.h"
 #include "BluetoothReplyRunnable.h"
 #include "BluetoothService.h"
@@ -356,21 +357,19 @@ BluetoothAdapter::Notify(const Bluetooth
     init.mBubbles = false;
     init.mCancelable = false;
     init.mAddress = address;
     init.mStatus = status;
     nsRefPtr<BluetoothStatusChangedEvent> event =
       BluetoothStatusChangedEvent::Constructor(this, aData.name(), init);
     DispatchTrustedEvent(event);
   } else if (aData.name().EqualsLiteral(REQUEST_MEDIA_PLAYSTATUS_ID)) {
-    nsCOMPtr<nsIDOMEvent> event;
-    nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-    NS_ENSURE_SUCCESS_VOID(rv);
+    nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
 
-    rv = event->InitEvent(aData.name(), false, false);
+    nsresult rv = event->InitEvent(aData.name(), false, false);
     NS_ENSURE_SUCCESS_VOID(rv);
 
     DispatchTrustedEvent(event);
   } else {
 #ifdef DEBUG
     nsCString warningMsg;
     warningMsg.AssignLiteral("Not handling adapter signal: ");
     warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
--- a/dom/bluetooth/bluetooth2/BluetoothAdapter.cpp
+++ b/dom/bluetooth/bluetooth2/BluetoothAdapter.cpp
@@ -11,16 +11,17 @@
 #include "nsIDocument.h"
 #include "nsIPrincipal.h"
 #include "nsTArrayHelpers.h"
 
 #include "mozilla/dom/BluetoothAdapter2Binding.h"
 #include "mozilla/dom/BluetoothAttributeEvent.h"
 #include "mozilla/dom/BluetoothStatusChangedEvent.h"
 #include "mozilla/dom/ContentChild.h"
+#include "mozilla/dom/Event.h"
 #include "mozilla/dom/File.h"
 
 #include "mozilla/dom/bluetooth/BluetoothAdapter.h"
 #include "mozilla/dom/bluetooth/BluetoothClassOfDevice.h"
 #include "mozilla/dom/bluetooth/BluetoothDevice.h"
 #include "mozilla/dom/bluetooth/BluetoothDiscoveryHandle.h"
 #include "mozilla/dom/bluetooth/BluetoothGattServer.h"
 #include "mozilla/dom/bluetooth/BluetoothPairingListener.h"
@@ -1191,21 +1192,19 @@ BluetoothAdapter::DispatchDeviceEvent(co
   nsRefPtr<BluetoothDeviceEvent> event =
     BluetoothDeviceEvent::Constructor(this, aType, aInit);
   DispatchTrustedEvent(event);
 }
 
 void
 BluetoothAdapter::DispatchEmptyEvent(const nsAString& aType)
 {
-  nsCOMPtr<nsIDOMEvent> event;
-  nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  NS_ENSURE_SUCCESS_VOID(rv);
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
 
-  rv = event->InitEvent(aType, false, false);
+  nsresult rv = event->InitEvent(aType, false, false);
   NS_ENSURE_SUCCESS_VOID(rv);
 
   DispatchTrustedEvent(event);
 }
 
 already_AddRefed<DOMRequest>
 BluetoothAdapter::Connect(BluetoothDevice& aDevice,
                           const Optional<short unsigned int>& aServiceUuid,
--- a/dom/bluetooth/bluetooth2/BluetoothGatt.cpp
+++ b/dom/bluetooth/bluetooth2/BluetoothGatt.cpp
@@ -7,16 +7,17 @@
 #include "BluetoothReplyRunnable.h"
 #include "BluetoothService.h"
 #include "BluetoothUtils.h"
 #include "mozilla/dom/bluetooth/BluetoothCommon.h"
 #include "mozilla/dom/bluetooth/BluetoothGatt.h"
 #include "mozilla/dom/bluetooth/BluetoothTypes.h"
 #include "mozilla/dom/BluetoothGattBinding.h"
 #include "mozilla/dom/BluetoothGattCharacteristicEvent.h"
+#include "mozilla/dom/Event.h"
 #include "mozilla/dom/Promise.h"
 #include "nsServiceManagerUtils.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 USING_BLUETOOTH_NAMESPACE
 
@@ -235,23 +236,22 @@ BluetoothGatt::DiscoverServices(ErrorRes
 
 void
 BluetoothGatt::UpdateConnectionState(BluetoothConnectionState aState)
 {
   BT_LOGR("GATT connection state changes to: %d", int(aState));
   mConnectionState = aState;
 
   // Dispatch connectionstatechanged event to application
-  nsCOMPtr<nsIDOMEvent> event;
-  nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  NS_ENSURE_SUCCESS_VOID(rv);
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
 
-  rv = event->InitEvent(NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID),
-                        false,
-                        false);
+  nsresult rv =
+    event->InitEvent(NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID),
+                     false,
+                     false);
   NS_ENSURE_SUCCESS_VOID(rv);
 
   DispatchTrustedEvent(event);
 }
 
 void
 BluetoothGatt::HandleServicesDiscovered(const BluetoothValue& aValue)
 {
--- a/dom/browser-element/BrowserElementParent.cpp
+++ b/dom/browser-element/BrowserElementParent.cpp
@@ -97,41 +97,36 @@ DispatchCustomDOMEvent(Element* aFrameEl
 {
   NS_ENSURE_TRUE(aFrameElement, false);
   nsIPresShell *shell = aFrameElement->OwnerDoc()->GetShell();
   nsRefPtr<nsPresContext> presContext;
   if (shell) {
     presContext = shell->GetPresContext();
   }
 
-  nsCOMPtr<nsIDOMEvent> domEvent;
-  EventDispatcher::CreateEvent(aFrameElement, presContext, nullptr,
-                               NS_LITERAL_STRING("customevent"),
-                               getter_AddRefs(domEvent));
-  NS_ENSURE_TRUE(domEvent, false);
+  nsRefPtr<CustomEvent> event =
+    NS_NewDOMCustomEvent(aFrameElement, presContext, nullptr);
 
-  nsCOMPtr<nsIDOMCustomEvent> customEvent = do_QueryInterface(domEvent);
-  NS_ENSURE_TRUE(customEvent, false);
   ErrorResult res;
-  CustomEvent* event = static_cast<CustomEvent*>(customEvent.get());
   event->InitCustomEvent(cx,
                          aEventName,
                          /* bubbles = */ true,
                          /* cancelable = */ true,
                          aDetailValue,
                          res);
   if (res.Failed()) {
     return false;
   }
-  customEvent->SetTrusted(true);
+  event->SetTrusted(true);
   // Dispatch the event.
   *aStatus = nsEventStatus_eConsumeNoDefault;
   nsresult rv =
     EventDispatcher::DispatchDOMEvent(aFrameElement, nullptr,
-                                      domEvent, presContext, aStatus);
+                                      static_cast<Event*>(event),
+                                      presContext, aStatus);
   return NS_SUCCEEDED(rv);
 }
 
 } // namespace
 
 namespace mozilla {
 
 /**
--- a/dom/events/AnimationEvent.cpp
+++ b/dom/events/AnimationEvent.cpp
@@ -83,19 +83,17 @@ AnimationEvent::GetPseudoElement(nsAStri
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMAnimationEvent(nsIDOMEvent** aInstancePtrResult,
-                        EventTarget* aOwner,
+already_AddRefed<AnimationEvent>
+NS_NewDOMAnimationEvent(EventTarget* aOwner,
                         nsPresContext* aPresContext,
                         InternalAnimationEvent* aEvent)
 {
-  AnimationEvent* it = new AnimationEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<AnimationEvent> it =
+    new AnimationEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/AnimationEvent.h
+++ b/dom/events/AnimationEvent.h
@@ -47,9 +47,14 @@ public:
 
 protected:
   ~AnimationEvent() {}
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::AnimationEvent>
+NS_NewDOMAnimationEvent(mozilla::dom::EventTarget* aOwner,
+                        nsPresContext* aPresContext,
+                        mozilla::InternalAnimationEvent* aEvent);
+
 #endif // mozilla_dom_AnimationEvent_h_
--- a/dom/events/AsyncEventDispatcher.cpp
+++ b/dom/events/AsyncEventDispatcher.cpp
@@ -21,35 +21,36 @@ using namespace dom;
  ******************************************************************************/
 
 AsyncEventDispatcher::AsyncEventDispatcher(EventTarget* aTarget,
                                            WidgetEvent& aEvent)
   : mTarget(aTarget)
   , mOnlyChromeDispatch(false)
 {
   MOZ_ASSERT(mTarget);
-  EventDispatcher::CreateEvent(aTarget, nullptr, &aEvent, EmptyString(),
-                               getter_AddRefs(mEvent));
+  nsRefPtr<Event> event =
+    EventDispatcher::CreateEvent(aTarget, nullptr, &aEvent, EmptyString());
+  mEvent = do_QueryInterface(event);
   NS_ASSERTION(mEvent, "Should never fail to create an event");
   mEvent->DuplicatePrivateData();
   mEvent->SetTrusted(aEvent.mFlags.mIsTrusted);
 }
 
 NS_IMETHODIMP
 AsyncEventDispatcher::Run()
 {
-  nsCOMPtr<nsIDOMEvent> event = mEvent;
+  nsRefPtr<Event> event = mEvent ? mEvent->InternalDOMEvent() : nullptr;
   if (!event) {
-    NS_NewDOMEvent(getter_AddRefs(event), mTarget, nullptr, nullptr);
+    event = NS_NewDOMEvent(mTarget, nullptr, nullptr);
     nsresult rv = event->InitEvent(mEventType, mBubbles, false);
     NS_ENSURE_SUCCESS(rv, rv);
     event->SetTrusted(true);
   }
   if (mOnlyChromeDispatch) {
-    MOZ_ASSERT(event->InternalDOMEvent()->IsTrusted());
+    MOZ_ASSERT(event->IsTrusted());
     event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
   }
   bool dummy;
   mTarget->DispatchEvent(event, &dummy);
   return NS_OK;
 }
 
 nsresult
--- a/dom/events/BeforeAfterKeyboardEvent.cpp
+++ b/dom/events/BeforeAfterKeyboardEvent.cpp
@@ -73,21 +73,17 @@ BeforeAfterKeyboardEvent::GetEmbeddedCan
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMBeforeAfterKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
-                                  EventTarget* aOwner,
+already_AddRefed<BeforeAfterKeyboardEvent>
+NS_NewDOMBeforeAfterKeyboardEvent(EventTarget* aOwner,
                                   nsPresContext* aPresContext,
                                   InternalBeforeAfterKeyboardEvent* aEvent)
 {
-  BeforeAfterKeyboardEvent* it =
+  nsRefPtr<BeforeAfterKeyboardEvent> it =
     new BeforeAfterKeyboardEvent(aOwner, aPresContext, aEvent);
-
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  return it.forget();
 }
--- a/dom/events/BeforeAfterKeyboardEvent.h
+++ b/dom/events/BeforeAfterKeyboardEvent.h
@@ -38,9 +38,14 @@ public:
   // This function returns a boolean value when event typs is either
   // "mozbrowserafterkeydown" or "mozbrowserafterkeyup".
   Nullable<bool> GetEmbeddedCancelled();
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::BeforeAfterKeyboardEvent>
+NS_NewDOMBeforeAfterKeyboardEvent(mozilla::dom::EventTarget* aOwner,
+                                  nsPresContext* aPresContext,
+                                  mozilla::InternalBeforeAfterKeyboardEvent* aEvent);
+
 #endif // mozilla_dom_BeforeAfterKeyboardEvent_h_
--- a/dom/events/BeforeUnloadEvent.cpp
+++ b/dom/events/BeforeUnloadEvent.cpp
@@ -31,19 +31,17 @@ BeforeUnloadEvent::GetReturnValue(nsAStr
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMBeforeUnloadEvent(nsIDOMEvent** aInstancePtrResult,
-                           EventTarget* aOwner,
+already_AddRefed<BeforeUnloadEvent>
+NS_NewDOMBeforeUnloadEvent(EventTarget* aOwner,
                            nsPresContext* aPresContext,
                            WidgetEvent* aEvent) 
 {
-  BeforeUnloadEvent* it = new BeforeUnloadEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<BeforeUnloadEvent> it =
+    new BeforeUnloadEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/BeforeUnloadEvent.h
+++ b/dom/events/BeforeUnloadEvent.h
@@ -42,9 +42,14 @@ protected:
   ~BeforeUnloadEvent() {}
 
   nsString mText;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::BeforeUnloadEvent>
+NS_NewDOMBeforeUnloadEvent(mozilla::dom::EventTarget* aOwner,
+                           nsPresContext* aPresContext,
+                           mozilla::WidgetEvent* aEvent);
+
 #endif // mozilla_dom_BeforeUnloadEvent_h_
--- a/dom/events/ClipboardEvent.cpp
+++ b/dom/events/ClipboardEvent.cpp
@@ -118,19 +118,17 @@ ClipboardEvent::GetClipboardData()
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMClipboardEvent(nsIDOMEvent** aInstancePtrResult,
-                        EventTarget* aOwner,
+already_AddRefed<ClipboardEvent>
+NS_NewDOMClipboardEvent(EventTarget* aOwner,
                         nsPresContext* aPresContext,
                         InternalClipboardEvent* aEvent)
 {
-  ClipboardEvent* it = new ClipboardEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<ClipboardEvent> it =
+    new ClipboardEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/ClipboardEvent.h
+++ b/dom/events/ClipboardEvent.h
@@ -51,9 +51,14 @@ public:
 
 protected:
   ~ClipboardEvent() {}
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::ClipboardEvent>
+NS_NewDOMClipboardEvent(mozilla::dom::EventTarget* aOwner,
+                        nsPresContext* aPresContext,
+                        mozilla::InternalClipboardEvent* aEvent);
+
 #endif // mozilla_dom_ClipboardEvent_h_
--- a/dom/events/CommandEvent.cpp
+++ b/dom/events/CommandEvent.cpp
@@ -59,19 +59,17 @@ CommandEvent::InitCommandEvent(const nsA
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMCommandEvent(nsIDOMEvent** aInstancePtrResult,
-                      EventTarget* aOwner,
+already_AddRefed<CommandEvent>
+NS_NewDOMCommandEvent(EventTarget* aOwner,
                       nsPresContext* aPresContext,
                       WidgetCommandEvent* aEvent)
 {
-  CommandEvent* it = new CommandEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<CommandEvent> it =
+    new CommandEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/CommandEvent.h
+++ b/dom/events/CommandEvent.h
@@ -46,9 +46,14 @@ public:
 
 protected:
   ~CommandEvent() {}
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::CommandEvent>
+NS_NewDOMCommandEvent(mozilla::dom::EventTarget* aOwner,
+                      nsPresContext* aPresContext,
+                      mozilla::WidgetCommandEvent* aEvent);
+
 #endif // mozilla_dom_CommandEvent_h_
--- a/dom/events/CompositionEvent.cpp
+++ b/dom/events/CompositionEvent.cpp
@@ -75,17 +75,17 @@ CompositionEvent::InitCompositionEvent(c
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMCompositionEvent(nsIDOMEvent** aInstancePtrResult,
-                          EventTarget* aOwner,
+already_AddRefed<CompositionEvent>
+NS_NewDOMCompositionEvent(EventTarget* aOwner,
                           nsPresContext* aPresContext,
                           WidgetCompositionEvent* aEvent)
 {
-  CompositionEvent* event = new CompositionEvent(aOwner, aPresContext, aEvent);
-  return CallQueryInterface(event, aInstancePtrResult);
+  nsRefPtr<CompositionEvent> event =
+    new CompositionEvent(aOwner, aPresContext, aEvent);
+  return event.forget();
 }
--- a/dom/events/CompositionEvent.h
+++ b/dom/events/CompositionEvent.h
@@ -49,9 +49,14 @@ protected:
 
   nsString mData;
   nsString mLocale;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::CompositionEvent>
+NS_NewDOMCompositionEvent(mozilla::dom::EventTarget* aOwner,
+                          nsPresContext* aPresContext,
+                          mozilla::WidgetCompositionEvent* aEvent);
+
 #endif // mozilla_dom_CompositionEvent_h_
--- a/dom/events/CustomEvent.cpp
+++ b/dom/events/CustomEvent.cpp
@@ -106,19 +106,17 @@ CustomEvent::GetDetail(JSContext* aCx,
   if (!mDetail) {
     aRetval.setNull();
     return;
   }
 
   VariantToJsval(aCx, mDetail, aRetval);
 }
 
-nsresult
-NS_NewDOMCustomEvent(nsIDOMEvent** aInstancePtrResult,
-                     mozilla::dom::EventTarget* aOwner,
+already_AddRefed<CustomEvent>
+NS_NewDOMCustomEvent(EventTarget* aOwner,
                      nsPresContext* aPresContext,
                      mozilla::WidgetEvent* aEvent)
 {
-  CustomEvent* it = new CustomEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<CustomEvent> it =
+    new CustomEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/CustomEvent.h
+++ b/dom/events/CustomEvent.h
@@ -53,9 +53,14 @@ public:
                   bool aCancelable,
                   JS::Handle<JS::Value> aDetail,
                   ErrorResult& aRv);
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::CustomEvent>
+NS_NewDOMCustomEvent(mozilla::dom::EventTarget* aOwner,
+                     nsPresContext* aPresContext,
+                     mozilla::WidgetEvent* aEvent);
+
 #endif // CustomEvent_h__
--- a/dom/events/DOMEventTargetHelper.cpp
+++ b/dom/events/DOMEventTargetHelper.cpp
@@ -257,18 +257,17 @@ DOMEventTargetHelper::DispatchEvent(nsID
 
   *aRetVal = (status != nsEventStatus_eConsumeNoDefault);
   return rv;
 }
 
 nsresult
 DOMEventTargetHelper::DispatchTrustedEvent(const nsAString& aEventName)
 {
-  nsCOMPtr<nsIDOMEvent> event;
-  NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
   nsresult rv = event->InitEvent(aEventName, false, false);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return DispatchTrustedEvent(event);
 }
 
 nsresult
 DOMEventTargetHelper::DispatchTrustedEvent(nsIDOMEvent* event)
--- a/dom/events/DataContainerEvent.cpp
+++ b/dom/events/DataContainerEvent.cpp
@@ -82,20 +82,18 @@ DataContainerEvent::SetData(JSContext* a
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMDataContainerEvent(nsIDOMEvent** aInstancePtrResult,
-                            EventTarget* aOwner,
+already_AddRefed<DataContainerEvent>
+NS_NewDOMDataContainerEvent(EventTarget* aOwner,
                             nsPresContext* aPresContext,
                             WidgetEvent* aEvent)
 {
-  DataContainerEvent* it = new DataContainerEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<DataContainerEvent> it =
+    new DataContainerEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
 
--- a/dom/events/DataContainerEvent.h
+++ b/dom/events/DataContainerEvent.h
@@ -52,9 +52,14 @@ protected:
 
 private:
   nsInterfaceHashtable<nsStringHashKey, nsIVariant> mData;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::DataContainerEvent>
+NS_NewDOMDataContainerEvent(mozilla::dom::EventTarget* aOwner,
+                            nsPresContext* aPresContext,
+                            mozilla::WidgetEvent* aEvent);
+
 #endif // mozilla_dom_DataContainerEvent_h_
--- a/dom/events/DeviceMotionEvent.cpp
+++ b/dom/events/DeviceMotionEvent.cpp
@@ -140,21 +140,17 @@ DeviceRotationRate::~DeviceRotationRate(
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMDeviceMotionEvent(nsIDOMEvent** aInstancePtrResult,
-                           EventTarget* aOwner,
+already_AddRefed<DeviceMotionEvent>
+NS_NewDOMDeviceMotionEvent(EventTarget* aOwner,
                            nsPresContext* aPresContext,
                            WidgetEvent* aEvent) 
 {
-  NS_ENSURE_ARG_POINTER(aInstancePtrResult);
-
-  DeviceMotionEvent* it = new DeviceMotionEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<DeviceMotionEvent> it =
+    new DeviceMotionEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/DeviceMotionEvent.h
+++ b/dom/events/DeviceMotionEvent.h
@@ -144,9 +144,14 @@ protected:
   nsRefPtr<DeviceAcceleration> mAccelerationIncludingGravity;
   nsRefPtr<DeviceRotationRate> mRotationRate;
   Nullable<double> mInterval;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::DeviceMotionEvent>
+NS_NewDOMDeviceMotionEvent(mozilla::dom::EventTarget* aOwner,
+                           nsPresContext* aPresContext,
+                           mozilla::WidgetEvent* aEvent);
+
 #endif // mozilla_dom_DeviceMotionEvent_h_
--- a/dom/events/DragEvent.cpp
+++ b/dom/events/DragEvent.cpp
@@ -154,17 +154,17 @@ DragEvent::Constructor(const GlobalObjec
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMDragEvent(nsIDOMEvent** aInstancePtrResult,
-                   EventTarget* aOwner,
+already_AddRefed<DragEvent>
+NS_NewDOMDragEvent(EventTarget* aOwner,
                    nsPresContext* aPresContext,
                    WidgetDragEvent* aEvent) 
 {
-  DragEvent* event = new DragEvent(aOwner, aPresContext, aEvent);
-  return CallQueryInterface(event, aInstancePtrResult);
+  nsRefPtr<DragEvent> event =
+    new DragEvent(aOwner, aPresContext, aEvent);
+  return event.forget();
 }
--- a/dom/events/DragEvent.h
+++ b/dom/events/DragEvent.h
@@ -56,9 +56,14 @@ public:
 
 protected:
   ~DragEvent() {}
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::DragEvent>
+NS_NewDOMDragEvent(mozilla::dom::EventTarget* aOwner,
+                   nsPresContext* aPresContext,
+                   mozilla::WidgetDragEvent* aEvent);
+
 #endif // mozilla_dom_DragEvent_h_
--- a/dom/events/Event.cpp
+++ b/dom/events/Event.cpp
@@ -1252,19 +1252,16 @@ Event::GetShadowRelatedTarget(nsIContent
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMEvent(nsIDOMEvent** aInstancePtrResult,
-               EventTarget* aOwner,
+already_AddRefed<Event>
+NS_NewDOMEvent(EventTarget* aOwner,
                nsPresContext* aPresContext,
                WidgetEvent* aEvent) 
 {
-  Event* it = new Event(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<Event> it = new Event(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/Event.h
+++ b/dom/events/Event.h
@@ -335,9 +335,14 @@ ToSupports(mozilla::dom::Event* e)
 }
 
 inline nsISupports*
 ToCanonicalSupports(mozilla::dom::Event* e)
 {
   return static_cast<nsIDOMEvent*>(e);
 }
 
+already_AddRefed<mozilla::dom::Event>
+NS_NewDOMEvent(mozilla::dom::EventTarget* aOwner,
+               nsPresContext* aPresContext,
+               mozilla::WidgetEvent* aEvent);
+
 #endif // mozilla_dom_Event_h_
--- a/dom/events/EventDispatcher.cpp
+++ b/dom/events/EventDispatcher.cpp
@@ -10,23 +10,38 @@
 #include <new>
 #include "nsIContent.h"
 #include "nsIDocument.h"
 #include "nsINode.h"
 #include "nsPIDOMWindow.h"
 #include "GeckoProfiler.h"
 #include "mozilla/ContentEvents.h"
 #include "mozilla/dom/CloseEvent.h"
+#include "mozilla/dom/CustomEvent.h"
 #include "mozilla/dom/DeviceOrientationEvent.h"
 #include "mozilla/dom/EventTarget.h"
+#include "mozilla/dom/FocusEvent.h"
 #include "mozilla/dom/HashChangeEvent.h"
+#include "mozilla/dom/InputEvent.h"
+#include "mozilla/dom/MessageEvent.h"
+#include "mozilla/dom/MouseScrollEvent.h"
+#include "mozilla/dom/MutationEvent.h"
+#include "mozilla/dom/NotifyPaintEvent.h"
 #include "mozilla/dom/PageTransitionEvent.h"
+#include "mozilla/dom/PointerEvent.h"
 #include "mozilla/dom/PopStateEvent.h"
+#include "mozilla/dom/ScrollAreaEvent.h"
+#include "mozilla/dom/SimpleGestureEvent.h"
 #include "mozilla/dom/StorageEvent.h"
+#include "mozilla/dom/SVGZoomEvent.h"
+#include "mozilla/dom/TimeEvent.h"
 #include "mozilla/dom/TouchEvent.h"
+#include "mozilla/dom/TransitionEvent.h"
+#include "mozilla/dom/WheelEvent.h"
+#include "mozilla/dom/XULCommandEvent.h"
 #include "mozilla/EventDispatcher.h"
 #include "mozilla/EventListenerManager.h"
 #include "mozilla/InternalMutationEvent.h"
 #include "mozilla/ipc/MessageChannel.h"
 #include "mozilla/MiscEvents.h"
 #include "mozilla/MouseEvents.h"
 #include "mozilla/TextEvents.h"
 #include "mozilla/TouchEvents.h"
@@ -700,198 +715,177 @@ EventDispatcher::DispatchDOMEvent(nsISup
                                      aDOMEvent, aEventStatus);
   } else if (aEvent) {
     return EventDispatcher::Dispatch(aTarget, aPresContext, aEvent,
                                      aDOMEvent, aEventStatus);
   }
   return NS_ERROR_ILLEGAL_VALUE;
 }
 
-/* static */ nsresult
+/* static */ already_AddRefed<dom::Event>
 EventDispatcher::CreateEvent(EventTarget* aOwner,
                              nsPresContext* aPresContext,
                              WidgetEvent* aEvent,
-                             const nsAString& aEventType,
-                             nsIDOMEvent** aDOMEvent)
+                             const nsAString& aEventType)
 {
-  *aDOMEvent = nullptr;
-
   if (aEvent) {
     switch(aEvent->mClass) {
     case eMutationEventClass:
-      return NS_NewDOMMutationEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMMutationEvent(aOwner, aPresContext,
                                     aEvent->AsMutationEvent());
     case eGUIEventClass:
     case eScrollPortEventClass:
     case eUIEventClass:
-      return NS_NewDOMUIEvent(aDOMEvent, aOwner, aPresContext,
-                              aEvent->AsGUIEvent());
+      return NS_NewDOMUIEvent(aOwner, aPresContext, aEvent->AsGUIEvent());
     case eScrollAreaEventClass:
-      return NS_NewDOMScrollAreaEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMScrollAreaEvent(aOwner, aPresContext,
                                       aEvent->AsScrollAreaEvent());
     case eKeyboardEventClass:
-      return NS_NewDOMKeyboardEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMKeyboardEvent(aOwner, aPresContext,
                                     aEvent->AsKeyboardEvent());
     case eBeforeAfterKeyboardEventClass:
-      return NS_NewDOMBeforeAfterKeyboardEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMBeforeAfterKeyboardEvent(aOwner, aPresContext,
                                                aEvent->AsBeforeAfterKeyboardEvent());
     case eCompositionEventClass:
-      return NS_NewDOMCompositionEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMCompositionEvent(aOwner, aPresContext,
                                        aEvent->AsCompositionEvent());
     case eMouseEventClass:
-      return NS_NewDOMMouseEvent(aDOMEvent, aOwner, aPresContext,
-                                 aEvent->AsMouseEvent());
+      return NS_NewDOMMouseEvent(aOwner, aPresContext, aEvent->AsMouseEvent());
     case eFocusEventClass:
-      return NS_NewDOMFocusEvent(aDOMEvent, aOwner, aPresContext,
-                                 aEvent->AsFocusEvent());
+      return NS_NewDOMFocusEvent(aOwner, aPresContext, aEvent->AsFocusEvent());
     case eMouseScrollEventClass:
-      return NS_NewDOMMouseScrollEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMMouseScrollEvent(aOwner, aPresContext,
                                        aEvent->AsMouseScrollEvent());
     case eWheelEventClass:
-      return NS_NewDOMWheelEvent(aDOMEvent, aOwner, aPresContext,
-                                 aEvent->AsWheelEvent());
+      return NS_NewDOMWheelEvent(aOwner, aPresContext, aEvent->AsWheelEvent());
     case eEditorInputEventClass:
-      return NS_NewDOMInputEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMInputEvent(aOwner, aPresContext,
                                  aEvent->AsEditorInputEvent());
     case eDragEventClass:
-      return NS_NewDOMDragEvent(aDOMEvent, aOwner, aPresContext,
-                                aEvent->AsDragEvent());
+      return NS_NewDOMDragEvent(aOwner, aPresContext, aEvent->AsDragEvent());
     case eClipboardEventClass:
-      return NS_NewDOMClipboardEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMClipboardEvent(aOwner, aPresContext,
                                      aEvent->AsClipboardEvent());
     case eSVGZoomEventClass:
-      return NS_NewDOMSVGZoomEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMSVGZoomEvent(aOwner, aPresContext,
                                    aEvent->AsSVGZoomEvent());
     case eSMILTimeEventClass:
-      return NS_NewDOMTimeEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMTimeEvent(aOwner, aPresContext,
                                 aEvent->AsSMILTimeEvent());
     case eCommandEventClass:
-      return NS_NewDOMCommandEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMCommandEvent(aOwner, aPresContext,
                                    aEvent->AsCommandEvent());
     case eSimpleGestureEventClass:
-      return NS_NewDOMSimpleGestureEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMSimpleGestureEvent(aOwner, aPresContext,
                                          aEvent->AsSimpleGestureEvent());
     case ePointerEventClass:
-      return NS_NewDOMPointerEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMPointerEvent(aOwner, aPresContext,
                                    aEvent->AsPointerEvent());
     case eTouchEventClass:
-      return NS_NewDOMTouchEvent(aDOMEvent, aOwner, aPresContext,
-                                 aEvent->AsTouchEvent());
+      return NS_NewDOMTouchEvent(aOwner, aPresContext, aEvent->AsTouchEvent());
     case eTransitionEventClass:
-      return NS_NewDOMTransitionEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMTransitionEvent(aOwner, aPresContext,
                                       aEvent->AsTransitionEvent());
     case eAnimationEventClass:
-      return NS_NewDOMAnimationEvent(aDOMEvent, aOwner, aPresContext,
+      return NS_NewDOMAnimationEvent(aOwner, aPresContext,
                                      aEvent->AsAnimationEvent());
     default:
       // For all other types of events, create a vanilla event object.
-      return NS_NewDOMEvent(aDOMEvent, aOwner, aPresContext, aEvent);
+      return NS_NewDOMEvent(aOwner, aPresContext, aEvent);
     }
   }
 
   // And if we didn't get an event, check the type argument.
 
   if (aEventType.LowerCaseEqualsLiteral("mouseevent") ||
       aEventType.LowerCaseEqualsLiteral("mouseevents") ||
       aEventType.LowerCaseEqualsLiteral("popupevents"))
-    return NS_NewDOMMouseEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMMouseEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("mousescrollevents"))
-    return NS_NewDOMMouseScrollEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMMouseScrollEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("dragevent") ||
       aEventType.LowerCaseEqualsLiteral("dragevents"))
-    return NS_NewDOMDragEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMDragEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("keyboardevent") ||
       aEventType.LowerCaseEqualsLiteral("keyevents"))
-    return NS_NewDOMKeyboardEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMKeyboardEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("compositionevent") ||
       aEventType.LowerCaseEqualsLiteral("textevent") ||
       aEventType.LowerCaseEqualsLiteral("textevents")) {
-    return NS_NewDOMCompositionEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMCompositionEvent(aOwner, aPresContext, nullptr);
   }
   if (aEventType.LowerCaseEqualsLiteral("mutationevent") ||
         aEventType.LowerCaseEqualsLiteral("mutationevents"))
-    return NS_NewDOMMutationEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMMutationEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("deviceorientationevent")) {
     DeviceOrientationEventInit init;
-    nsRefPtr<DeviceOrientationEvent> event =
-      DeviceOrientationEvent::Constructor(aOwner, EmptyString(), init);
-    event.forget(aDOMEvent);
-    return NS_OK;
+    return DeviceOrientationEvent::Constructor(aOwner, EmptyString(), init);
   }
   if (aEventType.LowerCaseEqualsLiteral("devicemotionevent"))
-    return NS_NewDOMDeviceMotionEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMDeviceMotionEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("uievent") ||
       aEventType.LowerCaseEqualsLiteral("uievents"))
-    return NS_NewDOMUIEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMUIEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("event") ||
       aEventType.LowerCaseEqualsLiteral("events") ||
       aEventType.LowerCaseEqualsLiteral("htmlevents") ||
       aEventType.LowerCaseEqualsLiteral("svgevent") ||
       aEventType.LowerCaseEqualsLiteral("svgevents"))
-    return NS_NewDOMEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("svgzoomevent") ||
       aEventType.LowerCaseEqualsLiteral("svgzoomevents"))
-    return NS_NewDOMSVGZoomEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMSVGZoomEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("timeevent") ||
       aEventType.LowerCaseEqualsLiteral("timeevents"))
-    return NS_NewDOMTimeEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMTimeEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("xulcommandevent") ||
       aEventType.LowerCaseEqualsLiteral("xulcommandevents"))
-    return NS_NewDOMXULCommandEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMXULCommandEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("commandevent") ||
       aEventType.LowerCaseEqualsLiteral("commandevents"))
-    return NS_NewDOMCommandEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMCommandEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("datacontainerevent") ||
       aEventType.LowerCaseEqualsLiteral("datacontainerevents"))
-    return NS_NewDOMDataContainerEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMDataContainerEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("messageevent"))
-    return NS_NewDOMMessageEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMMessageEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("notifypaintevent"))
-    return NS_NewDOMNotifyPaintEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMNotifyPaintEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("simplegestureevent"))
-    return NS_NewDOMSimpleGestureEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMSimpleGestureEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("beforeunloadevent"))
-    return NS_NewDOMBeforeUnloadEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMBeforeUnloadEvent(aOwner, aPresContext, nullptr);
   // XXXkhuey this is broken
   if (aEventType.LowerCaseEqualsLiteral("pagetransition")) {
     PageTransitionEventInit init;
-    nsRefPtr<PageTransitionEvent> event =
-      PageTransitionEvent::Constructor(aOwner, EmptyString(), init);
-    event.forget(aDOMEvent);
-    return NS_OK;
+    return PageTransitionEvent::Constructor(aOwner, EmptyString(), init);
   }
   if (aEventType.LowerCaseEqualsLiteral("scrollareaevent"))
-    return NS_NewDOMScrollAreaEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMScrollAreaEvent(aOwner, aPresContext, nullptr);
   // XXXkhuey Chrome supports popstateevent here, even though it provides no
   // initPopStateEvent method.  This is nuts ... but copying it is unlikely to
   // break the web.
   if (aEventType.LowerCaseEqualsLiteral("popstateevent")) {
     AutoJSContext cx;
     RootedDictionary<PopStateEventInit> init(cx);
-    nsRefPtr<PopStateEvent> event =
-      PopStateEvent::Constructor(aOwner, EmptyString(), init);
-    event.forget(aDOMEvent);
-    return NS_OK;
+    return PopStateEvent::Constructor(aOwner, EmptyString(), init);
   }
   if (aEventType.LowerCaseEqualsLiteral("touchevent") &&
       TouchEvent::PrefEnabled())
-    return NS_NewDOMTouchEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMTouchEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("hashchangeevent")) {
     HashChangeEventInit init;
-    nsRefPtr<HashChangeEvent> event =
-      HashChangeEvent::Constructor(aOwner, EmptyString(), init);
-    event.forget(aDOMEvent);
-    return NS_OK;
+    return HashChangeEvent::Constructor(aOwner, EmptyString(), init);
   }
   if (aEventType.LowerCaseEqualsLiteral("customevent"))
-    return NS_NewDOMCustomEvent(aDOMEvent, aOwner, aPresContext, nullptr);
+    return NS_NewDOMCustomEvent(aOwner, aPresContext, nullptr);
   if (aEventType.LowerCaseEqualsLiteral("storageevent")) {
-    return NS_NewDOMStorageEvent(aDOMEvent, aOwner);
+    return NS_NewDOMStorageEvent(aOwner);
   }
 
 
   // NEW EVENT TYPES SHOULD NOT BE ADDED HERE; THEY SHOULD USE ONLY EVENT
   // CONSTRUCTORS
 
-  return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
+  return nullptr;
 }
 
 } // namespace mozilla
--- a/dom/events/EventDispatcher.h
+++ b/dom/events/EventDispatcher.h
@@ -18,16 +18,17 @@
 class nsIContent;
 class nsIDOMEvent;
 class nsPresContext;
 
 template<class E> class nsCOMArray;
 
 namespace mozilla {
 namespace dom {
+class Event;
 class EventTarget;
 } // namespace dom
 
 /**
  * About event dispatching:
  * When either EventDispatcher::Dispatch or
  * EventDispatcher::DispatchDOMEvent is called an event target chain is
  * created. EventDispatcher creates the chain by calling PreHandleEvent 
@@ -267,23 +268,22 @@ public:
    */
   static nsresult DispatchDOMEvent(nsISupports* aTarget,
                                    WidgetEvent* aEvent,
                                    nsIDOMEvent* aDOMEvent,
                                    nsPresContext* aPresContext,
                                    nsEventStatus* aEventStatus);
 
   /**
-   * Creates a DOM Event.
+   * Creates a DOM Event.  Returns null if the event type is unsupported.
    */
-  static nsresult CreateEvent(dom::EventTarget* aOwner,
-                              nsPresContext* aPresContext,
-                              WidgetEvent* aEvent,
-                              const nsAString& aEventType,
-                              nsIDOMEvent** aDOMEvent);
+  static already_AddRefed<dom::Event> CreateEvent(dom::EventTarget* aOwner,
+                                                  nsPresContext* aPresContext,
+                                                  WidgetEvent* aEvent,
+                                                  const nsAString& aEventType);
 
   /**
    * Called at shutting down.
    */
   static void Shutdown();
 };
 
 } // namespace mozilla
--- a/dom/events/EventListenerManager.cpp
+++ b/dom/events/EventListenerManager.cpp
@@ -1104,18 +1104,20 @@ EventListenerManager::HandleEventInterna
       hasListener = true;
       if (listener->IsListening(aEvent) &&
           (aEvent->mFlags.mIsTrusted ||
            listener->mFlags.mAllowUntrustedEvents)) {
         if (!*aDOMEvent) {
           // This is tiny bit slow, but happens only once per event.
           nsCOMPtr<EventTarget> et =
             do_QueryInterface(aEvent->originalTarget);
-          EventDispatcher::CreateEvent(et, aPresContext,
-                                       aEvent, EmptyString(), aDOMEvent);
+          nsRefPtr<Event> event = EventDispatcher::CreateEvent(et, aPresContext,
+                                                               aEvent,
+                                                               EmptyString());
+          event.forget(aDOMEvent);
         }
         if (*aDOMEvent) {
           if (!aEvent->currentTarget) {
             aEvent->currentTarget = aCurrentTarget->GetTargetForDOMEvent();
             if (!aEvent->currentTarget) {
               break;
             }
           }
--- a/dom/events/EventStateManager.cpp
+++ b/dom/events/EventStateManager.cpp
@@ -11,16 +11,17 @@
 #include "mozilla/IMEStateManager.h"
 #include "mozilla/MiscEvents.h"
 #include "mozilla/MathAlgorithms.h"
 #include "mozilla/MouseEvents.h"
 #include "mozilla/TextComposition.h"
 #include "mozilla/TextEvents.h"
 #include "mozilla/TouchEvents.h"
 #include "mozilla/dom/ContentChild.h"
+#include "mozilla/dom/DragEvent.h"
 #include "mozilla/dom/Event.h"
 #include "mozilla/dom/TabParent.h"
 #include "mozilla/dom/UIEvent.h"
 
 #include "ContentEventHandler.h"
 #include "IMEContentObserver.h"
 #include "WheelHandlingHelper.h"
 
@@ -49,17 +50,16 @@
 #include "nsIContentViewer.h"
 #include "nsFrameManager.h"
 
 #include "nsIDOMXULElement.h"
 #include "nsIDOMKeyEvent.h"
 #include "nsIObserverService.h"
 #include "nsIDocShell.h"
 #include "nsIDOMWheelEvent.h"
-#include "nsIDOMDragEvent.h"
 #include "nsIDOMUIEvent.h"
 #include "nsIMozBrowserFrame.h"
 
 #include "nsSubDocumentFrame.h"
 #include "nsLayoutUtils.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsUnicharUtils.h"
 #include "nsContentUtils.h"
@@ -1876,32 +1876,26 @@ EventStateManager::DoDefaultDragStart(ns
   nsCOMPtr<nsISupportsArray> transArray =
     aDataTransfer->GetTransferables(dragTarget->AsDOMNode());
   if (!transArray)
     return false;
 
   // XXXndeakin don't really want to create a new drag DOM event
   // here, but we need something to pass to the InvokeDragSession
   // methods.
-  nsCOMPtr<nsIDOMEvent> domEvent;
-  NS_NewDOMDragEvent(getter_AddRefs(domEvent), dragTarget,
-                     aPresContext, aDragEvent);
-
-  nsCOMPtr<nsIDOMDragEvent> domDragEvent = do_QueryInterface(domEvent);
-  // if creating a drag event failed, starting a drag session will
-  // just fail.
+  nsRefPtr<DragEvent> event =
+    NS_NewDOMDragEvent(dragTarget, aPresContext, aDragEvent);
 
   // Use InvokeDragSessionWithSelection if a selection is being dragged,
   // such that the image can be generated from the selected text. However,
   // use InvokeDragSessionWithImage if a custom image was set or something
   // other than a selection is being dragged.
   if (!dragImage && aSelection) {
     dragService->InvokeDragSessionWithSelection(aSelection, transArray,
-                                                action, domDragEvent,
-                                                aDataTransfer);
+                                                action, event, aDataTransfer);
   }
   else {
     // if dragging within a XUL tree and no custom drag image was
     // set, the region argument to InvokeDragSessionWithImage needs
     // to be set to the area encompassing the selected rows of the
     // tree to ensure that the drag feedback gets clipped to those
     // rows. For other content, region should be null.
     nsCOMPtr<nsIScriptableRegion> region;
@@ -1917,18 +1911,17 @@ EventStateManager::DoDefaultDragStart(ns
       }
     }
 #endif
 
     dragService->InvokeDragSessionWithImage(dragTarget->AsDOMNode(), transArray,
                                             region, action,
                                             dragImage ? dragImage->AsDOMNode() :
                                                         nullptr,
-                                            imageX,
-                                            imageY, domDragEvent,
+                                            imageX, imageY, event,
                                             aDataTransfer);
   }
 
   return true;
 }
 
 nsresult
 EventStateManager::GetContentViewer(nsIContentViewer** aCv)
--- a/dom/events/FocusEvent.cpp
+++ b/dom/events/FocusEvent.cpp
@@ -72,19 +72,16 @@ FocusEvent::Constructor(const GlobalObje
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMFocusEvent(nsIDOMEvent** aInstancePtrResult,
-                    EventTarget* aOwner,
+already_AddRefed<FocusEvent>
+NS_NewDOMFocusEvent(EventTarget* aOwner,
                     nsPresContext* aPresContext,
                     InternalFocusEvent* aEvent)
 {
-  FocusEvent* it = new FocusEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<FocusEvent> it = new FocusEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/FocusEvent.h
+++ b/dom/events/FocusEvent.h
@@ -48,9 +48,14 @@ protected:
                           nsIDOMWindow* aView,
                           int32_t aDetail,
                           EventTarget* aRelatedTarget);
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::FocusEvent>
+NS_NewDOMFocusEvent(mozilla::dom::EventTarget* aOwner,
+                    nsPresContext* aPresContext,
+                    mozilla::InternalFocusEvent* aEvent);
+
 #endif // mozilla_dom_FocusEvent_h_
--- a/dom/events/InputEvent.cpp
+++ b/dom/events/InputEvent.cpp
@@ -58,19 +58,16 @@ InputEvent::Constructor(const GlobalObje
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMInputEvent(nsIDOMEvent** aInstancePtrResult,
-                    EventTarget* aOwner,
+already_AddRefed<InputEvent>
+NS_NewDOMInputEvent(EventTarget* aOwner,
                     nsPresContext* aPresContext,
                     InternalEditorInputEvent* aEvent)
 {
-  InputEvent* it = new InputEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<InputEvent> it = new InputEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/InputEvent.h
+++ b/dom/events/InputEvent.h
@@ -41,9 +41,14 @@ public:
 
 protected:
   ~InputEvent() {}
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::InputEvent>
+NS_NewDOMInputEvent(mozilla::dom::EventTarget* aOwner,
+                    nsPresContext* aPresContext,
+                    mozilla::InternalEditorInputEvent* aEvent);
+
 #endif // mozilla_dom_InputEvent_h_
--- a/dom/events/KeyboardEvent.cpp
+++ b/dom/events/KeyboardEvent.cpp
@@ -304,19 +304,16 @@ KeyboardEvent::InitKeyEvent(const nsAStr
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
-                       EventTarget* aOwner,
+already_AddRefed<KeyboardEvent>
+NS_NewDOMKeyboardEvent(EventTarget* aOwner,
                        nsPresContext* aPresContext,
                        WidgetKeyboardEvent* aEvent)
 {
-  KeyboardEvent* it = new KeyboardEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<KeyboardEvent> it = new KeyboardEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/KeyboardEvent.h
+++ b/dom/events/KeyboardEvent.h
@@ -88,9 +88,14 @@ private:
   // value.  mInitializedWhichValue stores it.  I.e., this is invalid when
   // mInitializedByCtor is false.
   uint32_t mInitializedWhichValue;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::KeyboardEvent>
+NS_NewDOMKeyboardEvent(mozilla::dom::EventTarget* aOwner,
+                       nsPresContext* aPresContext,
+                       mozilla::WidgetKeyboardEvent* aEvent);
+
 #endif // mozilla_dom_KeyboardEvent_h_
--- a/dom/events/MessageEvent.cpp
+++ b/dom/events/MessageEvent.cpp
@@ -167,17 +167,17 @@ MessageEvent::Constructor(EventTarget* a
   }
 
   if (aParam.mPorts.WasPassed() && !aParam.mPorts.Value().IsNull()) {
     nsTArray<nsRefPtr<MessagePortBase>> ports;
     for (uint32_t i = 0, len = aParam.mPorts.Value().Value().Length(); i < len; ++i) {
       ports.AppendElement(aParam.mPorts.Value().Value()[i].get());
     }
 
-    event->mPorts = new MessagePortList(static_cast<EventBase*>(event), ports);
+    event->mPorts = new MessagePortList(static_cast<Event*>(event), ports);
   }
 
   return event.forget();
 }
 
 NS_IMETHODIMP
 MessageEvent::InitMessageEvent(const nsAString& aType,
                                bool aCanBubble,
@@ -219,19 +219,16 @@ MessageEvent::SetSource(mozilla::dom::wo
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMMessageEvent(nsIDOMEvent** aInstancePtrResult,
-                      EventTarget* aOwner,
+already_AddRefed<MessageEvent>
+NS_NewDOMMessageEvent(EventTarget* aOwner,
                       nsPresContext* aPresContext,
                       WidgetEvent* aEvent) 
 {
-  MessageEvent* it = new MessageEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<MessageEvent> it = new MessageEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/MessageEvent.h
+++ b/dom/events/MessageEvent.h
@@ -97,9 +97,14 @@ private:
   nsRefPtr<MessagePortBase> mPortSource;
   nsRefPtr<workers::ServiceWorkerClient> mClientSource;
   nsRefPtr<MessagePortList> mPorts;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::MessageEvent>
+NS_NewDOMMessageEvent(mozilla::dom::EventTarget* aOwner,
+                      nsPresContext* aPresContext,
+                      mozilla::WidgetEvent* aEvent);
+
 #endif // mozilla_dom_MessageEvent_h_
--- a/dom/events/MouseEvent.cpp
+++ b/dom/events/MouseEvent.cpp
@@ -499,19 +499,16 @@ MouseEvent::GetMozInputSource(uint16_t* 
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMMouseEvent(nsIDOMEvent** aInstancePtrResult,
-                    EventTarget* aOwner,
+already_AddRefed<MouseEvent>
+NS_NewDOMMouseEvent(EventTarget* aOwner,
                     nsPresContext* aPresContext,
                     WidgetMouseEvent* aEvent)
 {
-  MouseEvent* it = new MouseEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<MouseEvent> it = new MouseEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/MouseEvent.h
+++ b/dom/events/MouseEvent.h
@@ -127,9 +127,14 @@ protected:
 
 } // namespace dom
 } // namespace mozilla
 
 #define NS_FORWARD_TO_MOUSEEVENT \
   NS_FORWARD_NSIDOMMOUSEEVENT(MouseEvent::) \
   NS_FORWARD_TO_UIEVENT
 
+already_AddRefed<mozilla::dom::MouseEvent>
+NS_NewDOMMouseEvent(mozilla::dom::EventTarget* aOwner,
+                    nsPresContext* aPresContext,
+                    mozilla::WidgetMouseEvent* aEvent);
+
 #endif // mozilla_dom_MouseEvent_h_
--- a/dom/events/MouseScrollEvent.cpp
+++ b/dom/events/MouseScrollEvent.cpp
@@ -83,19 +83,17 @@ MouseScrollEvent::Axis()
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace dom;
 
-nsresult
-NS_NewDOMMouseScrollEvent(nsIDOMEvent** aInstancePtrResult,
-                          EventTarget* aOwner,
+already_AddRefed<MouseScrollEvent>
+NS_NewDOMMouseScrollEvent(EventTarget* aOwner,
                           nsPresContext* aPresContext,
                           WidgetMouseScrollEvent* aEvent)
 {
-  MouseScrollEvent* it = new MouseScrollEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<MouseScrollEvent> it =
+    new MouseScrollEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/MouseScrollEvent.h
+++ b/dom/events/MouseScrollEvent.h
@@ -54,9 +54,14 @@ public:
 
 protected:
   ~MouseScrollEvent() {}
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::MouseScrollEvent>
+NS_NewDOMMouseScrollEvent(mozilla::dom::EventTarget* aOwner,
+                          nsPresContext* aPresContext,
+                          mozilla::WidgetMouseScrollEvent* aEvent);
+
 #endif // mozilla_dom_MouseScrollEvent_h_
--- a/dom/events/MutationEvent.cpp
+++ b/dom/events/MutationEvent.cpp
@@ -114,19 +114,16 @@ MutationEvent::InitMutationEvent(const n
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMMutationEvent(nsIDOMEvent** aInstancePtrResult,
-                       EventTarget* aOwner,
+already_AddRefed<MutationEvent>
+NS_NewDOMMutationEvent(EventTarget* aOwner,
                        nsPresContext* aPresContext,
                        InternalMutationEvent* aEvent) 
 {
-  MutationEvent* it = new MutationEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<MutationEvent> it = new MutationEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/MutationEvent.h
+++ b/dom/events/MutationEvent.h
@@ -60,9 +60,14 @@ public:
 
 protected:
   ~MutationEvent() {}
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::MutationEvent>
+NS_NewDOMMutationEvent(mozilla::dom::EventTarget* aOwner,
+                       nsPresContext* aPresContext,
+                       mozilla::InternalMutationEvent* aEvent);
+
 #endif // mozilla_dom_MutationEvent_h_
--- a/dom/events/NotifyPaintEvent.cpp
+++ b/dom/events/NotifyPaintEvent.cpp
@@ -157,22 +157,20 @@ NotifyPaintEvent::Deserialize(const IPC:
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMNotifyPaintEvent(nsIDOMEvent** aInstancePtrResult,
-                          EventTarget* aOwner,
+already_AddRefed<NotifyPaintEvent>
+NS_NewDOMNotifyPaintEvent(EventTarget* aOwner,
                           nsPresContext* aPresContext,
                           WidgetEvent* aEvent,
                           uint32_t aEventType,
                           nsInvalidateRequestList* aInvalidateRequests) 
 {
-  NotifyPaintEvent* it = new NotifyPaintEvent(aOwner, aPresContext, aEvent,
-                                              aEventType, aInvalidateRequests);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<NotifyPaintEvent> it =
+    new NotifyPaintEvent(aOwner, aPresContext, aEvent, aEventType,
+                         aInvalidateRequests);
+  return it.forget();
 }
--- a/dom/events/NotifyPaintEvent.h
+++ b/dom/events/NotifyPaintEvent.h
@@ -62,9 +62,17 @@ private:
   nsRegion GetRegion();
 
   nsTArray<nsInvalidateRequestList::Request> mInvalidateRequests;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+// This empties aInvalidateRequests.
+already_AddRefed<mozilla::dom::NotifyPaintEvent>
+NS_NewDOMNotifyPaintEvent(mozilla::dom::EventTarget* aOwner,
+                          nsPresContext* aPresContext,
+                          mozilla::WidgetEvent* aEvent,
+                          uint32_t aEventType = 0,
+                          nsInvalidateRequestList* aInvalidateRequests = nullptr);
+
 #endif // mozilla_dom_NotifyPaintEvent_h_
--- a/dom/events/PointerEvent.cpp
+++ b/dom/events/PointerEvent.cpp
@@ -159,19 +159,16 @@ PointerEvent::IsPrimary()
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMPointerEvent(nsIDOMEvent** aInstancePtrResult,
-                      EventTarget* aOwner,
+already_AddRefed<PointerEvent>
+NS_NewDOMPointerEvent(EventTarget* aOwner,
                       nsPresContext* aPresContext,
                       WidgetPointerEvent *aEvent)
 {
-  PointerEvent *it = new PointerEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<PointerEvent> it = new PointerEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/PointerEvent.h
+++ b/dom/events/PointerEvent.h
@@ -50,9 +50,14 @@ public:
   void GetPointerType(nsAString& aPointerType);
 };
 
 void ConvertPointerTypeToString(uint16_t aPointerTypeSrc, nsAString& aPointerTypeDest);
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::PointerEvent>
+NS_NewDOMPointerEvent(mozilla::dom::EventTarget* aOwner,
+                      nsPresContext* aPresContext,
+                      mozilla::WidgetPointerEvent* aEvent);
+
 #endif // mozilla_dom_PointerEvent_h_
--- a/dom/events/ScrollAreaEvent.cpp
+++ b/dom/events/ScrollAreaEvent.cpp
@@ -95,17 +95,17 @@ ScrollAreaEvent::Deserialize(const IPC::
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMScrollAreaEvent(nsIDOMEvent** aInstancePtrResult,
-                         EventTarget* aOwner,
+already_AddRefed<ScrollAreaEvent>
+NS_NewDOMScrollAreaEvent(EventTarget* aOwner,
                          nsPresContext* aPresContext,
                          InternalScrollAreaEvent* aEvent)
 {
-  ScrollAreaEvent* ev = new ScrollAreaEvent(aOwner, aPresContext, aEvent);
-  return CallQueryInterface(ev, aInstancePtrResult);
+  nsRefPtr<ScrollAreaEvent> ev =
+    new ScrollAreaEvent(aOwner, aPresContext, aEvent);
+  return ev.forget();
 }
--- a/dom/events/ScrollAreaEvent.h
+++ b/dom/events/ScrollAreaEvent.h
@@ -81,9 +81,14 @@ protected:
   ~ScrollAreaEvent() {}
 
   nsRefPtr<DOMRect> mClientArea;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::ScrollAreaEvent>
+NS_NewDOMScrollAreaEvent(mozilla::dom::EventTarget* aOwner,
+                         nsPresContext* aPresContext,
+                         mozilla::InternalScrollAreaEvent* aEvent);
+
 #endif // mozilla_dom_ScrollAreaEvent_h_
--- a/dom/events/SimpleGestureEvent.cpp
+++ b/dom/events/SimpleGestureEvent.cpp
@@ -141,19 +141,17 @@ SimpleGestureEvent::InitSimpleGestureEve
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMSimpleGestureEvent(nsIDOMEvent** aInstancePtrResult,
-                            EventTarget* aOwner,
+already_AddRefed<SimpleGestureEvent>
+NS_NewDOMSimpleGestureEvent(EventTarget* aOwner,
                             nsPresContext* aPresContext,
                             WidgetSimpleGestureEvent* aEvent)
 {
-  SimpleGestureEvent* it = new SimpleGestureEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<SimpleGestureEvent> it =
+    new SimpleGestureEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/SimpleGestureEvent.h
+++ b/dom/events/SimpleGestureEvent.h
@@ -73,9 +73,14 @@ public:
 
 protected:
   ~SimpleGestureEvent() {}
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::SimpleGestureEvent>
+NS_NewDOMSimpleGestureEvent(mozilla::dom::EventTarget* aOwner,
+                            nsPresContext* aPresContext,
+                            mozilla::WidgetSimpleGestureEvent* aEvent);
+
 #endif // mozilla_dom_SimpleGestureEvent_h_
--- a/dom/events/StorageEvent.cpp
+++ b/dom/events/StorageEvent.cpp
@@ -98,21 +98,20 @@ StorageEvent::InitStorageEvent(const nsA
   mNewValue = aNewValue;
   mUrl = aURL;
   mStorageArea = aStorageArea;
 }
 
 } // namespace dom
 } // namespace mozilla
 
-nsresult
-NS_NewDOMStorageEvent(nsIDOMEvent** aDOMEvent,
-                      mozilla::dom::EventTarget* aOwner)
+using namespace mozilla;
+using namespace mozilla::dom;
+
+already_AddRefed<StorageEvent>
+NS_NewDOMStorageEvent(EventTarget* aOwner)
 {
-  nsRefPtr<mozilla::dom::StorageEvent> e =
-    new mozilla::dom::StorageEvent(aOwner);
+  nsRefPtr<StorageEvent> e = new StorageEvent(aOwner);
 
   e->SetTrusted(e->Init(aOwner));
-  e.forget(aDOMEvent);
-
-  return NS_OK;
+  return e.forget();
 }
 
--- a/dom/events/StorageEvent.h
+++ b/dom/events/StorageEvent.h
@@ -9,18 +9,18 @@
 
 #include "mozilla/Attributes.h"
 #include "mozilla/ErrorResult.h"
 #include "mozilla/dom/BindingUtils.h"
 #include "mozilla/dom/Event.h"
 #include "mozilla/dom/StorageEventBinding.h"
 
 // Helper for EventDispatcher.
-nsresult NS_NewDOMStorageEvent(nsIDOMEvent** aDOMEvent,
-                               mozilla::dom::EventTarget* aOwner);
+already_AddRefed<mozilla::dom::StorageEvent>
+NS_NewDOMStorageEvent(mozilla::dom::EventTarget* aOwner);
 
 namespace mozilla {
 namespace dom {
 
 class DOMStorage;
 
 class StorageEvent : public Event
 {
--- a/dom/events/TouchEvent.cpp
+++ b/dom/events/TouchEvent.cpp
@@ -233,19 +233,16 @@ TouchEvent::ShiftKey()
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMTouchEvent(nsIDOMEvent** aInstancePtrResult,
-                    EventTarget* aOwner,
+already_AddRefed<TouchEvent>
+NS_NewDOMTouchEvent(EventTarget* aOwner,
                     nsPresContext* aPresContext,
                     WidgetTouchEvent* aEvent)
 {
-  TouchEvent* it = new TouchEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<TouchEvent> it = new TouchEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/TouchEvent.h
+++ b/dom/events/TouchEvent.h
@@ -127,9 +127,14 @@ protected:
   nsRefPtr<TouchList> mTouches;
   nsRefPtr<TouchList> mTargetTouches;
   nsRefPtr<TouchList> mChangedTouches;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::TouchEvent>
+NS_NewDOMTouchEvent(mozilla::dom::EventTarget* aOwner,
+                    nsPresContext* aPresContext,
+                    mozilla::WidgetTouchEvent* aEvent);
+
 #endif // mozilla_dom_TouchEvent_h_
--- a/dom/events/TransitionEvent.cpp
+++ b/dom/events/TransitionEvent.cpp
@@ -83,19 +83,17 @@ TransitionEvent::GetPseudoElement(nsAStr
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMTransitionEvent(nsIDOMEvent** aInstancePtrResult,
-                         EventTarget* aOwner,
+already_AddRefed<TransitionEvent>
+NS_NewDOMTransitionEvent(EventTarget* aOwner,
                          nsPresContext* aPresContext,
                          InternalTransitionEvent* aEvent)
 {
-  TransitionEvent *it = new TransitionEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<TransitionEvent> it =
+    new TransitionEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/TransitionEvent.h
+++ b/dom/events/TransitionEvent.h
@@ -47,9 +47,14 @@ public:
 
 protected:
   ~TransitionEvent() {}
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::TransitionEvent>
+NS_NewDOMTransitionEvent(mozilla::dom::EventTarget* aOwner,
+                         nsPresContext* aPresContext,
+                         mozilla::InternalTransitionEvent* aEvent);
+
 #endif // mozilla_dom_TransitionEvent_h_
--- a/dom/events/UIEvent.cpp
+++ b/dom/events/UIEvent.cpp
@@ -494,19 +494,16 @@ UIEvent::InitModifiers(const EventModifi
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMUIEvent(nsIDOMEvent** aInstancePtrResult,
-                 EventTarget* aOwner,
+already_AddRefed<UIEvent>
+NS_NewDOMUIEvent(EventTarget* aOwner,
                  nsPresContext* aPresContext,
                  WidgetGUIEvent* aEvent) 
 {
-  UIEvent* it = new UIEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<UIEvent> it = new UIEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/UIEvent.h
+++ b/dom/events/UIEvent.h
@@ -189,9 +189,14 @@ protected:
     UIEvent::Serialize(aMsg, aSerializeInterfaceType);      \
   }                                                         \
   NS_IMETHOD_(bool) Deserialize(const IPC::Message* aMsg,   \
                                 void** aIter) override      \
   {                                                         \
     return UIEvent::Deserialize(aMsg, aIter);               \
   }
 
+already_AddRefed<mozilla::dom::UIEvent>
+NS_NewDOMUIEvent(mozilla::dom::EventTarget* aOwner,
+                 nsPresContext* aPresContext,
+                 mozilla::WidgetGUIEvent* aEvent);
+
 #endif // mozilla_dom_UIEvent_h_
--- a/dom/events/WheelEvent.cpp
+++ b/dom/events/WheelEvent.cpp
@@ -170,19 +170,16 @@ WheelEvent::Constructor(const GlobalObje
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMWheelEvent(nsIDOMEvent** aInstancePtrResult,
-                    EventTarget* aOwner,
+already_AddRefed<WheelEvent>
+NS_NewDOMWheelEvent(EventTarget* aOwner,
                     nsPresContext* aPresContext,
                     WidgetWheelEvent* aEvent)
 {
-  WheelEvent* it = new WheelEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<WheelEvent> it = new WheelEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/WheelEvent.h
+++ b/dom/events/WheelEvent.h
@@ -55,9 +55,14 @@ protected:
 
 private:
   int32_t mAppUnitsPerDevPixel;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::WheelEvent>
+NS_NewDOMWheelEvent(mozilla::dom::EventTarget* aOwner,
+                    nsPresContext* aPresContext,
+                    mozilla::WidgetWheelEvent* aEvent);
+
 #endif // mozilla_dom_WheelEvent_h_
--- a/dom/events/XULCommandEvent.cpp
+++ b/dom/events/XULCommandEvent.cpp
@@ -124,19 +124,17 @@ XULCommandEvent::InitCommandEvent(const 
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMXULCommandEvent(nsIDOMEvent** aInstancePtrResult,
-                         EventTarget* aOwner,
+already_AddRefed<XULCommandEvent>
+NS_NewDOMXULCommandEvent(EventTarget* aOwner,
                          nsPresContext* aPresContext,
                          WidgetInputEvent* aEvent) 
 {
-  XULCommandEvent* it = new XULCommandEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<XULCommandEvent> it =
+    new XULCommandEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/events/XULCommandEvent.h
+++ b/dom/events/XULCommandEvent.h
@@ -66,9 +66,14 @@ protected:
   ~XULCommandEvent() {}
 
   nsCOMPtr<nsIDOMEvent> mSourceEvent;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::XULCommandEvent>
+NS_NewDOMXULCommandEvent(mozilla::dom::EventTarget* aOwner,
+                         nsPresContext* aPresContext,
+                         mozilla::WidgetInputEvent* aEvent);
+
 #endif // mozilla_dom_XULCommandEvent_h_
--- a/dom/events/moz.build
+++ b/dom/events/moz.build
@@ -130,16 +130,17 @@ include('/ipc/chromium/chromium-config.m
 
 FINAL_LIBRARY = 'xul'
 LOCAL_INCLUDES += [
     '/docshell/base',
     '/dom/base',
     '/dom/html',
     '/dom/settings',
     '/dom/storage',
+    '/dom/svg',
     '/dom/workers',
     '/dom/xml',
     '/dom/xul',
     '/js/xpconnect/wrappers',
     '/layout/generic',
     '/layout/xul',
     '/layout/xul/tree/',
 ]
--- a/dom/html/HTMLInputElement.cpp
+++ b/dom/html/HTMLInputElement.cpp
@@ -5398,27 +5398,23 @@ HTMLInputElement::GetPhonetic(nsAString&
 }
 
 #ifdef ACCESSIBILITY
 /*static*/ nsresult
 FireEventForAccessibility(nsIDOMHTMLInputElement* aTarget,
                           nsPresContext* aPresContext,
                           const nsAString& aEventType)
 {
-  nsCOMPtr<nsIDOMEvent> event;
   nsCOMPtr<mozilla::dom::Element> element = do_QueryInterface(aTarget);
-  if (NS_SUCCEEDED(EventDispatcher::CreateEvent(element, aPresContext, nullptr,
-                                                NS_LITERAL_STRING("Events"),
-                                                getter_AddRefs(event)))) {
-    event->InitEvent(aEventType, true, true);
-    event->SetTrusted(true);
-
-    EventDispatcher::DispatchDOMEvent(aTarget, nullptr, event, aPresContext,
-                                      nullptr);
-  }
+  nsRefPtr<Event> event = NS_NewDOMEvent(element, aPresContext, nullptr);
+  event->InitEvent(aEventType, true, true);
+  event->SetTrusted(true);
+
+  EventDispatcher::DispatchDOMEvent(aTarget, nullptr, event, aPresContext,
+                                    nullptr);
 
   return NS_OK;
 }
 #endif
 
 nsresult
 HTMLInputElement::SetDefaultValueAsValue()
 {
--- a/dom/interfaces/events/nsIDOMEvent.idl
+++ b/dom/interfaces/events/nsIDOMEvent.idl
@@ -210,167 +210,8 @@ interface nsIDOMEvent : nsISupports
   [noscript,notxpcom] void SetTrusted(in boolean aTrusted);
   [notxpcom] void Serialize(in IPCMessagePtr aMsg,
                             in boolean aSerializeInterfaceType);
   [notxpcom] boolean Deserialize(in ConstIPCMessagePtr aMsg, out voidPtr aIter);
   [noscript,notxpcom] void SetOwner(in EventTargetPtr aOwner);
   [notxpcom] DOMEventPtr InternalDOMEvent();
   [noscript] void stopCrossProcessForwarding();
 };
-
-%{C++
-
-nsresult
-NS_NewDOMEvent(nsIDOMEvent** aInstancePtrResult,
-               mozilla::dom::EventTarget* aOwner,
-               nsPresContext* aPresContext,
-               mozilla::WidgetEvent* aEvent);
-nsresult
-NS_NewDOMDataContainerEvent(nsIDOMEvent** aInstancePtrResult,
-                            mozilla::dom::EventTarget* aOwner,
-                            nsPresContext* aPresContext,
-                            mozilla::WidgetEvent* aEvent);
-nsresult
-NS_NewDOMUIEvent(nsIDOMEvent** aInstancePtrResult,
-                 mozilla::dom::EventTarget* aOwner,
-                 nsPresContext* aPresContext,
-                 mozilla::WidgetGUIEvent* aEvent);
-nsresult
-NS_NewDOMMouseEvent(nsIDOMEvent** aInstancePtrResult,
-                    mozilla::dom::EventTarget* aOwner,
-                    nsPresContext* aPresContext,
-                    mozilla::WidgetMouseEvent* aEvent);
-nsresult
-NS_NewDOMFocusEvent(nsIDOMEvent** aInstancePtrResult,
-                    mozilla::dom::EventTarget* aOwner,
-                    nsPresContext* aPresContext,
-                    mozilla::InternalFocusEvent* aEvent);
-nsresult
-NS_NewDOMMouseScrollEvent(nsIDOMEvent** aInstancePtrResult,
-                          mozilla::dom::EventTarget* aOwner,
-                          nsPresContext* aPresContext,
-                          mozilla::WidgetMouseScrollEvent* aEvent);
-nsresult
-NS_NewDOMWheelEvent(nsIDOMEvent** aInstancePtrResult,
-                    mozilla::dom::EventTarget* aOwner,
-                    nsPresContext* aPresContext,
-                    mozilla::WidgetWheelEvent* aEvent);
-nsresult
-NS_NewDOMDragEvent(nsIDOMEvent** aInstancePtrResult,
-                   mozilla::dom::EventTarget* aOwner,
-                   nsPresContext* aPresContext,
-                   mozilla::WidgetDragEvent* aEvent);
-nsresult
-NS_NewDOMClipboardEvent(nsIDOMEvent** aInstancePtrResult,
-                        mozilla::dom::EventTarget* aOwner,
-                        nsPresContext* aPresContext,
-                        mozilla::InternalClipboardEvent* aEvent);
-nsresult
-NS_NewDOMInputEvent(nsIDOMEvent** aInstancePtrResult,
-                    mozilla::dom::EventTarget* aOwner,
-                    nsPresContext* aPresContext,
-                    mozilla::InternalEditorInputEvent* aEvent);
-nsresult
-NS_NewDOMKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
-                       mozilla::dom::EventTarget* aOwner,
-                       nsPresContext* aPresContext,
-                       mozilla::WidgetKeyboardEvent* aEvent);
-
-nsresult
-NS_NewDOMBeforeAfterKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
-                                  mozilla::dom::EventTarget* aOwner,
-                                  nsPresContext* aPresContext,
-                                  mozilla::InternalBeforeAfterKeyboardEvent* aEvent);
-
-nsresult
-NS_NewDOMCompositionEvent(nsIDOMEvent** aInstancePtrResult,
-                          mozilla::dom::EventTarget* aOwner,
-                          nsPresContext* aPresContext,
-                          mozilla::WidgetCompositionEvent* aEvent);
-nsresult
-NS_NewDOMMutationEvent(nsIDOMEvent** aResult,
-                       mozilla::dom::EventTarget* aOwner,
-                       nsPresContext* aPresContext,
-                       mozilla::InternalMutationEvent* aEvent);
-nsresult
-NS_NewDOMDeviceMotionEvent(nsIDOMEvent** aResult,
-                           mozilla::dom::EventTarget* aOwner,
-                           nsPresContext* aPresContext,
-                           mozilla::WidgetEvent* aEvent);
-nsresult
-NS_NewDOMBeforeUnloadEvent(nsIDOMEvent** aResult,
-                           mozilla::dom::EventTarget* aOwner,
-                           nsPresContext* aPresContext,
-                           mozilla::WidgetEvent* aEvent);
-nsresult
-NS_NewDOMSVGEvent(nsIDOMEvent** aResult,
-                  mozilla::dom::EventTarget* aOwner,
-                  nsPresContext* aPresContext,
-                  mozilla::WidgetEvent* aEvent);
-nsresult
-NS_NewDOMSVGZoomEvent(nsIDOMEvent** aResult,
-                      mozilla::dom::EventTarget* aOwner,
-                      nsPresContext* aPresContext,
-                      mozilla::InternalSVGZoomEvent* aEvent);
-nsresult
-NS_NewDOMTimeEvent(nsIDOMEvent** aResult,
-                   mozilla::dom::EventTarget* aOwner,
-                   nsPresContext* aPresContext,
-                   mozilla::InternalSMILTimeEvent* aEvent);
-nsresult
-NS_NewDOMXULCommandEvent(nsIDOMEvent** aResult,
-                         mozilla::dom::EventTarget* aOwner,
-                         nsPresContext* aPresContext,
-                         mozilla::WidgetInputEvent* aEvent);
-nsresult
-NS_NewDOMCommandEvent(nsIDOMEvent** aInstancePtrResult,
-                      mozilla::dom::EventTarget* aOwner,
-                      nsPresContext* aPresContext,
-                      mozilla::WidgetCommandEvent* aEvent);
-nsresult
-NS_NewDOMMessageEvent(nsIDOMEvent** aInstancePtrResult,
-                      mozilla::dom::EventTarget* aOwner,
-                      nsPresContext* aPresContext,
-                      mozilla::WidgetEvent* aEvent);
-// This empties aInvalidateRequests.
-nsresult
-NS_NewDOMNotifyPaintEvent(nsIDOMEvent** aResult,
-                          mozilla::dom::EventTarget* aOwner,
-                          nsPresContext* aPresContext,
-                          mozilla::WidgetEvent* aEvent,
-                          uint32_t aEventType = 0,
-                          nsInvalidateRequestList* aInvalidateRequests = nullptr);
-nsresult
-NS_NewDOMSimpleGestureEvent(nsIDOMEvent** aInstancePtrResult,
-                            mozilla::dom::EventTarget* aOwner,
-                            nsPresContext* aPresContext,
-                            mozilla::WidgetSimpleGestureEvent* aEvent);
-nsresult
-NS_NewDOMScrollAreaEvent(nsIDOMEvent** aInstancePtrResult,
-                         mozilla::dom::EventTarget* aOwner,
-                         nsPresContext* aPresContext,
-                         mozilla::InternalScrollAreaEvent* aEvent);
-nsresult
-NS_NewDOMTransitionEvent(nsIDOMEvent** aInstancePtrResult,
-                         mozilla::dom::EventTarget* aOwner,
-                         nsPresContext* aPresContext,
-                         mozilla::InternalTransitionEvent* aEvent);
-nsresult
-NS_NewDOMAnimationEvent(nsIDOMEvent** aInstancePtrResult,
-                        mozilla::dom::EventTarget* aOwner,
-                        nsPresContext* aPresContext,
-                        mozilla::InternalAnimationEvent* aEvent);
-nsresult
-NS_NewDOMPointerEvent(nsIDOMEvent** aInstancePtrResult,
-                      mozilla::dom::EventTarget* aOwner,
-                      nsPresContext* aPresContext,
-                      mozilla::WidgetPointerEvent* aEvent);
-nsresult
-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);
-%}
--- a/dom/ipc/TabMessageUtils.cpp
+++ b/dom/ipc/TabMessageUtils.cpp
@@ -1,33 +1,30 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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/EventDispatcher.h"
+#include "mozilla/dom/Event.h"
 #include "mozilla/dom/TabMessageUtils.h"
 #include "nsCOMPtr.h"
-#include "nsIDOMEvent.h"
 
 namespace mozilla {
 namespace dom {
 
 bool
 ReadRemoteEvent(const IPC::Message* aMsg, void** aIter,
                 RemoteDOMEvent* aResult)
 {
   aResult->mEvent = nullptr;
   nsString type;
   NS_ENSURE_TRUE(ReadParam(aMsg, aIter, &type), false);
 
-  nsCOMPtr<nsIDOMEvent> event;
-  EventDispatcher::CreateEvent(nullptr, nullptr, nullptr, type,
-                               getter_AddRefs(event));
-  aResult->mEvent = do_QueryInterface(event);
-  NS_ENSURE_TRUE(aResult->mEvent, false);
+  aResult->mEvent = EventDispatcher::CreateEvent(nullptr, nullptr, nullptr,
+                                                 type);
 
   return aResult->mEvent->Deserialize(aMsg, aIter);
 }
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/ipc/TabParent.cpp
+++ b/dom/ipc/TabParent.cpp
@@ -3017,18 +3017,17 @@ bool
 TabParent::LayerTreeUpdate(bool aActive)
 {
   nsCOMPtr<mozilla::dom::EventTarget> target = do_QueryInterface(mFrameElement);
   if (!target) {
     NS_WARNING("Could not locate target for layer tree message.");
     return true;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  NS_NewDOMEvent(getter_AddRefs(event), mFrameElement, nullptr, nullptr);
+  nsRefPtr<Event> event = NS_NewDOMEvent(mFrameElement, nullptr, nullptr);
   if (aActive) {
     event->InitEvent(NS_LITERAL_STRING("MozLayerTreeReady"), true, false);
   } else {
     event->InitEvent(NS_LITERAL_STRING("MozLayerTreeCleared"), true, false);
   }
   event->SetTrusted(true);
   event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
   bool dummy;
@@ -3057,18 +3056,17 @@ bool
 TabParent::RecvRemotePaintIsReady()
 {
   nsCOMPtr<mozilla::dom::EventTarget> target = do_QueryInterface(mFrameElement);
   if (!target) {
     NS_WARNING("Could not locate target for MozAfterRemotePaint message.");
     return true;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  NS_NewDOMEvent(getter_AddRefs(event), mFrameElement, nullptr, nullptr);
+  nsRefPtr<Event> event = NS_NewDOMEvent(mFrameElement, nullptr, nullptr);
   event->InitEvent(NS_LITERAL_STRING("MozAfterRemotePaint"), false, false);
   event->SetTrusted(true);
   event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
   bool dummy;
   mFrameElement->DispatchEvent(event, &dummy);
   return true;
 }
 
--- a/dom/media/MediaRecorder.cpp
+++ b/dom/media/MediaRecorder.cpp
@@ -1025,22 +1025,17 @@ void
 MediaRecorder::DispatchSimpleEvent(const nsAString & aStr)
 {
   MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
   nsresult rv = CheckInnerWindowCorrectness();
   if (NS_FAILED(rv)) {
     return;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  if (NS_FAILED(rv)) {
-    NS_WARNING("Failed to create the error event!!!");
-    return;
-  }
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
   rv = event->InitEvent(aStr, false, false);
 
   if (NS_FAILED(rv)) {
     NS_WARNING("Failed to init the error event!!!");
     return;
   }
 
   event->SetTrusted(true);
--- a/dom/media/TextTrackList.cpp
+++ b/dom/media/TextTrackList.cpp
@@ -155,24 +155,19 @@ nsresult
 TextTrackList::DispatchTrackEvent(nsIDOMEvent* aEvent)
 {
   return DispatchTrustedEvent(aEvent);
 }
 
 void
 TextTrackList::CreateAndDispatchChangeEvent()
 {
-  nsCOMPtr<nsIDOMEvent> event;
-  nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  if (NS_FAILED(rv)) {
-    NS_WARNING("Failed to create the error event!");
-    return;
-  }
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
 
-  rv = event->InitEvent(NS_LITERAL_STRING("change"), false, false);
+  nsresult rv = event->InitEvent(NS_LITERAL_STRING("change"), false, false);
   if (NS_FAILED(rv)) {
     NS_WARNING("Failed to init the change event!");
     return;
   }
 
   event->SetTrusted(true);
 
   nsCOMPtr<nsIRunnable> eventRunner = new TrackEventRunner(this, event);
--- a/dom/notification/DesktopNotification.cpp
+++ b/dom/notification/DesktopNotification.cpp
@@ -168,26 +168,24 @@ DesktopNotification::~DesktopNotificatio
 
 void
 DesktopNotification::DispatchNotificationEvent(const nsString& aName)
 {
   if (NS_FAILED(CheckInnerWindowCorrectness())) {
     return;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  if (NS_SUCCEEDED(rv)) {
-    // it doesn't bubble, and it isn't cancelable
-    rv = event->InitEvent(aName, false, false);
-    if (NS_SUCCEEDED(rv)) {
-      event->SetTrusted(true);
-      DispatchDOMEvent(nullptr, event, nullptr, nullptr);
-    }
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
+  // it doesn't bubble, and it isn't cancelable
+  nsresult rv = event->InitEvent(aName, false, false);
+  if (NS_FAILED(rv)) {
+    return;
   }
+  event->SetTrusted(true);
+  DispatchDOMEvent(nullptr, event, nullptr, nullptr);
 }
 
 nsresult
 DesktopNotification::SetAllow(bool aAllow)
 {
   mAllow = aAllow;
 
   // if we have called Show() already, lets go ahead and post a notification
--- a/dom/notification/Notification.cpp
+++ b/dom/notification/Notification.cpp
@@ -1072,18 +1072,17 @@ Notification::DispatchNotificationClickE
   // Client.focus() API if it wishes.
   return false;
 }
 
 bool
 Notification::DispatchClickEvent()
 {
   AssertIsOnTargetThread();
-  nsCOMPtr<nsIDOMEvent> event;
-  NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
   nsresult rv = event->InitEvent(NS_LITERAL_STRING("click"), false, true);
   NS_ENSURE_SUCCESS(rv, false);
   event->SetTrusted(true);
   WantsPopupControlCheck popupControlCheck(event);
   bool doDefaultAction = true;
   DispatchEvent(event, &doDefaultAction);
   return doDefaultAction;
 }
--- a/dom/offline/nsDOMOfflineResourceList.cpp
+++ b/dom/offline/nsDOMOfflineResourceList.cpp
@@ -16,16 +16,17 @@
 #include "nsServiceManagerUtils.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsIOfflineCacheUpdate.h"
 #include "nsAutoPtr.h"
 #include "nsContentUtils.h"
 #include "nsIObserverService.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsIWebNavigation.h"
+#include "mozilla/dom/Event.h"
 #include "mozilla/dom/OfflineResourceListBinding.h"
 #include "mozilla/EventDispatcher.h"
 #include "mozilla/Preferences.h"
 
 #include "nsXULAppAPI.h"
 #define IS_CHILD_PROCESS() \
     (GeckoProcessType_Default != XRE_GetProcessType())
 
@@ -542,21 +543,17 @@ nsDOMOfflineResourceList::SendEvent(cons
   if (!GetOwner()) {
     return NS_OK;
   }
 
   if (!GetOwner()->GetDocShell()) {
     return NS_OK;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  nsresult rv = EventDispatcher::CreateEvent(this, nullptr, nullptr,
-                                             NS_LITERAL_STRING("Events"),
-                                             getter_AddRefs(event));
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
   event->InitEvent(aEventName, false, true);
 
   // We assume anyone that managed to call SendEvent is trusted
   event->SetTrusted(true);
 
   // If the window is frozen or we're still catching up on events that were
   // queued while frozen, save the event for later.
   if (GetOwner()->IsFrozen() || mPendingEvents.Count() > 0) {
--- a/dom/presentation/PresentationSession.cpp
+++ b/dom/presentation/PresentationSession.cpp
@@ -1,17 +1,17 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* 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/AsyncEventDispatcher.h"
+#include "mozilla/dom/MessageEvent.h"
 #include "nsCycleCollectionParticipant.h"
-#include "nsIDOMMessageEvent.h"
 #include "nsIPresentationService.h"
 #include "nsServiceManagerUtils.h"
 #include "nsStringStream.h"
 #include "PresentationSession.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
@@ -255,30 +255,26 @@ PresentationSession::DispatchMessageEven
 
   // Get the origin.
   nsAutoString origin;
   nsresult rv = nsContentUtils::GetUTFOrigin(global->PrincipalOrNull(), origin);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  rv = NS_NewDOMMessageEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
+  nsRefPtr<MessageEvent> messageEvent =
+    NS_NewDOMMessageEvent(this, nullptr, nullptr);
 
-  nsCOMPtr<nsIDOMMessageEvent> messageEvent = do_QueryInterface(event);
   rv = messageEvent->InitMessageEvent(NS_LITERAL_STRING("message"),
                                       false, false,
                                       aData,
                                       origin,
                                       EmptyString(), nullptr);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  event->SetTrusted(true);
+  messageEvent->SetTrusted(true);
 
   nsRefPtr<AsyncEventDispatcher> asyncDispatcher =
-    new AsyncEventDispatcher(this, event);
+    new AsyncEventDispatcher(this, static_cast<Event*>(messageEvent));
   return asyncDispatcher->PostDOMEvent();
 }
--- a/dom/smil/TimeEvent.cpp
+++ b/dom/smil/TimeEvent.cpp
@@ -75,19 +75,16 @@ TimeEvent::InitTimeEvent(const nsAString
 }
 
 } // namespace dom
 } // namespace mozilla
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-nsresult
-NS_NewDOMTimeEvent(nsIDOMEvent** aInstancePtrResult,
-                   EventTarget* aOwner,
+already_AddRefed<TimeEvent>
+NS_NewDOMTimeEvent(EventTarget* aOwner,
                    nsPresContext* aPresContext,
                    InternalSMILTimeEvent* aEvent)
 {
-  TimeEvent* it = new TimeEvent(aOwner, aPresContext, aEvent);
-  NS_ADDREF(it);
-  *aInstancePtrResult = static_cast<Event*>(it);
-  return NS_OK;
+  nsRefPtr<TimeEvent> it = new TimeEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/smil/TimeEvent.h
+++ b/dom/smil/TimeEvent.h
@@ -58,9 +58,14 @@ private:
 
   nsCOMPtr<nsIDOMWindow> mView;
   int32_t mDetail;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::TimeEvent>
+NS_NewDOMTimeEvent(mozilla::dom::EventTarget* aOwner,
+                   nsPresContext* aPresContext,
+                   mozilla::InternalSMILTimeEvent* aEvent);
+
 #endif // mozilla_dom_TimeEvent_h_
--- a/dom/speakermanager/SpeakerManager.cpp
+++ b/dom/speakermanager/SpeakerManager.cpp
@@ -1,24 +1,27 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 "SpeakerManager.h"
+
+#include "mozilla/Services.h"
+
+#include "mozilla/dom/Event.h"
+
+#include "AudioChannelService.h"
+#include "nsIDocShell.h"
 #include "nsIDOMClassInfo.h"
-#include "nsIDOMEvent.h"
 #include "nsIDOMEventListener.h"
-#include "SpeakerManagerService.h"
+#include "nsIInterfaceRequestorUtils.h"
 #include "nsIPermissionManager.h"
-#include "nsIInterfaceRequestorUtils.h"
-#include "nsIDocShell.h"
-#include "AudioChannelService.h"
-#include "mozilla/Services.h"
+#include "SpeakerManagerService.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_QUERY_INTERFACE_INHERITED(SpeakerManager, DOMEventTargetHelper,
                                   nsIDOMEventListener)
 NS_IMPL_ADDREF_INHERITED(SpeakerManager, DOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(SpeakerManager, DOMEventTargetHelper)
@@ -81,22 +84,17 @@ void
 SpeakerManager::DispatchSimpleEvent(const nsAString& aStr)
 {
   MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
   nsresult rv = CheckInnerWindowCorrectness();
   if (NS_FAILED(rv)) {
     return;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
-  if (NS_FAILED(rv)) {
-    NS_WARNING("Failed to create the error event!!!");
-    return;
-  }
+  nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
   rv = event->InitEvent(aStr, false, false);
 
   if (NS_FAILED(rv)) {
     NS_WARNING("Failed to init the error event!!!");
     return;
   }
 
   event->SetTrusted(true);
--- a/dom/svg/SVGZoomEvent.cpp
+++ b/dom/svg/SVGZoomEvent.cpp
@@ -85,18 +85,19 @@ SVGZoomEvent::~SVGZoomEvent()
 
 } // namespace dom
 } // namespace mozilla
 
 
 ////////////////////////////////////////////////////////////////////////
 // Exported creation functions:
 
-nsresult
-NS_NewDOMSVGZoomEvent(nsIDOMEvent** aInstancePtrResult,
-                      mozilla::dom::EventTarget* aOwner,
+using namespace mozilla;
+using namespace mozilla::dom;
+
+already_AddRefed<SVGZoomEvent>
+NS_NewDOMSVGZoomEvent(EventTarget* aOwner,
                       nsPresContext* aPresContext,
                       mozilla::InternalSVGZoomEvent* aEvent)
 {
-  mozilla::dom::SVGZoomEvent* it =
-    new mozilla::dom::SVGZoomEvent(aOwner, aPresContext, aEvent);
-  return CallQueryInterface(it, aInstancePtrResult);
+  nsRefPtr<SVGZoomEvent> it = new SVGZoomEvent(aOwner, aPresContext, aEvent);
+  return it.forget();
 }
--- a/dom/svg/SVGZoomEvent.h
+++ b/dom/svg/SVGZoomEvent.h
@@ -66,9 +66,14 @@ private:
   float mNewScale;
   nsRefPtr<DOMSVGPoint> mPreviousTranslate;
   nsRefPtr<DOMSVGPoint> mNewTranslate;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+already_AddRefed<mozilla::dom::SVGZoomEvent>
+NS_NewDOMSVGZoomEvent(mozilla::dom::EventTarget* aOwner,
+                      nsPresContext* aPresContext,
+                      mozilla::InternalSVGZoomEvent* aEvent);
+
 #endif // mozilla_dom_SVGZoomEvent_h
--- a/dom/workers/ServiceWorkerManager.cpp
+++ b/dom/workers/ServiceWorkerManager.cpp
@@ -2369,24 +2369,20 @@ public:
 
   bool
   WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
   {
     MOZ_ASSERT(aWorkerPrivate);
 
     WorkerGlobalScope* globalScope = aWorkerPrivate->GlobalScope();
 
-    nsCOMPtr<nsIDOMEvent> event;
-    nsresult rv =
-      NS_NewDOMEvent(getter_AddRefs(event), globalScope, nullptr, nullptr);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return false;
-    }
-
-    rv = event->InitEvent(NS_LITERAL_STRING("pushsubscriptionchange"), false, false);
+    nsRefPtr<Event> event = NS_NewDOMEvent(globalScope, nullptr, nullptr);
+
+    nsresult rv = event->InitEvent(NS_LITERAL_STRING("pushsubscriptionchange"),
+                                   false, false);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return false;
     }
 
     event->SetTrusted(true);
 
     globalScope->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
     return true;
--- a/dom/workers/WorkerPrivate.cpp
+++ b/dom/workers/WorkerPrivate.cpp
@@ -1043,25 +1043,19 @@ private:
   {
     JS::Rooted<JSObject*> target(aCx, JS::CurrentGlobalOrNull(aCx));
     NS_ASSERTION(target, "This must never be null!");
 
     aWorkerPrivate->CloseHandlerStarted();
 
     WorkerGlobalScope* globalScope = aWorkerPrivate->GlobalScope();
 
-    nsCOMPtr<nsIDOMEvent> event;
-    nsresult rv =
-      NS_NewDOMEvent(getter_AddRefs(event), globalScope, nullptr, nullptr);
-    if (NS_FAILED(rv)) {
-      Throw(aCx, rv);
-      return false;
-    }
-
-    rv = event->InitEvent(NS_LITERAL_STRING("close"), false, false);
+    nsRefPtr<Event> event = NS_NewDOMEvent(globalScope, nullptr, nullptr);
+
+    nsresult rv = event->InitEvent(NS_LITERAL_STRING("close"), false, false);
     if (NS_FAILED(rv)) {
       Throw(aCx, rv);
       return false;
     }
 
     event->SetTrusted(true);
 
     globalScope->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
@@ -3614,22 +3608,19 @@ WorkerPrivate::OfflineStatusChangeEventI
 
   nsString eventType;
   if (aIsOffline) {
     eventType.AssignLiteral("offline");
   } else {
     eventType.AssignLiteral("online");
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  nsresult rv =
-    NS_NewDOMEvent(getter_AddRefs(event), globalScope, nullptr, nullptr);
-  NS_ENSURE_SUCCESS_VOID(rv);
-
-  rv = event->InitEvent(eventType, false, false);
+  nsRefPtr<Event> event = NS_NewDOMEvent(globalScope, nullptr, nullptr);
+
+  nsresult rv = event->InitEvent(eventType, false, false);
   NS_ENSURE_SUCCESS_VOID(rv);
 
   event->SetTrusted(true);
 
   globalScope->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
 }
 
 template <class Derived>
--- a/dom/workers/XMLHttpRequest.cpp
+++ b/dom/workers/XMLHttpRequest.cpp
@@ -1390,29 +1390,29 @@ EventRunnable::WorkerRun(JSContext* aCx,
     target = xhr->GetUploadObjectNoCreate();
   }
   else {
     target = xhr;
   }
 
   MOZ_ASSERT(target);
 
-  nsCOMPtr<nsIDOMEvent> event;
+  nsRefPtr<Event> event;
   if (mProgressEvent) {
     ProgressEventInit init;
     init.mBubbles = false;
     init.mCancelable = false;
     init.mLengthComputable = mLengthComputable;
     init.mLoaded = mLoaded;
     init.mTotal = mTotal;
 
     event = ProgressEvent::Constructor(target, mType, init);
   }
   else {
-    NS_NewDOMEvent(getter_AddRefs(event), target, nullptr, nullptr);
+    event = NS_NewDOMEvent(target, nullptr, nullptr);
 
     if (event) {
       event->InitEvent(mType, false, false);
     }
   }
 
   if (!event) {
     return false;
@@ -1791,23 +1791,20 @@ XMLHttpRequest::DispatchPrematureAbortEv
   mWorkerPrivate->AssertIsOnWorkerThread();
   MOZ_ASSERT(aTarget);
 
   if (!mProxy) {
     aRv.Throw(NS_ERROR_FAILURE);
     return;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
+  nsRefPtr<Event> event;
   if (aEventType.EqualsLiteral("readystatechange")) {
-    NS_NewDOMEvent(getter_AddRefs(event), aTarget, nullptr, nullptr);
-
-    if (event) {
-      event->InitEvent(aEventType, false, false);
-    }
+    event = NS_NewDOMEvent(aTarget, nullptr, nullptr);
+    event->InitEvent(aEventType, false, false);
   }
   else {
     ProgressEventInit init;
     init.mBubbles = false;
     init.mCancelable = false;
     if (aUploadTarget) {
       init.mLengthComputable = mProxy->mLastUploadLengthComputable;
       init.mLoaded = mProxy->mLastUploadLoaded;
--- a/dom/xml/nsXMLPrettyPrinter.cpp
+++ b/dom/xml/nsXMLPrettyPrinter.cpp
@@ -18,17 +18,17 @@
 #include "mozilla/dom/Element.h"
 #include "nsIDOMDocumentFragment.h"
 #include "nsBindingManager.h"
 #include "nsXBLService.h"
 #include "nsIScriptSecurityManager.h"
 #include "mozilla/Preferences.h"
 #include "nsIDocument.h"
 #include "nsVariant.h"
-#include "nsIDOMCustomEvent.h"
+#include "mozilla/dom/CustomEvent.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 NS_IMPL_ISUPPORTS(nsXMLPrettyPrinter,
                   nsIDocumentObserver,
                   nsIMutationObserver)
 
@@ -151,32 +151,29 @@ nsXMLPrettyPrinter::PrettyPrint(nsIDocum
     // Load the bindings.
     nsRefPtr<nsXBLBinding> unused;
     bool ignored;
     rv = xblService->LoadBindings(rootCont, bindingUri, sysPrincipal,
                                   getter_AddRefs(unused), &ignored);
     NS_ENSURE_SUCCESS(rv, rv);
 
     // Fire an event at the bound element to pass it |resultFragment|.
-    nsCOMPtr<nsIDOMEvent> domEvent;
-    rv = NS_NewDOMCustomEvent(getter_AddRefs(domEvent), rootCont,
-                              nullptr, nullptr);
-    NS_ENSURE_SUCCESS(rv, rv);
-    nsCOMPtr<nsIDOMCustomEvent> customEvent = do_QueryInterface(domEvent);
-    MOZ_ASSERT(customEvent);
+    nsRefPtr<CustomEvent> event =
+      NS_NewDOMCustomEvent(rootCont, nullptr, nullptr);
+    MOZ_ASSERT(event);
     nsCOMPtr<nsIWritableVariant> resultFragmentVariant = new nsVariant();
     rv = resultFragmentVariant->SetAsISupports(resultFragment);
     MOZ_ASSERT(NS_SUCCEEDED(rv));
-    rv = customEvent->InitCustomEvent(NS_LITERAL_STRING("prettyprint-dom-created"),
-                                      /* bubbles = */ false, /* cancelable = */ false,
-                                      /* detail = */ resultFragmentVariant);
+    rv = event->InitCustomEvent(NS_LITERAL_STRING("prettyprint-dom-created"),
+                                /* bubbles = */ false, /* cancelable = */ false,
+                                /* detail = */ resultFragmentVariant);
     NS_ENSURE_SUCCESS(rv, rv);
-    customEvent->SetTrusted(true);
+    event->SetTrusted(true);
     bool dummy;
-    rv = rootCont->DispatchEvent(domEvent, &dummy);
+    rv = rootCont->DispatchEvent(static_cast<Event*>(event), &dummy);
     NS_ENSURE_SUCCESS(rv, rv);
 
     // Observe the document so we know when to switch to "normal" view
     aDocument->AddObserver(this);
     mDocument = aDocument;
 
     NS_ADDREF_THIS();
 
--- a/layout/base/nsPresContext.cpp
+++ b/layout/base/nsPresContext.cpp
@@ -52,17 +52,17 @@
 #include "nsSMILAnimationController.h"
 #include "mozilla/css/ImageLoader.h"
 #include "mozilla/dom/PBrowserParent.h"
 #include "mozilla/dom/TabChild.h"
 #include "mozilla/dom/TabParent.h"
 #include "nsRefreshDriver.h"
 #include "Layers.h"
 #include "ClientLayerManager.h"
-#include "nsIDOMEvent.h"
+#include "mozilla/dom/NotifyPaintEvent.h"
 #include "gfxPrefs.h"
 #include "nsIDOMChromeWindow.h"
 #include "nsFrameLoader.h"
 #include "mozilla/dom/FontFaceSet.h"
 #include "nsContentUtils.h"
 #include "nsPIWindowRoot.h"
 #include "mozilla/Preferences.h"
 #include "gfxTextRun.h"
@@ -2250,33 +2250,31 @@ nsPresContext::FireDOMPaintEvent(nsInval
     // automatically.)
     dispatchTarget = do_QueryInterface(ourWindow->GetParentTarget());
     if (!dispatchTarget) {
       return;
     }
   }
   // Events sent to the window get propagated to the chrome event handler
   // automatically.
-  nsCOMPtr<nsIDOMEvent> event;
+  //
   // This will empty our list in case dispatching the event causes more damage
   // (hopefully it won't, or we're likely to get an infinite loop! At least
   // it won't be blocking app execution though).
-  NS_NewDOMNotifyPaintEvent(getter_AddRefs(event), eventTarget, this, nullptr,
-                            NS_AFTERPAINT, aList);
-  if (!event) {
-    return;
-  }
+  nsRefPtr<NotifyPaintEvent> event =
+    NS_NewDOMNotifyPaintEvent(eventTarget, this, nullptr, NS_AFTERPAINT,
+                              aList);
 
   // Even if we're not telling the window about the event (so eventTarget is
   // the chrome event handler, not the window), the window is still
   // logically the event target.
   event->SetTarget(eventTarget);
   event->SetTrusted(true);
-  EventDispatcher::DispatchDOMEvent(dispatchTarget, nullptr, event, this,
-                                    nullptr);
+  EventDispatcher::DispatchDOMEvent(dispatchTarget, nullptr,
+                                    static_cast<Event*>(event), this, nullptr);
 }
 
 static bool
 MayHavePaintEventListenerSubdocumentCallback(nsIDocument* aDocument, void* aData)
 {
   bool *result = static_cast<bool*>(aData);
   nsIPresShell* shell = aDocument->GetShell();
   if (shell) {
--- a/layout/xul/nsMenuFrame.cpp
+++ b/layout/xul/nsMenuFrame.cpp
@@ -39,16 +39,17 @@
 #include "mozilla/EventStateManager.h"
 #include "mozilla/Likely.h"
 #include "mozilla/LookAndFeel.h"
 #include "mozilla/MouseEvents.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/Services.h"
 #include "mozilla/TextEvents.h"
 #include "mozilla/dom/Element.h"
+#include "mozilla/dom/Event.h"
 #include <algorithm>
 
 using namespace mozilla;
 
 #define NS_MENU_POPUP_LIST_INDEX 0
 
 #if defined(XP_WIN)
 #define NSCONTEXTMENUISMOUSEUP 1
@@ -89,27 +90,23 @@ public:
       domEventToFire.AssignLiteral("DOMMenuItemActive");
     }
     else {
       // Unhighlight the menu.
       mMenu->UnsetAttr(kNameSpaceID_None, nsGkAtoms::menuactive, true);
       domEventToFire.AssignLiteral("DOMMenuItemInactive");
     }
 
-    nsCOMPtr<nsIDOMEvent> event;
-    if (NS_SUCCEEDED(EventDispatcher::CreateEvent(mMenu, mPresContext, nullptr,
-                                                  NS_LITERAL_STRING("Events"),
-                                                  getter_AddRefs(event)))) {
-      event->InitEvent(domEventToFire, true, true);
+    nsRefPtr<Event> event = NS_NewDOMEvent(mMenu, mPresContext, nullptr);
+    event->InitEvent(domEventToFire, true, true);
 
-      event->SetTrusted(true);
+    event->SetTrusted(true);
 
-      EventDispatcher::DispatchDOMEvent(mMenu, nullptr, event,
-                                        mPresContext, nullptr);
-    }
+    EventDispatcher::DispatchDOMEvent(mMenu, nullptr, event,
+        mPresContext, nullptr);
 
     return NS_OK;
   }
 
 private:
   nsCOMPtr<nsIContent> mMenu;
   nsRefPtr<nsPresContext> mPresContext;
   bool mIsActivate;