Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Package URL handlers - mapping atom:// URIs to package functions - #11399

Merged
BinaryMuse merged 43 commits into
masterfrom
mkt-url-based-command-dispatch
Oct 17, 2017
Merged

BinaryMuse merged 43 commits into
masterfrom
mkt-url-based-command-dispatch

Conversation

@BinaryMuse

@BinaryMuse BinaryMuse commented Apr 7, 2016 •

Copy link
Copy Markdown
Contributor

See update below: #11399 (comment)


This PR implements the MessageRegistry, which is similar to the CommandRegistry in that it maps string types of some kind to callback functions, but it differs in that messages originate from outside the application via atom:// URIs handled via the open-url event.

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:

  • I restricted messages to URIs starting with atom://atom as 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 like atom://.message/...?)
  • Right now, the callback function is called with the string message (just the namespace:name portion) and the params, but maybe it makes more sense to pass the entire URI, or the parsed URI object.

@lee-dohm

lee-dohm commented Apr 8, 2016

Copy link
Copy Markdown
Contributor

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

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

@zeke

zeke commented Apr 8, 2016

Copy link
Copy Markdown
Contributor

red-wavy-underline is a great package name.

@BinaryMuse

Copy link
Copy Markdown
Contributor Author

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.

@BinaryMuse

Copy link
Copy Markdown
Contributor Author

This is used for importing resources into Less without needing to know the path where packages are stored

@lee-dohm and I discussed this and it's a slightly different thing.

@thedaniel

Copy link
Copy Markdown
Contributor

It looks like electron/electron#4896 (comment) took care of the work for allowing apps to be protcol handlers on OS X and Windows.

Does that mean this PR is now waiting on an Electron upgrade to 0.37.4+?

@BinaryMuse

Copy link
Copy Markdown
Contributor Author

@iolsen This is just a reminder ping that we need to chat about this. :)

@BinaryMuse

Copy link
Copy Markdown
Contributor Author

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

Michelle Tilley added 3 commits May 25, 2016 22:36
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]>
@BinaryMuse
BinaryMuse force-pushed the mkt-url-based-command-dispatch branch from 0cff287 to 1b7ab00 Compare May 26, 2016 05:37
@bastilian

Copy link
Copy Markdown

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

@nathansobo

Copy link
Copy Markdown
Contributor

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 atom://packages/my-package, I'd prefer it. Just like for services, I think we should encourage users to namespace their external URL handlers based on their package name... so for example if I had a package named foo, I would register the namespace atom://foo/.... I also think the handler should take the entire URL. Perhaps absent the atom:// prefix?

As for registering these handlers, I think we should consider a setup where handlers are registered statically via the package.json. We've moved in this direction with other global facilities in Atom. The big advantage to doing something static is that you don't have to activate the package or evaluate any code to know a handler has been registered. This makes it easier to defer loading for packages that only define these kinds of handlers.

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.

@jgebhardt

Copy link
Copy Markdown
Contributor

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 MessageRegistry is not super intuitive imho.

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?

@DavidSnider

Copy link
Copy Markdown

If we could make this happen, I would be suuuper happy

@bolinfest

Copy link
Copy Markdown
Contributor

@BinaryMuse Any chance you'd be willing to pick this up again soon?

@bolinfest

Copy link
Copy Markdown
Contributor

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 MessageRegistry naming for now. We would only have to bikeshed the name of the property in the package.json :).

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

@bolinfest

Copy link
Copy Markdown
Contributor

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 package.json:

{
  "name": "an-example",
  "providedServices": {
    "atom.url-handler": {
      "versions": {
        "1.0.0": "onAtomURI"
      }
    }
  }
}

Then if Atom received a URI of the form atom://url-handler/an-example/..., then an-example would be activated and the URI would be passed as an argument to the registered provider, onAtomURI().

@hansonw

hansonw commented Oct 21, 2016

Copy link
Copy Markdown
Contributor

+1 to @mbolin's suggestion. Maybe we could even simplify things a little and have atom://package-name/path?params trigger package-name's URL handler service callback?

From searching on github, there's only three package.jsons that contain a urlMain - one of them looks experimental and another is a homedir backup, so there's only one package (https://atom.io/packages/mastermind) that actually relies on this behaviour. (We could preserve the old urlMain behaviour if no path is specified).

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

facebook-github-bot pushed a commit to facebookarchive/nuclide that referenced this pull request Nov 7, 2016
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 nathansobo 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.

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.

Comment thread spec/url-handler-registry-spec.js Outdated

import {it} from './async-spec-helpers'

import UrlHandlerRegistry from '../src/url-handler-registry'

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.

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', () => {

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.

👍🏻

Comment thread src/atom-environment.coffee Outdated

dispatchUrlMessage: (uri) ->
if @packages.hasLoadedInitialPackages()
@urlHandlerRegistry.handleUrl(uri)

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.

s/Url/URL

@commands.dispatch(@contextMenu.activeElement, command, args)

dispatchUrlMessage: (uri) ->
if @packages.hasLoadedInitialPackages()

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.

Not saying you should do it as part of this PR, but it would be cool to have a getInitialPackagesLoadPromise method.

Comment thread src/config-schema.js
}
}
},
uriHandlerRegistration: {

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.

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

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.

Don't JS arrays have a built-in find method now?

else
@openWithAtomUrl(urlToOpen, devMode, safeMode, env)

openWithAtomUrl: (url, devMode, safeMode, env) ->

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.

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?

Comment thread src/package.coffee Outdated
@activate().then =>
@mainModule[methodName]?.apply(@mainModule, args)
unless @mainActivated
@activateNow()

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.

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?

@BinaryMuse

Copy link
Copy Markdown
Contributor Author

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

@BinaryMuse
BinaryMuse dismissed nathansobo’s stale review October 17, 2017 22:39

Addressed requested changes

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

🚢 when ready!

@iolsen

iolsen commented Oct 17, 2017

Copy link
Copy Markdown
Contributor

Some damn fine work pushing this monster effort over the finish line, @BinaryMuse! 💯

@BinaryMuse
BinaryMuse merged commit d6a3a60 into master Oct 17, 2017
@BinaryMuse
BinaryMuse deleted the mkt-url-based-command-dispatch branch October 17, 2017 23:29
@akonwi

akonwi commented Dec 13, 2017

Copy link
Copy Markdown
Contributor

This is great and I'm excited to use this. My biggest question right now is whether this means an atom://cool-package link will go to the package details in atom settings to allow the user to install it if it isn't already installed. From what I gathered skimming this thread, this api is more for already installed packages responding to URIs

@BinaryMuse

Copy link
Copy Markdown
Contributor Author

@akonwi settings-view sets up a URI handler that can show the installation/details page for a package; see atom/settings-view#1011

@akonwi

akonwi commented Dec 14, 2017

Copy link
Copy Markdown
Contributor

thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.