Skip to content

Commit 6670537

Browse files
authored
Merge pull request #3638 from brave/webtorrent-non-persistent
Fix #6372 Makes WebTorrent extension non-persistent
2 parents aa7d1dc + 7da959f commit 6670537

File tree

16 files changed

+72
-326
lines changed

16 files changed

+72
-326
lines changed

components/brave_webtorrent/extension/actions/tab_actions.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,16 @@ import { action } from 'typesafe-actions'
77
// Constants
88
import { types } from '../constants/tab_types'
99

10-
export const tabCreated = (tab: chrome.tabs.Tab) => action(types.TAB_CREATED, {
11-
tab
12-
})
13-
14-
export const tabUpdated = (tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) =>
10+
export const tabUpdated = (tabId: number,
11+
changeInfo: chrome.webNavigation.WebNavigationFramedCallbackDetails) =>
1512
action(types.TAB_UPDATED, {
16-
tabId, changeInfo, tab
13+
tabId, changeInfo
1714
})
1815

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

23-
export const activeTabChanged = (tabId: number, windowId: number) => action(types.ACTIVE_TAB_CHANGED, {
24-
tabId, windowId
25-
})
26-
2720
export const tabRetrieved = (tab: chrome.tabs.Tab) => action(types.TAB_RETRIEVED, {
2821
tab
2922
})

components/brave_webtorrent/extension/actions/window_actions.ts

-20
This file was deleted.

components/brave_webtorrent/extension/background.ts

-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@
55
import './background/events/webtorrentEvents'
66
import './background/events/torrentEvents'
77
import './background/events/tabsEvents'
8-
import './background/events/windowsEvents'
98
import './background/store'

components/brave_webtorrent/extension/background/actions/windowActions.ts

-8
This file was deleted.

components/brave_webtorrent/extension/background/api/tabs_api.ts

-9
This file was deleted.

components/brave_webtorrent/extension/background/events/tabsEvents.ts

+33-13
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,38 @@
44

55
import tabActions from '../actions/tabActions'
66

7-
chrome.tabs.onCreated.addListener((tab: chrome.tabs.Tab) => {
8-
tabActions.tabCreated(tab)
9-
})
7+
chrome.webNavigation.onCommitted.addListener(
8+
(details: chrome.webNavigation.WebNavigationFramedCallbackDetails) => {
9+
tabActions.tabUpdated(details.tabId, details)
10+
},
11+
{ url: [
12+
{ pathSuffix: '.torrent' },
13+
// have a magnet scheme
14+
{ schemes: ['magnet'] }]
15+
}
16+
)
1017

11-
chrome.tabs.onUpdated.addListener((tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => {
12-
tabActions.tabUpdated(tabId, changeInfo, tab)
13-
})
18+
chrome.webNavigation.onCommitted.addListener(
19+
(details: chrome.webNavigation.WebNavigationFramedCallbackDetails) => {
20+
// navigation events come in with the webtorrent extension as
21+
// the schema/host, the actual webtorrent URL appended to it
22+
// after the chrome-extenion://extension-id/view.html?
23+
// delete everything up to the first question mark
24+
let original = details.url.replace(new RegExp(/^[^\?]*\?/), '')
25+
let decoded = decodeURIComponent(original)
26+
details.url = decoded
27+
tabActions.tabUpdated(details.tabId, details)
28+
},
29+
{ url: [
30+
// when handling URLs as chrome-extension://...
31+
// the actual torrent URL is within query parameters
32+
{ schemes: ['chrome-extension'], queryContains: '.torrent' },
33+
{ schemes: ['chrome-extension'], queryContains: 'magnet%3A' }]
34+
}
35+
)
1436

15-
chrome.tabs.onRemoved.addListener((tabId: number, removeInfo: chrome.tabs.TabRemoveInfo) => {
16-
tabActions.tabRemoved(tabId)
17-
})
18-
19-
chrome.tabs.onActivated.addListener((activeInfo: chrome.tabs.TabActiveInfo) => {
20-
tabActions.activeTabChanged(activeInfo.tabId, activeInfo.windowId)
21-
})
37+
chrome.tabs.onRemoved.addListener(
38+
(tabId: number, removeInfo: chrome.tabs.TabRemoveInfo) => {
39+
tabActions.tabRemoved(tabId)
40+
}
41+
)

components/brave_webtorrent/extension/background/events/windowsEvents.ts

-17
This file was deleted.

components/brave_webtorrent/extension/background/reducers/webtorrent_reducer.ts

+5-70
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { parse } from 'querystring'
88

99
// Constants
1010
import * as tabTypes from '../../constants/tab_types'
11-
import * as windowTypes from '../../constants/window_types'
1211
import * as torrentTypes from '../../constants/webtorrent_types'
1312
import { File, TorrentState, TorrentsState } from '../../constants/webtorrentState'
1413

@@ -19,45 +18,8 @@ import {
1918
findTorrent,
2019
saveAllFiles as webtorrentSaveAllFiles
2120
} from '../webtorrent'
22-
import { getTabData } from '../api/tabs_api'
2321
import { parseTorrentRemote } from '../api/torrent_api'
2422

25-
const focusedWindowChanged = (windowId: number, state: TorrentsState) => {
26-
return { ...state, currentWindowId: windowId }
27-
}
28-
29-
const windowRemoved = (windowId: number, state: TorrentsState) => {
30-
const { activeTabIds } = state
31-
delete activeTabIds[windowId]
32-
return { ...state, activeTabIds }
33-
}
34-
35-
const activeTabChanged = (tabId: number, windowId: number, state: TorrentsState) => {
36-
const { activeTabIds, torrentStateMap } = state
37-
activeTabIds[windowId] = tabId
38-
if (!torrentStateMap[tabId]) {
39-
getTabData(tabId)
40-
}
41-
return { ...state, activeTabIds }
42-
}
43-
44-
const windowCreated = (window: chrome.windows.Window, state: TorrentsState) => {
45-
// update currentWindowId if needed
46-
if (window.focused || Object.keys(state.activeTabIds).length === 0) {
47-
state = focusedWindowChanged(window.id, state)
48-
}
49-
50-
// update its activeTabId
51-
if (window.tabs) {
52-
const tab = window.tabs.find((tab: chrome.tabs.Tab) => tab.active)
53-
if (tab && tab.id) {
54-
state = activeTabChanged(tab.id, window.id, state)
55-
}
56-
}
57-
58-
return state
59-
}
60-
6123
const tabUpdated = (tabId: number, url: string, state: TorrentsState) => {
6224
const { torrentStateMap, torrentObjMap } = state
6325
const origTorrentState: TorrentState = torrentStateMap[tabId]
@@ -125,7 +87,9 @@ const tabRemoved = (tabId: number, state: TorrentsState) => {
12587
if (torrentState) {
12688
const infoHash = torrentState.infoHash
12789
if (infoHash && torrentObjMap[infoHash]) { // unsubscribe
128-
torrentObjMap[infoHash].tabClients.delete(tabId)
90+
if (torrentObjMap[infoHash].tabClients) {
91+
torrentObjMap[infoHash].tabClients.delete(tabId)
92+
}
12993
if (!torrentObjMap[infoHash].tabClients.size) {
13094
delete torrentObjMap[infoHash]
13195
delTorrent(infoHash)
@@ -234,42 +198,13 @@ const saveAllFiles = (state: TorrentsState, infoHash: string) => {
234198
return { ...state }
235199
}
236200

237-
const defaultState: TorrentsState = { currentWindowId: -1, activeTabIds: {}, torrentStateMap: {}, torrentObjMap: {} }
201+
const defaultState: TorrentsState = { torrentStateMap: {}, torrentObjMap: {} }
238202
export const webtorrentReducer = (state: TorrentsState = defaultState, action: any) => { // TODO: modify any to be actual action type
239203
const payload = action.payload
240204
switch (action.type) {
241-
case windowTypes.types.WINDOW_CREATED:
242-
state = windowCreated(payload.window, state)
243-
break
244-
case windowTypes.types.WINDOW_REMOVED:
245-
state = windowRemoved(payload.windowId, state)
246-
break
247-
case windowTypes.types.WINDOW_FOCUS_CHANGED:
248-
state = focusedWindowChanged(payload.windowId, state)
249-
break
250-
case tabTypes.types.ACTIVE_TAB_CHANGED:
251-
if (state.currentWindowId === -1) {
252-
state = focusedWindowChanged(payload.windowId, state)
253-
}
254-
state = activeTabChanged(payload.tabId, payload.windowId, state)
255-
break
256-
case tabTypes.types.TAB_CREATED:
257-
if (payload.tab.id && payload.tab.url) {
258-
state = tabUpdated(payload.tab.id, payload.tab.url, state)
259-
}
260-
break
261205
case tabTypes.types.TAB_UPDATED:
262-
// it's possible to be the first event when browser starts
263-
// initialize currentWindowId and its activeTabId if so
264-
if (state.currentWindowId === -1) {
265-
state = focusedWindowChanged(payload.tab.windowId, state)
266-
}
267-
if (!state.activeTabIds[state.currentWindowId] && payload.tab.active) {
268-
state = activeTabChanged(payload.tab.id, payload.tab.windowId, state)
269-
}
270-
271206
if (payload.changeInfo.url) {
272-
state = tabUpdated(payload.tab.id, payload.changeInfo.url, state)
207+
state = tabUpdated(payload.changeInfo.tabId, payload.changeInfo.url, state)
273208
}
274209
break
275210
case tabTypes.types.TAB_REMOVED:

components/brave_webtorrent/extension/constants/tab_types.ts

-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
export const enum types {
6-
TAB_CREATED = '@@tab/TAB_CREATED',
76
TAB_UPDATED = '@@tab/TAB_UPDATED',
87
TAB_REMOVED = '@@tab/TAB_REMOVED',
9-
ACTIVE_TAB_CHANGED = '@@tab/ACTIVE_TAB_CHANGED',
108
TAB_RETRIEVED = '@@tab/TAB_RETRIEVED'
119
}

components/brave_webtorrent/extension/constants/webtorrentState.ts

-6
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ export interface ApplicationState {
2121
torrentsData: TorrentsState
2222
}
2323

24-
export interface ActiveTabIds {
25-
[key: number]: number // windowId as key
26-
}
27-
2824
export interface TorrentsState {
29-
currentWindowId: number
30-
activeTabIds: ActiveTabIds
3125
torrentStateMap: TorrentStateMap
3226
torrentObjMap: TorrentObjMap
3327
}

components/brave_webtorrent/extension/constants/window_types.ts

-9
This file was deleted.

components/brave_webtorrent/extension/manifest.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
"description": "WebTorrent extension",
66
"background" : {
77
"scripts": ["extension/out/brave_webtorrent_background.bundle.js"],
8-
"persistent": true
8+
"persistent": false
99
},
1010
"web_accessible_resources": [
1111
"extension/brave_webtorrent.html",
1212
"extension/brave_webtorrent2.html"
1313
],
14-
"permissions": ["downloads", "dns", "tabs", "windows", "<all_urls>"],
14+
"permissions": [
15+
"downloads",
16+
"dns",
17+
"tabs",
18+
"webNavigation",
19+
"<all_urls>" // without '<all_urls>' sometimes get CORS issues when fetching the torrent file
20+
],
1521
"sockets": {
1622
"udp": {
1723
"send": "*",

components/test/brave_webtorrent/actions/tab_action_test.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,14 @@ const changeInfo: chrome.tabs.TabChangeInfo = {
2323
}
2424

2525
const tabId: number = 1
26-
const windowId: number = 2
2726

2827
describe('tab_actions', () => {
29-
it('tabCreated', () => {
30-
expect(actions.tabCreated(tab)).toEqual({
31-
type: types.TAB_CREATED,
32-
meta: undefined,
33-
payload: { tab }
34-
})
35-
})
3628

3729
it('tabUpdated', () => {
38-
expect(actions.tabUpdated(tabId, changeInfo, tab)).toEqual({
30+
expect(actions.tabUpdated(tabId, changeInfo)).toEqual({
3931
type: types.TAB_UPDATED,
4032
meta: undefined,
41-
payload: { tabId, changeInfo, tab }
33+
payload: { tabId, changeInfo }
4234
})
4335
})
4436

@@ -50,11 +42,11 @@ describe('tab_actions', () => {
5042
})
5143
})
5244

53-
it('activeTabChanged', () => {
54-
expect(actions.activeTabChanged(tabId, windowId)).toEqual({
55-
type: types.ACTIVE_TAB_CHANGED,
45+
it('tabRetrieved', () => {
46+
expect(actions.tabRetrieved(tab)).toEqual({
47+
type: types.TAB_RETRIEVED,
5648
meta: undefined,
57-
payload: { tabId, windowId }
49+
payload: { tab }
5850
})
5951
})
6052
})

components/test/brave_webtorrent/actions/window_action_test.ts

-42
This file was deleted.

0 commit comments

Comments
 (0)