Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Support pageAction show/hide wrapping #59

Merged
merged 4 commits into from
Mar 13, 2018

Conversation

rpl
Copy link
Member

@rpl rpl commented Sep 26, 2017

This PR contains the changes to the api-metadata.json from #9 and it also applies the changes needed into the polyfill wrappers to be able to customize the wrapper behavior for the API methods that returns a Promise on Firefox but do not accept a callback on Chrome (which is the case for pageAction.show and hide).

@rpl
Copy link
Member Author

rpl commented Sep 26, 2017

@Rob--W do you mind to take a look at this PR?

similarly to #57 it is going to change the api-metadata.json but it also needs some additional changes in the polyfill wrappers, to be able to customize the behavior for the kind on chrome incompatibility that pageAction.show/hide presents.

@rpl rpl mentioned this pull request Sep 26, 2017
@Rob--W
Copy link
Member

Rob--W commented Sep 28, 2017

This doesn't look like a forward-compatible change to me.

Yes, it would fix the current error.
However, if Chrome were to support the callback parameter (which I expect to happen - #13 (comment), I've wanted to submit a patch myself, but that's near the bottom of my priority list), then the polyfill will cause an error again.

How about trying to call with callback, and if a synchronous (schema) error about unsupported callbacks occurs, invoke the method without callback?

try { chrome.browserAction.setTitle(1, function(){}); } catch (e) { console.log(e.message); }

gives the following result (tested Chrome 34 - 61:

Invocation of form browserAction.setTitle(integer, function) doesn't match definition browserAction.setTitle(object details)

and when --native-crx-bindings=1 is set (tested with Chrome 61) (Chrome is working on replacing JS-implemented extension bindings with C++):

Error in invocation of browserAction.setTitle(object details): Too many arguments.

PS. Why does browserAction.setTitle not appear in api-metadata.json?

console.warn(`${name} API method doesn't seem to support the callback parameter, ` +
"falling back to call it without a callback: ", cbError);

// Update the API method metadata, so that the next API call will not try to
Copy link
Member

Choose a reason for hiding this comment

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

Move this after line 170, to make sure that the API call is actually valid before considering the API callback-less.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 great call!

@Rob--W
Copy link
Member

Rob--W commented Sep 28, 2017

The browserAction API map is missing entries like browser.runtime.enable.

I guess that the JSON file is manually edited. Maybe it helps if you write a quick script to load the JSON files from mozilla-central and compare it with the existing definitions to see if anything is missing.

@rpl rpl force-pushed the fix/pageAction-show-hide-wrappers branch from 65b8475 to 5882834 Compare September 29, 2017 16:46
@rpl
Copy link
Member Author

rpl commented Sep 29, 2017

@Rob--W I've updated this PR based on your last round of review comments and updated the test cases accordingly, let me know how it looks to you.

Thanks again for you help!

function callAPIWithNoCallback() {
try {
target[name](...args);
if (chrome.runtime.lastError) {
Copy link
Member

Choose a reason for hiding this comment

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

This chrome.runtime.lastError check seems misplaced. The target[name](...args) call is asynchronous, so the error will never be set to that.

Worse, with the current code, if someone tries to use call e.g. browser.pageAction.show inside a function where lastError is set, then the promise will always be rejected.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 it was already supposed to be removed and I forgot, thanks for pointing it out.

try {
target[name](...args, makeCallback({resolve, reject}, metadata));
} catch (cbError) {
console.warn(`${name} API method doesn't seem to support the callback parameter, ` +
Copy link
Member

Choose a reason for hiding this comment

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

Under normal circumstances, this message will be displayed once.

This message will be displayed more than once if the extension passes the wrong arguments. Do you consider that to be OK? (personally I think that it is fine, since the code is simpler in this way; if you want to force the message to be displayed once, then it needs to be moved inside the else block above.

Why do we need log spam in the first place?

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought to emit at least a warning log mostly because it is not completely impossible that this runtime discovery can be fooled by some chrome behaviors (maybe a future chrome behavior) that the polyfill is not expecting, and it could completely hide the real error.

Emitting the warning every time we are actually falling back between the "with callback" to the "no callback" mode is fine from my current point of view, in the common scenarios it should only be produced once, and if it is produced more than once, then it could be a signal of something different between the behavior that it expect and the actual one.

target[name](...args);
if (chrome.runtime.lastError) {
// NOTE: Ideally this should rejects if there are runtime issues with
// the API call on Chrome, unfortunately currently when pageAction.show/hide
Copy link
Member

Choose a reason for hiding this comment

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

This whole comment block will go probably go away because of my above comment. But if you decide to re-instate the comments somewhere, generalize the affected APIs, e.g. "APIs such as pageAction.show" because pageAction.show/hide are not the only affected APIs.

@rpl rpl force-pushed the fix/pageAction-show-hide-wrappers branch from 5882834 to dbc376b Compare September 29, 2017 19:13
@rpl
Copy link
Member Author

rpl commented Sep 29, 2017

@Rob--W suggested changes applied in the updated patch

Copy link
Member

@Rob--W Rob--W left a comment

Choose a reason for hiding this comment

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

Looks good. I have posted one optional suggestion. The PR is fine either way.

// Catch API parameters validation errors and rejects the promise.
reject(error);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Now the callAPIWithNoCallback logic is very simple, it can be inlined below, without try-catch (because you are in the closure of a Promise).

The second callAPIWithNoCallback call does not require the metadata to be updated, so it can simply be:

target[name](...args);
resolve();

kirlat pushed a commit to alpheios-project/inflection-tables that referenced this pull request Oct 5, 2017
… Mozilla's webextension-polyfill. Had to change polyfill code with a temporary fix for a PageAction show bug: mozilla/webextension-polyfill#59.
@ermik
Copy link

ermik commented Feb 6, 2018

Expected at most 0 arguments for show(), got 1 is kind of killing me.

@Treora
Copy link
Contributor

Treora commented Mar 13, 2018

Would be great if this can be merged indeed, I stumbled upon #24 as well. Is this PR still held back by anything?

I notice one unprocessed review comment; I made a PR to this branch that inlines the function as indicated.

@rpl rpl force-pushed the fix/pageAction-show-hide-wrappers branch from dbc376b to 80a2fd5 Compare March 13, 2018 13:22
@rpl
Copy link
Member Author

rpl commented Mar 13, 2018

@Treora Thanks a lot for your PR! I've rebased this branch, added your changes on top of the commit already part of this PR and added pageAction.show to the smoke tests that runs on Chrome.

I'm going to land this PR as soon as the Travis job run successfully.

@rpl rpl merged commit 0fd5086 into mozilla:master Mar 13, 2018
kirlat pushed a commit to alpheios-project/alpheios-core that referenced this pull request Feb 10, 2020
… Mozilla's webextension-polyfill. Had to change polyfill code with a temporary fix for a PageAction show bug: mozilla/webextension-polyfill#59.
@rpl rpl deleted the fix/pageAction-show-hide-wrappers branch March 15, 2021 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants