Bug 1453925 - BroadcastChannel doesn't need to use a runnable to dispatch messages, r=asuth
authorAndrea Marchesini <amarchesini@mozilla.com>
Sat, 14 Apr 2018 01:01:52 +0200
changeset 414089 71b23fab4c0b8a450cf906a725f55171c0e81638
parent 414088 e2b49506d98cfc7dba7a15ce28e9c4b35a8328ed
child 414090 038b4d1d073af308be6aa92c2fa2c2e594ea2222
push id33858
push userncsoregi@mozilla.com
push dateTue, 17 Apr 2018 21:55:44 +0000
treeherdermozilla-central@d6eb5597d744 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersasuth
bugs1453925
milestone61.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 1453925 - BroadcastChannel doesn't need to use a runnable to dispatch messages, r=asuth
dom/broadcastchannel/BroadcastChannel.cpp
dom/broadcastchannel/tests/broadcastchannel_worker_alive.js
dom/broadcastchannel/tests/test_event_listener_leaks.html
--- a/dom/broadcastchannel/BroadcastChannel.cpp
+++ b/dom/broadcastchannel/BroadcastChannel.cpp
@@ -120,58 +120,16 @@ public:
 
 private:
   RefPtr<ThreadSafeWorkerRef> mWorkerRef;
   nsACString& mOrigin;
   PrincipalInfo& mPrincipalInfo;
   ErrorResult& mRv;
 };
 
-class BCPostMessageRunnable final : public nsIRunnable,
-                                    public nsICancelableRunnable
-{
-public:
-  NS_DECL_ISUPPORTS
-
-  BCPostMessageRunnable(BroadcastChannelChild* aActor,
-                        BroadcastChannelMessage* aData)
-    : mActor(aActor)
-    , mData(aData)
-  {
-    MOZ_ASSERT(mActor);
-  }
-
-  NS_IMETHOD Run() override
-  {
-    MOZ_ASSERT(mActor);
-    if (mActor->IsActorDestroyed()) {
-      return NS_OK;
-    }
-
-    ClonedMessageData message;
-    mData->BuildClonedMessageDataForBackgroundChild(mActor->Manager(), message);
-    mActor->SendPostMessage(message);
-    return NS_OK;
-  }
-
-  nsresult Cancel() override
-  {
-    mActor = nullptr;
-    return NS_OK;
-  }
-
-private:
-  ~BCPostMessageRunnable() {}
-
-  RefPtr<BroadcastChannelChild> mActor;
-  RefPtr<BroadcastChannelMessage> mData;
-};
-
-NS_IMPL_ISUPPORTS(BCPostMessageRunnable, nsICancelableRunnable, nsIRunnable)
-
 class CloseRunnable final : public nsIRunnable,
                             public nsICancelableRunnable
 {
 public:
   NS_DECL_ISUPPORTS
 
   explicit CloseRunnable(BroadcastChannel* aBC)
     : mBC(aBC)
@@ -393,45 +351,28 @@ void
 BroadcastChannel::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
                               ErrorResult& aRv)
 {
   if (mState != StateActive) {
     aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
     return;
   }
 
-  PostMessageInternal(aCx, aMessage, aRv);
-}
-
-void
-BroadcastChannel::PostMessageInternal(JSContext* aCx,
-                                      JS::Handle<JS::Value> aMessage,
-                                      ErrorResult& aRv)
-{
   RefPtr<BroadcastChannelMessage> data = new BroadcastChannelMessage();
 
   data->Write(aCx, aMessage, aRv);
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
-  PostMessageData(data);
-}
-
-void
-BroadcastChannel::PostMessageData(BroadcastChannelMessage* aData)
-{
   RemoveDocFromBFCache();
 
-  RefPtr<BCPostMessageRunnable> runnable =
-    new BCPostMessageRunnable(mActor, aData);
-
-  if (NS_FAILED(NS_DispatchToCurrentThread(runnable))) {
-    NS_WARNING("Failed to dispatch to the current thread!");
-  }
+  ClonedMessageData message;
+  data->BuildClonedMessageDataForBackgroundChild(mActor->Manager(), message);
+  mActor->SendPostMessage(message);
 }
 
 void
 BroadcastChannel::Close()
 {
   if (mState != StateActive) {
     return;
   }
--- a/dom/broadcastchannel/tests/broadcastchannel_worker_alive.js
+++ b/dom/broadcastchannel/tests/broadcastchannel_worker_alive.js
@@ -1,8 +1,7 @@
-(new BroadcastChannel('foobar')).postMessage('READY');
-
 (new BroadcastChannel('foobar')).addEventListener('message', function(event) {
   if (event.data != 'READY') {
     event.target.postMessage(event.data);
   }
 });
 
+(new BroadcastChannel('foobar')).postMessage('READY');
--- a/dom/broadcastchannel/tests/test_event_listener_leaks.html
+++ b/dom/broadcastchannel/tests/test_event_listener_leaks.html
@@ -18,20 +18,20 @@
 // exercise the leak condition.
 let count = 0;
 async function useBroadcastChannel(contentWindow) {
   contentWindow.messageCount = 0;
 
   count += 1;
   const name = `test_event_listener_leaks-${count}`;
 
+  let bc = new contentWindow.BroadcastChannel(name);
   let outer = new BroadcastChannel(name);
   outer.postMessage('foo');
 
-  let bc = new contentWindow.BroadcastChannel(name);
   await new Promise(resolve => {
     bc.onmessage = e => {
       contentWindow.messageCount += 1;
       resolve();
     };
   });
 
   is(contentWindow.messageCount, 1, "message should be received");