Bug 950315 part 1. Convert a bunch of event targets to passing a Window or nsDOMEventTargetHelper directly to the constructor of their ancestor nsDOMEventTargetHelper. r=smaug
authorBoris Zbarsky <bzbarsky@mit.edu>
Mon, 06 Jan 2014 21:53:23 -0500
changeset 162315 e9d6fd04d08e932028717fa5c0b09a4b04fc5118
parent 162314 fc1b037918d985f918c28a4ab485dbefa67d22fc
child 162316 451513e0b68cf053409209679e5b9e18c0362e22
push idunknown
push userunknown
push dateunknown
reviewerssmaug
bugs950315
milestone29.0a1
Bug 950315 part 1. Convert a bunch of event targets to passing a Window or nsDOMEventTargetHelper directly to the constructor of their ancestor nsDOMEventTargetHelper. r=smaug
content/base/src/EventSource.cpp
content/base/src/EventSource.h
content/base/src/WebSocket.cpp
content/base/src/WebSocket.h
content/base/src/nsDOMDataChannel.cpp
content/base/src/nsDOMDataChannel.h
content/base/src/nsXMLHttpRequest.h
content/events/src/nsDOMEventTargetHelper.cpp
content/events/src/nsDOMEventTargetHelper.h
content/media/MediaRecorder.cpp
content/media/MediaRecorder.h
content/media/webaudio/AudioContext.cpp
content/media/webaudio/AudioNode.cpp
content/media/webspeech/recognition/SpeechRecognition.cpp
content/media/webspeech/recognition/SpeechRecognition.h
content/media/webspeech/synth/SpeechSynthesisUtterance.cpp
content/media/webspeech/synth/SpeechSynthesisUtterance.h
dom/base/MessagePort.cpp
dom/base/Navigator.cpp
dom/base/nsScreen.cpp
dom/base/nsScreen.h
dom/battery/BatteryManager.cpp
dom/battery/BatteryManager.h
dom/devicestorage/DeviceStorage.h
dom/devicestorage/nsDeviceStorage.cpp
dom/file/DOMFileHandle.cpp
dom/file/DOMFileHandle.h
dom/file/FileHandle.h
dom/indexedDB/IDBFileHandle.cpp
dom/indexedDB/IDBFileHandle.h
dom/src/notification/DesktopNotification.cpp
dom/src/notification/Notification.cpp
dom/src/notification/Notification.h
dom/src/offline/nsDOMOfflineResourceList.cpp
dom/telephony/Telephony.cpp
dom/telephony/Telephony.h
dom/telephony/TelephonyCall.cpp
dom/telephony/TelephonyCall.h
dom/telephony/TelephonyCallGroup.cpp
dom/telephony/TelephonyCallGroup.h
layout/build/nsLayoutModule.cpp
--- a/content/base/src/EventSource.cpp
+++ b/content/base/src/EventSource.cpp
@@ -50,30 +50,30 @@ namespace dom {
 #define DEFAULT_BUFFER_SIZE 4096
 
 // Reconnection time related values in milliseconds. The default one is equal
 // to the default value of the pref dom.server-events.default-reconnection-time
 #define MIN_RECONNECTION_TIME_VALUE       500
 #define DEFAULT_RECONNECTION_TIME_VALUE   5000
 #define MAX_RECONNECTION_TIME_VALUE       PR_IntervalToMilliseconds(DELAY_INTERVAL_LIMIT)
 
-EventSource::EventSource() :
+EventSource::EventSource(nsPIDOMWindow* aOwnerWindow) :
+  nsDOMEventTargetHelper(aOwnerWindow),
   mStatus(PARSE_STATE_OFF),
   mFrozen(false),
   mErrorLoadOnRedirect(false),
   mGoingToDispatchAllMessages(false),
   mWithCredentials(false),
   mWaitingForOnStopRequest(false),
   mInterrupted(false),
   mLastConvertionResult(NS_OK),
   mReadyState(CONNECTING),
   mScriptLine(0),
   mInnerWindowID(0)
 {
-  SetIsDOMBinding();
 }
 
 EventSource::~EventSource()
 {
   Close();
 }
 
 //-----------------------------------------------------------------------------
@@ -181,34 +181,29 @@ nsresult
 EventSource::Init(nsISupports* aOwner,
                   const nsAString& aURL,
                   bool aWithCredentials)
 {
   if (mReadyState != CONNECTING || !PrefEnabled()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
-  nsCOMPtr<nsPIDOMWindow> ownerWindow = do_QueryInterface(aOwner);
-  NS_ENSURE_STATE(ownerWindow);
-  MOZ_ASSERT(ownerWindow->IsInnerWindow());
-
   nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aOwner);
   NS_ENSURE_STATE(sgo);
   nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
   NS_ENSURE_STATE(scriptContext);
 
   nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal =
     do_QueryInterface(aOwner);
   NS_ENSURE_STATE(scriptPrincipal);
   nsCOMPtr<nsIPrincipal> principal = scriptPrincipal->GetPrincipal();
   NS_ENSURE_STATE(principal);
 
   mPrincipal = principal;
   mWithCredentials = aWithCredentials;
-  BindToOwner(ownerWindow);
 
   // The conditional here is historical and not necessarily sane.
   if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
     const char *filename;
     if (nsJSUtils::GetCallingLocation(cx, &filename, &mScriptLine)) {
       mScriptFile.AssignASCII(filename);
     }
 
@@ -281,17 +276,25 @@ EventSource::WrapObject(JSContext* aCx, 
 }
 
 /* static */ already_AddRefed<EventSource>
 EventSource::Constructor(const GlobalObject& aGlobal,
                          const nsAString& aURL,
                          const EventSourceInit& aEventSourceInitDict,
                          ErrorResult& aRv)
 {
-  nsRefPtr<EventSource> eventSource = new EventSource();
+  nsCOMPtr<nsPIDOMWindow> ownerWindow =
+    do_QueryInterface(aGlobal.GetAsSupports());
+  if (!ownerWindow) {
+    aRv.Throw(NS_ERROR_UNEXPECTED);
+    return nullptr;
+  }
+  MOZ_ASSERT(ownerWindow->IsInnerWindow());
+
+  nsRefPtr<EventSource> eventSource = new EventSource(ownerWindow);
   aRv = eventSource->Init(aGlobal.GetAsSupports(), aURL,
                           aEventSourceInitDict.mWithCredentials);
   return eventSource.forget();
 }
 
 //-----------------------------------------------------------------------------
 // EventSource::nsIObserver
 //-----------------------------------------------------------------------------
--- a/content/base/src/EventSource.h
+++ b/content/base/src/EventSource.h
@@ -20,23 +20,16 @@
 #include "nsIChannelEventSink.h"
 #include "nsIInterfaceRequestor.h"
 #include "nsITimer.h"
 #include "nsIHttpChannel.h"
 #include "nsWeakReference.h"
 #include "nsDeque.h"
 #include "nsIUnicodeDecoder.h"
 
-#define NS_EVENTSOURCE_CID                          \
- { /* 755e2d2d-a836-4539-83f4-16b51156341f */       \
-  0x755e2d2d, 0xa836, 0x4539,                       \
- {0x83, 0xf4, 0x16, 0xb5, 0x11, 0x56, 0x34, 0x1f} }
-
-#define NS_EVENTSOURCE_CONTRACTID "@mozilla.org/eventsource;1"
-
 class nsPIDOMWindow;
 
 namespace mozilla {
 
 class ErrorResult;
 
 namespace dom {
 
@@ -48,17 +41,17 @@ class EventSource : public nsDOMEventTar
                   , public nsIStreamListener
                   , public nsIChannelEventSink
                   , public nsIInterfaceRequestor
                   , public nsSupportsWeakReference
 {
 friend class AsyncVerifyRedirectCallbackFwr;
 
 public:
-  EventSource();
+  EventSource(nsPIDOMWindow* aOwnerWindow);
   virtual ~EventSource();
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(EventSource,
                                                                    nsDOMEventTargetHelper)
 
   NS_DECL_NSIOBSERVER
   NS_DECL_NSISTREAMLISTENER
   NS_DECL_NSIREQUESTOBSERVER
--- a/content/base/src/WebSocket.cpp
+++ b/content/base/src/WebSocket.cpp
@@ -437,33 +437,34 @@ WebSocket::GetInterface(const nsIID& aII
 
   return QueryInterface(aIID, aResult);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 // WebSocket
 ////////////////////////////////////////////////////////////////////////////////
 
-WebSocket::WebSocket()
-: mKeepingAlive(false),
+WebSocket::WebSocket(nsPIDOMWindow* aOwnerWindow)
+: nsDOMEventTargetHelper(aOwnerWindow),
+  mKeepingAlive(false),
   mCheckMustKeepAlive(true),
   mOnCloseScheduled(false),
   mFailed(false),
   mDisconnected(false),
   mCloseEventWasClean(false),
   mCloseEventCode(nsIWebSocketChannel::CLOSE_ABNORMAL),
   mReadyState(WebSocket::CONNECTING),
   mOutgoingBufferedAmount(0),
   mBinaryType(dom::BinaryType::Blob),
   mScriptLine(0),
   mInnerWindowID(0)
 {
   NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
-
-  SetIsDOMBinding();
+  MOZ_ASSERT(aOwnerWindow);
+  MOZ_ASSERT(aOwnerWindow->IsInnerWindow());
 }
 
 WebSocket::~WebSocket()
 {
   NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
 
   // If we threw during Init we never called disconnect
   if (!mDisconnected) {
@@ -555,18 +556,18 @@ WebSocket::Constructor(const GlobalObjec
     if (protocolElement.FindChar(',') != -1)  /* interferes w/list */ {
       aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
       return nullptr;
     }
 
     protocolArray.AppendElement(protocolElement);
   }
 
-  nsRefPtr<WebSocket> webSocket = new WebSocket();
-  nsresult rv = webSocket->Init(aGlobal.GetContext(), principal, ownerWindow,
+  nsRefPtr<WebSocket> webSocket = new WebSocket(ownerWindow);
+  nsresult rv = webSocket->Init(aGlobal.GetContext(), principal,
                                 aUrl, protocolArray);
   if (NS_FAILED(rv)) {
     aRv.Throw(rv);
     return nullptr;
   }
 
   return webSocket.forget();
 }
@@ -635,31 +636,27 @@ WebSocket::DisconnectFromOwner()
 
 //-----------------------------------------------------------------------------
 // WebSocket:: initialization
 //-----------------------------------------------------------------------------
 
 nsresult
 WebSocket::Init(JSContext* aCx,
                 nsIPrincipal* aPrincipal,
-                nsPIDOMWindow* aOwnerWindow,
                 const nsAString& aURL,
                 nsTArray<nsString>& aProtocolArray)
 {
   NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
   MOZ_ASSERT(aPrincipal);
-  MOZ_ASSERT(aOwnerWindow);
-  MOZ_ASSERT(aOwnerWindow->IsInnerWindow());
 
   if (!PrefEnabled()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   mPrincipal = aPrincipal;
-  BindToOwner(aOwnerWindow);
 
   // Attempt to kill "ghost" websocket: but usually too early for check to fail
   nsresult rv = CheckInnerWindowCorrectness();
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Shut down websocket if window is frozen or destroyed (only needed for
   // "ghost" websockets--see bug 696085)
   nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
--- a/content/base/src/WebSocket.h
+++ b/content/base/src/WebSocket.h
@@ -135,23 +135,22 @@ public: // WebIDL interface:
   void Send(nsIDOMBlob* aData,
             ErrorResult& aRv);
   void Send(const ArrayBuffer& aData,
             ErrorResult& aRv);
   void Send(const ArrayBufferView& aData,
             ErrorResult& aRv);
 
 private: // constructor && distructor
-  WebSocket();
+  WebSocket(nsPIDOMWindow* aOwnerWindow);
   virtual ~WebSocket();
 
 protected:
   nsresult Init(JSContext* aCx,
                 nsIPrincipal* aPrincipal,
-                nsPIDOMWindow* aOwnerWindow,
                 const nsAString& aURL,
                 nsTArray<nsString>& aProtocolArray);
 
   void Send(nsIInputStream* aMsgStream,
             const nsACString& aMsgString,
             uint32_t aMsgLength,
             bool aIsBinary,
             ErrorResult& aRv);
--- a/content/base/src/nsDOMDataChannel.cpp
+++ b/content/base/src/nsDOMDataChannel.cpp
@@ -75,21 +75,23 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 
 NS_IMPL_ADDREF_INHERITED(nsDOMDataChannel, nsDOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(nsDOMDataChannel, nsDOMEventTargetHelper)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMDataChannel)
   NS_INTERFACE_MAP_ENTRY(nsIDOMDataChannel)
 NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
 
-nsDOMDataChannel::nsDOMDataChannel(already_AddRefed<mozilla::DataChannel> aDataChannel)
-  : mDataChannel(aDataChannel)
+nsDOMDataChannel::nsDOMDataChannel(already_AddRefed<mozilla::DataChannel> aDataChannel,
+                                   nsPIDOMWindow* aWindow)
+  : nsDOMEventTargetHelper(aWindow && aWindow->IsOuterWindow() ?
+                             aWindow->GetCurrentInnerWindow() : aWindow)
+  , mDataChannel(aDataChannel)
   , mBinaryType(DC_BINARY_TYPE_BLOB)
 {
-  SetIsDOMBinding();
 }
 
 nsresult
 nsDOMDataChannel::Init(nsPIDOMWindow* aDOMWindow)
 {
   nsresult rv;
   nsAutoString urlParam;
 
@@ -102,23 +104,16 @@ nsDOMDataChannel::Init(nsPIDOMWindow* aD
   nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
   NS_ENSURE_STATE(scriptContext);
 
   nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal(do_QueryInterface(aDOMWindow));
   NS_ENSURE_STATE(scriptPrincipal);
   nsCOMPtr<nsIPrincipal> principal = scriptPrincipal->GetPrincipal();
   NS_ENSURE_STATE(principal);
 
-  if (aDOMWindow) {
-    BindToOwner(aDOMWindow->IsOuterWindow() ?
-                aDOMWindow->GetCurrentInnerWindow() : aDOMWindow);
-  } else {
-    BindToOwner(aDOMWindow);
-  }
-
   // Attempt to kill "ghost" DataChannel (if one can happen): but usually too early for check to fail
   rv = CheckInnerWindowCorrectness();
   NS_ENSURE_SUCCESS(rv,rv);
 
   rv = nsContentUtils::GetUTFOrigin(principal,mOrigin);
   LOG(("%s: origin = %s\n",__FUNCTION__,NS_LossyConvertUTF16toASCII(mOrigin).get()));
   return rv;
 }
@@ -494,17 +489,18 @@ nsDOMDataChannel::AppReady()
 }
 
 /* static */
 nsresult
 NS_NewDOMDataChannel(already_AddRefed<mozilla::DataChannel> aDataChannel,
                      nsPIDOMWindow* aWindow,
                      nsIDOMDataChannel** aDomDataChannel)
 {
-  nsRefPtr<nsDOMDataChannel> domdc = new nsDOMDataChannel(aDataChannel);
+  nsRefPtr<nsDOMDataChannel> domdc =
+    new nsDOMDataChannel(aDataChannel, aWindow);
 
   nsresult rv = domdc->Init(aWindow);
   NS_ENSURE_SUCCESS(rv,rv);
 
   return CallQueryInterface(domdc, aDomDataChannel);
 }
 
 /* static */
--- a/content/base/src/nsDOMDataChannel.h
+++ b/content/base/src/nsDOMDataChannel.h
@@ -20,17 +20,18 @@ namespace mozilla {
 class DataChannel;
 };
 
 class nsDOMDataChannel : public nsDOMEventTargetHelper,
                          public nsIDOMDataChannel,
                          public mozilla::DataChannelListener
 {
 public:
-  nsDOMDataChannel(already_AddRefed<mozilla::DataChannel> aDataChannel);
+  nsDOMDataChannel(already_AddRefed<mozilla::DataChannel> aDataChannel,
+                   nsPIDOMWindow* aWindow);
   ~nsDOMDataChannel();
 
   nsresult Init(nsPIDOMWindow* aDOMWindow);
 
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_NSIDOMDATACHANNEL
 
   NS_REALLY_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper)
--- a/content/base/src/nsXMLHttpRequest.h
+++ b/content/base/src/nsXMLHttpRequest.h
@@ -95,16 +95,26 @@ protected:
                                     const uint8_t* aStart2, uint32_t aLength2);
 };
 
 } // namespace mozilla
 
 class nsXHREventTarget : public nsDOMEventTargetHelper,
                          public nsIXMLHttpRequestEventTarget
 {
+protected:
+  nsXHREventTarget(nsDOMEventTargetHelper* aOwner)
+    : nsDOMEventTargetHelper(aOwner)
+  {
+  }
+
+  nsXHREventTarget()
+  {
+  }
+
 public:
   typedef mozilla::dom::XMLHttpRequestResponseType
           XMLHttpRequestResponseType;
   typedef mozilla::ErrorResult
           ErrorResult;
 
   virtual ~nsXHREventTarget() {}
   NS_DECL_ISUPPORTS_INHERITED
@@ -124,20 +134,20 @@ public:
   virtual void DisconnectFromOwner();
 };
 
 class nsXMLHttpRequestUpload MOZ_FINAL : public nsXHREventTarget,
                                          public nsIXMLHttpRequestUpload
 {
 public:
   nsXMLHttpRequestUpload(nsDOMEventTargetHelper* aOwner)
+    : nsXHREventTarget(aOwner)
   {
-    BindToOwner(aOwner);
-    SetIsDOMBinding();
   }
+
   NS_DECL_ISUPPORTS_INHERITED
   NS_FORWARD_NSIXMLHTTPREQUESTEVENTTARGET(nsXHREventTarget::)
   NS_REALLY_FORWARD_NSIDOMEVENTTARGET(nsXHREventTarget)
   NS_DECL_NSIXMLHTTPREQUESTUPLOAD
 
   virtual JSObject* WrapObject(JSContext *cx,
                                JS::Handle<JSObject*> scope) MOZ_OVERRIDE;
   nsISupports* GetParentObject()
--- a/content/events/src/nsDOMEventTargetHelper.cpp
+++ b/content/events/src/nsDOMEventTargetHelper.cpp
@@ -84,16 +84,17 @@ nsDOMEventTargetHelper::~nsDOMEventTarge
     mListenerManager->Disconnect();
   }
   ReleaseWrapper(this);
 }
 
 void
 nsDOMEventTargetHelper::BindToOwner(nsPIDOMWindow* aOwner)
 {
+  MOZ_ASSERT(!aOwner || aOwner->IsInnerWindow());
   nsCOMPtr<nsIGlobalObject> glob = do_QueryInterface(aOwner);
   BindToOwner(glob);
 }
 
 void
 nsDOMEventTargetHelper::BindToOwner(nsIGlobalObject* aOwner)
 {
   if (mParentObject) {
--- a/content/events/src/nsDOMEventTargetHelper.h
+++ b/content/events/src/nsDOMEventTargetHelper.h
@@ -38,16 +38,25 @@ public:
     : mParentObject(nullptr)
     , mOwnerWindow(nullptr)
     , mHasOrHasHadOwnerWindow(false)
   {
     BindToOwner(aWindow);
     // All objects coming through here are WebIDL objects
     SetIsDOMBinding();
   }
+  nsDOMEventTargetHelper(nsDOMEventTargetHelper* aOther)
+    : mParentObject(nullptr)
+    , mOwnerWindow(nullptr)
+    , mHasOrHasHadOwnerWindow(false)
+  {
+    BindToOwner(aOther);
+    // All objects coming through here are WebIDL objects
+    SetIsDOMBinding();
+  }
 
   virtual ~nsDOMEventTargetHelper();
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsDOMEventTargetHelper)
 
   NS_DECL_NSIDOMEVENTTARGET
 
   virtual nsEventListenerManager*
--- a/content/media/MediaRecorder.cpp
+++ b/content/media/MediaRecorder.cpp
@@ -409,31 +409,25 @@ private:
 
 NS_IMPL_ISUPPORTS1(MediaRecorder::Session, nsIObserver)
 
 MediaRecorder::~MediaRecorder()
 {
   MOZ_ASSERT(mSession == nullptr);
 }
 
-void
-MediaRecorder::Init(nsPIDOMWindow* aOwnerWindow)
+MediaRecorder::MediaRecorder(DOMMediaStream& aStream, nsPIDOMWindow* aOwnerWindow)
+  : nsDOMEventTargetHelper(aOwnerWindow),
+    mState(RecordingState::Inactive),
+    mSession(nullptr),
+    mMutex("Session.Data.Mutex")
 {
   MOZ_ASSERT(aOwnerWindow);
   MOZ_ASSERT(aOwnerWindow->IsInnerWindow());
-  BindToOwner(aOwnerWindow);
-}
-
-MediaRecorder::MediaRecorder(DOMMediaStream& aStream)
-  : mState(RecordingState::Inactive),
-    mSession(nullptr),
-    mMutex("Session.Data.Mutex")
-{
   mStream = &aStream;
-  SetIsDOMBinding();
 }
 
 void
 MediaRecorder::SetMimeType(const nsString &aMimeType)
 {
   MutexAutoLock lock(mMutex);
   mMimeType = aMimeType;
 }
@@ -560,18 +554,17 @@ MediaRecorder::Constructor(const GlobalO
   }
 
   nsCOMPtr<nsPIDOMWindow> ownerWindow = do_QueryInterface(aGlobal.GetAsSupports());
   if (!ownerWindow) {
     aRv.Throw(NS_ERROR_FAILURE);
     return nullptr;
   }
 
-  nsRefPtr<MediaRecorder> object = new MediaRecorder(aStream);
-  object->Init(ownerWindow);
+  nsRefPtr<MediaRecorder> object = new MediaRecorder(aStream, ownerWindow);
   return object.forget();
 }
 
 nsresult
 MediaRecorder::CreateAndDispatchBlobEvent(Session *aSession)
 {
   NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
 
--- a/content/media/MediaRecorder.h
+++ b/content/media/MediaRecorder.h
@@ -34,17 +34,17 @@ namespace dom {
  * Also extract the encoded data and create blobs on every timeslice passed from start function or RequestData function called by UA.
  */
 
 class MediaRecorder : public nsDOMEventTargetHelper
 {
   class Session;
 
 public:
-  MediaRecorder(DOMMediaStream&);
+  MediaRecorder(DOMMediaStream&, nsPIDOMWindow* aOwnerWindow);
   virtual ~MediaRecorder();
 
   // nsWrapperCache
   virtual JSObject* WrapObject(JSContext* aCx,
                                JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
 
   nsPIDOMWindow* GetParentObject() { return GetOwner(); }
 
@@ -78,18 +78,16 @@ public:
 
   // EventHandler
   IMPL_EVENT_HANDLER(dataavailable)
   IMPL_EVENT_HANDLER(error)
   IMPL_EVENT_HANDLER(stop)
   IMPL_EVENT_HANDLER(warning)
 
 protected:
-  void Init(nsPIDOMWindow* aOwnerWindow);
-
   MediaRecorder& operator = (const MediaRecorder& x) MOZ_DELETE;
   // Create dataavailable event with Blob data and it runs in main thread
   nsresult CreateAndDispatchBlobEvent(Session *session);
   // Creating a simple event to notify UA simple event.
   void DispatchSimpleEvent(const nsAString & aStr);
   // Creating a error event with message.
   void NotifyError(nsresult aRv);
   // Check if the recorder's principal is the subsume of mediaStream
--- a/content/media/webaudio/AudioContext.cpp
+++ b/content/media/webaudio/AudioContext.cpp
@@ -76,25 +76,24 @@ float GetSampleRateForAudioContext(bool 
   }
 }
 
 AudioContext::AudioContext(nsPIDOMWindow* aWindow,
                            bool aIsOffline,
                            uint32_t aNumberOfChannels,
                            uint32_t aLength,
                            float aSampleRate)
-  : mSampleRate(GetSampleRateForAudioContext(aIsOffline, aSampleRate))
+  : nsDOMEventTargetHelper(aWindow)
+  , mSampleRate(GetSampleRateForAudioContext(aIsOffline, aSampleRate))
   , mNumberOfChannels(aNumberOfChannels)
   , mIsOffline(aIsOffline)
   , mIsStarted(!aIsOffline)
   , mIsShutDown(false)
 {
-  nsDOMEventTargetHelper::BindToOwner(aWindow);
   aWindow->AddAudioContext(this);
-  SetIsDOMBinding();
 
   // Note: AudioDestinationNode needs an AudioContext that must already be
   // bound to the window.
   mDestination = new AudioDestinationNode(this, aIsOffline, aNumberOfChannels,
                                           aLength, aSampleRate);
   mDestination->Stream()->AddAudioOutput(&gWebAudioOutputKey);
 }
 
--- a/content/media/webaudio/AudioNode.cpp
+++ b/content/media/webaudio/AudioNode.cpp
@@ -46,24 +46,23 @@ AudioNode::Release()
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(AudioNode)
 NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
 
 AudioNode::AudioNode(AudioContext* aContext,
                      uint32_t aChannelCount,
                      ChannelCountMode aChannelCountMode,
                      ChannelInterpretation aChannelInterpretation)
-  : mContext(aContext)
+  : nsDOMEventTargetHelper(aContext->GetParentObject())
+  , mContext(aContext)
   , mChannelCount(aChannelCount)
   , mChannelCountMode(aChannelCountMode)
   , mChannelInterpretation(aChannelInterpretation)
 {
   MOZ_ASSERT(aContext);
-  nsDOMEventTargetHelper::BindToOwner(aContext->GetParentObject());
-  SetIsDOMBinding();
 }
 
 AudioNode::~AudioNode()
 {
   MOZ_ASSERT(mInputNodes.IsEmpty());
   MOZ_ASSERT(mOutputNodes.IsEmpty());
   MOZ_ASSERT(mOutputParams.IsEmpty());
 }
--- a/content/media/webspeech/recognition/SpeechRecognition.cpp
+++ b/content/media/webspeech/recognition/SpeechRecognition.cpp
@@ -62,23 +62,23 @@ NS_INTERFACE_MAP_BEGIN(SpeechRecognition
   NS_INTERFACE_MAP_ENTRY(nsIObserver)
 NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
 
 NS_IMPL_ADDREF_INHERITED(SpeechRecognition, nsDOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(SpeechRecognition, nsDOMEventTargetHelper)
 
 struct SpeechRecognition::TestConfig SpeechRecognition::mTestConfig;
 
-SpeechRecognition::SpeechRecognition()
-  : mEndpointer(kSAMPLE_RATE)
+SpeechRecognition::SpeechRecognition(nsPIDOMWindow* aOwnerWindow)
+  : nsDOMEventTargetHelper(aOwnerWindow)
+  , mEndpointer(kSAMPLE_RATE)
   , mAudioSamplesPerChunk(mEndpointer.FrameSize())
   , mSpeechDetectionTimer(do_CreateInstance(NS_TIMER_CONTRACTID))
 {
   SR_LOG("created SpeechRecognition");
-  SetIsDOMBinding();
 
   mTestConfig.Init();
   if (mTestConfig.mEnableTests) {
     nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
     obs->AddObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC, false);
     obs->AddObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC, false);
   }
 
@@ -116,18 +116,17 @@ SpeechRecognition::Constructor(const Glo
                                ErrorResult& aRv)
 {
   nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aGlobal.GetAsSupports());
   if (!win) {
     aRv.Throw(NS_ERROR_FAILURE);
   }
 
   MOZ_ASSERT(win->IsInnerWindow());
-  nsRefPtr<SpeechRecognition> object = new SpeechRecognition();
-  object->BindToOwner(win);
+  nsRefPtr<SpeechRecognition> object = new SpeechRecognition(win);
   return object.forget();
 }
 
 nsISupports*
 SpeechRecognition::GetParentObject() const
 {
   return GetOwner();
 }
--- a/content/media/webspeech/recognition/SpeechRecognition.h
+++ b/content/media/webspeech/recognition/SpeechRecognition.h
@@ -55,17 +55,17 @@ PRLogModuleInfo* GetSpeechRecognitionLog
 #endif
 
 class SpeechRecognition MOZ_FINAL : public nsDOMEventTargetHelper,
                                     public nsIObserver,
                                     public EnableWebSpeechRecognitionCheck,
                                     public SupportsWeakPtr<SpeechRecognition>
 {
 public:
-  SpeechRecognition();
+  SpeechRecognition(nsPIDOMWindow* aOwnerWindow);
   virtual ~SpeechRecognition() {};
 
   NS_DECL_ISUPPORTS_INHERITED
 
   NS_DECL_NSIOBSERVER
 
   nsISupports* GetParentObject() const;
 
--- a/content/media/webspeech/synth/SpeechSynthesisUtterance.cpp
+++ b/content/media/webspeech/synth/SpeechSynthesisUtterance.cpp
@@ -23,18 +23,20 @@ NS_IMPL_CYCLE_COLLECTION_INHERITED_1(Spe
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(SpeechSynthesisUtterance)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
 NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
 
 NS_IMPL_ADDREF_INHERITED(SpeechSynthesisUtterance, nsDOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(SpeechSynthesisUtterance, nsDOMEventTargetHelper)
 
-SpeechSynthesisUtterance::SpeechSynthesisUtterance(const nsAString& text)
-  : mText(text)
+SpeechSynthesisUtterance::SpeechSynthesisUtterance(nsPIDOMWindow* aOwnerWindow,
+                                                   const nsAString& text)
+  : nsDOMEventTargetHelper(aOwnerWindow)
+  , mText(text)
   , mVolume(1)
   , mRate(1)
   , mPitch(1)
   , mState(STATE_NONE)
   , mPaused(false)
 {
   SetIsDOMBinding();
 }
@@ -69,18 +71,17 @@ SpeechSynthesisUtterance::Constructor(Gl
   nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aGlobal.GetAsSupports());
 
   if (!win) {
     aRv.Throw(NS_ERROR_FAILURE);
   }
 
   MOZ_ASSERT(win->IsInnerWindow());
   nsRefPtr<SpeechSynthesisUtterance> object =
-    new SpeechSynthesisUtterance(aText);
-  object->BindToOwner(win);
+    new SpeechSynthesisUtterance(win, aText);
   return object.forget();
 }
 
 void
 SpeechSynthesisUtterance::GetText(nsString& aResult) const
 {
   aResult = mText;
 }
--- a/content/media/webspeech/synth/SpeechSynthesisUtterance.h
+++ b/content/media/webspeech/synth/SpeechSynthesisUtterance.h
@@ -25,17 +25,17 @@ class nsSynthVoiceRegistry;
 class SpeechSynthesisUtterance MOZ_FINAL : public nsDOMEventTargetHelper,
                                            public EnableSpeechSynthesisCheck
 {
   friend class SpeechSynthesis;
   friend class nsSpeechTask;
   friend class nsSynthVoiceRegistry;
 
 public:
-  SpeechSynthesisUtterance(const nsAString& aText);
+  SpeechSynthesisUtterance(nsPIDOMWindow* aOwnerWindow, const nsAString& aText);
   virtual ~SpeechSynthesisUtterance();
 
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SpeechSynthesisUtterance,
                                            nsDOMEventTargetHelper)
   NS_REALLY_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper)
 
   nsISupports* GetParentObject() const;
--- a/dom/base/MessagePort.cpp
+++ b/dom/base/MessagePort.cpp
@@ -337,17 +337,16 @@ NS_INTERFACE_MAP_END_INHERITING(nsDOMEve
 
 NS_IMPL_ADDREF_INHERITED(MessagePort, nsDOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(MessagePort, nsDOMEventTargetHelper)
 
 MessagePort::MessagePort(nsPIDOMWindow* aWindow)
   : MessagePortBase(aWindow)
   , mMessageQueueEnabled(false)
 {
-  SetIsDOMBinding();
 }
 
 MessagePort::~MessagePort()
 {
   Close();
 }
 
 JSObject*
--- a/dom/base/Navigator.cpp
+++ b/dom/base/Navigator.cpp
@@ -1079,18 +1079,18 @@ Navigator::GetBattery(ErrorResult& aRv)
 {
   if (!mBatteryManager) {
     if (!mWindow) {
       aRv.Throw(NS_ERROR_UNEXPECTED);
       return nullptr;
     }
     NS_ENSURE_TRUE(mWindow->GetDocShell(), nullptr);
 
-    mBatteryManager = new battery::BatteryManager();
-    mBatteryManager->Init(mWindow);
+    mBatteryManager = new battery::BatteryManager(mWindow);
+    mBatteryManager->Init();
   }
 
   return mBatteryManager;
 }
 
 already_AddRefed<Promise>
 Navigator::GetDataStores(const nsAString& aName, ErrorResult& aRv)
 {
--- a/dom/base/nsScreen.cpp
+++ b/dom/base/nsScreen.cpp
@@ -44,31 +44,30 @@ nsScreen::Create(nsPIDOMWindow* aWindow)
   if (!aWindow->GetDocShell()) {
     return nullptr;
   }
 
   nsCOMPtr<nsIScriptGlobalObject> sgo =
     do_QueryInterface(static_cast<nsPIDOMWindow*>(aWindow));
   NS_ENSURE_TRUE(sgo, nullptr);
 
-  nsRefPtr<nsScreen> screen = new nsScreen();
-  screen->BindToOwner(aWindow);
+  nsRefPtr<nsScreen> screen = new nsScreen(aWindow);
 
   hal::RegisterScreenConfigurationObserver(screen);
   hal::ScreenConfiguration config;
   hal::GetCurrentScreenConfiguration(&config);
   screen->mOrientation = config.orientation();
 
   return screen.forget();
 }
 
-nsScreen::nsScreen()
-  : mEventListener(nullptr)
+nsScreen::nsScreen(nsPIDOMWindow* aWindow)
+  : nsDOMEventTargetHelper(aWindow)
+  , mEventListener(nullptr)
 {
-  SetIsDOMBinding();
 }
 
 nsScreen::~nsScreen()
 {
   MOZ_ASSERT(!mEventListener);
   hal::UnregisterScreenConfigurationObserver(this);
 }
 
--- a/dom/base/nsScreen.h
+++ b/dom/base/nsScreen.h
@@ -139,17 +139,17 @@ private:
   {
   public:
     FullScreenEventListener() {};
 
     NS_DECL_ISUPPORTS
     NS_DECL_NSIDOMEVENTLISTENER
   };
 
-  nsScreen();
+  nsScreen(nsPIDOMWindow* aWindow);
   virtual ~nsScreen();
 
   enum LockPermission {
     LOCK_DENIED,
     FULLSCREEN_LOCK_ALLOWED,
     LOCK_ALLOWED
   };
 
--- a/dom/battery/BatteryManager.cpp
+++ b/dom/battery/BatteryManager.cpp
@@ -21,29 +21,27 @@
 #define CHARGINGCHANGE_EVENT_NAME        NS_LITERAL_STRING("chargingchange")
 #define DISCHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("dischargingtimechange")
 #define CHARGINGTIMECHANGE_EVENT_NAME    NS_LITERAL_STRING("chargingtimechange")
 
 namespace mozilla {
 namespace dom {
 namespace battery {
 
-BatteryManager::BatteryManager()
-  : mLevel(kDefaultLevel)
+BatteryManager::BatteryManager(nsPIDOMWindow* aWindow)
+  : nsDOMEventTargetHelper(aWindow)
+  , mLevel(kDefaultLevel)
   , mCharging(kDefaultCharging)
   , mRemainingTime(kDefaultRemainingTime)
 {
-  SetIsDOMBinding();
 }
 
 void
-BatteryManager::Init(nsPIDOMWindow *aWindow)
+BatteryManager::Init()
 {
-  BindToOwner(aWindow);
-
   hal::RegisterBatteryObserver(this);
 
   hal::BatteryInformation batteryInfo;
   hal::GetCurrentBatteryInformation(&batteryInfo);
 
   UpdateFromBatteryInfo(batteryInfo);
 }
 
--- a/dom/battery/BatteryManager.h
+++ b/dom/battery/BatteryManager.h
@@ -23,19 +23,19 @@ class BatteryInformation;
 
 namespace dom {
 namespace battery {
 
 class BatteryManager : public nsDOMEventTargetHelper
                      , public BatteryObserver
 {
 public:
-  BatteryManager();
+  BatteryManager(nsPIDOMWindow* aWindow);
 
-  void Init(nsPIDOMWindow *aWindow);
+  void Init();
   void Shutdown();
 
   // For IObserver.
   void Notify(const hal::BatteryInformation& aBatteryInfo);
 
   /**
    * Returns whether the battery api is supported (ie. not disabled by the user)
    * @return whether the battery api is supported.
--- a/dom/devicestorage/DeviceStorage.h
+++ b/dom/devicestorage/DeviceStorage.h
@@ -170,17 +170,17 @@ public:
                    const mozilla::dom::Nullable<bool>& aWantsUntrusted,
                    ErrorResult& aRv) MOZ_OVERRIDE;
 
   virtual void RemoveEventListener(const nsAString& aType,
                                    mozilla::dom::EventListener* aListener,
                                    bool aUseCapture,
                                    ErrorResult& aRv) MOZ_OVERRIDE;
 
-  nsDOMDeviceStorage();
+  nsDOMDeviceStorage(nsPIDOMWindow* aWindow);
 
   nsresult Init(nsPIDOMWindow* aWindow, const nsAString& aType,
                 const nsAString& aVolName);
 
   bool IsAvailable();
   bool IsFullPath(const nsAString& aPath)
   {
     return aPath.Length() > 0 && aPath.CharAt(0) == '/';
--- a/dom/devicestorage/nsDeviceStorage.cpp
+++ b/dom/devicestorage/nsDeviceStorage.cpp
@@ -2569,21 +2569,21 @@ NS_IMPL_CYCLE_COLLECTION_4(DeviceStorage
 NS_INTERFACE_MAP_BEGIN(nsDOMDeviceStorage)
   NS_INTERFACE_MAP_ENTRY(nsIDOMDeviceStorage)
   NS_INTERFACE_MAP_ENTRY(nsIObserver)
 NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
 
 NS_IMPL_ADDREF_INHERITED(nsDOMDeviceStorage, nsDOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(nsDOMDeviceStorage, nsDOMEventTargetHelper)
 
-nsDOMDeviceStorage::nsDOMDeviceStorage()
-  : mIsWatchingFile(false)
+nsDOMDeviceStorage::nsDOMDeviceStorage(nsPIDOMWindow* aWindow)
+  : nsDOMEventTargetHelper(aWindow)
+  , mIsWatchingFile(false)
   , mAllowedToWatchFile(false)
 {
-  SetIsDOMBinding();
 }
 
 /* virtual */ JSObject*
 nsDOMDeviceStorage::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
 {
   return DeviceStorageBinding::Wrap(aCx, aScope, this);
 }
 
@@ -2600,18 +2600,16 @@ nsDOMDeviceStorage::Init(nsPIDOMWindow* 
   SetRootDirectoryForType(aType, aVolName);
   if (!mRootDirectory) {
     return NS_ERROR_NOT_AVAILABLE;
   }
   if (!mStorageName.IsEmpty()) {
     RegisterForSDCardChanges(this);
   }
 
-  BindToOwner(aWindow);
-
   // Grab the principal of the document
   nsCOMPtr<nsIDocument> doc = aWindow->GetDoc();
   if (!doc) {
     return NS_ERROR_FAILURE;
   }
   mPrincipal = doc->NodePrincipal();
 
   // the 'apps' type is special.  We only want this exposed
@@ -2696,17 +2694,17 @@ nsDOMDeviceStorage::CreateDeviceStorageF
   nsString storageName;
   if (!DeviceStorageTypeChecker::IsVolumeBased(aType)) {
     // The storage name will be the empty string
     storageName.Truncate();
   } else {
     GetDefaultStorageName(aType, storageName);
   }
 
-  nsRefPtr<nsDOMDeviceStorage> ds = new nsDOMDeviceStorage();
+  nsRefPtr<nsDOMDeviceStorage> ds = new nsDOMDeviceStorage(aWin);
   if (NS_FAILED(ds->Init(aWin, aType, storageName))) {
     *aStore = nullptr;
     return;
   }
   NS_ADDREF(*aStore = ds.get());
 }
 
 // static
@@ -2714,29 +2712,29 @@ void
 nsDOMDeviceStorage::CreateDeviceStoragesFor(
   nsPIDOMWindow* aWin,
   const nsAString &aType,
   nsTArray<nsRefPtr<nsDOMDeviceStorage> > &aStores)
 {
   nsresult rv;
 
   if (!DeviceStorageTypeChecker::IsVolumeBased(aType)) {
-    nsRefPtr<nsDOMDeviceStorage> storage = new nsDOMDeviceStorage();
+    nsRefPtr<nsDOMDeviceStorage> storage = new nsDOMDeviceStorage(aWin);
     rv = storage->Init(aWin, aType, EmptyString());
     if (NS_SUCCEEDED(rv)) {
       aStores.AppendElement(storage);
     }
     return;
   }
   VolumeNameArray volNames;
   GetOrderedVolumeNames(volNames);
 
   VolumeNameArray::size_type numVolumeNames = volNames.Length();
   for (VolumeNameArray::index_type i = 0; i < numVolumeNames; i++) {
-    nsRefPtr<nsDOMDeviceStorage> storage = new nsDOMDeviceStorage();
+    nsRefPtr<nsDOMDeviceStorage> storage = new nsDOMDeviceStorage(aWin);
     rv = storage->Init(aWin, aType, volNames[i]);
     if (NS_FAILED(rv)) {
       break;
     }
     aStores.AppendElement(storage);
   }
 }
 
@@ -2798,17 +2796,17 @@ nsDOMDeviceStorage::GetStorageByName(con
     return ds.forget();
   }
   VolumeNameArray volNames;
   GetOrderedVolumeNames(volNames);
   VolumeNameArray::size_type numVolumes = volNames.Length();
   VolumeNameArray::index_type i;
   for (i = 0; i < numVolumes; i++) {
     if (volNames[i].Equals(aStorageName)) {
-      ds = new nsDOMDeviceStorage();
+      ds = new nsDOMDeviceStorage(GetOwner());
       nsresult rv = ds->Init(GetOwner(), mStorageType, aStorageName);
       if (NS_FAILED(rv)) {
         return nullptr;
       }
       return ds.forget();
     }
   }
   return nullptr;
--- a/dom/file/DOMFileHandle.cpp
+++ b/dom/file/DOMFileHandle.cpp
@@ -19,19 +19,17 @@ USING_FILE_NAMESPACE
 // static
 already_AddRefed<DOMFileHandle>
 DOMFileHandle::Create(nsPIDOMWindow* aWindow,
                       nsIFileStorage* aFileStorage,
                       nsIFile* aFile)
 {
   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
 
-  nsRefPtr<DOMFileHandle> newFile(new DOMFileHandle);
-
-  newFile->BindToOwner(aWindow);
+  nsRefPtr<DOMFileHandle> newFile(new DOMFileHandle(aWindow));
 
   newFile->mFileStorage = aFileStorage;
   nsresult rv = aFile->GetLeafName(newFile->mName);
   NS_ENSURE_SUCCESS(rv, nullptr);
 
   newFile->mFile = aFile;
   newFile->mFileName = newFile->mName;
 
--- a/dom/file/DOMFileHandle.h
+++ b/dom/file/DOMFileHandle.h
@@ -24,17 +24,18 @@ public:
 
   virtual already_AddRefed<nsISupports>
   CreateStream(nsIFile* aFile, bool aReadOnly) MOZ_OVERRIDE;
 
   virtual already_AddRefed<nsIDOMFile>
   CreateFileObject(LockedFile* aLockedFile, uint32_t aFileSize) MOZ_OVERRIDE;
 
 protected:
-  DOMFileHandle()
+  DOMFileHandle(nsPIDOMWindow* aWindow)
+    : FileHandle(aWindow)
   { }
 
   ~DOMFileHandle()
   { }
 };
 
 END_FILE_NAMESPACE
 
--- a/dom/file/FileHandle.h
+++ b/dom/file/FileHandle.h
@@ -15,16 +15,17 @@
 
 #include "nsDOMEventTargetHelper.h"
 
 #include "mozilla/Attributes.h"
 #include "mozilla/dom/FileModeBinding.h"
 
 class nsIDOMFile;
 class nsIFileStorage;
+class nsPIDOMWindow;
 
 namespace mozilla {
 namespace dom {
 class DOMRequest;
 } // namespace dom
 } // namespace mozilla
 
 BEGIN_FILE_NAMESPACE
@@ -88,19 +89,24 @@ public:
 
   virtual already_AddRefed<nsISupports>
   CreateStream(nsIFile* aFile, bool aReadOnly) = 0;
 
   virtual already_AddRefed<nsIDOMFile>
   CreateFileObject(LockedFile* aLockedFile, uint32_t aFileSize) = 0;
 
 protected:
-  FileHandle()
+  FileHandle(nsPIDOMWindow* aWindow)
+    : nsDOMEventTargetHelper(aWindow)
   {
-    SetIsDOMBinding();
+  }
+
+  FileHandle(nsDOMEventTargetHelper* aOwner)
+    : nsDOMEventTargetHelper(aOwner)
+  {
   }
 
   ~FileHandle()
   { }
 
   nsCOMPtr<nsIFileStorage> mFileStorage;
 
   nsString mName;
--- a/dom/indexedDB/IDBFileHandle.cpp
+++ b/dom/indexedDB/IDBFileHandle.cpp
@@ -30,16 +30,21 @@ GetFileFor(FileInfo* aFileInfo)
                                                      aFileInfo->Id());
   NS_ENSURE_TRUE(file, nullptr);
 
   return file.forget();
 }
 
 } // anonymous namespace
 
+IDBFileHandle::IDBFileHandle(IDBDatabase* aOwner)
+  : FileHandle(aOwner)
+{
+}
+
 // virtual
 JSObject*
 IDBFileHandle::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
 {
   return IDBFileHandleBinding::Wrap(aCx, aScope, this);
 }
 
 // static
@@ -49,19 +54,17 @@ IDBFileHandle::Create(IDBDatabase* aData
                       const nsAString& aType,
                       already_AddRefed<FileInfo> aFileInfo)
 {
   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
 
   nsRefPtr<FileInfo> fileInfo(aFileInfo);
   NS_ASSERTION(fileInfo, "Null pointer!");
 
-  nsRefPtr<IDBFileHandle> newFile = new IDBFileHandle();
-
-  newFile->BindToOwner(aDatabase);
+  nsRefPtr<IDBFileHandle> newFile = new IDBFileHandle(aDatabase);
 
   newFile->mFileStorage = aDatabase;
   newFile->mName = aName;
   newFile->mType = aType;
 
   newFile->mFile = GetFileFor(fileInfo);
   NS_ENSURE_TRUE(newFile->mFile, nullptr);
   newFile->mFileName.AppendInt(fileInfo->Id());
--- a/dom/indexedDB/IDBFileHandle.h
+++ b/dom/indexedDB/IDBFileHandle.h
@@ -11,17 +11,17 @@
 
 #include "mozilla/dom/file/FileHandle.h"
 #include "mozilla/dom/indexedDB/FileInfo.h"
 
 BEGIN_INDEXEDDB_NAMESPACE
 
 class IDBDatabase;
 
-class IDBFileHandle : public mozilla::dom::file::FileHandle
+class IDBFileHandle : public file::FileHandle
 {
 public:
   virtual JSObject*
   WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
 
   NS_IMETHOD_(int64_t)
   GetFileId()
   {
@@ -37,25 +37,23 @@ public:
   static already_AddRefed<IDBFileHandle>
   Create(IDBDatabase* aDatabase, const nsAString& aName,
          const nsAString& aType, already_AddRefed<FileInfo> aFileInfo);
 
   virtual already_AddRefed<nsISupports>
   CreateStream(nsIFile* aFile, bool aReadOnly);
 
   virtual already_AddRefed<nsIDOMFile>
-  CreateFileObject(mozilla::dom::file::LockedFile* aLockedFile,
-                   uint32_t aFileSize);
+  CreateFileObject(file::LockedFile* aLockedFile, uint32_t aFileSize);
 
   IDBDatabase*
   Database();
 
 private:
-  IDBFileHandle()
-  { }
+  IDBFileHandle(IDBDatabase* aOwner);
 
   ~IDBFileHandle()
   { }
 
   nsRefPtr<FileInfo> mFileInfo;
 };
 
 END_INDEXEDDB_NAMESPACE
--- a/dom/src/notification/DesktopNotification.cpp
+++ b/dom/src/notification/DesktopNotification.cpp
@@ -130,36 +130,34 @@ DesktopNotification::PostDesktopNotifica
                                        principal);
 }
 
 DesktopNotification::DesktopNotification(const nsAString & title,
                                          const nsAString & description,
                                          const nsAString & iconURL,
                                          nsPIDOMWindow *aWindow,
                                          nsIPrincipal* principal)
-  : mTitle(title)
+  : nsDOMEventTargetHelper(aWindow)
+  , mTitle(title)
   , mDescription(description)
   , mIconURL(iconURL)
   , mPrincipal(principal)
   , mAllow(false)
   , mShowHasBeenCalled(false)
 {
-  BindToOwner(aWindow);
   if (Preferences::GetBool("notification.disabled", false)) {
     return;
   }
 
   // If we are in testing mode (running mochitests, for example)
   // and we are suppose to allow requests, then just post an allow event.
   if (Preferences::GetBool("notification.prompt.testing", false) &&
       Preferences::GetBool("notification.prompt.testing.allow", true)) {
     mAllow = true;
   }
-
-  SetIsDOMBinding();
 }
 
 void
 DesktopNotification::Init()
 {
   nsRefPtr<DesktopNotificationRequest> request = new DesktopNotificationRequest(this);
 
   // if we are in the content process, then remote it to the parent.
--- a/dom/src/notification/Notification.cpp
+++ b/dom/src/notification/Notification.cpp
@@ -399,21 +399,22 @@ NotificationObserver::Observe(nsISupport
     mNotification->DispatchTrustedEvent(NS_LITERAL_STRING("show"));
   }
 
   return NS_OK;
 }
 
 Notification::Notification(const nsAString& aID, const nsAString& aTitle, const nsAString& aBody,
                            NotificationDirection aDir, const nsAString& aLang,
-                           const nsAString& aTag, const nsAString& aIconUrl)
-  : mID(aID), mTitle(aTitle), mBody(aBody), mDir(aDir), mLang(aLang),
+                           const nsAString& aTag, const nsAString& aIconUrl,
+			   nsPIDOMWindow* aWindow)
+  : nsDOMEventTargetHelper(aWindow),
+    mID(aID), mTitle(aTitle), mBody(aBody), mDir(aDir), mLang(aLang),
     mTag(aTag), mIconUrl(aIconUrl), mIsClosed(false)
 {
-  SetIsDOMBinding();
 }
 
 // static
 already_AddRefed<Notification>
 Notification::Constructor(const GlobalObject& aGlobal,
                           const nsAString& aTitle,
                           const NotificationOptions& aOptions,
                           ErrorResult& aRv)
@@ -487,19 +488,18 @@ Notification::CreateInternal(nsPIDOMWind
   }
 
   nsRefPtr<Notification> notification = new Notification(id,
                                                          aTitle,
                                                          aOptions.mBody,
                                                          aOptions.mDir,
                                                          aOptions.mLang,
                                                          aOptions.mTag,
-                                                         aOptions.mIcon);
-
-  notification->BindToOwner(aWindow);
+                                                         aOptions.mIcon,
+							 aWindow);
   return notification.forget();
 }
 
 nsIPrincipal*
 Notification::GetPrincipal()
 {
   nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(GetOwner());
   NS_ENSURE_TRUE(sop, nullptr);
--- a/dom/src/notification/Notification.h
+++ b/dom/src/notification/Notification.h
@@ -92,17 +92,18 @@ public:
     return GetOwner();
   }
 
   virtual JSObject* WrapObject(JSContext* aCx,
                                JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
 protected:
   Notification(const nsAString& aID, const nsAString& aTitle, const nsAString& aBody,
                NotificationDirection aDir, const nsAString& aLang,
-               const nsAString& aTag, const nsAString& aIconUrl);
+               const nsAString& aTag, const nsAString& aIconUrl,
+	       nsPIDOMWindow* aWindow);
 
   static already_AddRefed<Notification> CreateInternal(nsPIDOMWindow* aWindow,
                                                        const nsAString& aID,
                                                        const nsAString& aTitle,
                                                        const NotificationOptions& aOptions);
 
   void ShowInternal();
   void CloseInternal();
--- a/dom/src/offline/nsDOMOfflineResourceList.cpp
+++ b/dom/src/offline/nsDOMOfflineResourceList.cpp
@@ -74,26 +74,25 @@ NS_IMPL_EVENT_HANDLER(nsDOMOfflineResour
 NS_IMPL_EVENT_HANDLER(nsDOMOfflineResourceList, progress)
 NS_IMPL_EVENT_HANDLER(nsDOMOfflineResourceList, cached)
 NS_IMPL_EVENT_HANDLER(nsDOMOfflineResourceList, updateready)
 NS_IMPL_EVENT_HANDLER(nsDOMOfflineResourceList, obsolete)
 
 nsDOMOfflineResourceList::nsDOMOfflineResourceList(nsIURI *aManifestURI,
                                                    nsIURI *aDocumentURI,
                                                    nsPIDOMWindow *aWindow)
-  : mInitialized(false)
+  : nsDOMEventTargetHelper(aWindow)
+  , mInitialized(false)
   , mManifestURI(aManifestURI)
   , mDocumentURI(aDocumentURI)
   , mExposeCacheUpdateStatus(true)
   , mStatus(nsIDOMOfflineResourceList::IDLE)
   , mCachedKeys(nullptr)
   , mCachedKeysCount(0)
 {
-  BindToOwner(aWindow);
-  SetIsDOMBinding();
 }
 
 nsDOMOfflineResourceList::~nsDOMOfflineResourceList()
 {
   ClearCachedKeys();
 }
 
 JSObject*
--- a/dom/telephony/Telephony.cpp
+++ b/dom/telephony/Telephony.cpp
@@ -74,26 +74,25 @@ public:
 
   NS_IMETHOD Run()
   {
     mTelephony->NotifyCallsChanged(nullptr);
     return NS_OK;
   }
 };
 
-Telephony::Telephony()
-: mActiveCall(nullptr), mEnumerated(false)
+Telephony::Telephony(nsPIDOMWindow* aOwner)
+  : nsDOMEventTargetHelper(aOwner),
+    mActiveCall(nullptr), mEnumerated(false)
 {
   if (!gTelephonyList) {
     gTelephonyList = new TelephonyList();
   }
 
   gTelephonyList->AppendElement(this);
-
-  SetIsDOMBinding();
 }
 
 Telephony::~Telephony()
 {
   Shutdown();
 
   NS_ASSERTION(gTelephonyList, "This should never be null!");
   NS_ASSERTION(gTelephonyList->Contains(this), "Should be in the list!");
@@ -147,19 +146,17 @@ Telephony::Create(nsPIDOMWindow* aOwner,
   }
 
   nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
   if (!scriptContext) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return nullptr;
   }
 
-  nsRefPtr<Telephony> telephony = new Telephony();
-
-  telephony->BindToOwner(aOwner);
+  nsRefPtr<Telephony> telephony = new Telephony(aOwner);
 
   telephony->mProvider = ril;
   telephony->mListener = new Listener(telephony);
   telephony->mCallsList = new CallsList(telephony);
   telephony->mGroup = TelephonyCallGroup::Create(telephony);
 
   nsresult rv = ril->EnumerateCalls(telephony->mListener);
   if (NS_FAILED(rv)) {
--- a/dom/telephony/Telephony.h
+++ b/dom/telephony/Telephony.h
@@ -138,17 +138,17 @@ public:
   CallsArray() const
   {
     return mCalls;
   }
 
   virtual void EventListenerAdded(nsIAtom* aType) MOZ_OVERRIDE;
 
 private:
-  Telephony();
+  Telephony(nsPIDOMWindow* aOwner);
   ~Telephony();
 
   void
   Shutdown();
 
   static bool
   IsValidNumber(const nsAString& aNumber);
 
--- a/dom/telephony/TelephonyCall.cpp
+++ b/dom/telephony/TelephonyCall.cpp
@@ -22,40 +22,38 @@ already_AddRefed<TelephonyCall>
 TelephonyCall::Create(Telephony* aTelephony, uint32_t aServiceId,
                       const nsAString& aNumber, uint16_t aCallState,
                       uint32_t aCallIndex, bool aEmergency, bool aIsConference)
 {
   NS_ASSERTION(aTelephony, "Null pointer!");
   NS_ASSERTION(!aNumber.IsEmpty(), "Empty number!");
   NS_ASSERTION(aCallIndex >= 1, "Invalid call index!");
 
-  nsRefPtr<TelephonyCall> call = new TelephonyCall();
-
-  call->BindToOwner(aTelephony->GetOwner());
+  nsRefPtr<TelephonyCall> call = new TelephonyCall(aTelephony->GetOwner());
 
   call->mTelephony = aTelephony;
   call->mServiceId = aServiceId;
   call->mNumber = aNumber;
   call->mCallIndex = aCallIndex;
   call->mError = nullptr;
   call->mEmergency = aEmergency;
   call->mGroup = aIsConference ? aTelephony->ConferenceGroup() : nullptr;
 
   call->ChangeStateInternal(aCallState, false);
 
   return call.forget();
 }
 
-TelephonyCall::TelephonyCall()
-  : mCallIndex(kOutgoingPlaceholderCallIndex),
+TelephonyCall::TelephonyCall(nsPIDOMWindow* aOwner)
+  : nsDOMEventTargetHelper(aOwner),
+    mCallIndex(kOutgoingPlaceholderCallIndex),
     mCallState(nsITelephonyProvider::CALL_STATE_UNKNOWN),
     mLive(false),
     mOutgoing(false)
 {
-  SetIsDOMBinding();
 }
 
 TelephonyCall::~TelephonyCall()
 {
 }
 
 JSObject*
 TelephonyCall::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
--- a/dom/telephony/TelephonyCall.h
+++ b/dom/telephony/TelephonyCall.h
@@ -165,17 +165,17 @@ public:
 
   void
   NotifyError(const nsAString& aError);
 
   void
   ChangeGroup(TelephonyCallGroup* aGroup);
 
 private:
-  TelephonyCall();
+  TelephonyCall(nsPIDOMWindow* aOwner);
 
   ~TelephonyCall();
 
   void
   ChangeStateInternal(uint16_t aCallState, bool aFireEvents);
 
   nsresult
   DispatchCallEvent(const nsAString& aType,
--- a/dom/telephony/TelephonyCallGroup.cpp
+++ b/dom/telephony/TelephonyCallGroup.cpp
@@ -10,35 +10,34 @@
 #include "CallEvent.h"
 #include "CallsList.h"
 #include "mozilla/dom/CallGroupErrorEvent.h"
 #include "Telephony.h"
 
 using namespace mozilla::dom;
 using mozilla::ErrorResult;
 
-TelephonyCallGroup::TelephonyCallGroup()
-: mCallState(nsITelephonyProvider::CALL_STATE_UNKNOWN)
+TelephonyCallGroup::TelephonyCallGroup(nsPIDOMWindow* aOwner)
+  : nsDOMEventTargetHelper(aOwner)
+  , mCallState(nsITelephonyProvider::CALL_STATE_UNKNOWN)
 {
-  SetIsDOMBinding();
 }
 
 TelephonyCallGroup::~TelephonyCallGroup()
 {
 }
 
 // static
 already_AddRefed<TelephonyCallGroup>
 TelephonyCallGroup::Create(Telephony* aTelephony)
 {
   NS_ASSERTION(aTelephony, "Null telephony!");
 
-  nsRefPtr<TelephonyCallGroup> group = new TelephonyCallGroup();
-
-  group->BindToOwner(aTelephony->GetOwner());
+  nsRefPtr<TelephonyCallGroup> group =
+    new TelephonyCallGroup(aTelephony->GetOwner());
 
   group->mTelephony = aTelephony;
   group->mCallsList = new CallsList(aTelephony, group);
 
   return group.forget();
 }
 
 JSObject*
--- a/dom/telephony/TelephonyCallGroup.h
+++ b/dom/telephony/TelephonyCallGroup.h
@@ -98,17 +98,17 @@ public:
   {
     return mCallState;
   }
 
   nsresult
   NotifyError(const nsAString& aName, const nsAString& aMessage);
 
 private:
-  TelephonyCallGroup();
+  TelephonyCallGroup(nsPIDOMWindow* aOwner);
   ~TelephonyCallGroup();
 
   nsresult
   NotifyCallsChanged(TelephonyCall* aCall);
 
   nsresult
   DispatchCallEvent(const nsAString& aType,
                     TelephonyCall* aCall);
--- a/layout/build/nsLayoutModule.cpp
+++ b/layout/build/nsLayoutModule.cpp
@@ -81,17 +81,16 @@
 #include "nsDOMScriptObjectFactory.h"
 #include "DOMStorageManager.h"
 #include "nsJSON.h"
 #include "nsZipArchive.h"
 #include "mozIApplicationClearPrivateDataParams.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/dom/DOMException.h"
 #include "mozilla/dom/DOMRequest.h"
-#include "mozilla/dom/EventSource.h"
 #include "mozilla/dom/indexedDB/IndexedDatabaseManager.h"
 #include "mozilla/dom/network/TCPSocketChild.h"
 #include "mozilla/dom/network/TCPSocketParent.h"
 #include "mozilla/dom/network/TCPServerSocketChild.h"
 #include "mozilla/dom/network/UDPSocketChild.h"
 #include "mozilla/dom/quota/QuotaManager.h"
 #include "mozilla/OSFileConstants.h"
 #include "mozilla/Services.h"
@@ -264,17 +263,16 @@ using mozilla::net::StreamingProtocolCon
 "@mozilla.org/transformiix-nodeset;1"
 
 // Factory Constructor
 NS_GENERIC_FACTORY_CONSTRUCTOR(txMozillaXSLTProcessor)
 NS_GENERIC_FACTORY_CONSTRUCTOR(XPathEvaluator)
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(txNodeSetAdaptor, Init)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsDOMSerializer)
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsXMLHttpRequest, Init)
-NS_GENERIC_FACTORY_CONSTRUCTOR(EventSource)
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsDOMFileReader, Init)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsFormData)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsBlobProtocolHandler)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMediaStreamProtocolHandler)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMediaSourceProtocolHandler)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsFontTableProtocolHandler)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsHostObjectURI)
 NS_GENERIC_FACTORY_CONSTRUCTOR(DOMParser)
@@ -744,17 +742,16 @@ NS_DEFINE_NAMED_CID(NS_XMLSERIALIZER_CID
 NS_DEFINE_NAMED_CID(NS_FILEREADER_CID);
 NS_DEFINE_NAMED_CID(NS_FORMDATA_CID);
 NS_DEFINE_NAMED_CID(NS_BLOBPROTOCOLHANDLER_CID);
 NS_DEFINE_NAMED_CID(NS_MEDIASTREAMPROTOCOLHANDLER_CID);
 NS_DEFINE_NAMED_CID(NS_MEDIASOURCEPROTOCOLHANDLER_CID);
 NS_DEFINE_NAMED_CID(NS_FONTTABLEPROTOCOLHANDLER_CID);
 NS_DEFINE_NAMED_CID(NS_HOSTOBJECTURI_CID);
 NS_DEFINE_NAMED_CID(NS_XMLHTTPREQUEST_CID);
-NS_DEFINE_NAMED_CID(NS_EVENTSOURCE_CID);
 NS_DEFINE_NAMED_CID(NS_DOMPARSER_CID);
 NS_DEFINE_NAMED_CID(NS_DOMSESSIONSTORAGEMANAGER_CID);
 NS_DEFINE_NAMED_CID(NS_DOMLOCALSTORAGEMANAGER_CID);
 NS_DEFINE_NAMED_CID(NS_DOMJSON_CID);
 NS_DEFINE_NAMED_CID(NS_TEXTEDITOR_CID);
 NS_DEFINE_NAMED_CID(INDEXEDDB_MANAGER_CID);
 NS_DEFINE_NAMED_CID(DOMREQUEST_SERVICE_CID);
 NS_DEFINE_NAMED_CID(QUOTA_MANAGER_CID);
@@ -1032,17 +1029,16 @@ static const mozilla::Module::CIDEntry k
   { &kNS_FILEREADER_CID, false, nullptr, nsDOMFileReaderConstructor },
   { &kNS_FORMDATA_CID, false, nullptr, nsFormDataConstructor },
   { &kNS_BLOBPROTOCOLHANDLER_CID, false, nullptr, nsBlobProtocolHandlerConstructor },
   { &kNS_MEDIASTREAMPROTOCOLHANDLER_CID, false, nullptr, nsMediaStreamProtocolHandlerConstructor },
   { &kNS_MEDIASOURCEPROTOCOLHANDLER_CID, false, nullptr, nsMediaSourceProtocolHandlerConstructor },
   { &kNS_FONTTABLEPROTOCOLHANDLER_CID, false, nullptr, nsFontTableProtocolHandlerConstructor },
   { &kNS_HOSTOBJECTURI_CID, false, nullptr, nsHostObjectURIConstructor },
   { &kNS_XMLHTTPREQUEST_CID, false, nullptr, nsXMLHttpRequestConstructor },
-  { &kNS_EVENTSOURCE_CID, false, nullptr, EventSourceConstructor },
   { &kNS_DOMPARSER_CID, false, nullptr, DOMParserConstructor },
   { &kNS_XPCEXCEPTION_CID, false, nullptr, ExceptionConstructor },
   { &kNS_DOMSESSIONSTORAGEMANAGER_CID, false, nullptr, DOMSessionStorageManagerConstructor },
   { &kNS_DOMLOCALSTORAGEMANAGER_CID, false, nullptr, DOMLocalStorageManagerConstructor },
   { &kNS_DOMJSON_CID, false, nullptr, NS_NewJSON },
   { &kNS_TEXTEDITOR_CID, false, nullptr, nsPlaintextEditorConstructor },
   { &kINDEXEDDB_MANAGER_CID, false, nullptr, IndexedDatabaseManagerConstructor },
   { &kDOMREQUEST_SERVICE_CID, false, nullptr, DOMRequestServiceConstructor },
@@ -1188,17 +1184,16 @@ static const mozilla::Module::ContractID
   { NS_XMLSERIALIZER_CONTRACTID, &kNS_XMLSERIALIZER_CID },
   { NS_FILEREADER_CONTRACTID, &kNS_FILEREADER_CID },
   { NS_FORMDATA_CONTRACTID, &kNS_FORMDATA_CID },
   { NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX BLOBURI_SCHEME, &kNS_BLOBPROTOCOLHANDLER_CID },
   { NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX MEDIASTREAMURI_SCHEME, &kNS_MEDIASTREAMPROTOCOLHANDLER_CID },
   { NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX MEDIASOURCEURI_SCHEME, &kNS_MEDIASOURCEPROTOCOLHANDLER_CID },
   { NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX FONTTABLEURI_SCHEME, &kNS_FONTTABLEPROTOCOLHANDLER_CID },
   { NS_XMLHTTPREQUEST_CONTRACTID, &kNS_XMLHTTPREQUEST_CID },
-  { NS_EVENTSOURCE_CONTRACTID, &kNS_EVENTSOURCE_CID },
   { NS_DOMPARSER_CONTRACTID, &kNS_DOMPARSER_CID },
   { XPC_EXCEPTION_CONTRACTID, &kNS_XPCEXCEPTION_CID },
   { "@mozilla.org/dom/localStorage-manager;1", &kNS_DOMLOCALSTORAGEMANAGER_CID },
   // Keeping the old ContractID for backward compatibility
   { "@mozilla.org/dom/storagemanager;1", &kNS_DOMLOCALSTORAGEMANAGER_CID },
   { "@mozilla.org/dom/sessionStorage-manager;1", &kNS_DOMSESSIONSTORAGEMANAGER_CID },
   { "@mozilla.org/dom/json;1", &kNS_DOMJSON_CID },
   { "@mozilla.org/editor/texteditor;1", &kNS_TEXTEDITOR_CID },