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 975
/
extensions.js
482 lines (446 loc) · 17.4 KB
/
extensions.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
const browserActions = require('./browser/extensions/browserActions')
const contextMenus = require('./browser/extensions/contextMenus')
const extensionActions = require('./common/actions/extensionActions')
const config = require('../js/constants/config')
const appConfig = require('../js/constants/appConfig')
const {fileUrl} = require('../js/lib/appUrlUtil')
const {getExtensionsPath, getBraveExtUrl, getBraveExtIndexHTML} = require('../js/lib/appUrlUtil')
const {getSetting} = require('../js/settings')
const settings = require('../js/constants/settings')
const extensionStates = require('../js/constants/extensionStates')
const {passwordManagers, extensionIds} = require('../js/constants/passwordManagers')
const appStore = require('../js/stores/appStore')
const extensionState = require('./common/state/extensionState')
const appActions = require('../js/actions/appActions')
const fs = require('fs')
const path = require('path')
// Takes Content Security Policy flags, for example { 'default-src': '*' }
// Returns a CSP string, for example 'default-src: *;'
let concatCSP = (cspDirectives) => {
let csp = ''
for (let directive in cspDirectives) {
csp += directive + ' ' + cspDirectives[directive] + '; '
}
return csp.trim()
}
// Returns the Chromium extension manifest for the braveExtension
// The braveExtension handles about: pages, ad blocking, and a few other things
let generateBraveManifest = () => {
const indexHTML = getBraveExtIndexHTML()
let baseManifest = {
name: 'brave',
manifest_version: 2,
version: '1.0',
background: {
scripts: [ 'content/scripts/idleHandler.js' ],
persistent: true
},
content_scripts: [
{
run_at: 'document_start',
all_frames: true,
matches: ['https://www.washingtonpost.com/*'],
css: [
'content/styles/removeEmptyElements.css'
]
},
{
run_at: 'document_start',
all_frames: true,
matches: ['<all_urls>'],
include_globs: [
'http://*/*', 'https://*/*', 'file://*', 'data:*', 'about:srcdoc'
],
exclude_globs: [
indexHTML
],
match_about_blank: true,
js: [
'content/scripts/util.js',
'content/scripts/navigator.js',
'content/scripts/blockFlash.js',
'content/scripts/blockCanvasFingerprinting.js',
'content/scripts/block3rdPartyContent.js',
'content/scripts/inputHandler.js'
],
css: [
'brave-default.css'
]
},
{
run_at: 'document_end',
all_frames: true,
matches: ['<all_urls>'],
include_globs: [
'http://*/*', 'https://*/*', 'file://*', 'data:*', 'about:srcdoc'
],
exclude_globs: [
indexHTML
],
js: [
'content/scripts/adInsertion.js',
'content/scripts/passwordManager.js',
'content/scripts/pageInformation.js',
'content/scripts/flashListener.js'
]
},
{
run_at: 'document_end',
all_frames: false,
matches: ['<all_urls>'],
include_globs: [
'http://*/*', 'https://*/*', 'file://*', 'data:*', 'about:srcdoc',
indexHTML,
getBraveExtUrl('about-*.html'),
getBraveExtUrl('about-*.html') + '#*'
],
exclude_globs: [
getBraveExtUrl('about-blank.html'),
getBraveExtUrl('about-blank.html') + '#*'
],
js: [
'content/scripts/spellCheck.js',
'content/scripts/themeColor.js'
]
},
{
run_at: 'document_start',
js: [
'content/scripts/util.js',
'content/scripts/inputHandler.js'
],
matches: [
'<all_urls>'
],
include_globs: [
indexHTML,
getBraveExtUrl('about-*.html'),
getBraveExtUrl('about-*.html') + '#*'
],
exclude_globs: [
getBraveExtUrl('about-blank.html'),
getBraveExtUrl('about-blank.html') + '#*'
]
}
],
permissions: [
'externally_connectable.all_urls', 'tabs', '<all_urls>', 'contentSettings', 'idle'
],
externally_connectable: {
matches: [
'<all_urls>'
]
},
web_accessible_resources: [
'img/favicon.ico'
],
incognito: 'spanning',
key: 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAupOLMy5Fd4dCSOtjcApsAQOnuBdTs+OvBVt/3P93noIrf068x0xXkvxbn+fpigcqfNamiJ5CjGyfx9zAIs7zcHwbxjOw0Uih4SllfgtK+svNTeE0r5atMWE0xR489BvsqNuPSxYJUmW28JqhaSZ4SabYrRx114KcU6ko7hkjyPkjQa3P+chStJjIKYgu5tWBiMJp5QVLelKoM+xkY6S7efvJ8AfajxCViLGyDQPDviGr2D0VvIBob0D1ZmAoTvYOWafcNCaqaejPDybFtuLFX3pZBqfyOCyyzGhucyCmfBXJALKbhjRAqN5glNsUmGhhPK87TuGATQfVuZtenMvXMQIDAQAB'
}
let cspDirectives = {
'default-src': '\'self\'',
'form-action': '\'none\'',
'referrer': 'no-referrer',
'style-src': '\'self\' \'unsafe-inline\'',
'img-src': '* data: file://*',
'frame-src': '\'self\' https://buy.coinbase.com'
}
if (process.env.NODE_ENV === 'development') {
// allow access to webpack dev server resources
let devServer = 'localhost:' + process.env.npm_package_config_port
cspDirectives['default-src'] = '\'self\' http://' + devServer
cspDirectives['connect-src'] = ['\'self\'',
'http://' + devServer,
'ws://' + devServer].join(' ')
cspDirectives['style-src'] = '\'self\' \'unsafe-inline\' http://' + devServer
}
baseManifest.content_security_policy = concatCSP(cspDirectives)
return baseManifest
}
// Returns the Chromium extension manifest for the torrentExtension
// The torrentExtension handles magnet: URLs
// Analagous to the PDFJS extension, it shows a special UI for that type of resource
let generateTorrentManifest = () => {
let cspDirectives = {
'default-src': '\'self\'',
// TODO(bridiver) - remove example.com when webtorrent no longer requires it
// (i.e. once Brave uses webpack v2)
'connect-src': '\'self\' https://example.com',
'media-src': '\'self\' http://localhost:*',
'form-action': '\'none\'',
'referrer': 'no-referrer',
'style-src': '\'self\' \'unsafe-inline\'',
'frame-src': '\'self\' http://localhost:*'
}
if (process.env.NODE_ENV === 'development') {
// allow access to webpack dev server resources
let devServer = 'localhost:' + process.env.npm_package_config_port
cspDirectives['default-src'] += ' http://' + devServer
cspDirectives['connect-src'] += ' http://' + devServer + ' ws://' + devServer
cspDirectives['media-src'] += ' http://' + devServer
cspDirectives['frame-src'] += ' http://' + devServer
cspDirectives['style-src'] += ' http://' + devServer
}
return {
name: 'Torrent Viewer',
manifest_version: 2,
version: '1.0',
content_security_policy: concatCSP(cspDirectives),
content_scripts: [],
permissions: [
'externally_connectable.all_urls', 'tabs', '<all_urls>'
],
externally_connectable: {
matches: [
'<all_urls>'
]
},
icons: {
128: 'img/webtorrent-128.png',
48: 'img/webtorrent-48.png',
16: 'img/webtorrent-16.png'
},
web_accessible_resources: [
'img/favicon.ico',
'img/webtorrent-128.png',
'webtorrent.html'
],
incognito: 'split',
key: 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyWl+wMvL0wZX3JUs7GeZAvxMP+LWEh2bwMV1HyuBra/lGZIq3Fmh0+AFnvFPXz1NpQkbLS3QWyqhdIn/lepGwuc2ma0glPzzmieqwctUurMGSGManApGO1MkcbSPhb+R1mx8tMam5+wbme4WoW37PI3oATgOs2NvHYuP60qol3U7b/zB3IWuqtwtqKe2Q1xY17btvPuz148ygWWIHneedt0jwfr6Zp+CSLARB9Heq/jqGXV4dPSVZ5ebBHLQ452iZkHxS6fm4Z+IxjKdYs3HNj/s8xbfEZ2ydnArGdJ0lpSK9jkDGYyUBugq5Qp3FH6zV89WqBvoV1dqUmL9gxbHsQIDAQAB'
}
}
let generateSyncManifest = () => {
let cspDirectives = {
'default-src': '\'self\'',
'form-action': '\'none\''
}
cspDirectives['connect-src'] = ['\'self\'',
appConfig.sync.serverUrl,
appConfig.sync.s3Url].join(' ')
if (process.env.NODE_ENV === 'development') {
// allow access to webpack dev server resources
let devServer = 'localhost:' + process.env.npm_package_config_port
cspDirectives['default-src'] += ' http://' + devServer
cspDirectives['connect-src'] += ' http://' + devServer + ' ws://' + devServer + ' ' + appConfig.sync.testS3Url
}
return {
name: 'Brave Sync',
manifest_version: 2,
version: '1.0',
content_security_policy: concatCSP(cspDirectives),
content_scripts: [],
background: {
scripts: [ 'content/scripts/sync.js' ]
},
icons: {
128: 'img/sync-128.png',
48: 'img/sync-48.png',
16: 'img/sync-16.png'
},
incognito: 'spanning',
key: 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxOmBmOVzntEY6zrcrGSAyrhzL2FJt4FaP12nb899+SrV0LgpOgyqDjytuXT5IlHS74j7ZK2zTOTQy5/E9hqo6ioi1GA3PQU8E71DTaN6kW+XzP+VyZmgPoQHIxPg8jkYk/H4erfP9kMhkVOtu/XqDTqluNhOT0BvVlBpWd4unTQFWdgpCYlPrI6PsYya4FSuIDe6rCKtJABfuKFEr7U9d9MNAOJEnRS8vdBHWCuhWHqsfAaAPyKHQhnwFSFZ4eB+JznBQf7cQtB3EpOoBElyR9QvmbWFrYu87eGL5XxsojKHCrxlQ4X5ANsALa1Mdd2DHDMVqLMIiEEU42DVB0ZDewIDAQAB'
}
}
const extensionInfo = {
isEnabled: function (extensionId) {
return this.extensionState[extensionId] === extensionStates.ENABLED
},
isDisabled: function (extensionId) {
return this.extensionState[extensionId] === extensionStates.DISABLED
},
isLoading: function (extensionId) {
return this.extensionState[extensionId] === extensionStates.LOADING
},
isLoaded: function (extensionId) {
return [extensionStates.ENABLED, extensionStates.DISABLED].includes(this.extensionState[extensionId])
},
isRegistered: function (extensionId) {
return [extensionStates.REGISTERED, extensionStates.LOADING, extensionStates.ENABLED, extensionStates.DISABLED].includes(this.extensionState[extensionId])
},
isRegistering: function (extensionId) {
return this.extensionState[extensionId] === extensionStates.REGISTERING
},
getInstallInfo: function (extensionId) {
return this.installInfo[extensionId]
},
setState: function (extensionId, state) {
this.extensionState[extensionId] = state
},
setInstallInfo: function (extensionId, installInfo) {
this.installInfo[extensionId] = installInfo
},
extensionState: {},
installInfo: {}
}
const isExtension = (componentId) =>
componentId !== config.widevineComponentId
const isWidevine = (componentId) =>
componentId === config.widevineComponentId
module.exports.init = () => {
browserActions.init()
contextMenus.init()
const {componentUpdater, session} = require('electron')
componentUpdater.on('component-checking-for-updates', () => {
// console.log('checking for update')
})
componentUpdater.on('component-update-found', () => {
// console.log('update-found')
})
componentUpdater.on('component-update-ready', () => {
// console.log('update-ready')
})
componentUpdater.on('component-update-updated', (e, extensionId, version) => {
// console.log('update-updated', extensionId, version)
})
componentUpdater.on('component-ready', (e, componentId, extensionPath) => {
// console.log('component-ready', componentId, extensionPath)
// Re-setup the loadedExtensions info if it exists
extensionInfo.setState(componentId, extensionStates.REGISTERED)
if (isExtension(componentId)) {
loadExtension(componentId, extensionPath)
} else if (isWidevine(componentId)) {
appActions.resourceReady(appConfig.resourceNames.WIDEVINE)
}
})
componentUpdater.on('component-not-updated', () => {
// console.log('update-not-updated')
})
componentUpdater.on('component-registered', (e, extensionId) => {
const extensions = extensionState.getExtensions(appStore.getState())
const extensionPath = extensions.getIn([extensionId, 'filePath'])
// If we don't have info on the extension yet, check for an update / install
if (!extensionPath) {
componentUpdater.checkNow(extensionId)
} else {
extensionInfo.setState(extensionId, extensionStates.REGISTERED)
loadExtension(extensionId, extensionPath)
}
})
process.on('reload-sync-extension', () => {
console.log('reloading sync')
disableExtension(config.syncExtensionId)
})
process.on('extension-load-error', (error) => {
console.error(error)
})
process.on('extension-unloaded', (extensionId) => {
extensionInfo.setState(extensionId, extensionStates.DISABLED)
extensionActions.extensionDisabled(extensionId)
if (extensionId === config.syncExtensionId) {
// Reload sync extension to restart the background script
setImmediate(() => {
enableExtension(config.syncExtensionId)
})
}
})
process.on('extension-ready', (installInfo) => {
extensionInfo.setState(installInfo.id, extensionStates.ENABLED)
extensionInfo.setInstallInfo(installInfo.id, installInfo)
installInfo.filePath = installInfo.base_path
installInfo.base_path = fileUrl(installInfo.base_path)
extensionActions.extensionInstalled(installInfo.id, installInfo)
extensionActions.extensionEnabled(installInfo.id)
})
let loadExtension = (extensionId, extensionPath, manifest = {}, manifestLocation = 'unpacked') => {
if (!extensionInfo.isLoaded(extensionId) && !extensionInfo.isLoading(extensionId)) {
extensionInfo.setState(extensionId, extensionStates.LOADING)
if (extensionId === config.braveExtensionId || extensionId === config.torrentExtensionId || extensionId === config.syncExtensionId) {
session.defaultSession.extensions.load(extensionPath, manifest, manifestLocation)
return
}
// Verify we don't have info about an extension which doesn't exist
// on disk anymore. It will crash if it doesn't exist, so this is
// just a safety net.
fs.exists(path.join(extensionPath, 'manifest.json'), (exists) => {
if (exists) {
session.defaultSession.extensions.load(extensionPath, manifest, manifestLocation)
} else {
// This is an error condition, but we can recover.
extensionInfo.setState(extensionId, undefined)
componentUpdater.checkNow(extensionId)
}
})
} else {
enableExtension(extensionId)
}
}
let enableExtension = (extensionId) => {
session.defaultSession.extensions.enable(extensionId)
}
let disableExtension = (extensionId) => {
session.defaultSession.extensions.disable(extensionId)
}
let registerComponent = (extensionId) => {
if (!extensionInfo.isRegistered(extensionId) && !extensionInfo.isRegistering(extensionId)) {
extensionInfo.setState(extensionId, extensionStates.REGISTERING)
componentUpdater.registerComponent(extensionId)
} else {
const extensions = extensionState.getExtensions(appStore.getState())
const extensionPath = extensions.getIn([extensionId, 'filePath'])
if (extensionPath) {
// Otheriwse just install it
loadExtension(extensionId, extensionPath)
}
}
}
// Manually install the braveExtension and torrentExtension
extensionInfo.setState(config.braveExtensionId, extensionStates.REGISTERED)
loadExtension(config.braveExtensionId, getExtensionsPath('brave'), generateBraveManifest(), 'component')
extensionInfo.setState(config.syncExtensionId, extensionStates.REGISTERED)
loadExtension(config.syncExtensionId, getExtensionsPath('brave'), generateSyncManifest(), 'unpacked')
if (getSetting(settings.TORRENT_VIEWER_ENABLED)) {
extensionInfo.setState(config.torrentExtensionId, extensionStates.REGISTERED)
loadExtension(config.torrentExtensionId, getExtensionsPath('torrent'), generateTorrentManifest(), 'component')
} else {
extensionInfo.setState(config.torrentExtensionId, extensionStates.DISABLED)
extensionActions.extensionDisabled(config.torrentExtensionId)
}
let registerComponents = (diff) => {
if (getSetting(settings.PDFJS_ENABLED)) {
registerComponent(config.PDFJSExtensionId)
} else {
disableExtension(config.PDFJSExtensionId)
}
const activePasswordManager = getSetting(settings.ACTIVE_PASSWORD_MANAGER)
if (activePasswordManager === passwordManagers.ONE_PASSWORD) {
registerComponent(extensionIds[passwordManagers.ONE_PASSWORD])
} else {
disableExtension(extensionIds[passwordManagers.ONE_PASSWORD])
}
if (activePasswordManager === passwordManagers.DASHLANE) {
registerComponent(extensionIds[passwordManagers.DASHLANE])
} else {
disableExtension(extensionIds[passwordManagers.DASHLANE])
}
if (activePasswordManager === passwordManagers.LAST_PASS) {
registerComponent(extensionIds[passwordManagers.LAST_PASS])
} else {
disableExtension(extensionIds[passwordManagers.LAST_PASS])
}
if (activePasswordManager === passwordManagers.ENPASS) {
registerComponent(extensionIds[passwordManagers.ENPASS])
} else {
disableExtension(extensionIds[passwordManagers.ENPASS])
}
if (activePasswordManager === passwordManagers.BITWARDEN) {
registerComponent(extensionIds[passwordManagers.BITWARDEN])
} else {
disableExtension(extensionIds[passwordManagers.BITWARDEN])
}
if (getSetting(settings.POCKET_ENABLED)) {
registerComponent(config.PocketExtensionId)
} else {
disableExtension(config.PocketExtensionId)
}
if (getSetting(settings.VIMIUM_ENABLED)) {
registerComponent(config.vimiumExtensionId)
} else {
disableExtension(config.vimiumExtensionId)
}
if (appStore.getState().getIn(['widevine', 'enabled'])) {
registerComponent(config.widevineComponentId)
}
}
registerComponents()
appStore.addChangeListener(registerComponents)
}