Repository navigation
Package URL handlers - mapping atom:// URIs to package functions - #11399
Conversation
This is used for importing resources into Less without needing to know the path where packages are stored: https://github.com/lee-dohm/red-wavy-underline/blob/a7c36b5b8790016f81f7cf48d840ed44333df7fa/styles/red-wavy-underline.less#L2 |
|
|
|
It looks like electron/electron#4896 (comment) took care of the work for allowing apps to be protcol handlers on OS X and Windows. Linux is still an open question for me. |
@lee-dohm and I discussed this and it's a slightly different thing. |
Does that mean this PR is now waiting on an Electron upgrade to 0.37.4+? |
|
@iolsen This is just a reminder ping that we need to chat about this. :) |
|
@thedaniel That's correct; we would get this from #11474, which we can move forward on now that #11348 has been merged. It looks like setting a default protocol handler via the Electron API is not supported by Linux; I'm actually not sure what it would take to do such a thing on Linux, or if it's even reasonable to be able to do so in a way that works for everyone. |
The MessageRegistry is similar to the CommandRegistry in that it maps string message types to callback functions; however, messages are dispatched to Atom from outside the application using URIs, and so should be more restrictive about what they allow. Signed-off-by: Katrina Uychaco <[email protected]>
0cff287 to
1b7ab00
Compare
|
@BinaryMuse I did a bit of digging around and it seems that xdg-utils is the best way to add a protocol handler on Linux, which seems to be used or integrated in most popular desktop environments XFCE, KDE and Gnome. |
|
I'd prefer a name like "external URL handler" or something similar. My concern about the term "message registry" is that it's a bit general, and presumably there are other ways to message the application such as an OS-provided RPC facility. A name that more closely mirrors the actual implementation might be clearer. I would definitely like to figure out if we're using the existing top-level namespace for anything critical. Even if we are, if we could move those URLs to something like As for registering these handlers, I think we should consider a setup where handlers are registered statically via the Obviously, some amount of arbitrary code execution might be required to interpret a given URL, but I don't think it would be too onerous to require packages to at least register for a specific URL prefix. |
|
Just wanted to re-iterate that having this functionality would be huge. I already used it to prototype a few use cases for potential handlers in Nuclide, and it would enable some very powerful integrations. @nathansobo's point about renaming the API makes sense, since Other than deciding on the name, registry, and namespacing convention, is anything else (maybe the Linux issue?) blocking this from making it into a release? |
|
If we could make this happen, I would be suuuper happy |
|
@BinaryMuse Any chance you'd be willing to pick this up again soon? |
|
I agree with @nathansobo's point that these should be able to be registered statically. I think that this could also sidestep the conversation about the In that vein, it could conceivably be exposed as a service, though up until now, I don't believe that services could include their own static configuration (though really, why not)? |
|
Actually, how bad would it be if it were just a service and you couldn't specify the prefix? That is, what if you were forced to use a prefix that matched the name of your extension? So if you had the following as your extension's Then if Atom received a URI of the form |
|
+1 to @mbolin's suggestion. Maybe we could even simplify things a little and have From searching on github, there's only three No matter what we go with, this has tremendous value to internal Facebook users so I'd be willing to try and push this through myself if @BinaryMuse is busy :P |
Summary: This enables atom://nuclide/path?param=1 URLs to be intercepted and processed from within Nuclide. atom/atom#11399 is an open PR on the Atom side, but likely requires some work and it'll be a while before this is ready. I plan to work on a new proposal to drive that forward, but in the meantime this leverages the existing `urlMain` functionality that Atom provides to make it work. A quick rundown of how this works: - Atom provides a `package.json`-level `urlMain` hook. If provided, `atom://{package}` URIs launch your `urlMain` code rather than the standard window initialization script: https://github.com/atom/atom/blob/master/src/main-process/atom-application.coffee#L627 - This adds a `urlMain` hook for Nuclide. There are two cases to handle: This comes with tests to ensure that the core functionality continues to work across Atom versions. It's by no means complete, but I imagine it should hold us over until we can upstream a saner method. Reviewed By: zertosh Differential Revision: D4085694 fbshipit-source-id: b4f091cf53d2e0d04d5cb1f6d13fbc38da2d3846
nathansobo
left a comment
There was a problem hiding this comment.
I'm not the world's best asynchronous code reviewer, but this all looks reasonable to me. On the pedantic naming front, I'd like to consistently use the term "URI" over URL since there's precedent for using the name "URI" elsewhere in the API, and also we should always capitalize the full initialism in camel-cased words. The only major question I have is about the ordering of package activation and URL handlers. Seems like the package should always activate first. Maybe that's what it does already and I'm misunderstanding, or maybe there's a good reason not to do that I'm not thinking of.
Also, where are we planning on documenting the new package.json features? I want to be sure documentation gets updated in sync with this feature.
Thanks for adding this! It's going to be useful in a variety of places.
|
|
||
| import {it} from './async-spec-helpers' | ||
|
|
||
| import UrlHandlerRegistry from '../src/url-handler-registry' |
There was a problem hiding this comment.
Some deep code review insight... To blend in with other names containing initialisms in the Atom codebase, URL should be capitalized.
| expect(history[4].url).toBe(urls[1]) | ||
| }) | ||
|
|
||
| it('refuses to handle bad URLs', () => { |
|
|
||
| dispatchUrlMessage: (uri) -> | ||
| if @packages.hasLoadedInitialPackages() | ||
| @urlHandlerRegistry.handleUrl(uri) |
| @commands.dispatch(@contextMenu.activeElement, command, args) | ||
|
|
||
| dispatchUrlMessage: (uri) -> | ||
| if @packages.hasLoadedInitialPackages() |
There was a problem hiding this comment.
Not saying you should do it as part of this PR, but it would be cool to have a getInitialPackagesLoadPromise method.
| } | ||
| } | ||
| }, | ||
| uriHandlerRegistration: { |
There was a problem hiding this comment.
More naming feedback... I'm seeing a mix of "URL" and "URI". I prefer URI myself because I think it's a more general concept.
| @lastFocusedWindow.sendUrlMessage url | ||
|
|
||
| findPackageWithName: (packageName, devMode) -> | ||
| _.find @getPackageManager(devMode).getAvailablePackageMetadata(), ({name}) -> name is packageName |
There was a problem hiding this comment.
Don't JS arrays have a built-in find method now?
| else | ||
| @openWithAtomUrl(urlToOpen, devMode, safeMode, env) | ||
|
|
||
| openWithAtomUrl: (url, devMode, safeMode, env) -> |
There was a problem hiding this comment.
I'm a bit confused about what makes this an "Atom" URL. I wonder if the method could indicate the distinction between the package URL main more clearly?
| @activate().then => | ||
| @mainModule[methodName]?.apply(@mainModule, args) | ||
| unless @mainActivated | ||
| @activateNow() |
There was a problem hiding this comment.
Would it be better to activate the package before handling the URL in case the handler depends on any state that gets established during activation?
|
@nathansobo I switched up the naming across the three relevant PRs (two of them in settings-view). Documentation will be added to the flight manual as part of this effort (see the URI Handling project for more). |
|
Some damn fine work pushing this monster effort over the finish line, @BinaryMuse! 💯 |
|
This is great and I'm excited to use this. My biggest question right now is whether this means an |
|
@akonwi |
|
thanks! |
See update below: #11399 (comment)
This PR implements the
MessageRegistry, which is similar to theCommandRegistryin that it maps string types of some kind to callback functions, but it differs in that messages originate from outside the application viaatom://URIs handled via theopen-urlevent.This partially implements #5262, provides a mechanism for hitting things like atom/settings-view#614 from outside the application (e.g. "View in Atom" links on package pages on the Atom site), and would enable package authors to implement things like #2037 for external debuggers, etc.
There is still some additional work to do around registering Atom as a system-level handler for
atom://URIs on non-Mac systems, but that probably will be installer-based work./cc @atom/core @atom/feedback for review. Some open questions I have are:
atom://atomas we already have a mechanism that utilizes URIs with hosts that match package names to perform some work. (What is this used for? I'm not familiar with it.) I'd love to get rid of this, as it means we can't use any host that might match a package name. If anyone has bright ideas for URI schemes, that'd be awesome. (I thought maybe something likeatom://.message/...?)namespace:nameportion) and the params, but maybe it makes more sense to pass the entire URI, or the parsed URI object.