Skip to content

Commit ac42e99

Browse files
committed
fix: windows url auth handler.
1 parent d6fc96e commit ac42e99

3 files changed

Lines changed: 77 additions & 10 deletions

File tree

include/element/application.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ class Application : public juce::JUCEApplication,
103103
*/
104104
void handleURLSchemeCallback (const juce::String& urlString);
105105

106-
#if JUCE_MAC
107-
/** Registers the URL scheme handler for macOS Apple Events. */
106+
#if JUCE_MAC || JUCE_WINDOWS
107+
/** Registers the URL scheme handler. */
108108
void registerURLSchemeHandler();
109109

110110
/** Unregisters the URL scheme handler. */

src/application.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,17 @@ void Application::initialise (const String& commandLine)
225225
initializeModulePath();
226226
printCopyNotice();
227227

228-
#if JUCE_MAC
228+
#if JUCE_MAC || JUCE_WINDOWS
229229
registerURLSchemeHandler();
230230
#endif
231231

232232
launchApplication();
233233

234-
// Handle URL scheme if passed on command line
235-
if (commandLine.startsWith ("element://"))
236-
handleURLSchemeCallback (commandLine);
234+
// Handle URL scheme if passed on command line. On Windows the URL arrives as a
235+
// quoted argument (element.exe "element://..."), so strip quotes before matching.
236+
const auto url = commandLine.unquoted().trim();
237+
if (url.startsWith ("element://"))
238+
handleURLSchemeCallback (url);
237239
}
238240

239241
void Application::actionListenerCallback (const String& message)
@@ -265,7 +267,7 @@ void Application::shutdown()
265267
if (! world)
266268
return;
267269

268-
#if JUCE_MAC
270+
#if JUCE_MAC || JUCE_WINDOWS
269271
unregisterURLSchemeHandler();
270272
#endif
271273

@@ -371,10 +373,12 @@ void Application::anotherInstanceStarted (const String& commandLine)
371373
if (! world)
372374
return;
373375

374-
// Handle custom URL scheme callbacks (e.g., auth)
375-
if (commandLine.startsWith ("element://"))
376+
// Handle custom URL scheme callbacks (e.g., auth). On Windows the URL arrives as a
377+
// quoted argument forwarded from the launching instance, so strip quotes before matching.
378+
const auto url = commandLine.unquoted().trim();
379+
if (url.startsWith ("element://"))
376380
{
377-
handleURLSchemeCallback (commandLine);
381+
handleURLSchemeCallback (url);
378382
return;
379383
}
380384

src/urlhandler.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// SPDX-FileCopyrightText: Copyright (C) Kushview, LLC.
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
#include <juce_core/system/juce_TargetPlatform.h>
5+
6+
#if JUCE_WINDOWS
7+
8+
#include <element/application.hpp>
9+
#include "log.hpp"
10+
11+
#ifndef WIN32_LEAN_AND_MEAN
12+
#define WIN32_LEAN_AND_MEAN
13+
#endif
14+
#ifndef NOMINMAX
15+
#define NOMINMAX
16+
#endif
17+
#include <windows.h>
18+
19+
using namespace juce;
20+
21+
namespace element {
22+
23+
void Application::registerURLSchemeHandler()
24+
{
25+
const auto exePath = File::getSpecialLocation (File::currentExecutableFile).getFullPathName();
26+
const auto cmd = "\"" + exePath + "\" \"%1\"";
27+
28+
HKEY hkey;
29+
DWORD disposition;
30+
const wchar_t* keyPath = L"Software\\Classes\\element";
31+
32+
// Create the element key
33+
if (::RegCreateKeyExW (HKEY_CURRENT_USER, keyPath, 0, nullptr, 0, KEY_WRITE, nullptr, &hkey, &disposition) == ERROR_SUCCESS)
34+
{
35+
// Set default value to "URL:Element Protocol"
36+
::RegSetValueExW (hkey, nullptr, 0, REG_SZ, (const BYTE*)L"URL:Element Protocol", sizeof (wchar_t) * 20);
37+
38+
// Set "URL Protocol" value to empty string
39+
::RegSetValueExW (hkey, L"URL Protocol", 0, REG_SZ, (const BYTE*)L"", sizeof (wchar_t));
40+
41+
::RegCloseKey (hkey);
42+
}
43+
44+
// Create the shell\open\command subkey
45+
const std::wstring fullPath = std::wstring (keyPath) + L"\\shell\\open\\command";
46+
if (::RegCreateKeyExW (HKEY_CURRENT_USER, fullPath.c_str(), 0, nullptr, 0, KEY_WRITE, nullptr, &hkey, &disposition) == ERROR_SUCCESS)
47+
{
48+
const std::wstring cmdW = cmd.toWideCharPointer();
49+
::RegSetValueExW (hkey, nullptr, 0, REG_SZ, (const BYTE*)cmdW.c_str(), (DWORD)((cmdW.length() + 1) * sizeof (wchar_t)));
50+
::RegCloseKey (hkey);
51+
}
52+
53+
Logger::writeToLog ("URL scheme handler (Windows Registry) registered");
54+
}
55+
56+
void Application::unregisterURLSchemeHandler()
57+
{
58+
::RegDeleteTreeW (HKEY_CURRENT_USER, L"Software\\Classes\\element");
59+
}
60+
61+
} // namespace element
62+
63+
#endif

0 commit comments

Comments
 (0)