Chromium Code Reviews| Index: mojo/public/tools/bindings/generators/java_templates/interface_definition.tmpl |
| diff --git a/mojo/public/tools/bindings/generators/java_templates/interface_definition.tmpl b/mojo/public/tools/bindings/generators/java_templates/interface_definition.tmpl |
| index c770bd82bc440d6755d4a2a4f037291d8a0160b1..56812c30ea71f016f31c05fdb07817749591ad2a 100644 |
| --- a/mojo/public/tools/bindings/generators/java_templates/interface_definition.tmpl |
| +++ b/mojo/public/tools/bindings/generators/java_templates/interface_definition.tmpl |
| @@ -1,9 +1,10 @@ |
| {% from "constant_definition.tmpl" import constant_def %} |
| {% from "enum_definition.tmpl" import enum_def %} |
| +{% from "struct_definition.tmpl" import struct_def %} |
| -{%- macro declare_params(parameters) %} |
| +{%- macro declare_params(parameters, boxed=false) %} |
| {%- for param in parameters -%} |
| -{{param.kind|java_type(False)}} {{param|name}} |
| +{{param.kind|java_type(boxed)}} {{param|name}} |
| {%- if not loop.last %}, {% endif %} |
| {%- endfor %} |
| {%- endmacro %} |
| @@ -17,6 +18,7 @@ |
| {% endmacro %} |
| {%- macro declare_callback(method) -%} |
| + |
| interface {{method|interface_response_name}} extends org.chromium.mojo.bindings.Callbacks.Callback{{method.response_parameters|length}}< |
| {%- for param in method.response_parameters -%} |
| {{param.kind|java_type(True)}} |
| @@ -25,19 +27,101 @@ interface {{method|interface_response_name}} extends org.chromium.mojo.bindings. |
| > { } |
| {%- endmacro -%} |
| -{%- macro super_class(client) -%} |
| +{%- macro run_callback(variable, parameters) -%} |
| +{%- if parameters -%} |
| +{%- for param in parameters -%} |
| +{{variable}}.{{param|name}} |
| +{%- if not loop.last %}, {% endif %} |
| +{%- endfor -%} |
| +{%- endif -%} |
| +{%- endmacro -%} |
| + |
| +{%- macro super_class(client, with_generic=True) -%} |
| {%- if client -%} |
| -org.chromium.mojo.bindings.InterfaceWithClient<{{client|java_type}}> |
| +org.chromium.mojo.bindings.InterfaceWithClient{% if with_generic %}<{{client|java_type}}>{% endif %} |
| {%- else -%} |
| org.chromium.mojo.bindings.Interface |
| {%- endif -%} |
| {%- endmacro -%} |
| -{% macro interface_def(interface, client) %} |
| -public interface {{interface|name}} extends {{super_class(client)}} { |
| +{%- macro flags(method, is_parameter) -%} |
| +{%- if not method.response_parameters -%} |
| +0 |
| +{%- elif is_parameter: -%} |
| +org.chromium.mojo.bindings.MessageHeader.MESSAGE_EXPECTS_RESPONSE_FLAG |
| +{%- else -%} |
| +org.chromium.mojo.bindings.MessageHeader.MESSAGE_IS_RESPONSE_FLAG |
| +{%- endif -%} |
| +{%- endmacro -%} |
| + |
| +{%- macro builder_class(interface, client, fqbc=False) -%} |
| +{% if fqbc %}{{super_class(client, False)}}.{% endif %}Builder<{{interface|name}}, {{interface|name}}.Proxy |
| +{%- if client -%}, {{client|java_type}}{%- endif -%} |
| +> |
| +{%- endmacro -%} |
| + |
| +{%- macro builder_def(interface, client) -%} |
| +public static final {{builder_class(interface, client, True)}} BUILDER = |
| + new {{builder_class(interface, client, True)}}() { |
| + |
| + public Proxy buildProxy(org.chromium.mojo.system.Core core, |
| + org.chromium.mojo.bindings.MessageReceiverWithResponder messageReceiver) { |
| + return new Proxy(core, messageReceiver); |
| + } |
| + |
| + public Stub buildStub(org.chromium.mojo.system.Core core, {{interface|name}} impl) { |
| + return new Stub(core, impl); |
| + } |
| - public static final Object BUILDER = new Object(); |
| + public {{interface|name}}[] newArray(int size) { |
| + return new {{interface|name}}[size]; |
| + } |
| +{% if client %} |
| + protected org.chromium.mojo.bindings.Interface.Builder<{{client|java_type}}, ?> getClientBuilder() { |
| + return {{client|java_type}}.BUILDER; |
| + } |
| +{% endif %} |
| +}; |
| +{%- endmacro -%} |
| + |
| +{%- macro accept_body(interface, with_response) -%} |
| +{% if (interface|has_method_with_response and with_response) or |
| + (interface|has_method_without_response and not with_response) %} |
| +try { |
| + org.chromium.mojo.bindings.MessageHeader header = message.getHeader(); |
| + switch(header.getType()) { |
| +{% for method in interface.methods %} |
| +{% if (with_response and method.response_parameters) or |
| + (not with_response and not method.response_parameters) %} |
| +{% set request_struct = method|struct_from_method %} |
| +{% if with_response %} |
| +{% set response_struct = method|response_struct_from_method %} |
| +{% endif %} |
| + case {{method|method_ordinal_name}}: { |
| + if (!header.validateHeader({{flags(method, True)}})) { |
| + return false; |
| + } |
| +{% if method.parameters %} |
| + {{request_struct|name}} data = |
| + {{request_struct|name}}.deserialize(message.getPayload()); |
| +{% else %} |
| + {{request_struct|name}}.deserialize(message.getPayload()); |
| +{% endif %} |
| + getImpl().{{method|name}}({{run_callback('data', method.parameters)}}{% if with_response %}{% if method.parameters %}, {% endif %}new {{response_struct|name}}ProxyToResponder(getCore(), receiver, header.getRequestId()){% endif %}); |
| + return true; |
| + } |
| +{% endif %} |
| +{% endfor %} |
| + } |
| +} catch (org.chromium.mojo.bindings.DeserializationException e) { |
| +} |
| +{% endif %} |
| +return false; |
| +{%- endmacro -%} |
| + |
| +{% macro interface_def(interface, client) %} |
| +public interface {{interface|name}} extends {{super_class(client)}} { |
| {% for constant in interface.constants %} |
| {{constant_def(constant)|indent(4)}} |
| @@ -46,6 +130,11 @@ public interface {{interface|name}} extends {{super_class(client)}} { |
| {{enum_def(enum, false)|indent(4)}} |
| {% endfor %} |
| + |
| + public interface Proxy extends {{interface|name}}, {{super_class(client, False)}}.Proxy{% if client %}<{{client|java_type}}>{% endif %} { |
| + } |
| + |
| + {{builder_class(interface, client)}} BUILDER = {{interface|name}}Internal.BUILDER; |
| {% for method in interface.methods %} |
| void {{method|name}}({{declare_request_params(method)}}); |
| @@ -53,6 +142,135 @@ public interface {{interface|name}} extends {{super_class(client)}} { |
| {{declare_callback(method)|indent(4)}} |
| {% endif %} |
| {% endfor %} |
| +} |
| +{% endmacro %} |
| + |
| +{% macro interface_internal_def(interface, client) %} |
| +class {{interface|name}}Internal { |
|
rmcilroy
2014/08/21 14:43:52
Are you sure this is safe to do? What if there is
qsr
2014/08/22 08:11:19
It would be possible, but the reason I put it ther
rmcilroy
2014/08/26 10:18:23
That makes sense. I could still see clients wanti
qsr
2014/08/26 12:43:56
The issue with this is that all internal class are
rmcilroy
2014/08/26 17:04:03
In that case it's probably better to be in the sam
qsr
2014/08/27 07:32:26
Changed to Baz_Internal. It is not possible to gen
|
| + |
| + {{builder_def(interface, client)|indent(4)}} |
| + |
| +{% for method in interface.methods %} |
| + private static final int {{method|method_ordinal_name}} = {{method.ordinal}}; |
| +{% endfor %} |
| + |
| + static final class Proxy extends {% if client %}org.chromium.mojo.bindings.InterfaceWithClient.AbstractProxy<{{client|java_type}}>{% else %}org.chromium.mojo.bindings.Interface.AbstractProxy{% endif %} implements {{interface|name}}.Proxy { |
| + |
| + Proxy(org.chromium.mojo.system.Core core, |
| + org.chromium.mojo.bindings.MessageReceiverWithResponder messageReceiver) { |
| + super(core, messageReceiver); |
| + } |
| +{% for method in interface.methods %} |
| + |
| + @Override |
| + public void {{method|name}}({{declare_request_params(method)}}) { |
| +{% set request_struct = method|struct_from_method %} |
| + {{request_struct|name}} message = new {{request_struct|name}}(); |
| +{% for param in method.parameters %} |
| + message.{{param|name}} = {{param|name}}; |
| +{% endfor %} |
| +{% if method.response_parameters %} |
| + getMessageReceiver().acceptWithResponder( |
| + message.serializeWithHeader( |
| + getCore(), |
| + new org.chromium.mojo.bindings.MessageHeader( |
| + {{method|method_ordinal_name}}, |
| + {{flags(method, True)}}, |
| + 0)), |
| + new {{method|response_struct_from_method|name}}ForwardToCallback(callback)); |
| +{% else %} |
| + getMessageReceiver().accept( |
| + message.serializeWithHeader( |
| + getCore(), |
| + new org.chromium.mojo.bindings.MessageHeader({{method|method_ordinal_name}}))); |
| +{% endif %} |
| + } |
| +{% endfor %} |
| + |
| + } |
| + |
| + static final class Stub extends org.chromium.mojo.bindings.Interface.Stub<{{interface|name}}> { |
| + |
| + Stub(org.chromium.mojo.system.Core core, {{interface|name}} impl) { |
| + super(core, impl); |
| + } |
| + |
| + @Override |
| + public boolean accept(org.chromium.mojo.bindings.MessageWithHeader message) { |
| + {{accept_body(interface, False)|indent(12)}} |
| + } |
| + |
| + @Override |
| + public boolean acceptWithResponder(org.chromium.mojo.bindings.MessageWithHeader message, org.chromium.mojo.bindings.MessageReceiver receiver) { |
| + {{accept_body(interface, True)|indent(12)}} |
| + } |
| + } |
| +{% for method in interface.methods %} |
| + |
| + {{ struct_def(method|struct_from_method, True)|indent(4) }} |
| +{% if method.response_parameters %} |
| +{% set response_struct = method|response_struct_from_method %} |
| + |
| + {{ struct_def(response_struct, True)|indent(4) }} |
| + |
| + static class {{response_struct|name}}ForwardToCallback extends org.chromium.mojo.bindings.SideEffectFreeCloseable |
| + implements org.chromium.mojo.bindings.MessageReceiver { |
| + private final {{interface|name}}.{{method|interface_response_name}} mCallback; |
| + |
| + {{response_struct|name}}ForwardToCallback({{interface|name}}.{{method|interface_response_name}} callback) { |
| + this.mCallback = callback; |
| + } |
| + |
| + @Override |
| + public boolean accept(org.chromium.mojo.bindings.MessageWithHeader message) { |
| + try { |
| + org.chromium.mojo.bindings.MessageHeader header = message.getHeader(); |
| + if (!header.validateHeader({{method|method_ordinal_name}}, |
| + {{flags(method, False)}})) { |
| + return false; |
| + } |
| + {{response_struct|name}} response = {{response_struct|name}}.deserialize(message.getPayload()); |
| + mCallback.call({{run_callback('response', method.response_parameters)}}); |
| + return true; |
| + } catch (org.chromium.mojo.bindings.DeserializationException e) { |
| + return false; |
| + } |
| + } |
| + } |
| + |
| + static class {{response_struct|name}}ProxyToResponder implements {{interface|name}}.{{method|interface_response_name}} { |
| + |
| + private final org.chromium.mojo.system.Core mCore; |
| + private final org.chromium.mojo.bindings.MessageReceiver mMessageReceiver; |
| + private final long mRequestId; |
| + |
| + {{response_struct|name}}ProxyToResponder( |
| + org.chromium.mojo.system.Core core, |
| + org.chromium.mojo.bindings.MessageReceiver messageReceiver, |
| + long requestId) { |
| + mCore = core; |
| + mMessageReceiver = messageReceiver; |
| + mRequestId = requestId; |
| + } |
| + |
| + @Override |
| + public void call({{declare_params(method.response_parameters, true)}}) { |
| + {{response_struct|name}} response = new {{response_struct|name}}(); |
| +{% for param in method.response_parameters %} |
| + response.{{param|name}} = {{param|name}}; |
| +{% endfor %} |
| + org.chromium.mojo.bindings.MessageWithHeader message = |
| + response.serializeWithHeader( |
| + mCore, |
| + new org.chromium.mojo.bindings.MessageHeader( |
| + {{method|method_ordinal_name}}, |
| + {{flags(method, False)}}, |
| + mRequestId)); |
| + mMessageReceiver.accept(message); |
| + } |
| + } |
| +{% endif %} |
| +{% endfor %} |
| } |
| {% endmacro %} |