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

{%- for method in interface.methods %}
{%-   set interface_method_id =
          interface.mojom_name ~ "_" ~ method.mojom_name %}
  var k{{interface_method_id}}_Name = {{method.ordinal}};
{%- endfor %}

  function {{interface.name}}Ptr(handleOrPtrInfo) {
    this.ptr = new bindings.InterfacePtrController({{interface.name}},
                                                   handleOrPtrInfo);
  }

  function {{interface.name}}AssociatedPtr(associatedInterfacePtrInfo) {
    this.ptr = new associatedBindings.AssociatedInterfacePtrController(
        {{interface.name}}, associatedInterfacePtrInfo);
  }

  {{interface.name}}AssociatedPtr.prototype =
      Object.create({{interface.name}}Ptr.prototype);
  {{interface.name}}AssociatedPtr.prototype.constructor =
      {{interface.name}}AssociatedPtr;

  function {{interface.name}}Proxy(receiver) {
    this.receiver_ = receiver;
  }

{%- for method in interface.methods %}
{%-   set interface_method_id =
          interface.mojom_name ~ "_" ~ method.mojom_name %}
  {{interface.name}}Ptr.prototype.{{method.name}} = function() {
    return {{interface.name}}Proxy.prototype.{{method.name}}
        .apply(this.ptr.getProxy(), arguments);
  };

  {{interface.name}}Proxy.prototype.{{method.name}} = function(
{%- for parameter in method.parameters -%}
{{parameter.name|sanitize_identifier}}{% if not loop.last %}, {% endif %}
{%- endfor -%}
) {
    var params_ = new {{interface_method_id}}_Params();
{%- for parameter in method.parameters %}
    params_.{{parameter.name}} = {{parameter.name|sanitize_identifier}};
{%- endfor %}

{%- if method.response_parameters == None %}
{%-   if method|method_passes_associated_kinds %}
    var builder = new codec.MessageV2Builder(
        k{{interface_method_id}}_Name,
        codec.align({{interface_method_id}}_Params.encodedSize));
    builder.setPayload({{interface_method_id}}_Params, params_);
{%-   else %}
    var builder = new codec.MessageV0Builder(
        k{{interface_method_id}}_Name,
        codec.align({{interface_method_id}}_Params.encodedSize));
    builder.encodeStruct({{interface_method_id}}_Params, params_);
{%-   endif %}
    var message = builder.finish();
    this.receiver_.accept(message);
{%- else %}
    return new Promise(function(resolve, reject) {
{%-   if method|method_passes_associated_kinds %}
      var builder = new codec.MessageV2Builder(
          k{{interface_method_id}}_Name,
          codec.align({{interface_method_id}}_Params.encodedSize),
          codec.kMessageExpectsResponse, 0);
      builder.setPayload({{interface_method_id}}_Params, params_);
{%-   else %}
      var builder = new codec.MessageV1Builder(
          k{{interface_method_id}}_Name,
          codec.align({{interface_method_id}}_Params.encodedSize),
          codec.kMessageExpectsResponse, 0);
      builder.encodeStruct({{interface_method_id}}_Params, params_);
{%-   endif %}
      var message = builder.finish();
      this.receiver_.acceptAndExpectResponse(message).then(function(message) {
        var reader = new codec.MessageReader(message);
        var responseParams =
            reader.decodeStruct({{interface_method_id}}_ResponseParams);
        resolve(responseParams);
      }).catch(function(result) {
        reject(Error("Connection error: " + result));
      });
    }.bind(this));
{%- endif %}
  };
{%- endfor %}

  function {{interface.name}}Stub(delegate) {
    this.delegate_ = delegate;
  }

{%- for method in interface.methods %}
  {{interface.name}}Stub.prototype.{{method.name}} = function({{method.parameters|map(attribute='name')|map('sanitize_identifier')|join(', ')}}) {
    return this.delegate_ && this.delegate_.{{method.name}} && this.delegate_.{{method.name}}({{method.parameters|map(attribute='name')|map('sanitize_identifier')|join(', ')}});
  }
{%- endfor %}

  {{interface.name}}Stub.prototype.accept = function(message) {
    var reader = new codec.MessageReader(message);
    switch (reader.messageName) {
{%- for method in interface.methods %}
{%-   set interface_method_id =
          interface.mojom_name ~ "_" ~ method.mojom_name %}
{%- if method.response_parameters == None %}
    case k{{interface_method_id}}_Name:
      var params = reader.decodeStruct({{interface_method_id}}_Params);
      this.{{method.name}}(
{%- for parameter in method.parameters -%}
  params.{{parameter.name}}{% if not loop.last %}, {% endif %}
{%- endfor %});
      return true;
{%- endif %}
{%- endfor %}
    default:
      return false;
    }
  };

  {{interface.name}}Stub.prototype.acceptWithResponder =
      function(message, responder) {
    var reader = new codec.MessageReader(message);
    switch (reader.messageName) {
{%- for method in interface.methods %}
{%-   set interface_method_id =
          interface.mojom_name ~ "_" ~ method.mojom_name %}
{%- if method.response_parameters != None %}
    case k{{interface_method_id}}_Name:
      var params = reader.decodeStruct({{interface_method_id}}_Params);
      this.{{method.name}}(
{%- for parameter in method.parameters -%}
params.{{parameter.name}}{% if not loop.last %}, {% endif -%}
{%- endfor %}).then(function(response) {
        var responseParams =
            new {{interface_method_id}}_ResponseParams();
{%-     for parameter in method.response_parameters %}
        responseParams.{{parameter.name}} = response.{{parameter.name}};
{%-     endfor %}
{%- if method|method_passes_associated_kinds %}
        var builder = new codec.MessageV2Builder(
            k{{interface_method_id}}_Name,
            codec.align({{interface_method_id}}_ResponseParams
                .encodedSize),
            codec.kMessageIsResponse, reader.requestID);
        builder.setPayload({{interface_method_id}}_ResponseParams,
                             responseParams);
{%- else %}
        var builder = new codec.MessageV1Builder(
            k{{interface_method_id}}_Name,
            codec.align({{interface_method_id}}_ResponseParams.encodedSize),
            codec.kMessageIsResponse, reader.requestID);
        builder.encodeStruct({{interface_method_id}}_ResponseParams,
                             responseParams);
{%- endif %}
        var message = builder.finish();
        responder.accept(message);
      });
      return true;
{%- endif %}
{%- endfor %}
    default:
      return false;
    }
  };

{#--- Validation #}

  function validate{{interface.name}}Request(messageValidator) {
{%- if not(interface.methods) %}
    return validator.validationError.NONE;
{%- else %}
    var message = messageValidator.message;
    var paramsClass = null;
    switch (message.getName()) {
{%-   for method in interface.methods %}
{%-     set interface_method_id =
            interface.mojom_name ~ "_" ~ method.mojom_name %}
      case k{{interface_method_id}}_Name:
{%-     if method.response_parameters == None %}
        if (!message.expectsResponse() && !message.isResponse())
          paramsClass = {{interface_method_id}}_Params;
{%-     else %}
        if (message.expectsResponse())
          paramsClass = {{interface_method_id}}_Params;
{%-     endif %}
      break;
{%-   endfor %}
    }
    if (paramsClass === null)
      return validator.validationError.NONE;
    return paramsClass.validate(messageValidator, messageValidator.message.getHeaderNumBytes());
{%- endif %}
  }

  function validate{{interface.name}}Response(messageValidator) {
{%- if not(interface|has_callbacks) %}
    return validator.validationError.NONE;
{%- else %}
   var message = messageValidator.message;
   var paramsClass = null;
   switch (message.getName()) {
{%-   for method in interface.methods %}
{%-     set interface_method_id =
            interface.mojom_name ~ "_" ~ method.mojom_name %}
{%-     if method.response_parameters != None %}
      case k{{interface_method_id}}_Name:
        if (message.isResponse())
          paramsClass = {{interface_method_id}}_ResponseParams;
        break;
{%-     endif %}
{%-   endfor %}
    }
    if (paramsClass === null)
      return validator.validationError.NONE;
    return paramsClass.validate(messageValidator, messageValidator.message.getHeaderNumBytes());
{%- endif %}
  }

  var {{interface.name}} = {
    name: '{{module.mojom_namespace}}.{{interface.mojom_name}}',
    kVersion: {{interface.version}},
    ptrClass: {{interface.name}}Ptr,
    proxyClass: {{interface.name}}Proxy,
    stubClass: {{interface.name}}Stub,
    validateRequest: validate{{interface.name}}Request,
{%- if interface|has_callbacks %}
    validateResponse: validate{{interface.name}}Response,
{%- else %}
    validateResponse: null,
{%- endif %}
{%- if generate_fuzzing %}
    mojomId: '{{module.path}}',
    fuzzMethods: {
      {%- for method in interface.methods %}
      {%-   set interface_method_id =
                interface.mojom_name ~ "_" ~ method.mojom_name %}
      {{ method.name }}: {
        params: {{interface_method_id}}_Params,
      },
      {%- endfor %}
    },
{%- endif %}
  };
{#--- Interface Constants #}
{%- for constant in interface.constants %}
  {{interface.name}}.{{constant.name}} = {{constant.value|expression_to_text}},
{%-   endfor %}
{#--- Interface Enums #}
{%- from "enum_definition.tmpl" import enum_def -%}
{%- for enum in interface.enums %}
  {{ enum_def("%s.%s"|format(interface.name, enum.name), enum) }}
{%-  endfor %}
  {{interface.name}}Stub.prototype.validator = validate{{interface.name}}Request;
{%- if interface|has_callbacks %}
  {{interface.name}}Proxy.prototype.validator = validate{{interface.name}}Response;
{%- else %}
  {{interface.name}}Proxy.prototype.validator = null;
{%- endif %}