Skip to content

Commit

Permalink
Bug 1749675 - [remote] Don't apply session data for modules that don'…
Browse files Browse the repository at this point in the history
…t exist for a given destination. r=webdriver-reviewers,jdescottes

Differential Revision: https://phabricator.services.mozilla.com/D134266
  • Loading branch information
whimboo committed Jan 20, 2022
1 parent 1937921 commit 2387586
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 9 deletions.
15 changes: 15 additions & 0 deletions remote/shared/messagehandler/ModuleCache.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ class ModuleCache {
return module;
}

/**
* Check if the given module exists for the destination.
*
* @param {String} moduleName
* The name of the module.
* @param {Destination} destination
* The destination.
* @returns {Boolean}
* True if the module exists.
*/
hasModule(moduleName, destination) {
const classes = this.getAllModuleClasses(moduleName, destination);
return classes.length != 0;
}

toString() {
return `[object ${this.constructor.name} ${this.messageHandler.name}]`;
}
Expand Down
20 changes: 14 additions & 6 deletions remote/shared/messagehandler/RootMessageHandler.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,27 @@ class RootMessageHandler extends MessageHandler {
return [];
}

const destination = {
type: WindowGlobalMessageHandler.type,
contextDescriptor: {
type: CONTEXT_DESCRIPTOR_TYPES.ALL,
},
};

// Don't apply session data if the module is not present
// for the destination.
if (!this._moduleCache.hasModule(moduleName, destination)) {
return Promise.resolve();
}

return this.handleCommand({
moduleName,
commandName: "_applySessionData",
params: {
[isAdding ? "added" : "removed"]: updatedValues,
category,
},
destination: {
type: WindowGlobalMessageHandler.type,
contextDescriptor: {
type: CONTEXT_DESCRIPTOR_TYPES.ALL,
},
},
destination,
});
}
}
14 changes: 11 additions & 3 deletions remote/shared/messagehandler/WindowGlobalMessageHandler.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class WindowGlobalMessageHandler extends MessageHandler {
return;
}

const destination = {
type: WindowGlobalMessageHandler.type,
};

for (const sessionDataItem of sessionDataItems) {
const {
moduleName,
Expand All @@ -80,6 +84,12 @@ class WindowGlobalMessageHandler extends MessageHandler {
value,
} = sessionDataItem;
if (this._isRelevantContext(contextDescriptor)) {
// Don't apply session data if the module is not present
// for the destination.
if (!this._moduleCache.hasModule(moduleName, destination)) {
continue;
}

await this.handleCommand({
moduleName,
commandName: "_applySessionData",
Expand All @@ -91,9 +101,7 @@ class WindowGlobalMessageHandler extends MessageHandler {
// though it will make the implementation more complex.
added: [value],
},
destination: {
type: WindowGlobalMessageHandler.type,
},
destination,
});
}
}
Expand Down
30 changes: 30 additions & 0 deletions remote/shared/messagehandler/test/browser/browser_session_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,36 @@ add_task(async function test_sessionData() {
is(sessionDataSnapshot.size, 0, "session data should be empty again");
});

add_task(async function test_sessionDataRootOnlyModule() {
const sessionId = "sessionData-test-rootOnly";

const rootMessageHandler = createRootMessageHandler(sessionId);
ok(rootMessageHandler, "Valid ROOT MessageHandler created");

await BrowserTestUtils.loadURI(
gBrowser,
"https://example.com/document-builder.sjs?html=tab"
);

const windowGlobalCreated = rootMessageHandler.once("message-handler-event");

// Updating the session data on the root message handler should not cause
// failures for other message handlers if the module only exists for root.
await rootMessageHandler.addSessionData({
moduleName: "rootOnly",
category: "session_data_root_only",
contextDescriptor: {
type: CONTEXT_DESCRIPTOR_TYPES.ALL,
},
values: [true],
});

await windowGlobalCreated;
ok(true, "Window global has been initialized");

rootMessageHandler.destroy();
});

function checkSessionDataItem(item, moduleName, category, contextType, value) {
is(item.moduleName, moduleName, "Data item has the expected module name");
is(item.category, category, "Data item has the expected category");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const EXPORTED_SYMBOLS = ["rootOnly"];

const { Module } = ChromeUtils.import(
"chrome://remote/content/shared/messagehandler/Module.jsm"
);

class RootOnly extends Module {
destroy() {}

/**
* Commands
*/

testCommand(params = {}) {
return params;
}
}

const rootOnly = RootOnly;

0 comments on commit 2387586

Please sign in to comment.