Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: templates/TypeBuilder_cpp.template

Issue 2526603002: [inspector_protocol] Introduce protocol::Serializable (Closed)
Patch Set: minor fix Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « templates/Imported_h.template ('k') | templates/TypeBuilder_h.template » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 {{format_include(config.protocol.package, domain.domain)}} 7 #include {{format_include(config.protocol.package, domain.domain)}}
8 8
9 #include {{format_include(config.protocol.package, "Protocol")}} 9 #include {{format_include(config.protocol.package, "Protocol")}}
10 10
(...skipping 29 matching lines...) Expand all
40 {% for property in type.properties %} 40 {% for property in type.properties %}
41 {% if "enum" in property %} 41 {% if "enum" in property %}
42 42
43 {% for literal in property.enum %} 43 {% for literal in property.enum %}
44 const char* {{type.id}}::{{property.name | to_title_case}}Enum::{{literal | dash _to_camelcase}} = "{{literal}}"; 44 const char* {{type.id}}::{{property.name | to_title_case}}Enum::{{literal | dash _to_camelcase}} = "{{literal}}";
45 {% endfor %} 45 {% endfor %}
46 {% endif %} 46 {% endif %}
47 {% endfor %} 47 {% endfor %}
48 {% if not (type.type == "object") or not ("properties" in type) %}{% continu e %}{% endif %} 48 {% if not (type.type == "object") or not ("properties" in type) %}{% continu e %}{% endif %}
49 49
50 std::unique_ptr<{{type.id}}> {{type.id}}::parse(protocol::Value* value, ErrorSup port* errors) 50 std::unique_ptr<{{type.id}}> {{type.id}}::fromValue(protocol::Value* value, Erro rSupport* errors)
51 { 51 {
52 if (!value || value->type() != protocol::Value::TypeObject) { 52 if (!value || value->type() != protocol::Value::TypeObject) {
53 errors->addError("object expected"); 53 errors->addError("object expected");
54 return nullptr; 54 return nullptr;
55 } 55 }
56 56
57 std::unique_ptr<{{type.id}}> result(new {{type.id}}()); 57 std::unique_ptr<{{type.id}}> result(new {{type.id}}());
58 protocol::DictionaryValue* object = DictionaryValue::cast(value); 58 protocol::DictionaryValue* object = DictionaryValue::cast(value);
59 errors->push(); 59 errors->push();
60 {% for property in type.properties %} 60 {% for property in type.properties %}
61 protocol::Value* {{property.name}}Value = object->get("{{property.name}}"); 61 protocol::Value* {{property.name}}Value = object->get("{{property.name}}");
62 {% if property.optional %} 62 {% if property.optional %}
63 if ({{property.name}}Value) { 63 if ({{property.name}}Value) {
64 errors->setName("{{property.name}}"); 64 errors->setName("{{property.name}}");
65 result->m_{{property.name}} = ValueConversions<{{resolve_type(property). raw_type}}>::parse({{property.name}}Value, errors); 65 result->m_{{property.name}} = ValueConversions<{{resolve_type(property). raw_type}}>::fromValue({{property.name}}Value, errors);
66 } 66 }
67 {% else %} 67 {% else %}
68 errors->setName("{{property.name}}"); 68 errors->setName("{{property.name}}");
69 result->m_{{property.name}} = ValueConversions<{{resolve_type(property).raw_ type}}>::parse({{property.name}}Value, errors); 69 result->m_{{property.name}} = ValueConversions<{{resolve_type(property).raw_ type}}>::fromValue({{property.name}}Value, errors);
70 {% endif %} 70 {% endif %}
71 {% endfor %} 71 {% endfor %}
72 errors->pop(); 72 errors->pop();
73 if (errors->hasErrors()) 73 if (errors->hasErrors())
74 return nullptr; 74 return nullptr;
75 return result; 75 return result;
76 } 76 }
77 77
78 std::unique_ptr<protocol::DictionaryValue> {{type.id}}::serialize() const 78 std::unique_ptr<protocol::DictionaryValue> {{type.id}}::toValue() const
79 { 79 {
80 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create( ); 80 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create( );
81 {% for property in type.properties %} 81 {% for property in type.properties %}
82 {% if property.optional %} 82 {% if property.optional %}
83 if (m_{{property.name}}.isJust()) 83 if (m_{{property.name}}.isJust())
84 result->setValue("{{property.name}}", ValueConversions<{{resolve_type(pr operty).raw_type}}>::serialize(m_{{property.name}}.fromJust())); 84 result->setValue("{{property.name}}", ValueConversions<{{resolve_type(pr operty).raw_type}}>::toValue(m_{{property.name}}.fromJust()));
85 {% else %} 85 {% else %}
86 result->setValue("{{property.name}}", ValueConversions<{{resolve_type(proper ty).raw_type}}>::serialize({{resolve_type(property).to_raw_type % ("m_" + proper ty.name)}})); 86 result->setValue("{{property.name}}", ValueConversions<{{resolve_type(proper ty).raw_type}}>::toValue({{resolve_type(property).to_raw_type % ("m_" + property .name)}}));
87 {% endif %} 87 {% endif %}
88 {% endfor %} 88 {% endfor %}
89 return result; 89 return result;
90 } 90 }
91 91
92 std::unique_ptr<{{type.id}}> {{type.id}}::clone() const 92 std::unique_ptr<{{type.id}}> {{type.id}}::clone() const
93 { 93 {
94 ErrorSupport errors; 94 ErrorSupport errors;
95 return parse(serialize().get(), &errors); 95 return fromValue(toValue().get(), &errors);
96 } 96 }
97 {% if type.exported %} 97 {% if type.exported %}
98 98
99 {{config.exported.string_out}} {{type.id}}::toJSONString() const 99 {{config.exported.string_out}} {{type.id}}::toJSONString() const
100 { 100 {
101 String json = serialize()->toJSONString(); 101 String json = toValue()->serialize();
102 return {{config.exported.to_string_out % "json"}}; 102 return {{config.exported.to_string_out % "json"}};
103 } 103 }
104 104
105 // static 105 // static
106 std::unique_ptr<API::{{type.id}}> API::{{type.id}}::fromJSONString(const {{confi g.exported.string_in}}& json) 106 std::unique_ptr<API::{{type.id}}> API::{{type.id}}::fromJSONString(const {{confi g.exported.string_in}}& json)
107 { 107 {
108 ErrorSupport errors; 108 ErrorSupport errors;
109 std::unique_ptr<Value> value = StringUtil::parseJSON(json); 109 std::unique_ptr<Value> value = StringUtil::parseJSON(json);
110 if (!value) 110 if (!value)
111 return nullptr; 111 return nullptr;
112 return protocol::{{domain.domain}}::{{type.id}}::parse(value.get(), &errors) ; 112 return protocol::{{domain.domain}}::{{type.id}}::fromValue(value.get(), &err ors);
113 } 113 }
114 {% endif %} 114 {% endif %}
115 {% endfor %} 115 {% endfor %}
116 116
117 // ------------- Enum values from params. 117 // ------------- Enum values from params.
118 118
119 {% for command in join_arrays(domain, ["commands", "events"]) %} 119 {% for command in join_arrays(domain, ["commands", "events"]) %}
120 {% for param in join_arrays(command, ["parameters", "returns"]) %} 120 {% for param in join_arrays(command, ["parameters", "returns"]) %}
121 {% if "enum" in param %} 121 {% if "enum" in param %}
122 122
(...skipping 26 matching lines...) Expand all
149 149
150 void Frontend::{{event.name | to_method_case}}( 150 void Frontend::{{event.name | to_method_case}}(
151 {%- for parameter in event.parameters %} 151 {%- for parameter in event.parameters %}
152 {% if "optional" in parameter -%} 152 {% if "optional" in parameter -%}
153 Maybe<{{resolve_type(parameter).raw_type}}> 153 Maybe<{{resolve_type(parameter).raw_type}}>
154 {%- else -%} 154 {%- else -%}
155 {{resolve_type(parameter).pass_type}} 155 {{resolve_type(parameter).pass_type}}
156 {%- endif %} {{parameter.name}}{%- if not loop.last -%}, {% endif -%} 156 {%- endif %} {{parameter.name}}{%- if not loop.last -%}, {% endif -%}
157 {% endfor -%}) 157 {% endfor -%})
158 { 158 {
159 std::unique_ptr<protocol::DictionaryValue> jsonMessage = DictionaryValue::cr eate(); 159 if (!m_frontendChannel)
160 jsonMessage->setString("method", "{{domain.domain}}.{{event.name}}"); 160 return;
161 std::unique_ptr<protocol::DictionaryValue> paramsObject = DictionaryValue::c reate(); 161 {% if event.parameters %}
162 {% for parameter in event.parameters %} 162 std::unique_ptr<{{event.name | to_title_case}}Notification> messageData = {{ event.name | to_title_case}}Notification::{{"create" | to_method_case}}()
163 {% if "optional" in parameter %} 163 {% for parameter in event.parameters %}
164 {% if not "optional" in parameter %}
165 .{{"set" | to_method_case}}{{parameter.name | to_title_case}}({{resolve_ type(parameter).to_pass_type % parameter.name}})
166 {% endif %}
167 {% endfor %}
168 .{{ "build" | to_method_case }}();
169 {% for parameter in event.parameters %}
170 {% if "optional" in parameter %}
164 if ({{parameter.name}}.isJust()) 171 if ({{parameter.name}}.isJust())
165 paramsObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_ type(parameter).raw_type}}>::serialize({{parameter.name}}.fromJust())); 172 messageData->{{"set" | to_method_case}}{{parameter.name | to_title_case} }(std::move({{parameter.name}}).takeJust());
173 {% endif %}
174 {% endfor %}
175 m_frontendChannel->sendProtocolNotification(InternalResponse::createNotifica tion("{{domain.domain}}.{{event.name}}", std::move(messageData)));
166 {% else %} 176 {% else %}
167 paramsObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_type (parameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % param eter.name}})); 177 m_frontendChannel->sendProtocolNotification(InternalResponse::createNotifica tion("{{domain.domain}}.{{event.name}}"));
168 {% endif %} 178 {% endif %}
169 {% endfor %}
170 jsonMessage->setObject("params", std::move(paramsObject));
171 if (m_frontendChannel)
172 m_frontendChannel->sendProtocolNotification(jsonMessage->toJSONString()) ;
173 } 179 }
174 {% endfor %} 180 {% endfor %}
175 181
176 void Frontend::flush() 182 void Frontend::flush()
177 { 183 {
178 m_frontendChannel->flushProtocolNotifications(); 184 m_frontendChannel->flushProtocolNotifications();
179 } 185 }
180 186
181 void Frontend::sendRawNotification(const String& notification) 187 void Frontend::sendRawNotification(const String& notification)
182 { 188 {
183 m_frontendChannel->sendProtocolNotification(notification); 189 m_frontendChannel->sendProtocolNotification(InternalRawNotification::create( notification));
184 } 190 }
185 191
186 // --------------------- Dispatcher. 192 // --------------------- Dispatcher.
187 193
188 class DispatcherImpl : public protocol::DispatcherBase { 194 class DispatcherImpl : public protocol::DispatcherBase {
189 public: 195 public:
190 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend, bool fall ThroughForNotFound) 196 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend, bool fall ThroughForNotFound)
191 : DispatcherBase(frontendChannel) 197 : DispatcherBase(frontendChannel)
192 , m_backend(backend) 198 , m_backend(backend)
193 , m_fallThroughForNotFound(fallThroughForNotFound) { 199 , m_fallThroughForNotFound(fallThroughForNotFound) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 {%- else -%} 252 {%- else -%}
247 {{resolve_type(parameter).pass_type}} {{parameter.name}} 253 {{resolve_type(parameter).pass_type}} {{parameter.name}}
248 {%- endif -%} 254 {%- endif -%}
249 {%- if not loop.last -%}, {% endif -%} 255 {%- if not loop.last -%}, {% endif -%}
250 {%- endfor -%}) override 256 {%- endfor -%}) override
251 { 257 {
252 std::unique_ptr<protocol::DictionaryValue> resultObject = DictionaryValu e::create(); 258 std::unique_ptr<protocol::DictionaryValue> resultObject = DictionaryValu e::create();
253 {% for parameter in command.returns %} 259 {% for parameter in command.returns %}
254 {% if "optional" in parameter %} 260 {% if "optional" in parameter %}
255 if ({{parameter.name}}.isJust()) 261 if ({{parameter.name}}.isJust())
256 resultObject->setValue("{{parameter.name}}", ValueConversions<{{reso lve_type(parameter).raw_type}}>::serialize({{parameter.name}}.fromJust())); 262 resultObject->setValue("{{parameter.name}}", ValueConversions<{{reso lve_type(parameter).raw_type}}>::toValue({{parameter.name}}.fromJust()));
257 {% else %} 263 {% else %}
258 resultObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_ type(parameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % p arameter.name}})); 264 resultObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_ type(parameter).raw_type}}>::toValue({{resolve_type(parameter).to_raw_type % par ameter.name}}));
259 {% endif %} 265 {% endif %}
260 {% endfor %} 266 {% endfor %}
261 sendIfActive(std::move(resultObject), DispatchResponse::OK()); 267 sendIfActive(std::move(resultObject), DispatchResponse::OK());
262 } 268 }
263 269
264 void fallThrough() override 270 void fallThrough() override
265 { 271 {
266 fallThroughIfActive(); 272 fallThroughIfActive();
267 } 273 }
268 274
(...skipping 10 matching lines...) Expand all
279 {% if "parameters" in command %} 285 {% if "parameters" in command %}
280 // Prepare input parameters. 286 // Prepare input parameters.
281 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje ct->get("params")); 287 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje ct->get("params"));
282 errors->push(); 288 errors->push();
283 {% for property in command.parameters %} 289 {% for property in command.parameters %}
284 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n ame}}") : nullptr; 290 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n ame}}") : nullptr;
285 {% if property.optional %} 291 {% if property.optional %}
286 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; 292 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}};
287 if ({{property.name}}Value) { 293 if ({{property.name}}Value) {
288 errors->setName("{{property.name}}"); 294 errors->setName("{{property.name}}");
289 in_{{property.name}} = ValueConversions<{{resolve_type(property).raw_typ e}}>::parse({{property.name}}Value, errors); 295 in_{{property.name}} = ValueConversions<{{resolve_type(property).raw_typ e}}>::fromValue({{property.name}}Value, errors);
290 } 296 }
291 {% else %} 297 {% else %}
292 errors->setName("{{property.name}}"); 298 errors->setName("{{property.name}}");
293 {{resolve_type(property).type}} in_{{property.name}} = ValueConversions<{{re solve_type(property).raw_type}}>::parse({{property.name}}Value, errors); 299 {{resolve_type(property).type}} in_{{property.name}} = ValueConversions<{{re solve_type(property).raw_type}}>::fromValue({{property.name}}Value, errors);
294 {% endif %} 300 {% endif %}
295 {% endfor %} 301 {% endfor %}
296 errors->pop(); 302 errors->pop();
297 if (errors->hasErrors()) { 303 if (errors->hasErrors()) {
298 reportProtocolError(callId, DispatchResponse::kInvalidParams, kInvalidPa ramsString, errors); 304 reportProtocolError(callId, DispatchResponse::kInvalidParams, kInvalidPa ramsString, errors);
299 return DispatchResponse::kError; 305 return DispatchResponse::kError;
300 } 306 }
301 {% endif %} 307 {% endif %}
302 {% if "returns" in command and not is_async_command(domain.domain, command.n ame) %} 308 {% if "returns" in command and not is_async_command(domain.domain, command.n ame) %}
303 // Declare output parameters. 309 // Declare output parameters.
(...skipping 24 matching lines...) Expand all
328 {%- endfor %} 334 {%- endfor %}
329 {% endif %}); 335 {% endif %});
330 {% if "returns" in command %} 336 {% if "returns" in command %}
331 if (response.status() == DispatchResponse::kFallThrough) 337 if (response.status() == DispatchResponse::kFallThrough)
332 return response.status(); 338 return response.status();
333 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create( ); 339 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create( );
334 if (response.status() == DispatchResponse::kSuccess) { 340 if (response.status() == DispatchResponse::kSuccess) {
335 {% for parameter in command.returns %} 341 {% for parameter in command.returns %}
336 {% if "optional" in parameter %} 342 {% if "optional" in parameter %}
337 if (out_{{parameter.name}}.isJust()) 343 if (out_{{parameter.name}}.isJust())
338 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_ty pe(parameter).raw_type}}>::serialize(out_{{parameter.name}}.fromJust())); 344 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_ty pe(parameter).raw_type}}>::toValue(out_{{parameter.name}}.fromJust()));
339 {% else %} 345 {% else %}
340 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(p arameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % ("out_" + parameter.name)}})); 346 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(p arameter).raw_type}}>::toValue({{resolve_type(parameter).to_raw_type % ("out_" + parameter.name)}}));
341 {% endif %} 347 {% endif %}
342 {% endfor %} 348 {% endfor %}
343 } 349 }
344 if (weak->get()) 350 if (weak->get())
345 weak->get()->sendResponse(callId, response, std::move(result)); 351 weak->get()->sendResponse(callId, response, std::move(result));
346 {% else %} 352 {% else %}
347 if (weak->get()) 353 if (weak->get())
348 weak->get()->sendResponse(callId, response); 354 weak->get()->sendResponse(callId, response);
349 {% endif %} 355 {% endif %}
350 return response.status(); 356 return response.status();
(...skipping 18 matching lines...) Expand all
369 // static 375 // static
370 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend) 376 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend)
371 { 377 {
372 dispatcher->registerBackend("{{domain.domain}}", std::unique_ptr<protocol::D ispatcherBase>(new DispatcherImpl(dispatcher->channel(), backend, dispatcher->fa llThroughForNotFound()))); 378 dispatcher->registerBackend("{{domain.domain}}", std::unique_ptr<protocol::D ispatcherBase>(new DispatcherImpl(dispatcher->channel(), backend, dispatcher->fa llThroughForNotFound())));
373 } 379 }
374 380
375 } // {{domain.domain}} 381 } // {{domain.domain}}
376 {% for namespace in config.protocol.namespace %} 382 {% for namespace in config.protocol.namespace %}
377 } // namespace {{namespace}} 383 } // namespace {{namespace}}
378 {% endfor %} 384 {% endfor %}
OLDNEW
« no previous file with comments | « templates/Imported_h.template ('k') | templates/TypeBuilder_h.template » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698