Skip to content

Commit

Permalink
[FB] Workspaces | Add integrity checks to stabilize workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
surapunoyousei committed Aug 22, 2024
1 parent e60b572 commit 6b7b921
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 38 deletions.
85 changes: 51 additions & 34 deletions browser/base/content/browser-workspaces.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var BrowserOpenTab;

/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */

import { WorkspacesMigratorUtils } from "./modules/workspaces/WorkspacesMigratorUtils.mjs";
Expand Down Expand Up @@ -343,8 +341,27 @@ export const gWorkspaces = {
},

/* get Workspaces information */
getCurrentWindowId() {
async getCurrentWindowId() {
let windowId = window.workspacesWindowId;

if (windowId !== null) {
const windowIntegrityResult =
await WorkspacesWindowIdUtils.checkWindowIntegrity(
windowId,
window.gBrowser.tabs
);

if (!windowIntegrityResult) {
windowId = await WorkspacesWindowIdUtils.getWindowIdByInference(
window.gBrowser.tabs
);

if (windowId !== null) {
window.workspacesWindowId = windowId;
}
}
}

if (windowId == null) {
windowId = WorkspacesWindowUuidService.getGeneratedUuid();
window.workspacesWindowId = windowId;
Expand All @@ -354,7 +371,7 @@ export const gWorkspaces = {
},

async getCurrentWorkspace() {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let workspaceId = await WorkspacesWindowIdUtils.getSelectedWorkspaceId(
windowId
);
Expand Down Expand Up @@ -384,15 +401,15 @@ export const gWorkspaces = {
},

async getCurrentWorkspacesData() {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let workspacesData = await WorkspacesWindowIdUtils.getWindowWorkspacesData(
windowId
);
return workspacesData;
},

async getCurrentWorkspacesDataWithoutPreferences() {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let workspacesData =
await WorkspacesWindowIdUtils.getWindowWorkspacesDataWithoutPreferences(
windowId
Expand All @@ -401,14 +418,14 @@ export const gWorkspaces = {
},

async getCurrentWorkspacesCount() {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let workspacesCount =
await WorkspacesWindowIdUtils.getWindowWorkspacesCount(windowId);
return workspacesCount;
},

async getDefaultWorkspace() {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let defaultWorkspaceId =
await WorkspacesWindowIdUtils.getDefaultWorkspaceId(windowId);
let defaultWorkspace = await WorkspacesIdUtils.getWorkspaceByIdAndWindowId(
Expand All @@ -419,14 +436,14 @@ export const gWorkspaces = {
},

async getDefaultWorkspaceId() {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let defaultWorkspaceId =
await WorkspacesWindowIdUtils.getDefaultWorkspaceId(windowId);
return defaultWorkspaceId;
},

async getAllWorkspacesBlockElements() {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let result = await WorkspacesElementService.getAllWorkspacesBlockElements(
windowId,
this._workspaceManageOnBMSMode
Expand All @@ -435,7 +452,7 @@ export const gWorkspaces = {
},

async getWorkspaceBlockElement(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let result = await WorkspacesElementService.getWorkspaceBlockElement(
workspaceId,
windowId,
Expand All @@ -445,7 +462,7 @@ export const gWorkspaces = {
},

async getWorkspaceById(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let result = await WorkspacesIdUtils.getWorkspaceByIdAndWindowId(
workspaceId,
windowId
Expand All @@ -454,7 +471,7 @@ export const gWorkspaces = {
},

async getAllWorkspacesId() {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let allWorkspacesId = await WorkspacesWindowIdUtils.getAllWorkspacesId(
windowId
);
Expand All @@ -464,20 +481,20 @@ export const gWorkspaces = {

/* Workspaces saver */
async saveWorkspacesDataWithoutOverwritingPreferences(workspacesData) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesDataSaver.saveWorkspacesDataWithoutOverwritingPreferences(
workspacesData,
windowId
);
},

async saveWorkspaceData(workspaceData) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesDataSaver.saveWorkspaceData(workspaceData, windowId);
},

async saveWindowPreferences(preferences) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesDataSaver.saveWindowPreferences(preferences, windowId);
},

Expand All @@ -501,13 +518,13 @@ export const gWorkspaces = {
},

async removeWorkspaceById(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesIdUtils.removeWorkspaceById(workspaceId, windowId);
this.removeWorkspaceTabs(workspaceId);
},

async removeWindowWorkspacesDataById() {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesWindowIdUtils.removeWindowWorkspacesDataById(windowId);
},

Expand All @@ -520,7 +537,7 @@ export const gWorkspaces = {
icon,
currentTabMigration = false
) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let createdWorkspaceId = await WorkspacesService.createWorkspace(
workspaceName,
windowId,
Expand Down Expand Up @@ -551,7 +568,7 @@ export const gWorkspaces = {
},

async deleteWorkspace(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let currentWorkspaceId = await this.getCurrentWorkspaceId();
await WorkspacesService.deleteWorkspace(workspaceId, windowId);
this.removeWorkspaceTabs(workspaceId);
Expand All @@ -565,12 +582,12 @@ export const gWorkspaces = {
},

async renameWorkspace(workspaceId, newName) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesService.renameWorkspace(workspaceId, newName, windowId);
},

async setDefaultWorkspace(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesService.setDefaultWorkspace(workspaceId, windowId);

// rebuild the workspacesToolbar
Expand All @@ -587,7 +604,7 @@ export const gWorkspaces = {
},

async checkAllWorkspacesHasTab() {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let allWorkspacesId = await WorkspacesWindowIdUtils.getAllWorkspacesId(
windowId
);
Expand Down Expand Up @@ -698,7 +715,7 @@ export const gWorkspaces = {
},

async workspaceIdExists(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let result = await WorkspacesIdUtils.workspaceIdExists(
workspaceId,
windowId
Expand All @@ -707,7 +724,7 @@ export const gWorkspaces = {
},

async setSelectWorkspace(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesService.setSelectWorkspace(workspaceId, windowId);
},

Expand Down Expand Up @@ -871,7 +888,7 @@ export const gWorkspaces = {

/* workspace icon Service */
async getWorkspaceIcon(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let icon = await WorkspacesIdUtils.getWorkspaceIconByIdAndWindowId(
workspaceId,
windowId
Expand All @@ -880,13 +897,13 @@ export const gWorkspaces = {
},

async setWorkspaceIcon(workspaceId, icon) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesService.setWorkspaceIcon(workspaceId, icon, windowId);
},

/* userContext Service */
async getWorkspaceContainerUserContextId(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
let userContextId =
await WorkspacesIdUtils.getWorkspaceContainerUserContextId(
workspaceId,
Expand All @@ -896,7 +913,7 @@ export const gWorkspaces = {
},

async setWorkspaceContainerUserContextId(workspaceId, userContextId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesService.setWorkspaceContainerUserContextId(
workspaceId,
userContextId,
Expand All @@ -909,7 +926,7 @@ export const gWorkspaces = {
userContextId,
icon
) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesService.setWorkspaceContainerUserContextIdAndIcon(
workspaceId,
userContextId,
Expand Down Expand Up @@ -1027,7 +1044,7 @@ export const gWorkspaces = {

/* Reorder Service */
async reorderWorkspaceDown(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesReorderService.reorderWorkspaceDown(
workspaceId,
windowId
Expand All @@ -1037,7 +1054,7 @@ export const gWorkspaces = {
},

async reorderWorkspaceUp(workspaceId) {
let windowId = this.getCurrentWindowId();
let windowId = await this.getCurrentWindowId();
await WorkspacesReorderService.reorderWorkspaceUp(
workspaceId,
windowId
Expand Down Expand Up @@ -1136,7 +1153,7 @@ export const gWorkspaces = {
// Check Workspaces Need migrate from Legacy Workspaces
await WorkspacesMigratorUtils.importDataFromLegacyWorkspaces(
window.gBrowser.tabs,
this.getCurrentWindowId()
await this.getCurrentWindowId()
);

if (
Expand Down Expand Up @@ -1287,7 +1304,7 @@ export const gWorkspaces = {
);
const defaultWorkspaceId =
await WorkspacesWindowIdUtils.getDefaultWorkspaceId(
gWorkspaces.getCurrentWindowId()
await gWorkspaces.getCurrentWindowId()
);
const beforeSiblingElem =
event.explicitOriginalTarget.previousElementSibling?.getAttribute(
Expand Down
21 changes: 21 additions & 0 deletions browser/components/FloorpStartup.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ const isMainBrowser = env.get("MOZ_BROWSER_TOOLBOX_PORT") === "";
prefs.setBoolPref("alerts.useSystemBackend", isNativeNotificationEnabled);
}

{
let workspacesFile = FileUtils.File(
PathUtils.join(
Services.dirsvc.get("ProfD", Ci.nsIFile).path,
"Workspaces.json",
),
);

if (workspacesFile.exists()) {
// Copy the file to Workspaces folder
let workspacesDir = FileUtils.File(
PathUtils.join(Services.dirsvc.get("ProfD", Ci.nsIFile).path, "Workspaces"),
);
if (!workspacesDir.exists()) {
workspacesDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
}
workspacesFile.copyTo(workspacesDir, "Workspaces.json");
workspacesFile.remove(false);
}
}

export async function onFinalUIStartup() {
Services.obs.removeObserver(onFinalUIStartup, "final-ui-startup");
let { BrowserManagerSidebar } = ChromeUtils.importESModule(
Expand Down
53 changes: 49 additions & 4 deletions browser/components/workspaces/WorkspacesWindowIdUtils.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */

import { WorkspacesExternalFileService } from "chrome://floorp/content/modules/workspaces/WorkspacesExternalFileService.mjs";
import { WorkspacesService } from "./WorkspacesService.mjs";

export const WorkspacesWindowIdUtils = {
get _workspacesStoreFile() {
Expand Down Expand Up @@ -104,9 +105,53 @@ export const WorkspacesWindowIdUtils = {
}
}

await IOUtils.writeJSON(
this._workspacesStoreFile,
json
);
await IOUtils.writeJSON(this._workspacesStoreFile, json);
},

// Verify that the correct windowId is assigned by parsing the data stored in windowId and the tabs.
async checkWindowIntegrity(windowId, tabs) {
const workspacesData = await this.getWindowWorkspacesData(windowId);
const workspaceIds = Object.keys(workspacesData);

// generate a list of workspaceIds from the tabs excluding null values
const workspaceIdsFromTabs = tabs
.map(tab =>
tab.getAttribute(WorkspacesService.workspacesTabAttributionId)
)
.filter(workspaceId => workspaceId !== null);

// check if the all the workspaceIds from the tabs are present in the workspacesData
for (const workspaceId of workspaceIdsFromTabs) {
if (!workspaceIds.includes(workspaceId)) {
console.error(
`WorkspaceId ${workspaceId} is not present in the workspacesData`
);
return false;
}
}
return true;
},

// Infer the windowId from the tabs
// Get the id from the tab's attributes and find the data that has that id.
async getWindowIdByInference(tabs) {
const workspaceIdsFromTabs = tabs
.map(tab =>
tab.getAttribute(WorkspacesService.workspacesTabAttributionId)
)
.filter(workspaceId => workspaceId !== null);

const workspacesData = await this.getAllWindowAndWorkspacesData();
for (const windowId in workspacesData.windows) {
const windowWorkspacesData = workspacesData.windows[windowId];
for (const workspaceId of workspaceIdsFromTabs) {
if (windowWorkspacesData[workspaceId]) {
return windowId;
}
}
}

console.error("WindowId could not be inferred from tabs");
return null;
},
};

0 comments on commit 6b7b921

Please sign in to comment.