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

Side by Side Diff: third_party/WebKit/Source/platform/inspector_protocol/Dispatcher_cpp.template

Issue 1738073002: DevTools: introduce protocol::Value, baseline for hierarchical data in remote debugging protocol. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months 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
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 "platform/inspector_protocol/{{class_name}}.h" 7 #include "platform/inspector_protocol/{{class_name}}.h"
8 8
9 #include "platform/JSONParser.h"
10 #include "platform/inspector_protocol/FrontendChannel.h" 9 #include "platform/inspector_protocol/FrontendChannel.h"
10 #include "platform/inspector_protocol/Parser.h"
11 #include "wtf/text/CString.h" 11 #include "wtf/text/CString.h"
12 12
13 namespace blink { 13 namespace blink {
14 namespace protocol { 14 namespace protocol {
15 15
16 using protocol::Maybe; 16 using protocol::Maybe;
17 17
18 class DispatcherImpl : public Dispatcher { 18 class DispatcherImpl : public Dispatcher {
19 public: 19 public:
20 DispatcherImpl(FrontendChannel* frontendChannel) 20 DispatcherImpl(FrontendChannel* frontendChannel)
(...skipping 17 matching lines...) Expand all
38 m_commonErrors.insert(InvalidParams, -32602); 38 m_commonErrors.insert(InvalidParams, -32602);
39 m_commonErrors.insert(InternalError, -32603); 39 m_commonErrors.insert(InternalError, -32603);
40 m_commonErrors.insert(ServerError, -32000); 40 m_commonErrors.insert(ServerError, -32000);
41 } 41 }
42 42
43 virtual void clearFrontend() { m_frontendChannel = 0; } 43 virtual void clearFrontend() { m_frontendChannel = 0; }
44 virtual void dispatch(int sessionId, const String& message); 44 virtual void dispatch(int sessionId, const String& message);
45 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode, const String& errorMessage, ErrorSupport* errors) const; 45 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode, const String& errorMessage, ErrorSupport* errors) const;
46 using Dispatcher::reportProtocolError; 46 using Dispatcher::reportProtocolError;
47 47
48 void sendResponse(int sessionId, int callId, const ErrorString& invocationEr ror, ErrorSupport* errors, PassRefPtr<JSONObject> result); 48 void sendResponse(int sessionId, int callId, const ErrorString& invocationEr ror, ErrorSupport* errors, PassRefPtr<protocol::DictionaryValue> result);
49 bool isActive() { return m_frontendChannel; } 49 bool isActive() { return m_frontendChannel; }
50 50
51 {% for domain in api.domains %} 51 {% for domain in api.domains %}
52 virtual void registerAgent({{domain.domain}}CommandHandler* agent) { ASSERT( !m_{{domain.domain | lower}}Agent); m_{{domain.domain | lower}}Agent = agent; } 52 virtual void registerAgent({{domain.domain}}CommandHandler* agent) { ASSERT( !m_{{domain.domain | lower}}Agent); m_{{domain.domain | lower}}Agent = agent; }
53 {% endfor %} 53 {% endfor %}
54 54
55 private: 55 private:
56 using CallHandler = void (DispatcherImpl::*)(int sessionId, int callId, JSON Object* messageObject, ErrorSupport* errors); 56 using CallHandler = void (DispatcherImpl::*)(int sessionId, int callId, Dict ionaryValue* messageObject, ErrorSupport* errors);
57 using DispatchMap = HashMap<String, CallHandler>; 57 using DispatchMap = HashMap<String, CallHandler>;
58 58
59 {% for domain in api.domains %} 59 {% for domain in api.domains %}
60 {% for command in domain.commands %} 60 {% for command in domain.commands %}
61 {% if "redirect" in command %}{% continue %}{% endif %} 61 {% if "redirect" in command %}{% continue %}{% endif %}
62 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} 62 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %}
63 void {{domain.domain}}_{{command.name}}(int sessionId, int callId, JSONObjec t* requestMessageObject, ErrorSupport*); 63 void {{domain.domain}}_{{command.name}}(int sessionId, int callId, Dictionar yValue* requestMessageObject, ErrorSupport*);
64 {% endfor %} 64 {% endfor %}
65 {% endfor %} 65 {% endfor %}
66 66
67 FrontendChannel* m_frontendChannel; 67 FrontendChannel* m_frontendChannel;
68 68
69 {% for domain in api.domains %} 69 {% for domain in api.domains %}
70 {{domain.domain}}CommandHandler* m_{{domain.domain | lower}}Agent; 70 {{domain.domain}}CommandHandler* m_{{domain.domain | lower}}Agent;
71 {% endfor %} 71 {% endfor %}
72 72
73 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssRefPtr<JSONObject> result) 73 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssRefPtr<protocol::DictionaryValue> result)
74 { 74 {
75 sendResponse(sessionId, callId, invocationError, nullptr, result); 75 sendResponse(sessionId, callId, invocationError, nullptr, result);
76 } 76 }
77 77
78 void sendResponse(int sessionId, int callId, ErrorString invocationError) 78 void sendResponse(int sessionId, int callId, ErrorString invocationError)
79 { 79 {
80 sendResponse(sessionId, callId, invocationError, nullptr, JSONObject::cr eate()); 80 sendResponse(sessionId, callId, invocationError, nullptr, DictionaryValu e::create());
81 } 81 }
82 82
83 static const char InvalidParamsFormatString[]; 83 static const char InvalidParamsFormatString[];
84 84
85 DispatchMap m_dispatchMap; 85 DispatchMap m_dispatchMap;
86 Vector<int> m_commonErrors; 86 Vector<int> m_commonErrors;
87 }; 87 };
88 88
89 const char DispatcherImpl::InvalidParamsFormatString[] = "Some arguments of meth od '%s' can't be processed"; 89 const char DispatcherImpl::InvalidParamsFormatString[] = "Some arguments of meth od '%s' can't be processed";
90 {% for domain in api.domains %} 90 {% for domain in api.domains %}
91 {% for command in domain.commands %} 91 {% for command in domain.commands %}
92 {% if "redirect" in command %}{% continue %}{% endif %} 92 {% if "redirect" in command %}{% continue %}{% endif %}
93 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} 93 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %}
94 94
95 {% if "async" in command %} 95 {% if "async" in command %}
96 Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Cal lback::{{command.name | to_title_case}}Callback(PassRefPtr<DispatcherImpl> backe ndImpl, int sessionId, int id) : CallbackBase(backendImpl, sessionId, id) { } 96 Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Cal lback::{{command.name | to_title_case}}Callback(PassRefPtr<DispatcherImpl> backe ndImpl, int sessionId, int id) : CallbackBase(backendImpl, sessionId, id) { }
97 97
98 void Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case }}Callback::sendSuccess( 98 void Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case }}Callback::sendSuccess(
99 {%- for parameter in command.returns -%} 99 {%- for parameter in command.returns -%}
100 {%- if "optional" in parameter -%} 100 {%- if "optional" in parameter -%}
101 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}} 101 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}}
102 {%- else -%} 102 {%- else -%}
103 {{resolve_type(parameter).pass_type}} {{parameter.name}} 103 {{resolve_type(parameter).pass_type}} {{parameter.name}}
104 {%- endif -%} 104 {%- endif -%}
105 {%- if not loop.last -%}, {% endif -%} 105 {%- if not loop.last -%}, {% endif -%}
106 {% endfor %}) 106 {% endfor %})
107 { 107 {
108 RefPtr<JSONObject> resultObject = JSONObject::create(); 108 RefPtr<protocol::DictionaryValue> resultObject = DictionaryValue::create();
109 {% for parameter in command.returns %} 109 {% for parameter in command.returns %}
110 {% if "optional" in parameter %} 110 {% if "optional" in parameter %}
111 if ({{parameter.name}}.isJust()) 111 if ({{parameter.name}}.isJust())
112 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}}. fromJust())); 112 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}}. fromJust()));
113 {% else %} 113 {% else %}
114 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}})); 114 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}}));
115 {% endif %} 115 {% endif %}
116 {% endfor %} 116 {% endfor %}
117 sendIfActive(resultObject.release(), ErrorString()); 117 sendIfActive(resultObject.release(), ErrorString());
118 } 118 }
119 {% endif %} 119 {% endif %}
120 120
121 void DispatcherImpl::{{domain.domain}}_{{command.name}}(int sessionId, int callI d, JSONObject* requestMessageObject, ErrorSupport* errors) 121 void DispatcherImpl::{{domain.domain}}_{{command.name}}(int sessionId, int callI d, DictionaryValue* requestMessageObject, ErrorSupport* errors)
122 { 122 {
123 if (!m_{{domain.domain | lower}}Agent) 123 if (!m_{{domain.domain | lower}}Agent)
124 errors->addError("{{domain.domain}} handler is not available."); 124 errors->addError("{{domain.domain}} handler is not available.");
125 125
126 if (errors->hasErrors()) { 126 if (errors->hasErrors()) {
127 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), errors); 127 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), errors);
128 return; 128 return;
129 } 129 }
130 {% if "parameters" in command %} 130 {% if "parameters" in command %}
131 131
132 // Prepare input parameters. 132 // Prepare input parameters.
133 RefPtr<JSONObject> object = JSONObject::cast(requestMessageObject->get("para ms")); 133 RefPtr<protocol::DictionaryValue> object = DictionaryValue::cast(requestMess ageObject->get("params"));
134 errors->push(); 134 errors->push();
135 {% for property in command.parameters %} 135 {% for property in command.parameters %}
136 RefPtr<JSONValue> {{property.name}}Value = object ? object->get("{{property. name}}") : nullptr; 136 RefPtr<protocol::Value> {{property.name}}Value = object ? object->get("{{pro perty.name}}") : nullptr;
137 {% if property.optional %} 137 {% if property.optional %}
138 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; 138 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}};
139 if ({{property.name}}Value) { 139 if ({{property.name}}Value) {
140 errors->setName("{{property.name}}"); 140 errors->setName("{{property.name}}");
141 in_{{property.name}} = FromValue<{{resolve_type(property).raw_type}}>::p arse({{property.name}}Value, errors); 141 in_{{property.name}} = FromValue<{{resolve_type(property).raw_type}}>::p arse({{property.name}}Value, errors);
142 } 142 }
143 {% else %} 143 {% else %}
144 errors->setName("{{property.name}}"); 144 errors->setName("{{property.name}}");
145 {{resolve_type(property).type}} in_{{property.name}} = FromValue<{{resolve_t ype(property).raw_type}}>::parse({{property.name}}Value, errors); 145 {{resolve_type(property).type}} in_{{property.name}} = FromValue<{{resolve_t ype(property).raw_type}}>::parse({{property.name}}Value, errors);
146 {% endif %} 146 {% endif %}
147 {% endfor %} 147 {% endfor %}
148 errors->pop(); 148 errors->pop();
149 if (errors->hasErrors()) { 149 if (errors->hasErrors()) {
150 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), errors); 150 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), errors);
151 return; 151 return;
152 } 152 }
153 {% endif %} 153 {% endif %}
154 154
155 {% if "async" in command %} 155 {% if "async" in command %}
156 RefPtr<{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Call back> callback = adoptRef(new {{domain.domain}}CommandHandler::{{command.name | to_title_case}}Callback(this, sessionId, callId)); 156 RefPtr<{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Call back> callback = adoptRef(new {{domain.domain}}CommandHandler::{{command.name | to_title_case}}Callback(this, sessionId, callId));
157 {% elif "returns" in command %} 157 {% elif "returns" in command %}
158 // Declare output parameters. 158 // Declare output parameters.
159 RefPtr<JSONObject> result = JSONObject::create(); 159 RefPtr<protocol::DictionaryValue> result = DictionaryValue::create();
160 {% for property in command.returns %} 160 {% for property in command.returns %}
161 {% if "optional" in property %} 161 {% if "optional" in property %}
162 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}}; 162 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}};
163 {% else %} 163 {% else %}
164 {{resolve_type(property).type}} out_{{property.name}}; 164 {{resolve_type(property).type}} out_{{property.name}};
165 {% endif %} 165 {% endif %}
166 {% endfor %} 166 {% endfor %}
167 {% endif %} 167 {% endif %}
168 168
169 ErrorString error; 169 ErrorString error;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 203
204 PassRefPtr<Dispatcher> Dispatcher::create(FrontendChannel* frontendChannel) 204 PassRefPtr<Dispatcher> Dispatcher::create(FrontendChannel* frontendChannel)
205 { 205 {
206 return adoptRef(new DispatcherImpl(frontendChannel)); 206 return adoptRef(new DispatcherImpl(frontendChannel));
207 } 207 }
208 208
209 void DispatcherImpl::dispatch(int sessionId, const String& message) 209 void DispatcherImpl::dispatch(int sessionId, const String& message)
210 { 210 {
211 RefPtr<Dispatcher> protect(this); 211 RefPtr<Dispatcher> protect(this);
212 int callId = 0; 212 int callId = 0;
213 RefPtr<JSONValue> parsedMessage = parseJSON(message); 213 RefPtr<protocol::Value> parsedMessage = parseJSON(message);
214 ASSERT(parsedMessage); 214 ASSERT(parsedMessage);
215 RefPtr<JSONObject> messageObject = JSONObject::cast(parsedMessage.release()) ; 215 RefPtr<protocol::DictionaryValue> messageObject = DictionaryValue::cast(pars edMessage.release());
216 ASSERT(messageObject); 216 ASSERT(messageObject);
217 217
218 RefPtr<JSONValue> callIdValue = messageObject->get("id"); 218 RefPtr<protocol::Value> callIdValue = messageObject->get("id");
219 bool success = callIdValue->asNumber(&callId); 219 bool success = callIdValue->asNumber(&callId);
220 ASSERT_UNUSED(success, success); 220 ASSERT_UNUSED(success, success);
221 221
222 RefPtr<JSONValue> methodValue = messageObject->get("method"); 222 RefPtr<protocol::Value> methodValue = messageObject->get("method");
223 String method; 223 String method;
224 success = methodValue && methodValue->asString(&method); 224 success = methodValue && methodValue->asString(&method);
225 ASSERT_UNUSED(success, success); 225 ASSERT_UNUSED(success, success);
226 226
227 HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(method); 227 HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(method);
228 if (it == m_dispatchMap.end()) { 228 if (it == m_dispatchMap.end()) {
229 reportProtocolError(sessionId, callId, MethodNotFound, "'" + method + "' wasn't found"); 229 reportProtocolError(sessionId, callId, MethodNotFound, "'" + method + "' wasn't found");
230 return; 230 return;
231 } 231 }
232 232
233 protocol::ErrorSupport errors; 233 protocol::ErrorSupport errors;
234 ((*this).*it->value)(sessionId, callId, messageObject.get(), &errors); 234 ((*this).*it->value)(sessionId, callId, messageObject.get(), &errors);
235 } 235 }
236 236
237 void DispatcherImpl::sendResponse(int sessionId, int callId, const ErrorString& invocationError, ErrorSupport* errors, PassRefPtr<JSONObject> result) 237 void DispatcherImpl::sendResponse(int sessionId, int callId, const ErrorString& invocationError, ErrorSupport* errors, PassRefPtr<protocol::DictionaryValue> res ult)
238 { 238 {
239 if (invocationError.length() || (errors && errors->hasErrors())) { 239 if (invocationError.length() || (errors && errors->hasErrors())) {
240 reportProtocolError(sessionId, callId, ServerError, invocationError, err ors); 240 reportProtocolError(sessionId, callId, ServerError, invocationError, err ors);
241 return; 241 return;
242 } 242 }
243 243
244 RefPtr<JSONObject> responseMessage = JSONObject::create(); 244 RefPtr<protocol::DictionaryValue> responseMessage = DictionaryValue::create( );
245 responseMessage->setNumber("id", callId); 245 responseMessage->setNumber("id", callId);
246 responseMessage->setObject("result", result); 246 responseMessage->setObject("result", result);
247 if (m_frontendChannel) 247 if (m_frontendChannel)
248 m_frontendChannel->sendProtocolResponse(sessionId, callId, responseMessa ge.release()); 248 m_frontendChannel->sendProtocolResponse(sessionId, callId, responseMessa ge.release());
249 } 249 }
250 250
251 void Dispatcher::reportProtocolError(int sessionId, int callId, CommonErrorCode code, const String& errorMessage) const 251 void Dispatcher::reportProtocolError(int sessionId, int callId, CommonErrorCode code, const String& errorMessage) const
252 { 252 {
253 ErrorSupport errors; 253 ErrorSupport errors;
254 reportProtocolError(sessionId, callId, code, errorMessage, &errors); 254 reportProtocolError(sessionId, callId, code, errorMessage, &errors);
255 } 255 }
256 256
257 void DispatcherImpl::reportProtocolError(int sessionId, int callId, CommonErrorC ode code, const String& errorMessage, ErrorSupport* errors) const 257 void DispatcherImpl::reportProtocolError(int sessionId, int callId, CommonErrorC ode code, const String& errorMessage, ErrorSupport* errors) const
258 { 258 {
259 ASSERT(code >=0); 259 ASSERT(code >=0);
260 ASSERT((unsigned)code < m_commonErrors.size()); 260 ASSERT((unsigned)code < m_commonErrors.size());
261 ASSERT(m_commonErrors[code]); 261 ASSERT(m_commonErrors[code]);
262 RefPtr<JSONObject> error = JSONObject::create(); 262 RefPtr<protocol::DictionaryValue> error = DictionaryValue::create();
263 error->setNumber("code", m_commonErrors[code]); 263 error->setNumber("code", m_commonErrors[code]);
264 error->setString("message", errorMessage); 264 error->setString("message", errorMessage);
265 ASSERT(error); 265 ASSERT(error);
266 if (errors && errors->hasErrors()) 266 if (errors && errors->hasErrors())
267 error->setString("data", errors->errors()); 267 error->setString("data", errors->errors());
268 RefPtr<JSONObject> message = JSONObject::create(); 268 RefPtr<protocol::DictionaryValue> message = DictionaryValue::create();
269 message->setObject("error", error); 269 message->setObject("error", error);
270 message->setNumber("id", callId); 270 message->setNumber("id", callId);
271 if (m_frontendChannel) 271 if (m_frontendChannel)
272 m_frontendChannel->sendProtocolResponse(sessionId, callId, message.relea se()); 272 m_frontendChannel->sendProtocolResponse(sessionId, callId, message.relea se());
273 } 273 }
274 274
275 bool Dispatcher::getCommandName(const String& message, String* result) 275 bool Dispatcher::getCommandName(const String& message, String* result)
276 { 276 {
277 RefPtr<JSONValue> value = parseJSON(message); 277 RefPtr<protocol::Value> value = parseJSON(message);
278 if (!value) 278 if (!value)
279 return false; 279 return false;
280 280
281 RefPtr<JSONObject> object = JSONObject::cast(value.release()); 281 RefPtr<protocol::DictionaryValue> object = DictionaryValue::cast(value.relea se());
282 if (!object) 282 if (!object)
283 return false; 283 return false;
284 284
285 if (!object->getString("method", result)) 285 if (!object->getString("method", result))
286 return false; 286 return false;
287 287
288 return true; 288 return true;
289 } 289 }
290 290
291 Dispatcher::CallbackBase::CallbackBase(PassRefPtr<DispatcherImpl> backendImpl, i nt sessionId, int id) 291 Dispatcher::CallbackBase::CallbackBase(PassRefPtr<DispatcherImpl> backendImpl, i nt sessionId, int id)
292 : m_backendImpl(backendImpl), m_sessionId(sessionId), m_id(id), m_alreadySen t(false) { } 292 : m_backendImpl(backendImpl), m_sessionId(sessionId), m_id(id), m_alreadySen t(false) { }
293 293
294 Dispatcher::CallbackBase::~CallbackBase() { } 294 Dispatcher::CallbackBase::~CallbackBase() { }
295 295
296 void Dispatcher::CallbackBase::sendFailure(const ErrorString& error) 296 void Dispatcher::CallbackBase::sendFailure(const ErrorString& error)
297 { 297 {
298 ASSERT(error.length()); 298 ASSERT(error.length());
299 sendIfActive(nullptr, error); 299 sendIfActive(nullptr, error);
300 } 300 }
301 301
302 bool Dispatcher::CallbackBase::isActive() 302 bool Dispatcher::CallbackBase::isActive()
303 { 303 {
304 return !m_alreadySent && m_backendImpl->isActive(); 304 return !m_alreadySent && m_backendImpl->isActive();
305 } 305 }
306 306
307 void Dispatcher::CallbackBase::sendIfActive(PassRefPtr<JSONObject> partialMessag e, const ErrorString& invocationError) 307 void Dispatcher::CallbackBase::sendIfActive(PassRefPtr<protocol::DictionaryValue > partialMessage, const ErrorString& invocationError)
308 { 308 {
309 if (m_alreadySent) 309 if (m_alreadySent)
310 return; 310 return;
311 m_backendImpl->sendResponse(m_sessionId, m_id, invocationError, nullptr, par tialMessage); 311 m_backendImpl->sendResponse(m_sessionId, m_id, invocationError, nullptr, par tialMessage);
312 m_alreadySent = true; 312 m_alreadySent = true;
313 } 313 }
314 314
315 } // namespace protocol 315 } // namespace protocol
316 } // namespace blink 316 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698