Skip to content

Commit

Permalink
fix: promise support with webFrameMain.executeJavaScript (#35358)
Browse files Browse the repository at this point in the history
* fix: promise support with webFrameMain.executeJavaScript

* chore: reject when result is an error

Co-authored-by: deepak1556 <hop2deep@gmail.com>
  • Loading branch information
trop[bot] and deepak1556 committed Aug 23, 2022
1 parent 3f6b9a8 commit a415eb6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
32 changes: 20 additions & 12 deletions shell/browser/api/electron_api_web_frame_main.cc
Expand Up @@ -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"
Expand Down Expand Up @@ -143,17 +144,24 @@ v8::Local<v8::Promise> 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<base::Value> promise,
base::Value value) { promise.Resolve(value); },
std::move(promise)));
static_cast<content::RenderFrameHostImpl*>(render_frame_)
->ExecuteJavaScriptForTests(
code, user_gesture, true /* resolve_promises */,
content::ISOLATED_WORLD_ID_GLOBAL,
base::BindOnce(
[](gin_helper::Promise<base::Value> 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;
}
Expand Down
38 changes: 38 additions & 0 deletions spec-main/api-web-frame-main-spec.ts
Expand Up @@ -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', () => {
Expand Down

0 comments on commit a415eb6

Please sign in to comment.