Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] WebTorrent Support #353

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.tags*
/.idea/
/browser/resources/brave_extension/
/browser/resources/brave_webtorrent/*.bundle.js
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't be writing back into the source directory. Please see the wip sync branch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we couldn't access these *.bundle.js files from the extension if it's in gen, so I'm still generating webpack output files into src, they're now under components/brave_webtorrent/entension/out.

/dist/
/out/
/vendor/requests
Expand Down Expand Up @@ -39,4 +40,4 @@ npm-debug.log
*~
CMakeLists.txt
cmake-build-debug/
coverage/
coverage/
5 changes: 5 additions & 0 deletions browser/extensions/brave_component_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ void BraveComponentLoader::AddDefaultComponentExtensions(
brave_extension_path.Append(FILE_PATH_LITERAL("brave_extension"));
Add(IDR_BRAVE_EXTENSON, brave_extension_path);
}

base::FilePath brave_webtorrent_path(FILE_PATH_LITERAL(""));
brave_webtorrent_path =
brave_webtorrent_path.Append(FILE_PATH_LITERAL("brave_webtorrent"));
Add(IDR_BRAVE_WEBTORRENT, brave_webtorrent_path);
}

} // namespace extensions
4 changes: 3 additions & 1 deletion browser/extensions/brave_extension_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bool IsWhitelisted(const extensions::Extension* extension) {
}
static std::vector<std::string> whitelist({
brave_extension_id,
brave_webtorrent_extension_id,
pdfjs_extension_id,
// 1Password
"aomjjhallfgjeglblehebfpbcfeobpgk",
Expand Down Expand Up @@ -126,7 +127,8 @@ bool BraveExtensionProvider::UserMayLoad(const Extension* extension,

bool BraveExtensionProvider::MustRemainInstalled(const Extension* extension,
base::string16* error) const {
return extension->id() == brave_extension_id;
return extension->id() == brave_extension_id ||
extension->id() == brave_webtorrent_extension_id;
}

} // namespace extensions
1 change: 1 addition & 0 deletions browser/resources/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ grit("brave_extension_grit") {

deps = [
"//brave/vendor/brave-extension",
"//brave/browser/resources/brave_webtorrent",
]

output_dir = "$target_gen_dir"
Expand Down
5 changes: 5 additions & 0 deletions browser/resources/brave_extension.grd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<include name="IDR_BRAVE_EXTENSON_EN_US_MESSAGES_JSON" file="brave_extension/_locales/en_US/messages.json" type="BINDATA" />
<include name="IDR_BRAVE_EXTENSON_BRAVE_SHIELDS_HTML" file="brave_extension/braveShieldsPanel.html" type="BINDATA" />
<include name="IDR_BRAVE_EXTENSON_BRAVELIZER_JS" file="brave_extension/bravelizer.css" type="BINDATA" />

<!-- Brave Webtorrent -->
Copy link
Collaborator

@bridiver bridiver Aug 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see 88818ab - it handles both local and generated resources

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed by 32c0fe2

<include name="IDR_BRAVE_WEBTORRENT_BACKGROUND_JS" file="brave_webtorrent/brave_webtorrent_background.bundle.js" type="BINDATA" />
<include name="IDR_BRAVE_WEBTORRENT_HTML" file="brave_webtorrent/brave_webtorrent.html" type="BINDATA" />
<include name="IDR_BRAVE_WEBTORRENT_BUNDLE_JS" file="brave_webtorrent/brave_webtorrent.bundle.js" type="BINDATA" />
</includes>
</release>
</grit>
22 changes: 22 additions & 0 deletions browser/resources/brave_webtorrent/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import("//tools/grit/grit_rule.gni")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should all move into components/webtorrent and resources should be kept inside the component like sync

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed by 32c0fe2


action("brave_webtorrent") {
script = "//brave/script/build-brave-webtorrent.py"
inputs = [
"background.ts",
"brave_webtorrent.tsx",
]
outputs = [
"$target_out_dir/brave_webtorrent_background.bundle.js",
"$target_out_dir/brave_webtorrent.bundle.js",
]
gen_dir = rebase_path(root_gen_dir, "//")
args = [
"--target_gen_dir=$gen_dir"
]
if (is_official_build) {
args += [
"--production",
]
}
}
25 changes: 25 additions & 0 deletions browser/resources/brave_webtorrent/actions/tab_actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* 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/. */

import { action } from 'typesafe-actions'

// Constants
import { types } from '../constants/tab_types'

export const tabCreated = (tab: chrome.tabs.Tab) => action(types.TAB_CREATED, {
tab
})

export const tabUpdated = (tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) =>
action(types.TAB_UPDATED, {
tabId, changeInfo, tab
})

export const tabRemoved = (tabId: number) => action(types.TAB_REMOVED, {
tabId
})

export const activeTabChanged = (tabId: number, windowId: number) => action(types.ACTIVE_TAB_CHANGED, {
tabId, windowId
})
16 changes: 16 additions & 0 deletions browser/resources/brave_webtorrent/actions/webtorrent_actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* 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/. */

import { action } from 'typesafe-actions'
import { Torrent } from 'webtorrent'

// Constants
import { types } from '../constants/webtorrent_types'

export const progressUpdated = (torrent: Torrent) => action(types.WEBTORRENT_PROGRESS_UPDATED, { torrent })
export const infoUpdated = (torrent: Torrent) => action(types.WEBTORRENT_INFO_UPDATED, { torrent })
export const serverUpdated = (torrent: Torrent, serverURL: string) =>
action(types.WEBTORRENT_SERVER_UPDATED, { torrent, serverURL })
export const startTorrent = (torrentId: string, tabId: number) => action(types.WEBTORRENT_START_TORRENT, { torrentId, tabId })
export const stopDownload = (tabId: number) => action(types.WEBTORRENT_STOP_DOWNLOAD, { tabId })
20 changes: 20 additions & 0 deletions browser/resources/brave_webtorrent/actions/window_actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* 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/. */

import { action } from 'typesafe-actions'

// Constants
import { types } from '../constants/window_types'

export const windowCreated = (window: chrome.windows.Window) => action(types.WINDOW_CREATED, {
window
})

export const windowRemoved = (windowId: number) => action(types.WINDOW_REMOVED, {
windowId
})

export const windowFocusChanged = (windowId: number) => action(types.WINDOW_FOCUS_CHANGED, {
windowId
})
10 changes: 10 additions & 0 deletions browser/resources/brave_webtorrent/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* 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/. */

import { init } from './background/webtorrent'
init()

require('./background/store')
require('./background/events/tabsEvents')
require('./background/events/windowsEvents')
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* 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/. */

import { bindActionCreators } from 'redux'
import store from '../store'
import * as tabActions from '../../actions/tab_actions'
export default bindActionCreators(tabActions, store.dispatch)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* 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/. */

import { bindActionCreators } from 'redux'
import store from '../store'
import * as webtorrentActions from '../../actions/webtorrent_actions'
export default bindActionCreators(webtorrentActions, store.dispatch)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* 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/. */

import { bindActionCreators } from 'redux'
import store from '../store'
import * as windowActions from '../../actions/window_actions'
export default bindActionCreators(windowActions, store.dispatch)
21 changes: 21 additions & 0 deletions browser/resources/brave_webtorrent/background/events/tabsEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* 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/. */

import tabActions from '../actions/tabActions'

chrome.tabs.onCreated.addListener((tab: chrome.tabs.Tab) => {
tabActions.tabCreated(tab)
})

chrome.tabs.onUpdated.addListener((tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => {
tabActions.tabUpdated(tabId, changeInfo, tab)
})

chrome.tabs.onRemoved.addListener((tabId: number, removeInfo: chrome.tabs.TabRemoveInfo) => {
tabActions.tabRemoved(tabId)
})

chrome.tabs.onActivated.addListener((activeInfo: chrome.tabs.TabActiveInfo) => {
tabActions.activeTabChanged(activeInfo.tabId, activeInfo.windowId)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* 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/. */

import { Torrent } from 'webtorrent'
import * as throttle from 'throttleit'

import webtorrentActions from '../actions/webtorrentActions'
import { createServer } from '../webtorrent'

export const addTorrentEvents = (torrent: Torrent) => {
torrent.on('done', () => {
webtorrentActions.progressUpdated(torrent)
})
torrent.on('infoHash', () => {
console.log('infoHash event')
webtorrentActions.infoUpdated(torrent)
})
torrent.on('metadata', () => {
console.log('metadata event')
webtorrentActions.infoUpdated(torrent)
})
torrent.on('download', throttle((bytes: number) => {
webtorrentActions.progressUpdated(torrent)
}, 1000))
torrent.on('upload', throttle((bytes: number) => {
webtorrentActions.progressUpdated(torrent)
}, 1000))
torrent.on('ready', () => {
console.log('ready', torrent)
createServer(torrent, (serverURL: string) => {
webtorrentActions.serverUpdated(torrent, serverURL)
})
})
torrent.on('warning', (e: Error | string) => {
console.log('warning: ', torrent, e)
})
torrent.on('error', (e: Error | string) => {
console.log('error: ', torrent, e)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* 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/. */

import { Instance } from 'webtorrent'

export const addWebtorrentEvents = (webtorrent: Instance) => {
webtorrent.on('error', (e: Error | string) => {
console.log('WebTorrent error: ', e)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* 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/. */

import windowActions from '../actions/windowActions'

chrome.windows.onFocusChanged.addListener((windowId: number) => {
windowActions.windowFocusChanged(windowId)
})

chrome.windows.onCreated.addListener((window: chrome.windows.Window) => {
windowActions.windowCreated(window)
})

chrome.windows.onRemoved.addListener((windowId: number) => {
windowActions.windowRemoved(windowId)
})
13 changes: 13 additions & 0 deletions browser/resources/brave_webtorrent/background/reducers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* 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/. */

import { combineReducers } from 'redux'

// Utils
import webtorrentReducer from './webtorrent_reducer'
import { ApplicationState } from '../../constants/webtorrentState'

export default combineReducers<ApplicationState>({
torrentsData: webtorrentReducer
})
Loading