chromium/mojo/public/tools/bindings/generators/js_templates/lite/interface_definition_for_module.tmpl

{% macro generateMethodAnnotation(method) %}
  /**
{%-   for param in method.parameters %}
   * @param { {{param.kind|type_in_js_module_with_nullability}} } {{param.name|sanitize_identifier}}
{%-   endfor -%}
{%-   if method.response_parameters != None %}
{%-     if method.response_parameters|length == 0 %}
   * @return {!Promise}
{%-     else %}
   * @return {!Promise<{
{%-       for response_parameter in method.response_parameters %}
        {{response_parameter.name}}: {{response_parameter.kind|type_in_js_module_with_nullability}},
{%-       endfor %}
   *  }>}
{%-     endif %}
{%-   endif %}
   */
{% endmacro %}

/**
 * @implements {mojo.internal.interfaceSupport.PendingReceiver}
 */
export class {{interface.name}}PendingReceiver {
  /**
   * @param {!MojoHandle|!mojo.internal.interfaceSupport.Endpoint} handle
   */
  constructor(handle) {
    /** @public {!mojo.internal.interfaceSupport.Endpoint} */
    this.handle = mojo.internal.interfaceSupport.getEndpointForReceiver(handle);
  }

  /** @param {string=} scope */
  bindInBrowser(scope = 'context') {
    mojo.internal.interfaceSupport.bind(
        this.handle, '{{mojom_namespace}}.{{interface.name}}', scope);
  }
}

/** @interface */
export class {{interface.name}}Interface {
{%-  for method in interface.methods %}
  {{generateMethodAnnotation(method)}}
  {{method.name}}(
{%-   for param in method.parameters -%}
{{param.name|sanitize_identifier}}{% if not loop.last %}, {% endif %}
{%-   endfor -%}
) {}
{%- endfor %}
}

/**
 * @implements { {{interface.name}}Interface }
 */
export class {{interface.name}}Remote {
  /** @param {MojoHandle|mojo.internal.interfaceSupport.Endpoint=} handle */
  constructor(handle = undefined) {
    /**
     * @private {!mojo.internal.interfaceSupport.InterfaceRemoteBase<!{{interface.name}}PendingReceiver>}
     */
    this.proxy =
        new mojo.internal.interfaceSupport.InterfaceRemoteBase(
          {{interface.name}}PendingReceiver,
          handle);

    /**
     * @public {!mojo.internal.interfaceSupport.InterfaceRemoteBaseWrapper<!{{interface.name}}PendingReceiver>}
     */
    this.$ = new mojo.internal.interfaceSupport.InterfaceRemoteBaseWrapper(this.proxy);

    /** @public {!mojo.internal.interfaceSupport.ConnectionErrorEventRouter} */
    this.onConnectionError = this.proxy.getConnectionErrorEventRouter();
  }

{%- for method in interface.methods -%}
{%-   set interface_message_id =
          interface.mojom_name ~ "_" ~ method.mojom_name %}

  {{generateMethodAnnotation(method)}}
  {{method.name}}(
{%- for param in method.parameters %}
      {{param.name}}{%- if not loop.last %},{% endif %}
{%- endfor -%}) {
{%- if method.response_parameters != None %}
    return this.proxy.sendMessage(
{%- else %}
    this.proxy.sendMessage(
{%- endif %}
        {{method.ordinal}},
        {{interface_message_id}}_ParamsSpec.$,
{%-   if method.response_parameters != None %}
        {{interface_message_id}}_ResponseParamsSpec.$,
{%-   else %}
        null,
{%-   endif %}
        [
{%- for param in method.parameters %}
          {{param.name}}{%- if not loop.last %},{% endif %}
{%- endfor %}
        ]);
  }
{%-  endfor %}
}

/**
 * An object which receives request messages for the {{interface.name}}
 * mojom interface. Must be constructed over an object which implements that
 * interface.
 */
export class {{interface.name}}Receiver {
  /**
   * @param {!{{interface.name}}Interface } impl
   */
  constructor(impl) {
    /** @private {!mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal<!{{interface.name}}Remote>} */
    this.helper_internal_ = new mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal(
        {{interface.name}}Remote);

    /**
     * @public {!mojo.internal.interfaceSupport.InterfaceReceiverHelper<!{{interface.name}}Remote>}
     */
    this.$ = new mojo.internal.interfaceSupport.InterfaceReceiverHelper(this.helper_internal_);

{%  for method in interface.methods %}
{%-   set interface_message_id =
          interface.mojom_name ~ "_" ~ method.mojom_name %}
    this.helper_internal_.registerHandler(
        {{method.ordinal}},
        {{interface_message_id}}_ParamsSpec.$,
{%-   if method.response_parameters != None %}
        {{interface_message_id}}_ResponseParamsSpec.$,
{%-   else %}
        null,
{%-   endif %}
        impl.{{method.name}}.bind(impl));
{%- endfor %}
    /** @public {!mojo.internal.interfaceSupport.ConnectionErrorEventRouter} */
    this.onConnectionError = this.helper_internal_.getConnectionErrorEventRouter();
  }
}

export class {{interface.name}} {
  /**
   * @return {!string}
   */
  static get $interfaceName() {
    return "{{mojom_namespace}}.{{interface.name}}";
  }

  /**
   * Returns a remote for this interface which sends messages to the browser.
   * The browser must have an interface request binder registered for this
   * interface and accessible to the calling document's frame.
   *
   * @return {!{{interface.name}}Remote}
   */
  static getRemote() {
    let remote = new {{interface.name}}Remote;
    remote.$.bindNewPipeAndPassReceiver().bindInBrowser();
    return remote;
  }
}

{#--- Enums #}
{% from "lite/enum_definition_for_module.tmpl" import enum_def with context %}
{%- for enum in interface.enums %}
{{enum_def(enum)}}
{%-  endfor %}

/**
 * An object which receives request messages for the {{interface.name}}
 * mojom interface and dispatches them as callbacks. One callback receiver exists
 * on this object for each message defined in the mojom interface, and each
 * receiver can have any number of listeners added to it.
 */
export class {{interface.name}}CallbackRouter {
  constructor() {
    this.helper_internal_ = new mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal(
      {{interface.name}}Remote);

    /**
     * @public {!mojo.internal.interfaceSupport.InterfaceReceiverHelper<!{{interface.name}}Remote>}
     */
    this.$ = new mojo.internal.interfaceSupport.InterfaceReceiverHelper(this.helper_internal_);

    this.router_ = new mojo.internal.interfaceSupport.CallbackRouter;
{%  for method in interface.methods %}
{%-   set interface_message_id =
          interface.mojom_name ~ "_" ~ method.mojom_name %}
    /**
     * @public {!mojo.internal.interfaceSupport.InterfaceCallbackReceiver}
     */
    this.{{method.name}} =
        new mojo.internal.interfaceSupport.InterfaceCallbackReceiver(
            this.router_);

    this.helper_internal_.registerHandler(
        {{method.ordinal}},
        {{interface_message_id}}_ParamsSpec.$,
{%-   if method.response_parameters != None %}
        {{interface_message_id}}_ResponseParamsSpec.$,
        this.{{method.name}}.createReceiverHandler(true /* expectsResponse */));
{%-   else %}
        null,
        this.{{method.name}}.createReceiverHandler(false /* expectsResponse */));
{%-   endif %}
{%- endfor %}
    /** @public {!mojo.internal.interfaceSupport.ConnectionErrorEventRouter} */
    this.onConnectionError = this.helper_internal_.getConnectionErrorEventRouter();
  }

  /**
   * @param {number} id An ID returned by a prior call to addListener.
   * @return {boolean} True iff the identified listener was found and removed.
   */
  removeListener(id) {
    return this.router_.removeListener(id);
  }
}