-
Notifications
You must be signed in to change notification settings - Fork 312
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
dynamically registered event listeners for extension service worker #1698
Comments
Looping in @oliverdunk and @patrickkettner who may have opinions on this. |
Thanks for opening this Jackie. As we discussed in the last WECG meeting, I think improving extension events is something everyone is supportive of longer term and very curious to see if any web folk have thoughts on how we can make things more ergonomic. That said, I'm not sure if this proposal is the right one:
I'd prefer to look at ways of using |
To understand this proposal, we must first understand the current way of working. The current way of working:
This proposal is
Anyway, in order to be able to dynamically add or remove, enable or disable event handlers, the browser must remember some additional information beyond service worker lifecycle. |
I think specifying the function name dynamically via a string is a non-starter. Although there is precedent in HTML for inline JS that could be said to resemble this, it's bad precedent and is a major source of XSS issues. For the particular use-case of WebExtensions that seems to be motivating this request, my general impression from discussion with our (Mozilla's) add-on teams is that one of our greatest concerns is dynamism in WebExtensions that enables bad actors to create extensions that seem benign but later are dynamically reconfigured to violate the user's privacy and/or security. An API surface like this would intentionally create problems for static analyses. If the high-level goal is for WebExtensions to be able to reduce their performance impact on the browser until the user activates specific functionality, I would suggest that the correct approach for this is to have the enabling/disabling located on the WebExtension API surface that would be responsible for generating the event. Individual WebExtension logic can then add event listeners using addEventListener in the usual way. This a more appropriate layering for efficiency and devtools integration. NavigationPreloadManager is not a perfect example of this, but is a pattern that WECG could follow if there somehow is not already an existing idiom for this. |
Note that I'm talking about browser extension service worker, not Web service worker. But I would like to seek advice from Web service worker experts here.
Unlike the web environment, browser extensions usually provide users with some functional options in the extension settings interface. Ideally, the extension only registers relevant events in extension service worker when the user turns on a feature. So some developers are proposing to dynamically register events at WECG.
Here is my proposal:
At present, an extension
Event
object has these method:For dynamically registered events for service worker, I proposal these new methods instead of changing existing methods:
For example (there are lots of different events in browser extensions):
"FunctionName" is a global function name that declared in service worker, rather than a function object, and doesn't need to be registered in the first event loop when service worker wakes up. This allows the browser to remember both the event and this specific function independently of service worker's lifecycle, not just remember the event like addListener() does.
These new methods can be called in both service-worker context and non-service-worker context (e.g. a user setting page) dynamically, but only trigger events in service worker context, not in other contexts.
After calling
some_event.subscribe(function-name)
, the browser triggers related events in service worker. If the service worker is inactive, wake up it first. Then looks for the function (by name) to execute.I'd love to hear from the browser implementation perspective, such as whether it's possible or what problems it will encounter.
The text was updated successfully, but these errors were encountered: