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

Side by Side Diff: third_party/WebCore/inspector/CodeGeneratorInspectorStrings.py

Issue 12319151: WebKit IDL roll. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 | Annotate | Revision Log
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 CodeGeneratorInspector.
30 # Its syntax is a Python syntax subset, suitable for manual parsing.
31
32 frontend_domain_class = (
33 """ class $domainClassName {
34 public:
35 $domainClassName(InspectorFrontendChannel* inspectorFrontendChannel) : m _inspectorFrontendChannel(inspectorFrontendChannel) { }
36 ${frontendDomainMethodDeclarations} void setInspectorFrontendChannel(Insp ectorFrontendChannel* inspectorFrontendChannel) { m_inspectorFrontendChannel = i nspectorFrontendChannel; }
37 InspectorFrontendChannel* getInspectorFrontendChannel() { return m_inspe ctorFrontendChannel; }
38 private:
39 InspectorFrontendChannel* m_inspectorFrontendChannel;
40 };
41
42 $domainClassName* $domainFieldName() { return &m_$domainFieldName; }
43
44 """)
45
46 backend_method = (
47 """void InspectorBackendDispatcherImpl::${domainName}_$methodName(long callId, I nspectorObject*$requestMessageObject)
48 {
49 RefPtr<InspectorArray> protocolErrors = InspectorArray::create();
50
51 if (!$agentField)
52 protocolErrors->pushString("${domainName} handler is not available.");
53 $methodOutCode
54 $methodInCode
55 RefPtr<InspectorObject> result = InspectorObject::create();
56 ErrorString error;
57 if (!protocolErrors->length()) {
58 $agentField->$methodName(&error$agentCallParams);
59
60 ${responseCook}
61 }
62 sendResponse(callId, result, commandNames[$commandNameIndex], protocolErrors , error);
63 }
64 """)
65
66 frontend_method = ("""void InspectorFrontend::$domainName::$eventName($parameter s)
67 {
68 RefPtr<InspectorObject> jsonMessage = InspectorObject::create();
69 jsonMessage->setString("method", "$domainName.$eventName");
70 $code if (m_inspectorFrontendChannel)
71 m_inspectorFrontendChannel->sendMessageToFrontend(jsonMessage->toJSONStr ing());
72 }
73 """)
74
75 callback_method = (
76 """InspectorBackendDispatcher::$agentName::$callbackName::$callbackName(PassRefP tr<InspectorBackendDispatcherImpl> backendImpl, int id) : CallbackBase(backendIm pl, id) {}
77
78 void InspectorBackendDispatcher::$agentName::$callbackName::sendSuccess($paramet ers)
79 {
80 RefPtr<InspectorObject> jsonMessage = InspectorObject::create();
81 $code sendIfActive(jsonMessage, ErrorString());
82 }
83 """)
84
85 frontend_h = (
86 """#ifndef InspectorFrontend_h
87 #define InspectorFrontend_h
88
89 #include "InspectorTypeBuilder.h"
90 #include "InspectorValues.h"
91 #include <wtf/PassRefPtr.h>
92 #include <wtf/text/WTFString.h>
93
94 namespace WebCore {
95
96 class InspectorFrontendChannel;
97
98 // Both InspectorObject and InspectorArray may or may not be declared at this po int as defined by ENABLED_INSPECTOR.
99 // Double-check we have them at least as forward declaration.
100 class InspectorArray;
101 class InspectorObject;
102
103 typedef String ErrorString;
104
105 #if ENABLE(INSPECTOR)
106
107 class InspectorFrontend {
108 public:
109 InspectorFrontend(InspectorFrontendChannel*);
110
111
112 $domainClassList
113 private:
114 ${fieldDeclarations}};
115
116 #endif // ENABLE(INSPECTOR)
117
118 } // namespace WebCore
119 #endif // !defined(InspectorFrontend_h)
120 """)
121
122 backend_h = (
123 """#ifndef InspectorBackendDispatcher_h
124 #define InspectorBackendDispatcher_h
125
126 #include <wtf/PassRefPtr.h>
127 #include <wtf/RefCounted.h>
128 #include <wtf/text/WTFString.h>
129 #include "InspectorTypeBuilder.h"
130
131 namespace WebCore {
132
133 class InspectorAgent;
134 class InspectorObject;
135 class InspectorArray;
136 class InspectorFrontendChannel;
137
138 typedef String ErrorString;
139
140 class InspectorBackendDispatcherImpl;
141
142 class InspectorBackendDispatcher: public RefCounted<InspectorBackendDispatcher> {
143 public:
144 static PassRefPtr<InspectorBackendDispatcher> create(InspectorFrontendChanne l* inspectorFrontendChannel);
145 virtual ~InspectorBackendDispatcher() { }
146
147 class CallbackBase: public RefCounted<CallbackBase> {
148 public:
149 CallbackBase(PassRefPtr<InspectorBackendDispatcherImpl> backendImpl, int id);
150 virtual ~CallbackBase();
151 void sendFailure(const ErrorString&);
152 bool isActive();
153
154 protected:
155 void sendIfActive(PassRefPtr<InspectorObject> partialMessage, const Erro rString& invocationError);
156
157 private:
158 void disable() { m_alreadySent = true; }
159
160 RefPtr<InspectorBackendDispatcherImpl> m_backendImpl;
161 int m_id;
162 bool m_alreadySent;
163
164 friend class InspectorBackendDispatcherImpl;
165 };
166
167 $agentInterfaces
168 $virtualSetters
169
170 virtual void clearFrontend() = 0;
171
172 enum CommonErrorCode {
173 ParseError = 0,
174 InvalidRequest,
175 MethodNotFound,
176 InvalidParams,
177 InternalError,
178 ServerError,
179 LastEntry,
180 };
181
182 void reportProtocolError(const long* const callId, CommonErrorCode, const St ring& errorMessage) const;
183 virtual void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage, PassRefPtr<InspectorArray> data) const = 0;
184 virtual void dispatch(const String& message) = 0;
185 static bool getCommandName(const String& message, String* result);
186
187 enum MethodNames {
188 $methodNamesEnumContent
189
190 kMethodNamesEnumSize
191 };
192
193 static const char* commandNames[];
194 };
195
196 } // namespace WebCore
197 #endif // !defined(InspectorBackendDispatcher_h)
198
199
200 """)
201
202 backend_cpp = (
203 """
204
205 #include "config.h"
206
207 #if ENABLE(INSPECTOR)
208
209 #include "InspectorBackendDispatcher.h"
210 #include <wtf/text/WTFString.h>
211 #include <wtf/text/CString.h>
212
213 #include "InspectorAgent.h"
214 #include "InspectorValues.h"
215 #include "InspectorFrontendChannel.h"
216 #include <wtf/text/WTFString.h>
217
218 namespace WebCore {
219
220 const char* InspectorBackendDispatcher::commandNames[] = {
221 $methodNameDeclarations
222 };
223
224
225 class InspectorBackendDispatcherImpl : public InspectorBackendDispatcher {
226 public:
227 InspectorBackendDispatcherImpl(InspectorFrontendChannel* inspectorFrontendCh annel)
228 : m_inspectorFrontendChannel(inspectorFrontendChannel)
229 $constructorInit
230 { }
231
232 virtual void clearFrontend() { m_inspectorFrontendChannel = 0; }
233 virtual void dispatch(const String& message);
234 virtual void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage, PassRefPtr<InspectorArray> data) const;
235 using InspectorBackendDispatcher::reportProtocolError;
236
237 void sendResponse(long callId, PassRefPtr<InspectorObject> result, const Err orString& invocationError);
238 bool isActive() { return m_inspectorFrontendChannel; }
239
240 $setters
241 private:
242 $methodDeclarations
243
244 InspectorFrontendChannel* m_inspectorFrontendChannel;
245 $fieldDeclarations
246
247 template<typename R, typename V, typename V0>
248 static R getPropertyValueImpl(InspectorObject* object, const String& name, b ool* valueFound, InspectorArray* protocolErrors, V0 initial_value, bool (*as_met hod)(InspectorValue*, V*), const char* type_name);
249
250 static int getInt(InspectorObject* object, const String& name, bool* valueFo und, InspectorArray* protocolErrors);
251 static double getDouble(InspectorObject* object, const String& name, bool* v alueFound, InspectorArray* protocolErrors);
252 static String getString(InspectorObject* object, const String& name, bool* v alueFound, InspectorArray* protocolErrors);
253 static bool getBoolean(InspectorObject* object, const String& name, bool* va lueFound, InspectorArray* protocolErrors);
254 static PassRefPtr<InspectorObject> getObject(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors);
255 static PassRefPtr<InspectorArray> getArray(InspectorObject* object, const St ring& name, bool* valueFound, InspectorArray* protocolErrors);
256
257 void sendResponse(long callId, PassRefPtr<InspectorObject> result, const cha r* commandName, PassRefPtr<InspectorArray> protocolErrors, ErrorString invocatio nError);
258
259 };
260
261 $methods
262
263 PassRefPtr<InspectorBackendDispatcher> InspectorBackendDispatcher::create(Inspec torFrontendChannel* inspectorFrontendChannel)
264 {
265 return adoptRef(new InspectorBackendDispatcherImpl(inspectorFrontendChannel) );
266 }
267
268
269 void InspectorBackendDispatcherImpl::dispatch(const String& message)
270 {
271 RefPtr<InspectorBackendDispatcher> protect = this;
272 typedef void (InspectorBackendDispatcherImpl::*CallHandler)(long callId, Ins pectorObject* messageObject);
273 typedef HashMap<String, CallHandler> DispatchMap;
274 DEFINE_STATIC_LOCAL(DispatchMap, dispatchMap, );
275 long callId = 0;
276
277 if (dispatchMap.isEmpty()) {
278 static CallHandler handlers[] = {
279 $messageHandlers
280 };
281 size_t length = WTF_ARRAY_LENGTH(commandNames);
282 for (size_t i = 0; i < length; ++i)
283 dispatchMap.add(commandNames[i], handlers[i]);
284 }
285
286 RefPtr<InspectorValue> parsedMessage = InspectorValue::parseJSON(message);
287 if (!parsedMessage) {
288 reportProtocolError(0, ParseError, "Message must be in JSON format");
289 return;
290 }
291
292 RefPtr<InspectorObject> messageObject = parsedMessage->asObject();
293 if (!messageObject) {
294 reportProtocolError(0, InvalidRequest, "Message must be a JSONified obje ct");
295 return;
296 }
297
298 RefPtr<InspectorValue> callIdValue = messageObject->get("id");
299 if (!callIdValue) {
300 reportProtocolError(0, InvalidRequest, "'id' property was not found");
301 return;
302 }
303
304 if (!callIdValue->asNumber(&callId)) {
305 reportProtocolError(0, InvalidRequest, "The type of 'id' property must b e number");
306 return;
307 }
308
309 RefPtr<InspectorValue> methodValue = messageObject->get("method");
310 if (!methodValue) {
311 reportProtocolError(&callId, InvalidRequest, "'method' property wasn't f ound");
312 return;
313 }
314
315 String method;
316 if (!methodValue->asString(&method)) {
317 reportProtocolError(&callId, InvalidRequest, "The type of 'method' prope rty must be string");
318 return;
319 }
320
321 HashMap<String, CallHandler>::iterator it = dispatchMap.find(method);
322 if (it == dispatchMap.end()) {
323 reportProtocolError(&callId, MethodNotFound, "'" + method + "' wasn't fo und");
324 return;
325 }
326
327 ((*this).*it->value)(callId, messageObject.get());
328 }
329
330 void InspectorBackendDispatcherImpl::sendResponse(long callId, PassRefPtr<Inspec torObject> result, const char* commandName, PassRefPtr<InspectorArray> protocolE rrors, ErrorString invocationError)
331 {
332 if (protocolErrors->length()) {
333 String errorMessage = String::format("Some arguments of method '%s' can' t be processed", commandName);
334 reportProtocolError(&callId, InvalidParams, errorMessage, protocolErrors );
335 return;
336 }
337 sendResponse(callId, result, invocationError);
338 }
339
340 void InspectorBackendDispatcherImpl::sendResponse(long callId, PassRefPtr<Inspec torObject> result, const ErrorString& invocationError)
341 {
342 if (invocationError.length()) {
343 reportProtocolError(&callId, ServerError, invocationError);
344 return;
345 }
346
347 RefPtr<InspectorObject> responseMessage = InspectorObject::create();
348 responseMessage->setObject("result", result);
349 responseMessage->setNumber("id", callId);
350 if (m_inspectorFrontendChannel)
351 m_inspectorFrontendChannel->sendMessageToFrontend(responseMessage->toJSO NString());
352 }
353
354 void InspectorBackendDispatcher::reportProtocolError(const long* const callId, C ommonErrorCode code, const String& errorMessage) const
355 {
356 reportProtocolError(callId, code, errorMessage, 0);
357 }
358
359 void InspectorBackendDispatcherImpl::reportProtocolError(const long* const callI d, CommonErrorCode code, const String& errorMessage, PassRefPtr<InspectorArray> data) const
360 {
361 DEFINE_STATIC_LOCAL(Vector<int>,s_commonErrors,);
362 if (!s_commonErrors.size()) {
363 s_commonErrors.insert(ParseError, -32700);
364 s_commonErrors.insert(InvalidRequest, -32600);
365 s_commonErrors.insert(MethodNotFound, -32601);
366 s_commonErrors.insert(InvalidParams, -32602);
367 s_commonErrors.insert(InternalError, -32603);
368 s_commonErrors.insert(ServerError, -32000);
369 }
370 ASSERT(code >=0);
371 ASSERT((unsigned)code < s_commonErrors.size());
372 ASSERT(s_commonErrors[code]);
373 RefPtr<InspectorObject> error = InspectorObject::create();
374 error->setNumber("code", s_commonErrors[code]);
375 error->setString("message", errorMessage);
376 ASSERT(error);
377 if (data)
378 error->setArray("data", data);
379 RefPtr<InspectorObject> message = InspectorObject::create();
380 message->setObject("error", error);
381 if (callId)
382 message->setNumber("id", *callId);
383 else
384 message->setValue("id", InspectorValue::null());
385 if (m_inspectorFrontendChannel)
386 m_inspectorFrontendChannel->sendMessageToFrontend(message->toJSONString( ));
387 }
388
389 template<typename R, typename V, typename V0>
390 R InspectorBackendDispatcherImpl::getPropertyValueImpl(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors, V0 initial _value, bool (*as_method)(InspectorValue*, V*), const char* type_name)
391 {
392 ASSERT(protocolErrors);
393
394 if (valueFound)
395 *valueFound = false;
396
397 V value = initial_value;
398
399 if (!object) {
400 if (!valueFound) {
401 // Required parameter in missing params container.
402 protocolErrors->pushString(String::format("'params' object must cont ain required parameter '%s' with type '%s'.", name.utf8().data(), type_name));
403 }
404 return value;
405 }
406
407 InspectorObject::const_iterator end = object->end();
408 InspectorObject::const_iterator valueIterator = object->find(name);
409
410 if (valueIterator == end) {
411 if (!valueFound)
412 protocolErrors->pushString(String::format("Parameter '%s' with type '%s' was not found.", name.utf8().data(), type_name));
413 return value;
414 }
415
416 if (!as_method(valueIterator->value.get(), &value))
417 protocolErrors->pushString(String::format("Parameter '%s' has wrong type . It must be '%s'.", name.utf8().data(), type_name));
418 else
419 if (valueFound)
420 *valueFound = true;
421 return value;
422 }
423
424 struct AsMethodBridges {
425 static bool asInt(InspectorValue* value, int* output) { return value->asNumb er(output); }
426 static bool asDouble(InspectorValue* value, double* output) { return value-> asNumber(output); }
427 static bool asString(InspectorValue* value, String* output) { return value-> asString(output); }
428 static bool asBoolean(InspectorValue* value, bool* output) { return value->a sBoolean(output); }
429 static bool asObject(InspectorValue* value, RefPtr<InspectorObject>* output) { return value->asObject(output); }
430 static bool asArray(InspectorValue* value, RefPtr<InspectorArray>* output) { return value->asArray(output); }
431 };
432
433 int InspectorBackendDispatcherImpl::getInt(InspectorObject* object, const String & name, bool* valueFound, InspectorArray* protocolErrors)
434 {
435 return getPropertyValueImpl<int, int, int>(object, name, valueFound, protoco lErrors, 0, AsMethodBridges::asInt, "Number");
436 }
437
438 double InspectorBackendDispatcherImpl::getDouble(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors)
439 {
440 return getPropertyValueImpl<double, double, double>(object, name, valueFound , protocolErrors, 0, AsMethodBridges::asDouble, "Number");
441 }
442
443 String InspectorBackendDispatcherImpl::getString(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors)
444 {
445 return getPropertyValueImpl<String, String, String>(object, name, valueFound , protocolErrors, "", AsMethodBridges::asString, "String");
446 }
447
448 bool InspectorBackendDispatcherImpl::getBoolean(InspectorObject* object, const S tring& name, bool* valueFound, InspectorArray* protocolErrors)
449 {
450 return getPropertyValueImpl<bool, bool, bool>(object, name, valueFound, prot ocolErrors, false, AsMethodBridges::asBoolean, "Boolean");
451 }
452
453 PassRefPtr<InspectorObject> InspectorBackendDispatcherImpl::getObject(InspectorO bject* object, const String& name, bool* valueFound, InspectorArray* protocolErr ors)
454 {
455 return getPropertyValueImpl<PassRefPtr<InspectorObject>, RefPtr<InspectorObj ect>, InspectorObject*>(object, name, valueFound, protocolErrors, 0, AsMethodBri dges::asObject, "Object");
456 }
457
458 PassRefPtr<InspectorArray> InspectorBackendDispatcherImpl::getArray(InspectorObj ect* object, const String& name, bool* valueFound, InspectorArray* protocolError s)
459 {
460 return getPropertyValueImpl<PassRefPtr<InspectorArray>, RefPtr<InspectorArra y>, InspectorArray*>(object, name, valueFound, protocolErrors, 0, AsMethodBridge s::asArray, "Array");
461 }
462
463 bool InspectorBackendDispatcher::getCommandName(const String& message, String* r esult)
464 {
465 RefPtr<InspectorValue> value = InspectorValue::parseJSON(message);
466 if (!value)
467 return false;
468
469 RefPtr<InspectorObject> object = value->asObject();
470 if (!object)
471 return false;
472
473 if (!object->getString("method", result))
474 return false;
475
476 return true;
477 }
478
479 InspectorBackendDispatcher::CallbackBase::CallbackBase(PassRefPtr<InspectorBacke ndDispatcherImpl> backendImpl, int id)
480 : m_backendImpl(backendImpl), m_id(id), m_alreadySent(false) {}
481
482 InspectorBackendDispatcher::CallbackBase::~CallbackBase() {}
483
484 void InspectorBackendDispatcher::CallbackBase::sendFailure(const ErrorString& er ror)
485 {
486 ASSERT(error.length());
487 sendIfActive(0, error);
488 }
489
490 bool InspectorBackendDispatcher::CallbackBase::isActive()
491 {
492 return !m_alreadySent && m_backendImpl->isActive();
493 }
494
495 void InspectorBackendDispatcher::CallbackBase::sendIfActive(PassRefPtr<Inspector Object> partialMessage, const ErrorString& invocationError)
496 {
497 if (m_alreadySent)
498 return;
499 m_backendImpl->sendResponse(m_id, partialMessage, invocationError);
500 m_alreadySent = true;
501 }
502
503 COMPILE_ASSERT(static_cast<int>(InspectorBackendDispatcher::kMethodNamesEnumSize ) == WTF_ARRAY_LENGTH(InspectorBackendDispatcher::commandNames), command_name_ar ray_problem);
504
505 } // namespace WebCore
506
507 #endif // ENABLE(INSPECTOR)
508 """)
509
510 frontend_cpp = (
511 """
512
513 #include "config.h"
514 #if ENABLE(INSPECTOR)
515
516 #include "InspectorFrontend.h"
517 #include <wtf/text/WTFString.h>
518 #include <wtf/text/CString.h>
519
520 #include "InspectorFrontendChannel.h"
521 #include "InspectorValues.h"
522 #include <wtf/text/WTFString.h>
523
524 namespace WebCore {
525
526 InspectorFrontend::InspectorFrontend(InspectorFrontendChannel* inspectorFrontend Channel)
527 : $constructorInit{
528 }
529
530 $methods
531
532 } // namespace WebCore
533
534 #endif // ENABLE(INSPECTOR)
535 """)
536
537 typebuilder_h = (
538 """
539 #ifndef InspectorTypeBuilder_h
540 #define InspectorTypeBuilder_h
541
542 #if ENABLE(INSPECTOR)
543
544 #include "InspectorValues.h"
545 #include <wtf/Assertions.h>
546 #include <wtf/PassRefPtr.h>
547
548 namespace WebCore {
549
550 namespace TypeBuilder {
551
552 template<typename T>
553 class OptOutput {
554 public:
555 OptOutput() : m_assigned(false) { }
556
557 void operator=(T value)
558 {
559 m_value = value;
560 m_assigned = true;
561 }
562
563 bool isAssigned() { return m_assigned; }
564
565 T getValue()
566 {
567 ASSERT(isAssigned());
568 return m_value;
569 }
570
571 private:
572 T m_value;
573 bool m_assigned;
574
575 WTF_MAKE_NONCOPYABLE(OptOutput);
576 };
577
578
579 // A small transient wrapper around int type, that can be used as a funciton par ameter type
580 // cleverly disallowing C++ implicit casts from float or double.
581 class ExactlyInt {
582 public:
583 template<typename T>
584 ExactlyInt(T t) : m_value(cast_to_int<T>(t)) {}
585
586 ExactlyInt() {}
587
588 operator int() { return m_value; }
589 private:
590 int m_value;
591
592 template<typename T>
593 static int cast_to_int(T) { return T::default_case_cast_is_not_supported(); }
594 };
595
596 template<>
597 inline int ExactlyInt::cast_to_int<int>(int i) { return i; }
598
599 template<>
600 inline int ExactlyInt::cast_to_int<unsigned int>(unsigned int i) { return i; }
601
602 class RuntimeCastHelper {
603 public:
604 #if $validatorIfdefName
605 template<InspectorValue::Type TYPE>
606 static void assertType(InspectorValue* value)
607 {
608 ASSERT(value->type() == TYPE);
609 }
610 static void assertAny(InspectorValue*);
611 static void assertInt(InspectorValue* value);
612 #endif
613 };
614
615
616 // This class provides "Traits" type for the input type T. It is programmed usin g C++ template specialization
617 // technique. By default it simply takes "ItemTraits" type from T, but it doesn' t work with the base types.
618 template<typename T>
619 struct ArrayItemHelper {
620 typedef typename T::ItemTraits Traits;
621 };
622
623 template<typename T>
624 class Array : public InspectorArrayBase {
625 private:
626 Array() { }
627
628 InspectorArray* openAccessors() {
629 COMPILE_ASSERT(sizeof(InspectorArray) == sizeof(Array<T>), cannot_cast);
630 return static_cast<InspectorArray*>(static_cast<InspectorArrayBase*>(thi s));
631 }
632
633 public:
634 void addItem(PassRefPtr<T> value)
635 {
636 ArrayItemHelper<T>::Traits::pushRefPtr(this->openAccessors(), value);
637 }
638
639 void addItem(T value)
640 {
641 ArrayItemHelper<T>::Traits::pushRaw(this->openAccessors(), value);
642 }
643
644 static PassRefPtr<Array<T> > create()
645 {
646 return adoptRef(new Array<T>());
647 }
648
649 static PassRefPtr<Array<T> > runtimeCast(PassRefPtr<InspectorValue> value)
650 {
651 RefPtr<InspectorArray> array;
652 bool castRes = value->asArray(&array);
653 ASSERT_UNUSED(castRes, castRes);
654 #if $validatorIfdefName
655 assertCorrectValue(array.get());
656 #endif // $validatorIfdefName
657 COMPILE_ASSERT(sizeof(Array<T>) == sizeof(InspectorArray), type_cast_pro blem);
658 return static_cast<Array<T>*>(static_cast<InspectorArrayBase*>(array.get ()));
659 }
660
661 #if $validatorIfdefName
662 static void assertCorrectValue(InspectorValue* value)
663 {
664 RefPtr<InspectorArray> array;
665 bool castRes = value->asArray(&array);
666 ASSERT_UNUSED(castRes, castRes);
667 for (unsigned i = 0; i < array->length(); i++)
668 ArrayItemHelper<T>::Traits::template assertCorrectValue<T>(array->ge t(i).get());
669 }
670
671 #endif // $validatorIfdefName
672 };
673
674 struct StructItemTraits {
675 static void pushRefPtr(InspectorArray* array, PassRefPtr<InspectorValue> val ue)
676 {
677 array->pushValue(value);
678 }
679
680 #if $validatorIfdefName
681 template<typename T>
682 static void assertCorrectValue(InspectorValue* value) {
683 T::assertCorrectValue(value);
684 }
685 #endif // $validatorIfdefName
686 };
687
688 template<>
689 struct ArrayItemHelper<String> {
690 struct Traits {
691 static void pushRaw(InspectorArray* array, const String& value)
692 {
693 array->pushString(value);
694 }
695
696 #if $validatorIfdefName
697 template<typename T>
698 static void assertCorrectValue(InspectorValue* value) {
699 RuntimeCastHelper::assertType<InspectorValue::TypeString>(value);
700 }
701 #endif // $validatorIfdefName
702 };
703 };
704
705 template<>
706 struct ArrayItemHelper<int> {
707 struct Traits {
708 static void pushRaw(InspectorArray* array, int value)
709 {
710 array->pushInt(value);
711 }
712
713 #if $validatorIfdefName
714 template<typename T>
715 static void assertCorrectValue(InspectorValue* value) {
716 RuntimeCastHelper::assertInt(value);
717 }
718 #endif // $validatorIfdefName
719 };
720 };
721
722 template<>
723 struct ArrayItemHelper<double> {
724 struct Traits {
725 static void pushRaw(InspectorArray* array, double value)
726 {
727 array->pushNumber(value);
728 }
729
730 #if $validatorIfdefName
731 template<typename T>
732 static void assertCorrectValue(InspectorValue* value) {
733 RuntimeCastHelper::assertType<InspectorValue::TypeNumber>(value);
734 }
735 #endif // $validatorIfdefName
736 };
737 };
738
739 template<>
740 struct ArrayItemHelper<bool> {
741 struct Traits {
742 static void pushRaw(InspectorArray* array, bool value)
743 {
744 array->pushBoolean(value);
745 }
746
747 #if $validatorIfdefName
748 template<typename T>
749 static void assertCorrectValue(InspectorValue* value) {
750 RuntimeCastHelper::assertType<InspectorValue::TypeBoolean>(value);
751 }
752 #endif // $validatorIfdefName
753 };
754 };
755
756 template<>
757 struct ArrayItemHelper<InspectorValue> {
758 struct Traits {
759 static void pushRefPtr(InspectorArray* array, PassRefPtr<InspectorValue> value)
760 {
761 array->pushValue(value);
762 }
763
764 #if $validatorIfdefName
765 template<typename T>
766 static void assertCorrectValue(InspectorValue* value) {
767 RuntimeCastHelper::assertAny(value);
768 }
769 #endif // $validatorIfdefName
770 };
771 };
772
773 template<>
774 struct ArrayItemHelper<InspectorObject> {
775 struct Traits {
776 static void pushRefPtr(InspectorArray* array, PassRefPtr<InspectorValue> value)
777 {
778 array->pushValue(value);
779 }
780
781 #if $validatorIfdefName
782 template<typename T>
783 static void assertCorrectValue(InspectorValue* value) {
784 RuntimeCastHelper::assertType<InspectorValue::TypeObject>(value);
785 }
786 #endif // $validatorIfdefName
787 };
788 };
789
790 template<>
791 struct ArrayItemHelper<InspectorArray> {
792 struct Traits {
793 static void pushRefPtr(InspectorArray* array, PassRefPtr<InspectorArray> value)
794 {
795 array->pushArray(value);
796 }
797
798 #if $validatorIfdefName
799 template<typename T>
800 static void assertCorrectValue(InspectorValue* value) {
801 RuntimeCastHelper::assertType<InspectorValue::TypeArray>(value);
802 }
803 #endif // $validatorIfdefName
804 };
805 };
806
807 template<typename T>
808 struct ArrayItemHelper<TypeBuilder::Array<T> > {
809 struct Traits {
810 static void pushRefPtr(InspectorArray* array, PassRefPtr<TypeBuilder::Ar ray<T> > value)
811 {
812 array->pushValue(value);
813 }
814
815 #if $validatorIfdefName
816 template<typename S>
817 static void assertCorrectValue(InspectorValue* value) {
818 S::assertCorrectValue(value);
819 }
820 #endif // $validatorIfdefName
821 };
822 };
823
824 ${forwards}
825
826 String getEnumConstantValue(int code);
827
828 ${typeBuilders}
829 } // namespace TypeBuilder
830
831
832 } // namespace WebCore
833
834 #endif // ENABLE(INSPECTOR)
835
836 #endif // !defined(InspectorTypeBuilder_h)
837
838 """)
839
840 typebuilder_cpp = (
841 """
842
843 #include "config.h"
844 #if ENABLE(INSPECTOR)
845
846 #include "InspectorTypeBuilder.h"
847 #include <wtf/text/CString.h>
848
849 namespace WebCore {
850
851 namespace TypeBuilder {
852
853 const char* const enum_constant_values[] = {
854 $enumConstantValues};
855
856 String getEnumConstantValue(int code) {
857 return enum_constant_values[code];
858 }
859
860 } // namespace TypeBuilder
861
862 $implCode
863
864 #if $validatorIfdefName
865
866 void TypeBuilder::RuntimeCastHelper::assertAny(InspectorValue*)
867 {
868 // No-op.
869 }
870
871
872 void TypeBuilder::RuntimeCastHelper::assertInt(InspectorValue* value)
873 {
874 double v;
875 bool castRes = value->asNumber(&v);
876 ASSERT_UNUSED(castRes, castRes);
877 ASSERT(static_cast<double>(static_cast<int>(v)) == v);
878 }
879
880 $validatorCode
881
882 #endif // $validatorIfdefName
883
884 } // namespace WebCore
885
886 #endif // ENABLE(INSPECTOR)
887 """)
888
889 backend_js = (
890 """
891
892 $domainInitializers
893 """)
894
895 param_container_access_code = """
896 RefPtr<InspectorObject> paramsContainer = requestMessageObject->getObject("p arams");
897 InspectorObject* paramsContainerPtr = paramsContainer.get();
898 InspectorArray* protocolErrorsPtr = protocolErrors.get();
899 """
900
901 class_binding_builder_part_1 = (
902 """ AllFieldsSet = %s
903 };
904
905 template<int STATE>
906 class Builder {
907 private:
908 RefPtr<InspectorObject> m_result;
909
910 template<int STEP> Builder<STATE | STEP>& castState()
911 {
912 return *reinterpret_cast<Builder<STATE | STEP>*>(this);
913 }
914
915 Builder(PassRefPtr</*%s*/InspectorObject> ptr)
916 {
917 COMPILE_ASSERT(STATE == NoFieldsSet, builder_created_in_non_init_sta te);
918 m_result = ptr;
919 }
920 friend class %s;
921 public:
922 """)
923
924 class_binding_builder_part_2 = ("""
925 Builder<STATE | %s>& set%s(%s value)
926 {
927 COMPILE_ASSERT(!(STATE & %s), property_%s_already_set);
928 m_result->set%s("%s", %s);
929 return castState<%s>();
930 }
931 """)
932
933 class_binding_builder_part_3 = ("""
934 operator RefPtr<%s>& ()
935 {
936 COMPILE_ASSERT(STATE == AllFieldsSet, result_is_not_ready);
937 COMPILE_ASSERT(sizeof(%s) == sizeof(InspectorObject), cannot_cast);
938 return *reinterpret_cast<RefPtr<%s>*>(&m_result);
939 }
940
941 PassRefPtr<%s> release()
942 {
943 return RefPtr<%s>(*this).release();
944 }
945 };
946
947 """)
948
949 class_binding_builder_part_4 = (
950 """ static Builder<NoFieldsSet> create()
951 {
952 return Builder<NoFieldsSet>(InspectorObject::create());
953 }
954 """)
OLDNEW
« no previous file with comments | « third_party/WebCore/inspector/CodeGeneratorInspector.py ('k') | third_party/WebCore/inspector/InjectedScriptHost.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698