fix: promise support with webFrameMain.executeJavaScript (#35292)

* fix: promise support with webFrameMain.executeJavaScript

* chore: reject when result is an error
This commit is contained in:
Robo 2022-08-17 13:08:13 +09:00 committed by GitHub
parent 8e4a168a13
commit 43182bf030
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 12 deletions

View file

@ -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<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;
}

View file

@ -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', () => {