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 {{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 |
11 {% for namespace in config.protocol.namespace %} | 11 {% for namespace in config.protocol.namespace %} |
12 namespace {{namespace}} { | 12 namespace {{namespace}} { |
13 {% endfor %} | 13 {% endfor %} |
14 | |
15 namespace { | |
16 | |
17 class RawNotification : public Serializable { | |
dgozman
2016/11/22 22:36:00
Move this to DispatcherBase.h
kozy
2016/11/22 23:01:24
Done.
| |
18 public: | |
19 explicit RawNotification(const String& notification) | |
20 : m_notification(notification) | |
21 { | |
22 } | |
23 ~RawNotification() override {} | |
24 | |
25 String serialize() override | |
26 { | |
27 return m_notification; | |
28 } | |
29 | |
30 private: | |
31 String m_notification; | |
32 }; | |
33 | |
34 } // namespace | |
35 | |
14 namespace {{domain.domain}} { | 36 namespace {{domain.domain}} { |
15 | 37 |
16 // ------------- Enum values from types. | 38 // ------------- Enum values from types. |
17 | 39 |
18 const char Metainfo::domainName[] = "{{domain.domain}}"; | 40 const char Metainfo::domainName[] = "{{domain.domain}}"; |
19 const char Metainfo::commandPrefix[] = "{{domain.domain}}."; | 41 const char Metainfo::commandPrefix[] = "{{domain.domain}}."; |
20 const char Metainfo::version[] = "{{domain.version}}"; | 42 const char Metainfo::version[] = "{{domain.version}}"; |
21 {% for type in domain.types %} | 43 {% for type in domain.types %} |
22 {% if "enum" in type %} | 44 {% if "enum" in type %} |
23 | 45 |
(...skipping 16 matching lines...) Expand all Loading... | |
40 {% for property in type.properties %} | 62 {% for property in type.properties %} |
41 {% if "enum" in property %} | 63 {% if "enum" in property %} |
42 | 64 |
43 {% for literal in property.enum %} | 65 {% for literal in property.enum %} |
44 const char* {{type.id}}::{{property.name | to_title_case}}Enum::{{literal | dash _to_camelcase}} = "{{literal}}"; | 66 const char* {{type.id}}::{{property.name | to_title_case}}Enum::{{literal | dash _to_camelcase}} = "{{literal}}"; |
45 {% endfor %} | 67 {% endfor %} |
46 {% endif %} | 68 {% endif %} |
47 {% endfor %} | 69 {% endfor %} |
48 {% if not (type.type == "object") or not ("properties" in type) %}{% continu e %}{% endif %} | 70 {% if not (type.type == "object") or not ("properties" in type) %}{% continu e %}{% endif %} |
49 | 71 |
50 std::unique_ptr<{{type.id}}> {{type.id}}::parse(protocol::Value* value, ErrorSup port* errors) | 72 std::unique_ptr<{{type.id}}> {{type.id}}::fromValue(protocol::Value* value, Erro rSupport* errors) |
51 { | 73 { |
52 if (!value || value->type() != protocol::Value::TypeObject) { | 74 if (!value || value->type() != protocol::Value::TypeObject) { |
53 errors->addError("object expected"); | 75 errors->addError("object expected"); |
54 return nullptr; | 76 return nullptr; |
55 } | 77 } |
56 | 78 |
57 std::unique_ptr<{{type.id}}> result(new {{type.id}}()); | 79 std::unique_ptr<{{type.id}}> result(new {{type.id}}()); |
58 protocol::DictionaryValue* object = DictionaryValue::cast(value); | 80 protocol::DictionaryValue* object = DictionaryValue::cast(value); |
59 errors->push(); | 81 errors->push(); |
60 {% for property in type.properties %} | 82 {% for property in type.properties %} |
61 protocol::Value* {{property.name}}Value = object->get("{{property.name}}"); | 83 protocol::Value* {{property.name}}Value = object->get("{{property.name}}"); |
62 {% if property.optional %} | 84 {% if property.optional %} |
63 if ({{property.name}}Value) { | 85 if ({{property.name}}Value) { |
64 errors->setName("{{property.name}}"); | 86 errors->setName("{{property.name}}"); |
65 result->m_{{property.name}} = ValueConversions<{{resolve_type(property). raw_type}}>::parse({{property.name}}Value, errors); | 87 result->m_{{property.name}} = ValueConversions<{{resolve_type(property). raw_type}}>::fromValue({{property.name}}Value, errors); |
66 } | 88 } |
67 {% else %} | 89 {% else %} |
68 errors->setName("{{property.name}}"); | 90 errors->setName("{{property.name}}"); |
69 result->m_{{property.name}} = ValueConversions<{{resolve_type(property).raw_ type}}>::parse({{property.name}}Value, errors); | 91 result->m_{{property.name}} = ValueConversions<{{resolve_type(property).raw_ type}}>::fromValue({{property.name}}Value, errors); |
70 {% endif %} | 92 {% endif %} |
71 {% endfor %} | 93 {% endfor %} |
72 errors->pop(); | 94 errors->pop(); |
73 if (errors->hasErrors()) | 95 if (errors->hasErrors()) |
74 return nullptr; | 96 return nullptr; |
75 return result; | 97 return result; |
76 } | 98 } |
77 | 99 |
78 std::unique_ptr<protocol::DictionaryValue> {{type.id}}::serialize() const | 100 std::unique_ptr<protocol::DictionaryValue> {{type.id}}::toValue() const |
79 { | 101 { |
80 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create( ); | 102 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create( ); |
81 {% for property in type.properties %} | 103 {% for property in type.properties %} |
82 {% if property.optional %} | 104 {% if property.optional %} |
83 if (m_{{property.name}}.isJust()) | 105 if (m_{{property.name}}.isJust()) |
84 result->setValue("{{property.name}}", ValueConversions<{{resolve_type(pr operty).raw_type}}>::serialize(m_{{property.name}}.fromJust())); | 106 result->setValue("{{property.name}}", ValueConversions<{{resolve_type(pr operty).raw_type}}>::serialize(m_{{property.name}}.fromJust())); |
85 {% else %} | 107 {% else %} |
86 result->setValue("{{property.name}}", ValueConversions<{{resolve_type(proper ty).raw_type}}>::serialize({{resolve_type(property).to_raw_type % ("m_" + proper ty.name)}})); | 108 result->setValue("{{property.name}}", ValueConversions<{{resolve_type(proper ty).raw_type}}>::serialize({{resolve_type(property).to_raw_type % ("m_" + proper ty.name)}})); |
87 {% endif %} | 109 {% endif %} |
88 {% endfor %} | 110 {% endfor %} |
89 return result; | 111 return result; |
90 } | 112 } |
91 | 113 |
92 std::unique_ptr<{{type.id}}> {{type.id}}::clone() const | 114 std::unique_ptr<{{type.id}}> {{type.id}}::clone() const |
93 { | 115 { |
94 ErrorSupport errors; | 116 ErrorSupport errors; |
95 return parse(serialize().get(), &errors); | 117 return fromValue(toValue().get(), &errors); |
96 } | 118 } |
97 {% if type.exported %} | 119 {% if type.exported %} |
98 | 120 |
99 {{config.exported.string_out}} {{type.id}}::toJSONString() const | 121 {{config.exported.string_out}} {{type.id}}::toJSONString() const |
100 { | 122 { |
101 String json = serialize()->toJSONString(); | 123 String json = toValue()->toJSONString(); |
102 return {{config.exported.to_string_out % "json"}}; | 124 return {{config.exported.to_string_out % "json"}}; |
103 } | 125 } |
104 | 126 |
105 // static | 127 // static |
106 std::unique_ptr<API::{{type.id}}> API::{{type.id}}::fromJSONString(const {{confi g.exported.string_in}}& json) | 128 std::unique_ptr<API::{{type.id}}> API::{{type.id}}::fromJSONString(const {{confi g.exported.string_in}}& json) |
107 { | 129 { |
108 ErrorSupport errors; | 130 ErrorSupport errors; |
109 std::unique_ptr<Value> value = StringUtil::parseJSON(json); | 131 std::unique_ptr<Value> value = StringUtil::parseJSON(json); |
110 if (!value) | 132 if (!value) |
111 return nullptr; | 133 return nullptr; |
112 return protocol::{{domain.domain}}::{{type.id}}::parse(value.get(), &errors) ; | 134 return protocol::{{domain.domain}}::{{type.id}}::fromValue(value.get(), &err ors); |
113 } | 135 } |
114 {% endif %} | 136 {% endif %} |
115 {% endfor %} | 137 {% endfor %} |
116 | 138 |
117 // ------------- Enum values from params. | 139 // ------------- Enum values from params. |
118 | 140 |
119 {% for command in join_arrays(domain, ["commands", "events"]) %} | 141 {% for command in join_arrays(domain, ["commands", "events"]) %} |
120 {% for param in join_arrays(command, ["parameters", "returns"]) %} | 142 {% for param in join_arrays(command, ["parameters", "returns"]) %} |
121 {% if "enum" in param %} | 143 {% if "enum" in param %} |
122 | 144 |
(...skipping 26 matching lines...) Expand all Loading... | |
149 | 171 |
150 void Frontend::{{event.name | to_method_case}}( | 172 void Frontend::{{event.name | to_method_case}}( |
151 {%- for parameter in event.parameters %} | 173 {%- for parameter in event.parameters %} |
152 {% if "optional" in parameter -%} | 174 {% if "optional" in parameter -%} |
153 Maybe<{{resolve_type(parameter).raw_type}}> | 175 Maybe<{{resolve_type(parameter).raw_type}}> |
154 {%- else -%} | 176 {%- else -%} |
155 {{resolve_type(parameter).pass_type}} | 177 {{resolve_type(parameter).pass_type}} |
156 {%- endif %} {{parameter.name}}{%- if not loop.last -%}, {% endif -%} | 178 {%- endif %} {{parameter.name}}{%- if not loop.last -%}, {% endif -%} |
157 {% endfor -%}) | 179 {% endfor -%}) |
158 { | 180 { |
159 std::unique_ptr<protocol::DictionaryValue> jsonMessage = DictionaryValue::cr eate(); | 181 if (!m_frontendChannel) |
160 jsonMessage->setString("method", "{{domain.domain}}.{{event.name}}"); | 182 return; |
161 std::unique_ptr<protocol::DictionaryValue> paramsObject = DictionaryValue::c reate(); | 183 {% if event.parameters %} |
162 {% for parameter in event.parameters %} | 184 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 %} | 185 {% for parameter in event.parameters %} |
186 {% if not "optional" in parameter %} | |
187 .{{"set" | to_method_case}}{{parameter.name | to_title_case}}({{resolve_ type(parameter).to_pass_type % parameter.name}}) | |
188 {% endif %} | |
189 {% endfor %} | |
190 .{{ "build" | to_method_case }}(); | |
191 {% for parameter in event.parameters %} | |
192 {% if "optional" in parameter %} | |
164 if ({{parameter.name}}.isJust()) | 193 if ({{parameter.name}}.isJust()) |
165 paramsObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_ type(parameter).raw_type}}>::serialize({{parameter.name}}.fromJust())); | 194 messageData->{{"set" | to_method_case}}{{parameter.name | to_title_case} }(std::move({{parameter.name}}).takeJust()); |
195 {% endif %} | |
196 {% endfor %} | |
197 m_frontendChannel->sendProtocolNotification(InternalResponse::createNotifica tion("{{domain.domain}}.{{event.name}}", std::move(messageData))); | |
166 {% else %} | 198 {% else %} |
167 paramsObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_type (parameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % param eter.name}})); | 199 m_frontendChannel->sendProtocolNotification(InternalResponse::createNotifica tion("{{domain.domain}}.{{event.name}}")); |
168 {% endif %} | 200 {% endif %} |
169 {% endfor %} | |
170 jsonMessage->setObject("params", std::move(paramsObject)); | |
171 if (m_frontendChannel) | |
172 m_frontendChannel->sendProtocolNotification(jsonMessage->toJSONString()) ; | |
173 } | 201 } |
174 {% endfor %} | 202 {% endfor %} |
175 | 203 |
176 void Frontend::flush() | 204 void Frontend::flush() |
177 { | 205 { |
178 m_frontendChannel->flushProtocolNotifications(); | 206 m_frontendChannel->flushProtocolNotifications(); |
179 } | 207 } |
180 | 208 |
181 void Frontend::sendRawNotification(const String& notification) | 209 void Frontend::sendRawNotification(const String& notification) |
182 { | 210 { |
183 m_frontendChannel->sendProtocolNotification(notification); | 211 m_frontendChannel->sendProtocolNotification(std::unique_ptr<Serializable>(ne w RawNotification(notification))); |
184 } | 212 } |
185 | 213 |
186 // --------------------- Dispatcher. | 214 // --------------------- Dispatcher. |
187 | 215 |
188 class DispatcherImpl : public protocol::DispatcherBase { | 216 class DispatcherImpl : public protocol::DispatcherBase { |
189 public: | 217 public: |
190 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend, bool fall ThroughForNotFound) | 218 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend, bool fall ThroughForNotFound) |
191 : DispatcherBase(frontendChannel) | 219 : DispatcherBase(frontendChannel) |
192 , m_backend(backend) | 220 , m_backend(backend) |
193 , m_fallThroughForNotFound(fallThroughForNotFound) { | 221 , m_fallThroughForNotFound(fallThroughForNotFound) { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 {% if "parameters" in command %} | 307 {% if "parameters" in command %} |
280 // Prepare input parameters. | 308 // Prepare input parameters. |
281 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje ct->get("params")); | 309 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje ct->get("params")); |
282 errors->push(); | 310 errors->push(); |
283 {% for property in command.parameters %} | 311 {% for property in command.parameters %} |
284 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n ame}}") : nullptr; | 312 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n ame}}") : nullptr; |
285 {% if property.optional %} | 313 {% if property.optional %} |
286 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; | 314 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; |
287 if ({{property.name}}Value) { | 315 if ({{property.name}}Value) { |
288 errors->setName("{{property.name}}"); | 316 errors->setName("{{property.name}}"); |
289 in_{{property.name}} = ValueConversions<{{resolve_type(property).raw_typ e}}>::parse({{property.name}}Value, errors); | 317 in_{{property.name}} = ValueConversions<{{resolve_type(property).raw_typ e}}>::fromValue({{property.name}}Value, errors); |
290 } | 318 } |
291 {% else %} | 319 {% else %} |
292 errors->setName("{{property.name}}"); | 320 errors->setName("{{property.name}}"); |
293 {{resolve_type(property).type}} in_{{property.name}} = ValueConversions<{{re solve_type(property).raw_type}}>::parse({{property.name}}Value, errors); | 321 {{resolve_type(property).type}} in_{{property.name}} = ValueConversions<{{re solve_type(property).raw_type}}>::fromValue({{property.name}}Value, errors); |
294 {% endif %} | 322 {% endif %} |
295 {% endfor %} | 323 {% endfor %} |
296 errors->pop(); | 324 errors->pop(); |
297 if (errors->hasErrors()) { | 325 if (errors->hasErrors()) { |
298 reportProtocolError(callId, DispatchResponse::kInvalidParams, kInvalidPa ramsString, errors); | 326 reportProtocolError(callId, DispatchResponse::kInvalidParams, kInvalidPa ramsString, errors); |
299 return DispatchResponse::kError; | 327 return DispatchResponse::kError; |
300 } | 328 } |
301 {% endif %} | 329 {% endif %} |
302 {% if "returns" in command and not is_async_command(domain.domain, command.n ame) %} | 330 {% if "returns" in command and not is_async_command(domain.domain, command.n ame) %} |
303 // Declare output parameters. | 331 // Declare output parameters. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 // static | 397 // static |
370 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend) | 398 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend) |
371 { | 399 { |
372 dispatcher->registerBackend("{{domain.domain}}", std::unique_ptr<protocol::D ispatcherBase>(new DispatcherImpl(dispatcher->channel(), backend, dispatcher->fa llThroughForNotFound()))); | 400 dispatcher->registerBackend("{{domain.domain}}", std::unique_ptr<protocol::D ispatcherBase>(new DispatcherImpl(dispatcher->channel(), backend, dispatcher->fa llThroughForNotFound()))); |
373 } | 401 } |
374 | 402 |
375 } // {{domain.domain}} | 403 } // {{domain.domain}} |
376 {% for namespace in config.protocol.namespace %} | 404 {% for namespace in config.protocol.namespace %} |
377 } // namespace {{namespace}} | 405 } // namespace {{namespace}} |
378 {% endfor %} | 406 {% endfor %} |
OLD | NEW |