1- // @flow
2-
31/*
42Copyright 2016 Aviral Dasgupta
53Copyright 2016 OpenMarket Ltd
@@ -20,14 +18,13 @@ limitations under the License.
2018// Squirrel on windows starts the app with various flags
2119// as hooks to tell us when we've been installed/uninstalled
2220// etc.
23- const check_squirrel_hooks = require ( './squirrelhooks' ) ;
24- if ( check_squirrel_hooks ( ) ) return ;
21+ const checkSquirrelHooks = require ( './squirrelhooks' ) ;
22+ if ( checkSquirrelHooks ( ) ) return ;
2523
2624const electron = require ( 'electron' ) ;
2725
2826const tray = require ( './tray' ) ;
29-
30- const VectorMenu = require ( './vectormenu' ) ;
27+ const vectorMenu = require ( './vectormenu' ) ;
3128const webContentsHandler = require ( './webcontents-handler' ) ;
3229
3330const windowStateKeeper = require ( 'electron-window-state' ) ;
@@ -48,6 +45,47 @@ const INITIAL_UPDATE_DELAY_MS = 30 * 1000;
4845let mainWindow = null ;
4946let appQuitting = false ;
5047
48+ function safeOpenURL ( target ) {
49+ // openExternal passes the target to open/start/xdg-open,
50+ // so put fairly stringent limits on what can be opened
51+ // (for instance, open /bin/sh does indeed open a terminal
52+ // with a shell, albeit with no arguments)
53+ const parsedUrl = url . parse ( target ) ;
54+ if ( PERMITTED_URL_SCHEMES . indexOf ( parsedUrl . protocol ) > - 1 ) {
55+ // explicitly use the URL re-assembled by the url library,
56+ // so we know the url parser has understood all the parts
57+ // of the input string
58+ const newTarget = url . format ( parsedUrl ) ;
59+ electron . shell . openExternal ( newTarget ) ;
60+ }
61+ }
62+
63+ function onWindowOrNavigate ( ev , target ) {
64+ // always prevent the default: if something goes wrong,
65+ // we don't want to end up opening it in the electron
66+ // app, as we could end up opening any sort of random
67+ // url in a window that has node scripting access.
68+ ev . preventDefault ( ) ;
69+ safeOpenURL ( target ) ;
70+ }
71+
72+ function onLinkContextMenu ( ev , params ) {
73+ const popupMenu = new electron . Menu ( ) ;
74+
75+ popupMenu . append ( new electron . MenuItem ( {
76+ label : params . linkURL ,
77+ click ( ) { safeOpenURL ( params . linkURL ) ; } ,
78+ } ) ) ;
79+
80+ popupMenu . append ( new electron . MenuItem ( {
81+ label : 'Copy Link Address' ,
82+ click ( ) { electron . clipboard . writeText ( params . linkURL ) ; } ,
83+ } ) ) ;
84+
85+ popupMenu . popup ( ) ;
86+ ev . preventDefault ( ) ;
87+ }
88+
5189function installUpdate ( ) {
5290 // for some reason, quitAndInstall does not fire the
5391 // before-quit event, so we need to set the flag here.
@@ -59,38 +97,37 @@ function pollForUpdates() {
5997 try {
6098 electron . autoUpdater . checkForUpdates ( ) ;
6199 } catch ( e ) {
62- console . log ( " Couldn't check for update" , e ) ;
100+ console . log ( ' Couldn\ 't check for update' , e ) ;
63101 }
64102}
65103
66- function startAutoUpdate ( update_base_url ) {
67- if ( update_base_url . slice ( - 1 ) !== '/' ) {
68- update_base_url = update_base_url + '/' ;
104+ function startAutoUpdate ( updateBaseUrl ) {
105+ if ( updateBaseUrl . slice ( - 1 ) !== '/' ) {
106+ updateBaseUrl = updateBaseUrl + '/' ;
69107 }
70108 try {
71109 // For reasons best known to Squirrel, the way it checks for updates
72110 // is completely different between macOS and windows. On macOS, it
73111 // hits a URL that either gives it a 200 with some json or
74112 // 204 No Content. On windows it takes a base path and looks for
75113 // files under that path.
76- if ( process . platform == 'darwin' ) {
114+ if ( process . platform === 'darwin' ) {
77115 // include the current version in the URL we hit. Electron doesn't add
78116 // it anywhere (apart from the User-Agent) so it's up to us. We could
79117 // (and previously did) just use the User-Agent, but this doesn't
80118 // rely on NSURLConnection setting the User-Agent to what we expect,
81119 // and also acts as a convenient cache-buster to ensure that when the
82120 // app updates it always gets a fresh value to avoid update-looping.
83121 electron . autoUpdater . setFeedURL (
84- update_base_url +
85- 'macos/?localVersion=' + encodeURIComponent ( electron . app . getVersion ( ) )
86- ) ;
87- } else if ( process . platform == 'win32' ) {
88- electron . autoUpdater . setFeedURL ( update_base_url + 'win32/' + process . arch + '/' ) ;
122+ `${ updateBaseUrl } macos/?localVersion=${ encodeURIComponent ( electron . app . getVersion ( ) ) } ` ) ;
123+
124+ } else if ( process . platform === 'win32' ) {
125+ electron . autoUpdater . setFeedURL ( `${ updateBaseUrl } win32/${ process . arch } /` ) ;
89126 } else {
90127 // Squirrel / electron only supports auto-update on these two platforms.
91128 // I'm not even going to try to guess which feed style they'd use if they
92129 // implemented it on Linux, or if it would be different again.
93- console . log ( " Auto update not supported on this platform" ) ;
130+ console . log ( ' Auto update not supported on this platform' ) ;
94131 }
95132 // We check for updates ourselves rather than using 'updater' because we need to
96133 // do it in the main process (and we don't really need to check every 10 minutes:
@@ -103,7 +140,7 @@ function startAutoUpdate(update_base_url) {
103140 setInterval ( pollForUpdates , UPDATE_POLL_INTERVAL_MS ) ;
104141 } catch ( err ) {
105142 // will fail if running in debug mode
106- console . log ( " Couldn't enable update checking" , err ) ;
143+ console . log ( ' Couldn\ 't enable update checking' , err ) ;
107144 }
108145}
109146
@@ -114,7 +151,7 @@ function startAutoUpdate(update_base_url) {
114151// Assuming we generally run from the console when developing,
115152// this is far preferable.
116153process . on ( 'uncaughtException' , function ( error ) {
117- console . log ( " Unhandled exception" , error ) ;
154+ console . log ( ' Unhandled exception' , error ) ;
118155} ) ;
119156
120157electron . ipcMain . on ( 'install_update' , installUpdate ) ;
@@ -168,30 +205,28 @@ const shouldQuit = electron.app.makeSingleInstance((commandLine, workingDirector
168205} ) ;
169206
170207if ( shouldQuit ) {
171- console . log ( " Other instance detected: exiting" ) ;
208+ console . log ( ' Other instance detected: exiting' ) ;
172209 electron . app . quit ( ) ;
173210}
174211
175212electron . app . on ( 'ready' , ( ) => {
176213 if ( vectorConfig . update_base_url ) {
177- console . log ( " Starting auto update with base URL: " + vectorConfig . update_base_url ) ;
214+ console . log ( ` Starting auto update with base URL: ${ vectorConfig . update_base_url } ` ) ;
178215 startAutoUpdate ( vectorConfig . update_base_url ) ;
179216 } else {
180- console . log ( " No update_base_url is defined: auto update is disabled" ) ;
217+ console . log ( ' No update_base_url is defined: auto update is disabled' ) ;
181218 }
182219
183- const icon_path = `${ __dirname } /../img/riot.` + (
184- process . platform == 'win32' ? 'ico' : 'png'
185- ) ;
220+ const iconPath = `${ __dirname } /../img/riot.${ process . platform === 'win32' ? 'ico' : 'png' } ` ;
186221
187222 // Load the previous window state with fallback to defaults
188- let mainWindowState = windowStateKeeper ( {
223+ const mainWindowState = windowStateKeeper ( {
189224 defaultWidth : 1024 ,
190225 defaultHeight : 768 ,
191226 } ) ;
192227
193228 mainWindow = new electron . BrowserWindow ( {
194- icon : icon_path ,
229+ icon : iconPath ,
195230 show : false ,
196231 autoHideMenuBar : true ,
197232
@@ -201,12 +236,12 @@ electron.app.on('ready', () => {
201236 height : mainWindowState . height ,
202237 } ) ;
203238 mainWindow . loadURL ( `file://${ __dirname } /../../webapp/index.html` ) ;
204- electron . Menu . setApplicationMenu ( VectorMenu ) ;
239+ electron . Menu . setApplicationMenu ( vectorMenu ) ;
205240
206241 // Create trayIcon icon
207242 tray . create ( mainWindow , {
208- icon_path : icon_path ,
209- brand : vectorConfig . brand || 'Riot'
243+ icon_path : iconPath ,
244+ brand : vectorConfig . brand || 'Riot' ,
210245 } ) ;
211246
212247 if ( ! process . argv . includes ( '--hidden' ) ) {
@@ -219,7 +254,7 @@ electron.app.on('ready', () => {
219254 mainWindow = null ;
220255 } ) ;
221256 mainWindow . on ( 'close' , ( e ) => {
222- if ( ! appQuitting && ( tray . hasTray ( ) || process . platform == 'darwin' ) ) {
257+ if ( ! appQuitting && ( tray . hasTray ( ) || process . platform === 'darwin' ) ) {
223258 // On Mac, closing the window just hides it
224259 // (this is generally how single-window Mac apps
225260 // behave, eg. Mail.app)
0 commit comments