@@ -16,38 +16,55 @@ limitations under the License.
1616
1717const { app} = require ( 'electron' ) ;
1818
19+ const PROTOCOL = "riot://" ;
20+ const SEARCH_PARAM = "riot-desktop-user-data-path" ;
21+
1922const processUrl = ( url ) => {
2023 if ( ! global . mainWindow ) return ;
2124 console . log ( "Handling link: " , url ) ;
22- global . mainWindow . loadURL ( url . replace ( "riot://" , "vector://" ) ) ;
25+ global . mainWindow . loadURL ( url . replace ( PROTOCOL , "vector://" ) ) ;
2326} ;
2427
25- module . exports = ( ) => {
26- // get all args except `hidden` as it'd mean the app would not get focused
27- // XXX: passing args to protocol handlers only works on Windows,
28- // so unpackaged deep-linking and --profile passing won't work on Mac/Linux
29- const args = process . argv . slice ( 1 ) . filter ( arg => arg !== "--hidden" && arg !== "-hidden" ) ;
30- if ( app . isPackaged ) {
31- app . setAsDefaultProtocolClient ( 'riot' , process . execPath , args ) ;
32- } else if ( process . platform === 'win32' ) { // on Mac/Linux this would just cause the electron binary to open
33- // special handler for running without being packaged, e.g `electron .` by passing our app path to electron
34- app . setAsDefaultProtocolClient ( 'riot' , process . execPath , [ app . getAppPath ( ) , ...args ] ) ;
35- }
36-
37- if ( process . platform === 'darwin' ) {
38- // Protocol handler for macos
39- app . on ( 'open-url' , function ( ev , url ) {
40- ev . preventDefault ( ) ;
41- processUrl ( url ) ;
42- } ) ;
43- } else {
44- // Protocol handler for win32/Linux
45- app . on ( 'second-instance' , ( ev , commandLine ) => {
46- const url = commandLine [ commandLine . length - 1 ] ;
47- if ( ! url . startsWith ( "riot://" ) ) return ;
48- processUrl ( url ) ;
49- } ) ;
50- }
28+ module . exports = {
29+ getProfileFromDeeplink : ( args ) => {
30+ // check if we are passed a profile in the SSO callback url
31+ const deeplinkUrl = args . find ( arg => arg . startsWith ( 'riot://' ) ) ;
32+ if ( deeplinkUrl && deeplinkUrl . includes ( SEARCH_PARAM ) ) {
33+ const parsedUrl = new URL ( deeplinkUrl ) ;
34+ if ( parsedUrl . protocol === 'riot:' ) {
35+ const profile = parsedUrl . searchParams . get ( SEARCH_PARAM ) ;
36+ console . log ( "Forwarding to profile: " , profile ) ;
37+ return profile ;
38+ }
39+ }
40+ } ,
41+ protocolInit : ( ) => {
42+ // get all args except `hidden` as it'd mean the app would not get focused
43+ // XXX: passing args to protocol handlers only works on Windows, so unpackaged deep-linking
44+ // --profile/--profile-dir are passed via the SEARCH_PARAM var in the callback url
45+ const args = process . argv . slice ( 1 ) . filter ( arg => arg !== "--hidden" && arg !== "-hidden" ) ;
46+ if ( app . isPackaged ) {
47+ app . setAsDefaultProtocolClient ( 'riot' , process . execPath , args ) ;
48+ } else if ( process . platform === 'win32' ) { // on Mac/Linux this would just cause the electron binary to open
49+ // special handler for running without being packaged, e.g `electron .` by passing our app path to electron
50+ app . setAsDefaultProtocolClient ( 'riot' , process . execPath , [ app . getAppPath ( ) , ...args ] ) ;
51+ }
52+
53+ if ( process . platform === 'darwin' ) {
54+ // Protocol handler for macos
55+ app . on ( 'open-url' , function ( ev , url ) {
56+ ev . preventDefault ( ) ;
57+ processUrl ( url ) ;
58+ } ) ;
59+ } else {
60+ // Protocol handler for win32/Linux
61+ app . on ( 'second-instance' , ( ev , commandLine ) => {
62+ const url = commandLine [ commandLine . length - 1 ] ;
63+ if ( ! url . startsWith ( PROTOCOL ) ) return ;
64+ processUrl ( url ) ;
65+ } ) ;
66+ }
67+ } ,
5168} ;
5269
5370
0 commit comments