Bug 1114554 - Patch 7 - Call BindToOwner on all threads. r=wchen
☠☠ backed out by 531f100d2bd8 ☠ ☠
authorNikhil Marathe <nsm.nikhil@gmail.com>
Fri, 22 May 2015 14:55:40 -0700
changeset 268374 5578d5c280a6781a0ad9e064aded80ed26f463d5
parent 268373 404830c1ecf70d7075bd48ccc4d20f89e3e9a090
child 268375 247ca4bf258ea72aa5be01b6e79daf8174b6fc3c
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-esr52@6dd5a4f5f745 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswchen
bugs1114554
milestone41.0a1
Bug 1114554 - Patch 7 - Call BindToOwner on all threads. r=wchen
dom/notification/Notification.cpp
dom/notification/Notification.h
--- a/dom/notification/Notification.cpp
+++ b/dom/notification/Notification.cpp
@@ -685,27 +685,35 @@ Notification::PrefEnabled(JSContext* aCx
 
 // static
 bool
 Notification::IsGetEnabled(JSContext* aCx, JSObject* aObj)
 {
   return NS_IsMainThread();
 }
 
-Notification::Notification(const nsAString& aID, const nsAString& aTitle, const nsAString& aBody,
+Notification::Notification(nsIGlobalObject* aGlobal, const nsAString& aID,
+                           const nsAString& aTitle, const nsAString& aBody,
                            NotificationDirection aDir, const nsAString& aLang,
                            const nsAString& aTag, const nsAString& aIconUrl,
                            const NotificationBehavior& aBehavior)
   : DOMEventTargetHelper(),
     mWorkerPrivate(nullptr), mObserver(nullptr),
     mID(aID), mTitle(aTitle), mBody(aBody), mDir(aDir), mLang(aLang),
     mTag(aTag), mIconUrl(aIconUrl), mBehavior(aBehavior), mIsClosed(false),
     mIsStored(false), mTaskCount(0)
 {
-  if (!NS_IsMainThread()) {
+  if (NS_IsMainThread()) {
+    // We can only call this on the main thread because
+    // Event::SetEventType() called down the call chain when dispatching events
+    // using DOMEventTargetHelper::DispatchTrustedEvent() will assume the event
+    // is a main thread event if it has a valid owner. It will then attempt to
+    // fetch the atom for the event name which asserts main thread only.
+    BindToOwner(aGlobal);
+  } else {
     mWorkerPrivate = GetCurrentThreadWorkerPrivate();
     MOZ_ASSERT(mWorkerPrivate);
   }
 }
 
 void
 Notification::SetAlertName()
 {
@@ -782,24 +790,18 @@ Notification::ConstructFromFields(
   MOZ_ASSERT(ok);
 
   RootedDictionary<NotificationOptions> options(jsapi.cx());
   options.mDir = Notification::StringToDirection(nsString(aDir));
   options.mLang = aLang;
   options.mBody = aBody;
   options.mTag = aTag;
   options.mIcon = aIcon;
-  nsRefPtr<Notification> notification;
-  notification = Notification::CreateInternal(aID,
-                                              aTitle,
-                                              options);
-
-  if (NS_IsMainThread()) {
-    notification->BindToOwner(aGlobal);
-  }
+  nsRefPtr<Notification> notification = CreateInternal(aGlobal, aID, aTitle,
+                                                       options);
 
   notification->InitFromBase64(jsapi.cx(), aData, aRv);
   if (NS_WARN_IF(aRv.Failed())) {
     return nullptr;
   }
 
   notification->SetScope(aServiceWorkerRegistrationID);
 
@@ -874,17 +876,18 @@ Notification::UnpersistNotification()
         notificationStorage->Delete(origin, mID);
       }
     }
     SetStoredState(false);
   }
 }
 
 already_AddRefed<Notification>
-Notification::CreateInternal(const nsAString& aID,
+Notification::CreateInternal(nsIGlobalObject* aGlobal,
+                             const nsAString& aID,
                              const nsAString& aTitle,
                              const NotificationOptions& aOptions)
 {
   nsString id;
   if (!aID.IsEmpty()) {
     id = aID;
   } else {
     nsCOMPtr<nsIUUIDGenerator> uuidgen =
@@ -895,18 +898,17 @@ Notification::CreateInternal(const nsASt
     NS_ENSURE_SUCCESS(rv, nullptr);
 
     char buffer[NSID_LENGTH];
     uuid.ToProvidedString(buffer);
     NS_ConvertASCIItoUTF16 convertedID(buffer);
     id = convertedID;
   }
 
-  nsRefPtr<Notification> notification = new Notification(id,
-                                                         aTitle,
+  nsRefPtr<Notification> notification = new Notification(aGlobal, id, aTitle,
                                                          aOptions.mBody,
                                                          aOptions.mDir,
                                                          aOptions.mLang,
                                                          aOptions.mTag,
                                                          aOptions.mIcon,
                                                          aOptions.mMozbehavior);
   return notification.forget();
 }
@@ -2269,21 +2271,18 @@ Notification::CreateAndShow(nsIGlobalObj
                             ErrorResult& aRv)
 {
   MOZ_ASSERT(aGlobal);
 
   AutoJSAPI jsapi;
   jsapi.Init(aGlobal);
   JSContext* cx = jsapi.cx();
 
-  nsRefPtr<Notification> notification = CreateInternal(EmptyString(),
-                                                       aTitle,
-                                                       aOptions);
-
-  notification->BindToOwner(aGlobal);
+  nsRefPtr<Notification> notification = CreateInternal(aGlobal, EmptyString(),
+                                                       aTitle, aOptions);
 
   // Make a structured clone of the aOptions.mData object
   JS::Rooted<JS::Value> data(cx, aOptions.mData);
   notification->InitFromJSVal(cx, data, aRv);
   if (aRv.Failed()) {
     return nullptr;
   }
 
--- a/dom/notification/Notification.h
+++ b/dom/notification/Notification.h
@@ -284,24 +284,24 @@ public:
                                               ErrorResult& aRv);
 
   static NotificationPermission GetPermissionInternal(nsIPrincipal* aPrincipal,
                                                       ErrorResult& rv);
 
   bool DispatchClickEvent();
   bool DispatchNotificationClickEvent();
 protected:
-  // Callers MUST bind the Notification to the correct DOMEventTargetHelper parent using
-  // BindToOwner().
-  Notification(const nsAString& aID, const nsAString& aTitle, const nsAString& aBody,
+  Notification(nsIGlobalObject* aGlobal, const nsAString& aID,
+               const nsAString& aTitle, const nsAString& aBody,
                NotificationDirection aDir, const nsAString& aLang,
                const nsAString& aTag, const nsAString& aIconUrl,
                const NotificationBehavior& aBehavior);
 
-  static already_AddRefed<Notification> CreateInternal(const nsAString& aID,
+  static already_AddRefed<Notification> CreateInternal(nsIGlobalObject* aGlobal,
+                                                       const nsAString& aID,
                                                        const nsAString& aTitle,
                                                        const NotificationOptions& aOptions);
 
   void ShowInternal();
   void CloseInternal();
 
   static NotificationPermission GetPermissionInternal(nsISupports* aGlobal,
                                                       ErrorResult& rv);