This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 973
/
index.js
401 lines (347 loc) · 13.5 KB
/
index.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// @flow
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'
let ready = false
require('v8-compile-cache')
const CrashHerald = require('./crash-herald')
const telemetry = require('./telemetry')
const {setUserPref} = require('../js/state/userPrefs')
// set initial base line checkpoint
telemetry.setCheckpoint('init')
const handleUncaughtError = (stack, message) => {
muon.crashReporter.setJavascriptInfoCrashValue(JSON.stringify({stack, message}))
muon.crashReporter.dumpWithoutCrashing()
if (!ready) {
console.error('Waiting 60 seconds for process to load')
setTimeout(() => {
if (!ready) {
console.error('Process failed to load within 60 seconds')
process.exit(1)
}
}, 60 * 1000)
}
}
process.on('uncaughtException', function (error) {
var message, ref, stack
if (typeof error === 'string') {
stack = error
} else {
stack = (ref = error.stack) != null ? ref : error.name + ': ' + error.message
}
message = 'Uncaught Exception:\n' + stack
console.error('An uncaught exception occurred in the main process ' + message)
handleUncaughtError(stack, message)
})
process.on('unhandledRejection', function (reason, promise) {
console.error('Unhandled promise rejection in the main process ' + reason)
handleUncaughtError(promise, reason)
})
process.on('warning', warning => console.warn(warning.stack))
let initState
if (process.platform === 'win32') {
initState = require('./windowsInit')()
} else if (process.platform === 'linux') {
require('./linuxInit')
} else if (process.platform === 'darwin') {
initState = require('./darwinInit')()
}
const electron = require('electron')
const app = electron.app
const ipcMain = electron.ipcMain
const Immutable = require('immutable')
const updater = require('./updater')
const Importer = require('./importer')
const messages = require('../js/constants/messages')
const appActions = require('../js/actions/appActions')
const tabActions = require('./common/actions/tabActions')
const SessionStore = require('./sessionStore')
const {startSessionSaveInterval} = require('./sessionStoreShutdown')
const appStore = require('../js/stores/appStore')
const Autofill = require('./autofill')
const Extensions = require('./extensions')
const TrackingProtection = require('./trackingProtection')
const AdBlock = require('./adBlock')
const AdInsertion = require('./browser/ads/adInsertion')
const HttpsEverywhere = require('./httpsEverywhere')
const PDFJS = require('./pdfJS')
const SiteHacks = require('./siteHacks')
const CmdLine = require('./cmdLine')
const urlParse = require('./common/urlParse')
const locale = require('./locale')
const contentSettings = require('../js/state/contentSettings')
const privacy = require('../js/state/privacy')
const settings = require('../js/constants/settings')
const {getSetting} = require('../js/settings')
const BookmarksExporter = require('./browser/bookmarksExporter')
const {getIsObsolete} = require('./common/state/obsoletionStateHelper')
app.commandLine.appendSwitch('enable-features', 'BlockSmallPluginContent,PreferHtmlOverPlugins')
// Fix https://github.com/brave/browser-laptop/issues/15337
app.commandLine.appendSwitch('disable-databases')
// Domains to accept bad certs for. TODO: Save the accepted cert fingerprints.
let acceptCertDomains = {}
let errorCerts = {}
const prefsRestartCallbacks = {}
const prefsRestartLastValue = {}
const defaultProtocols = ['http', 'https']
// exit cleanly on signals
;['SIGTERM', 'SIGHUP', 'SIGINT', 'SIGBREAK'].forEach((signal) => {
process.on(signal, () => {
app.quit()
})
})
let loadAppStatePromise = SessionStore.loadAppState()
// Some settings must be set right away on startup, those settings should be handled here.
loadAppStatePromise.then((initialImmutableState) => {
const {HARDWARE_ACCELERATION_ENABLED, SMOOTH_SCROLL_ENABLED, SEND_CRASH_REPORTS, SITE_ISOLATION_ENABLED} = require('../js/constants/settings')
const initialSettings = initialImmutableState.get('settings')
if (getSetting(SITE_ISOLATION_ENABLED, initialSettings) === true) {
// This needs to happen as early as possible :O
app.commandLine.appendSwitch('site-per-process')
}
CrashHerald.init(getSetting(SEND_CRASH_REPORTS, initialSettings))
telemetry.setCheckpointAndReport('state-loaded')
// Check in case a user is upgrading with this setting.
// For non-legacy users, they will always have these settings in sync.
const oldHardwareAccelValue = getSetting(HARDWARE_ACCELERATION_ENABLED, initialSettings)
if (oldHardwareAccelValue !== app.getBooleanPref('hardware_acceleration_mode.enabled')) {
// This won't take effect until the next restart of the app, so some users
// may have to restart an extra time if UI doesn't work.
app.setBooleanPref('hardware_acceleration_mode.enabled', oldHardwareAccelValue)
}
if (getSetting(SMOOTH_SCROLL_ENABLED, initialSettings) === false) {
app.commandLine.appendSwitch('disable-smooth-scrolling')
}
})
const notifyCertError = (webContents, url, error, cert) => {
errorCerts[url] = {
subjectName: cert.subjectName,
issuerName: cert.issuerName,
serialNumber: cert.serialNumber,
validStart: cert.validStart,
validExpiry: cert.validExpiry,
fingerprint: cert.fingerprint
}
// Load the certificate error page
// and provide details about the error,
// including enough data for in-page actions to force the
// insecure page to load or show the certificate.
tabActions.setContentsError(webContents.getId(), {
url,
error,
cert,
tabId: webContents.getId()
})
appActions.loadURLRequested(webContents.getId(), 'about:certerror')
}
app.on('ready', () => {
setUserPref('safebrowsing.enabled', false)
app.on('certificate-error', (e, webContents, url, error, cert, resourceType, strictEnforcement, expiredPreviousDecision, muonCb) => {
let host = urlParse(url).host
if (host && acceptCertDomains[host] === true) {
// Ignore the cert error
muonCb('continue')
return
} else {
muonCb('deny')
}
if (resourceType !== 'mainFrame') {
return
}
notifyCertError(webContents, url, error, cert)
})
app.on('network-connected', () => {
appActions.networkConnected()
})
app.on('network-disconnected', () => {
appActions.networkDisconnected()
})
loadAppStatePromise.then((initialImmutableState) => {
// merge state which was set during optional platform-specific init
initialImmutableState = initialImmutableState.setIn(['about', 'init'],
Immutable.fromJS(initState || {}))
// Do this after loading the state
// For tests we always want to load default app state
const isObsolete = getIsObsolete(initialImmutableState)
const loadedPerWindowImmutableState = isObsolete
? Immutable.List()
: initialImmutableState.get('perWindowState')
initialImmutableState = initialImmutableState.delete('perWindowState')
// Restore map order after load
appActions.setState(initialImmutableState)
setImmediate(() => perWindowStateLoaded(loadedPerWindowImmutableState))
})
const perWindowStateLoaded = (loadedPerWindowImmutableState) => {
// TODO(bridiver) - this shold be refactored into reducers
// DO NOT ADD ANYHING TO THIS LIST
// See tabsReducer.js for app state init example
contentSettings.init()
privacy.init()
Autofill.init()
Extensions.init()
SiteHacks.init()
HttpsEverywhere.init()
TrackingProtection.init()
AdBlock.init()
AdInsertion.init()
PDFJS.init()
if (!loadedPerWindowImmutableState || loadedPerWindowImmutableState.size === 0) {
if (!CmdLine.newWindowURL()) {
appActions.newWindow()
}
} else {
const lastIndex = loadedPerWindowImmutableState.size - 1
loadedPerWindowImmutableState
.sort((a, b) => {
let comparison = 0
const aTime = a.getIn(['windowInfo', 'focusTime'], 0)
const bTime = b.getIn(['windowInfo', 'focusTime'], 0)
if (aTime > bTime) {
comparison = 1
} else if (aTime < bTime) {
comparison = -1
}
return comparison
})
.forEach((wndState, i) => {
const isLastWindow = i === lastIndex
if (CmdLine.shouldDebugWindowEvents && isLastWindow) {
console.log(`The restored window which should get focus has ${wndState.get('frames').size} frames`)
}
appActions.newWindow(undefined, isLastWindow ? undefined : { inactive: true }, wndState, true)
})
}
process.emit(messages.APP_INITIALIZED)
if (process.env.BRAVE_IS_DEFAULT_BROWSER !== undefined) {
if (process.env.BRAVE_IS_DEFAULT_BROWSER === 'true') {
appActions.changeSetting(settings.IS_DEFAULT_BROWSER, true)
} else if (process.env.BRAVE_IS_DEFAULT_BROWSER === 'false') {
appActions.changeSetting(settings.IS_DEFAULT_BROWSER, false)
}
} else {
// Default browser checking
let isDefaultBrowser
if (['development', 'test'].includes(process.env.NODE_ENV)) {
isDefaultBrowser = true
} else if (process.platform === 'linux') {
const Channel = require('./channel')
const desktopName = Channel.getLinuxDesktopName()
isDefaultBrowser = app.isDefaultProtocolClient('', desktopName)
} else {
isDefaultBrowser = defaultProtocols.every(p => app.isDefaultProtocolClient(p))
}
appActions.changeSetting(settings.IS_DEFAULT_BROWSER, isDefaultBrowser)
}
if (CmdLine.newWindowURL()) {
appActions.newWindow(Immutable.fromJS({
location: CmdLine.newWindowURL(),
isObsoleteAction: true
}))
}
// DO NOT TO THIS LIST
// using ipcMain.on is deprecated and should be replaced by actions/reducers
ipcMain.on(messages.PREFS_RESTART, (e, config, value) => {
var message = locale.translation('prefsRestart')
if (prefsRestartLastValue[config] !== undefined && prefsRestartLastValue[config] !== value) {
delete prefsRestartLastValue[config]
appActions.hideNotification(message)
} else {
appActions.showNotification({
buttons: [
{text: locale.translation('no')},
{text: locale.translation('yes')}
],
options: {
persist: false
},
position: 'global',
message
})
prefsRestartCallbacks[message] = (buttonIndex, persist) => {
delete prefsRestartCallbacks[message]
if (buttonIndex === 1) {
const args = process.argv.slice(1)
args.push('--relaunch')
app.relaunch({args})
app.quit()
} else {
delete prefsRestartLastValue[config]
appActions.hideNotification(message)
}
}
if (prefsRestartLastValue[config] === undefined && typeof value === 'boolean') {
prefsRestartLastValue[config] = value
}
}
})
ipcMain.on(messages.SET_CLIPBOARD, (e, text) => {
electron.clipboard.writeText(text)
})
ipcMain.on(messages.CERT_ERROR_ACCEPTED, (event, url) => {
try {
let host = urlParse(url).host
if (host) {
acceptCertDomains[host] = true
}
} catch (e) {
console.error('Cannot add url `' + url + '` to accepted domain list', e)
}
})
ipcMain.on(messages.CHECK_CERT_ERROR_ACCEPTED, (event, host, tabId) => {
// If the host is associated with a URL with a cert error, update the
// security state to insecure
event.sender.send(messages.SET_SECURITY_STATE, tabId, {
secure: 2
})
})
ipcMain.on(messages.GET_CERT_ERROR_DETAIL, (event, url) => {
if (errorCerts[url]) {
event.sender.send(messages.SET_CERT_ERROR_DETAIL, {
subjectName: errorCerts[url].subjectName,
issuerName: errorCerts[url].issuerName,
serialNumber: errorCerts[url].serialNumber,
validStart: errorCerts[url].validStart,
validExpiry: errorCerts[url].validExpiry,
fingerprint: errorCerts[url].fingerprint
})
}
})
startSessionSaveInterval()
ipcMain.on(messages.NOTIFICATION_RESPONSE, (e, message, buttonIndex, persist) => {
if (prefsRestartCallbacks[message]) {
prefsRestartCallbacks[message](buttonIndex, persist)
}
})
ipcMain.on(messages.IMPORT_BROWSER_DATA_NOW, () => {
Importer.init()
})
ipcMain.on(messages.EXPORT_BOOKMARKS, () => {
BookmarksExporter.showDialog(appStore.getState())
})
// DO NOT TO THIS LIST - see above
loadAppStatePromise.then((initialImmutableState) => {
updater.init(
process.platform,
process.arch,
process.env.BRAVE_UPDATE_VERSION || app.getVersion(),
process.env.BRAVE_ENABLE_PREVIEW_UPDATES !== undefined
)
// This is fired by a menu entry
process.on(messages.CHECK_FOR_UPDATE, () => updater.checkForUpdate(true))
ipcMain.on(messages.CHECK_FOR_UPDATE, () => updater.checkForUpdate(true))
// This is fired from a auto-update metadata call
process.on(messages.UPDATE_META_DATA_RETRIEVED, (metadata) => {
console.log(metadata)
})
})
// This is fired by a menu entry
process.on(messages.IMPORT_BROWSER_DATA_NOW, () => {
Importer.init()
})
process.on(messages.EXPORT_BOOKMARKS, () => {
BookmarksExporter.showDialog(appStore.getState())
})
ready = true
}
})