Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
move webtorrent to its own extension
Browse files Browse the repository at this point in the history
previously, torrent support was implemented in the brave extension
  • Loading branch information
dcposch authored and bbondy committed Nov 22, 2016
1 parent e748b2f commit d14c778
Show file tree
Hide file tree
Showing 20 changed files with 166 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ win64-dist
Brave.tar.bz2

app/extensions/brave/gen
app/extensions/torrent/gen
*.pfx
buildConfig.js

Expand Down
4 changes: 2 additions & 2 deletions app/browser/tabs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {app, BrowserWindow, session, webContents} = require('electron')
const extensions = process.atomBinding('extension')
const { getIndexHTML } = require('../../js/lib/appUrlUtil')
const { getBraveExtIndexHTML } = require('../../js/lib/appUrlUtil')

let currentWebContents = {}
let activeTab = null
Expand All @@ -13,7 +13,7 @@ const tabs = {
init: () => {
app.on('web-contents-created', function (event, tab) {
// TODO(bridiver) - also exclude extension action windows??
if (extensions.isBackgroundPage(tab) || tab.getURL() === getIndexHTML()) {
if (extensions.isBackgroundPage(tab) || tab.getURL() === getBraveExtIndexHTML()) {
return
}
let tabId = tab.getId()
Expand Down
97 changes: 73 additions & 24 deletions app/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 {getAppUrl, getExtensionsPath, getIndexHTML} = 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')
Expand All @@ -15,7 +15,21 @@ 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,
Expand All @@ -32,7 +46,7 @@ let generateBraveManifest = () => {
'http://*/*', 'https://*/*', 'file://*', 'data:*', 'about:srcdoc'
],
exclude_globs: [
getIndexHTML()
indexHTML
],
match_about_blank: true,
js: [
Expand All @@ -55,7 +69,7 @@ let generateBraveManifest = () => {
'http://*/*', 'https://*/*', 'file://*', 'data:*', 'about:srcdoc'
],
exclude_globs: [
getIndexHTML()
indexHTML
],
js: [
'content/scripts/adInsertion.js',
Expand All @@ -70,13 +84,13 @@ let generateBraveManifest = () => {
matches: ['<all_urls>'],
include_globs: [
'http://*/*', 'https://*/*', 'file://*', 'data:*', 'about:srcdoc',
getIndexHTML(),
getAppUrl('about-*.html'),
getAppUrl('about-*.html') + '#*'
indexHTML,
getBraveExtUrl('about-*.html'),
getBraveExtUrl('about-*.html') + '#*'
],
exclude_globs: [
getAppUrl('about-blank.html'),
getAppUrl('about-blank.html') + '#*'
getBraveExtUrl('about-blank.html'),
getBraveExtUrl('about-blank.html') + '#*'
],
js: [
'content/scripts/spellCheck.js',
Expand All @@ -93,13 +107,13 @@ let generateBraveManifest = () => {
'<all_urls>'
],
include_globs: [
getIndexHTML(),
getAppUrl('about-*.html'),
getAppUrl('about-*.html') + '#*'
indexHTML,
getBraveExtUrl('about-*.html'),
getBraveExtUrl('about-*.html') + '#*'
],
exclude_globs: [
getAppUrl('about-blank.html'),
getAppUrl('about-blank.html') + '#*'
getBraveExtUrl('about-blank.html'),
getBraveExtUrl('about-blank.html') + '#*'
]
}
],
Expand All @@ -125,7 +139,7 @@ let generateBraveManifest = () => {
'form-action': '\'none\'',
'referrer': 'no-referrer',
'style-src': '\'self\' \'unsafe-inline\'',
'img-src': '* data: blob:',
'img-src': '* data:',
'frame-src': '\'self\' https://buy.coinbase.com'
}

Expand All @@ -137,19 +151,52 @@ let generateBraveManifest = () => {
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\'',
'form-action': '\'none\'',
'referrer': 'no-referrer',
'style-src': '\'self\' \'unsafe-inline\'',
'img-src': '* data: blob:'
}

// TODO:
// * Move WebTorrent to its own renderer process, similar to the way it's done in
// WebTorrent Desktop
// * Move WebTorrent to its own process, similar to the way it's done in WebTorrent Desktop
// * Remove this CSP exception:
cspDirectives['connect-src'] = '*'

var csp = ''
for (var directive in cspDirectives) {
csp += directive + ' ' + cspDirectives[directive] + '; '
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'
],
incognito: 'split'
}
baseManifest.content_security_policy = csp.trim()

return baseManifest
}

const extensionInfo = {
Expand Down Expand Up @@ -252,7 +299,7 @@ module.exports.init = () => {
let loadExtension = (extensionId, extensionPath, manifest = {}, manifestLocation = 'unpacked') => {
if (!extensionInfo.isLoaded(extensionId) && !extensionInfo.isLoading(extensionId)) {
extensionInfo.setState(extensionId, extensionStates.LOADING)
if (extensionId === config.braveExtensionId) {
if (extensionId === config.braveExtensionId || extensionId === config.torrentExtensionId) {
session.defaultSession.extensions.load(extensionPath, manifest, manifestLocation)
return
}
Expand Down Expand Up @@ -295,9 +342,11 @@ module.exports.init = () => {
}
}

// Manually install only the braveExtension
// Manually install the braveExtension and torrentExtension
extensionInfo.setState(config.braveExtensionId, extensionStates.REGISTERED)
loadExtension(config.braveExtensionId, getExtensionsPath('brave'), generateBraveManifest(), 'component')
extensionInfo.setState(config.torrentExtensionId, extensionStates.REGISTERED)
loadExtension(config.torrentExtensionId, getExtensionsPath('torrent'), generateTorrentManifest(), 'component')

let registerComponents = () => {
if (getSetting(settings.PDFJS_ENABLED)) {
Expand Down
Loading

0 comments on commit d14c778

Please sign in to comment.