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

Side by Side Diff: third_party/WebKit/Source/platform/inspector_protocol/CodeGeneratorStrings.py

Issue 1702673002: DevTools: migrate remote debugging protocol generators to jinja2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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
(Empty)
1 # Copyright (c) 2013 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 # THis file contains string resources for CodeGenerator.
30 # Its syntax is a Python syntax subset, suitable for manual parsing.
31
32 frontend_domain_class = (
33 """ class PLATFORM_EXPORT $domainClassName {
34 public:
35 static $domainClassName* from(Frontend* frontend) { return &(frontend->m _$domainFieldName) ;}
36 $domainClassName(FrontendChannel* frontendChannel) : m_frontendChannel(f rontendChannel) { }
37 ${frontendDomainMethodDeclarations}
38 void flush() { m_frontendChannel->flush(); }
39 private:
40 FrontendChannel* m_frontendChannel;
41 };
42
43 """)
44
45 backend_method = (
46 """void DispatcherImpl::${domainName}_$methodName(int sessionId, int callId, JSO NObject*$requestMessageObject, JSONArray* protocolErrors)
47 {
48 if (!$agentField)
49 protocolErrors->pushString("${domainName} handler is not available.");
50 $methodCode
51 if (protocolErrors->length()) {
52 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, commandName($commandNameIndex)), protocolErrors);
53 return;
54 }
55 $agentCallParamsDeclaration
56 $agentField->$methodName($agentCallParams);
57 $responseCook
58 sendResponse(sessionId, callId, $sendResponseCallParams);
59 }
60 """)
61
62 frontend_method = ("""void Frontend::$domainName::$eventName($parameters)
63 {
64 RefPtr<JSONObject> jsonMessage = JSONObject::create();
65 jsonMessage->setString("method", "$domainName.$eventName");
66 $code if (m_frontendChannel)
67 m_frontendChannel->sendProtocolNotification(jsonMessage.release());
68 }
69 """)
70
71 callback_main_methods = ("""
72 Dispatcher::$agentName::$callbackName::$callbackName(PassRefPtr<DispatcherImpl> backendImpl, int sessionId, int id) : CallbackBase(backendImpl, sessionId, id) { }
73
74 void Dispatcher::$agentName::$callbackName::sendSuccess($parameters)
75 {
76 RefPtr<JSONObject> jsonMessage = JSONObject::create();
77 $code sendIfActive(jsonMessage, ErrorString(), PassRefPtr<JSONValue>());
78 }
79 """)
80
81
82 frontend_h = (
83 """#ifndef Frontend_h
84 #define Frontend_h
85
86 #include "platform/inspector_protocol/TypeBuilder.h"
87
88 #include "platform/JSONValues.h"
89 #include "platform/PlatformExport.h"
90 #include "platform/inspector_protocol/FrontendChannel.h"
91 #include "wtf/PassRefPtr.h"
92 #include "wtf/text/WTFString.h"
93
94 namespace blink {
95 namespace protocol {
96
97 using ErrorString = String;
98
99 class PLATFORM_EXPORT Frontend {
100 public:
101 Frontend(FrontendChannel*);
102 FrontendChannel* channel() { return m_frontendChannel; }
103
104 $domainClassList
105 private:
106 FrontendChannel* m_frontendChannel;
107 ${fieldDeclarations}};
108
109 } // namespace protocol
110 } // namespace blink
111 #endif // !defined(Frontend_h)
112 """)
113
114 backend_h = (
115 """#ifndef Dispatcher_h
116 #define Dispatcher_h
117
118 #include "platform/inspector_protocol/TypeBuilder.h"
119
120 #include "platform/PlatformExport.h"
121 #include "platform/heap/Handle.h"
122 #include "wtf/PassRefPtr.h"
123 #include "wtf/RefCounted.h"
124 #include "wtf/text/WTFString.h"
125
126 namespace blink {
127
128 class JSONObject;
129 class JSONArray;
130
131 namespace protocol {
132
133 class FrontendChannel;
134
135 using ErrorString = String;
136
137 class DispatcherImpl;
138
139 class PLATFORM_EXPORT Dispatcher: public RefCounted<Dispatcher> {
140 public:
141 static PassRefPtr<Dispatcher> create(FrontendChannel* frontendChannel);
142 virtual ~Dispatcher() { }
143
144 class PLATFORM_EXPORT CallbackBase: public RefCounted<CallbackBase> {
145 public:
146 CallbackBase(PassRefPtr<DispatcherImpl> backendImpl, int sessionId, int id);
147 virtual ~CallbackBase();
148 void sendFailure(const ErrorString&);
149 bool isActive();
150
151 protected:
152 void sendIfActive(PassRefPtr<JSONObject> partialMessage, const ErrorStri ng& invocationError, PassRefPtr<JSONValue> errorData);
153
154 private:
155 void disable() { m_alreadySent = true; }
156
157 RefPtr<DispatcherImpl> m_backendImpl;
158 int m_sessionId;
159 int m_id;
160 bool m_alreadySent;
161
162 friend class DispatcherImpl;
163 };
164
165 $agentInterfaces
166 $virtualSetters
167
168 virtual void clearFrontend() = 0;
169
170 enum CommonErrorCode {
171 ParseError = 0,
172 InvalidRequest,
173 MethodNotFound,
174 InvalidParams,
175 InternalError,
176 ServerError,
177 LastEntry,
178 };
179
180 void reportProtocolError(int sessionId, int callId, CommonErrorCode, const S tring& errorMessage) const;
181 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode, const String& errorMessage, PassRefPtr<JSONValue> data) const = 0;
182 virtual void dispatch(int sessionId, const String& message) = 0;
183 static bool getCommandName(const String& message, String* result);
184
185 enum MethodNames {
186 $methodNamesEnumContent
187
188 kMethodNamesEnumSize
189 };
190
191 static const char* commandName(MethodNames);
192
193 private:
194 static const char commandNames[];
195 static const unsigned short commandNamesIndex[];
196 };
197
198 } // namespace protocol
199 } // namespace blink
200
201 using blink::protocol::ErrorString;
202
203 #endif // !defined(Dispatcher_h)
204
205
206 """)
207
208 backend_cpp = (
209 """
210
211 #include "platform/inspector_protocol/Dispatcher.h"
212
213 #include "platform/JSONParser.h"
214 #include "platform/JSONValues.h"
215 #include "platform/inspector_protocol/FrontendChannel.h"
216 #include "wtf/text/CString.h"
217 #include "wtf/text/WTFString.h"
218
219 namespace blink {
220 namespace protocol {
221
222 const char Dispatcher::commandNames[] = {
223 $methodNameDeclarations
224 };
225
226 const unsigned short Dispatcher::commandNamesIndex[] = {
227 $methodNameDeclarationsIndex
228 };
229
230 const char* Dispatcher::commandName(MethodNames index) {
231 static_assert(static_cast<int>(kMethodNamesEnumSize) == WTF_ARRAY_LENGTH(com mandNamesIndex), "MethodNames enum should have the same number of elements as co mmandNamesIndex");
232 return commandNames + commandNamesIndex[index];
233 }
234
235 class DispatcherImpl : public Dispatcher {
236 public:
237 DispatcherImpl(FrontendChannel* frontendChannel)
238 : m_frontendChannel(frontendChannel)
239 $constructorInit
240 {
241 // Initialize dispatch map.
242 const CallHandler handlers[] = {
243 $messageHandlers
244 };
245 for (size_t i = 0; i < kMethodNamesEnumSize; ++i)
246 m_dispatchMap.add(commandName(static_cast<MethodNames>(i)), handlers [i]);
247
248 // Initialize common errors.
249 m_commonErrors.insert(ParseError, -32700);
250 m_commonErrors.insert(InvalidRequest, -32600);
251 m_commonErrors.insert(MethodNotFound, -32601);
252 m_commonErrors.insert(InvalidParams, -32602);
253 m_commonErrors.insert(InternalError, -32603);
254 m_commonErrors.insert(ServerError, -32000);
255 }
256
257 virtual void clearFrontend() { m_frontendChannel = 0; }
258 virtual void dispatch(int sessionId, const String& message);
259 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode, const String& errorMessage, PassRefPtr<JSONValue> data) const;
260 using Dispatcher::reportProtocolError;
261
262 void sendResponse(int sessionId, int callId, const ErrorString& invocationEr ror, PassRefPtr<JSONValue> errorData, PassRefPtr<JSONObject> result);
263 bool isActive() { return m_frontendChannel; }
264
265 $setters
266 private:
267 using CallHandler = void (DispatcherImpl::*)(int sessionId, int callId, JSON Object* messageObject, JSONArray* protocolErrors);
268 using DispatchMap = HashMap<String, CallHandler>;
269
270 $methodDeclarations
271
272 FrontendChannel* m_frontendChannel;
273 $fieldDeclarations
274
275 template<typename R, typename V, typename V0>
276 static R getPropertyValueImpl(JSONObject* object, const char* name, bool* va lueFound, JSONArray* protocolErrors, V0 initial_value, bool (*as_method)(JSONVal ue*, V*), const char* type_name);
277
278 static int getInt(JSONObject* object, const char* name, bool* valueFound, JS ONArray* protocolErrors);
279 static double getDouble(JSONObject* object, const char* name, bool* valueFou nd, JSONArray* protocolErrors);
280 static String getString(JSONObject* object, const char* name, bool* valueFou nd, JSONArray* protocolErrors);
281 static bool getBoolean(JSONObject* object, const char* name, bool* valueFoun d, JSONArray* protocolErrors);
282 static PassRefPtr<JSONObject> getObject(JSONObject* object, const char* name , bool* valueFound, JSONArray* protocolErrors);
283 static PassRefPtr<JSONArray> getArray(JSONObject* object, const char* name, bool* valueFound, JSONArray* protocolErrors);
284
285 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssRefPtr<JSONObject> result)
286 {
287 sendResponse(sessionId, callId, invocationError, RefPtr<JSONValue>(), re sult);
288 }
289 void sendResponse(int sessionId, int callId, ErrorString invocationError)
290 {
291 sendResponse(sessionId, callId, invocationError, RefPtr<JSONValue>(), JS ONObject::create());
292 }
293 static const char InvalidParamsFormatString[];
294
295 DispatchMap m_dispatchMap;
296 Vector<int> m_commonErrors;
297 };
298
299 const char DispatcherImpl::InvalidParamsFormatString[] = "Some arguments of meth od '%s' can't be processed";
300
301 $methods
302
303 PassRefPtr<Dispatcher> Dispatcher::create(FrontendChannel* frontendChannel)
304 {
305 return adoptRef(new DispatcherImpl(frontendChannel));
306 }
307
308
309 void DispatcherImpl::dispatch(int sessionId, const String& message)
310 {
311 RefPtr<Dispatcher> protect(this);
312 int callId = 0;
313 RefPtr<JSONValue> parsedMessage = parseJSON(message);
314 ASSERT(parsedMessage);
315 RefPtr<JSONObject> messageObject = parsedMessage->asObject();
316 ASSERT(messageObject);
317
318 RefPtr<JSONValue> callIdValue = messageObject->get("id");
319 bool success = callIdValue->asNumber(&callId);
320 ASSERT_UNUSED(success, success);
321
322 RefPtr<JSONValue> methodValue = messageObject->get("method");
323 String method;
324 success = methodValue && methodValue->asString(&method);
325 ASSERT_UNUSED(success, success);
326
327 HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(method);
328 if (it == m_dispatchMap.end()) {
329 reportProtocolError(sessionId, callId, MethodNotFound, "'" + method + "' wasn't found");
330 return;
331 }
332
333 RefPtr<JSONArray> protocolErrors = JSONArray::create();
334 ((*this).*it->value)(sessionId, callId, messageObject.get(), protocolErrors. get());
335 }
336
337 void DispatcherImpl::sendResponse(int sessionId, int callId, const ErrorString& invocationError, PassRefPtr<JSONValue> errorData, PassRefPtr<JSONObject> result)
338 {
339 if (invocationError.length()) {
340 reportProtocolError(sessionId, callId, ServerError, invocationError, err orData);
341 return;
342 }
343
344 RefPtr<JSONObject> responseMessage = JSONObject::create();
345 responseMessage->setNumber("id", callId);
346 responseMessage->setObject("result", result);
347 if (m_frontendChannel)
348 m_frontendChannel->sendProtocolResponse(sessionId, callId, responseMessa ge.release());
349 }
350
351 void Dispatcher::reportProtocolError(int sessionId, int callId, CommonErrorCode code, const String& errorMessage) const
352 {
353 reportProtocolError(sessionId, callId, code, errorMessage, PassRefPtr<JSONVa lue>());
354 }
355
356 void DispatcherImpl::reportProtocolError(int sessionId, int callId, CommonErrorC ode code, const String& errorMessage, PassRefPtr<JSONValue> data) const
357 {
358 ASSERT(code >=0);
359 ASSERT((unsigned)code < m_commonErrors.size());
360 ASSERT(m_commonErrors[code]);
361 RefPtr<JSONObject> error = JSONObject::create();
362 error->setNumber("code", m_commonErrors[code]);
363 error->setString("message", errorMessage);
364 ASSERT(error);
365 if (data)
366 error->setValue("data", data);
367 RefPtr<JSONObject> message = JSONObject::create();
368 message->setObject("error", error);
369 message->setNumber("id", callId);
370 if (m_frontendChannel)
371 m_frontendChannel->sendProtocolResponse(sessionId, callId, message.relea se());
372 }
373
374 template<typename R, typename V, typename V0>
375 R DispatcherImpl::getPropertyValueImpl(JSONObject* object, const char* name, boo l* valueFound, JSONArray* protocolErrors, V0 initial_value, bool (*as_method)(JS ONValue*, V*), const char* type_name)
376 {
377 ASSERT(protocolErrors);
378
379 if (valueFound)
380 *valueFound = false;
381
382 V value = initial_value;
383
384 if (!object) {
385 if (!valueFound) {
386 // Required parameter in missing params container.
387 protocolErrors->pushString(String::format("'params' object must cont ain required parameter '%s' with type '%s'.", name, type_name));
388 }
389 return value;
390 }
391
392 JSONObject::const_iterator end = object->end();
393 JSONObject::const_iterator valueIterator = object->find(name);
394
395 if (valueIterator == end) {
396 if (!valueFound)
397 protocolErrors->pushString(String::format("Parameter '%s' with type '%s' was not found.", name, type_name));
398 return value;
399 }
400
401 if (!as_method(valueIterator->value.get(), &value))
402 protocolErrors->pushString(String::format("Parameter '%s' has wrong type . It must be '%s'.", name, type_name));
403 else
404 if (valueFound)
405 *valueFound = true;
406 return value;
407 }
408
409 struct AsMethodBridges {
410 static bool asInt(JSONValue* value, int* output) { return value->asNumber(ou tput); }
411 static bool asDouble(JSONValue* value, double* output) { return value->asNum ber(output); }
412 static bool asString(JSONValue* value, String* output) { return value->asStr ing(output); }
413 static bool asBoolean(JSONValue* value, bool* output) { return value->asBool ean(output); }
414 static bool asObject(JSONValue* value, RefPtr<JSONObject>* output) { return value->asObject(output); }
415 static bool asArray(JSONValue* value, RefPtr<JSONArray>* output) { return va lue->asArray(output); }
416 };
417
418 int DispatcherImpl::getInt(JSONObject* object, const char* name, bool* valueFoun d, JSONArray* protocolErrors)
419 {
420 return getPropertyValueImpl<int, int, int>(object, name, valueFound, protoco lErrors, 0, AsMethodBridges::asInt, "Number");
421 }
422
423 double DispatcherImpl::getDouble(JSONObject* object, const char* name, bool* val ueFound, JSONArray* protocolErrors)
424 {
425 return getPropertyValueImpl<double, double, double>(object, name, valueFound , protocolErrors, 0, AsMethodBridges::asDouble, "Number");
426 }
427
428 String DispatcherImpl::getString(JSONObject* object, const char* name, bool* val ueFound, JSONArray* protocolErrors)
429 {
430 return getPropertyValueImpl<String, String, String>(object, name, valueFound , protocolErrors, "", AsMethodBridges::asString, "String");
431 }
432
433 bool DispatcherImpl::getBoolean(JSONObject* object, const char* name, bool* valu eFound, JSONArray* protocolErrors)
434 {
435 return getPropertyValueImpl<bool, bool, bool>(object, name, valueFound, prot ocolErrors, false, AsMethodBridges::asBoolean, "Boolean");
436 }
437
438 PassRefPtr<JSONObject> DispatcherImpl::getObject(JSONObject* object, const char* name, bool* valueFound, JSONArray* protocolErrors)
439 {
440 return getPropertyValueImpl<PassRefPtr<JSONObject>, RefPtr<JSONObject>, JSON Object*>(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asObject, "Object");
441 }
442
443 PassRefPtr<JSONArray> DispatcherImpl::getArray(JSONObject* object, const char* n ame, bool* valueFound, JSONArray* protocolErrors)
444 {
445 return getPropertyValueImpl<PassRefPtr<JSONArray>, RefPtr<JSONArray>, JSONAr ray*>(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asArray, "Ar ray");
446 }
447
448 bool Dispatcher::getCommandName(const String& message, String* result)
449 {
450 RefPtr<JSONValue> value = parseJSON(message);
451 if (!value)
452 return false;
453
454 RefPtr<JSONObject> object = value->asObject();
455 if (!object)
456 return false;
457
458 if (!object->getString("method", result))
459 return false;
460
461 return true;
462 }
463
464 Dispatcher::CallbackBase::CallbackBase(PassRefPtr<DispatcherImpl> backendImpl, i nt sessionId, int id)
465 : m_backendImpl(backendImpl), m_sessionId(sessionId), m_id(id), m_alreadySen t(false) {}
466
467 Dispatcher::CallbackBase::~CallbackBase() {}
468
469 void Dispatcher::CallbackBase::sendFailure(const ErrorString& error)
470 {
471 ASSERT(error.length());
472 sendIfActive(nullptr, error, PassRefPtr<JSONValue>());
473 }
474
475 bool Dispatcher::CallbackBase::isActive()
476 {
477 return !m_alreadySent && m_backendImpl->isActive();
478 }
479
480 void Dispatcher::CallbackBase::sendIfActive(PassRefPtr<JSONObject> partialMessag e, const ErrorString& invocationError, PassRefPtr<JSONValue> errorData)
481 {
482 if (m_alreadySent)
483 return;
484 m_backendImpl->sendResponse(m_sessionId, m_id, invocationError, errorData, p artialMessage);
485 m_alreadySent = true;
486 }
487
488 } // namespace protocol
489 } // namespace blink
490
491 """)
492
493 frontend_cpp = (
494 """
495
496 #include "platform/inspector_protocol/Frontend.h"
497
498 #include "platform/JSONValues.h"
499 #include "platform/inspector_protocol/FrontendChannel.h"
500 #include "wtf/text/CString.h"
501 #include "wtf/text/WTFString.h"
502
503 namespace blink {
504 namespace protocol {
505
506 Frontend::Frontend(FrontendChannel* frontendChannel)
507 : m_frontendChannel(frontendChannel)
508 , $constructorInit
509 {
510 }
511
512 $methods
513
514 } // namespace protocol
515 } // namespace blink
516
517 """)
518
519 typebuilder_h = (
520 """
521 #ifndef TypeBuilder_h
522 #define TypeBuilder_h
523
524 #include "platform/JSONValues.h"
525 #include "platform/PlatformExport.h"
526 #include "wtf/Assertions.h"
527 #include "wtf/PassRefPtr.h"
528
529 namespace blink {
530 namespace protocol {
531
532 namespace TypeBuilder {
533
534 template<typename T>
535 class OptOutput {
536 public:
537 OptOutput() : m_assigned(false) { }
538
539 void operator=(T value)
540 {
541 m_value = value;
542 m_assigned = true;
543 }
544
545 bool isAssigned() { return m_assigned; }
546
547 T getValue()
548 {
549 ASSERT(isAssigned());
550 return m_value;
551 }
552
553 private:
554 T m_value;
555 bool m_assigned;
556
557 WTF_MAKE_NONCOPYABLE(OptOutput);
558 };
559
560 class RuntimeCastHelper {
561 public:
562 #if $validatorIfdefName
563 template<JSONValue::Type TYPE>
564 static void assertType(JSONValue* value)
565 {
566 ASSERT(value->type() == TYPE);
567 }
568 static void assertAny(JSONValue*);
569 static void assertInt(JSONValue* value);
570 #endif
571 };
572
573
574 // This class provides "Traits" type for the input type T. It is programmed usin g C++ template specialization
575 // technique. By default it simply takes "ItemTraits" type from T, but it doesn' t work with the base types.
576 template<typename T>
577 struct ArrayItemHelper {
578 typedef typename T::ItemTraits Traits;
579 };
580
581 template<typename T>
582 class Array : public JSONArrayBase {
583 private:
584 Array() { }
585
586 JSONArray* openAccessors() {
587 static_assert(sizeof(JSONArray) == sizeof(Array<T>), "JSONArray should b e the same size as Array<T>");
588 return static_cast<JSONArray*>(static_cast<JSONArrayBase*>(this));
589 }
590
591 public:
592 void addItem(PassRefPtr<T> value)
593 {
594 ArrayItemHelper<T>::Traits::pushRefPtr(this->openAccessors(), value);
595 }
596
597 void addItem(T value)
598 {
599 ArrayItemHelper<T>::Traits::pushRaw(this->openAccessors(), value);
600 }
601
602 static PassRefPtr<Array<T>> create()
603 {
604 return adoptRef(new Array<T>());
605 }
606
607 static PassRefPtr<Array<T>> runtimeCast(PassRefPtr<JSONValue> value)
608 {
609 RefPtr<JSONArray> array;
610 bool castRes = value->asArray(&array);
611 ASSERT_UNUSED(castRes, castRes);
612 #if $validatorIfdefName
613 assertCorrectValue(array.get());
614 #endif // $validatorIfdefName
615 static_assert(sizeof(Array<T>) == sizeof(JSONArray), "Array<T> should be the same size as JSONArray");
616 return static_cast<Array<T>*>(static_cast<JSONArrayBase*>(array.get()));
617 }
618
619 void concat(PassRefPtr<Array<T>> array)
620 {
621 return ArrayItemHelper<T>::Traits::concat(this->openAccessors(), array-> openAccessors());
622 }
623
624 #if $validatorIfdefName
625 static void assertCorrectValue(JSONValue* value)
626 {
627 RefPtr<JSONArray> array;
628 bool castRes = value->asArray(&array);
629 ASSERT_UNUSED(castRes, castRes);
630 for (unsigned i = 0; i < array->length(); i++)
631 ArrayItemHelper<T>::Traits::template assertCorrectValue<T>(array->ge t(i).get());
632 }
633
634 #endif // $validatorIfdefName
635 };
636
637 struct StructItemTraits {
638 static void pushRefPtr(JSONArray* array, PassRefPtr<JSONValue> value)
639 {
640 array->pushValue(value);
641 }
642
643 static void concat(JSONArray* array, JSONArray* anotherArray)
644 {
645 for (JSONArray::iterator it = anotherArray->begin(); it != anotherArray- >end(); ++it)
646 array->pushValue(*it);
647 }
648
649 #if $validatorIfdefName
650 template<typename T>
651 static void assertCorrectValue(JSONValue* value) {
652 T::assertCorrectValue(value);
653 }
654 #endif // $validatorIfdefName
655 };
656
657 template<>
658 struct ArrayItemHelper<String> {
659 struct Traits {
660 static void pushRaw(JSONArray* array, const String& value)
661 {
662 array->pushString(value);
663 }
664
665 #if $validatorIfdefName
666 template<typename T>
667 static void assertCorrectValue(JSONValue* value) {
668 RuntimeCastHelper::assertType<JSONValue::TypeString>(value);
669 }
670 #endif // $validatorIfdefName
671 };
672 };
673
674 template<>
675 struct ArrayItemHelper<int> {
676 struct Traits {
677 static void pushRaw(JSONArray* array, int value)
678 {
679 array->pushInt(value);
680 }
681
682 #if $validatorIfdefName
683 template<typename T>
684 static void assertCorrectValue(JSONValue* value) {
685 RuntimeCastHelper::assertInt(value);
686 }
687 #endif // $validatorIfdefName
688 };
689 };
690
691 template<>
692 struct ArrayItemHelper<double> {
693 struct Traits {
694 static void pushRaw(JSONArray* array, double value)
695 {
696 array->pushNumber(value);
697 }
698
699 #if $validatorIfdefName
700 template<typename T>
701 static void assertCorrectValue(JSONValue* value) {
702 RuntimeCastHelper::assertType<JSONValue::TypeNumber>(value);
703 }
704 #endif // $validatorIfdefName
705 };
706 };
707
708 template<>
709 struct ArrayItemHelper<bool> {
710 struct Traits {
711 static void pushRaw(JSONArray* array, bool value)
712 {
713 array->pushBoolean(value);
714 }
715
716 #if $validatorIfdefName
717 template<typename T>
718 static void assertCorrectValue(JSONValue* value) {
719 RuntimeCastHelper::assertType<JSONValue::TypeBoolean>(value);
720 }
721 #endif // $validatorIfdefName
722 };
723 };
724
725 template<>
726 struct ArrayItemHelper<JSONValue> {
727 struct Traits {
728 static void pushRefPtr(JSONArray* array, PassRefPtr<JSONValue> value)
729 {
730 array->pushValue(value);
731 }
732
733 #if $validatorIfdefName
734 template<typename T>
735 static void assertCorrectValue(JSONValue* value) {
736 RuntimeCastHelper::assertAny(value);
737 }
738 #endif // $validatorIfdefName
739 };
740 };
741
742 template<>
743 struct ArrayItemHelper<JSONObject> {
744 struct Traits {
745 static void pushRefPtr(JSONArray* array, PassRefPtr<JSONValue> value)
746 {
747 array->pushValue(value);
748 }
749
750 #if $validatorIfdefName
751 template<typename T>
752 static void assertCorrectValue(JSONValue* value) {
753 RuntimeCastHelper::assertType<JSONValue::TypeObject>(value);
754 }
755 #endif // $validatorIfdefName
756 };
757 };
758
759 template<>
760 struct ArrayItemHelper<JSONArray> {
761 struct Traits {
762 static void pushRefPtr(JSONArray* array, PassRefPtr<JSONArray> value)
763 {
764 array->pushArray(value);
765 }
766
767 #if $validatorIfdefName
768 template<typename T>
769 static void assertCorrectValue(JSONValue* value) {
770 RuntimeCastHelper::assertType<JSONValue::TypeArray>(value);
771 }
772 #endif // $validatorIfdefName
773 };
774 };
775
776 template<typename T>
777 struct ArrayItemHelper<protocol::TypeBuilder::Array<T>> {
778 struct Traits {
779 static void pushRefPtr(JSONArray* array, PassRefPtr<protocol::TypeBuilde r::Array<T>> value)
780 {
781 array->pushValue(value);
782 }
783
784 #if $validatorIfdefName
785 template<typename S>
786 static void assertCorrectValue(JSONValue* value) {
787 S::assertCorrectValue(value);
788 }
789 #endif // $validatorIfdefName
790 };
791 };
792
793 ${forwards}
794
795 PLATFORM_EXPORT String getEnumConstantValue(int code);
796
797 ${typeBuilders}
798 } // namespace TypeBuilder
799
800 } // namespace protocol
801 } // namespace blink
802
803 #endif // !defined(TypeBuilder_h)
804
805 """)
806
807 typebuilder_cpp = (
808 """
809
810 #include "platform/inspector_protocol/TypeBuilder.h"
811 #include "wtf/text/CString.h"
812
813 namespace blink {
814 namespace protocol {
815
816 namespace TypeBuilder {
817
818 const char* const enum_constant_values[] = {
819 $enumConstantValues};
820
821 String getEnumConstantValue(int code) {
822 return enum_constant_values[code];
823 }
824
825 } // namespace TypeBuilder
826
827 $implCode
828
829 #if $validatorIfdefName
830
831 void TypeBuilder::RuntimeCastHelper::assertAny(JSONValue*)
832 {
833 // No-op.
834 }
835
836
837 void TypeBuilder::RuntimeCastHelper::assertInt(JSONValue* value)
838 {
839 double v;
840 bool castRes = value->asNumber(&v);
841 ASSERT_UNUSED(castRes, castRes);
842 ASSERT(static_cast<double>(static_cast<int>(v)) == v);
843 }
844
845 $validatorCode
846
847 #endif // $validatorIfdefName
848
849 } // namespace protocol
850 } // namespace blink
851
852 """)
853
854 param_container_access_code = """
855 RefPtr<JSONObject> paramsContainer = requestMessageObject->getObject("params ");
856 JSONObject* paramsContainerPtr = paramsContainer.get();
857 """
858
859 class_binding_builder_part_1 = (
860 """ AllFieldsSet = %s
861 };
862
863 template<int STATE>
864 class Builder {
865 private:
866 RefPtr<JSONObject> m_result;
867
868 template<int STEP> Builder<STATE | STEP>& castState()
869 {
870 return *reinterpret_cast<Builder<STATE | STEP>*>(this);
871 }
872
873 Builder(PassRefPtr</*%s*/JSONObject> ptr)
874 {
875 static_assert(STATE == NoFieldsSet, "builder should not be created i n non-init state");
876 m_result = ptr;
877 }
878 friend class %s;
879 public:
880 """)
881
882 class_binding_builder_part_2 = ("""
883 Builder<STATE | %s>& set%s(%s value)
884 {
885 static_assert(!(STATE & %s), "property %s should not be set yet");
886 m_result->set%s("%s", %s);
887 return castState<%s>();
888 }
889 """)
890
891 class_binding_builder_part_3 = ("""
892 operator RefPtr<%s>& ()
893 {
894 static_assert(STATE == AllFieldsSet, "state should be AllFieldsSet") ;
895 static_assert(sizeof(%s) == sizeof(JSONObject), "%s should be the sa me size as JSONObject");
896 return *reinterpret_cast<RefPtr<%s>*>(&m_result);
897 }
898
899 PassRefPtr<%s> release()
900 {
901 return RefPtr<%s>(*this).release();
902 }
903 };
904
905 """)
906
907 class_binding_builder_part_4 = (
908 """ static Builder<NoFieldsSet> create()
909 {
910 return Builder<NoFieldsSet>(JSONObject::create());
911 }
912 """)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698