MacPWAs: Work around hang due to -[NSWindow hasKeyAppearance]

The function -[NativeWidgetMacNSWindow hasKeyAppearance] is called on
non-main threads (particularly often on macOS 10.11). This is
problematic because it calls into either a mojo interface or into
a ui::Widget that the main thread expects to have exclusive access to.

Work around this by calling -[super hasKeyAppearance] if we are not
on the main thread.

TBR=ccameron@chromium.org

(cherry picked from commit 6c8aad7a5526cf4f7f6f1e49987545509b00ee9a)

Bug: 941506
Change-Id: I6a05921c3ae9821b562a33723e5e3207d0fde73e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1522005
Commit-Queue: Avi Drissman <avi@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#640640}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1528485
Reviewed-by: ccameron <ccameron@chromium.org>
Cr-Commit-Position: refs/branch-heads/3683@{#838}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/ui/views_bridge_mac/native_widget_mac_nswindow.mm b/ui/views_bridge_mac/native_widget_mac_nswindow.mm
index 54ec2c8..fac57d3 100644
--- a/ui/views_bridge_mac/native_widget_mac_nswindow.mm
+++ b/ui/views_bridge_mac/native_widget_mac_nswindow.mm
@@ -200,6 +200,12 @@
 
 // Lets the traffic light buttons on the parent window keep their active state.
 - (BOOL)hasKeyAppearance {
+  // Note that this function is called off of the main thread. In such cases,
+  // it is not safe to access the mojo interface or the ui::Widget, as they are
+  // not reentrant.
+  // https://crbug.com/941506.
+  if (![NSThread isMainThread])
+    return [super hasKeyAppearance];
   if (bridgeImpl_) {
     bool isAlwaysRenderWindowAsKey = NO;
     bridgeImpl_->host()->GetAlwaysRenderWindowAsKey(&isAlwaysRenderWindowAsKey);