Bug 1320753 - Stop using already_AddRefed for EventTargetFor (r=ehsan)
authorBill McCloskey <billm@mozilla.com>
Thu, 15 Dec 2016 13:48:14 -0800
changeset 453589 f980cbff80529d0c9d19d257e32f2af416c559b2
parent 453588 30b32a2aabb1d18f952492ea41f9465c32ecebe7
child 453590 4e9f3f0d6d8e2c7e8aa0037decd8405d5024bfa0
push id39711
push userdmitchell@mozilla.com
push dateFri, 23 Dec 2016 21:59:47 +0000
reviewersehsan
bugs1320753
milestone53.0a1
Bug 1320753 - Stop using already_AddRefed for EventTargetFor (r=ehsan) MozReview-Commit-ID: ECy1jlQ7qne
dom/base/Dispatcher.cpp
dom/base/Dispatcher.h
dom/base/DocGroup.cpp
dom/base/DocGroup.h
dom/base/TabGroup.cpp
dom/base/TabGroup.h
dom/base/nsDocument.cpp
dom/base/nsGlobalWindow.cpp
dom/base/nsGlobalWindow.h
dom/base/nsIDocument.h
--- a/dom/base/Dispatcher.cpp
+++ b/dom/base/Dispatcher.cpp
@@ -24,21 +24,21 @@ DispatcherTrait::Dispatch(const char* aN
   }
   if (NS_IsMainThread()) {
     return NS_DispatchToCurrentThread(runnable.forget());
   } else {
     return NS_DispatchToMainThread(runnable.forget());
   }
 }
 
-already_AddRefed<nsIEventTarget>
+nsIEventTarget*
 DispatcherTrait::EventTargetFor(TaskCategory aCategory) const
 {
   nsCOMPtr<nsIEventTarget> main = do_GetMainThread();
-  return main.forget();
+  return main;
 }
 
 namespace {
 
 #define NS_DISPATCHEREVENTTARGET_IID \
 { 0xbf4e36c8, 0x7d04, 0x4ef4, \
   { 0xbb, 0xd8, 0x11, 0x09, 0x0a, 0xdb, 0x4d, 0xf7 } }
 
--- a/dom/base/Dispatcher.h
+++ b/dom/base/Dispatcher.h
@@ -51,32 +51,30 @@ public:
   // it is safe. For nsIGlobalWindow it is not safe.
   virtual nsresult Dispatch(const char* aName,
                             TaskCategory aCategory,
                             already_AddRefed<nsIRunnable>&& aRunnable);
 
   // This method may or may not be safe off of the main thread. For nsIDocument
   // it is safe. For nsIGlobalWindow it is not safe. The nsIEventTarget can
   // always be used off the main thread.
-  virtual already_AddRefed<nsIEventTarget>
-  EventTargetFor(TaskCategory aCategory) const;
+  virtual nsIEventTarget* EventTargetFor(TaskCategory aCategory) const;
 };
 
 // Base class for DocGroup and TabGroup.
 class Dispatcher : public nsISupports {
 public:
   // This method is always safe to call off the main thread.
   virtual nsresult Dispatch(const char* aName,
                             TaskCategory aCategory,
                             already_AddRefed<nsIRunnable>&& aRunnable) = 0;
 
   // This method is always safe to call off the main thread. The nsIEventTarget
   // can always be used off the main thread.
-  virtual already_AddRefed<nsIEventTarget>
-  EventTargetFor(TaskCategory aCategory) const = 0;
+  virtual nsIEventTarget* EventTargetFor(TaskCategory aCategory) const = 0;
 
   // These methods perform a safe cast. They return null if |this| is not of the
   // requested type.
   virtual TabGroup* AsTabGroup() { return nullptr; }
 
 protected:
   virtual already_AddRefed<nsIEventTarget>
   CreateEventTargetFor(TaskCategory aCategory);
--- a/dom/base/DocGroup.cpp
+++ b/dom/base/DocGroup.cpp
@@ -58,16 +58,16 @@ NS_IMPL_ISUPPORTS(DocGroup, nsISupports)
 nsresult
 DocGroup::Dispatch(const char* aName,
                    TaskCategory aCategory,
                    already_AddRefed<nsIRunnable>&& aRunnable)
 {
   return mTabGroup->Dispatch(aName, aCategory, Move(aRunnable));
 }
 
-already_AddRefed<nsIEventTarget>
+nsIEventTarget*
 DocGroup::EventTargetFor(TaskCategory aCategory) const
 {
   return mTabGroup->EventTargetFor(aCategory);
 }
 
 }
 }
--- a/dom/base/DocGroup.h
+++ b/dom/base/DocGroup.h
@@ -64,18 +64,17 @@ public:
   {
     return mDocuments.end();
   }
 
   virtual nsresult Dispatch(const char* aName,
                             TaskCategory aCategory,
                             already_AddRefed<nsIRunnable>&& aRunnable) override;
 
-  virtual already_AddRefed<nsIEventTarget>
-  EventTargetFor(TaskCategory aCategory) const override;
+  virtual nsIEventTarget* EventTargetFor(TaskCategory aCategory) const override;
 
 private:
   DocGroup(TabGroup* aTabGroup, const nsACString& aKey);
   ~DocGroup();
 
   nsCString mKey;
   RefPtr<TabGroup> mTabGroup;
   nsTArray<nsIDocument*> mDocuments;
--- a/dom/base/TabGroup.cpp
+++ b/dom/base/TabGroup.cpp
@@ -250,18 +250,17 @@ TabGroup::Dispatch(const char* aName,
   }
   if (NS_IsMainThread()) {
     return NS_DispatchToCurrentThread(runnable.forget());
   } else {
     return NS_DispatchToMainThread(runnable.forget());
   }
 }
 
-already_AddRefed<nsIEventTarget>
+nsIEventTarget*
 TabGroup::EventTargetFor(TaskCategory aCategory) const
 {
   MOZ_RELEASE_ASSERT(!mLastWindowLeft);
-  nsCOMPtr<nsIEventTarget> target = mEventTargets[size_t(aCategory)];
-  return target.forget();
+  return mEventTargets[size_t(aCategory)];
 }
 
 }
 }
--- a/dom/base/TabGroup.h
+++ b/dom/base/TabGroup.h
@@ -8,16 +8,17 @@
 #define TabGroup_h
 
 #include "nsISupports.h"
 #include "nsISupportsImpl.h"
 #include "nsIPrincipal.h"
 #include "nsTHashtable.h"
 #include "nsString.h"
 
+#include "mozilla/Atomics.h"
 #include "mozilla/dom/Dispatcher.h"
 #include "mozilla/RefPtr.h"
 
 namespace mozilla {
 class ThrottledEventQueue;
 namespace dom {
 
 // Two browsing contexts are considered "related" if they are reachable from one
@@ -109,31 +110,33 @@ public:
 
   nsTArray<nsPIDOMWindowOuter*> GetTopLevelWindows();
 
   // Get the event queue that associated windows can use to issue runnables to
   // the main thread.  This may return nullptr during browser shutdown.
   ThrottledEventQueue*
   GetThrottledEventQueue() const;
 
+  // This method is always safe to call off the main thread.
   virtual nsresult Dispatch(const char* aName,
                             TaskCategory aCategory,
                             already_AddRefed<nsIRunnable>&& aRunnable) override;
 
-  virtual already_AddRefed<nsIEventTarget>
-  EventTargetFor(TaskCategory aCategory) const override;
+  // This method is always safe to call off the main thread. The nsIEventTarget
+  // can always be used off the main thread.
+  virtual nsIEventTarget* EventTargetFor(TaskCategory aCategory) const override;
 
   TabGroup* AsTabGroup() override { return this; }
 
 private:
   void EnsureThrottledEventQueues();
 
   ~TabGroup();
   DocGroupMap mDocGroups;
-  bool mLastWindowLeft;
+  Atomic<bool> mLastWindowLeft;
   nsTArray<nsPIDOMWindowOuter*> mWindows;
   bool mThrottledQueuesInitialized;
   RefPtr<ThrottledEventQueue> mThrottledEventQueue;
   nsCOMPtr<nsIEventTarget> mEventTargets[size_t(TaskCategory::Count)];
 };
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -2886,17 +2886,17 @@ nsIDocument::Dispatch(const char* aName,
 {
   // Note that this method may be called off the main thread.
   if (mDocGroup) {
     return mDocGroup->Dispatch(aName, aCategory, Move(aRunnable));
   }
   return DispatcherTrait::Dispatch(aName, aCategory, Move(aRunnable));
 }
 
-already_AddRefed<nsIEventTarget>
+nsIEventTarget*
 nsIDocument::EventTargetFor(TaskCategory aCategory) const
 {
   if (mDocGroup) {
     return mDocGroup->EventTargetFor(aCategory);
   }
   return DispatcherTrait::EventTargetFor(aCategory);
 }
 
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -14261,17 +14261,17 @@ nsGlobalWindow::Dispatch(const char* aNa
 {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
   if (GetDocGroup()) {
     return GetDocGroup()->Dispatch(aName, aCategory, Move(aRunnable));
   }
   return DispatcherTrait::Dispatch(aName, aCategory, Move(aRunnable));
 }
 
-already_AddRefed<nsIEventTarget>
+nsIEventTarget*
 nsGlobalWindow::EventTargetFor(TaskCategory aCategory) const
 {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
   if (GetDocGroup()) {
     return GetDocGroup()->EventTargetFor(aCategory);
   }
   return DispatcherTrait::EventTargetFor(aCategory);
 }
--- a/dom/base/nsGlobalWindow.h
+++ b/dom/base/nsGlobalWindow.h
@@ -1765,17 +1765,17 @@ private:
   bool IsBackgroundInternal() const;
 
 public:
   // Dispatch a runnable related to the global.
   virtual nsresult Dispatch(const char* aName,
                             mozilla::dom::TaskCategory aCategory,
                             already_AddRefed<nsIRunnable>&& aRunnable) override;
 
-  virtual already_AddRefed<nsIEventTarget>
+  virtual nsIEventTarget*
   EventTargetFor(mozilla::dom::TaskCategory aCategory) const override;
 
 protected:
   // These members are only used on outer window objects. Make sure
   // you never set any of these on an inner object!
   bool                          mFullScreen : 1;
   bool                          mFullscreenMode : 1;
   bool                          mIsClosed : 1;
--- a/dom/base/nsIDocument.h
+++ b/dom/base/nsIDocument.h
@@ -2867,17 +2867,17 @@ public:
   virtual void ScheduleIntersectionObserverNotification() = 0;
   virtual void NotifyIntersectionObservers() = 0;
 
   // Dispatch a runnable related to the document.
   virtual nsresult Dispatch(const char* aName,
                             mozilla::dom::TaskCategory aCategory,
                             already_AddRefed<nsIRunnable>&& aRunnable) override;
 
-  virtual already_AddRefed<nsIEventTarget>
+  virtual nsIEventTarget*
   EventTargetFor(mozilla::dom::TaskCategory aCategory) const override;
 
   // The URLs passed to these functions should match what
   // JS::DescribeScriptedCaller() returns, since these APIs are used to
   // determine whether some code is being called from a tracking script.
   void NoteScriptTrackingStatus(const nsACString& aURL, bool isTracking);
   bool IsScriptTracking(const nsACString& aURL) const;