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