-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track hidden tabs using session data
Whenever we hide a tab, tag it with a marker in the browser session data so we can tell it was Tab Stash that hid it. This allows us to more accurately identify tabs that need to be cleaned up later. We were previously cleaning up hidden tabs by looking only at what URLs were removed from the stash and closing the corresponding tabs. However, that doesn't always work, because the tab's URL could spontaneously change (e.g. a user getting logged out), and then we wouldn't think it belonged to us anymore. Now, if we see a tab with the Tab Stash marker but with a URL that isn't stashed, we know it's one of ours and we can close it, regardless of why the URL is unrecognized. Closes #425.
- Loading branch information
1 parent
b48dadf
commit fd7f93e
Showing
10 changed files
with
323 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import bookmarks from "./bookmarks"; | ||
import containers from "./containers"; | ||
import runtime from "./runtime"; | ||
import sessions from "./sessions"; | ||
import storage from "./storage"; | ||
import tabs_and_windows from "./tabs-and-windows"; | ||
|
||
export {bookmarks, containers, runtime, storage, tabs_and_windows}; | ||
export {bookmarks, containers, runtime, sessions, storage, tabs_and_windows}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import type {Sessions, Tabs as T, Windows as W} from "webextension-polyfill"; | ||
|
||
import * as events from "../events"; | ||
import type {State} from "./tabs-and-windows"; | ||
import tabs_and_windows from "./tabs-and-windows"; | ||
|
||
type Metadata = Map<string, any>; | ||
|
||
class MockSessions implements Sessions.Static { | ||
readonly MAX_SESSION_RESULTS = 25; | ||
|
||
readonly onChanged: events.MockEvent<() => void> = new events.MockEvent( | ||
"browser.sessions.onChanged", | ||
); | ||
|
||
private readonly _state: State; | ||
private readonly _windows = new WeakMap<W.Window, Metadata>(); | ||
private readonly _tabs = new WeakMap<T.Tab, Metadata>(); | ||
|
||
constructor(state: State) { | ||
this._state = state; | ||
} | ||
|
||
forgetClosedTab(windowId: number, sessionId: string): Promise<void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
forgetClosedWindow(sessionId: string): Promise<void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
getRecentlyClosed( | ||
filter?: Sessions.Filter | undefined, | ||
): Promise<Sessions.Session[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
restore(sessionId?: string | undefined): Promise<Sessions.Session> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
async setTabValue(tabId: number, key: string, value: any): Promise<void> { | ||
const md = this._tab(tabId); | ||
md.set(key, JSON.stringify(value)); | ||
} | ||
async getTabValue(tabId: number, key: string): Promise<any> { | ||
const md = this._tab(tabId); | ||
const val = md.get(key); | ||
if (val === undefined) return undefined; | ||
return JSON.parse(val); | ||
} | ||
async removeTabValue(tabId: number, key: string): Promise<void> { | ||
const md = this._tab(tabId); | ||
md.delete(key); | ||
} | ||
|
||
async setWindowValue( | ||
windowId: number, | ||
key: string, | ||
value: any, | ||
): Promise<void> { | ||
const md = this._window(windowId); | ||
md.set(key, JSON.stringify(value)); | ||
} | ||
async getWindowValue(windowId: number, key: string): Promise<any> { | ||
const md = this._window(windowId); | ||
const val = md.get(key); | ||
if (val === undefined) return undefined; | ||
return JSON.parse(val); | ||
} | ||
async removeWindowValue(windowId: number, key: string): Promise<void> { | ||
const md = this._window(windowId); | ||
md.delete(key); | ||
} | ||
|
||
private _window(id: number): Metadata { | ||
const win = this._state.win(id); | ||
let md = this._windows.get(win); | ||
if (!md) { | ||
md = new Map(); | ||
this._windows.set(win, md); | ||
} | ||
return md; | ||
} | ||
|
||
private _tab(id: number): Metadata { | ||
const tab = this._state.tab(id); | ||
let md = this._tabs.get(tab); | ||
if (!md) { | ||
md = new Map(); | ||
this._tabs.set(tab, md); | ||
} | ||
return md; | ||
} | ||
} | ||
|
||
export default (() => { | ||
const exports = { | ||
sessions: new MockSessions(tabs_and_windows.state), | ||
reset() { | ||
exports.sessions = new MockSessions(tabs_and_windows.state); | ||
(<any>globalThis).browser.sessions = exports.sessions; | ||
}, | ||
}; | ||
|
||
exports.reset(); | ||
|
||
return exports; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.