import { WebContents } from 'electron' ; import { apiName } from '../common/api-interface' ; import { isMac } from '../common/env' ; import { logger } from '../common/logger' ; import { getCommandLineArgs } from '../common/utils' ; import { activate } from './window-actions' ; enum protocol { SymphonyProtocol = 'symphony://' } class ProtocolHandler { private static isValidProtocolUri = ( uri ) : boolean => ! ! ( uri && uri . startsWith ( protocol . SymphonyProtocol ) ) ; private preloadWebContents : WebContents | null = null ; private protocolUri : string | null = null ; constructor ( ) { this . processArgv ( ) ; } param webContents { WeContents } public setPreloadWebContents ( webContents ) { this . preloadWebContents = webContents ; logger . info ( `protocol handler: SFE is active and we have a valid protocol window with web contents!` ) ; if ( this . protocolUri ) { this . sendProtocol ( this . protocolUri ) ; logger . info ( `protocol handler: we have a cached url ${ this . protocolUri }, so, processed the request to SFE!` ) ; this . protocolUri = null ; } } param url { String } param isAppRunning { Boolean } public sendProtocol ( url , isAppRunning = true ) { if ( url && url . length > 2083 ) { logger . info ( `protocol-handler: protocol handler url length is greater than 2083, not performing any action!` ) ; return ; } logger . info ( `protocol handler: processing protocol request for the url ${ url }!` ) ; if ( ! this . preloadWebContents || ! isAppRunning ) { logger . info ( `protocol handler: app was started from the protocol request. Caching the URL ${ url }!` ) ; this . protocolUri = url ; return ; } if ( isMac ) { activate ( apiName . mainWindowName ) ; } if ( ProtocolHandler . isValidProtocolUri ( url ) ) { logger . info ( `protocol handler: our protocol request is a valid url ${ url }! sending request to SFE for further action!` ) ; this . preloadWebContents . send ( 'protocol-action' , url ) ; } } param argv { String [ ] } public processArgv ( argv ? , isAppAlreadyOpen = false ) { logger . info ( `protocol handler: processing protocol args!` ) ; const protocolUriFromArgv = getCommandLineArgs ( argv || process . argv , protocol . SymphonyProtocol , false ) ; if ( protocolUriFromArgv ) { logger . info ( `protocol handler: we have a protocol request for the url ${ protocolUriFromArgv }!` ) ; this . sendProtocol ( protocolUriFromArgv , isAppAlreadyOpen ) ; } } } const protocolHandler = new ProtocolHandler ( ) ; export { protocolHandler } ;