Commit 3ddf08d2 authored by Ken Rockot's avatar Ken Rockot Committed by Commit Bot

Duplicate JS lite templates for modules

An upcoming change will introduce generation of JS modules for mojom
bindings. While most of the boilerplate in the existing jinja templates
remains the same, there are enough differences to warrant forking them.

This CL copies existing templates to new files with a _for_module
filename suffix. No changes are made here, so that the CL which actually
implements JS modules support can change the new files in-place, making
the differences easier to review.

Bug: 1004256
Change-Id: I37b95929b070ed6cb41406cc529f7dba84065720
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2451270
Auto-Submit: Ken Rockot <rockot@google.com>
Commit-Queue: Oksana Zhuravlova <oksamyt@chromium.org>
Reviewed-by: default avatarOksana Zhuravlova <oksamyt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814256}
parent 333407cb
{%- macro enum_def(enum_spec_parent, enum_parent, enum) -%}
{# TODO: Less generic enum annotations would be nice. We do it this way because
the bindings generator is still too stupid to generate the right integral
constants directly. #}
{%- set enum_name = enum|lite_closure_type -%}
{% if generate_closure_exports -%}
goog.provide('{{enum_name}}');
goog.provide('{{enum_spec_parent}}.{{enum.name}}Spec');
{%- endif %}
/**
* @const { {$: !mojo.internal.MojomType} }
* @export
*/
{{enum_spec_parent}}.{{enum.name}}Spec = { $: mojo.internal.Enum() };
/**
* @enum {number}
* @export
*/
{{enum_name}} = {
{# Set up the enum here, but fill out the values later. #}
{%- for field in enum.fields %}
{{field.name}}: {{field.numeric_value}},
{%- endfor %}
{%- if enum.min_value is not none %}
MIN_VALUE: {{enum.min_value}},
{%- endif %}
{%- if enum.max_value is not none %}
MAX_VALUE: {{enum.max_value}},
{%- endif %}
};
{%- endmacro %}
{% 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} handle */
constructor(handle) {
/** @public {!MojoHandle} */
this.handle = handle;
}
};
{% 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=} opt_handle */
constructor(opt_handle) {
/**
* @private {!mojo.internal.interfaceSupport.InterfaceRemoteBase<!{{module.namespace}}.{{interface.name}}PendingReceiver>}
*/
this.proxy =
new mojo.internal.interfaceSupport.InterfaceRemoteBase(
{{module.namespace}}.{{interface.name}}PendingReceiver,
opt_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;
Mojo.bindInterface({{module.namespace}}.{{interface.name}}.$interfaceName,
remote.$.bindNewPipeAndPassReceiver().handle);
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);
}
};
{% for constant in struct.constants %}
/**
* @const { {{constant.kind|lite_closure_type_with_nullability}} }
* @export
*/
{{module.namespace}}.{{struct.name}}_{{constant.name}} =
{{constant.value|expression_to_text_lite}};
{% endfor %}
{%- from "lite/enum_definition.tmpl" import enum_def with context %}
{% for enum in struct.enums %}
{{enum_def("%s.%sSpec"|format(module.namespace, struct.name),
"%s.%s"|format(module.namespace, struct.name), enum)}}
{% endfor %}
mojo.internal.Struct(
{{module.namespace}}.{{struct.name}}Spec.$,
'{{struct.name}}',
{{struct.packed|payload_size}},
[
{%- for packed_field in struct.packed.packed_fields_in_ordinal_order %}
mojo.internal.StructField(
'{{packed_field.field.name}}', {{packed_field.offset}},
{% if packed_field.field.kind|is_bool_kind %}{{packed_field.bit}}
{%- else %}0{% endif %},
{{packed_field.field.kind|lite_js_type}},
{{packed_field.field|lite_default_value}},
{%- if packed_field.field.kind.is_nullable %}
true /* nullable */),
{%- else %}
false /* nullable */),
{%- endif %}
{%- endfor %}
]);
{% if generate_struct_deserializers %}
{{module.namespace}}.{{struct.name}}_Deserialize =
mojo.internal.createStructDeserializer({{module.namespace}}.{{struct.name}}Spec.$);
{% endif %}
{% if generate_closure_exports -%}
goog.provide('{{module.namespace}}.{{struct.name}}');
/** @record */
{{module.namespace}}.{{struct.name}} = class {
constructor() {
{%- for packed_field in struct.packed.packed_fields %}
/** @export { {{packed_field.field.kind|lite_closure_field_type}} } */
this.{{packed_field.field.name}};
{%- endfor %}
}
};
{%- endif %}
{% if generate_closure_exports -%}
goog.provide('{{module.namespace}}.{{union.name}}');
{% endif %}
mojo.internal.Union(
{{module.namespace}}.{{union.name}}Spec.$, '{{union.name}}',
{
{%- for field in union.fields %}
'{{field.name}}': {
'ordinal': {{field.ordinal}},
'type': {{field.kind|lite_js_type}},
{%- if field.kind.is_nullable %}
'nullable': true,
{%- endif %}
},
{%- endfor %}
});
/**
* @typedef { {
{%- for field in union.fields %}
* {{field.name}}: ({{field.kind|lite_closure_type_with_nullability}}|undefined),
{%- endfor %}
* } }
*/
{{module.namespace}}.{{union.name}};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment