Repository navigation
feat: bring back optional callbacks for WebFrame#executeJavaScript* methods - #21423
Conversation
|
New PR. I mentioned before, I think there is a commit hook that's converting .md line endings to crlf. I am editing the file as lf. |
@bughit revert the line endings change and then use |
cea335d to
a67fbf5
Compare
done |
|
@MarshallOfSound Is there anything I need to do for this? |
nornagon
left a comment
There was a problem hiding this comment.
Mostly LGTM. A few tweaks suggested, and needs tests. Please test at least:
- that both the promise and the callback are called, in both success and error cases
- that the callback is called synchronously when the context isn't suspended
| public: | ||
| // for compatibility with the older version of this, error is after result | ||
| using CompletionCallback = | ||
| base::Callback<void(const v8::Local<v8::Value>& result, |
There was a problem hiding this comment.
| base::Callback<void(const v8::Local<v8::Value>& result, | |
| base::OnceCallback<void(const v8::Local<v8::Value>& result, |
There was a problem hiding this comment.
this required using std::move which I'm not confident about, so please check those.
|
I don't think it is a good idea to change the behavior of API depending on whether a parameter is passed, it would make the API hard to understand and easy to misuse. It is probably easier to add a new API instead of changing the current one. |
The optional callback does not change the core behavior of the api, nothing about how js is executed changes. There are or have been other APIs that take optional callbacks. I don't think you can describe them as "changing their behavior", they do the same core work, and just call back optionally.
Here's the documentation for the optional callback:
Everything important about it described, it's function, it's optional, it's called after script execution, synchronously (with the modal dialog exception) Why do you say that's hard to understand?
How?
I've already tried (#21315, #21371). This PR is a compromise that I understood was acceptable, @nornagon just asked for a test which I haven't had a chance to add. Is that no longer the case? Then what would be acceptable? |
|
Thanks for the clarification, I'm good with this PR then. |
6204914 to
bc8abda
Compare
@nornagon What can be done about the failing test for the error case? It seems the synthetic error is being caught by the test framework, which alters the normal flow of the call. |
610f1de to
6954629
Compare
|
@bughit not sure how to answer that as the test you're referring to doesn't seem to exist in the code..? |
I removed the test to get everything else checked. I haven't been able to run tests locally due to first #22343, then #22400, #22432. Here it is: it('executeJavaScript() yields errors via a promise and a sync callback', done => {
let callbackResult, callbackError
webFrame
.executeJavaScript('thisShouldProduceAnError()', (result, error) => {
callbackResult = result
callbackError = error
})
.then(
promiseResult => {
done(new Error('error is expected'))
},
promiseError => {
expect(promiseError).to.be.an('error')
done()
}
)
expect(callbackResult).to.be.undefined()
expect(callbackError).to.be.an('error')
})I manually tested it of course, it's fine, but the test framework appears to treat the synthetic eval error as a test failure. |
Looks like you're not waiting for the promise to be completed before calling |
The callback is synchronous, so callbackError is available right after the call. In fact this assertion is what tests the syncness. The same thing is done in the working test. So this is not the problem. Did you see the the test runner output ( |
|
Ah, I see the issue. The test is executing the error-producing function in the frame that's running the tests itself, and mocha is catching "uncaught" errors. Here's a way around that by using |
6954629 to
84665f3
Compare
|
Adding a test for You can confirm it with Fixing this is not really in the scope of this PR, as the problem applies equally to the callback and promise, but if it's quick, and since the test is already here, perhaps it makes to include it? How would you like to proceed? |
|
in this case |
|
If the bug is already present in existing code, I think it's fine to leave that to a followup PR. |
84665f3 to
4ac689a
Compare
|
ok, I commented out (with an explanation) the executeJavaScriptInIsolatedWorld error test. I believe everything else you requested is there. |
|
executeJavascriptInIsolatedWorld rejection issue: #22490
|
|
Release Notes Persisted
|
|
Can this be backported to 9? |
|
@nornagon My understanding is that you allow backporting to the beta branch, is that not so? And have an automated way to do that, assuming it merges cleanly. Can that be done here? |
|
/trop run backport-to 9-x-y |
|
The backport process for this PR has been manually initiated - |
|
I have automatically backported this PR to "9-x-y", please check out #22501 |
Description of Change
Before the promisification (#17312) of
WebFrame#executeJavaScript*their interface was async (callback) but behavior (with some exceptions, e.g. page being suspended due to a modal alert) was sync (callback called before the methods returned). So they could be used in sync mode.This PR brings back optional callbacks, which are invoked in addition to promise resolution.
Fixes #19161
Checklist
npm testpassesRelease Notes
Notes: Allow an optional callback parameter for WebFrame.executeJavaScript* methods, which is called synchronously unless the target context is paused.