Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Conversation

cagonzalezcs
Copy link
Contributor

@cagonzalezcs cagonzalezcs commented Feb 8, 2024

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.

@github-actions github-actions bot added the needs-qa Marks a PR as requiring QA approval label Feb 8, 2024
Copy link

codecov bot commented Feb 8, 2024

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 ⚠️
apps/browser/src/background/runtime.background.ts 0.00% 3 Missing ⚠️
.../platform/offscreen-document/offscreen-document.ts 88.00% 2 Missing and 1 partial ⚠️
apps/browser/src/platform/alarms/alarm-state.ts 0.00% 2 Missing ⚠️
.../src/autofill/background/web-request.background.ts 0.00% 1 Missing ⚠️
apps/browser/src/background/commands.background.ts 0.00% 1 Missing ⚠️
apps/browser/src/platform/background.ts 0.00% 1 Missing ⚠️
...round/service-factories/storage-service.factory.ts 0.00% 1 Missing ⚠️
...latform/services/browser-platform-utils.service.ts 97.29% 0 Missing and 1 partial ⚠️
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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bitwarden-bot
Copy link

bitwarden-bot commented Feb 9, 2024

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

cagonzalezcs and others added 24 commits February 9, 2024 13:12
@cagonzalezcs cagonzalezcs marked this pull request as ready for review February 15, 2024 16:27
dani-garcia
dani-garcia previously approved these changes Feb 28, 2024
*
* @param expectedVersion - The expected manifest version to check against.
*/
static isManifestVersion(expectedVersion: 2 | 3) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking naming nit (the nittiest) - don't bother if you don't need to make other changes, tho

Suggested change
static isManifestVersion(expectedVersion: 2 | 3) {
static isManifestVersion(expectedManifestVersion: 2 | 3) {

*
* @param expectedVersion - The expected manifest version to check against.
*/
static isManifestVersion(expectedVersion: 2 | 3) {
Copy link
Contributor

@jprusik jprusik Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking nit/question:

I'm guessing no, but Is there a scenario where either of these two values in the comparison are not 2 or 3 (say, undefined)? Should we defend against that scenario?

Another way I'm thinking about it; any reason to replace BrowserApi.manifestVersion === 3 with this method other than (the slightly improved) conciseness?

jprusik
jprusik previously approved these changes Feb 28, 2024
dani-garcia
dani-garcia previously approved these changes Mar 5, 2024
@cagonzalezcs cagonzalezcs removed the needs-qa Marks a PR as requiring QA approval label Mar 6, 2024
@cagonzalezcs cagonzalezcs enabled auto-merge (squash) March 6, 2024 16:33
@cagonzalezcs cagonzalezcs merged commit 51f482d into main Mar 6, 2024
22 of 24 checks passed
@cagonzalezcs cagonzalezcs deleted the autofill/pm-5880-refactor-browser-platform-utils-service-to-remove-window-reference branch March 6, 2024 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants