make event delayed. draft
authorBrad Werth <bwerth@mozilla.com>
Tue, 20 Feb 2018 11:29:29 -0800
changeset 757492 ac02fffbe1045d587369412450434ee943b8da45
parent 757491 6ce6145ae78ae8c82f5adb3e1774e2a5315d2c7d
child 757493 24a1af144407dc0f1ac769530f2b47448c9e9777
push id99771
push userbwerth@mozilla.com
push dateTue, 20 Feb 2018 19:37:13 +0000
milestone60.0a1
make event delayed. MozReview-Commit-ID: 2ybsh1BVpg3
image/RasterImage.cpp
--- a/image/RasterImage.cpp
+++ b/image/RasterImage.cpp
@@ -1789,18 +1789,43 @@ RasterImage::NotifyDecodeComplete(const 
       if (mWantFullDecode) {
         mWantFullDecode = false;
         RequestDecodeForSize(mSize, DECODE_FLAGS_DEFAULT);
       }
     } else {
       // We've decoded fully; if we're still waiting to send the
       // load event, do it now.
       if (mLoadProgress) {
-        NotifyForLoadEvent(*mLoadProgress);
+        // If we intentionally delayed until full decode, delay
+        // dispatch of the event itself, since we want to ensure that
+        // whatever needed the decode has time to act on it (reflow,
+        // for example).
+        Progress progress = *mLoadProgress;
         mLoadProgress = Nothing();
+
+        if (mDelayLoadEventUntilDecode) {
+          // Dispatch the event as a runnable.
+          nsCOMPtr<nsIEventTarget> eventTarget;
+          if (mProgressTracker) {
+            eventTarget = mProgressTracker->GetEventTarget();
+          } else {
+            eventTarget = do_GetMainThread();
+          }
+
+          RefPtr<RasterImage> image = this;
+          nsCOMPtr<nsIRunnable> ev = NS_NewRunnableFunction(
+                                    "RasterImage::NotifyDecodeComplete",
+                                    [=]() -> void {
+            image->NotifyForLoadEvent(progress);
+          });
+          eventTarget->Dispatch(ev.forget(), NS_DISPATCH_NORMAL);
+        } else {
+          // Run the event directly.
+          NotifyForLoadEvent(progress);
+        }
       }
     }
   }
 }
 
 void
 RasterImage::ReportDecoderError()
 {