OLD | NEW |
1 {% from "constant_definition.tmpl" import constant_def %} | 1 {% from "constant_definition.tmpl" import constant_def %} |
2 {% from "enum_definition.tmpl" import enum_def %} | 2 {% from "enum_definition.tmpl" import enum_def %} |
3 {% from "struct_definition.tmpl" import struct_def %} | |
4 | 3 |
5 {%- macro declare_params(parameters, boxed=false) %} | 4 {%- macro declare_params(parameters) %} |
6 {%- for param in parameters -%} | 5 {%- for param in parameters -%} |
7 {{param.kind|java_type(boxed)}} {{param|name}} | 6 {{param.kind|java_type(False)}} {{param|name}} |
8 {%- if not loop.last %}, {% endif %} | 7 {%- if not loop.last %}, {% endif %} |
9 {%- endfor %} | 8 {%- endfor %} |
10 {%- endmacro %} | 9 {%- endmacro %} |
11 | 10 |
12 {% macro declare_request_params(method) %} | 11 {% macro declare_request_params(method) %} |
13 {{declare_params(method.parameters)}} | 12 {{declare_params(method.parameters)}} |
14 {%- if method.response_parameters != None -%} | 13 {%- if method.response_parameters != None -%} |
15 {%- if method.parameters %}, {% endif %} | 14 {%- if method.parameters %}, {% endif %} |
16 {{method|interface_response_name}} callback | 15 {{method|interface_response_name}} callback |
17 {%- endif -%} | 16 {%- endif -%} |
18 {% endmacro %} | 17 {% endmacro %} |
19 | 18 |
20 {%- macro declare_callback(method) -%} | 19 {%- macro declare_callback(method) -%} |
21 | |
22 interface {{method|interface_response_name}} extends org.chromium.mojo.bindings.
Callbacks.Callback{{method.response_parameters|length}}< | 20 interface {{method|interface_response_name}} extends org.chromium.mojo.bindings.
Callbacks.Callback{{method.response_parameters|length}}< |
23 {%- for param in method.response_parameters -%} | 21 {%- for param in method.response_parameters -%} |
24 {{param.kind|java_type(True)}} | 22 {{param.kind|java_type(True)}} |
25 {%- if not loop.last %}, {% endif %} | 23 {%- if not loop.last %}, {% endif %} |
26 {%- endfor -%} | 24 {%- endfor -%} |
27 > { } | 25 > { } |
28 {%- endmacro -%} | 26 {%- endmacro -%} |
29 | 27 |
30 {%- macro run_callback(variable, parameters) -%} | 28 {%- macro super_class(client) -%} |
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 super_class(client, with_generic=True) -%} | |
40 {%- if client -%} | 29 {%- if client -%} |
41 org.chromium.mojo.bindings.InterfaceWithClient{% if with_generic %}<{{client|jav
a_type}}>{% endif %} | 30 org.chromium.mojo.bindings.InterfaceWithClient<{{client|java_type}}> |
42 {%- else -%} | 31 {%- else -%} |
43 org.chromium.mojo.bindings.Interface | 32 org.chromium.mojo.bindings.Interface |
44 {%- endif -%} | 33 {%- endif -%} |
45 {%- endmacro -%} | 34 {%- endmacro -%} |
46 | 35 |
47 {%- macro flags_for_method(method, is_parameter) -%} | |
48 {{flags(method.response_parameters, is_parameter)}} | |
49 {%- endmacro -%} | |
50 | |
51 {%- macro flags(has_response_parameters, is_parameter) -%} | |
52 {%- if not has_response_parameters -%} | |
53 org.chromium.mojo.bindings.MessageHeader.NO_FLAG | |
54 {%- elif is_parameter: -%} | |
55 org.chromium.mojo.bindings.MessageHeader.MESSAGE_EXPECTS_RESPONSE_FLAG | |
56 {%- else -%} | |
57 org.chromium.mojo.bindings.MessageHeader.MESSAGE_IS_RESPONSE_FLAG | |
58 {%- endif -%} | |
59 {%- endmacro -%} | |
60 | |
61 {%- macro manager_class(interface, client, fully_qualified=False) -%} | |
62 {% if fully_qualified %}{{super_class(client, False)}}.{% endif %}Manager<{{inte
rface|name}}, {{interface|name}}.Proxy | |
63 {%- if client -%}, {{client|java_type}}{%- endif -%} | |
64 > | |
65 {%- endmacro -%} | |
66 | |
67 {%- macro manager_def(interface, client) -%} | |
68 public static final {{manager_class(interface, client, True)}} MANAGER = | |
69 new {{manager_class(interface, client, True)}}() { | |
70 | |
71 public Proxy buildProxy(org.chromium.mojo.system.Core core, | |
72 org.chromium.mojo.bindings.MessageReceiverWithRespon
der messageReceiver) { | |
73 return new Proxy(core, messageReceiver); | |
74 } | |
75 | |
76 public Stub buildStub(org.chromium.mojo.system.Core core, {{interface|name}}
impl) { | |
77 return new Stub(core, impl); | |
78 } | |
79 | |
80 public {{interface|name}}[] buildArray(int size) { | |
81 return new {{interface|name}}[size]; | |
82 } | |
83 {% if client %} | |
84 | |
85 protected org.chromium.mojo.bindings.Interface.Manager<{{client|java_type}},
?> getClientManager() { | |
86 return {{client|java_type}}.MANAGER; | |
87 } | |
88 {% endif %} | |
89 }; | |
90 {%- endmacro -%} | |
91 | |
92 {%- macro accept_body(interface, with_response) -%} | |
93 {% if (interface|has_method_with_response and with_response) or | |
94 (interface|has_method_without_response and not with_response) %} | |
95 try { | |
96 org.chromium.mojo.bindings.MessageHeader header = message.getHeader(); | |
97 if (!header.validateHeader({{flags(with_response, True)}})) { | |
98 return false; | |
99 } | |
100 switch(header.getType()) { | |
101 {% for method in interface.methods %} | |
102 {% if (with_response and method.response_parameters) or | |
103 (not with_response and not method.response_parameters) %} | |
104 {% set request_struct = method|struct_from_method %} | |
105 {% if with_response %} | |
106 {% set response_struct = method|response_struct_from_method %} | |
107 {% endif %} | |
108 case {{method|method_ordinal_name}}: { | |
109 {% if method.parameters %} | |
110 {{request_struct|name}} data = | |
111 {{request_struct|name}}.deserialize(message.getPayload()); | |
112 {% else %} | |
113 {{request_struct|name}}.deserialize(message.getPayload()); | |
114 {% endif %} | |
115 getImpl().{{method|name}}({{run_callback('data', method.parameters)}
}{% if with_response %}{% if method.parameters %}, {% endif %}new {{response_str
uct|name}}ProxyToResponder(getCore(), receiver, header.getRequestId()){% endif %
}); | |
116 return true; | |
117 } | |
118 {% endif %} | |
119 {% endfor %} | |
120 default: | |
121 return false; | |
122 } | |
123 } catch (org.chromium.mojo.bindings.DeserializationException e) { | |
124 return false; | |
125 } | |
126 {% else %} | |
127 return false; | |
128 {% endif %} | |
129 {%- endmacro -%} | |
130 | |
131 {% macro interface_def(interface, client) %} | 36 {% macro interface_def(interface, client) %} |
132 public interface {{interface|name}} extends {{super_class(client)}} { | 37 public interface {{interface|name}} extends {{super_class(client)}} { |
| 38 |
| 39 public static final Object BUILDER = new Object(); |
| 40 |
133 {% for constant in interface.constants %} | 41 {% for constant in interface.constants %} |
134 | 42 |
135 {{constant_def(constant)|indent(4)}} | 43 {{constant_def(constant)|indent(4)}} |
136 {% endfor %} | 44 {% endfor %} |
137 {% for enum in interface.enums %} | 45 {% for enum in interface.enums %} |
138 | 46 |
139 {{enum_def(enum, false)|indent(4)}} | 47 {{enum_def(enum, false)|indent(4)}} |
140 {% endfor %} | 48 {% endfor %} |
141 | |
142 public interface Proxy extends {{interface|name}}, {{super_class(client, Fal
se)}}.Proxy{% if client %}<{{client|java_type}}>{% endif %} { | |
143 } | |
144 | |
145 {{manager_class(interface, client)}} MANAGER = {{interface|name}}_Internal.M
ANAGER; | |
146 {% for method in interface.methods %} | 49 {% for method in interface.methods %} |
147 | 50 |
148 void {{method|name}}({{declare_request_params(method)}}); | 51 void {{method|name}}({{declare_request_params(method)}}); |
149 {% if method.response_parameters %} | 52 {% if method.response_parameters %} |
150 {{declare_callback(method)|indent(4)}} | 53 {{declare_callback(method)|indent(4)}} |
151 {% endif %} | 54 {% endif %} |
152 {% endfor %} | 55 {% endfor %} |
153 } | |
154 {% endmacro %} | |
155 | |
156 {% macro interface_internal_def(interface, client) %} | |
157 class {{interface|name}}_Internal { | |
158 | |
159 {{manager_def(interface, client)|indent(4)}} | |
160 | |
161 {% for method in interface.methods %} | |
162 private static final int {{method|method_ordinal_name}} = {{method.ordinal}}
; | |
163 {% endfor %} | |
164 | |
165 static final class Proxy extends {% if client %}org.chromium.mojo.bindings.I
nterfaceWithClient.AbstractProxy<{{client|java_type}}>{% else %}org.chromium.moj
o.bindings.Interface.AbstractProxy{% endif %} implements {{interface|name}}.Prox
y { | |
166 | |
167 Proxy(org.chromium.mojo.system.Core core, | |
168 org.chromium.mojo.bindings.MessageReceiverWithResponder messageRec
eiver) { | |
169 super(core, messageReceiver); | |
170 } | |
171 {% for method in interface.methods %} | |
172 | |
173 @Override | |
174 public void {{method|name}}({{declare_request_params(method)}}) { | |
175 {% set request_struct = method|struct_from_method %} | |
176 {{request_struct|name}} message = new {{request_struct|name}}(); | |
177 {% for param in method.parameters %} | |
178 message.{{param|name}} = {{param|name}}; | |
179 {% endfor %} | |
180 {% if method.response_parameters %} | |
181 getMessageReceiver().acceptWithResponder( | |
182 message.serializeWithHeader( | |
183 getCore(), | |
184 new org.chromium.mojo.bindings.MessageHeader( | |
185 {{method|method_ordinal_name}}, | |
186 {{flags_for_method(method, True)}}, | |
187 0)), | |
188 new {{method|response_struct_from_method|name}}ForwardToCall
back(callback)); | |
189 {% else %} | |
190 getMessageReceiver().accept( | |
191 message.serializeWithHeader( | |
192 getCore(), | |
193 new org.chromium.mojo.bindings.MessageHeader({{metho
d|method_ordinal_name}}))); | |
194 {% endif %} | |
195 } | |
196 {% endfor %} | |
197 | |
198 } | |
199 | |
200 static final class Stub extends org.chromium.mojo.bindings.Interface.Stub<{{
interface|name}}> { | |
201 | |
202 Stub(org.chromium.mojo.system.Core core, {{interface|name}} impl) { | |
203 super(core, impl); | |
204 } | |
205 | |
206 @Override | |
207 public boolean accept(org.chromium.mojo.bindings.MessageWithHeader messa
ge) { | |
208 {{accept_body(interface, False)|indent(12)}} | |
209 } | |
210 | |
211 @Override | |
212 public boolean acceptWithResponder(org.chromium.mojo.bindings.MessageWit
hHeader message, org.chromium.mojo.bindings.MessageReceiver receiver) { | |
213 {{accept_body(interface, True)|indent(12)}} | |
214 } | |
215 } | |
216 {% for method in interface.methods %} | |
217 | |
218 {{ struct_def(method|struct_from_method, True)|indent(4) }} | |
219 {% if method.response_parameters %} | |
220 {% set response_struct = method|response_struct_from_method %} | |
221 | |
222 {{ struct_def(response_struct, True)|indent(4) }} | |
223 | |
224 static class {{response_struct|name}}ForwardToCallback extends org.chromium.
mojo.bindings.SideEffectFreeCloseable | |
225 implements org.chromium.mojo.bindings.MessageReceiver { | |
226 private final {{interface|name}}.{{method|interface_response_name}} mCal
lback; | |
227 | |
228 {{response_struct|name}}ForwardToCallback({{interface|name}}.{{method|in
terface_response_name}} callback) { | |
229 this.mCallback = callback; | |
230 } | |
231 | |
232 @Override | |
233 public boolean accept(org.chromium.mojo.bindings.MessageWithHeader messa
ge) { | |
234 try { | |
235 org.chromium.mojo.bindings.MessageHeader header = message.getHea
der(); | |
236 if (!header.validateHeader({{method|method_ordinal_name}}, | |
237 {{flags_for_method(method, False)}}))
{ | |
238 return false; | |
239 } | |
240 {{response_struct|name}} response = {{response_struct|name}}.des
erialize(message.getPayload()); | |
241 mCallback.call({{run_callback('response', method.response_parame
ters)}}); | |
242 return true; | |
243 } catch (org.chromium.mojo.bindings.DeserializationException e) { | |
244 return false; | |
245 } | |
246 } | |
247 } | |
248 | |
249 static class {{response_struct|name}}ProxyToResponder implements {{interface
|name}}.{{method|interface_response_name}} { | |
250 | |
251 private final org.chromium.mojo.system.Core mCore; | |
252 private final org.chromium.mojo.bindings.MessageReceiver mMessageReceive
r; | |
253 private final long mRequestId; | |
254 | |
255 {{response_struct|name}}ProxyToResponder( | |
256 org.chromium.mojo.system.Core core, | |
257 org.chromium.mojo.bindings.MessageReceiver messageReceiver, | |
258 long requestId) { | |
259 mCore = core; | |
260 mMessageReceiver = messageReceiver; | |
261 mRequestId = requestId; | |
262 } | |
263 | |
264 @Override | |
265 public void call({{declare_params(method.response_parameters, true)}}) { | |
266 {{response_struct|name}} response = new {{response_struct|name}}(); | |
267 {% for param in method.response_parameters %} | |
268 response.{{param|name}} = {{param|name}}; | |
269 {% endfor %} | |
270 org.chromium.mojo.bindings.MessageWithHeader message = | |
271 response.serializeWithHeader( | |
272 mCore, | |
273 new org.chromium.mojo.bindings.MessageHeader( | |
274 {{method|method_ordinal_name}}, | |
275 {{flags_for_method(method, False)}}, | |
276 mRequestId)); | |
277 mMessageReceiver.accept(message); | |
278 } | |
279 } | |
280 {% endif %} | |
281 {% endfor %} | |
282 | 56 |
283 } | 57 } |
284 {% endmacro %} | 58 {% endmacro %} |
OLD | NEW |