chromium/mojo/public/tools/bindings/generators/ts_templates/interface_definition.tmpl

{%- macro generate_return_type(method) %}
{%-   if method.response_parameters != None %}
{%-     if method.response_parameters|length == 0 %}Promise<void>
{%-     else %}Promise<{
{%-       for response_parameter in method.response_parameters %}
        {{response_parameter.name}}: {{response_parameter.kind|ts_type_maybe_nullable}},
{%-       endfor %} }>
{%-     endif %}
{%-   else %}void
{%-   endif %}
{%- endmacro %}

export class {{interface.name}}PendingReceiver implements
    mojo.internal.interfaceSupport.PendingReceiver {

  handle: mojo.internal.interfaceSupport.Endpoint;
  constructor(handle: MojoHandle|mojo.internal.interfaceSupport.Endpoint) {
    this.handle = mojo.internal.interfaceSupport.getEndpointForReceiver(handle);
  }

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

export interface {{interface.name}}Interface {
{%-  for method in interface.methods %}
  {{method.name}}(
{%-   for param in method.parameters -%}
{{param.name|sanitize_identifier}}: {{param.kind|ts_type_maybe_nullable}}{% if not loop.last %}, {% endif %}
{%-   endfor -%}): {{generate_return_type(method)}};
{%- endfor %}
}

export class {{interface.name}}Remote implements {{interface.name}}Interface {
  private proxy: mojo.internal.interfaceSupport.InterfaceRemoteBase<{{interface.name}}PendingReceiver>;
  $: mojo.internal.interfaceSupport.InterfaceRemoteBaseWrapper<{{interface.name}}PendingReceiver>;
  onConnectionError: mojo.internal.interfaceSupport.ConnectionErrorEventRouter;

  constructor(
      handle?: MojoHandle|mojo.internal.interfaceSupport.Endpoint) {
    this.proxy =
        new mojo.internal.interfaceSupport.InterfaceRemoteBase(
          {{interface.name}}PendingReceiver, handle);

    this.$ = new mojo.internal.interfaceSupport.InterfaceRemoteBaseWrapper(this.proxy);

    this.onConnectionError = this.proxy.getConnectionErrorEventRouter();
  }

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

  {{method.name}}(
{%- for param in method.parameters %}
      {{param.name}}: {{param.kind|ts_type_maybe_nullable}}{%- if not loop.last %},{% endif %}
{%- endfor -%}): {{generate_return_type(method)}} {
{%- 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 {
  private helper_internal_: mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal<{{interface.name}}Remote>;

  $: mojo.internal.interfaceSupport.InterfaceReceiverHelper<{{interface.name}}Remote>;

  onConnectionError: mojo.internal.interfaceSupport.ConnectionErrorEventRouter;

{% if interface.methods|length %}
  constructor(impl: {{interface.name}}Interface) {
{% else %}
  constructor(_impl: {{interface.name}}Interface) {
{%- endif %}
    this.helper_internal_ = new mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal(
        {{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 %}
    this.onConnectionError = this.helper_internal_.getConnectionErrorEventRouter();
  }
}

export class {{interface.name}} {
  static get $interfaceName(): string {
    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.
   */
  static getRemote(): {{interface.name}}Remote {
    let remote = new {{interface.name}}Remote;
    remote.$.bindNewPipeAndPassReceiver().bindInBrowser();
    return remote;
  }
}

{#--- Enums #}
{%- from "enum_definition.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 {
  private helper_internal_: mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal<{{interface.name}}Remote>;
  $: mojo.internal.interfaceSupport.InterfaceReceiverHelper<{{interface.name}}Remote>;
  router_: mojo.internal.interfaceSupport.CallbackRouter;
{%  for method in interface.methods %}
{%-   set interface_message_id =
          interface.mojom_name ~ "_" ~ method.mojom_name %}
  {{method.name}}: mojo.internal.interfaceSupport.InterfaceCallbackReceiver;
{%- endfor %}
  onConnectionError: mojo.internal.interfaceSupport.ConnectionErrorEventRouter;

  constructor() {
    this.helper_internal_ = new mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal(
      {{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 %}
    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 %}
    this.onConnectionError = this.helper_internal_.getConnectionErrorEventRouter();
  }

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