diff --git a/shell/browser/api/electron_api_web_frame_main.cc b/shell/browser/api/electron_api_web_frame_main.cc index f4bbe118ba24..178d3e30bda9 100644 --- a/shell/browser/api/electron_api_web_frame_main.cc +++ b/shell/browser/api/electron_api_web_frame_main.cc @@ -11,8 +11,9 @@ #include "base/logging.h" #include "base/no_destructor.h" -#include "content/browser/renderer_host/frame_tree_node.h" // nogncheck +#include "content/browser/renderer_host/render_frame_host_impl.h" // nogncheck #include "content/public/browser/render_frame_host.h" +#include "content/public/common/isolated_world_ids.h" #include "electron/shell/common/api/api.mojom.h" #include "gin/object_template_builder.h" #include "services/service_manager/public/cpp/interface_provider.h" @@ -141,17 +142,24 @@ v8::Local WebFrameMain::ExecuteJavaScript( return handle; } - if (user_gesture) { - auto* ftn = content::FrameTreeNode::From(render_frame_); - ftn->UpdateUserActivationState( - blink::mojom::UserActivationUpdateType::kNotifyActivation, - blink::mojom::UserActivationNotificationType::kTest); - } - - render_frame_->ExecuteJavaScriptForTests( - code, base::BindOnce([](gin_helper::Promise promise, - base::Value value) { promise.Resolve(value); }, - std::move(promise))); + static_cast(render_frame_) + ->ExecuteJavaScriptForTests( + code, user_gesture, true /* resolve_promises */, + content::ISOLATED_WORLD_ID_GLOBAL, + base::BindOnce( + [](gin_helper::Promise promise, + blink::mojom::JavaScriptExecutionResultType type, + base::Value value) { + if (type == + blink::mojom::JavaScriptExecutionResultType::kSuccess) { + promise.Resolve(value); + } else { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::HandleScope scope(isolate); + promise.Reject(gin::ConvertToV8(isolate, value)); + } + }, + std::move(promise))); return handle; } diff --git a/spec/api-web-frame-main-spec.ts b/spec/api-web-frame-main-spec.ts index 46178641e500..8c3e0e37e1f2 100644 --- a/spec/api-web-frame-main-spec.ts +++ b/spec/api-web-frame-main-spec.ts @@ -163,6 +163,44 @@ describe('webFrameMain module', () => { expect(await getUrl(webFrame.frames[0])).to.equal(fileUrl('frame-with-frame.html')); expect(await getUrl(webFrame.frames[0].frames[0])).to.equal(fileUrl('frame.html')); }); + + it('can resolve promise', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } }); + await w.loadFile(path.join(subframesPath, 'frame.html')); + const webFrame = w.webContents.mainFrame; + const p = () => webFrame.executeJavaScript('new Promise(resolve => setTimeout(resolve(42), 2000));'); + const result = await p(); + expect(result).to.equal(42); + }); + + it('can reject with error', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } }); + await w.loadFile(path.join(subframesPath, 'frame.html')); + const webFrame = w.webContents.mainFrame; + const p = () => webFrame.executeJavaScript('new Promise((r,e) => setTimeout(e("error!"), 500));'); + await expect(p()).to.be.eventually.rejectedWith('error!'); + const errorTypes = new Set([ + Error, + ReferenceError, + EvalError, + RangeError, + SyntaxError, + TypeError, + URIError + ]); + for (const error of errorTypes) { + await expect(webFrame.executeJavaScript(`Promise.reject(new ${error.name}("Wamp-wamp"))`)) + .to.eventually.be.rejectedWith(/Error/); + } + }); + + it('can reject when script execution fails', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } }); + await w.loadFile(path.join(subframesPath, 'frame.html')); + const webFrame = w.webContents.mainFrame; + const p = () => webFrame.executeJavaScript('console.log(test)'); + await expect(p()).to.be.eventually.rejectedWith(/ReferenceError/); + }); }); describe('WebFrame.reload', () => {