-
Notifications
You must be signed in to change notification settings - Fork 215
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
Conversation
This doesn't look like a forward-compatible change to me. Yes, it would fix the current error. 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:
and when
PS. Why does browserAction.setTitle not appear in api-metadata.json? |
src/browser-polyfill.js
Outdated
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 great call!
The 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. |
65b8475
to
5882834
Compare
@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! |
src/browser-polyfill.js
Outdated
function callAPIWithNoCallback() { | ||
try { | ||
target[name](...args); | ||
if (chrome.runtime.lastError) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
src/browser-polyfill.js
Outdated
try { | ||
target[name](...args, makeCallback({resolve, reject}, metadata)); | ||
} catch (cbError) { | ||
console.warn(`${name} API method doesn't seem to support the callback parameter, ` + |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
src/browser-polyfill.js
Outdated
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 |
There was a problem hiding this comment.
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.
5882834
to
dbc376b
Compare
@Rob--W suggested changes applied in the updated patch |
There was a problem hiding this 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.
src/browser-polyfill.js
Outdated
// Catch API parameters validation errors and rejects the promise. | ||
reject(error); | ||
} | ||
} |
There was a problem hiding this comment.
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();
… Mozilla's webextension-polyfill. Had to change polyfill code with a temporary fix for a PageAction show bug: mozilla/webextension-polyfill#59.
|
…e pageAction.show/hide wrappers behavior
dbc376b
to
80a2fd5
Compare
@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. |
… Mozilla's webextension-polyfill. Had to change polyfill code with a temporary fix for a PageAction show bug: mozilla/webextension-polyfill#59.
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).