Skip to content
This repository has been archived by the owner on Jun 16, 2020. It is now read-only.

Commit

Permalink
following example of Webtorrent PR 5342
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jun 27, 2017
1 parent 9ef1d5a commit 34d2501
Show file tree
Hide file tree
Showing 37 changed files with 208 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Brave.tar.bz2

app/extensions/brave/gen
app/extensions/torrent/gen
app/extensions/ipfs/gen

*.pfx
buildConfig.js

Expand Down
77 changes: 77 additions & 0 deletions app/browser/ipfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* IPFS node in electron's main process */

// TODO(diasdavid) - review electron's main process support https://github.com/ipfs/js-ipfs/issues/843

// const electron = require('electron')
// const ipc = electron.ipcMain
// const messages = require('../../js/constants/messages')
// const IPFS = require('ipfs')
// const os = require('os')
// const path = require('path')

module.exports = {init}

// Set to see communication between WebTorrent and torrent viewer tabs
const DEBUG_IPC = false
if (DEBUG_IPC) {
console.log('IPFS IPC debugging enabled')
}

// IPFS node
// let node = null
// let channels = {}

// Receive messages via the window process, ultimately from the UI in a <webview> process
function init (state, action) {
if (DEBUG_IPC) {
console.log('IPFS IPC init')
}

return state

/*
node = new IPFS({
repo: path.join(os.homedir(), '/.brave-jsipfs')
})
node.on('ready', () => {
if (DEBUG_IPC) {
console.log('IPFS node is ready')
}
})
node.on('error', (err) => console.log('IPFS: ', err))
// access on the browser with remote.getGlobal('ipfs')
global.ipfs = node
*/

/* TODO(diasdavid) consider enabling the browser tab to start and stop the node
ipc.on(messages.IPFS_MESSAGE, function (event, msg) {
if (DEBUG_IPC) {
console.log('IPFS: Received IPC: ' + JSON.stringify(msg))
}
channels[msg.clientKey] = e.sender
server.receive(msg)
})
*/
}

// Send messages from the browser process (here), thru the window process, to the <webview>
/*
function send (msg) {
if (DEBUG_IPC) console.log('Sending IPC: ' + JSON.stringify(msg))
const channel = channels[msg.clientKey]
if (!channel) {
if (DEBUG_IPC) console.error('Ignoring unrecognized clientKey ' + msg.clientKey)
return
}
if (channel.isDestroyed()) {
if (DEBUG_IPC) console.log('Removing destroyed channel, clientKey ' + msg.clientKey)
delete channels[msg.clientKey]
return
}
channel.send(messages.IPFS_MESSAGE, msg)
}
*/
65 changes: 63 additions & 2 deletions app/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,53 @@ let generateTorrentManifest = () => {
}
}

// Returns the Chromium extension manifest for the ipfsExtension
// The ipfsExtension handles ipfs:// and dweb:
// Analagous to the PDFJS extension, it shows a special UI for that type of resource
let generateIPFSManifest = () => {
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\'',
'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: 'IPFS Viewer',
description: l10n.translation('ipfsDesc'),
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/ipfs-128.png',
48: 'img/ipfs-48.png',
16: 'img/ipfs-16.png'
},
incognito: 'split',
// TODO(diasdavid) How to get this key??
key: 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyWl+wMvL0wZX3JUs7GeZAvxMP+LWEh2bwMV1HyuBra/lGZIq3Fmh0+AFnvFPXz1NpQkbLS3QWyqhdIn/lepGwuc2ma0glPzzmieqwctUurMGSGManApGO1MkcbSPhb+R1mx8tMam5+wbme4WoW37PI3oATgOs2NvHYuP60qol3U7b/zB3IWuqtwtqKe2Q1xY17btvPuz148ygWWIHneedt0jwfr6Zp+CSLARB9Heq/jqGXV4dPSVZ5ebBHLQ452iZkHxS6fm4Z+IxjKdYs3HNj/s8xbfEZ2ydnArGdJ0lpSK9jkDGYyUBugq5Qp3FH6zV89WqBvoV1dqUmL9gxbHsQIDAQAB'
}
}

let generateSyncManifest = () => {
let cspDirectives = {
'default-src': '\'self\'',
Expand Down Expand Up @@ -419,7 +466,10 @@ module.exports.init = () => {
}
if (!extensionInfo.isLoaded(extensionId) && !extensionInfo.isLoading(extensionId)) {
extensionInfo.setState(extensionId, extensionStates.LOADING)
if (extensionId === config.braveExtensionId || extensionId === config.torrentExtensionId || extensionId === config.syncExtensionId) {
if (extensionId === config.braveExtensionId ||
extensionId === config.torrentExtensionId ||
extensionId === config.syncExtensionId ||
extensionId === config.ipfsExtensionId) {
session.defaultSession.extensions.load(extensionPath, manifest, manifestLocation)
return
}
Expand Down Expand Up @@ -454,11 +504,13 @@ module.exports.init = () => {
}
}

// Manually install the braveExtension and torrentExtension
// Manually install the braveExtension, torrentExtension and ipfsExtension
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')

// torrentExtension
if (getSetting(settings.TORRENT_VIEWER_ENABLED)) {
extensionInfo.setState(config.torrentExtensionId, extensionStates.REGISTERED)
loadExtension(config.torrentExtensionId, getExtensionsPath('torrent'), generateTorrentManifest(), 'component')
Expand All @@ -467,6 +519,15 @@ module.exports.init = () => {
extensionActions.extensionDisabled(config.torrentExtensionId)
}

// ipfsExtension
if (getSetting(settings.IPFS_ENABLED)) {
extensionInfo.setState(config.ipfsExtensionId, extensionStates.REGISTERED)
loadExtension(config.ipfsExtensionId, getExtensionsPath('ipfs'), generateIPFSManifest(), 'component')
} else {
extensionInfo.setState(config.ipfsExtensionId, extensionStates.DISABLED)
extensionActions.extensionDisabled(config.ipfsExtensionId)
}

let registerComponents = (diff) => {
if (getSetting(settings.PDFJS_ENABLED)) {
registerComponent(config.PDFJSExtensionId)
Expand Down
1 change: 1 addition & 0 deletions app/extensions/brave/locales/bn-BD/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=সর্বদা URL বার দেখান
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/bn-IN/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Always show the URL bar
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/cs/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=Volby pro adresní řádek
disableTitleMode=Vždy ukázat adresní řádek
wideURLbar=Použít široký adresní řádek
autoplay=Automatické přehrávání
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/de-DE/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=Einstellungen der URL-Zeile
disableTitleMode=URL Zeile immer anzeigen
wideURLbar=Lange URL-Zeile verwenden
autoplay=Automatisch Medien abspielen
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/en-US/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Always show the URL bar
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/es/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Mostrar siempre la barra de URL
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/fr-FR/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Toujours afficher la barre d'adresse
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/hi-IN/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Always show the URL bar
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/id-ID/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=Pilihan bilah URL
disableTitleMode=Selalu tampilkan bilah URL
wideURLbar=Gunakan bilah URL yang lebar
autoplay=Media Putar Otomatis
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/it-IT/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=Opzioni della barra degli indirizzi
disableTitleMode=Visualizza sempre la barra degli indirizzi
wideURLbar=Utilizza barra degli indirizzi ampia
autoplay=Riproduzione automatica dei file multimediali
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/ja-JP/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URLバーの設定
disableTitleMode=URLバーを常に表示
wideURLbar=幅の広いURLバーを使う
autoplay=自動再生
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/ko-KR/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=주소창 항상 표시
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/ms-MY/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=Pilihan Bar URL
disableTitleMode=Sentiasa pamer bar URL
wideURLbar=Guna bar URL lebar
autoplay=Media Autoplay
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/nl-NL/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Altijd de URL-balk laten zien
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/pl-PL/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Always show the URL bar
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/pt-BR/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Mostrar sempre a barra de URL
wideURLbar=Use wide URL bar
autoplay=Reproduzir mídia automaticamente
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/ru/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=Параметры панели адресной строки
disableTitleMode=Всегда отображать строку адреса
wideURLbar=Использовать широкую панель адресной строки
autoplay=Автовоспроизведение медиа
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/sl/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=Možnosti vrstice URL
disableTitleMode=Vedno pokaži vrstico naslova URL
wideURLbar=Uporabi široko vrstico URL
autoplay=Samodejno predvajaj datoteke
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/ta/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Always show the URL bar
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/te/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Always show the URL bar
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/tr-TR/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Url çubuğunu her zaman göster
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/uk/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=Завжди показувати панель адреси
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
1 change: 1 addition & 0 deletions app/extensions/brave/locales/zh-CN/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ urlBarOptions=URL Bar Options
disableTitleMode=总是显示URL栏
wideURLbar=Use wide URL bar
autoplay=Autoplay Media
useIPFS=Enable IPFS (requires browser restart)
9 changes: 9 additions & 0 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ function registerPermissionHandler (session, partition) {
return
}

// The IPFS extension is always allowed to show fullscreen media
if (permission === 'fullscreen' &&
origin.startsWith('chrome-extension://' + config.ipfsExtensionId)) {
cb(true)
return
}

// Always allow fullscreen if setting is ON
const alwaysAllowFullscreen = module.exports.alwaysAllowFullscreen() === fullscreenOption.ALWAYS_ALLOW
if (permission === 'fullscreen' && alwaysAllowFullscreen) {
Expand Down Expand Up @@ -412,6 +419,8 @@ function registerPermissionHandler (session, partition) {
// This covers an edge case where you open a magnet link tab, then disable Torrent Viewer
// and restart Brave. I don't think it needs localization. See 'Brave Browser' above.
origin = 'Magnet URL'
} else if (mainFrameUrl.startsWith('ipfs://') || mainFrameUrl.startsWith('ipfs://')) {
origin = 'IPFS path'
} else {
// Strip trailing slash
origin = getOrigin(origin)
Expand Down
10 changes: 9 additions & 1 deletion app/renderer/components/frame/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const domUtil = require('../../lib/domUtil')
const {
aboutUrls,
isSourceMagnetUrl,
isSourceIPFSPath,
isTargetAboutUrl,
getTargetAboutUrl,
getBaseUrl,
Expand Down Expand Up @@ -68,6 +69,12 @@ function isTorrentViewerURL (url) {
return isEnabled && isSourceMagnetUrl(url)
}

function isIPFSPath (url) {
const isEnabled = getSetting(settings.IPFS_ENABLED)
console.log('IPFS: is enabled (%s) is IPFS path (%s)', isEnabled, isSourceIPFSPath(url))
return isEnabled && isSourceIPFSPath(url)
}

class Frame extends React.Component {
constructor (props) {
super(props)
Expand Down Expand Up @@ -316,7 +323,8 @@ class Frame extends React.Component {
// In this case both the user display and the user think they're on this.props.location.
if (this.props.tabUrl !== this.props.location &&
!this.isAboutPage() &&
!isTorrentViewerURL(this.props.location)) {
!isTorrentViewerURL(this.props.location) &&
!isIPFSPath(this.props.location)) {
this.webview.loadURL(this.props.location)
} else {
tabActions.reload(this.props.tabId)
Expand Down
1 change: 1 addition & 0 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ AppStore
'advanced.send-usage-statistics': boolean, // true or undefined if usage reports should be sent
'advanced.smooth-scroll-enabled': boolean, // false if smooth scrolling should be explicitly disabled
'advanced.torrent-viewer-enabled': boolean, // whether to render magnet links in the browser
'advanced.ipfs-enabled': boolean, // whether to use IPFS to render ipfs:// or dweb: links in the browser
'bookmarks.toolbar.show': boolean, // true if the bookmakrs toolbar should be shown
'bookmarks.toolbar.showFavicon': boolean, // true if bookmark favicons should be shown on the bookmarks toolbar
'bookmarks.toolbar.showOnlyFavicon': boolean, // true if only favicons should be shown on the bookmarks toolbar
Expand Down
8 changes: 6 additions & 2 deletions js/about/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,12 @@ class AboutPreferences extends React.Component {
if (key === settings.HARDWARE_ACCELERATION_ENABLED ||
key === settings.DO_NOT_TRACK ||
key === settings.LANGUAGE ||
key === settings.PDFJS_ENABLED || key === settings.TORRENT_VIEWER_ENABLED ||
key === settings.SMOOTH_SCROLL_ENABLED || key === settings.SEND_CRASH_REPORTS || key === settings.UPDATE_TO_PREVIEW_RELEASES) {
key === settings.PDFJS_ENABLED ||
key === settings.TORRENT_VIEWER_ENABLED ||
key === settings.IPFS_ENABLED ||
key === settings.SMOOTH_SCROLL_ENABLED ||
key === settings.SEND_CRASH_REPORTS ||
key === settings.UPDATE_TO_PREVIEW_RELEASES) {
ipc.send(messages.PREFS_RESTART, key, value)
}
if (key === settings.PAYMENTS_ENABLED) {
Expand Down
Loading

0 comments on commit 34d2501

Please sign in to comment.