Skip to content

Commit

Permalink
Pull request #631: AG-24854 Handle browser tab replacement
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/tsurlfilter from fix/AG-24854 to master

Squashed commit of the following:

commit d01142f
Author: Vladimir Zhelvis <v.zhelvis@adguard.com>
Date:   Thu Aug 17 23:27:02 2023 +0300

    update tabs api
  • Loading branch information
zhelvis committed Aug 17, 2023
1 parent a130373 commit 7f81f9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/tswebextension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Blocking third-party requests with `csp_report` content-type.
- Handling discarded tabs replacement on wake up.

### Fixed
- Do not expose JS rules in global page scope
Expand Down
23 changes: 23 additions & 0 deletions packages/tswebextension/src/lib/mv2/background/tabs/tabs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export class TabsApi {

public onActivate = new EventChannel<TabContext>();

public onReplace = new EventChannel<TabContext>();

/**
* Tabs API constructor.
*
Expand All @@ -39,6 +41,7 @@ export class TabsApi {
this.handleTabUpdate = this.handleTabUpdate.bind(this);
this.handleTabActivate = this.handleTabActivate.bind(this);
this.handleTabDelete = this.handleTabDelete.bind(this);
this.handleTabReplace = this.handleTabReplace.bind(this);

this.handleFrameRequest = this.handleFrameRequest.bind(this);
this.handleFrameCosmeticResult = this.handleFrameCosmeticResult.bind(this);
Expand All @@ -59,6 +62,7 @@ export class TabsApi {
browser.tabs.onRemoved.addListener(this.handleTabDelete);
browser.tabs.onUpdated.addListener(this.handleTabUpdate);
browser.tabs.onActivated.addListener(this.handleTabActivate);
browser.tabs.onReplaced.addListener(this.handleTabReplace);

browser.windows.onFocusChanged.addListener(this.onWindowFocusChanged);
}
Expand Down Expand Up @@ -345,6 +349,25 @@ export class TabsApi {
}
}

/**
* The browser tab may be replaced by another when the discarded tab wakes up.
* We handle this case on {@link browser.tabs.onReplaced} event.
* It fires before the tab details are updated,
* so we just move the existing tab context to the new key.
*
* @param addedTabId - Id of the new tab context moved to.
* @param removedTabId - Id of removed tab.
*/
private handleTabReplace(addedTabId: number, removedTabId: number): void {
const tabContext = this.context.get(removedTabId);

if (tabContext) {
this.context.delete(removedTabId);
this.context.set(addedTabId, tabContext);
this.onReplace.dispatch(tabContext);
}
}

/**
* Called when focus state of window changed.
*
Expand Down

0 comments on commit 7f81f9e

Please sign in to comment.