OLD | NEW |
1 // This file is generated | 1 // This file is generated |
2 | 2 |
3 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 3 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
4 // Use of this source code is governed by a BSD-style license that can be | 4 // Use of this source code is governed by a BSD-style license that can be |
5 // found in the LICENSE file. | 5 // found in the LICENSE file. |
6 | 6 |
7 #include "{{config.protocol.package}}/{{domain.domain}}.h" | 7 #include "{{config.protocol.package}}/{{domain.domain}}.h" |
8 | 8 |
9 #include "{{config.protocol.package}}/Protocol.h" | 9 #include "{{config.protocol.package}}/Protocol.h" |
10 | 10 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend) | 185 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend) |
186 : DispatcherBase(frontendChannel) | 186 : DispatcherBase(frontendChannel) |
187 , m_backend(backend) { | 187 , m_backend(backend) { |
188 {% for command in domain.commands %} | 188 {% for command in domain.commands %} |
189 {% if "redirect" in command %}{% continue %}{% endif %} | 189 {% if "redirect" in command %}{% continue %}{% endif %} |
190 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} | 190 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} |
191 m_dispatchMap["{{domain.domain}}.{{command.name}}"] = &DispatcherImpl::{
{command.name}}; | 191 m_dispatchMap["{{domain.domain}}.{{command.name}}"] = &DispatcherImpl::{
{command.name}}; |
192 {% endfor %} | 192 {% endfor %} |
193 } | 193 } |
194 ~DispatcherImpl() override { } | 194 ~DispatcherImpl() override { } |
195 void dispatch(int callId, const String& method, std::unique_ptr<protocol::Di
ctionaryValue> messageObject) override; | 195 DispatchResponse::Status dispatch(int callId, const String& method, std::uni
que_ptr<protocol::DictionaryValue> messageObject) override; |
196 | 196 |
197 protected: | 197 protected: |
198 using CallHandler = void (DispatcherImpl::*)(int callId, std::unique_ptr<Dic
tionaryValue> messageObject, ErrorSupport* errors); | 198 using CallHandler = DispatchResponse::Status (DispatcherImpl::*)(int callId,
std::unique_ptr<DictionaryValue> messageObject, ErrorSupport* errors); |
199 using DispatchMap = protocol::HashMap<String, CallHandler>; | 199 using DispatchMap = protocol::HashMap<String, CallHandler>; |
200 DispatchMap m_dispatchMap; | 200 DispatchMap m_dispatchMap; |
201 | 201 |
202 {% for command in domain.commands %} | 202 {% for command in domain.commands %} |
203 {% if "redirect" in command %}{% continue %}{% endif %} | 203 {% if "redirect" in command %}{% continue %}{% endif %} |
204 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} | 204 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} |
205 void {{command.name}}(int callId, std::unique_ptr<DictionaryValue> requestMe
ssageObject, ErrorSupport*); | 205 DispatchResponse::Status {{command.name}}(int callId, std::unique_ptr<Dictio
naryValue> requestMessageObject, ErrorSupport*); |
206 {% endfor %} | 206 {% endfor %} |
207 | 207 |
208 Backend* m_backend; | 208 Backend* m_backend; |
209 }; | 209 }; |
210 | 210 |
211 void DispatcherImpl::dispatch(int callId, const String& method, std::unique_ptr<
protocol::DictionaryValue> messageObject) | 211 DispatchResponse::Status DispatcherImpl::dispatch(int callId, const String& meth
od, std::unique_ptr<protocol::DictionaryValue> messageObject) |
212 { | 212 { |
213 protocol::HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(met
hod); | 213 protocol::HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(met
hod); |
214 if (it == m_dispatchMap.end()) { | 214 if (it == m_dispatchMap.end()) { |
215 reportProtocolError(callId, MethodNotFound, "'" + method + "' wasn't fou
nd", nullptr); | 215 reportProtocolError(callId, DispatchResponse::kMethodNotFound, "'" + met
hod + "' wasn't found", nullptr); |
216 return; | 216 return DispatchResponse::kError; |
217 } | 217 } |
218 | 218 |
219 protocol::ErrorSupport errors; | 219 protocol::ErrorSupport errors; |
220 (this->*(it->second))(callId, std::move(messageObject), &errors); | 220 return (this->*(it->second))(callId, std::move(messageObject), &errors); |
221 } | 221 } |
222 | 222 |
223 {% for command in domain.commands %} | 223 {% for command in domain.commands %} |
224 {% if "redirect" in command %}{% continue %}{% endif %} | 224 {% if "redirect" in command %}{% continue %}{% endif %} |
225 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} | 225 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} |
226 {% if "async" in command %} | 226 {% if "async" in command %} |
227 | 227 |
228 class {{command.name | to_title_case}}CallbackImpl : public Backend::{{command.n
ame | to_title_case}}Callback, public DispatcherBase::Callback { | 228 class {{command.name | to_title_case}}CallbackImpl : public Backend::{{command.n
ame | to_title_case}}Callback, public DispatcherBase::Callback { |
229 public: | 229 public: |
230 {{command.name | to_title_case}}CallbackImpl(std::unique_ptr<DispatcherBase:
:WeakPtr> backendImpl, int callId) | 230 {{command.name | to_title_case}}CallbackImpl(std::unique_ptr<DispatcherBase:
:WeakPtr> backendImpl, int callId) |
231 : DispatcherBase::Callback(std::move(backendImpl), callId) { } | 231 : DispatcherBase::Callback(std::move(backendImpl), callId) { } |
232 | 232 |
233 void sendSuccess( | 233 void sendSuccess( |
234 {%- for parameter in command.returns -%} | 234 {%- for parameter in command.returns -%} |
235 {%- if "optional" in parameter -%} | 235 {%- if "optional" in parameter -%} |
236 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}} | 236 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}} |
237 {%- else -%} | 237 {%- else -%} |
238 {{resolve_type(parameter).pass_type}} {{parameter.name}} | 238 {{resolve_type(parameter).pass_type}} {{parameter.name}} |
239 {%- endif -%} | 239 {%- endif -%} |
240 {%- if not loop.last -%}, {% endif -%} | 240 {%- if not loop.last -%}, {% endif -%} |
241 {%- endfor -%}) override | 241 {%- endfor -%}) override |
242 { | 242 { |
243 std::unique_ptr<protocol::DictionaryValue> resultObject = DictionaryValu
e::create(); | 243 std::unique_ptr<protocol::DictionaryValue> resultObject = DictionaryValu
e::create(); |
244 {% for parameter in command.returns %} | 244 {% for parameter in command.returns %} |
245 {% if "optional" in parameter %} | 245 {% if "optional" in parameter %} |
246 if ({{parameter.name}}.isJust()) | 246 if ({{parameter.name}}.isJust()) |
247 resultObject->setValue("{{parameter.name}}", ValueConversions<{{reso
lve_type(parameter).raw_type}}>::serialize({{parameter.name}}.fromJust())); | 247 resultObject->setValue("{{parameter.name}}", ValueConversions<{{reso
lve_type(parameter).raw_type}}>::serialize({{parameter.name}}.fromJust())); |
248 {% else %} | 248 {% else %} |
249 resultObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_
type(parameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % p
arameter.name}})); | 249 resultObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_
type(parameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % p
arameter.name}})); |
250 {% endif %} | 250 {% endif %} |
251 {% endfor %} | 251 {% endfor %} |
252 sendIfActive(std::move(resultObject), ErrorString()); | 252 sendIfActive(std::move(resultObject), DispatchResponse::OK()); |
253 } | 253 } |
254 | 254 |
| 255 {% if new_style(domain) %} |
| 256 void sendFailure(const DispatchResponse& response) override |
| 257 { |
| 258 DCHECK(response.status() == DispatchResponse::kError); |
| 259 sendIfActive(nullptr, response); |
| 260 } |
| 261 {% else %} |
255 void sendFailure(const ErrorString& error) override | 262 void sendFailure(const ErrorString& error) override |
256 { | 263 { |
257 DCHECK(error.length()); | 264 DCHECK(error.length()); |
258 sendIfActive(nullptr, error); | 265 sendIfActive(nullptr, DispatchResponse::Error(error)); |
259 } | 266 } |
260 | 267 {% endif %} |
261 }; | 268 }; |
262 {% endif %} | 269 {% endif %} |
263 | 270 |
264 void DispatcherImpl::{{command.name}}(int callId, std::unique_ptr<DictionaryValu
e> requestMessageObject, ErrorSupport* errors) | 271 DispatchResponse::Status DispatcherImpl::{{command.name}}(int callId, std::uniqu
e_ptr<DictionaryValue> requestMessageObject, ErrorSupport* errors) |
265 { | 272 { |
266 {% if "parameters" in command %} | 273 {% if "parameters" in command %} |
267 // Prepare input parameters. | 274 // Prepare input parameters. |
268 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje
ct->get("params")); | 275 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje
ct->get("params")); |
269 errors->push(); | 276 errors->push(); |
270 {% for property in command.parameters %} | 277 {% for property in command.parameters %} |
271 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n
ame}}") : nullptr; | 278 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n
ame}}") : nullptr; |
272 {% if property.optional %} | 279 {% if property.optional %} |
273 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; | 280 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; |
274 if ({{property.name}}Value) { | 281 if ({{property.name}}Value) { |
275 errors->setName("{{property.name}}"); | 282 errors->setName("{{property.name}}"); |
276 in_{{property.name}} = ValueConversions<{{resolve_type(property).raw_typ
e}}>::parse({{property.name}}Value, errors); | 283 in_{{property.name}} = ValueConversions<{{resolve_type(property).raw_typ
e}}>::parse({{property.name}}Value, errors); |
277 } | 284 } |
278 {% else %} | 285 {% else %} |
279 errors->setName("{{property.name}}"); | 286 errors->setName("{{property.name}}"); |
280 {{resolve_type(property).type}} in_{{property.name}} = ValueConversions<{{re
solve_type(property).raw_type}}>::parse({{property.name}}Value, errors); | 287 {{resolve_type(property).type}} in_{{property.name}} = ValueConversions<{{re
solve_type(property).raw_type}}>::parse({{property.name}}Value, errors); |
281 {% endif %} | 288 {% endif %} |
282 {% endfor %} | 289 {% endfor %} |
283 errors->pop(); | 290 errors->pop(); |
284 if (errors->hasErrors()) { | 291 if (errors->hasErrors()) { |
285 reportProtocolError(callId, InvalidParams, kInvalidRequest, errors); | 292 reportProtocolError(callId, DispatchResponse::kInvalidParams, kInvalidPa
ramsString, errors); |
286 return; | 293 return DispatchResponse::kError; |
287 } | 294 } |
288 {% endif %} | 295 {% endif %} |
289 {% if "async" in command %} | 296 {% if "returns" in command and not ("async" in command) %} |
290 std::unique_ptr<{{command.name | to_title_case}}CallbackImpl> callback(new {
{command.name | to_title_case}}CallbackImpl(weakPtr(), callId)); | |
291 {% elif "returns" in command %} | |
292 // Declare output parameters. | 297 // Declare output parameters. |
293 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create(
); | |
294 {% for property in command.returns %} | 298 {% for property in command.returns %} |
295 {% if "optional" in property %} | 299 {% if "optional" in property %} |
296 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}}; | 300 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}}; |
297 {% else %} | 301 {% else %} |
298 {{resolve_type(property).type}} out_{{property.name}}; | 302 {{resolve_type(property).type}} out_{{property.name}}; |
299 {% endif %} | 303 {% endif %} |
300 {% endfor %} | 304 {% endfor %} |
301 {% endif %} | 305 {% endif %} |
302 | 306 |
| 307 {% if not("async" in command) %} |
303 std::unique_ptr<DispatcherBase::WeakPtr> weak = weakPtr(); | 308 std::unique_ptr<DispatcherBase::WeakPtr> weak = weakPtr(); |
304 {% if not("async" in command) %} | 309 {% if not new_style(domain) %} |
305 ErrorString error; | 310 ErrorString error; |
306 m_backend->{{command.name}}(&error | 311 m_backend->{{command.name}}(&error |
| 312 {%- else %} |
| 313 DispatchResponse response = m_backend->{{command.name}}( |
| 314 {%- endif -%} |
307 {%- for property in command.parameters -%} | 315 {%- for property in command.parameters -%} |
| 316 {%- if not loop.first or not new_style(domain) -%}, {% endif -%} |
308 {%- if "optional" in property -%} | 317 {%- if "optional" in property -%} |
309 , in_{{property.name}} | 318 in_{{property.name}} |
310 {%- else -%} | 319 {%- else -%} |
311 , {{resolve_type(property).to_pass_type % ("in_" + property.name)}} | 320 {{resolve_type(property).to_pass_type % ("in_" + property.name)}} |
312 {%- endif -%} | 321 {%- endif -%} |
313 {%- endfor %} | 322 {%- endfor %} |
314 {%- if "returns" in command %} | 323 {%- if "returns" in command %} |
315 {%- for property in command.returns -%} | 324 {%- for property in command.returns -%} |
316 , &out_{{property.name}} | 325 {%- if not loop.first or command.parameters or not new_style(domain) -
%}, {% endif -%} |
| 326 &out_{{property.name}} |
317 {%- endfor %} | 327 {%- endfor %} |
318 {% endif %}); | 328 {% endif %}); |
319 {% if "returns" in command and not("async" in command) %} | 329 {% if not new_style(domain) %} |
320 if (!error.length()) { | 330 DispatchResponse response = error.length() ? DispatchResponse::Error(error)
: DispatchResponse::OK(); |
| 331 {% endif %} |
| 332 {% if "returns" in command %} |
| 333 if (response.status() == DispatchResponse::kFallThrough) |
| 334 return response.status(); |
| 335 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create(
); |
| 336 if (response.status() == DispatchResponse::kSuccess) { |
321 {% for parameter in command.returns %} | 337 {% for parameter in command.returns %} |
322 {% if "optional" in parameter %} | 338 {% if "optional" in parameter %} |
323 if (out_{{parameter.name}}.isJust()) | 339 if (out_{{parameter.name}}.isJust()) |
324 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_ty
pe(parameter).raw_type}}>::serialize(out_{{parameter.name}}.fromJust())); | 340 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_ty
pe(parameter).raw_type}}>::serialize(out_{{parameter.name}}.fromJust())); |
325 {% else %} | 341 {% else %} |
326 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(p
arameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % ("out_"
+ parameter.name)}})); | 342 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(p
arameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % ("out_"
+ parameter.name)}})); |
327 {% endif %} | 343 {% endif %} |
328 {% endfor %} | 344 {% endfor %} |
329 } | 345 } |
330 if (weak->get()) | 346 if (weak->get()) |
331 weak->get()->sendResponse(callId, error, std::move(result)); | 347 weak->get()->sendResponse(callId, response, std::move(result)); |
332 {% else %} | 348 {% else %} |
333 if (weak->get()) | 349 if (weak->get()) |
334 weak->get()->sendResponse(callId, error); | 350 weak->get()->sendResponse(callId, response); |
335 {% endif %} | 351 {% endif %} |
336 {%- else %} | 352 return response.status(); |
| 353 {% else %} |
| 354 std::unique_ptr<{{command.name | to_title_case}}CallbackImpl> callback(new {
{command.name | to_title_case}}CallbackImpl(weakPtr(), callId)); |
337 m_backend->{{command.name}}( | 355 m_backend->{{command.name}}( |
338 {%- for property in command.parameters -%} | 356 {%- for property in command.parameters -%} |
| 357 {%- if not loop.first -%}, {% endif -%} |
339 {%- if "optional" in property -%} | 358 {%- if "optional" in property -%} |
340 in_{{property.name}}, | 359 in_{{property.name}} |
341 {%- else -%} | 360 {%- else -%} |
342 {{resolve_type(property).to_pass_type % ("in_" + property.name)}}, | 361 {{resolve_type(property).to_pass_type % ("in_" + property.name)}} |
343 {%- endif -%} | 362 {%- endif -%} |
344 {%- endfor -%} | 363 {%- endfor -%} |
| 364 {%- if command.parameters -%}, {% endif -%} |
345 std::move(callback)); | 365 std::move(callback)); |
| 366 return DispatchResponse::kAsync; |
346 {% endif %} | 367 {% endif %} |
347 } | 368 } |
348 {% endfor %} | 369 {% endfor %} |
349 | 370 |
350 // static | 371 // static |
351 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend) | 372 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend) |
352 { | 373 { |
353 dispatcher->registerBackend("{{domain.domain}}", wrapUnique(new DispatcherIm
pl(dispatcher->channel(), backend))); | 374 dispatcher->registerBackend("{{domain.domain}}", wrapUnique(new DispatcherIm
pl(dispatcher->channel(), backend))); |
354 } | 375 } |
355 | 376 |
356 } // {{domain.domain}} | 377 } // {{domain.domain}} |
357 {% for namespace in config.protocol.namespace %} | 378 {% for namespace in config.protocol.namespace %} |
358 } // namespace {{namespace}} | 379 } // namespace {{namespace}} |
359 {% endfor %} | 380 {% endfor %} |
OLD | NEW |