Skip to content

feat: bring back optional callbacks for WebFrame#executeJavaScript* methods - #21423

Merged
nornagon merged 1 commit into
electron:masterfrom
bughit:web_frame_js_exec_callback
Mar 3, 2020
Merged

nornagon merged 1 commit into
electron:masterfrom
bughit:web_frame_js_exec_callback

Conversation

@bughit

@bughit bughit commented Dec 6, 2019 •

Copy link
Copy Markdown
Contributor

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

Release Notes

Notes: Allow an optional callback parameter for WebFrame.executeJavaScript* methods, which is called synchronously unless the target context is paused.

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Dec 6, 2019
@bughit

bughit commented Dec 6, 2019

Copy link
Copy Markdown
Contributor Author

@MarshallOfSound @nornagon

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.

@MarshallOfSound

Copy link
Copy Markdown
Member

I am editing the file as lf.

@bughit revert the line endings change and then use git commit --ammend --no-verify and it should bypass the commit hook

@bughit
bughit force-pushed the web_frame_js_exec_callback branch from cea335d to a67fbf5 Compare December 6, 2019 22:30
@bughit

bughit commented Dec 6, 2019

Copy link
Copy Markdown
Contributor Author

@bughit revert the line endings change and then use git commit --ammend --no-verify and it should bypass the commit hook

done

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Dec 7, 2019
@bughit

bughit commented Dec 12, 2019

Copy link
Copy Markdown
Contributor Author

@MarshallOfSound Is there anything I need to do for this?

@nornagon nornagon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
base::Callback<void(const v8::Local<v8::Value>& result,
base::OnceCallback<void(const v8::Local<v8::Value>& result,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this required using std::move which I'm not confident about, so please check those.

@zcbenz

zcbenz commented Jan 21, 2020

Copy link
Copy Markdown
Contributor

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.

@bughit

bughit commented Jan 21, 2020

Copy link
Copy Markdown
Contributor Author

I don't think it is a good idea to change the behavior of API depending on whether a parameter is passed

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.

it would make the API hard to understand

Here's the documentation for the optional callback:

callback Function (optional) - Called after script has been executed. Unless
the frame is suspended (e.g. showing a modal alert), execution will be
synchronous and the callback will be invoked before the method returns.

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?

and easy to misuse.

How?

It is probably easier to add a new API instead of changing the current one.

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?

@zcbenz

zcbenz commented Jan 21, 2020

Copy link
Copy Markdown
Contributor

Thanks for the clarification, I'm good with this PR then.

@zcbenz zcbenz added the wip ⚒ label Feb 12, 2020
@bughit
bughit force-pushed the web_frame_js_exec_callback branch 8 times, most recently from 6204914 to bc8abda Compare February 25, 2020 02:57
@bughit

bughit commented Feb 25, 2020

Copy link
Copy Markdown
Contributor Author

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

@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.

not ok 48 webFrame module executeJavaScript() yields errors via a promise and a sync callback
  ReferenceError: thisShouldProduceAnError is not defined
      at <anonymous>:1:1
      at WebFrame../lib/renderer/api/web-frame.ts.WebFrame.<computed> [as executeJavaScript] (electron/js2c/renderer_init.js:1513:33)
      at Context.<anonymous> (/home/builduser/project/src/electron/spec/api-web-frame-spec.js:55:8)
ok 49 webFrame module executeJavaScript() yields errors via a promise and a sync callback
not ok 50 webFrame module executeJavaScript() yields errors via a promise and a sync callback
  TypeError: Cannot read property 'isPending' of undefined
      at processImmediate (internal/timers.js:439:21)

@bughit
bughit force-pushed the web_frame_js_exec_callback branch 2 times, most recently from 610f1de to 6954629 Compare February 26, 2020 20:58
@bughit
bughit requested a review from nornagon February 26, 2020 21:30
@nornagon

Copy link
Copy Markdown
Contributor

@bughit not sure how to answer that as the test you're referring to doesn't seem to exist in the code..?

@bughit

bughit commented Feb 27, 2020 •

Copy link
Copy Markdown
Contributor Author

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.

@bughit

bughit commented Feb 28, 2020

Copy link
Copy Markdown
Contributor Author

@nornagon So can you accept this without the error test? Keep in mind that there were no executeJavascript tests in api-web-frame-spec before, and now there are some.

If not, then please help with #22432 so I can experiment with tests locally and/or suggest how the error test I posted can be fixed.

@zcbenz zcbenz removed the wip ⚒ label Mar 2, 2020
@nornagon

nornagon commented Mar 2, 2020

Copy link
Copy Markdown
Contributor
  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')
  })

Looks like you're not waiting for the promise to be completed before calling expect(callbackError).to.be.an('error'). Make the test function async and then await the executeJavaScript call perhaps?

@bughit

bughit commented Mar 2, 2020

Copy link
Copy Markdown
Contributor Author

Looks like you're not waiting for the promise to be completed before calling expect(callbackError).to.be.an('error')

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 (ReferenceError: thisShouldProduceAnError is not defined)? It's referring to the the synthetic error which I am deliberately generating to test the error scenario. It's treating this error as a test failure.

@nornagon

nornagon commented Mar 2, 2020

Copy link
Copy Markdown
Contributor

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 webFrame to execute in a child frame instead: https://gist.github.com/nornagon/033e8497e16dae0e4216652cb3bf83ae

@bughit
bughit force-pushed the web_frame_js_exec_callback branch from 6954629 to 84665f3 Compare March 2, 2020 21:21
@bughit

bughit commented Mar 2, 2020

Copy link
Copy Markdown
Contributor Author

Adding a test for executeJavascriptInIsolatedWorld revealed a problem in the original error detection logic added by @MarshallOfSound in 0fc172f

You can confirm it with try { await webFrame.executeJavaScriptInIsolatedWorld(999, [{code: 'thisShouldProduceAnError()'}]); console.log('this should not be printed');} catch {} in 9.0

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?

@bughit

bughit commented Mar 2, 2020

Copy link
Copy Markdown
Contributor Author

in this case result[0].IsEmpty() is false

@nornagon

nornagon commented Mar 2, 2020

Copy link
Copy Markdown
Contributor

If the bug is already present in existing code, I think it's fine to leave that to a followup PR.

@bughit
bughit force-pushed the web_frame_js_exec_callback branch from 84665f3 to 4ac689a Compare March 2, 2020 23:10
@bughit

bughit commented Mar 2, 2020

Copy link
Copy Markdown
Contributor Author

ok, I commented out (with an explanation) the executeJavaScriptInIsolatedWorld error test. I believe everything else you requested is there.

@bughit

bughit commented Mar 2, 2020

Copy link
Copy Markdown
Contributor Author

executeJavascriptInIsolatedWorld rejection issue: #22490

linux-ia32-testing-tests failure does not appear to be related to this PR

@nornagon
nornagon merged commit 84126a4 into electron:master Mar 3, 2020
@release-clerk

release-clerk Bot commented Mar 3, 2020

Copy link
Copy Markdown

Release Notes Persisted

Allow an optional callback parameter for WebFrame.executeJavaScript* methods, which is called synchronously unless the target context is paused.

@bughit

bughit commented Mar 3, 2020

Copy link
Copy Markdown
Contributor Author

Can this be backported to 9?

@bughit

bughit commented Mar 3, 2020

Copy link
Copy Markdown
Contributor Author

@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?

@nornagon

nornagon commented Mar 3, 2020

Copy link
Copy Markdown
Contributor

/trop run backport-to 9-x-y

@trop

trop Bot commented Mar 3, 2020

Copy link
Copy Markdown
Contributor

The backport process for this PR has been manually initiated -
sending your commits to "9-x-y"!

@trop

trop Bot commented Mar 3, 2020

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "9-x-y", please check out #22501

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

promisifying WebFrame.executeJavaScript(InIsolatedWorld) is a breaking change without a workaround

4 participants