Skip to content

Commit

Permalink
[PM-1900] Run Process Reload on Firefox (#10149)
Browse files Browse the repository at this point in the history
* Remove Firefox Exemption

* Do Not Open Sidebar On Install

* Run `chrome.runtime.reload` on All Browsers

* Update Docs & Test Name

* Should Probably Call The Sut In Test

* Update Doc Comment

* Update apps/browser/src/platform/browser/browser-api.ts

Co-authored-by: Daniel James Smith <[email protected]>

---------

Co-authored-by: Daniel James Smith <[email protected]>
  • Loading branch information
justindbaur and djsmith85 authored Aug 28, 2024
1 parent 00bdfa1 commit 37523ca
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 49 deletions.
6 changes: 1 addition & 5 deletions apps/browser/src/background/main.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,12 +1028,8 @@ export default class MainBackground {
);

const systemUtilsServiceReloadCallback = async () => {
const forceWindowReload =
this.platformUtilsService.isSafari() ||
this.platformUtilsService.isFirefox() ||
this.platformUtilsService.isOpera();
await this.taskSchedulerService.clearAllScheduledTasks();
BrowserApi.reloadExtension(forceWindowReload ? self : null);
BrowserApi.reloadExtension();
};

this.systemService = new SystemService(
Expand Down
2 changes: 1 addition & 1 deletion apps/browser/src/background/nativeMessaging.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class NativeMessagingBackground {
// Reload extension to activate nativeMessaging
chrome.permissions.onAdded.addListener((permissions) => {
if (permissions.permissions?.includes("nativeMessaging")) {
BrowserApi.reloadExtension(null);
BrowserApi.reloadExtension();
}
});
}
Expand Down
1 change: 1 addition & 0 deletions apps/browser/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"default_title": "Bitwarden",
"default_panel": "popup/index.html?uilocation=sidebar",
"default_icon": "images/icon19.png",
"open_at_install": false,
"browser_style": false
},
"storage": {
Expand Down
3 changes: 2 additions & 1 deletion apps/browser/src/manifest.v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
"sidebar_action": {
"default_title": "Bitwarden",
"default_panel": "popup/index.html?uilocation=sidebar",
"default_icon": "images/icon19.png"
"default_icon": "images/icon19.png",
"open_at_install": false
},
"storage": {
"managed_schema": "managed_schema.json"
Expand Down
22 changes: 2 additions & 20 deletions apps/browser/src/platform/browser/browser-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,26 +276,8 @@ describe("BrowserApi", () => {
});

describe("reloadExtension", () => {
it("reloads the window location if the passed globalContext is for the window", () => {
const windowMock = mock<Window>({
location: { reload: jest.fn() },
}) as unknown as Window & typeof globalThis;

BrowserApi.reloadExtension(windowMock);

expect(windowMock.location.reload).toHaveBeenCalled();
});

it("reloads the extension runtime if the passed globalContext is not for the window", () => {
const globalMock = mock<typeof globalThis>({}) as any;
BrowserApi.reloadExtension(globalMock);

expect(chrome.runtime.reload).toHaveBeenCalled();
});

it("reloads the extension runtime if a null value is passed as the globalContext", () => {
BrowserApi.reloadExtension(null);

it("forwards call to extension runtime", () => {
BrowserApi.reloadExtension();
expect(chrome.runtime.reload).toHaveBeenCalled();
});
});
Expand Down
13 changes: 2 additions & 11 deletions apps/browser/src/platform/browser/browser-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,18 +412,9 @@ export class BrowserApi {
}

/**
* Handles reloading the extension, either by calling the window location
* to reload or by calling the extension's runtime to reload.
*
* @param globalContext - The global context to use for the reload.
* Handles reloading the extension using the underlying functionality exposed by the browser API.
*/
static reloadExtension(globalContext: (Window & typeof globalThis) | null) {
// The passed globalContext might be a ServiceWorkerGlobalScope, as a result
// we need to check if the location object exists before calling reload on it.
if (typeof globalContext?.location?.reload === "function") {
return (globalContext as any).location.reload(true);
}

static reloadExtension() {
return chrome.runtime.reload();
}

Expand Down
11 changes: 0 additions & 11 deletions apps/browser/src/popup/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
ToastService,
} from "@bitwarden/components";

import { BrowserApi } from "../platform/browser/browser-api";
import { PopupViewCacheService } from "../platform/popup/view-cache/popup-view-cache.service";
import { initPopupClosedListener } from "../platform/services/popup-view-cache-background.service";
import { BrowserSendStateService } from "../tools/popup/services/browser-send-state.service";
Expand Down Expand Up @@ -128,16 +127,6 @@ export class AppComponent implements OnInit, OnDestroy {
this.showNativeMessagingFingerprintDialog(msg);
} else if (msg.command === "showToast") {
this.toastService._showToast(msg);
} else if (msg.command === "reloadProcess") {
const forceWindowReload =
this.platformUtilsService.isSafari() ||
this.platformUtilsService.isFirefox() ||
this.platformUtilsService.isOpera();
// Wait to make sure background has reloaded first.
window.setTimeout(
() => BrowserApi.reloadExtension(forceWindowReload ? window : null),
2000,
);
} else if (msg.command === "reloadPopup") {
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down

0 comments on commit 37523ca

Please sign in to comment.