import { contextBridge , ipcRenderer , webFrame } from 'electron' ; import * as React from 'react' ; import * as ReactDOM from 'react-dom' ; import { apiCmds , apiName } from '../common/api-interface' ; import { i18n } from '../common/i18n-preload' ; import './app-bridge' ; import DownloadManager from './components/download-manager' ; import MessageBanner from './components/message-banner' ; import NetworkError from './components/network-error' ; import SnackBar from './components/snack-bar' ; import { SSFApi } from './ssf-api' ; interface ISSFWindow extends Window { ssf ? : SSFApi ; } const ssfWindow = window ; const minMemoryFetchInterval = 4 * 60 * 60 * 1000 ; const maxMemoryFetchInterval = 12 * 60 * 60 * 1000 ; const snackBar = new SnackBar ( ) ; const banner = new MessageBanner ( ) ; /**
 * creates API exposed from electron.
 */ const createAPI = ( ) => { if ( window . self !== window . top ) { return ; } ssfWindow . ssf = new SSFApi ( ) ; Object . freeze ( ssfWindow . ssf ) ; } ; createAPI ( ) ; if ( ssfWindow . ssf ) { contextBridge . exposeInMainWorld ( 'manaSSF' , { setIsMana : ssfWindow . ssf . setIsMana , CryptoLib : ssfWindow . ssf . CryptoLib , Search : ssfWindow . ssf . Search , Notification : ssfWindow . ssf . Notification , getMediaSource : ssfWindow . ssf . getMediaSource , activate : ssfWindow . ssf . activate , bringToFront : ssfWindow . ssf . bringToFront , getVersionInfo : ssfWindow . ssf . getVersionInfo , registerActivityDetection : ssfWindow . ssf . registerActivityDetection , registerDownloadHandler : ssfWindow . ssf . registerDownloadHandler , openDownloadedItem : ssfWindow . ssf . openDownloadedItem , showDownloadedItem : ssfWindow . ssf . showDownloadedItem , clearDownloadedItems : ssfWindow . ssf . clearDownloadedItems , registerBoundsChange : ssfWindow . ssf . registerBoundsChange , registerLogger : ssfWindow . ssf . registerLogger , registerProtocolHandler : ssfWindow . ssf . registerProtocolHandler , registerLogRetriever : ssfWindow . ssf . registerLogRetriever , sendLogs : ssfWindow . ssf . sendLogs , registerAnalyticsEvent : ssfWindow . ssf . registerAnalyticsEvent , ScreenSnippet : ssfWindow . ssf . ScreenSnippet , openScreenSnippet : ssfWindow . ssf . openScreenSnippet , closeScreenSnippet : ssfWindow . ssf . closeScreenSnippet , setBadgeCount : ssfWindow . ssf . setBadgeCount , setLocale : ssfWindow . ssf . setLocale , setIsInMeeting : ssfWindow . ssf . setIsInMeeting , showNotificationSettings : ssfWindow . ssf . showNotificationSettings , showScreenSharingIndicator : ssfWindow . ssf . showScreenSharingIndicator , openScreenSharingIndicator : ssfWindow . ssf . openScreenSharingIndicator , closeScreenSharingIndicator : ssfWindow . ssf . closeScreenSharingIndicator , registerRestartFloater : ssfWindow . ssf . registerRestartFloater , setCloudConfig : ssfWindow . ssf . setCloudConfig , checkMediaPermission : ssfWindow . ssf . checkMediaPermission , showNotification : ssfWindow . ssf . showNotification , closeNotification : ssfWindow . ssf . closeNotification , restartApp : ssfWindow . ssf . restartApp , closeAllWrapperWindows : ssfWindow . ssf . closeAllWrapperWindows , setZoomLevel : ssfWindow . ssf . setZoomLevel , getZoomLevel : ssfWindow . ssf . getZoomLevel , supportedSettings : ssfWindow . ssf . supportedSettings , } ) ; } param min { number } param max { number } const getRandomTime = ( min , max ) => { min = Math . ceil ( min ) ; max = Math . floor ( max ) ; return Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ; } ; param time const monitorMemory = ( time ) => { setTimeout ( async ( ) => { const memoryInfo = await process . getProcessMemoryInfo ( ) ; ipcRenderer . send ( apiName . symphonyApi , { cmd : apiCmds . memoryInfo , memoryInfo , } ) ; monitorMemory ( getRandomTime ( minMemoryFetchInterval , maxMemoryFetchInterval ) ) ; } , time ) ; } ; ipcRenderer . on ( 'page-load' , ( _event , { locale , resources } ) => { i18n . setResource ( locale , resources ) ; webFrame . setSpellCheckProvider ( 'en-US' , { async spellCheck ( words , callback ) { const misspelled = await Promise . all ( words . map ( async ( word ) => { return ( await ipcRenderer . invoke ( apiName . symphonyApi , { cmd : apiCmds . isMisspelled , word , } ) ) ? word : '' ; } ) ) ; callback ( misspelled . filter ( ( word ) => word ) ) ; } , } ) ; snackBar . initSnackBar ( ) ; const downloadManager = new DownloadManager ( ) ; downloadManager . initDownloadManager ( ) ; banner . initBanner ( ) ; banner . showBanner ( false , 'error' ) ; } ) ; ipcRenderer . on ( 'page-load-failed' , ( _event , { locale , resources } ) => { i18n . setResource ( locale , resources ) ; } ) ; ipcRenderer . on ( 'network-error' , ( _event , { error } ) => { const networkErrorContainer = document . createElement ( 'div' ) ; networkErrorContainer . id = 'main-frame' ; networkErrorContainer . classList . add ( 'content-wrapper' ) ; document . body . append ( networkErrorContainer ) ; const networkError = React . createElement ( NetworkError , { error } ) ; ReactDOM . render ( networkError , networkErrorContainer ) ; } ) ; ipcRenderer . on ( 'show-banner' , ( _event , { show , bannerType , url } ) => { if ( ! ! document . getElementsByClassName ( 'sda-banner-show' ) . length ) { return ; } banner . showBanner ( show , bannerType , url ) ; } ) ; ipcRenderer . on ( 'initialize-memory-refresh' , ( ) => { monitorMemory ( getRandomTime ( minMemoryFetchInterval , maxMemoryFetchInterval ) ) ; } ) ; ipcRenderer . on ( 'exit-html-fullscreen' , async ( ) => { if ( document && document . fullscreenElement ) { await document . exitFullscreen ( ) ; } } ) ;