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

{% macro generateMethodAnnotation(method) %}
  /**
{%-   for param in method.parameters %}
   * @param { {{param.kind|lite_closure_type_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|lite_closure_type_with_nullability}},
{%-       endfor %}
   *  }>}
{%-     endif %}
{%-   endif %}
   */
{% endmacro %}

{%  if generate_closure_exports -%}
goog.provide('{{module.namespace}}.{{interface.name}}');
goog.provide('{{module.namespace}}.{{interface.name}}Receiver');
goog.provide('{{module.namespace}}.{{interface.name}}CallbackRouter');
goog.provide('{{module.namespace}}.{{interface.name}}Interface');
goog.provide('{{module.namespace}}.{{interface.name}}Remote');
goog.provide('{{module.namespace}}.{{interface.name}}PendingReceiver');
{% endif %}

/**
 * @implements {mojo.internal.interfaceSupport.PendingReceiver}
 * @export
 */
{{module.namespace}}.{{interface.name}}PendingReceiver = class {
  /**
   * @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,
        {{module.namespace}}.{{interface.name}}.$interfaceName,
        scope);
  }
};

{%  if generate_closure_exports -%}
/** @interface */
{{module.namespace}}.{{interface.name}}Interface = class {
{%-  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 %}
};
{%- endif %}

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

    /**
     * @public {!mojo.internal.interfaceSupport.InterfaceRemoteBaseWrapper<!{{module.namespace}}.{{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}},
        {{module.namespace}}.{{interface_message_id}}_ParamsSpec.$,
{%-   if method.response_parameters != None %}
        {{module.namespace}}.{{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
 */
{{module.namespace}}.{{interface.name}}Receiver = class {
  /**
   * @param {!{{module.namespace}}.{{interface.name}}Interface } impl
   */
  constructor(impl) {
    /** @private {!mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal<!{{module.namespace}}.{{interface.name}}Remote>} */
    this.helper_internal_ = new mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal(
        {{module.namespace}}.{{interface.name}}Remote);

    /**
     * @public {!mojo.internal.interfaceSupport.InterfaceReceiverHelper<!{{module.namespace}}.{{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}},
        {{module.namespace}}.{{interface_message_id}}_ParamsSpec.$,
{%-   if method.response_parameters != None %}
        {{module.namespace}}.{{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
 */
{{module.namespace}}.{{interface.name}} = class {
  /**
   * @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 {!{{module.namespace}}.{{interface.name}}Remote}
   * @export
   */
  static getRemote() {
    let remote = new {{module.namespace}}.{{interface.name}}Remote;
    remote.$.bindNewPipeAndPassReceiver().bindInBrowser();
    return remote;
  }
};

{#--- Enums #}
{% from "lite/enum_definition.tmpl" import enum_def with context %}
{%- for enum in interface.enums %}
{{ enum_def("%s.%s"|format(module.namespace, interface.name),
            "%s.%s"|format(module.namespace, interface.name),
            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
 */
{{module.namespace}}.{{interface.name}}CallbackRouter = class {
  constructor() {
    this.helper_internal_ = new mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal(
      {{module.namespace}}.{{interface.name}}Remote);

    /**
     * @public {!mojo.internal.interfaceSupport.InterfaceReceiverHelper<!{{module.namespace}}.{{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}},
        {{module.namespace}}.{{interface_message_id}}_ParamsSpec.$,
{%-   if method.response_parameters != None %}
        {{module.namespace}}.{{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.
   * @export
   */
  removeListener(id) {
    return this.router_.removeListener(id);
  }
};