Commit caf75049 authored by Henrik Skupin's avatar Henrik Skupin
Browse files

Bug 1830884 - [webdriver-bidi] Add serialization / deserialization helpers to...

Bug 1830884 - [webdriver-bidi] Add serialization / deserialization helpers to WindowGlobalBiDiModule. r=webdriver-reviewers,jdescottes

Differential Revision: https://phabricator.services.mozilla.com/D177495
parent ae48ea1f
Loading
Loading
Loading
Loading
+31 −32
Original line number Diff line number Diff line
@@ -47,6 +47,17 @@ export const OwnershipModel = {
  Root: "root",
};

/**
 * Extra options for serializing and deserializing remote values.
 *
 * @typedef {object} RemoteValueOptions
 *
 * @property {NodeCache=} nodeCache
 *     The cache containing DOM node references.
 * @param {Function=} emitScriptMessage
 *     The function to emit "script.message" event.
 */

/**
 * An object which holds the information of how
 * ECMAScript objects should be serialized.
@@ -144,9 +155,8 @@ function checkDateTimeString(dateString) {
 *     The Realm in which the value is deserialized.
 * @param {Array} serializedValueList
 *     List of serialized values.
 * @param {object} options
 * @param {NodeCache} options.nodeCache
 *     The cache containing DOM node references.
 * @param {RemoteValueOptions} options
 *     Extra Remote Value deserialization options.
 *
 * @returns {Array} List of deserialized values.
 *
@@ -177,9 +187,8 @@ function deserializeValueList(realm, serializedValueList, options = {}) {
 *     The Realm in which the value is deserialized.
 * @param {Array} serializedKeyValueList
 *     List of serialized key-value.
 * @param {object} options
 * @param {NodeCache} options.nodeCache
 *     The cache containing DOM node references.
 * @param {RemoteValueOptions} options
 *     Extra Remote Value deserialization options.
 *
 * @returns {Array} List of deserialized key-value.
 *
@@ -225,9 +234,8 @@ function deserializeKeyValueList(realm, serializedKeyValueList, options = {}) {
 *     Shared unique reference of the Node.
 * @param {Realm} realm
 *     The Realm in which the value is deserialized.
 * @param {object} options
 * @param {NodeCache} options.nodeCache
 *     The cache containing DOM node references.
 * @param {RemoteValueOptions} options
 *     Extra Remote Value deserialization options.
 *
 * @returns {Node} The deserialized DOM node.
 */
@@ -278,11 +286,8 @@ function deserializeSharedReference(sharedRef, realm, options = {}) {
 *     The Realm in which the value is deserialized.
 * @param {object} serializedValue
 *     Value of any type to be deserialized.
 * @param {object} options
 * @param {NodeCache} options.nodeCache
 *     The cache containing DOM node references.
 * @param {Function} options.emitScriptMessage
 *     The function to emit "script.message" event.
 * @param {RemoteValueOptions} options
 *     Extra Remote Value deserialization options.
 *
 * @returns {object} Deserialized representation of the value.
 */
@@ -460,9 +465,8 @@ function getHandleForObject(realm, ownershipType, object) {
 *    Node to create the unique reference for.
 * @param {Realm} realm
 *     The Realm in which the value is serialized.
 * @param {object} options
 * @param {NodeCache} options.nodeCache
 *     The cache containing DOM node references.
 * @param {RemoteValueOptions} options
 *     Extra Remote Value serialization options.
 *
 * @returns {string}
 *    Shared unique reference for the Node.
@@ -522,9 +526,8 @@ function isShadowRoot(node) {
 *     Map of internal ids.
 * @param {Realm} realm
 *     The Realm from which comes the value being serialized.
 * @param {object} options
 * @param {NodeCache} options.nodeCache
 *     The cache containing DOM node references.
 * @param {RemoteValueOptions} options
 *     Extra Remote Value serialization options.
 *
 * @returns {object} Object for serialized values.
 */
@@ -571,9 +574,8 @@ function serializeArrayLike(
 *     Map of internal ids.
 * @param {Realm} realm
 *     The Realm from which comes the value being serialized.
 * @param {object} options
 * @param {NodeCache} options.nodeCache
 *     The cache containing DOM node references.
 * @param {RemoteValueOptions} options
 *     Extra Remote Value serialization options.
 *
 * @returns {Array} List of serialized values.
 */
@@ -625,9 +627,8 @@ function serializeList(
 *     Map of internal ids.
 * @param {Realm} realm
 *     The Realm from which comes the value being serialized.
 * @param {object} options
 * @param {NodeCache} options.nodeCache
 *     The cache containing DOM node references.
 * @param {RemoteValueOptions} options
 *     Extra Remote Value serialization options.
 *
 * @returns {Array} List of serialized values.
 */
@@ -688,9 +689,8 @@ function serializeMapping(
 *     Map of internal ids.
 * @param {Realm} realm
 *     The Realm from which comes the value being serialized.
 * @param {object} options
 * @param {NodeCache} options.nodeCache
 *     The cache containing DOM node references.
 * @param {RemoteValueOptions} options
 *     Extra Remote Value serialization options.
 *
 * @returns {object} Serialized value.
 */
@@ -792,9 +792,8 @@ function serializeNode(
 *     Map of internal ids.
 * @param {Realm} realm
 *     The Realm from which comes the value being serialized.
 * @param {object} options
 * @param {NodeCache} options.nodeCache
 *     The cache containing DOM node references.
 * @param {RemoteValueOptions} options
 *     Extra Remote Value serialization options.
 *
 * @returns {object} Serialized representation of the value.
 */
+59 −3
Original line number Diff line number Diff line
@@ -4,15 +4,71 @@

import { Module } from "chrome://remote/content/shared/messagehandler/Module.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  deserialize: "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
  serialize: "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
});

/**
 * Base class for all WindowGlobal BiDi MessageHandler modules.
 */
export class WindowGlobalBiDiModule extends Module {
  get nodeCache() {
    return this.processActor.getNodeCache();
  get #nodeCache() {
    return this.#processActor.getNodeCache();
  }

  get processActor() {
  get #processActor() {
    return ChromeUtils.domProcessChild.getActor("WebDriverProcessData");
  }

  /**
   * Wrapper to deserialize a local / remote value.
   *
   * @param {Realm} realm
   *     The Realm in which the value is deserialized.
   * @param {object} serializedValue
   *     Value of any type to be deserialized.
   * @param {RemoteValueOptions=} options
   *     Extra Remote Value deserialization options.
   *
   * @returns {object}
   *     Deserialized representation of the value.
   */
  deserialize(realm, serializedValue, options = {}) {
    options.nodeCache = this.#nodeCache;

    return lazy.deserialize(realm, serializedValue, options);
  }

  /**
   * Wrapper to serialize a value as a remote value.
   *
   * @param {object} value
   *     Value of any type to be serialized.
   * @param {SerializationOptions} serializationOptions
   *     Options which define how ECMAScript objects should be serialized.
   * @param {OwnershipModel} ownershipType
   *     The ownership model to use for this serialization.
   * @param {Realm} realm
   *     The Realm from which comes the value being serialized.
   * @param {RemoteValueOptions=} options
   *     Extra Remote Value serialization options.
   *
   * @returns {object}
   *     Serialized representation of the value.
   */
  serialize(value, serializationOptions, ownershipType, realm, options = {}) {
    options.nodeCache = this.#nodeCache;

    return lazy.serialize(
      value,
      serializationOptions,
      ownershipType,
      new Map(),
      realm,
      options
    );
  }
}
+2 −4
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@ const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  action: "chrome://remote/content/shared/webdriver/Actions.sys.mjs",
  deserialize: "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
  error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
});

@@ -33,6 +32,7 @@ class InputModule extends WindowGlobalBiDiModule {

    await this.#deserializeActionOrigins(actions);
    const actionChain = lazy.action.Chain.fromJSON(this.#actionState, actions);

    await actionChain.dispatch(this.#actionState, this.messageHandler.window);
  }

@@ -85,9 +85,7 @@ class InputModule extends WindowGlobalBiDiModule {
    }

    const realm = this.messageHandler.getRealm();
    return lazy.deserialize(realm, sharedReference, {
      nodeCache: this.nodeCache,
    });
    return this.deserialize(realm, sharedReference);
  }
}

+2 −8
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@ ChromeUtils.defineESModuleGetters(lazy, {
    "chrome://remote/content/shared/listeners/ConsoleListener.sys.mjs",
  isChromeFrame: "chrome://remote/content/shared/Stack.sys.mjs",
  OwnershipModel: "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
  serialize: "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
  setDefaultSerializationOptions:
    "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
});
@@ -138,22 +137,17 @@ class LogModule extends WindowGlobalBiDiModule {

    // Serialize each arg as remote value.
    const defaultRealm = this.messageHandler.getRealm();
    const nodeCache = this.nodeCache;
    const serializedArgs = [];
    for (const arg of args) {
      // Note that we can pass a default realm for now since realms are only
      // involved when creating object references, which will not happen with
      // OwnershipModel.None. This will be revisited in Bug 1742589.
      serializedArgs.push(
        lazy.serialize(
        this.serialize(
          Cu.waiveXrays(arg),
          lazy.setDefaultSerializationOptions(),
          lazy.OwnershipModel.None,
          new Map(),
          defaultRealm,
          {
            nodeCache,
          }
          defaultRealm
        )
      );
    }
+13 −31
Original line number Diff line number Diff line
@@ -7,12 +7,10 @@ import { WindowGlobalBiDiModule } from "chrome://remote/content/webdriver-bidi/m
const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  deserialize: "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
  error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
  getFramesFromStack: "chrome://remote/content/shared/Stack.sys.mjs",
  isChromeFrame: "chrome://remote/content/shared/Stack.sys.mjs",
  OwnershipModel: "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
  serialize: "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
  setDefaultSerializationOptions:
    "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
  stringify: "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
@@ -64,7 +62,7 @@ class ScriptModule extends WindowGlobalBiDiModule {
    }
  }

  #buildExceptionDetails(exception, stack, realm, resultOwnership, options) {
  #buildExceptionDetails(exception, stack, realm, resultOwnership) {
    exception = this.#toRawObject(exception);

    // A stacktrace is mandatory to build exception details and a missing stack
@@ -94,13 +92,11 @@ class ScriptModule extends WindowGlobalBiDiModule {

    return {
      columnNumber: stack.column - 1,
      exception: lazy.serialize(
      exception: this.serialize(
        exception,
        lazy.setDefaultSerializationOptions(),
        resultOwnership,
        new Map(),
        realm,
        options
        realm
      ),
      lineNumber: stack.line - 1,
      stackTrace: { callFrames },
@@ -113,8 +109,7 @@ class ScriptModule extends WindowGlobalBiDiModule {
    realm,
    awaitPromise,
    resultOwnership,
    serializationOptions,
    options
    serializationOptions
  ) {
    let evaluationStatus, exception, result, stack;

@@ -160,13 +155,11 @@ class ScriptModule extends WindowGlobalBiDiModule {
      case EvaluationStatus.Normal:
        return {
          evaluationStatus,
          result: lazy.serialize(
          result: this.serialize(
            this.#toRawObject(result),
            serializationOptions,
            resultOwnership,
            new Map(),
            realm,
            options
            realm
          ),
          realmId: realm.id,
        };
@@ -177,8 +170,7 @@ class ScriptModule extends WindowGlobalBiDiModule {
            exception,
            stack,
            realm,
            resultOwnership,
            options
            resultOwnership
          ),
          realmId: realm.id,
        };
@@ -203,11 +195,10 @@ class ScriptModule extends WindowGlobalBiDiModule {
      serializationOptions,
    } = channelProperties;

    const data = lazy.serialize(
    const data = this.serialize(
      this.#toRawObject(message),
      lazy.setDefaultSerializationOptions(serializationOptions),
      ownershipType,
      new Map(),
      realm
    );

@@ -234,7 +225,7 @@ class ScriptModule extends WindowGlobalBiDiModule {
      } = script;
      const realm = this.messageHandler.getRealm({ sandboxName: sandbox });
      const deserializedArguments = commandArguments.map(arg =>
        lazy.deserialize(realm, arg, {
        this.deserialize(realm, arg, {
          emitScriptMessage: this.#emitScriptMessage,
        })
      );
@@ -336,23 +327,20 @@ class ScriptModule extends WindowGlobalBiDiModule {
    } = options;

    const realm = this.messageHandler.getRealm({ realmId, sandboxName });
    const nodeCache = this.nodeCache;

    const deserializedArguments =
      commandArguments !== null
        ? commandArguments.map(arg =>
            lazy.deserialize(realm, arg, {
            this.deserialize(realm, arg, {
              emitScriptMessage: this.#emitScriptMessage,
              nodeCache,
            })
          )
        : [];

    const deserializedThis =
      thisParameter !== null
        ? lazy.deserialize(realm, thisParameter, {
        ? this.deserialize(realm, thisParameter, {
            emitScriptMessage: this.#emitScriptMessage,
            nodeCache,
          })
        : null;

@@ -367,10 +355,7 @@ class ScriptModule extends WindowGlobalBiDiModule {
      realm,
      awaitPromise,
      resultOwnership,
      serializationOptions,
      {
        nodeCache,
      }
      serializationOptions
    );
  }

@@ -436,10 +421,7 @@ class ScriptModule extends WindowGlobalBiDiModule {
      realm,
      awaitPromise,
      resultOwnership,
      serializationOptions,
      {
        nodeCache: this.nodeCache,
      }
      serializationOptions
    );
  }