clients icon indicating copy to clipboard operation
clients copied to clipboard

[PM-5880] Refactor browser platform utils service to remove `window` references

Open cagonzalezcs opened this issue 1 year ago • 2 comments
trafficstars

Type of change

- [ ] Bug fix
- [ ] New feature development
- [x] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other

Objective

The goal for this ticket is to remove a direct dependency on the window object within the BrowserPlatformUtilsService. To do this, we are refactoring the typing information within the service, adjusting how the service is instantiated to remove dependence on the window object, and reworking the clipboard related methods to conditionally use the chrome.offscreen API when we the extension is built using manifest v3.

Code changes

  • apps/browser/src/autofill/background/overlay.background.spec.ts: Removing a reference to the window object when handling a copy to clipboard action
  • apps/browser/src/autofill/background/overlay.background.ts: Removing a reference to the window object when handling a copy to clipboard action
  • apps/browser/src/autofill/background/web-request.background.ts: Updating how we identify if the manifest version is mv2
  • apps/browser/src/background/commands.background.ts: Removing a reference to the window object when handling a copy to clipboard action
  • apps/browser/src/background/main.background.ts: Updating various element to change how we identify if the manifest version is for mv2 or mv3 and removing references to the window object that directly affect the BrowserPlatformUtilsService class.
  • apps/browser/src/background/runtime.background.ts: Removing a reference to the window object when handling a copy to clipboard action
  • apps/browser/src/manifest.v3.json: Adding the offscreen API to the manifest
  • apps/browser/src/platform/alarms/alarm-state.ts: Updating how we identify if the manifest version is mv3
  • apps/browser/src/platform/background.ts: Updating how we identify if the manifest version is mv3
  • apps/browser/src/platform/background/service-factories/storage-service.factory.ts: Updating how we identify if the manifest version if mv3
  • apps/browser/src/platform/browser/browser-api.spec.ts: Adding Jest tests to validate the changes within the BrowserApi class.
  • apps/browser/src/platform/browser/browser-api.ts: Implementing an isManifestVersion method that simplifies checking for mv3, as well as methods for creating and closing offscreen documents as needed.
  • apps/browser/src/platform/decorators/session-sync-observable/session-syncer.ts: Updating how we identify if the manifest version is mv3
  • apps/browser/src/platform/globals.d.ts: Adding a custom declaration to indicate potentially expected values in for the ServiceWorkerGlobalScope. This likely should be updated at some point, but scoping down the expected service worker scope to specific files, especially when they might switch between mv2 and mv3, has proven difficult.
  • apps/browser/src/platform/offscreen-document/abstractions/offscreen-document.ts: Adding typing information for the script used within the offscreen document
  • apps/browser/src/platform/offscreen-document/index.html: The markup for the offscreen document
  • apps/browser/src/platform/offscreen-document/offscreen-document.spec.ts: Jest tests validating the currently established behavior within the offscreen document script
  • apps/browser/src/platform/offscreen-document/offscreen-document.ts: Implemented behavior that facilitates the ability to trigger the clipboard copy to and read from actions from the offscreen document.
  • apps/browser/src/platform/popup/browser-popup-utils.ts: Updating how we identify if the manifest version is mv3
  • apps/browser/src/platform/services/browser-clipboard.service.spec.ts: Jest tests for the BrowserClipboardService class
  • apps/browser/src/platform/services/browser-clipboard.service.ts: Refactored implementation for the browser clipboard behavior out of the BrowserPlatformUtilsService class. The two public methods copy and read first attempt to enact their behavior using the Clipboard API, and on failure will fallback to legacy methods. This allows us to have the same behavior within the BrowserPlatformUtilsService class as well as the offscreen document.
  • apps/browser/src/platform/services/browser-platform-utils.service.spec.ts: Jest tests to validate the changes made to the BrowserPlatformUtilsService class
  • apps/browser/src/platform/services/browser-platform-utils.service.ts: Worked through re-typing the usage of window objects throughout the script, and reworked the copy/read from clipboard functionality.
  • apps/browser/test.setup.ts: Adding chrome extension api scaffolding for the test runner
  • apps/browser/webpack.config.js: Adding a conditional build of the offscreen document script and html if we are building the extension for manifest v3.

cagonzalezcs avatar Feb 08 '24 22:02 cagonzalezcs

Codecov Report

Attention: Patch coverage is 85.60606% with 19 lines in your changes are missing coverage. Please review.

Project coverage is 25.31%. Comparing base (940fd21) to head (44c9fc3).

Files Patch % Lines
apps/browser/src/background/main.background.ts 0.00% 6 Missing :warning:
apps/browser/src/background/runtime.background.ts 0.00% 3 Missing :warning:
.../platform/offscreen-document/offscreen-document.ts 88.00% 2 Missing and 1 partial :warning:
apps/browser/src/platform/alarms/alarm-state.ts 0.00% 2 Missing :warning:
.../src/autofill/background/web-request.background.ts 0.00% 1 Missing :warning:
apps/browser/src/background/commands.background.ts 0.00% 1 Missing :warning:
apps/browser/src/platform/background.ts 0.00% 1 Missing :warning:
...round/service-factories/storage-service.factory.ts 0.00% 1 Missing :warning:
...latform/services/browser-platform-utils.service.ts 97.29% 0 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7885      +/-   ##
==========================================
+ Coverage   25.15%   25.31%   +0.15%     
==========================================
  Files        2252     2254       +2     
  Lines       65846    65894      +48     
  Branches    12414    12415       +1     
==========================================
+ Hits        16566    16679     +113     
+ Misses      47938    47870      -68     
- Partials     1342     1345       +3     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Feb 08 '24 23:02 codecov[bot]

Logo Checkmarx One – Scan Summary & Detailse867bff9-5a2c-4379-9c1e-96d23fe3ae6e

New Issues

Severity Issue Source File / Package Checkmarx Insight
MEDIUM Client_Privacy_Violation /apps/browser/src/background/runtime.background.ts: 317 Attack Vector

Fixed Issues

Severity Issue Source File / Package
MEDIUM Client_Privacy_Violation /apps/browser/src/background/runtime.background.ts: 317

bitwarden-bot avatar Feb 09 '24 02:02 bitwarden-bot