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