Stop Forwarding SingletonHwnd Messages If There is No MessageLoop Set

If there is no MessageLoop and SingletonHwnd is receiving messages,
this means it is receiving messages via an external message pump
such as COM Uninitialization.

It is unsafe to forward these messages as observers may depend on
the existence of a MessageLoop to proceed.

BUG=912626

Change-Id: I9d6e76a03931db59450ac4b3d17c6aeaa365061b
Reviewed-on: https://chromium-review.googlesource.com/c/1366278
Reviewed-by: Scott Violet <sky@chromium.org>
Commit-Queue: Robert Liao <robliao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#614737}
diff --git a/ui/gfx/win/singleton_hwnd.cc b/ui/gfx/win/singleton_hwnd.cc
index d97adfa..28e567b 100644
--- a/ui/gfx/win/singleton_hwnd.cc
+++ b/ui/gfx/win/singleton_hwnd.cc
@@ -21,6 +21,16 @@
                                          LPARAM lparam,
                                          LRESULT& result,
                                          DWORD msg_map_id) {
+  if (!base::MessageLoopCurrentForUI::IsSet()) {
+    // If there is no MessageLoop and SingletonHwnd is receiving messages, this
+    // means it is receiving messages via an external message pump such as COM
+    // uninitialization.
+    //
+    // It is unsafe to forward these messages as observers may depend on the
+    // existence of a MessageLoop to proceed.
+    return false;
+  }
+
   for (SingletonHwndObserver& observer : observer_list_)
     observer.OnWndProc(window, message, wparam, lparam);
   return false;