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

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

Issue 1767883002: DevTools: generate string16-based handlers for v8_inspector. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: for landing 2 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/inspector_protocol/Collections.h" 9 #include "platform/inspector_protocol/Collections.h"
10 #include "platform/inspector_protocol/FrontendChannel.h" 10 #include "platform/inspector_protocol/FrontendChannel.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 m_weakPtrs.clear(); 63 m_weakPtrs.clear();
64 } 64 }
65 65
66 PassOwnPtr<DispatcherImplWeakPtr> weakPtr() 66 PassOwnPtr<DispatcherImplWeakPtr> weakPtr()
67 { 67 {
68 OwnPtr<DispatcherImplWeakPtr> weak = adoptPtr(new DispatcherImplWeakPtr( this)); 68 OwnPtr<DispatcherImplWeakPtr> weak = adoptPtr(new DispatcherImplWeakPtr( this));
69 m_weakPtrs.add(weak.get()); 69 m_weakPtrs.add(weak.get());
70 return weak.release(); 70 return weak.release();
71 } 71 }
72 72
73 virtual void dispatch(int sessionId, const String& message); 73 virtual void dispatch(int sessionId, const String16& message);
74 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode, const String& errorMessage, ErrorSupport* errors) const; 74 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode, const String16& errorMessage, ErrorSupport* errors) const;
75 using Dispatcher::reportProtocolError; 75 using Dispatcher::reportProtocolError;
76 76
77 void sendResponse(int sessionId, int callId, const ErrorString& invocationEr ror, ErrorSupport* errors, PassOwnPtr<protocol::DictionaryValue> result); 77 void sendResponse(int sessionId, int callId, const ErrorString& invocationEr ror, ErrorSupport* errors, PassOwnPtr<protocol::DictionaryValue> result);
78 78
79 {% for domain in api.domains %} 79 {% for domain in api.domains %}
80 virtual void registerAgent({{domain.domain}}CommandHandler* agent) { ASSERT( !m_{{domain.domain | lower}}Agent); m_{{domain.domain | lower}}Agent = agent; } 80 virtual void registerAgent({{domain.domain}}CommandHandler* agent) { ASSERT( !m_{{domain.domain | lower}}Agent); m_{{domain.domain | lower}}Agent = agent; }
81 {% endfor %} 81 {% endfor %}
82 82
83 private: 83 private:
84 friend class CallbackBase; 84 friend class CallbackBase;
85 friend class DispatcherImplWeakPtr; 85 friend class DispatcherImplWeakPtr;
86 using CallHandler = void (DispatcherImpl::*)(int sessionId, int callId, Pass OwnPtr<DictionaryValue> messageObject, ErrorSupport* errors); 86 using CallHandler = void (DispatcherImpl::*)(int sessionId, int callId, Pass OwnPtr<DictionaryValue> messageObject, ErrorSupport* errors);
87 using DispatchMap = protocol::HashMap<String, CallHandler>; 87 using DispatchMap = protocol::HashMap<String16, CallHandler>;
88 88
89 {% for domain in api.domains %} 89 {% for domain in api.domains %}
90 {% for command in domain.commands %} 90 {% for command in domain.commands %}
91 {% if "redirect" in command %}{% continue %}{% endif %} 91 {% if "redirect" in command %}{% continue %}{% endif %}
92 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} 92 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %}
93 void {{domain.domain}}_{{command.name}}(int sessionId, int callId, PassOwnPt r<DictionaryValue> requestMessageObject, ErrorSupport*); 93 void {{domain.domain}}_{{command.name}}(int sessionId, int callId, PassOwnPt r<DictionaryValue> requestMessageObject, ErrorSupport*);
94 {% endfor %} 94 {% endfor %}
95 {% endfor %} 95 {% endfor %}
96 96
97 FrontendChannel* m_frontendChannel; 97 FrontendChannel* m_frontendChannel;
98 98
99 {% for domain in api.domains %} 99 {% for domain in api.domains %}
100 {{domain.domain}}CommandHandler* m_{{domain.domain | lower}}Agent; 100 {{domain.domain}}CommandHandler* m_{{domain.domain | lower}}Agent;
101 {% endfor %} 101 {% endfor %}
102 102
103 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssOwnPtr<protocol::DictionaryValue> result) 103 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssOwnPtr<protocol::DictionaryValue> result)
104 { 104 {
105 sendResponse(sessionId, callId, invocationError, nullptr, result); 105 sendResponse(sessionId, callId, invocationError, nullptr, result);
106 } 106 }
107 107
108 void sendResponse(int sessionId, int callId, ErrorString invocationError) 108 void sendResponse(int sessionId, int callId, ErrorString invocationError)
109 { 109 {
110 sendResponse(sessionId, callId, invocationError, nullptr, DictionaryValu e::create()); 110 sendResponse(sessionId, callId, invocationError, nullptr, DictionaryValu e::create());
111 } 111 }
112 112
113 static const char InvalidParamsFormatString[]; 113 static const char kInvalidRequest[];
114 114
115 DispatchMap m_dispatchMap; 115 DispatchMap m_dispatchMap;
116 protocol::Vector<int> m_commonErrors; 116 protocol::Vector<int> m_commonErrors;
117 protocol::HashSet<DispatcherImplWeakPtr*> m_weakPtrs; 117 protocol::HashSet<DispatcherImplWeakPtr*> m_weakPtrs;
118 }; 118 };
119 119
120 DispatcherImplWeakPtr::~DispatcherImplWeakPtr() 120 DispatcherImplWeakPtr::~DispatcherImplWeakPtr()
121 { 121 {
122 if (m_dispatcher) 122 if (m_dispatcher)
123 m_dispatcher->m_weakPtrs.remove(this); 123 m_dispatcher->m_weakPtrs.remove(this);
124 } 124 }
125 125
126 const char DispatcherImpl::InvalidParamsFormatString[] = "Some arguments of meth od '%s' can't be processed"; 126 const char DispatcherImpl::kInvalidRequest[] = "Invalid request";
127
127 {% for domain in api.domains %} 128 {% for domain in api.domains %}
128 {% for command in domain.commands %} 129 {% for command in domain.commands %}
129 {% if "redirect" in command %}{% continue %}{% endif %} 130 {% if "redirect" in command %}{% continue %}{% endif %}
130 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} 131 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %}
131 132
132 {% if "async" in command %} 133 {% if "async" in command %}
133 Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Cal lback::{{command.name | to_title_case}}Callback(PassOwnPtr<DispatcherImplWeakPtr > backendImpl, int sessionId, int id) : CallbackBase(backendImpl, sessionId, id) { } 134 Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Cal lback::{{command.name | to_title_case}}Callback(PassOwnPtr<DispatcherImplWeakPtr > backendImpl, int sessionId, int id) : CallbackBase(backendImpl, sessionId, id) { }
134 135
135 void Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case }}Callback::sendSuccess( 136 void Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case }}Callback::sendSuccess(
136 {%- for parameter in command.returns -%} 137 {%- for parameter in command.returns -%}
(...skipping 17 matching lines...) Expand all
154 sendIfActive(resultObject.release(), ErrorString()); 155 sendIfActive(resultObject.release(), ErrorString());
155 } 156 }
156 {% endif %} 157 {% endif %}
157 158
158 void DispatcherImpl::{{domain.domain}}_{{command.name}}(int sessionId, int callI d, PassOwnPtr<DictionaryValue> requestMessageObject, ErrorSupport* errors) 159 void DispatcherImpl::{{domain.domain}}_{{command.name}}(int sessionId, int callI d, PassOwnPtr<DictionaryValue> requestMessageObject, ErrorSupport* errors)
159 { 160 {
160 if (!m_{{domain.domain | lower}}Agent) 161 if (!m_{{domain.domain | lower}}Agent)
161 errors->addError("{{domain.domain}} handler is not available."); 162 errors->addError("{{domain.domain}} handler is not available.");
162 163
163 if (errors->hasErrors()) { 164 if (errors->hasErrors()) {
164 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), errors); 165 reportProtocolError(sessionId, callId, InvalidParams, kInvalidRequest, e rrors);
165 return; 166 return;
166 } 167 }
167 {% if "parameters" in command %} 168 {% if "parameters" in command %}
168 169
169 // Prepare input parameters. 170 // Prepare input parameters.
170 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje ct->get("params")); 171 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje ct->get("params"));
171 errors->push(); 172 errors->push();
172 {% for property in command.parameters %} 173 {% for property in command.parameters %}
173 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n ame}}") : nullptr; 174 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n ame}}") : nullptr;
174 {% if property.optional %} 175 {% if property.optional %}
175 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; 176 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}};
176 if ({{property.name}}Value) { 177 if ({{property.name}}Value) {
177 errors->setName("{{property.name}}"); 178 errors->setName("{{property.name}}");
178 in_{{property.name}} = FromValue<{{resolve_type(property).raw_type}}>::p arse({{property.name}}Value, errors); 179 in_{{property.name}} = FromValue<{{resolve_type(property).raw_type}}>::p arse({{property.name}}Value, errors);
179 } 180 }
180 {% else %} 181 {% else %}
181 errors->setName("{{property.name}}"); 182 errors->setName("{{property.name}}");
182 {{resolve_type(property).type}} in_{{property.name}} = FromValue<{{resolve_t ype(property).raw_type}}>::parse({{property.name}}Value, errors); 183 {{resolve_type(property).type}} in_{{property.name}} = FromValue<{{resolve_t ype(property).raw_type}}>::parse({{property.name}}Value, errors);
183 {% endif %} 184 {% endif %}
184 {% endfor %} 185 {% endfor %}
185 errors->pop(); 186 errors->pop();
186 if (errors->hasErrors()) { 187 if (errors->hasErrors()) {
187 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), errors); 188 reportProtocolError(sessionId, callId, InvalidParams, kInvalidRequest, e rrors);
188 return; 189 return;
189 } 190 }
190 {% endif %} 191 {% endif %}
191 192
192 {% if "async" in command %} 193 {% if "async" in command %}
193 OwnPtr<{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Call back> callback = adoptPtr(new {{domain.domain}}CommandHandler::{{command.name | to_title_case}}Callback(weakPtr(), sessionId, callId)); 194 OwnPtr<{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Call back> callback = adoptPtr(new {{domain.domain}}CommandHandler::{{command.name | to_title_case}}Callback(weakPtr(), sessionId, callId));
194 {% elif "returns" in command %} 195 {% elif "returns" in command %}
195 // Declare output parameters. 196 // Declare output parameters.
196 OwnPtr<protocol::DictionaryValue> result = DictionaryValue::create(); 197 OwnPtr<protocol::DictionaryValue> result = DictionaryValue::create();
197 {% for property in command.returns %} 198 {% for property in command.returns %}
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 {% endif %} 240 {% endif %}
240 } 241 }
241 {% endfor %} 242 {% endfor %}
242 {% endfor %} 243 {% endfor %}
243 244
244 PassOwnPtr<Dispatcher> Dispatcher::create(FrontendChannel* frontendChannel) 245 PassOwnPtr<Dispatcher> Dispatcher::create(FrontendChannel* frontendChannel)
245 { 246 {
246 return adoptPtr(new DispatcherImpl(frontendChannel)); 247 return adoptPtr(new DispatcherImpl(frontendChannel));
247 } 248 }
248 249
249 void DispatcherImpl::dispatch(int sessionId, const String& message) 250 void DispatcherImpl::dispatch(int sessionId, const String16& message)
250 { 251 {
251 int callId = 0; 252 int callId = 0;
252 OwnPtr<protocol::Value> parsedMessage = parseJSON(message); 253 OwnPtr<protocol::Value> parsedMessage = parseJSON(message);
253 ASSERT(parsedMessage); 254 ASSERT(parsedMessage);
254 OwnPtr<protocol::DictionaryValue> messageObject = DictionaryValue::cast(pars edMessage.release()); 255 OwnPtr<protocol::DictionaryValue> messageObject = DictionaryValue::cast(pars edMessage.release());
255 ASSERT(messageObject); 256 ASSERT(messageObject);
256 257
257 protocol::Value* callIdValue = messageObject->get("id"); 258 protocol::Value* callIdValue = messageObject->get("id");
258 bool success = callIdValue->asNumber(&callId); 259 bool success = callIdValue->asNumber(&callId);
259 ASSERT_UNUSED(success, success); 260 ASSERT_UNUSED(success, success);
260 261
261 protocol::Value* methodValue = messageObject->get("method"); 262 protocol::Value* methodValue = messageObject->get("method");
262 String method; 263 String16 method;
263 success = methodValue && methodValue->asString(&method); 264 success = methodValue && methodValue->asString(&method);
264 ASSERT_UNUSED(success, success); 265 ASSERT_UNUSED(success, success);
265 266
266 protocol::HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(met hod); 267 protocol::HashMap<String16, CallHandler>::iterator it = m_dispatchMap.find(m ethod);
267 if (it == m_dispatchMap.end()) { 268 if (it == m_dispatchMap.end()) {
268 reportProtocolError(sessionId, callId, MethodNotFound, "'" + method + "' wasn't found"); 269 reportProtocolError(sessionId, callId, MethodNotFound, "'" + method + "' wasn't found");
269 return; 270 return;
270 } 271 }
271 272
272 protocol::ErrorSupport errors; 273 protocol::ErrorSupport errors;
273 ((*this).*(*it->second))(sessionId, callId, messageObject.release(), &errors ); 274 ((*this).*(*it->second))(sessionId, callId, messageObject.release(), &errors );
274 } 275 }
275 276
276 void DispatcherImpl::sendResponse(int sessionId, int callId, const ErrorString& invocationError, ErrorSupport* errors, PassOwnPtr<protocol::DictionaryValue> res ult) 277 void DispatcherImpl::sendResponse(int sessionId, int callId, const ErrorString& invocationError, ErrorSupport* errors, PassOwnPtr<protocol::DictionaryValue> res ult)
277 { 278 {
278 if (invocationError.length() || (errors && errors->hasErrors())) { 279 if (invocationError.length() || (errors && errors->hasErrors())) {
279 reportProtocolError(sessionId, callId, ServerError, invocationError, err ors); 280 reportProtocolError(sessionId, callId, ServerError, invocationError, err ors);
280 return; 281 return;
281 } 282 }
282 283
283 OwnPtr<protocol::DictionaryValue> responseMessage = DictionaryValue::create( ); 284 OwnPtr<protocol::DictionaryValue> responseMessage = DictionaryValue::create( );
284 responseMessage->setNumber("id", callId); 285 responseMessage->setNumber("id", callId);
285 responseMessage->setObject("result", result); 286 responseMessage->setObject("result", result);
286 if (m_frontendChannel) 287 if (m_frontendChannel)
287 m_frontendChannel->sendProtocolResponse(sessionId, callId, responseMessa ge.release()); 288 m_frontendChannel->sendProtocolResponse(sessionId, callId, responseMessa ge.release());
288 } 289 }
289 290
290 void Dispatcher::reportProtocolError(int sessionId, int callId, CommonErrorCode code, const String& errorMessage) const 291 void Dispatcher::reportProtocolError(int sessionId, int callId, CommonErrorCode code, const String16& errorMessage) const
291 { 292 {
292 ErrorSupport errors; 293 ErrorSupport errors;
293 reportProtocolError(sessionId, callId, code, errorMessage, &errors); 294 reportProtocolError(sessionId, callId, code, errorMessage, &errors);
294 } 295 }
295 296
296 void DispatcherImpl::reportProtocolError(int sessionId, int callId, CommonErrorC ode code, const String& errorMessage, ErrorSupport* errors) const 297 void DispatcherImpl::reportProtocolError(int sessionId, int callId, CommonErrorC ode code, const String16& errorMessage, ErrorSupport* errors) const
297 { 298 {
298 ASSERT(code >=0); 299 ASSERT(code >=0);
299 ASSERT((unsigned)code < m_commonErrors.size()); 300 ASSERT((unsigned)code < m_commonErrors.size());
300 ASSERT(m_commonErrors[code]); 301 ASSERT(m_commonErrors[code]);
301 OwnPtr<protocol::DictionaryValue> error = DictionaryValue::create(); 302 OwnPtr<protocol::DictionaryValue> error = DictionaryValue::create();
302 error->setNumber("code", m_commonErrors[code]); 303 error->setNumber("code", m_commonErrors[code]);
303 error->setString("message", errorMessage); 304 error->setString("message", errorMessage);
304 ASSERT(error); 305 ASSERT(error);
305 if (errors && errors->hasErrors()) 306 if (errors && errors->hasErrors())
306 error->setString("data", errors->errors()); 307 error->setString("data", errors->errors());
307 OwnPtr<protocol::DictionaryValue> message = DictionaryValue::create(); 308 OwnPtr<protocol::DictionaryValue> message = DictionaryValue::create();
308 message->setObject("error", error.release()); 309 message->setObject("error", error.release());
309 message->setNumber("id", callId); 310 message->setNumber("id", callId);
310 if (m_frontendChannel) 311 if (m_frontendChannel)
311 m_frontendChannel->sendProtocolResponse(sessionId, callId, message.relea se()); 312 m_frontendChannel->sendProtocolResponse(sessionId, callId, message.relea se());
312 } 313 }
313 314
314 bool Dispatcher::getCommandName(const String& message, String* result) 315 bool Dispatcher::getCommandName(const String16& message, String16* result)
315 { 316 {
316 OwnPtr<protocol::Value> value = parseJSON(message); 317 OwnPtr<protocol::Value> value = parseJSON(message);
317 if (!value) 318 if (!value)
318 return false; 319 return false;
319 320
320 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); 321 protocol::DictionaryValue* object = DictionaryValue::cast(value.get());
321 if (!object) 322 if (!object)
322 return false; 323 return false;
323 324
324 if (!object->getString("method", result)) 325 if (!object->getString("method", result))
(...skipping 21 matching lines...) Expand all
346 void Dispatcher::CallbackBase::sendIfActive(PassOwnPtr<protocol::DictionaryValue > partialMessage, const ErrorString& invocationError) 347 void Dispatcher::CallbackBase::sendIfActive(PassOwnPtr<protocol::DictionaryValue > partialMessage, const ErrorString& invocationError)
347 { 348 {
348 if (!m_backendImpl->get()) 349 if (!m_backendImpl->get())
349 return; 350 return;
350 m_backendImpl->get()->sendResponse(m_sessionId, m_id, invocationError, nullp tr, partialMessage); 351 m_backendImpl->get()->sendResponse(m_sessionId, m_id, invocationError, nullp tr, partialMessage);
351 m_backendImpl = nullptr; 352 m_backendImpl = nullptr;
352 } 353 }
353 354
354 } // namespace protocol 355 } // namespace protocol
355 } // namespace blink 356 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698