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 "platform/inspector_protocol/{{class_name}}.h" | 7 #include "platform/inspector_protocol/{{class_name}}.h" |
8 | 8 |
| 9 #include "platform/inspector_protocol/DispatcherBase.h" |
| 10 |
9 namespace blink { | 11 namespace blink { |
10 namespace protocol { | 12 namespace protocol { |
11 | 13 |
| 14 static const char kInvalidRequest[] = "Invalid request"; |
| 15 |
12 // ------------- Enum values from types. | 16 // ------------- Enum values from types. |
13 {% for domain in api.domains %} | 17 {% for domain in api.domains %} |
14 | 18 |
15 namespace {{domain.domain}} { | 19 namespace {{domain.domain}} { |
| 20 |
| 21 const char Metainfo::domainName[] = "{{domain.domain}}"; |
16 {% for type in domain.types %} | 22 {% for type in domain.types %} |
17 {% if "enum" in type %} | 23 {% if "enum" in type %} |
18 | 24 |
19 namespace {{type.id}}Enum { | 25 namespace {{type.id}}Enum { |
20 {% for literal in type.enum %} | 26 {% for literal in type.enum %} |
21 const char* {{ literal | dash_to_camelcase}} = "{{literal}}"; | 27 const char* {{ literal | dash_to_camelcase}} = "{{literal}}"; |
22 {% endfor %} | 28 {% endfor %} |
23 } // {{type.id}}Enum | 29 } // {{type.id}}Enum |
24 {% endif %} | 30 {% endif %} |
25 {% for property in type.properties %} | 31 {% for property in type.properties %} |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 {% endfor %} | 79 {% endfor %} |
74 return result; | 80 return result; |
75 } | 81 } |
76 | 82 |
77 std::unique_ptr<{{type.id}}> {{type.id}}::clone() const | 83 std::unique_ptr<{{type.id}}> {{type.id}}::clone() const |
78 { | 84 { |
79 ErrorSupport errors; | 85 ErrorSupport errors; |
80 return parse(serialize().get(), &errors); | 86 return parse(serialize().get(), &errors); |
81 } | 87 } |
82 {% endfor %} | 88 {% endfor %} |
83 } // {{domain.domain}} | |
84 {% endfor %} | |
85 | 89 |
86 // ------------- Enum values from params. | 90 // ------------- Enum values from params. |
87 {% for domain in api.domains %} | 91 |
88 {% for command in join_arrays(domain, ["commands", "events"]) %} | 92 {% for command in join_arrays(domain, ["commands", "events"]) %} |
89 {% for param in join_arrays(command, ["parameters", "returns"]) %} | 93 {% for param in join_arrays(command, ["parameters", "returns"]) %} |
90 {% if "enum" in param %} | 94 {% if "enum" in param %} |
91 | 95 |
92 namespace {{domain.domain}} { | |
93 namespace {{command.name | to_title_case}} { | 96 namespace {{command.name | to_title_case}} { |
94 namespace {{param.name | to_title_case}}Enum { | 97 namespace {{param.name | to_title_case}}Enum { |
95 {% for literal in param.enum %} | 98 {% for literal in param.enum %} |
96 const char* {{ literal | to_title_case}} = "{{literal}}"; | 99 const char* {{ literal | to_title_case}} = "{{literal}}"; |
97 {% endfor %} | 100 {% endfor %} |
98 } // {{param.name | to_title_case}}Enum | 101 } // {{param.name | to_title_case}}Enum |
99 } // {{command.name | to_title_case }} | 102 } // {{command.name | to_title_case }} |
100 } // {{domain.domain}} | |
101 {% endif %} | 103 {% endif %} |
102 {% endfor %} | 104 {% endfor %} |
103 {% endfor %} | 105 {% endfor %} |
| 106 |
| 107 // ------------- Frontend notifications. |
| 108 {% for event in domain.events %} |
| 109 {% if "handlers" in event and not ("renderer" in event["handlers"]) %}{% con
tinue %}{% endif %} |
| 110 |
| 111 void Frontend::{{event.name}}( |
| 112 {%- for parameter in event.parameters %} |
| 113 {% if "optional" in parameter -%} |
| 114 const Maybe<{{resolve_type(parameter).raw_type}}>& |
| 115 {%- else -%} |
| 116 {{resolve_type(parameter).pass_type}} |
| 117 {%- endif %} {{parameter.name}}{%- if not loop.last -%}, {% endif -%} |
| 118 {% endfor -%}) |
| 119 { |
| 120 std::unique_ptr<protocol::DictionaryValue> jsonMessage = DictionaryValue::cr
eate(); |
| 121 jsonMessage->setString("method", "{{domain.domain}}.{{event.name}}"); |
| 122 std::unique_ptr<protocol::DictionaryValue> paramsObject = DictionaryValue::c
reate(); |
| 123 {% for parameter in event.parameters %} |
| 124 {% if "optional" in parameter %} |
| 125 if ({{parameter.name}}.isJust()) |
| 126 paramsObject->setValue("{{parameter.name}}", toValue({{parameter.name}}.
fromJust())); |
| 127 {% else %} |
| 128 paramsObject->setValue("{{parameter.name}}", toValue({{resolve_type(paramete
r).to_raw_type % parameter.name}})); |
| 129 {% endif %} |
| 130 {% endfor %} |
| 131 jsonMessage->setObject("params", std::move(paramsObject)); |
| 132 if (m_frontendChannel) |
| 133 m_frontendChannel->sendProtocolNotification(jsonMessage->toJSONString())
; |
| 134 } |
| 135 {% endfor %} |
| 136 |
| 137 // --------------------- Dispatcher. |
| 138 |
| 139 class DispatcherImpl : public protocol::DispatcherBase { |
| 140 public: |
| 141 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend) |
| 142 : DispatcherBase(frontendChannel) |
| 143 , m_backend(backend) { |
| 144 {% for command in domain.commands %} |
| 145 {% if "redirect" in command %}{% continue %}{% endif %} |
| 146 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} |
| 147 m_dispatchMap.set("{{domain.domain}}.{{command.name}}", &DispatcherImpl:
:{{command.name}}); |
| 148 {% endfor %} |
| 149 } |
| 150 ~DispatcherImpl() override { } |
| 151 void dispatch(int callId, const String16& method, std::unique_ptr<protocol::
DictionaryValue> messageObject) override; |
| 152 |
| 153 protected: |
| 154 using CallHandler = void (DispatcherImpl::*)(int callId, std::unique_ptr<Dic
tionaryValue> messageObject, ErrorSupport* errors); |
| 155 using DispatchMap = protocol::HashMap<String16, CallHandler>; |
| 156 DispatchMap m_dispatchMap; |
| 157 |
| 158 {% for command in domain.commands %} |
| 159 {% if "redirect" in command %}{% continue %}{% endif %} |
| 160 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} |
| 161 void {{command.name}}(int callId, std::unique_ptr<DictionaryValue> requestMe
ssageObject, ErrorSupport*); |
| 162 {% endfor %} |
| 163 |
| 164 Backend* m_backend; |
| 165 }; |
| 166 |
| 167 void DispatcherImpl::dispatch(int callId, const String16& method, std::unique_pt
r<protocol::DictionaryValue> messageObject) |
| 168 { |
| 169 protocol::HashMap<String16, CallHandler>::iterator it = m_dispatchMap.find(m
ethod); |
| 170 if (it == m_dispatchMap.end()) { |
| 171 reportProtocolError(callId, MethodNotFound, "'" + method + "' wasn't fou
nd", nullptr); |
| 172 return; |
| 173 } |
| 174 |
| 175 protocol::ErrorSupport errors; |
| 176 ((*this).*(*it->second))(callId, std::move(messageObject), &errors); |
| 177 } |
| 178 |
| 179 {% for command in domain.commands %} |
| 180 {% if "redirect" in command %}{% continue %}{% endif %} |
| 181 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} |
| 182 {% if "async" in command %} |
| 183 |
| 184 class PLATFORM_EXPORT {{command.name | to_title_case}}CallbackImpl : public Back
end::{{command.name | to_title_case}}Callback, public DispatcherBase::Callback { |
| 185 public: |
| 186 {{command.name | to_title_case}}CallbackImpl(std::unique_ptr<DispatcherBase:
:WeakPtr> backendImpl, int callId) |
| 187 : DispatcherBase::Callback(std::move(backendImpl), callId) { } |
| 188 |
| 189 void sendSuccess( |
| 190 {%- for parameter in command.returns -%} |
| 191 {%- if "optional" in parameter -%} |
| 192 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}} |
| 193 {%- else -%} |
| 194 {{resolve_type(parameter).pass_type}} {{parameter.name}} |
| 195 {%- endif -%} |
| 196 {%- if not loop.last -%}, {% endif -%} |
| 197 {%- endfor -%}) override |
| 198 { |
| 199 std::unique_ptr<protocol::DictionaryValue> resultObject = DictionaryValu
e::create(); |
| 200 {% for parameter in command.returns %} |
| 201 {% if "optional" in parameter %} |
| 202 if ({{parameter.name}}.isJust()) |
| 203 resultObject->setValue("{{parameter.name}}", toValue({{parameter.nam
e}}.fromJust())); |
| 204 {% else %} |
| 205 resultObject->setValue("{{parameter.name}}", toValue({{resolve_type(para
meter).to_raw_type % parameter.name}})); |
| 206 {% endif %} |
| 207 {% endfor %} |
| 208 sendIfActive(std::move(resultObject), ErrorString()); |
| 209 } |
| 210 |
| 211 void sendFailure(const ErrorString& error) override |
| 212 { |
| 213 DCHECK(error.length()); |
| 214 sendIfActive(nullptr, error); |
| 215 } |
| 216 |
| 217 }; |
| 218 {% endif %} |
| 219 |
| 220 void DispatcherImpl::{{command.name}}(int callId, std::unique_ptr<DictionaryValu
e> requestMessageObject, ErrorSupport* errors) |
| 221 { |
| 222 {% if "parameters" in command %} |
| 223 // Prepare input parameters. |
| 224 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje
ct->get("params")); |
| 225 errors->push(); |
| 226 {% for property in command.parameters %} |
| 227 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n
ame}}") : nullptr; |
| 228 {% if property.optional %} |
| 229 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; |
| 230 if ({{property.name}}Value) { |
| 231 errors->setName("{{property.name}}"); |
| 232 in_{{property.name}} = FromValue<{{resolve_type(property).raw_type}}>::p
arse({{property.name}}Value, errors); |
| 233 } |
| 234 {% else %} |
| 235 errors->setName("{{property.name}}"); |
| 236 {{resolve_type(property).type}} in_{{property.name}} = FromValue<{{resolve_t
ype(property).raw_type}}>::parse({{property.name}}Value, errors); |
| 237 {% endif %} |
| 238 {% endfor %} |
| 239 errors->pop(); |
| 240 if (errors->hasErrors()) { |
| 241 reportProtocolError(callId, InvalidParams, kInvalidRequest, errors); |
| 242 return; |
| 243 } |
| 244 {% endif %} |
| 245 {% if "async" in command %} |
| 246 std::unique_ptr<{{command.name | to_title_case}}CallbackImpl> callback(new {
{command.name | to_title_case}}CallbackImpl(weakPtr(), callId)); |
| 247 {% elif "returns" in command %} |
| 248 // Declare output parameters. |
| 249 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create(
); |
| 250 {% for property in command.returns %} |
| 251 {% if "optional" in property %} |
| 252 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}}; |
| 253 {% else %} |
| 254 {{resolve_type(property).type}} out_{{property.name}}; |
| 255 {% endif %} |
| 256 {% endfor %} |
| 257 {% endif %} |
| 258 |
| 259 std::unique_ptr<DispatcherBase::WeakPtr> weak = weakPtr(); |
| 260 ErrorString error; |
| 261 m_backend->{{command.name}}(&error |
| 262 {%- for property in command.parameters -%} |
| 263 {%- if "optional" in property -%} |
| 264 , in_{{property.name}} |
| 265 {%- else -%} |
| 266 , {{resolve_type(property).to_pass_type % ("in_" + property.name)}} |
| 267 {%- endif -%} |
| 268 {%- endfor %} |
| 269 {%- if "async" in command -%} |
| 270 , std::move(callback) |
| 271 {%- elif "returns" in command %} |
| 272 {%- for property in command.returns -%} |
| 273 , &out_{{property.name}} |
| 274 {%- endfor %} |
| 275 {% endif %}); |
| 276 {% if "returns" in command and not("async" in command) %} |
| 277 if (!error.length()) { |
| 278 {% for parameter in command.returns %} |
| 279 {% if "optional" in parameter %} |
| 280 if (out_{{parameter.name}}.isJust()) |
| 281 result->setValue("{{parameter.name}}", toValue(out_{{parameter.name}
}.fromJust())); |
| 282 {% else %} |
| 283 result->setValue("{{parameter.name}}", toValue({{resolve_type(parameter)
.to_raw_type % ("out_" + parameter.name)}})); |
| 284 {% endif %} |
| 285 {% endfor %} |
| 286 } |
| 287 if (weak->get()) |
| 288 weak->get()->sendResponse(callId, error, std::move(result)); |
| 289 {% elif not("async" in command) %} |
| 290 if (weak->get()) |
| 291 weak->get()->sendResponse(callId, error); |
| 292 {% endif %} |
| 293 } |
| 294 {% endfor %} |
| 295 |
| 296 // static |
| 297 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend) |
| 298 { |
| 299 dispatcher->registerBackend("{{domain.domain}}", wrapUnique(new DispatcherIm
pl(dispatcher->channel(), backend))); |
| 300 } |
| 301 |
| 302 } // {{domain.domain}} |
104 {% endfor %} | 303 {% endfor %} |
105 | 304 |
106 } // namespace protocol | 305 } // namespace protocol |
107 } // namespace blink | 306 } // namespace blink |
OLD | NEW |