• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1{% from "constant_definition.tmpl" import constant_def %}
2{% from "enum_definition.tmpl" import enum_def %}
3{% from "data_types_definition.tmpl" import struct_def %}
4
5{%- macro declare_params(parameters, boxed=false) %}
6{%-   for param in parameters -%}
7{{param.kind|java_type(boxed)}} {{param|name}}
8{%- if not loop.last %}, {% endif %}
9{%-   endfor %}
10{%- endmacro %}
11
12{% macro declare_request_params(method) %}
13{{declare_params(method.parameters)}}
14{%-   if method.response_parameters != None -%}
15{%- if method.parameters %}, {% endif %}
16{{method|interface_response_name}} callback
17{%-   endif -%}
18{% endmacro %}
19
20{%- macro declare_callback(method) -%}
21
22interface {{method|interface_response_name}} extends org.chromium.mojo.bindings.Callbacks.Callback{{method.response_parameters|length}}{% if method.response_parameters %}<
23{%-   for param in method.response_parameters -%}
24{{param.kind|java_type(True)}}
25{%- if not loop.last %}, {% endif %}
26{%-   endfor -%}
27>{% endif %} { }
28{%- endmacro -%}
29
30{%- macro run_callback(variable, parameters) -%}
31{%-   if parameters -%}
32{%-     for param in parameters -%}
33{{variable}}.{{param|name}}
34{%-   if not loop.last %}, {% endif %}
35{%-     endfor -%}
36{%-   endif -%}
37{%- endmacro -%}
38
39{%- macro flags_for_method(method, is_request) -%}
40{{flags(method.response_parameters != None, is_request)}}
41{%- endmacro -%}
42
43{%- macro flags(use_response_flag, is_request) -%}
44{%-  if not use_response_flag -%}
45org.chromium.mojo.bindings.MessageHeader.NO_FLAG
46{%-   elif is_request: -%}
47org.chromium.mojo.bindings.MessageHeader.MESSAGE_EXPECTS_RESPONSE_FLAG
48{%-   else -%}
49org.chromium.mojo.bindings.MessageHeader.MESSAGE_IS_RESPONSE_FLAG
50{%-   endif -%}
51{%- endmacro -%}
52
53{%- macro manager_class(interface, fully_qualified=False) -%}
54{% if fully_qualified %}org.chromium.mojo.bindings.Interface.{% endif %}Manager<{{interface|name}}, {{interface|name}}.Proxy>
55{%- endmacro -%}
56
57{%- macro manager_def(interface) -%}
58public static final {{manager_class(interface, True)}} MANAGER =
59        new {{manager_class(interface, True)}}() {
60
61    public String getName() {
62        return "{{namespace|replace(".","::")}}::{{interface.name}}";
63    }
64
65    public int getVersion() {
66      return {{interface.version}};
67    }
68
69    public Proxy buildProxy(org.chromium.mojo.system.Core core,
70                            org.chromium.mojo.bindings.MessageReceiverWithResponder messageReceiver) {
71        return new Proxy(core, messageReceiver);
72    }
73
74    public Stub buildStub(org.chromium.mojo.system.Core core, {{interface|name}} impl) {
75        return new Stub(core, impl);
76    }
77
78    public {{interface|name}}[] buildArray(int size) {
79      return new {{interface|name}}[size];
80    }
81};
82{%- endmacro -%}
83
84{%- macro accept_body(interface, with_response) -%}
85try {
86    org.chromium.mojo.bindings.ServiceMessage messageWithHeader =
87            message.asServiceMessage();
88    org.chromium.mojo.bindings.MessageHeader header = messageWithHeader.getHeader();
89    if (!header.validateHeader({{flags(with_response, True)}})) {
90        return false;
91    }
92    switch(header.getType()) {
93{%   if with_response %}
94        case org.chromium.mojo.bindings.InterfaceControlMessagesConstants.RUN_MESSAGE_ID:
95            return org.chromium.mojo.bindings.InterfaceControlMessagesHelper.handleRun(
96                    getCore(), {{interface|name}}_Internal.MANAGER, messageWithHeader, receiver);
97{%   else %}
98        case org.chromium.mojo.bindings.InterfaceControlMessagesConstants.RUN_OR_CLOSE_PIPE_MESSAGE_ID:
99            return org.chromium.mojo.bindings.InterfaceControlMessagesHelper.handleRunOrClosePipe(
100                    {{interface|name}}_Internal.MANAGER, messageWithHeader);
101{%   endif %}
102{%   for method in interface.methods %}
103{%     if (with_response and method.response_parameters != None) or
104        (not with_response and method.response_parameters == None) %}
105{%       set request_struct = method.param_struct %}
106{%       if with_response %}
107{%         set response_struct = method.response_param_struct %}
108{%       endif %}
109        case {{method|method_ordinal_name}}: {
110{%       if method.parameters %}
111            {{request_struct|name}} data =
112                    {{request_struct|name}}.deserialize(messageWithHeader.getPayload());
113{%       else %}
114            {{request_struct|name}}.deserialize(messageWithHeader.getPayload());
115{%       endif %}
116            try {
117                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 %});
118            } catch (RuntimeException e) {
119                // TODO(lhchavez): Remove this hack. See b/28814913 for details.
120                android.util.Log.wtf("{{namespace}}.{{interface.name}}", "Uncaught runtime exception", e);
121            }
122            return true;
123        }
124{%     endif %}
125{%   endfor %}
126        default:
127            return false;
128    }
129} catch (org.chromium.mojo.bindings.DeserializationException e) {
130    System.err.println(e.toString());
131    return false;
132}
133{%- endmacro -%}
134
135{% macro interface_def(interface) %}
136public interface {{interface|name}} extends org.chromium.mojo.bindings.Interface {
137{%  for constant in interface.constants %}
138
139    {{constant_def(constant)|indent(4)}}
140{%  endfor %}
141{%  for enum in interface.enums %}
142
143    {{enum_def(enum, false)|indent(4)}}
144{% endfor %}
145
146    public interface Proxy extends {{interface|name}}, org.chromium.mojo.bindings.Interface.Proxy {
147    }
148
149    {{manager_class(interface)}} MANAGER = {{interface|name}}_Internal.MANAGER;
150{% for method in interface.methods %}
151
152    void {{method|name}}({{declare_request_params(method)}});
153{%   if method.response_parameters != None %}
154    {{declare_callback(method)|indent(4)}}
155{%   endif %}
156{% endfor %}
157}
158{% endmacro %}
159
160{% macro interface_internal_def(interface) %}
161class {{interface|name}}_Internal {
162
163    {{manager_def(interface)|indent(4)}}
164
165{% for method in interface.methods %}
166    private static final int {{method|method_ordinal_name}} = {{method.ordinal}};
167{% endfor %}
168
169    static final class Proxy extends org.chromium.mojo.bindings.Interface.AbstractProxy implements {{interface|name}}.Proxy {
170
171        Proxy(org.chromium.mojo.system.Core core,
172              org.chromium.mojo.bindings.MessageReceiverWithResponder messageReceiver) {
173            super(core, messageReceiver);
174        }
175{% for method in interface.methods %}
176
177        @Override
178        public void {{method|name}}({{declare_request_params(method)}}) {
179{%   set request_struct = method.param_struct %}
180            {{request_struct|name}} _message = new {{request_struct|name}}();
181{%   for param in method.parameters %}
182            _message.{{param|name}} = {{param|name}};
183{%   endfor %}
184{%   if method.response_parameters != None %}
185            getProxyHandler().getMessageReceiver().acceptWithResponder(
186                    _message.serializeWithHeader(
187                            getProxyHandler().getCore(),
188                            new org.chromium.mojo.bindings.MessageHeader(
189                                    {{method|method_ordinal_name}},
190                                    {{flags_for_method(method, True)}},
191                                    0)),
192                    new {{method.response_param_struct|name}}ForwardToCallback(callback));
193{%   else %}
194            getProxyHandler().getMessageReceiver().accept(
195                    _message.serializeWithHeader(
196                            getProxyHandler().getCore(),
197                            new org.chromium.mojo.bindings.MessageHeader({{method|method_ordinal_name}})));
198{%   endif %}
199        }
200{% endfor %}
201
202    }
203
204    static final class Stub extends org.chromium.mojo.bindings.Interface.Stub<{{interface|name}}> {
205
206        Stub(org.chromium.mojo.system.Core core, {{interface|name}} impl) {
207            super(core, impl);
208        }
209
210        @Override
211        public boolean accept(org.chromium.mojo.bindings.Message message) {
212            {{accept_body(interface, False)|indent(12)}}
213        }
214
215        @Override
216        public boolean acceptWithResponder(org.chromium.mojo.bindings.Message message, org.chromium.mojo.bindings.MessageReceiver receiver) {
217            {{accept_body(interface, True)|indent(12)}}
218        }
219    }
220{% for method in interface.methods %}
221
222    {{ struct_def(method.param_struct, True)|indent(4) }}
223{%   if method.response_parameters != None %}
224{%   set response_struct = method.response_param_struct %}
225
226    {{ struct_def(response_struct, True)|indent(4) }}
227
228    static class {{response_struct|name}}ForwardToCallback extends org.chromium.mojo.bindings.SideEffectFreeCloseable
229            implements org.chromium.mojo.bindings.MessageReceiver {
230        private final {{interface|name}}.{{method|interface_response_name}} mCallback;
231
232        {{response_struct|name}}ForwardToCallback({{interface|name}}.{{method|interface_response_name}} callback) {
233            this.mCallback = callback;
234        }
235
236        @Override
237        public boolean accept(org.chromium.mojo.bindings.Message message) {
238            try {
239                org.chromium.mojo.bindings.ServiceMessage messageWithHeader =
240                        message.asServiceMessage();
241                org.chromium.mojo.bindings.MessageHeader header = messageWithHeader.getHeader();
242                if (!header.validateHeader({{method|method_ordinal_name}},
243                                           {{flags_for_method(method, False)}})) {
244                    return false;
245                }
246{%   if method.response_parameters|length %}
247                {{response_struct|name}} response = {{response_struct|name}}.deserialize(messageWithHeader.getPayload());
248{%   endif %}
249                mCallback.call({{run_callback('response', method.response_parameters)}});
250                return true;
251            } catch (org.chromium.mojo.bindings.DeserializationException e) {
252                return false;
253            }
254        }
255    }
256
257    static class {{response_struct|name}}ProxyToResponder implements {{interface|name}}.{{method|interface_response_name}} {
258
259        private final org.chromium.mojo.system.Core mCore;
260        private final org.chromium.mojo.bindings.MessageReceiver mMessageReceiver;
261        private final long mRequestId;
262
263        {{response_struct|name}}ProxyToResponder(
264                org.chromium.mojo.system.Core core,
265                org.chromium.mojo.bindings.MessageReceiver messageReceiver,
266                long requestId) {
267            mCore = core;
268            mMessageReceiver = messageReceiver;
269            mRequestId = requestId;
270        }
271
272        @Override
273        public void call({{declare_params(method.response_parameters, true)}}) {
274            {{response_struct|name}} _response = new {{response_struct|name}}();
275{%   for param in method.response_parameters %}
276            _response.{{param|name}} = {{param|name}};
277{%   endfor %}
278            org.chromium.mojo.bindings.ServiceMessage _message =
279                    _response.serializeWithHeader(
280                            mCore,
281                            new org.chromium.mojo.bindings.MessageHeader(
282                                    {{method|method_ordinal_name}},
283                                    {{flags_for_method(method, False)}},
284                                    mRequestId));
285            mMessageReceiver.accept(_message);
286        }
287    }
288{%   endif %}
289{% endfor %}
290
291}
292{% endmacro %}
293