CodeGeneratorInspectorStrings.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. # THis file contains string resources for CodeGeneratorInspector.
  29. # Its syntax is a Python syntax subset, suitable for manual parsing.
  30. frontend_domain_class = (
  31. """ class $domainClassName {
  32. public:
  33. $domainClassName(InspectorFrontendChannel* inspectorFrontendChannel) : m_inspectorFrontendChannel(inspectorFrontendChannel) { }
  34. ${frontendDomainMethodDeclarations} void setInspectorFrontendChannel(InspectorFrontendChannel* inspectorFrontendChannel) { m_inspectorFrontendChannel = inspectorFrontendChannel; }
  35. InspectorFrontendChannel* getInspectorFrontendChannel() { return m_inspectorFrontendChannel; }
  36. private:
  37. InspectorFrontendChannel* m_inspectorFrontendChannel;
  38. };
  39. $domainClassName* $domainFieldName() { return &m_$domainFieldName; }
  40. """)
  41. backend_method = (
  42. """void InspectorBackendDispatcherImpl::${domainName}_$methodName(long callId, InspectorObject*$requestMessageObject)
  43. {
  44. RefPtr<InspectorArray> protocolErrors = InspectorArray::create();
  45. if (!$agentField)
  46. protocolErrors->pushString("${domainName} handler is not available.");
  47. $methodOutCode
  48. $methodInCode
  49. RefPtr<InspectorObject> result = InspectorObject::create();
  50. ErrorString error;
  51. if (!protocolErrors->length()) {
  52. $agentField->$methodName(&error$agentCallParams);
  53. ${responseCook}
  54. }
  55. sendResponse(callId, result, commandNames[$commandNameIndex], protocolErrors, error);
  56. }
  57. """)
  58. frontend_method = ("""void InspectorFrontend::$domainName::$eventName($parameters)
  59. {
  60. RefPtr<InspectorObject> jsonMessage = InspectorObject::create();
  61. jsonMessage->setString("method", "$domainName.$eventName");
  62. $code if (m_inspectorFrontendChannel)
  63. m_inspectorFrontendChannel->sendMessageToFrontend(jsonMessage->toJSONString());
  64. }
  65. """)
  66. callback_method = (
  67. """InspectorBackendDispatcher::$agentName::$callbackName::$callbackName(PassRefPtr<InspectorBackendDispatcherImpl> backendImpl, int id) : CallbackBase(backendImpl, id) {}
  68. void InspectorBackendDispatcher::$agentName::$callbackName::sendSuccess($parameters)
  69. {
  70. RefPtr<InspectorObject> jsonMessage = InspectorObject::create();
  71. $code sendIfActive(jsonMessage, ErrorString());
  72. }
  73. """)
  74. frontend_h = (
  75. """#ifndef InspectorFrontend_h
  76. #define InspectorFrontend_h
  77. #include "InspectorTypeBuilder.h"
  78. #include "InspectorValues.h"
  79. #include <wtf/PassRefPtr.h>
  80. #include <wtf/text/WTFString.h>
  81. namespace WebCore {
  82. class InspectorFrontendChannel;
  83. // Both InspectorObject and InspectorArray may or may not be declared at this point as defined by ENABLED_INSPECTOR.
  84. // Double-check we have them at least as forward declaration.
  85. class InspectorArray;
  86. class InspectorObject;
  87. typedef String ErrorString;
  88. #if ENABLE(INSPECTOR)
  89. class InspectorFrontend {
  90. public:
  91. InspectorFrontend(InspectorFrontendChannel*);
  92. $domainClassList
  93. private:
  94. ${fieldDeclarations}};
  95. #endif // ENABLE(INSPECTOR)
  96. } // namespace WebCore
  97. #endif // !defined(InspectorFrontend_h)
  98. """)
  99. backend_h = (
  100. """#ifndef InspectorBackendDispatcher_h
  101. #define InspectorBackendDispatcher_h
  102. #include <wtf/PassRefPtr.h>
  103. #include <wtf/RefCounted.h>
  104. #include <wtf/text/WTFString.h>
  105. #include "InspectorTypeBuilder.h"
  106. namespace WebCore {
  107. class InspectorAgent;
  108. class InspectorObject;
  109. class InspectorArray;
  110. class InspectorFrontendChannel;
  111. typedef String ErrorString;
  112. class InspectorBackendDispatcherImpl;
  113. class InspectorBackendDispatcher: public RefCounted<InspectorBackendDispatcher> {
  114. public:
  115. static PassRefPtr<InspectorBackendDispatcher> create(InspectorFrontendChannel* inspectorFrontendChannel);
  116. virtual ~InspectorBackendDispatcher() { }
  117. class CallbackBase: public RefCounted<CallbackBase> {
  118. public:
  119. CallbackBase(PassRefPtr<InspectorBackendDispatcherImpl> backendImpl, int id);
  120. virtual ~CallbackBase();
  121. void sendFailure(const ErrorString&);
  122. bool isActive();
  123. protected:
  124. void sendIfActive(PassRefPtr<InspectorObject> partialMessage, const ErrorString& invocationError);
  125. private:
  126. void disable() { m_alreadySent = true; }
  127. RefPtr<InspectorBackendDispatcherImpl> m_backendImpl;
  128. int m_id;
  129. bool m_alreadySent;
  130. friend class InspectorBackendDispatcherImpl;
  131. };
  132. $agentInterfaces
  133. $virtualSetters
  134. virtual void clearFrontend() = 0;
  135. enum CommonErrorCode {
  136. ParseError = 0,
  137. InvalidRequest,
  138. MethodNotFound,
  139. InvalidParams,
  140. InternalError,
  141. ServerError,
  142. LastEntry,
  143. };
  144. void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage) const;
  145. virtual void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage, PassRefPtr<InspectorArray> data) const = 0;
  146. virtual void dispatch(const String& message) = 0;
  147. static bool getCommandName(const String& message, String* result);
  148. enum MethodNames {
  149. $methodNamesEnumContent
  150. kMethodNamesEnumSize
  151. };
  152. static const char* commandNames[];
  153. };
  154. } // namespace WebCore
  155. #endif // !defined(InspectorBackendDispatcher_h)
  156. """)
  157. backend_cpp = (
  158. """
  159. #include "config.h"
  160. #if ENABLE(INSPECTOR)
  161. #include "InspectorBackendDispatcher.h"
  162. #include <wtf/text/WTFString.h>
  163. #include <wtf/text/CString.h>
  164. #include "InspectorAgent.h"
  165. #include "InspectorValues.h"
  166. #include "InspectorFrontendChannel.h"
  167. #include <wtf/text/WTFString.h>
  168. namespace WebCore {
  169. const char* InspectorBackendDispatcher::commandNames[] = {
  170. $methodNameDeclarations
  171. };
  172. class InspectorBackendDispatcherImpl : public InspectorBackendDispatcher {
  173. public:
  174. InspectorBackendDispatcherImpl(InspectorFrontendChannel* inspectorFrontendChannel)
  175. : m_inspectorFrontendChannel(inspectorFrontendChannel)
  176. $constructorInit
  177. { }
  178. virtual void clearFrontend() { m_inspectorFrontendChannel = 0; }
  179. virtual void dispatch(const String& message);
  180. virtual void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage, PassRefPtr<InspectorArray> data) const;
  181. using InspectorBackendDispatcher::reportProtocolError;
  182. void sendResponse(long callId, PassRefPtr<InspectorObject> result, const ErrorString& invocationError);
  183. bool isActive() { return m_inspectorFrontendChannel; }
  184. $setters
  185. private:
  186. $methodDeclarations
  187. InspectorFrontendChannel* m_inspectorFrontendChannel;
  188. $fieldDeclarations
  189. template<typename R, typename V, typename V0>
  190. static R getPropertyValueImpl(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors, V0 initial_value, bool (*as_method)(InspectorValue*, V*), const char* type_name);
  191. static int getInt(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors);
  192. static double getDouble(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors);
  193. static String getString(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors);
  194. static bool getBoolean(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors);
  195. static PassRefPtr<InspectorObject> getObject(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors);
  196. static PassRefPtr<InspectorArray> getArray(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors);
  197. void sendResponse(long callId, PassRefPtr<InspectorObject> result, const char* commandName, PassRefPtr<InspectorArray> protocolErrors, ErrorString invocationError);
  198. };
  199. $methods
  200. PassRefPtr<InspectorBackendDispatcher> InspectorBackendDispatcher::create(InspectorFrontendChannel* inspectorFrontendChannel)
  201. {
  202. return adoptRef(new InspectorBackendDispatcherImpl(inspectorFrontendChannel));
  203. }
  204. void InspectorBackendDispatcherImpl::dispatch(const String& message)
  205. {
  206. RefPtr<InspectorBackendDispatcher> protect = this;
  207. typedef void (InspectorBackendDispatcherImpl::*CallHandler)(long callId, InspectorObject* messageObject);
  208. typedef HashMap<String, CallHandler> DispatchMap;
  209. DEFINE_STATIC_LOCAL(DispatchMap, dispatchMap, );
  210. long callId = 0;
  211. if (dispatchMap.isEmpty()) {
  212. static CallHandler handlers[] = {
  213. $messageHandlers
  214. };
  215. size_t length = WTF_ARRAY_LENGTH(commandNames);
  216. for (size_t i = 0; i < length; ++i)
  217. dispatchMap.add(commandNames[i], handlers[i]);
  218. }
  219. RefPtr<InspectorValue> parsedMessage = InspectorValue::parseJSON(message);
  220. if (!parsedMessage) {
  221. reportProtocolError(0, ParseError, "Message must be in JSON format");
  222. return;
  223. }
  224. RefPtr<InspectorObject> messageObject = parsedMessage->asObject();
  225. if (!messageObject) {
  226. reportProtocolError(0, InvalidRequest, "Message must be a JSONified object");
  227. return;
  228. }
  229. RefPtr<InspectorValue> callIdValue = messageObject->get("id");
  230. if (!callIdValue) {
  231. reportProtocolError(0, InvalidRequest, "'id' property was not found");
  232. return;
  233. }
  234. if (!callIdValue->asNumber(&callId)) {
  235. reportProtocolError(0, InvalidRequest, "The type of 'id' property must be number");
  236. return;
  237. }
  238. RefPtr<InspectorValue> methodValue = messageObject->get("method");
  239. if (!methodValue) {
  240. reportProtocolError(&callId, InvalidRequest, "'method' property wasn't found");
  241. return;
  242. }
  243. String method;
  244. if (!methodValue->asString(&method)) {
  245. reportProtocolError(&callId, InvalidRequest, "The type of 'method' property must be string");
  246. return;
  247. }
  248. HashMap<String, CallHandler>::iterator it = dispatchMap.find(method);
  249. if (it == dispatchMap.end()) {
  250. reportProtocolError(&callId, MethodNotFound, "'" + method + "' wasn't found");
  251. return;
  252. }
  253. ((*this).*it->value)(callId, messageObject.get());
  254. }
  255. void InspectorBackendDispatcherImpl::sendResponse(long callId, PassRefPtr<InspectorObject> result, const char* commandName, PassRefPtr<InspectorArray> protocolErrors, ErrorString invocationError)
  256. {
  257. if (protocolErrors->length()) {
  258. String errorMessage = String::format("Some arguments of method '%s' can't be processed", commandName);
  259. reportProtocolError(&callId, InvalidParams, errorMessage, protocolErrors);
  260. return;
  261. }
  262. sendResponse(callId, result, invocationError);
  263. }
  264. void InspectorBackendDispatcherImpl::sendResponse(long callId, PassRefPtr<InspectorObject> result, const ErrorString& invocationError)
  265. {
  266. if (invocationError.length()) {
  267. reportProtocolError(&callId, ServerError, invocationError);
  268. return;
  269. }
  270. RefPtr<InspectorObject> responseMessage = InspectorObject::create();
  271. responseMessage->setObject("result", result);
  272. responseMessage->setNumber("id", callId);
  273. if (m_inspectorFrontendChannel)
  274. m_inspectorFrontendChannel->sendMessageToFrontend(responseMessage->toJSONString());
  275. }
  276. void InspectorBackendDispatcher::reportProtocolError(const long* const callId, CommonErrorCode code, const String& errorMessage) const
  277. {
  278. reportProtocolError(callId, code, errorMessage, 0);
  279. }
  280. void InspectorBackendDispatcherImpl::reportProtocolError(const long* const callId, CommonErrorCode code, const String& errorMessage, PassRefPtr<InspectorArray> data) const
  281. {
  282. DEFINE_STATIC_LOCAL(Vector<int>,s_commonErrors,);
  283. if (!s_commonErrors.size()) {
  284. s_commonErrors.insert(ParseError, -32700);
  285. s_commonErrors.insert(InvalidRequest, -32600);
  286. s_commonErrors.insert(MethodNotFound, -32601);
  287. s_commonErrors.insert(InvalidParams, -32602);
  288. s_commonErrors.insert(InternalError, -32603);
  289. s_commonErrors.insert(ServerError, -32000);
  290. }
  291. ASSERT(code >=0);
  292. ASSERT((unsigned)code < s_commonErrors.size());
  293. ASSERT(s_commonErrors[code]);
  294. RefPtr<InspectorObject> error = InspectorObject::create();
  295. error->setNumber("code", s_commonErrors[code]);
  296. error->setString("message", errorMessage);
  297. ASSERT(error);
  298. if (data)
  299. error->setArray("data", data);
  300. RefPtr<InspectorObject> message = InspectorObject::create();
  301. message->setObject("error", error);
  302. if (callId)
  303. message->setNumber("id", *callId);
  304. else
  305. message->setValue("id", InspectorValue::null());
  306. if (m_inspectorFrontendChannel)
  307. m_inspectorFrontendChannel->sendMessageToFrontend(message->toJSONString());
  308. }
  309. template<typename R, typename V, typename V0>
  310. R InspectorBackendDispatcherImpl::getPropertyValueImpl(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors, V0 initial_value, bool (*as_method)(InspectorValue*, V*), const char* type_name)
  311. {
  312. ASSERT(protocolErrors);
  313. if (valueFound)
  314. *valueFound = false;
  315. V value = initial_value;
  316. if (!object) {
  317. if (!valueFound) {
  318. // Required parameter in missing params container.
  319. protocolErrors->pushString(String::format("'params' object must contain required parameter '%s' with type '%s'.", name.utf8().data(), type_name));
  320. }
  321. return value;
  322. }
  323. InspectorObject::const_iterator end = object->end();
  324. InspectorObject::const_iterator valueIterator = object->find(name);
  325. if (valueIterator == end) {
  326. if (!valueFound)
  327. protocolErrors->pushString(String::format("Parameter '%s' with type '%s' was not found.", name.utf8().data(), type_name));
  328. return value;
  329. }
  330. if (!as_method(valueIterator->value.get(), &value))
  331. protocolErrors->pushString(String::format("Parameter '%s' has wrong type. It must be '%s'.", name.utf8().data(), type_name));
  332. else
  333. if (valueFound)
  334. *valueFound = true;
  335. return value;
  336. }
  337. struct AsMethodBridges {
  338. static bool asInt(InspectorValue* value, int* output) { return value->asNumber(output); }
  339. static bool asDouble(InspectorValue* value, double* output) { return value->asNumber(output); }
  340. static bool asString(InspectorValue* value, String* output) { return value->asString(output); }
  341. static bool asBoolean(InspectorValue* value, bool* output) { return value->asBoolean(output); }
  342. static bool asObject(InspectorValue* value, RefPtr<InspectorObject>* output) { return value->asObject(output); }
  343. static bool asArray(InspectorValue* value, RefPtr<InspectorArray>* output) { return value->asArray(output); }
  344. };
  345. int InspectorBackendDispatcherImpl::getInt(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors)
  346. {
  347. return getPropertyValueImpl<int, int, int>(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asInt, "Number");
  348. }
  349. double InspectorBackendDispatcherImpl::getDouble(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors)
  350. {
  351. return getPropertyValueImpl<double, double, double>(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asDouble, "Number");
  352. }
  353. String InspectorBackendDispatcherImpl::getString(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors)
  354. {
  355. return getPropertyValueImpl<String, String, String>(object, name, valueFound, protocolErrors, "", AsMethodBridges::asString, "String");
  356. }
  357. bool InspectorBackendDispatcherImpl::getBoolean(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors)
  358. {
  359. return getPropertyValueImpl<bool, bool, bool>(object, name, valueFound, protocolErrors, false, AsMethodBridges::asBoolean, "Boolean");
  360. }
  361. PassRefPtr<InspectorObject> InspectorBackendDispatcherImpl::getObject(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors)
  362. {
  363. return getPropertyValueImpl<PassRefPtr<InspectorObject>, RefPtr<InspectorObject>, InspectorObject*>(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asObject, "Object");
  364. }
  365. PassRefPtr<InspectorArray> InspectorBackendDispatcherImpl::getArray(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors)
  366. {
  367. return getPropertyValueImpl<PassRefPtr<InspectorArray>, RefPtr<InspectorArray>, InspectorArray*>(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asArray, "Array");
  368. }
  369. bool InspectorBackendDispatcher::getCommandName(const String& message, String* result)
  370. {
  371. RefPtr<InspectorValue> value = InspectorValue::parseJSON(message);
  372. if (!value)
  373. return false;
  374. RefPtr<InspectorObject> object = value->asObject();
  375. if (!object)
  376. return false;
  377. if (!object->getString("method", result))
  378. return false;
  379. return true;
  380. }
  381. InspectorBackendDispatcher::CallbackBase::CallbackBase(PassRefPtr<InspectorBackendDispatcherImpl> backendImpl, int id)
  382. : m_backendImpl(backendImpl), m_id(id), m_alreadySent(false) {}
  383. InspectorBackendDispatcher::CallbackBase::~CallbackBase() {}
  384. void InspectorBackendDispatcher::CallbackBase::sendFailure(const ErrorString& error)
  385. {
  386. ASSERT(error.length());
  387. sendIfActive(0, error);
  388. }
  389. bool InspectorBackendDispatcher::CallbackBase::isActive()
  390. {
  391. return !m_alreadySent && m_backendImpl->isActive();
  392. }
  393. void InspectorBackendDispatcher::CallbackBase::sendIfActive(PassRefPtr<InspectorObject> partialMessage, const ErrorString& invocationError)
  394. {
  395. if (m_alreadySent)
  396. return;
  397. m_backendImpl->sendResponse(m_id, partialMessage, invocationError);
  398. m_alreadySent = true;
  399. }
  400. COMPILE_ASSERT(static_cast<int>(InspectorBackendDispatcher::kMethodNamesEnumSize) == WTF_ARRAY_LENGTH(InspectorBackendDispatcher::commandNames), command_name_array_problem);
  401. } // namespace WebCore
  402. #endif // ENABLE(INSPECTOR)
  403. """)
  404. frontend_cpp = (
  405. """
  406. #include "config.h"
  407. #if ENABLE(INSPECTOR)
  408. #include "InspectorFrontend.h"
  409. #include <wtf/text/WTFString.h>
  410. #include <wtf/text/CString.h>
  411. #include "InspectorFrontendChannel.h"
  412. #include "InspectorValues.h"
  413. #include <wtf/text/WTFString.h>
  414. namespace WebCore {
  415. InspectorFrontend::InspectorFrontend(InspectorFrontendChannel* inspectorFrontendChannel)
  416. : $constructorInit{
  417. }
  418. $methods
  419. } // namespace WebCore
  420. #endif // ENABLE(INSPECTOR)
  421. """)
  422. typebuilder_h = (
  423. """
  424. #ifndef InspectorTypeBuilder_h
  425. #define InspectorTypeBuilder_h
  426. #if ENABLE(INSPECTOR)
  427. #include "InspectorValues.h"
  428. #include <wtf/Assertions.h>
  429. #include <wtf/PassRefPtr.h>
  430. namespace WebCore {
  431. namespace TypeBuilder {
  432. template<typename T>
  433. class OptOutput {
  434. public:
  435. OptOutput() : m_assigned(false) { }
  436. void operator=(T value)
  437. {
  438. m_value = value;
  439. m_assigned = true;
  440. }
  441. bool isAssigned() { return m_assigned; }
  442. T getValue()
  443. {
  444. ASSERT(isAssigned());
  445. return m_value;
  446. }
  447. private:
  448. T m_value;
  449. bool m_assigned;
  450. WTF_MAKE_NONCOPYABLE(OptOutput);
  451. };
  452. // A small transient wrapper around int type, that can be used as a funciton parameter type
  453. // cleverly disallowing C++ implicit casts from float or double.
  454. class ExactlyInt {
  455. public:
  456. template<typename T>
  457. ExactlyInt(T t) : m_value(cast_to_int<T>(t)) {}
  458. ExactlyInt() {}
  459. operator int() { return m_value; }
  460. private:
  461. int m_value;
  462. template<typename T>
  463. static int cast_to_int(T) { return T::default_case_cast_is_not_supported(); }
  464. };
  465. template<>
  466. inline int ExactlyInt::cast_to_int<int>(int i) { return i; }
  467. template<>
  468. inline int ExactlyInt::cast_to_int<unsigned int>(unsigned int i) { return i; }
  469. class RuntimeCastHelper {
  470. public:
  471. #if $validatorIfdefName
  472. template<InspectorValue::Type TYPE>
  473. static void assertType(InspectorValue* value)
  474. {
  475. ASSERT(value->type() == TYPE);
  476. }
  477. static void assertAny(InspectorValue*);
  478. static void assertInt(InspectorValue* value);
  479. #endif
  480. };
  481. // This class provides "Traits" type for the input type T. It is programmed using C++ template specialization
  482. // technique. By default it simply takes "ItemTraits" type from T, but it doesn't work with the base types.
  483. template<typename T>
  484. struct ArrayItemHelper {
  485. typedef typename T::ItemTraits Traits;
  486. };
  487. template<typename T>
  488. class Array : public InspectorArrayBase {
  489. private:
  490. Array() { }
  491. InspectorArray* openAccessors() {
  492. COMPILE_ASSERT(sizeof(InspectorArray) == sizeof(Array<T>), cannot_cast);
  493. return static_cast<InspectorArray*>(static_cast<InspectorArrayBase*>(this));
  494. }
  495. public:
  496. void addItem(PassRefPtr<T> value)
  497. {
  498. ArrayItemHelper<T>::Traits::pushRefPtr(this->openAccessors(), value);
  499. }
  500. void addItem(T value)
  501. {
  502. ArrayItemHelper<T>::Traits::pushRaw(this->openAccessors(), value);
  503. }
  504. static PassRefPtr<Array<T> > create()
  505. {
  506. return adoptRef(new Array<T>());
  507. }
  508. static PassRefPtr<Array<T> > runtimeCast(PassRefPtr<InspectorValue> value)
  509. {
  510. RefPtr<InspectorArray> array;
  511. bool castRes = value->asArray(&array);
  512. ASSERT_UNUSED(castRes, castRes);
  513. #if $validatorIfdefName
  514. assertCorrectValue(array.get());
  515. #endif // $validatorIfdefName
  516. COMPILE_ASSERT(sizeof(Array<T>) == sizeof(InspectorArray), type_cast_problem);
  517. return static_cast<Array<T>*>(static_cast<InspectorArrayBase*>(array.get()));
  518. }
  519. #if $validatorIfdefName
  520. static void assertCorrectValue(InspectorValue* value)
  521. {
  522. RefPtr<InspectorArray> array;
  523. bool castRes = value->asArray(&array);
  524. ASSERT_UNUSED(castRes, castRes);
  525. for (unsigned i = 0; i < array->length(); i++)
  526. ArrayItemHelper<T>::Traits::template assertCorrectValue<T>(array->get(i).get());
  527. }
  528. #endif // $validatorIfdefName
  529. };
  530. struct StructItemTraits {
  531. static void pushRefPtr(InspectorArray* array, PassRefPtr<InspectorValue> value)
  532. {
  533. array->pushValue(value);
  534. }
  535. #if $validatorIfdefName
  536. template<typename T>
  537. static void assertCorrectValue(InspectorValue* value) {
  538. T::assertCorrectValue(value);
  539. }
  540. #endif // $validatorIfdefName
  541. };
  542. template<>
  543. struct ArrayItemHelper<String> {
  544. struct Traits {
  545. static void pushRaw(InspectorArray* array, const String& value)
  546. {
  547. array->pushString(value);
  548. }
  549. #if $validatorIfdefName
  550. template<typename T>
  551. static void assertCorrectValue(InspectorValue* value) {
  552. RuntimeCastHelper::assertType<InspectorValue::TypeString>(value);
  553. }
  554. #endif // $validatorIfdefName
  555. };
  556. };
  557. template<>
  558. struct ArrayItemHelper<int> {
  559. struct Traits {
  560. static void pushRaw(InspectorArray* array, int value)
  561. {
  562. array->pushInt(value);
  563. }
  564. #if $validatorIfdefName
  565. template<typename T>
  566. static void assertCorrectValue(InspectorValue* value) {
  567. RuntimeCastHelper::assertInt(value);
  568. }
  569. #endif // $validatorIfdefName
  570. };
  571. };
  572. template<>
  573. struct ArrayItemHelper<double> {
  574. struct Traits {
  575. static void pushRaw(InspectorArray* array, double value)
  576. {
  577. array->pushNumber(value);
  578. }
  579. #if $validatorIfdefName
  580. template<typename T>
  581. static void assertCorrectValue(InspectorValue* value) {
  582. RuntimeCastHelper::assertType<InspectorValue::TypeNumber>(value);
  583. }
  584. #endif // $validatorIfdefName
  585. };
  586. };
  587. template<>
  588. struct ArrayItemHelper<bool> {
  589. struct Traits {
  590. static void pushRaw(InspectorArray* array, bool value)
  591. {
  592. array->pushBoolean(value);
  593. }
  594. #if $validatorIfdefName
  595. template<typename T>
  596. static void assertCorrectValue(InspectorValue* value) {
  597. RuntimeCastHelper::assertType<InspectorValue::TypeBoolean>(value);
  598. }
  599. #endif // $validatorIfdefName
  600. };
  601. };
  602. template<>
  603. struct ArrayItemHelper<InspectorValue> {
  604. struct Traits {
  605. static void pushRefPtr(InspectorArray* array, PassRefPtr<InspectorValue> value)
  606. {
  607. array->pushValue(value);
  608. }
  609. #if $validatorIfdefName
  610. template<typename T>
  611. static void assertCorrectValue(InspectorValue* value) {
  612. RuntimeCastHelper::assertAny(value);
  613. }
  614. #endif // $validatorIfdefName
  615. };
  616. };
  617. template<>
  618. struct ArrayItemHelper<InspectorObject> {
  619. struct Traits {
  620. static void pushRefPtr(InspectorArray* array, PassRefPtr<InspectorValue> value)
  621. {
  622. array->pushValue(value);
  623. }
  624. #if $validatorIfdefName
  625. template<typename T>
  626. static void assertCorrectValue(InspectorValue* value) {
  627. RuntimeCastHelper::assertType<InspectorValue::TypeObject>(value);
  628. }
  629. #endif // $validatorIfdefName
  630. };
  631. };
  632. template<>
  633. struct ArrayItemHelper<InspectorArray> {
  634. struct Traits {
  635. static void pushRefPtr(InspectorArray* array, PassRefPtr<InspectorArray> value)
  636. {
  637. array->pushArray(value);
  638. }
  639. #if $validatorIfdefName
  640. template<typename T>
  641. static void assertCorrectValue(InspectorValue* value) {
  642. RuntimeCastHelper::assertType<InspectorValue::TypeArray>(value);
  643. }
  644. #endif // $validatorIfdefName
  645. };
  646. };
  647. template<typename T>
  648. struct ArrayItemHelper<TypeBuilder::Array<T> > {
  649. struct Traits {
  650. static void pushRefPtr(InspectorArray* array, PassRefPtr<TypeBuilder::Array<T> > value)
  651. {
  652. array->pushValue(value);
  653. }
  654. #if $validatorIfdefName
  655. template<typename S>
  656. static void assertCorrectValue(InspectorValue* value) {
  657. S::assertCorrectValue(value);
  658. }
  659. #endif // $validatorIfdefName
  660. };
  661. };
  662. ${forwards}
  663. String getEnumConstantValue(int code);
  664. ${typeBuilders}
  665. } // namespace TypeBuilder
  666. } // namespace WebCore
  667. #endif // ENABLE(INSPECTOR)
  668. #endif // !defined(InspectorTypeBuilder_h)
  669. """)
  670. typebuilder_cpp = (
  671. """
  672. #include "config.h"
  673. #if ENABLE(INSPECTOR)
  674. #include "InspectorTypeBuilder.h"
  675. #include <wtf/text/CString.h>
  676. namespace WebCore {
  677. namespace TypeBuilder {
  678. const char* const enum_constant_values[] = {
  679. $enumConstantValues};
  680. String getEnumConstantValue(int code) {
  681. return enum_constant_values[code];
  682. }
  683. } // namespace TypeBuilder
  684. $implCode
  685. #if $validatorIfdefName
  686. void TypeBuilder::RuntimeCastHelper::assertAny(InspectorValue*)
  687. {
  688. // No-op.
  689. }
  690. void TypeBuilder::RuntimeCastHelper::assertInt(InspectorValue* value)
  691. {
  692. double v;
  693. bool castRes = value->asNumber(&v);
  694. ASSERT_UNUSED(castRes, castRes);
  695. ASSERT(static_cast<double>(static_cast<int>(v)) == v);
  696. }
  697. $validatorCode
  698. #endif // $validatorIfdefName
  699. } // namespace WebCore
  700. #endif // ENABLE(INSPECTOR)
  701. """)
  702. backend_js = (
  703. """
  704. $domainInitializers
  705. """)
  706. param_container_access_code = """
  707. RefPtr<InspectorObject> paramsContainer = requestMessageObject->getObject("params");
  708. InspectorObject* paramsContainerPtr = paramsContainer.get();
  709. InspectorArray* protocolErrorsPtr = protocolErrors.get();
  710. """
  711. class_binding_builder_part_1 = (
  712. """ AllFieldsSet = %s
  713. };
  714. template<int STATE>
  715. class Builder {
  716. private:
  717. RefPtr<InspectorObject> m_result;
  718. template<int STEP> Builder<STATE | STEP>& castState()
  719. {
  720. return *reinterpret_cast<Builder<STATE | STEP>*>(this);
  721. }
  722. Builder(PassRefPtr</*%s*/InspectorObject> ptr)
  723. {
  724. COMPILE_ASSERT(STATE == NoFieldsSet, builder_created_in_non_init_state);
  725. m_result = ptr;
  726. }
  727. friend class %s;
  728. public:
  729. """)
  730. class_binding_builder_part_2 = ("""
  731. Builder<STATE | %s>& set%s(%s value)
  732. {
  733. COMPILE_ASSERT(!(STATE & %s), property_%s_already_set);
  734. m_result->set%s("%s", %s);
  735. return castState<%s>();
  736. }
  737. """)
  738. class_binding_builder_part_3 = ("""
  739. operator RefPtr<%s>& ()
  740. {
  741. COMPILE_ASSERT(STATE == AllFieldsSet, result_is_not_ready);
  742. COMPILE_ASSERT(sizeof(%s) == sizeof(InspectorObject), cannot_cast);
  743. return *reinterpret_cast<RefPtr<%s>*>(&m_result);
  744. }
  745. PassRefPtr<%s> release()
  746. {
  747. return RefPtr<%s>(*this).release();
  748. }
  749. };
  750. """)
  751. class_binding_builder_part_4 = (
  752. """ static Builder<NoFieldsSet> create()
  753. {
  754. return Builder<NoFieldsSet>(InspectorObject::create());
  755. }
  756. """)