Browse Source

fix: add dispatch_async to replace GetUIThreadTaskRunner

alice 5 months ago
parent
commit
204e195b6f
2 changed files with 13 additions and 17 deletions
  1. 7 8
      shell/common/platform_util_mac.mm
  2. 6 9
      spec/api-shell-spec.ts

+ 7 - 8
shell/common/platform_util_mac.mm

@@ -158,14 +158,13 @@ void OpenExternal(const GURL& url,
           configuration:configuration
       completionHandler:^(NSRunningApplication* _Nullable app,
                           NSError* _Nullable error) {
-        if (error) {
-          content::GetUIThreadTaskRunner({})->PostTask(
-              FROM_HERE,
-              base::BindOnce(std::move(copied_callback), "Failed to open URL"));
-        } else {
-          content::GetUIThreadTaskRunner({})->PostTask(
-              FROM_HERE, base::BindOnce(std::move(copied_callback), ""));
-        }
+        dispatch_async(dispatch_get_main_queue(), ^{
+          if (error) {
+            std::move(copied_callback).Run("Failed to open URL");
+          } else {
+            std::move(copied_callback).Run("");
+          }
+        });
       }];
 }
 

+ 6 - 9
spec/api-shell-spec.ts

@@ -77,7 +77,7 @@ describe('shell module', () => {
       ]);
     });
 
-    it('should focus the external app when opening the URL on macOS', async () => {
+    ifit(process.platform === 'darwin')('should focus the external app when opening the URL on macOS', async () => {
       const testUrl = 'http://127.0.0.1';
       const mainWindow = new BrowserWindow({ show: true, webPreferences: { nodeIntegration: true, contextIsolation: false } });
       await mainWindow.loadURL('about:blank');
@@ -86,14 +86,11 @@ describe('shell module', () => {
         event.preventDefault();
         shell.openExternal(rawTarget);
       });
-
-      if (process.platform === 'darwin') {
-        const blurPromise = once(mainWindow, 'blur');
-        expect(mainWindow.isFocused()).to.be.true();
-        await mainWindow.webContents.executeJavaScript(`window.location.href = '${testUrl}'`);
-        await blurPromise;
-        expect(mainWindow.isFocused()).to.be.false();
-      }
+      const blurPromise = once(mainWindow, 'blur');
+      expect(mainWindow.isFocused()).to.be.true();
+      await mainWindow.webContents.executeJavaScript(`window.location.href = '${testUrl}'`);
+      await blurPromise;
+      expect(mainWindow.isFocused()).to.be.false();
     });
   });