Skip to content

Enable Electron to set/unset itself as default protocol handler (OS X, Windows) - #4896

Merged
zcbenz merged 2 commits into
electron:masterfrom
felixrieseberg:default-protocol-handler
Mar 31, 2016
Merged

zcbenz merged 2 commits into
electron:masterfrom
felixrieseberg:default-protocol-handler

Conversation

@felixrieseberg

Copy link
Copy Markdown
Member

This PR adds an API enabling Electron to set itself as the default protocol handler for a custom protocol on both OS X and Windows. The API uses the Windows Registry and LSSetDefaultHandlerForURLScheme internally.

app.setAsDefaultProtocolClient(protocol)

  • protocol String - The name of your protocol, without ://. If you want your app to handle electron:// links, call this method with electron as the parameter.

This method sets the current executable as the default handler for a protocol (aka URI scheme). It allows you to integrate your app deeper into the operating system. Once registered, all links with your-protocol:// will be openend with the current executable. The whole link, including protocol, will be passed to your application as a parameter.

On OS X, one can only register protocols that have been added to the app's info.plist, which can not be modified at runtime (and is therefore not part of the API). You can however change the file with a simple text editor or script during build time.

app.removeAsDefaultProtocolClient(protocol) Windows

  • protocol String - The name of your protocol, without ://.

This method checks if the current executable as the default handler for a protocol (aka URI scheme). If so, it will remove the app as the default handler. On OS X, removing the app will automatically remove the app as the default protocol handler.

Closes #4857 || cc @paulcbetts @zcbenz @kevinsawicki

@felixrieseberg felixrieseberg changed the title Add API: SetASDefaultProtocolHandler (OS X, Windows) Add API: SetAsDefaultProtocolHandler (OS X, Windows) Mar 24, 2016
@felixrieseberg
felixrieseberg force-pushed the default-protocol-handler branch from f93dfb0 to da88b7d Compare March 24, 2016 01:13
Comment thread docs/api/app.md

Clears the recent documents list.

### `app.setAsDefaultProtocolClient(protocol)` _OS X_ _Windows_

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.

How will people unregister from this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

On both systems, a different app can always overwrite. On OS X, simply removing would involve a bit of hackery (we could call the set with an invalid bundle is); on Windows, we could also delete or overwrite the key. Basically the same it works with magnet links, phone links, or other non-restricted uri schemes.

Both systems don't really have a way to simply "unset" custom uri schemes (in a way that would restore previous handlers, for instance), so I'm inclined to not offer an API that makes Electron users think that we can really offer that reliably.

I trust your opinion though, I could easily implement an "overwrite with nonsense" API.

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 mostly thinking about the uninstall case - on Mac people will delete the app and launchd will react appropriately, but on Windows you leave a trash regkey around. I'm not so worried about restoring whatever was there

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

On the other side, if you check your registry right now, you'll see that we (Microsoft) leave all uri schemes that we ever came across there. We really like to collect them ;-)

Buuuut for a @paulcbetts 👍, I'd totally throw in a removeAsDefaultProtocolHandler, that checks if the current executable is set as handler for a given protocol and removes it. Thoughts?

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.

@felixrieseberg Perfect, works for me

This PR adds an API enabling Electron to set itself as the default
protocol handler for a custom porotocl on both oS X and Windows.

For details, please see `docs/app.md`.

Closes electron#4857
@felixrieseberg
felixrieseberg force-pushed the default-protocol-handler branch from da88b7d to d2567b0 Compare March 24, 2016 05:12
@deepak1556

Copy link
Copy Markdown
Member

wouldn't platform_util be a better place for the implementation details to go in ?

Comment thread atom/browser/browser_win.cc Outdated
if (FAILED(commandKey.WriteValue(L"", exe.c_str())))
return false;

VLOG(1) << "Chrome registered as default handler for " << protocol << ".";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

log can be removed, or should be modified with applications' name.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, thanks!

@felixrieseberg
felixrieseberg force-pushed the default-protocol-handler branch from ad48c66 to 40356dd Compare March 24, 2016 17:57
@felixrieseberg

Copy link
Copy Markdown
Member Author

Added a remove API to round it off.

Oh, and @deepak1556: I don't think so, given that other APIs that are similar also live in browser.cc - see Dock interactions or executable operations. I think this API should really be neighbors with AddRecentDocuments, SetAppUserModelID, etc.

@felixrieseberg felixrieseberg changed the title Add API: SetAsDefaultProtocolHandler (OS X, Windows) Enable Electron to set/unset itself as default protocol handler (OS X, Windows) Mar 24, 2016
@deepak1556

Copy link
Copy Markdown
Member

given that other APIs that are similar also live in browser.cc - see Dock interactions or executable operations

Yup but the browser class says // This class is used for control application-wide operations, so wanted to clarify what should go under platform_util and what under browser.

Comment thread atom/browser/api/atom_api_app.cc Outdated
.SetMethod("setUserTasks",
base::Bind(&Browser::SetUserTasks, browser))
.SetMethod("isAeroGlassEnabled", &App::IsAeroGlassEnabled)
.SetMethod("removeAsDefaultProtocolClient",

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.

Should we provide this API on other platforms even if it does nothing? It is a bit weird that all platforms have set but only one platform has remove.

@zcbenz

zcbenz commented Mar 27, 2016

Copy link
Copy Markdown
Contributor

wouldn't platform_util be a better place for the implementation details to go in ?

The APIs are part of the app module, so developers would most likely go to browser.cc when they want to look for the code.

@felixrieseberg
felixrieseberg force-pushed the default-protocol-handler branch from 40356dd to 05e4f96 Compare March 28, 2016 02:46
@felixrieseberg

Copy link
Copy Markdown
Member Author

@zcbenz: Good point. I added the API to both OS X and Linux, where it just returns false (as there's nothing we can do), but it does make it possible for people to call the API on any platform.

I incorporated all feedback, this should now be good 👍

@felixrieseberg
felixrieseberg force-pushed the default-protocol-handler branch from 05e4f96 to 1231360 Compare March 28, 2016 22:31
@zcbenz

zcbenz commented Mar 31, 2016

Copy link
Copy Markdown
Contributor

👍

@baconbrad

baconbrad commented Jul 11, 2016 •

Copy link
Copy Markdown
Contributor

Is there really nothing that can be done for Linux users?

Because there isn't an unified way to do it across distributions.

@atypicalprogrammer

Copy link
Copy Markdown

Some (most popular) distributions is better than nothing.

@baconbrad

Copy link
Copy Markdown
Contributor

Some (most popular) distributions is better than nothing.

Unfortunately targeting distributions will be a shot in the dark. As some of the links you pointed out the configuration is done via the window manager or desktop environment. So it's not performed by the OS itself but by Gnome, KDE, XFCE, xdg-open, or some other shell or program. It isn't always promised a distribution will have either of the necessary programs to do this. Even if they do have a shell such a Gnome installed it could still be possible they don't have gnome-open available. Even if you know the distro is ships with them it is possible the user can remove them.

@baconbrad

Copy link
Copy Markdown
Contributor

I suppose checking for gnome-open, exo-open, xdg-open, gvfs-open, or kde-open and using which ever is available might be better than nothing though. You wouldn't target a distro but you could target possible common environments.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants