aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/index.ts')
-rw-r--r--packages/main/src/index.ts57
1 files changed, 5 insertions, 52 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts
index 128ae35..2072017 100644
--- a/packages/main/src/index.ts
+++ b/packages/main/src/index.ts
@@ -22,7 +22,6 @@
22import { readFileSync } from 'node:fs'; 22import { readFileSync } from 'node:fs';
23import { readFile } from 'node:fs/promises'; 23import { readFile } from 'node:fs/promises';
24import { arch } from 'node:os'; 24import { arch } from 'node:os';
25import { URL } from 'node:url';
26 25
27import { 26import {
28 ServiceToMainIpcMessage, 27 ServiceToMainIpcMessage,
@@ -41,11 +40,12 @@ import { getSnapshot, onAction, onPatch } from 'mobx-state-tree';
41import osName from 'os-name'; 40import osName from 'os-name';
42 41
43import { 42import {
44 DEVMODE_ALLOWED_URL_PREFIXES,
45 enableStacktraceSourceMaps, 43 enableStacktraceSourceMaps,
46 installDevToolsExtensions, 44 installDevToolsExtensions,
47 openDevToolsWhenReady, 45 openDevToolsWhenReady,
48} from './infrastructure/electron/impl/devTools'; 46} from './infrastructure/electron/impl/devTools';
47import hardenSession from './infrastructure/electron/impl/hardenSession';
48import lockWebContentsToFile from './infrastructure/electron/impl/lockWebContentsToFile';
49import getDistResources from './infrastructure/resources/impl/getDistResources'; 49import getDistResources from './infrastructure/resources/impl/getDistResources';
50import initReactions from './initReactions'; 50import initReactions from './initReactions';
51import { createMainStore } from './stores/MainStore'; 51import { createMainStore } from './stores/MainStore';
@@ -128,32 +128,6 @@ initReactions(store)
128 log.log('Failed to initialize application', error); 128 log.log('Failed to initialize application', error);
129 }); 129 });
130 130
131const rendererBaseURL = resources.getRendererURL('/');
132function shouldCancelMainWindowRequest(url: string, method: string): boolean {
133 if (method !== 'GET') {
134 return true;
135 }
136 let normalizedURL: string;
137 try {
138 normalizedURL = new URL(url).toString();
139 } catch {
140 return true;
141 }
142 if (
143 isDevelopment &&
144 DEVMODE_ALLOWED_URL_PREFIXES.some((prefix) =>
145 normalizedURL.startsWith(prefix),
146 )
147 ) {
148 return false;
149 }
150 const isHttp = normalizedURL.startsWith(rendererBaseURL);
151 const isWs = normalizedURL.startsWith(
152 rendererBaseURL.replace(/^http:/, 'ws:'),
153 );
154 return !isHttp && !isWs;
155}
156
157async function createWindow(): Promise<unknown> { 131async function createWindow(): Promise<unknown> {
158 mainWindow = new BrowserWindow({ 132 mainWindow = new BrowserWindow({
159 show: false, 133 show: false,
@@ -169,29 +143,7 @@ async function createWindow(): Promise<unknown> {
169 143
170 webContents.userAgent = originalUserAgent; 144 webContents.userAgent = originalUserAgent;
171 145
172 webContents.session.setPermissionRequestHandler( 146 hardenSession(resources, isDevelopment, webContents.session);
173 (_webContents, _permission, callback) => {
174 callback(false);
175 },
176 );
177
178 webContents.session.webRequest.onBeforeRequest(
179 ({ url, method }, callback) => {
180 callback({
181 cancel: shouldCancelMainWindowRequest(url, method),
182 });
183 },
184 );
185
186 const pageURL = resources.getRendererURL('index.html');
187
188 webContents.on('will-navigate', (event, url) => {
189 if (url !== pageURL) {
190 event.preventDefault();
191 }
192 });
193
194 webContents.setWindowOpenHandler(() => ({ action: 'deny' }));
195 147
196 if (isDevelopment) { 148 if (isDevelopment) {
197 openDevToolsWhenReady(mainWindow); 149 openDevToolsWhenReady(mainWindow);
@@ -211,6 +163,7 @@ async function createWindow(): Promise<unknown> {
211 }); 163 });
212 164
213 browserView.webContents.userAgent = userAgent; 165 browserView.webContents.userAgent = userAgent;
166
214 autorun(() => { 167 autorun(() => {
215 browserView.setBounds(store.browserViewBounds); 168 browserView.setBounds(store.browserViewBounds);
216 }); 169 });
@@ -350,7 +303,7 @@ async function createWindow(): Promise<unknown> {
350 log.error('Failed to load browser', error); 303 log.error('Failed to load browser', error);
351 }); 304 });
352 305
353 return mainWindow.loadURL(pageURL); 306 return lockWebContentsToFile(resources, 'index.html', webContents);
354} 307}
355 308
356app.on('second-instance', () => { 309app.on('second-instance', () => {