File size: 6,007 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const { app, BrowserWindow, BrowserView, ipcMain: ipc } = require( 'electron' );
const { getPath } = require( '../lib/assets' );
const Config = require( '../lib/config' );
const log = require( '../lib/logger' )( 'desktop:runapp' );
const { isNonDesktopLoginUrl } = require( '../lib/login' );
const platform = require( '../lib/platform' );
const SessionManager = require( '../lib/session' );
const Settings = require( '../lib/settings' );
const settingConstants = require( '../lib/settings/constants' );
const System = require( '../lib/system' );

/**
 * Module variables
 */
const TITLE_BAR_HEIGHT = 38;

let mainWindow = null;

function getInitialUrl() {
	let appUrl = Config.loginURL();
	if ( process.env.CI || process.env.WP_DESKTOP_DEBUG ) {
		return appUrl;
	}

	const lastLocation = Settings.getSetting( settingConstants.LAST_LOCATION );
	if ( ! lastLocation || ! lastLocation.startsWith( 'http' ) ) {
		return appUrl;
	}

	log.info( `Will use last location as initial URL: ${ lastLocation }` );
	appUrl = lastLocation;

	if ( Config.oauthLoginEnabled && isNonDesktopLoginUrl( appUrl ) ) {
		appUrl = Config.loginURL();
		log.info(
			`Last location pointed to the regular login URL, will use the desktop login URL instead: ${ appUrl }`
		);
	}
	return appUrl;
}

function showAppWindow() {
	const preloadFile = getPath( 'preload.js' );
	const appUrl = getInitialUrl();
	log.info( 'Loading app (' + appUrl + ') in mainWindow' );

	const windowConfig = Settings.getSettingGroup( Config.mainWindow, null );
	windowConfig.webPreferences.spellcheck = Settings.getSetting( 'spellcheck-enabled' );
	windowConfig.webPreferences.preload = preloadFile;
	windowConfig.webPreferences.nativeWindowOpen = true;

	const bounds = {
		...{ width: 1200, height: 800 },
		...Settings.getSettingGroup( {}, 'window', [ 'x', 'y', 'width', 'height' ] ),
	};

	// Allow insecure content only in debug mode
	if ( process.env.WP_DESKTOP_DEBUG ) {
		windowConfig.webPreferences.allowRunningInsecureContent = true;
	}

	if ( process.platform === 'linux' ) {
		windowConfig.icon = getPath( 'linux-icon.png' );
	}

	mainWindow = new BrowserWindow( {
		...windowConfig,
		...bounds,
		...{
			frame: process.platform !== 'darwin',
			titleBarStyle: 'hiddenInset',
		},
	} );

	const mainView = new BrowserView( windowConfig );

	mainWindow.webContents.loadURL( `file://${ getPath( 'index.html' ) }` );

	mainWindow.setBrowserView( mainView );
	mainView.setBounds( {
		x: 0,
		y: TITLE_BAR_HEIGHT,
		width: bounds.width,
		height: bounds.height - TITLE_BAR_HEIGHT,
	} );

	// Windows and Linux don't resize properly and require extra space added to fit properly after resize.
	mainWindow.on( 'resize', function () {
		setTimeout( () => {
			const newBounds = mainWindow.getBounds();
			const boundsPadding = { width: 0, height: TITLE_BAR_HEIGHT };
			if ( process.platform === 'win32' ) {
				boundsPadding.width = 15;
				boundsPadding.height = TITLE_BAR_HEIGHT + 55;
			}
			if ( process.platform === 'linux' ) {
				boundsPadding.width = 1;
				boundsPadding.height = TITLE_BAR_HEIGHT + 25;
			}

			mainView.setBounds( {
				x: 0,
				y: TITLE_BAR_HEIGHT,
				width: newBounds.width - boundsPadding.width,
				height: newBounds.height - boundsPadding.height,
			} );
		}, 10 );
	} );

	SessionManager.init( mainWindow );

	mainView.webContents.on( 'did-finish-load', function () {
		mainView.webContents.send( 'app-config', System.getDetails() );

		ipc.on( 'mce-contextmenu', function ( ev ) {
			mainView.webContents.send( 'mce-contextmenu', ev );
		} );
	} );

	mainView.webContents.session.webRequest.onBeforeRequest( function ( details, callback ) {
		if (
			! process.env.WP_DESKTOP_DEBUG &&
			details.resourceType === 'script' &&
			details.url.startsWith( 'http://' )
		) {
			log.info(
				'Redirecting http request ' + details.url + ' to ' + details.url.replace( 'http', 'https' )
			);
			callback( { redirectURL: details.url.replace( 'http', 'https' ) } );
		} else {
			callback( {} );
		}
	} );

	mainView.webContents.session.webRequest.onHeadersReceived( function ( details, callback ) {
		// always allow previews to be loaded in iframes
		if ( details.resourceType === 'subFrame' ) {
			const headers = Object.assign( {}, details.responseHeaders );
			Object.keys( headers ).forEach( function ( name ) {
				if ( name.toLowerCase() === 'x-frame-options' ) {
					delete headers[ name ];
				}
			} );
			callback( {
				cancel: false,
				responseHeaders: headers,
			} );
			return;
		}
		callback( { cancel: false } );
	} );

	ipc.handle( 'get-config', () => {
		return Config.toRenderer();
	} );

	ipc.handle( 'get-settings', () => {
		return Settings.toRenderer();
	} );

	log.info( `Loading URL: '${ appUrl }'` );
	void mainView.webContents.loadURL( appUrl );

	mainWindow.on( 'close', function () {
		const currentURL = mainView.webContents.getURL();
		log.info( `Closing main window, last location: '${ currentURL }'` );
		Settings.saveSetting( settingConstants.LAST_LOCATION, currentURL );
	} );

	mainWindow.on( 'closed', function () {
		log.info( 'Window closed' );
		mainWindow = null;
	} );

	const appWindow = { view: mainView, window: mainWindow };
	require( '../window-handlers/login' )( appWindow );
	require( '../window-handlers/incoming-urls' )( appWindow );
	require( '../window-handlers/failed-to-load' )( appWindow );
	require( '../window-handlers/login-status' )( appWindow );
	require( '../window-handlers/notifications' )( appWindow );
	require( '../window-handlers/external-links' )( appWindow );
	require( '../window-handlers/window-saver' )( appWindow );
	require( '../window-handlers/debug-tools' )( appWindow );
	require( '../window-handlers/spellcheck' )( appWindow );
	require( '../window-handlers/navigation' )( appWindow );
	require( '../window-handlers/clipboard' )( appWindow );
	require( '../window-handlers/unsaved-changes' )( appWindow );

	platform.setMainWindow( appWindow );

	return mainWindow;
}

module.exports = function () {
	app.on( 'ready', showAppWindow );
};