Bug 720768 - add nsDOMEventTargetHelper::DispatchTrustedEvent; r=mounir, sr=smaug
authorNathan Froyd <froydnj@mozilla.com>
Thu, 27 Sep 2012 16:11:31 -0400
changeset 108769 ef201a4b3f37010396f8cb865f3d4496fd1232cc
parent 108768 48724698ca340cd4c2ed22d39f106f6e51ff7a8c
child 108770 589328aa176a962429c759e1e38b0d11793f03ec
push id23589
push userryanvm@gmail.com
push dateTue, 02 Oct 2012 01:31:11 +0000
treeherdermozilla-central@fb076a446870 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmounir, smaug
bugs720768
milestone18.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 720768 - add nsDOMEventTargetHelper::DispatchTrustedEvent; r=mounir, sr=smaug
content/base/src/nsInProcessTabChildGlobal.cpp
content/events/src/nsDOMEventTargetHelper.cpp
content/events/src/nsDOMEventTargetHelper.h
dom/base/nsScreen.cpp
dom/battery/BatteryManager.cpp
dom/battery/BatteryManager.h
dom/bluetooth/BluetoothAdapter.cpp
dom/bluetooth/BluetoothDevice.cpp
dom/bluetooth/BluetoothManager.cpp
dom/file/FileRequest.cpp
dom/fm/FMRadio.cpp
dom/fm/FMRadio.h
dom/icc/src/IccManager.cpp
dom/icc/src/IccManager.h
dom/network/src/Connection.cpp
dom/network/src/Connection.h
dom/network/src/MobileConnection.cpp
dom/network/src/MobileConnection.h
dom/sms/src/SmsManager.cpp
dom/telephony/Voicemail.cpp
--- a/content/base/src/nsInProcessTabChildGlobal.cpp
+++ b/content/base/src/nsInProcessTabChildGlobal.cpp
@@ -223,25 +223,17 @@ nsInProcessTabChildGlobal::Disconnect()
 
 void
 nsInProcessTabChildGlobal::DelayedDisconnect()
 {
   // Don't let the event escape
   mOwner = nullptr;
 
   // Fire the "unload" event
-  nsCOMPtr<nsIDOMEvent> event;
-  NS_NewDOMEvent(getter_AddRefs(event), nullptr, nullptr);
-  if (event) {
-    event->InitEvent(NS_LITERAL_STRING("unload"), false, false);
-    event->SetTrusted(true);
-
-    bool dummy;
-    nsDOMEventTargetHelper::DispatchEvent(event, &dummy);
-  }
+  nsDOMEventTargetHelper::DispatchTrustedEvent(NS_LITERAL_STRING("unload"));
 
   // Continue with the Disconnect cleanup
   nsCOMPtr<nsIDOMWindow> win = do_GetInterface(mDocShell);
   nsCOMPtr<nsPIDOMWindow> pwin = do_QueryInterface(win);
   if (pwin) {
     pwin->SetChromeEventHandler(pwin->GetChromeEventHandler());
   }
   mDocShell = nullptr;
--- a/content/events/src/nsDOMEventTargetHelper.cpp
+++ b/content/events/src/nsDOMEventTargetHelper.cpp
@@ -7,16 +7,17 @@
 #include "nsContentUtils.h"
 #include "nsEventDispatcher.h"
 #include "nsGUIEvent.h"
 #include "nsIDocument.h"
 #include "nsIJSContextStack.h"
 #include "nsDOMJSUtils.h"
 #include "prprf.h"
 #include "nsGlobalWindow.h"
+#include "nsDOMEvent.h"
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMEventTargetHelper)
 
 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsDOMEventTargetHelper)
   NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
 NS_IMPL_CYCLE_COLLECTION_TRACE_END
 
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsDOMEventTargetHelper)
@@ -200,16 +201,36 @@ nsDOMEventTargetHelper::DispatchEvent(ns
   nsresult rv =
     nsEventDispatcher::DispatchDOMEvent(this, nullptr, aEvent, nullptr, &status);
 
   *aRetVal = (status != nsEventStatus_eConsumeNoDefault);
   return rv;
 }
 
 nsresult
+nsDOMEventTargetHelper::DispatchTrustedEvent(const nsAString& aEventName)
+{
+  nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
+  nsresult rv = event->InitEvent(aEventName, false, false);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  return DispatchTrustedEvent(event);
+}
+
+nsresult
+nsDOMEventTargetHelper::DispatchTrustedEvent(nsIDOMEvent* event)
+{
+  nsresult rv = event->SetTrusted(true);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  bool dummy;
+  return DispatchEvent(event, &dummy);
+}
+
+nsresult
 nsDOMEventTargetHelper::SetEventHandler(nsIAtom* aType,
                                         JSContext* aCx,
                                         const JS::Value& aValue)
 {
   nsEventListenerManager* elm = GetListenerManager(true);
 
   JSObject* obj = GetWrapper();
   if (!obj) {
--- a/content/events/src/nsDOMEventTargetHelper.h
+++ b/content/events/src/nsDOMEventTargetHelper.h
@@ -14,16 +14,18 @@
 #include "nsPIDOMWindow.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsEventListenerManager.h"
 #include "nsIScriptContext.h"
 #include "nsWrapperCache.h"
 #include "mozilla/ErrorResult.h"
 #include "mozilla/Attributes.h"
 
+class nsDOMEvent;
+
 class nsDOMEventTargetHelper : public nsIDOMEventTarget,
                                public nsWrapperCache
 {
 public:
   nsDOMEventTargetHelper() : mOwner(nullptr), mHasOrHasHadOwner(false) {}
   virtual ~nsDOMEventTargetHelper();
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsDOMEventTargetHelper)
@@ -109,16 +111,20 @@ public:
 
   void BindToOwner(nsPIDOMWindow* aOwner);
   void BindToOwner(nsDOMEventTargetHelper* aOther);
   virtual void DisconnectFromOwner();                   
   nsPIDOMWindow* GetOwner() const { return mOwner; }
   bool HasOrHasHadOwner() { return mHasOrHasHadOwner; }
 protected:
   nsRefPtr<nsEventListenerManager> mListenerManager;
+  // Dispatch a trusted, non-cancellable and non-bubbling event to |this|.
+  nsresult DispatchTrustedEvent(const nsAString& aEventName);
+  // Make |event| trusted and dispatch |aEvent| to |this|.
+  nsresult DispatchTrustedEvent(nsIDOMEvent* aEvent);
 private:
   // These may be null (native callers or xpcshell).
   nsPIDOMWindow*             mOwner; // Inner window.
   bool                       mHasOrHasHadOwner;
 };
 
 #define NS_IMPL_EVENT_HANDLER(_class, _event)                                 \
     NS_IMETHODIMP _class::GetOn##_event(JSContext* aCx, JS::Value* aValue)    \
--- a/dom/base/nsScreen.cpp
+++ b/dom/base/nsScreen.cpp
@@ -278,33 +278,17 @@ nsScreen::Notify(const hal::ScreenConfig
 
   NS_ASSERTION(mOrientation == eScreenOrientation_PortraitPrimary ||
                mOrientation == eScreenOrientation_PortraitSecondary ||
                mOrientation == eScreenOrientation_LandscapePrimary ||
                mOrientation == eScreenOrientation_LandscapeSecondary,
                "Invalid orientation value passed to notify method!");
 
   if (mOrientation != previousOrientation) {
-    // TODO: use an helper method, see bug 720768.
-    nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
-    nsresult rv = event->InitEvent(NS_LITERAL_STRING("mozorientationchange"), false, false);
-    if (NS_FAILED(rv)) {
-      return;
-    }
-
-    rv = event->SetTrusted(true);
-    if (NS_FAILED(rv)) {
-      return;
-    }
-
-    bool dummy;
-    rv = DispatchEvent(event, &dummy);
-    if (NS_FAILED(rv)) {
-      return;
-    }
+    DispatchTrustedEvent(NS_LITERAL_STRING("mozorientationchange"));
   }
 }
 
 NS_IMETHODIMP
 nsScreen::GetMozOrientation(nsAString& aOrientation)
 {
   switch (mOrientation) {
     case eScreenOrientation_PortraitPrimary:
--- a/dom/battery/BatteryManager.cpp
+++ b/dom/battery/BatteryManager.cpp
@@ -113,33 +113,16 @@ BatteryManager::GetChargingTime(double* 
     return NS_OK;
   }
 
   *aChargingTime = mRemainingTime;
 
   return NS_OK;
 }
 
-nsresult
-BatteryManager::DispatchTrustedEventToSelf(const nsAString& aEventName)
-{
-  nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
-  nsresult rv = event->InitEvent(aEventName, false, false);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = event->SetTrusted(true);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  bool dummy;
-  rv = DispatchEvent(event, &dummy);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return NS_OK;
-}
-
 void
 BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
 {
   mLevel = aBatteryInfo.level();
   mCharging = aBatteryInfo.charging();
   mRemainingTime = aBatteryInfo.remainingTime();
 
   // Add some guards to make sure the values are coherent.
@@ -156,44 +139,44 @@ BatteryManager::Notify(const hal::Batter
 {
   double previousLevel = mLevel;
   bool previousCharging = mCharging;
   double previousRemainingTime = mRemainingTime;
 
   UpdateFromBatteryInfo(aBatteryInfo);
 
   if (previousCharging != mCharging) {
-    DispatchTrustedEventToSelf(CHARGINGCHANGE_EVENT_NAME);
+    DispatchTrustedEvent(CHARGINGCHANGE_EVENT_NAME);
   }
 
   if (previousLevel != mLevel) {
-    DispatchTrustedEventToSelf(LEVELCHANGE_EVENT_NAME);
+    DispatchTrustedEvent(LEVELCHANGE_EVENT_NAME);
   }
 
   /*
    * There are a few situations that could happen here:
    * 1. Charging state changed:
    *   a. Previous remaining time wasn't unkwonw, we have to fire an event for
    *      the change.
    *   b. New remaining time isn't unkwonw, we have to fire an event for it.
    * 2. Charging state didn't change but remainingTime did, we have to fire
    *    the event that correspond to the current charging state.
    */
   if (mCharging != previousCharging) {
     if (previousRemainingTime != kUnknownRemainingTime) {
-      DispatchTrustedEventToSelf(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME
-                                                  : DISCHARGINGTIMECHANGE_EVENT_NAME);
+      DispatchTrustedEvent(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME
+                                            : DISCHARGINGTIMECHANGE_EVENT_NAME);
     }
     if (mRemainingTime != kUnknownRemainingTime) {
-      DispatchTrustedEventToSelf(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
-                                           : DISCHARGINGTIMECHANGE_EVENT_NAME);
+      DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
+                                     : DISCHARGINGTIMECHANGE_EVENT_NAME);
     }
   } else if (previousRemainingTime != mRemainingTime) {
-    DispatchTrustedEventToSelf(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
-                                         : DISCHARGINGTIMECHANGE_EVENT_NAME);
+    DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
+                                   : DISCHARGINGTIMECHANGE_EVENT_NAME);
   }
 }
 
 /* static */ bool
 BatteryManager::HasSupport()
 {
   return Preferences::GetBool("dom.battery.enabled", true);
 }
--- a/dom/battery/BatteryManager.h
+++ b/dom/battery/BatteryManager.h
@@ -49,21 +49,16 @@ public:
    * Returns whether the battery api is supported (ie. not disabled by the user)
    * @return whether the battery api is supported.
    */
   static bool HasSupport();
 
 
 private:
   /**
-   * Dispatch a trusted non-cancellable and non-bubbling event to itself.
-   */
-  nsresult DispatchTrustedEventToSelf(const nsAString& aEventName);
-
-  /**
    * Update the battery information stored in the battery manager object using
    * a battery information object.
    */
   void UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo);
 
   double mLevel;
   bool   mCharging;
   /**
--- a/dom/bluetooth/BluetoothAdapter.cpp
+++ b/dom/bluetooth/BluetoothAdapter.cpp
@@ -272,47 +272,41 @@ BluetoothAdapter::Notify(const Bluetooth
   if (aData.name().EqualsLiteral("DeviceFound")) {
     nsRefPtr<BluetoothDevice> device = BluetoothDevice::Create(GetOwner(), mPath, aData.value());
     nsCOMPtr<nsIDOMEvent> event;
     NS_NewDOMBluetoothDeviceEvent(getter_AddRefs(event), nullptr, nullptr);
 
     nsCOMPtr<nsIDOMBluetoothDeviceEvent> e = do_QueryInterface(event);
     e->InitBluetoothDeviceEvent(NS_LITERAL_STRING("devicefound"),
                                 false, false, device);
-    e->SetTrusted(true);
-    bool dummy;
-    DispatchEvent(event, &dummy);
+    DispatchTrustedEvent(event);
   } else if (aData.name().EqualsLiteral("DeviceDisappeared")) {
     const nsAString& deviceAddress = aData.value().get_nsString();
 
     nsCOMPtr<nsIDOMEvent> event;
     NS_NewDOMBluetoothDeviceAddressEvent(getter_AddRefs(event), nullptr, nullptr);
 
     nsCOMPtr<nsIDOMBluetoothDeviceAddressEvent> e = do_QueryInterface(event);
     e->InitBluetoothDeviceAddressEvent(NS_LITERAL_STRING("devicedisappeared"),
                                        false, false, deviceAddress);
-    e->SetTrusted(true);
-    bool dummy;
-    DispatchEvent(event, &dummy);
+    DispatchTrustedEvent(e);
   } else if (aData.name().EqualsLiteral("DeviceCreated")) {
     NS_ASSERTION(aData.value().type() == BluetoothValue::TArrayOfBluetoothNamedValue,
                  "DeviceCreated: Invalid value type");
 
     nsRefPtr<BluetoothDevice> device = BluetoothDevice::Create(GetOwner(),
                                                                GetPath(),
                                                                aData.value());
     nsCOMPtr<nsIDOMEvent> event;
     NS_NewDOMBluetoothDeviceEvent(getter_AddRefs(event), nullptr, nullptr);
 
     nsCOMPtr<nsIDOMBluetoothDeviceEvent> e = do_QueryInterface(event);
     e->InitBluetoothDeviceEvent(NS_LITERAL_STRING("devicecreated"),
                                 false, false, device);
-    e->SetTrusted(true);
-    bool dummy;
-    DispatchEvent(event, &dummy);
+    DispatchTrustedEvent(e);
   } else if (aData.name().EqualsLiteral("PropertyChanged")) {
     NS_ASSERTION(aData.value().type() == BluetoothValue::TArrayOfBluetoothNamedValue,
                  "PropertyChanged: Invalid value type");
     arr = aData.value().get_ArrayOfBluetoothNamedValue();
 
     NS_ASSERTION(arr.Length() == 1, "Got more than one property in a change message!");
     BluetoothNamedValue v = arr[0];
 
--- a/dom/bluetooth/BluetoothDevice.cpp
+++ b/dom/bluetooth/BluetoothDevice.cpp
@@ -195,31 +195,18 @@ BluetoothDevice::Notify(const BluetoothS
     InfallibleTArray<BluetoothNamedValue> arr = aData.value().get_ArrayOfBluetoothNamedValue();
 
     NS_ASSERTION(arr.Length() == 1, "Got more than one property in a change message!");
     BluetoothNamedValue v = arr[0];
     nsString name = v.name();
 
     SetPropertyByValue(v);
     if (name.EqualsLiteral("Connected")) {
-      nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
-      nsresult rv;
-      if (mConnected) {
-        rv = event->InitEvent(NS_LITERAL_STRING("connected"), false, false);
-      } else {
-        rv = event->InitEvent(NS_LITERAL_STRING("disconnected"), false, false);
-      }
-      if (NS_FAILED(rv)) {
-        NS_WARNING("Failed to init the connected/disconnected event!!!");
-        return;
-      }
-
-      event->SetTrusted(true);
-      bool dummy;
-      DispatchEvent(event, &dummy);
+      DispatchTrustedEvent(mConnected ? NS_LITERAL_STRING("connected")
+                           : NS_LITERAL_STRING("disconnected"));
     } else {
       nsRefPtr<BluetoothPropertyEvent> e = BluetoothPropertyEvent::Create(name);
       e->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("propertychanged"));
     }
   } else {
 #ifdef DEBUG
     nsCString warningMsg;
     warningMsg.AssignLiteral("Not handling device signal: ");
--- a/dom/bluetooth/BluetoothManager.cpp
+++ b/dom/bluetooth/BluetoothManager.cpp
@@ -122,36 +122,18 @@ public:
 private:
   nsRefPtr<BluetoothManager> mManagerPtr;
   bool mEnabled;
 };
 
 nsresult
 BluetoothManager::FireEnabledDisabledEvent(bool aEnabled)
 {
-  nsString eventName;
-
-  if (aEnabled) {
-    eventName.AssignLiteral("enabled");
-  } else {
-    eventName.AssignLiteral("disabled");
-  }
-
-  nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
-  nsresult rv = event->InitEvent(eventName, false, false);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = event->SetTrusted(true);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  bool dummy;
-  rv = DispatchEvent(event, &dummy);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return NS_OK;
+  return DispatchTrustedEvent(aEnabled ? NS_LITERAL_STRING("enabled")
+                              : NS_LITERAL_STRING("disabled"));
 }
 
 BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow)
 : BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER)
 {
   MOZ_ASSERT(aWindow);
 
   BindToOwner(aWindow);
@@ -268,27 +250,17 @@ NS_NewBluetoothManager(nsPIDOMWindow* aW
   bluetoothManager.forget(aBluetoothManager);
   return NS_OK;
 }
 
 void
 BluetoothManager::Notify(const BluetoothSignal& aData)
 {
   if (aData.name().EqualsLiteral("AdapterAdded")) {
-    nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
-    nsresult rv = event->InitEvent(NS_LITERAL_STRING("adapteradded"), false, false);
-
-    if (NS_FAILED(rv)) {
-      NS_WARNING("Failed to init the adapteradded event!!!");
-      return;
-    }
-
-    event->SetTrusted(true);
-    bool dummy;
-    DispatchEvent(event, &dummy);
+    DispatchTrustedEvent(NS_LITERAL_STRING("adapteradded"));
   } else {
 #ifdef DEBUG
     nsCString warningMsg;
     warningMsg.AssignLiteral("Not handling manager signal: ");
     warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
     NS_WARNING(warningMsg.get());
 #endif
   }
--- a/dom/file/FileRequest.cpp
+++ b/dom/file/FileRequest.cpp
@@ -147,19 +147,10 @@ FileRequest::FireProgressEvent(uint64_t 
   nsCOMPtr<nsIDOMProgressEvent> progress = do_QueryInterface(event);
   MOZ_ASSERT(progress);
   rv = progress->InitProgressEvent(NS_LITERAL_STRING("progress"), false, false,
                                    false, aLoaded, aTotal);
   if (NS_FAILED(rv)) {
     return;
   }
 
-  rv = event->SetTrusted(true);
-  if (NS_FAILED(rv)) {
-    return;
-  }
-
-  bool dummy;
-  rv = DispatchEvent(event, &dummy);
-  if (NS_FAILED(rv)) {
-    return;
-  }
+  DispatchTrustedEvent(event);
 }
--- a/dom/fm/FMRadio.cpp
+++ b/dom/fm/FMRadio.cpp
@@ -195,47 +195,27 @@ NS_IMETHODIMP FMRadio::SetFrequency(int3
   return NS_OK;
 }
 
 void FMRadio::Notify(const SwitchEvent& aEvent)
 {
   if (mHeadphoneState != aEvent.status()) {
     LOG("Antenna state is changed!");
     mHeadphoneState = aEvent.status();
-    DispatchTrustedEventToSelf(ANTENNA_STATE_CHANGED_EVENT_NAME);
+    DispatchTrustedEvent(ANTENNA_STATE_CHANGED_EVENT_NAME);
   }
 }
 
 void FMRadio::Notify(const FMRadioOperationInformation& info)
 {
   switch (info.operation())
   {
     case FM_RADIO_OPERATION_ENABLE:
-      DispatchTrustedEventToSelf(RADIO_ENABLED_EVENT_NAME);
+      DispatchTrustedEvent(RADIO_ENABLED_EVENT_NAME);
       break;
     case FM_RADIO_OPERATION_DISABLE:
-      DispatchTrustedEventToSelf(RADIO_DIABLED_EVENT_NAME);
+      DispatchTrustedEvent(RADIO_DIABLED_EVENT_NAME);
       break;
     case FM_RADIO_OPERATION_SEEK:
-      DispatchTrustedEventToSelf(RADIO_SEEK_COMPLETE_EVENT_NAME);
+      DispatchTrustedEvent(RADIO_SEEK_COMPLETE_EVENT_NAME);
       break;
   }
 }
-
-nsresult
-FMRadio::DispatchTrustedEventToSelf(const nsAString& aEventName)
-{
-  nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
-  nsresult rv = event->InitEvent(aEventName,
-                                 /* bubbles = */ false,
-                                 /* cancelable = */ false);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = event->SetTrusted(true);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  bool dummy;
-  rv = DispatchEvent(event, &dummy);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return NS_OK;
-}
-
--- a/dom/fm/FMRadio.h
+++ b/dom/fm/FMRadio.h
@@ -38,19 +38,15 @@ public:
   FMRadio();
   virtual void Notify(const hal::FMRadioOperationInformation& info);
   virtual void Notify(const hal::SwitchEvent& aEvent);
 
 private:
   ~FMRadio();
   bool mHasInternalAntenna;
   hal::SwitchState mHeadphoneState;
-  /**
-   * Dispatch a trusted non-cancellable and no-bubbling event to itself
-   */
-  nsresult DispatchTrustedEventToSelf(const nsAString& aEventName);
 };
 
 } // namespace fm
 } // namespace dom
 } // namespace mozilla
 #endif
 
--- a/dom/icc/src/IccManager.cpp
+++ b/dom/icc/src/IccManager.cpp
@@ -98,17 +98,17 @@ IccManager::Observe(nsISupports* aSubjec
 
     nsresult rv = event->Dispatch(ToIDOMEventTarget(), STKCOMMAND_EVENTNAME);
     NS_ENSURE_SUCCESS(rv, rv);
 
     return NS_OK;
   }
 
   if (!strcmp(aTopic, kStkSessionEndTopic)) {
-    InternalDispatchEvent(STKSESSIONEND_EVENTNAME);
+    DispatchTrustedEvent(STKSESSIONEND_EVENTNAME);
     return NS_OK;
   }
 
   MOZ_NOT_REACHED("Unknown observer topic!");
 
   return NS_OK;
 }
 
@@ -143,32 +143,14 @@ IccManager::SendStkEventDownload(const J
   if (!mProvider) {
     return NS_ERROR_FAILURE;
   }
 
   mProvider->SendStkEventDownload(GetOwner(), aEvent);
   return NS_OK;
 }
 
-nsresult
-IccManager::InternalDispatchEvent(const nsAString& aType)
-{
-  nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
-  nsresult rv = event->InitEvent(aType, false, false);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = event->SetTrusted(true);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  bool dummy;
-  rv = DispatchEvent(event, &dummy);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return NS_OK;
-}
-
-
 NS_IMPL_EVENT_HANDLER(IccManager, stkcommand)
 NS_IMPL_EVENT_HANDLER(IccManager, stksessionend)
 
 } // namespace icc
 } // namespace dom
 } // namespace mozilla
--- a/dom/icc/src/IccManager.h
+++ b/dom/icc/src/IccManager.h
@@ -38,17 +38,15 @@ private:
   nsCOMPtr<nsIMobileConnectionProvider> mProvider;
 
   nsIDOMEventTarget*
   ToIDOMEventTarget() const
   {
     return static_cast<nsDOMEventTargetHelper*>(
            const_cast<IccManager*>(this));
   }
-
-  nsresult InternalDispatchEvent(const nsAString& aType);
 };
 
 } // namespace icc
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_icc_IccManager_h
--- a/dom/network/src/Connection.cpp
+++ b/dom/network/src/Connection.cpp
@@ -92,33 +92,16 @@ Connection::GetMetered(bool* aMetered)
     return NS_OK;
   }
 
   *aMetered = Preferences::GetBool(sMeteredPrefName,
                                    sMeteredDefaultValue);
   return NS_OK;
 }
 
-nsresult
-Connection::DispatchTrustedEventToSelf(const nsAString& aEventName)
-{
-  nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
-  nsresult rv = event->InitEvent(aEventName, false, false);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = event->SetTrusted(true);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  bool dummy;
-  rv = DispatchEvent(event, &dummy);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return NS_OK;
-}
-
 void
 Connection::UpdateFromNetworkInfo(const hal::NetworkInformation& aNetworkInfo)
 {
   mBandwidth = aNetworkInfo.bandwidth();
   mCanBeMetered = aNetworkInfo.canBeMetered();
 }
 
 void
@@ -129,15 +112,15 @@ Connection::Notify(const hal::NetworkInf
 
   UpdateFromNetworkInfo(aNetworkInfo);
 
   if (previousBandwidth == mBandwidth &&
       previousCanBeMetered == mCanBeMetered) {
     return;
   }
 
-  DispatchTrustedEventToSelf(CHANGE_EVENT_NAME);
+  DispatchTrustedEvent(CHANGE_EVENT_NAME);
 }
 
 } // namespace network
 } // namespace dom
 } // namespace mozilla
 
--- a/dom/network/src/Connection.h
+++ b/dom/network/src/Connection.h
@@ -39,21 +39,16 @@ public:
   // For IObserver
   void Notify(const hal::NetworkInformation& aNetworkInfo);
 
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Connection,
                                            nsDOMEventTargetHelper)
 
 private:
   /**
-   * Dispatch a trusted non-cancellable and non-bubbling event to itself.
-   */
-  nsresult DispatchTrustedEventToSelf(const nsAString& aEventName);
-
-  /**
    * Update the connection information stored in the object using a
    * NetworkInformation object.
    */
   void UpdateFromNetworkInfo(const hal::NetworkInformation& aNetworkInfo);
 
   /**
    * If the connection is of a type that can be metered.
    */
--- a/dom/network/src/MobileConnection.cpp
+++ b/dom/network/src/MobileConnection.cpp
@@ -120,32 +120,32 @@ MobileConnection::Shutdown()
 // nsIObserver
 
 NS_IMETHODIMP
 MobileConnection::Observe(nsISupports* aSubject,
                           const char* aTopic,
                           const PRUnichar* aData)
 {
   if (!strcmp(aTopic, kVoiceChangedTopic)) {
-    InternalDispatchEvent(VOICECHANGE_EVENTNAME);
+    DispatchTrustedEvent(VOICECHANGE_EVENTNAME);
     return NS_OK;
   }
 
   if (!strcmp(aTopic, kDataChangedTopic)) {
-    InternalDispatchEvent(DATACHANGE_EVENTNAME);
+    DispatchTrustedEvent(DATACHANGE_EVENTNAME);
     return NS_OK;
   }
 
   if (!strcmp(aTopic, kCardStateChangedTopic)) {
-    InternalDispatchEvent(CARDSTATECHANGE_EVENTNAME);
+    DispatchTrustedEvent(CARDSTATECHANGE_EVENTNAME);
     return NS_OK;
   }
 
   if (!strcmp(aTopic, kIccInfoChangedTopic)) {
-    InternalDispatchEvent(ICCINFOCHANGE_EVENTNAME);
+    DispatchTrustedEvent(ICCINFOCHANGE_EVENTNAME);
     return NS_OK;
   }
 
   if (!strcmp(aTopic, kUssdReceivedTopic)) {
     nsString ussd;
     ussd.Assign(aData);
     nsRefPtr<USSDReceivedEvent> event = USSDReceivedEvent::Create(ussd);
     NS_ASSERTION(event, "This should never fail!");
@@ -319,28 +319,11 @@ MobileConnection::CancelUSSD(nsIDOMDOMRe
 {
   if (!mProvider) {
     return NS_ERROR_FAILURE;
   }
 
   return mProvider->CancelUSSD(GetOwner(), request);
 }
 
-nsresult
-MobileConnection::InternalDispatchEvent(const nsAString& aType)
-{
-  nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
-  nsresult rv = event->InitEvent(aType, false, false);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = event->SetTrusted(true);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  bool dummy;
-  rv = DispatchEvent(event, &dummy);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return NS_OK;
-}
-
 } // namespace network
 } // namespace dom
 } // namespace mozilla
--- a/dom/network/src/MobileConnection.h
+++ b/dom/network/src/MobileConnection.h
@@ -45,17 +45,15 @@ private:
   nsRefPtr<icc::IccManager> mIccManager;
 
   nsIDOMEventTarget*
   ToIDOMEventTarget() const
   {
     return static_cast<nsDOMEventTargetHelper*>(
            const_cast<MobileConnection*>(this));
   }
-
-  nsresult InternalDispatchEvent(const nsAString& aType);
 };
 
 } // namespace network
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_network_MobileConnection_h
--- a/dom/sms/src/SmsManager.cpp
+++ b/dom/sms/src/SmsManager.cpp
@@ -347,24 +347,17 @@ SmsManager::MarkMessageRead(int32_t aId,
 nsresult
 SmsManager::DispatchTrustedSmsEventToSelf(const nsAString& aEventName, nsIDOMMozSmsMessage* aMessage)
 {
   nsRefPtr<nsDOMEvent> event = new SmsEvent(nullptr, nullptr);
   nsresult rv = static_cast<SmsEvent*>(event.get())->Init(aEventName, false,
                                                           false, aMessage);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = event->SetTrusted(true);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  bool dummy;
-  rv = DispatchEvent(event, &dummy);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return NS_OK;
+  return DispatchTrustedEvent(event);
 }
 
 NS_IMETHODIMP
 SmsManager::Observe(nsISupports* aSubject, const char* aTopic,
                     const PRUnichar* aData)
 {
   if (!strcmp(aTopic, kSmsReceivedObserverTopic)) {
     nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
--- a/dom/telephony/Voicemail.cpp
+++ b/dom/telephony/Voicemail.cpp
@@ -95,24 +95,17 @@ NS_IMPL_EVENT_HANDLER(Voicemail, statusc
 NS_IMETHODIMP
 Voicemail::VoicemailNotification(nsIDOMMozVoicemailStatus* aStatus)
 {
   nsRefPtr<VoicemailEvent> event = new VoicemailEvent(nullptr, nullptr);
   nsresult rv = event->InitVoicemailEvent(NS_LITERAL_STRING("statuschanged"),
                                           false, false, aStatus);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = event->SetTrusted(true);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  bool dummy;
-  rv = DispatchEvent(static_cast<nsIDOMMozVoicemailEvent*>(event), &dummy);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return NS_OK;
+  return DispatchTrustedEvent(static_cast<nsIDOMMozVoicemailEvent*>(event));
 }
 
 nsresult
 NS_NewVoicemail(nsPIDOMWindow* aWindow, nsIDOMMozVoicemail** aVoicemail)
 {
   nsPIDOMWindow* innerWindow = aWindow->IsInnerWindow() ?
     aWindow :
     aWindow->GetCurrentInnerWindow();