Bug 1749675 - [remote] Don't apply session data for modules that don't exist for a given destination. r=webdriver-reviewers,jdescottes

Differential Revision: https://phabricator.services.mozilla.com/D134266
This commit is contained in:
Henrik Skupin 2022-01-20 07:28:38 +00:00
Родитель 1937921909
Коммит 23875862ae
5 изменённых файлов: 95 добавлений и 9 удалений

Просмотреть файл

@ -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}]`;
}

Просмотреть файл

@ -146,6 +146,19 @@ 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",
@ -153,12 +166,7 @@ class RootMessageHandler extends MessageHandler {
[isAdding ? "added" : "removed"]: updatedValues,
category,
},
destination: {
type: WindowGlobalMessageHandler.type,
contextDescriptor: {
type: CONTEXT_DESCRIPTOR_TYPES.ALL,
},
},
destination,
});
}
}

Просмотреть файл

@ -72,6 +72,10 @@ class WindowGlobalMessageHandler extends MessageHandler {
return;
}
const destination = {
type: WindowGlobalMessageHandler.type,
};
for (const sessionDataItem of sessionDataItems) {
const {
moduleName,
@ -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",
@ -91,9 +101,7 @@ class WindowGlobalMessageHandler extends MessageHandler {
// though it will make the implementation more complex.
added: [value],
},
destination: {
type: WindowGlobalMessageHandler.type,
},
destination,
});
}
}

Просмотреть файл

@ -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");

Просмотреть файл

@ -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;