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 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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}}>::parse({{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}}::serializeValue() 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}}>::serialize(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}}>::serialize({{resolve_type(property).to_raw_type % ("m_" + proper ty.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 parse(serializeValue().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 = serializeValue()->toJSONString(); |
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; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 |
187 namespace { | |
188 | |
189 class RawNotification : public Serializable { | |
dgozman
2016/11/22 19:39:24
Move this out of template as well.
kozy
2016/11/22 21:01:27
Done.
| |
190 public: | |
191 explicit RawNotification(const String& notification) | |
192 : m_notification(notification) | |
193 { | |
194 } | |
195 | |
196 String serialize() override { | |
197 return m_notification; | |
198 } | |
199 | |
200 private: | |
201 String m_notification; | |
202 }; | |
203 | |
204 } // namespace | |
205 | |
181 void Frontend::sendRawNotification(const String& notification) | 206 void Frontend::sendRawNotification(const String& notification) |
182 { | 207 { |
183 m_frontendChannel->sendProtocolNotification(notification); | 208 m_frontendChannel->sendProtocolNotification(std::unique_ptr<Serializable>(ne w RawNotification(notification))); |
184 } | 209 } |
185 | 210 |
186 // --------------------- Dispatcher. | 211 // --------------------- Dispatcher. |
187 | 212 |
188 class DispatcherImpl : public protocol::DispatcherBase { | 213 class DispatcherImpl : public protocol::DispatcherBase { |
189 public: | 214 public: |
190 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend, bool fall ThroughForNotFound) | 215 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend, bool fall ThroughForNotFound) |
191 : DispatcherBase(frontendChannel) | 216 : DispatcherBase(frontendChannel) |
192 , m_backend(backend) | 217 , m_backend(backend) |
193 , m_fallThroughForNotFound(fallThroughForNotFound) { | 218 , m_fallThroughForNotFound(fallThroughForNotFound) { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 // static | 394 // static |
370 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend) | 395 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend) |
371 { | 396 { |
372 dispatcher->registerBackend("{{domain.domain}}", std::unique_ptr<protocol::D ispatcherBase>(new DispatcherImpl(dispatcher->channel(), backend, dispatcher->fa llThroughForNotFound()))); | 397 dispatcher->registerBackend("{{domain.domain}}", std::unique_ptr<protocol::D ispatcherBase>(new DispatcherImpl(dispatcher->channel(), backend, dispatcher->fa llThroughForNotFound()))); |
373 } | 398 } |
374 | 399 |
375 } // {{domain.domain}} | 400 } // {{domain.domain}} |
376 {% for namespace in config.protocol.namespace %} | 401 {% for namespace in config.protocol.namespace %} |
377 } // namespace {{namespace}} | 402 } // namespace {{namespace}} |
378 {% endfor %} | 403 {% endfor %} |
OLD | NEW |