Skip to content

Fix #6372 Makes WebTorrent extension non-persistent #3638

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

Merged
merged 2 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 3 additions & 10 deletions components/brave_webtorrent/extension/actions/tab_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,16 @@ 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) =>
export const tabUpdated = (tabId: number,
changeInfo: chrome.webNavigation.WebNavigationFramedCallbackDetails) =>
action(types.TAB_UPDATED, {
tabId, changeInfo, tab
tabId, changeInfo
})

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

export const activeTabChanged = (tabId: number, windowId: number) => action(types.ACTIVE_TAB_CHANGED, {
tabId, windowId
})

export const tabRetrieved = (tab: chrome.tabs.Tab) => action(types.TAB_RETRIEVED, {
tab
})
20 changes: 0 additions & 20 deletions components/brave_webtorrent/extension/actions/window_actions.ts

This file was deleted.

1 change: 0 additions & 1 deletion components/brave_webtorrent/extension/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
import './background/events/webtorrentEvents'
import './background/events/torrentEvents'
import './background/events/tabsEvents'
import './background/events/windowsEvents'
import './background/store'

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,38 @@

import tabActions from '../actions/tabActions'

chrome.tabs.onCreated.addListener((tab: chrome.tabs.Tab) => {
tabActions.tabCreated(tab)
})
chrome.webNavigation.onCommitted.addListener(
(details: chrome.webNavigation.WebNavigationFramedCallbackDetails) => {
tabActions.tabUpdated(details.tabId, details)
},
{ url: [
{ pathSuffix: '.torrent' },
// have a magnet scheme
{ schemes: ['magnet'] }]
}
)

chrome.tabs.onUpdated.addListener((tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => {
tabActions.tabUpdated(tabId, changeInfo, tab)
})
chrome.webNavigation.onCommitted.addListener(
(details: chrome.webNavigation.WebNavigationFramedCallbackDetails) => {
// navigation events come in with the webtorrent extension as
// the schema/host, the actual webtorrent URL appended to it
// after the chrome-extenion://extension-id/view.html?
// delete everything up to the first question mark
let original = details.url.replace(new RegExp(/^[^\?]*\?/), '')
let decoded = decodeURIComponent(original)
details.url = decoded
tabActions.tabUpdated(details.tabId, details)
},
{ url: [
// when handling URLs as chrome-extension://...
// the actual torrent URL is within query parameters
{ schemes: ['chrome-extension'], queryContains: '.torrent' },
{ schemes: ['chrome-extension'], queryContains: 'magnet%3A' }]
}
)

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)
})
chrome.tabs.onRemoved.addListener(
(tabId: number, removeInfo: chrome.tabs.TabRemoveInfo) => {
tabActions.tabRemoved(tabId)
}
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { parse } from 'querystring'

// Constants
import * as tabTypes from '../../constants/tab_types'
import * as windowTypes from '../../constants/window_types'
import * as torrentTypes from '../../constants/webtorrent_types'
import { File, TorrentState, TorrentsState } from '../../constants/webtorrentState'

Expand All @@ -19,45 +18,8 @@ import {
findTorrent,
saveAllFiles as webtorrentSaveAllFiles
} from '../webtorrent'
import { getTabData } from '../api/tabs_api'
import { parseTorrentRemote } from '../api/torrent_api'

const focusedWindowChanged = (windowId: number, state: TorrentsState) => {
return { ...state, currentWindowId: windowId }
}

const windowRemoved = (windowId: number, state: TorrentsState) => {
const { activeTabIds } = state
delete activeTabIds[windowId]
return { ...state, activeTabIds }
}

const activeTabChanged = (tabId: number, windowId: number, state: TorrentsState) => {
const { activeTabIds, torrentStateMap } = state
activeTabIds[windowId] = tabId
if (!torrentStateMap[tabId]) {
getTabData(tabId)
}
return { ...state, activeTabIds }
}

const windowCreated = (window: chrome.windows.Window, state: TorrentsState) => {
// update currentWindowId if needed
if (window.focused || Object.keys(state.activeTabIds).length === 0) {
state = focusedWindowChanged(window.id, state)
}

// update its activeTabId
if (window.tabs) {
const tab = window.tabs.find((tab: chrome.tabs.Tab) => tab.active)
if (tab && tab.id) {
state = activeTabChanged(tab.id, window.id, state)
}
}

return state
}

const tabUpdated = (tabId: number, url: string, state: TorrentsState) => {
const { torrentStateMap, torrentObjMap } = state
const origTorrentState: TorrentState = torrentStateMap[tabId]
Expand Down Expand Up @@ -125,7 +87,9 @@ const tabRemoved = (tabId: number, state: TorrentsState) => {
if (torrentState) {
const infoHash = torrentState.infoHash
if (infoHash && torrentObjMap[infoHash]) { // unsubscribe
torrentObjMap[infoHash].tabClients.delete(tabId)
if (torrentObjMap[infoHash].tabClients) {
torrentObjMap[infoHash].tabClients.delete(tabId)
}
if (!torrentObjMap[infoHash].tabClients.size) {
delete torrentObjMap[infoHash]
delTorrent(infoHash)
Expand Down Expand Up @@ -234,42 +198,13 @@ const saveAllFiles = (state: TorrentsState, infoHash: string) => {
return { ...state }
}

const defaultState: TorrentsState = { currentWindowId: -1, activeTabIds: {}, torrentStateMap: {}, torrentObjMap: {} }
const defaultState: TorrentsState = { torrentStateMap: {}, torrentObjMap: {} }
export const webtorrentReducer = (state: TorrentsState = defaultState, action: any) => { // TODO: modify any to be actual action type
const payload = action.payload
switch (action.type) {
case windowTypes.types.WINDOW_CREATED:
state = windowCreated(payload.window, state)
break
case windowTypes.types.WINDOW_REMOVED:
state = windowRemoved(payload.windowId, state)
break
case windowTypes.types.WINDOW_FOCUS_CHANGED:
state = focusedWindowChanged(payload.windowId, state)
break
case tabTypes.types.ACTIVE_TAB_CHANGED:
if (state.currentWindowId === -1) {
state = focusedWindowChanged(payload.windowId, state)
}
state = activeTabChanged(payload.tabId, payload.windowId, state)
break
case tabTypes.types.TAB_CREATED:
if (payload.tab.id && payload.tab.url) {
state = tabUpdated(payload.tab.id, payload.tab.url, state)
}
break
case tabTypes.types.TAB_UPDATED:
// it's possible to be the first event when browser starts
// initialize currentWindowId and its activeTabId if so
if (state.currentWindowId === -1) {
state = focusedWindowChanged(payload.tab.windowId, state)
}
if (!state.activeTabIds[state.currentWindowId] && payload.tab.active) {
state = activeTabChanged(payload.tab.id, payload.tab.windowId, state)
}

if (payload.changeInfo.url) {
state = tabUpdated(payload.tab.id, payload.changeInfo.url, state)
state = tabUpdated(payload.changeInfo.tabId, payload.changeInfo.url, state)
}
break
case tabTypes.types.TAB_REMOVED:
Expand Down
2 changes: 0 additions & 2 deletions components/brave_webtorrent/extension/constants/tab_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

export const enum types {
TAB_CREATED = '@@tab/TAB_CREATED',
TAB_UPDATED = '@@tab/TAB_UPDATED',
TAB_REMOVED = '@@tab/TAB_REMOVED',
ACTIVE_TAB_CHANGED = '@@tab/ACTIVE_TAB_CHANGED',
TAB_RETRIEVED = '@@tab/TAB_RETRIEVED'
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ export interface ApplicationState {
torrentsData: TorrentsState
}

export interface ActiveTabIds {
[key: number]: number // windowId as key
}

export interface TorrentsState {
currentWindowId: number
activeTabIds: ActiveTabIds
torrentStateMap: TorrentStateMap
torrentObjMap: TorrentObjMap
}
Expand Down

This file was deleted.

10 changes: 8 additions & 2 deletions components/brave_webtorrent/extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
"description": "WebTorrent extension",
"background" : {
"scripts": ["extension/out/brave_webtorrent_background.bundle.js"],
"persistent": true
"persistent": false
},
"web_accessible_resources": [
"extension/brave_webtorrent.html"
],
"permissions": ["downloads", "dns", "tabs", "windows", "<all_urls>"],
"permissions": [
"downloads",
"dns",
"tabs",
"webNavigation",
"<all_urls>" // without '<all_urls>' sometimes get CORS issues when fetching the torrent file
],
"sockets": {
"udp": {
"send": "*",
Expand Down
20 changes: 6 additions & 14 deletions components/test/brave_webtorrent/actions/tab_action_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,14 @@ const changeInfo: chrome.tabs.TabChangeInfo = {
}

const tabId: number = 1
const windowId: number = 2

describe('tab_actions', () => {
it('tabCreated', () => {
expect(actions.tabCreated(tab)).toEqual({
type: types.TAB_CREATED,
meta: undefined,
payload: { tab }
})
})

it('tabUpdated', () => {
expect(actions.tabUpdated(tabId, changeInfo, tab)).toEqual({
expect(actions.tabUpdated(tabId, changeInfo)).toEqual({
type: types.TAB_UPDATED,
meta: undefined,
payload: { tabId, changeInfo, tab }
payload: { tabId, changeInfo }
})
})

Expand All @@ -50,11 +42,11 @@ describe('tab_actions', () => {
})
})

it('activeTabChanged', () => {
expect(actions.activeTabChanged(tabId, windowId)).toEqual({
type: types.ACTIVE_TAB_CHANGED,
it('tabRetrieved', () => {
expect(actions.tabRetrieved(tab)).toEqual({
type: types.TAB_RETRIEVED,
meta: undefined,
payload: { tabId, windowId }
payload: { tab }
})
})
})
42 changes: 0 additions & 42 deletions components/test/brave_webtorrent/actions/window_action_test.ts

This file was deleted.

Loading