From a609a7104853cc9d9804697a158021bfb01b5e67 Mon Sep 17 00:00:00 2001 From: Andrius Aucinas Date: Mon, 7 Oct 2019 18:50:22 +0100 Subject: [PATCH 1/2] Makes WebTorrent extension non-persistent Fix #6372 Turning extension background to `persistent: false` allows browser to unload it when not in use. To avoid launching the extension on every page activity/tab switch/window switch needed to switch to using `webNavigation` events that can be filtered by URL patterns. Completely removed window event handling as no key functionality appears to depend on it. Removed most of tab events, only leaving `chrome.tabs.onRemoved` to keep torrent cleanup for now. --- .../extension/actions/tab_actions.ts | 13 +- .../extension/actions/window_actions.ts | 20 ---- .../brave_webtorrent/extension/background.ts | 1 - .../background/actions/windowActions.ts | 8 -- .../extension/background/api/tabs_api.ts | 9 -- .../extension/background/events/tabsEvents.ts | 46 +++++-- .../background/events/windowsEvents.ts | 17 --- .../background/reducers/webtorrent_reducer.ts | 76 +----------- .../extension/constants/tab_types.ts | 2 - .../extension/constants/webtorrentState.ts | 6 - .../extension/constants/window_types.ts | 9 -- .../brave_webtorrent/extension/manifest.json | 10 +- .../actions/tab_action_test.ts | 20 +--- .../actions/window_action_test.ts | 42 ------- .../reducers/webtorrent_reducer_test.ts | 112 +++--------------- components/test/brave_webtorrent/testData.ts | 8 +- 16 files changed, 73 insertions(+), 326 deletions(-) delete mode 100644 components/brave_webtorrent/extension/actions/window_actions.ts delete mode 100644 components/brave_webtorrent/extension/background/actions/windowActions.ts delete mode 100644 components/brave_webtorrent/extension/background/api/tabs_api.ts delete mode 100644 components/brave_webtorrent/extension/background/events/windowsEvents.ts delete mode 100644 components/brave_webtorrent/extension/constants/window_types.ts delete mode 100644 components/test/brave_webtorrent/actions/window_action_test.ts diff --git a/components/brave_webtorrent/extension/actions/tab_actions.ts b/components/brave_webtorrent/extension/actions/tab_actions.ts index 16c57c9edeca..0f819787b017 100644 --- a/components/brave_webtorrent/extension/actions/tab_actions.ts +++ b/components/brave_webtorrent/extension/actions/tab_actions.ts @@ -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 }) diff --git a/components/brave_webtorrent/extension/actions/window_actions.ts b/components/brave_webtorrent/extension/actions/window_actions.ts deleted file mode 100644 index b54a4fed7326..000000000000 --- a/components/brave_webtorrent/extension/actions/window_actions.ts +++ /dev/null @@ -1,20 +0,0 @@ -/* 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 -}) diff --git a/components/brave_webtorrent/extension/background.ts b/components/brave_webtorrent/extension/background.ts index 1a8125cb5fb6..ee7d14a07fa3 100644 --- a/components/brave_webtorrent/extension/background.ts +++ b/components/brave_webtorrent/extension/background.ts @@ -5,5 +5,4 @@ import './background/events/webtorrentEvents' import './background/events/torrentEvents' import './background/events/tabsEvents' -import './background/events/windowsEvents' import './background/store' diff --git a/components/brave_webtorrent/extension/background/actions/windowActions.ts b/components/brave_webtorrent/extension/background/actions/windowActions.ts deleted file mode 100644 index 948d7c928798..000000000000 --- a/components/brave_webtorrent/extension/background/actions/windowActions.ts +++ /dev/null @@ -1,8 +0,0 @@ -/* 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) diff --git a/components/brave_webtorrent/extension/background/api/tabs_api.ts b/components/brave_webtorrent/extension/background/api/tabs_api.ts deleted file mode 100644 index f5cba1a94837..000000000000 --- a/components/brave_webtorrent/extension/background/api/tabs_api.ts +++ /dev/null @@ -1,9 +0,0 @@ -/* 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/. */ - -export const getTabData = (tabId: number) => - chrome.tabs.get(tabId, (tab: chrome.tabs.Tab) => { - const tabActions = require('../actions/tabActions').default - tabActions.tabRetrieved(tab) - }) diff --git a/components/brave_webtorrent/extension/background/events/tabsEvents.ts b/components/brave_webtorrent/extension/background/events/tabsEvents.ts index e6c07b834112..65c5bfea7209 100644 --- a/components/brave_webtorrent/extension/background/events/tabsEvents.ts +++ b/components/brave_webtorrent/extension/background/events/tabsEvents.ts @@ -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) + } +) diff --git a/components/brave_webtorrent/extension/background/events/windowsEvents.ts b/components/brave_webtorrent/extension/background/events/windowsEvents.ts deleted file mode 100644 index 9a8a328f390c..000000000000 --- a/components/brave_webtorrent/extension/background/events/windowsEvents.ts +++ /dev/null @@ -1,17 +0,0 @@ -/* 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) -}) diff --git a/components/brave_webtorrent/extension/background/reducers/webtorrent_reducer.ts b/components/brave_webtorrent/extension/background/reducers/webtorrent_reducer.ts index 437932641bed..a25164dfb9cd 100644 --- a/components/brave_webtorrent/extension/background/reducers/webtorrent_reducer.ts +++ b/components/brave_webtorrent/extension/background/reducers/webtorrent_reducer.ts @@ -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' @@ -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] @@ -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) @@ -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: @@ -304,5 +239,6 @@ export const webtorrentReducer = (state: TorrentsState = defaultState, action: a break } + // console.error("Got state", JSON.stringify(state)); return state } diff --git a/components/brave_webtorrent/extension/constants/tab_types.ts b/components/brave_webtorrent/extension/constants/tab_types.ts index b652307370f5..f26a8c4561ff 100644 --- a/components/brave_webtorrent/extension/constants/tab_types.ts +++ b/components/brave_webtorrent/extension/constants/tab_types.ts @@ -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' } diff --git a/components/brave_webtorrent/extension/constants/webtorrentState.ts b/components/brave_webtorrent/extension/constants/webtorrentState.ts index 88ba575fe724..37ef1ab6175f 100644 --- a/components/brave_webtorrent/extension/constants/webtorrentState.ts +++ b/components/brave_webtorrent/extension/constants/webtorrentState.ts @@ -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 } diff --git a/components/brave_webtorrent/extension/constants/window_types.ts b/components/brave_webtorrent/extension/constants/window_types.ts deleted file mode 100644 index 904504e6c3a7..000000000000 --- a/components/brave_webtorrent/extension/constants/window_types.ts +++ /dev/null @@ -1,9 +0,0 @@ -/* 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/. */ - -export const enum types { - WINDOW_CREATED = '@@window/WINDOW_CREATED', - WINDOW_REMOVED = '@@window/WINDOW_REMOVED', - WINDOW_FOCUS_CHANGED = '@@window/WINDOW_FOCUS_CHANGED' -} diff --git a/components/brave_webtorrent/extension/manifest.json b/components/brave_webtorrent/extension/manifest.json index a179f1ee1f35..623203a7b2ee 100644 --- a/components/brave_webtorrent/extension/manifest.json +++ b/components/brave_webtorrent/extension/manifest.json @@ -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", ""], + "permissions": [ + "downloads", + "dns", + "tabs", + "webNavigation", + "" // without '' sometimes get CORS issues when fetching the torrent file + ], "sockets": { "udp": { "send": "*", diff --git a/components/test/brave_webtorrent/actions/tab_action_test.ts b/components/test/brave_webtorrent/actions/tab_action_test.ts index 3f71cd6b0796..8488a3c8d667 100644 --- a/components/test/brave_webtorrent/actions/tab_action_test.ts +++ b/components/test/brave_webtorrent/actions/tab_action_test.ts @@ -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 } }) }) @@ -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 } }) }) }) diff --git a/components/test/brave_webtorrent/actions/window_action_test.ts b/components/test/brave_webtorrent/actions/window_action_test.ts deleted file mode 100644 index 903dab9fc8e8..000000000000 --- a/components/test/brave_webtorrent/actions/window_action_test.ts +++ /dev/null @@ -1,42 +0,0 @@ -/* 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 { types } from '../../../brave_webtorrent/extension/constants/window_types' -import * as actions from '../../../brave_webtorrent/extension/actions/window_actions' - -describe('window_actions', () => { - it('windowCreated', () => { - const window: chrome.windows.Window = { - id: 1, - state: 'normal', - focused: true, - alwaysOnTop: false, - incognito: false, - type: 'normal' - } - expect(actions.windowCreated(window)).toEqual({ - type: types.WINDOW_CREATED, - meta: undefined, - payload: { window } - }) - }) - - const windowId = 7 - - it('windowRemoved', () => { - expect(actions.windowRemoved(windowId)).toEqual({ - type: types.WINDOW_REMOVED, - meta: undefined, - payload: { windowId } - }) - }) - - it('windowFocusChanged', () => { - expect(actions.windowFocusChanged(windowId)).toEqual({ - type: types.WINDOW_FOCUS_CHANGED, - meta: undefined, - payload: { windowId } - }) - }) -}) diff --git a/components/test/brave_webtorrent/background/reducers/webtorrent_reducer_test.ts b/components/test/brave_webtorrent/background/reducers/webtorrent_reducer_test.ts index def0d9aee817..5d382cd9681b 100644 --- a/components/test/brave_webtorrent/background/reducers/webtorrent_reducer_test.ts +++ b/components/test/brave_webtorrent/background/reducers/webtorrent_reducer_test.ts @@ -4,80 +4,18 @@ import { webtorrentReducer } from '../../../../brave_webtorrent/extension/background/reducers/webtorrent_reducer' import * as tabActions from '../../../../brave_webtorrent/extension/actions/tab_actions' -import * as windowActions from '../../../../brave_webtorrent/extension/actions/window_actions' -import { torrentsState } from '../../testData' +import { torrentObjMap } from '../../testData' // this import seems to trigger createStore and get an undefined reducer jest.mock('../../../../brave_webtorrent/extension/background/events/torrentEvents') -const defaultState = { currentWindowId: -1, activeTabIds: {}, - torrentStateMap: {}, torrentObjMap: {} } +const defaultState = { torrentStateMap: {}, torrentObjMap: {} } describe('webtorrent reducer test', () => { it('handle the initial state', () => { const state = webtorrentReducer(undefined, tabActions.tabRemoved(1)) expect(state).toEqual(defaultState) }) - describe('WINDOW_CREATED', () => { - it('sets currentWindowId if it is the first window', () => { - const window: chrome.windows.Window = { - state: 'normal', - type: 'normal', - id: 1, - focused: false, - incognito: false, - alwaysOnTop: false - } - const state = webtorrentReducer(defaultState, - windowActions.windowCreated(window)) - expect(state).toEqual({ ...defaultState, currentWindowId: 1 }) - }) - - it('sets currentWindowId if it is focused', () => { - const window: chrome.windows.Window = { - state: 'normal', - type: 'normal', - id: 2, - focused: true, - incognito: false, - alwaysOnTop: false - } - const state = webtorrentReducer(torrentsState, - windowActions.windowCreated(window)) - expect(state).toEqual({ ...torrentsState, currentWindowId: 2 }) - }) - - it('do not set currentWindowId if either focused or it is the first window', () => { - const window: chrome.windows.Window = { - state: 'normal', - type: 'normal', - id: 1, - focused: false, - incognito: false, - alwaysOnTop: false - } - const state = webtorrentReducer(torrentsState, - windowActions.windowCreated(window)) - expect(state).toEqual({ ...torrentsState, currentWindowId: 0 }) - }) - }) - - describe('WINDOW_REMOVED', () => { - it('entry from activeTabIds should be deleted when window is removed', () => { - const state = webtorrentReducer(torrentsState, - windowActions.windowRemoved(0)) - expect(state.activeTabIds).toEqual({ 1: 1 }) - }) - }) - - describe('WINDOW_FOCUS_CHANGED', () => { - it('currentWindowID should be updated when window focus changed', () => { - const state = webtorrentReducer(torrentsState, - windowActions.windowFocusChanged(1)) - expect(state.currentWindowId).toEqual(1) - }) - }) - const tab: chrome.tabs.Tab = { discarded: false, autoDiscardable: false, @@ -90,44 +28,26 @@ describe('webtorrent reducer test', () => { incognito: false, selected: false } - describe('TAB_CREATED', () => { - it('state is unchanged if tab url is not ready', () => { - const state = webtorrentReducer(torrentsState, - tabActions.tabCreated(tab)) - expect(state).toEqual(state) - }) - // TODO: mock ParseTorrent to test tab url case - }) - const changeInfo = {} describe('TAB_UPDATED', () => { - it('state is unchanged if tab url is not ready', () => { - if (tab && tab.id) { - const state = webtorrentReducer(torrentsState, - tabActions.tabUpdated(tab.id, changeInfo, tab)) - expect(state).toEqual(state) - } - }) + // TODO: mock ParseTorrent to test tab url case + }) - it('update currentWindowID if it is not initialized yet', () => { - if (tab && tab.id) { - const stateWithoutWindowId = { ...torrentsState, currentWindowId: -1 } - const state = webtorrentReducer(stateWithoutWindowId, - tabActions.tabUpdated(tab.id, changeInfo, tab)) - expect(state).toEqual({ ...stateWithoutWindowId, currentWindowId: 0 }) + describe('TAB_REMOVED', () => { + it('removed tab state is removed', () => { + const setupTorrentsState: TorrentsState = { + torrentStateMap: { + 0: { + tabId: 0, torrentId: 'testTorrentId', infoHash: 'infoHash' + } + }, + torrentObjMap } - }) - it('update activeTabIds if it is not initialized yet for this window', () => { - const stateWithNoActiveTabIds = { ...torrentsState, activeTabIds: {} } - const activeTab = { ...tab, active: true } - if (activeTab && activeTab.id) { - const state = webtorrentReducer(stateWithNoActiveTabIds, - tabActions.tabUpdated(activeTab.id, changeInfo, activeTab)) - expect(state).toEqual({ ...torrentsState, activeTabIds: { 0: 0 } }) - } + const state = webtorrentReducer(setupTorrentsState, + tabActions.tabRemoved(tab.id)) + expect(state).toEqual(defaultState) }) - // TODO: mock ParseTorrent to test tab url case }) }) diff --git a/components/test/brave_webtorrent/testData.ts b/components/test/brave_webtorrent/testData.ts index 60e1cf4edd88..7e6698d9dce0 100644 --- a/components/test/brave_webtorrent/testData.ts +++ b/components/test/brave_webtorrent/testData.ts @@ -2,11 +2,7 @@ * 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 { ActiveTabIds, ApplicationState, TorrentObj, TorrentState, TorrentStateMap, TorrentObjMap, TorrentsState } from '../../brave_webtorrent/extension/constants/webtorrentState' - -export const currentWindowId: number = 0 - -export const activeTabIds: ActiveTabIds = { 0: 0, 1: 1 } +import { ApplicationState, TorrentObj, TorrentState, TorrentStateMap, TorrentObjMap, TorrentsState } from '../../brave_webtorrent/extension/constants/webtorrentState' export const torrentState: TorrentState = { tabId: 0, torrentId: 'testTorrentId' @@ -35,8 +31,6 @@ export const torrentObjMap: TorrentObjMap = { } export const torrentsState: TorrentsState = { - activeTabIds, - currentWindowId, torrentStateMap, torrentObjMap } From 7da959f40626022c2b96dde92c08e5b1a7f0f68c Mon Sep 17 00:00:00 2001 From: Andrius Date: Tue, 22 Oct 2019 19:40:23 +0100 Subject: [PATCH 2/2] removes leftover console log --- .../extension/background/reducers/webtorrent_reducer.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/components/brave_webtorrent/extension/background/reducers/webtorrent_reducer.ts b/components/brave_webtorrent/extension/background/reducers/webtorrent_reducer.ts index a25164dfb9cd..1f1998254286 100644 --- a/components/brave_webtorrent/extension/background/reducers/webtorrent_reducer.ts +++ b/components/brave_webtorrent/extension/background/reducers/webtorrent_reducer.ts @@ -239,6 +239,5 @@ export const webtorrentReducer = (state: TorrentsState = defaultState, action: a break } - // console.error("Got state", JSON.stringify(state)); return state }