1 # Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 package CodeGeneratorInspector;
13 $typeTransform{"ApplicationCache"} = {
14 "forward" => "InspectorApplicationCacheAgent",
15 "header" => "InspectorApplicationCacheAgent.h",
16 "domainAccessor" => "m_inspectorAgent->applicationCacheAgent()",
18 $typeTransform{"CSS"} = {
19 "forward" => "InspectorCSSAgent",
20 "header" => "InspectorCSSAgent.h",
21 "domainAccessor" => "m_inspectorAgent->cssAgent()",
23 $typeTransform{"Console"} = {
24 "forward" => "InspectorConsoleAgent",
25 "header" => "InspectorConsoleAgent.h",
26 "domainAccessor" => "m_inspectorAgent->consoleAgent()",
28 $typeTransform{"Debugger"} = {
29 "forward" => "InspectorDebuggerAgent",
30 "header" => "InspectorDebuggerAgent.h",
31 "domainAccessor" => "m_inspectorAgent->debuggerAgent()",
33 $typeTransform{"BrowserDebugger"} = {
34 "forward" => "InspectorBrowserDebuggerAgent",
35 "header" => "InspectorBrowserDebuggerAgent.h",
36 "domainAccessor" => "m_inspectorAgent->browserDebuggerAgent()",
38 $typeTransform{"Database"} = {
39 "forward" => "InspectorDatabaseAgent",
40 "header" => "InspectorDatabaseAgent.h",
41 "domainAccessor" => "m_inspectorAgent->databaseAgent()",
43 $typeTransform{"DOM"} = {
44 "forward" => "InspectorDOMAgent",
45 "header" => "InspectorDOMAgent.h",
46 "domainAccessor" => "m_inspectorAgent->domAgent()",
48 $typeTransform{"DOMStorage"} = {
49 "forward" => "InspectorDOMStorageAgent",
50 "header" => "InspectorDOMStorageAgent.h",
51 "domainAccessor" => "m_inspectorAgent->domStorageAgent()",
53 $typeTransform{"FileSystem"} = {
54 "forward" => "InspectorFileSystemAgent",
55 "header" => "InspectorFileSystemAgent.h",
56 "domainAccessor" => "m_inspectorAgent->fileSystemAgent()",
58 $typeTransform{"InjectedScript"} = {
59 "forwardHeader" => "InjectedScriptHost.h",
60 "domainAccessor" => "m_inspectorAgent->injectedScriptAgent()",
62 $typeTransform{"Inspector"} = {
63 "forwardHeader" => "InspectorAgent.h",
64 "domainAccessor" => "m_inspectorAgent",
66 $typeTransform{"Network"} = {
67 "forward" => "InspectorResourceAgent",
68 "header" => "InspectorResourceAgent.h",
69 "domainAccessor" => "m_inspectorAgent->resourceAgent()",
71 $typeTransform{"Profiler"} = {
72 "forward" => "InspectorProfilerAgent",
73 "header" => "InspectorProfilerAgent.h",
74 "domainAccessor" => "m_inspectorAgent->profilerAgent()",
76 $typeTransform{"Runtime"} = {
77 "forward" => "InspectorRuntimeAgent",
78 "header" => "InspectorRuntimeAgent.h",
79 "domainAccessor" => "m_inspectorAgent->runtimeAgent()",
81 $typeTransform{"Timeline"} = {
82 "forward" => "InspectorTimelineAgent",
83 "header" => "InspectorTimelineAgent.h",
84 "domainAccessor" => "m_inspectorAgent->timelineAgent()",
87 $typeTransform{"Frontend"} = {
88 "forward" => "InspectorFrontend",
89 "header" => "InspectorFrontend.h",
91 $typeTransform{"InspectorClient"} = {
92 "forward" => "InspectorClient",
93 "header" => "InspectorClient.h",
95 $typeTransform{"PassRefPtr"} = {
96 "forwardHeader" => "wtf/PassRefPtr.h",
98 $typeTransform{"Object"} = {
99 "param" => "PassRefPtr<InspectorObject>",
100 "variable" => "RefPtr<InspectorObject>",
101 "defaultValue" => "InspectorObject::create()",
102 "forward" => "InspectorObject",
103 "header" => "InspectorValues.h",
104 "JSONType" => "Object",
105 "JSType" => "object",
108 $typeTransform{"Array"} = {
109 "param" => "PassRefPtr<InspectorArray>",
110 "variable" => "RefPtr<InspectorArray>",
111 "defaultValue" => "InspectorArray::create()",
112 "forward" => "InspectorArray",
113 "header" => "InspectorValues.h",
114 "JSONType" => "Array",
115 "JSType" => "object",
116 "DocType" => "array of %s"
118 $typeTransform{"Value"} = {
119 "param" => "PassRefPtr<InspectorValue>",
120 "variable" => "RefPtr<InspectorValue>",
121 "defaultValue" => "InspectorValue::null()",
122 "forward" => "InspectorValue",
123 "header" => "InspectorValues.h",
124 "JSONType" => "Value",
128 $typeTransform{"String"} = {
129 "param" => "const String&",
130 "variable" => "String",
131 "return" => "String",
132 "defaultValue" => "\"\"",
133 "forwardHeader" => "PlatformString.h",
134 "header" => "PlatformString.h",
135 "JSONType" => "String",
138 $typeTransform{"long"} = {
140 "variable" => "long",
141 "defaultValue" => "0",
144 "JSONType" => "Number",
147 $typeTransform{"int"} = {
150 "defaultValue" => "0",
153 "JSONType" => "Number",
156 $typeTransform{"unsigned long"} = {
157 "param" => "unsigned long",
158 "variable" => "unsigned long",
159 "defaultValue" => "0u",
162 "JSONType" => "Number",
165 $typeTransform{"unsigned int"} = {
166 "param" => "unsigned int",
167 "variable" => "unsigned int",
168 "defaultValue" => "0u",
171 "JSONType" => "Number",
174 $typeTransform{"double"} = {
176 "variable" => "double",
177 "defaultValue" => "0.0",
180 "JSONType" => "Number",
183 $typeTransform{"boolean"} = {
186 "defaultValue" => "false",
189 "JSONType" => "Boolean",
190 "JSType" => "boolean"
192 $typeTransform{"void"} = {
197 # Default License Templates
199 my $licenseTemplate = << "EOF";
200 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
201 // Use of this source code is governed by a BSD-style license that can be
202 // found in the LICENSE file.
207 my $outputHeadersDir;
208 my $writeDependencies;
213 my $backendClassName;
214 my $backendJSStubName;
217 my @backendMethodsImpl;
218 my %backendMethodSignatures;
219 my $backendConstructor;
220 my @backendConstantDeclarations;
221 my @backendConstantDefinitions;
225 my $frontendClassName;
228 my @frontendMethodsImpl;
229 my %frontendMethodSignatures;
230 my $frontendConstructor;
231 my @frontendConstantDeclarations;
232 my @frontendConstantDefinitions;
235 my @documentationToc;
236 my @documentationLines;
238 # Default constructor
244 $codeGenerator = shift;
246 $outputHeadersDir = shift;
247 shift; # $useLayerOnTop
248 shift; # $preprocessor
249 $writeDependencies = shift;
252 bless($reference, $object);
256 # Params: 'idlDocument' struct
260 my $dataNode = shift;
262 $namespace = $dataNode->module;
263 $namespace =~ s/core/WebCore/;
266 # Params: 'idlDocument' struct
267 sub GenerateInterface
270 my $interface = shift;
273 my $className = $interface->name;
275 $frontendClassName = "InspectorFrontend";
276 $frontendConstructor = " ${frontendClassName}(InspectorClient* inspectorClient) : m_inspectorClient(inspectorClient) { }";
277 $frontendFooter = " private:\n InspectorClient* m_inspectorClient;";
278 $frontendTypes{"String"} = 1;
279 $frontendTypes{"InspectorClient"} = 1;
280 $frontendTypes{"PassRefPtr"} = 1;
282 $backendClassName = "InspectorBackendDispatcher";
283 $backendJSStubName = "InspectorBackendStub";
285 push(@backendHead, " ${backendClassName}(InspectorAgent* inspectorAgent) : m_inspectorAgent(inspectorAgent) { }");
286 push(@backendHead, " void reportProtocolError(const long callId, const String& errorText) const;");
287 push(@backendHead, " void dispatch(const String& message);");
288 push(@backendHead, " static bool getCommandName(const String& message, String* result);");
289 $backendConstructor = join("\n", @backendHead);
290 $backendFooter = " InspectorAgent* m_inspectorAgent;";
291 $backendTypes{"Inspector"} = 1;
292 $backendTypes{"InspectorClient"} = 1;
293 $backendTypes{"PassRefPtr"} = 1;
294 $backendTypes{"Object"} = 1;
296 generateFunctions($interface);
299 sub generateFunctions
301 my $interface = shift;
303 foreach my $function (@{$interface->functions}) {
304 if ($function->signature->extendedAttributes->{"event"}) {
305 generateFrontendFunction($interface, $function);
307 generateBackendFunction($interface, $function);
311 push(@documentationToc, "<li><a href='#" . $interface->name . "'>" . $interface->name . "</a></li>");
312 push(@documentationLines, "<h2 id='" . $interface->name . "'><a name=" . $interface->name . "></a>" . $interface->name . "</h2>");
314 push(@documentationLines, "<h3>Events</h3>");
315 foreach my $function (grep($_->signature->extendedAttributes->{"event"}, @{$interface->functions}) ) {
316 generateDocumentationEvent($interface, $function);
319 push(@documentationLines, "<h3>Commands</h3>");
320 foreach my $function (grep(!$_->signature->extendedAttributes->{"event"}, @{$interface->functions})) {
321 generateDocumentationCommand($interface, $function);
324 collectBackendJSStubFunctions($interface);
327 sub generateFrontendFunction
329 my $interface = shift;
330 my $function = shift;
332 my $functionName = $function->signature->name;
334 my $domain = $interface->name;
335 my @argsFiltered = grep($_->direction eq "out", @{$function->parameters}); # just keep only out parameters for frontend interface.
336 map($frontendTypes{$_->type} = 1, @argsFiltered); # register required types.
337 my $arguments = join(", ", map(typeTraits($_->type, "param") . " " . $_->name, @argsFiltered)); # prepare arguments for function signature.
339 my $signature = " void ${functionName}(${arguments});";
340 !$frontendMethodSignatures{${signature}} || die "Duplicate frontend function was detected for signature '$signature'.";
341 $frontendMethodSignatures{${signature}} = 1;
342 push(@frontendMethods, $signature);
345 push(@function, "void ${frontendClassName}::${functionName}(${arguments})");
346 push(@function, "{");
347 push(@function, " RefPtr<InspectorObject> ${functionName}Message = InspectorObject::create();");
348 push(@function, " ${functionName}Message->setString(\"type\", \"event\");");
349 push(@function, " ${functionName}Message->setString(\"domain\", \"$domain\");");
350 push(@function, " ${functionName}Message->setString(\"event\", \"$functionName\");");
351 push(@function, " RefPtr<InspectorObject> payloadDataObject = InspectorObject::create();");
352 my @pushArguments = map(" payloadDataObject->set" . typeTraits($_->type, "JSONType") . "(\"" . $_->name . "\", " . $_->name . ");", @argsFiltered);
353 push(@function, @pushArguments);
354 push(@function, " ${functionName}Message->setObject(\"data\", payloadDataObject);");
355 push(@function, " m_inspectorClient->sendMessageToFrontend(${functionName}Message->toJSONString());");
357 push(@function, "}");
359 push(@frontendMethodsImpl, @function);
362 sub generateDocumentationEvent
364 my $interface = shift;
365 my $function = shift;
367 my $functionName = $function->signature->name;
368 my $domain = $interface->name;
370 my @argsFiltered = grep($_->direction eq "out", @{$function->parameters});
373 push(@lines, "<h4>" . $interface->name . "." . ${functionName} . "</h4>");
374 my $doc = $function->signature->extendedAttributes->{"doc"};
379 push(@lines, "<pre style='background: lightGrey; padding: 10px'>");
381 push(@lines, " seq: <number>,");
382 push(@lines, " type: \"event\",");
383 push(@lines, " domain: \"$domain\",");
384 if (scalar(@argsFiltered)) {
385 push(@lines, " event: \"${functionName}\",");
386 push(@lines, " data: {");
388 foreach my $parameter (@argsFiltered) {
389 push(@parameters, " " . parameterDocLine($parameter));
391 push(@lines, join(",\n", @parameters));
394 push(@lines, " event: \"${functionName}\"");
397 push(@lines, "</pre>");
398 push(@documentationLines, @lines);
404 $value =~ s/\b(\w)/\U$1/g; # make a camel-case name for type name
409 sub generateBackendFunction
411 my $interface = shift;
412 my $function = shift;
414 my $functionName = $function->signature->name;
415 my $fullQualifiedFunctionName = $interface->name . "_" . $function->signature->name;
417 push(@backendConstantDeclarations, " static const char* ${fullQualifiedFunctionName}Cmd;");
418 push(@backendConstantDefinitions, "const char* ${backendClassName}::${fullQualifiedFunctionName}Cmd = \"${functionName}\";");
420 map($backendTypes{$_->type} = 1, @{$function->parameters}); # register required types
421 my @inArgs = grep($_->direction eq "in" && !($_->name eq "callId") , @{$function->parameters});
422 my @outArgs = grep($_->direction eq "out", @{$function->parameters});
424 my $signature = " void ${fullQualifiedFunctionName}(long callId, InspectorObject* requestMessageObject);";
425 !$backendMethodSignatures{${signature}} || die "Duplicate function was detected for signature '$signature'.";
426 $backendMethodSignatures{${signature}} = "$fullQualifiedFunctionName";
427 push(@backendMethods, ${signature});
430 my $requestMessageObject = scalar(@inArgs) ? " requestMessageObject" : "";
431 push(@function, "void ${backendClassName}::${fullQualifiedFunctionName}(long callId, InspectorObject*$requestMessageObject)");
432 push(@function, "{");
433 push(@function, " RefPtr<InspectorArray> protocolErrors = InspectorArray::create();");
436 my $domain = $interface->name;
437 my $domainAccessor = typeTraits($domain, "domainAccessor");
438 $backendTypes{$domain} = 1;
439 push(@function, " if (!$domainAccessor)");
440 push(@function, " protocolErrors->pushString(\"Protocol Error: $domain handler is not available.\");");
443 # declare local variables for out arguments.
444 push(@function, map(" " . typeTraits($_->type, "variable") . " " . $_->name . " = " . typeTraits($_->type, "defaultValue") . ";", @outArgs));
447 if (scalar(@inArgs)) {
448 push(@function, " if (RefPtr<InspectorObject> argumentsContainer = requestMessageObject->getObject(\"arguments\")) {");
450 foreach my $parameter (@inArgs) {
451 my $name = $parameter->name;
452 my $type = $parameter->type;
453 my $typeString = camelCase($parameter->type);
454 push(@function, " " . typeTraits($type, "variable") . " $name = get$typeString(argumentsContainer.get(), \"$name\", protocolErrors.get());");
460 my $args = join(", ", (map($_->name, @inArgs), map("&" . $_->name, @outArgs)));
461 push(@function, "$indent if (!protocolErrors->length())");
462 push(@function, "$indent $domainAccessor->$functionName($args);");
463 if (scalar(@inArgs)) {
464 push(@function, " } else {");
465 push(@function, " protocolErrors->pushString(\"Protocol Error: 'arguments' property with type 'object' was not found.\");");
466 push(@function, " }");
469 push(@function, " // use InspectorFrontend as a marker of WebInspector availability");
470 push(@function, " if ((callId || protocolErrors->length()) && m_inspectorAgent->hasFrontend()) {");
471 push(@function, " RefPtr<InspectorObject> responseMessage = InspectorObject::create();");
472 push(@function, " responseMessage->setNumber(\"seq\", callId);");
473 push(@function, " responseMessage->setString(\"domain\", \"$domain\");");
474 push(@function, " responseMessage->setBoolean(\"success\", !protocolErrors->length());");
476 push(@function, " if (protocolErrors->length())");
477 push(@function, " responseMessage->setArray(\"errors\", protocolErrors);");
478 if (scalar(@outArgs)) {
479 push(@function, " else {");
480 push(@function, " RefPtr<InspectorObject> responseData = InspectorObject::create();");
481 push(@function, map(" responseData->set" . typeTraits($_->type, "JSONType") . "(\"" . $_->name . "\", " . $_->name . ");", @outArgs));
482 push(@function, " responseMessage->setObject(\"data\", responseData);");
483 push(@function, " }");
485 push(@function, " m_inspectorAgent->inspectorClient()->sendMessageToFrontend(responseMessage->toJSONString());");
486 push(@function, " }");
489 push(@function, "}");
491 push(@backendMethodsImpl, @function);
494 sub generateDocumentationCommand
496 my $interface = shift;
497 my $function = shift;
499 my $functionName = $function->signature->name;
500 my $domain = $interface->name;
504 push(@lines, "<h4>" . $interface->name . "." . ${functionName} . "</h4>");
505 my $doc = $function->signature->extendedAttributes->{"doc"};
510 my @inArgs = grep($_->direction eq "in" && !($_->name eq "callId") , @{$function->parameters});
511 push(@lines, "<pre style='background: lightGrey; padding: 10px'>");
512 push(@lines, "request: {");
513 push(@lines, " seq: <number>,");
514 push(@lines, " type: \"request\",");
515 push(@lines, " domain: \"" . $interface->name . "\",");
516 if (scalar(@inArgs)) {
517 push(@lines, " command: \"${functionName}\",");
518 push(@lines, " arguments: {");
520 foreach my $parameter (@inArgs) {
521 push(@parameters, " " . parameterDocLine($parameter));
523 push(@lines, join(",\n", @parameters));
526 push(@lines, " command: \"${functionName}\"");
530 my @outArgs = grep($_->direction eq "out", @{$function->parameters});
532 push(@lines, "response: {");
533 push(@lines, " seq: <number>,");
534 if (scalar(@outArgs)) {
535 push(@lines, " type: \"response\",");
536 push(@lines, " body: {");
538 foreach my $parameter (@outArgs) {
539 push(@parameters, " " . parameterDocLine($parameter));
541 push(@lines, join(",\n", @parameters));
544 push(@lines, " type: \"response\"");
547 push(@lines, "</pre>");
549 push(@documentationLines, @lines);
552 sub generateBackendReportProtocolError
554 my $reportProtocolError = << "EOF";
556 void ${backendClassName}::reportProtocolError(const long callId, const String& errorText) const
558 RefPtr<InspectorObject> message = InspectorObject::create();
559 message->setNumber("seq", callId);
560 message->setBoolean("success", false);
561 RefPtr<InspectorArray> errors = InspectorArray::create();
562 errors->pushString(errorText);
563 message->setArray("errors", errors);
564 m_inspectorAgent->inspectorClient()->sendMessageToFrontend(message->toJSONString());
567 return split("\n", $reportProtocolError);
570 sub generateArgumentGetters
573 my $json = typeTraits($type, "JSONType");
574 my $variable = typeTraits($type, "variable");
575 my $defaultValue = typeTraits($type, "defaultValue");
576 my $return = typeTraits($type, "return") ? typeTraits($type, "return") : typeTraits($type, "param");
578 my $typeString = camelCase($type);
579 push(@backendConstantDeclarations, " $return get$typeString(InspectorObject* object, const String& name, InspectorArray* protocolErrors);");
580 my $getterBody = << "EOF";
582 $return InspectorBackendDispatcher::get$typeString(InspectorObject* object, const String& name, InspectorArray* protocolErrors)
585 ASSERT(protocolErrors);
587 $variable value = $defaultValue;
588 InspectorObject::const_iterator end = object->end();
589 InspectorObject::const_iterator valueIterator = object->find(name);
591 if (valueIterator == end)
592 protocolErrors->pushString(String::format("Protocol Error: Argument '\%s' with type '$json' was not found.", name.utf8().data()));
594 if (!valueIterator->second->as$json(&value))
595 protocolErrors->pushString(String::format("Protocol Error: Argument '\%s' has wrong type. It should be '$json'.", name.utf8().data()));
601 return split("\n", $getterBody);
604 sub generateBackendDispatcher
607 my @mapEntries = map(" dispatchMap.add(${_}Cmd, &${backendClassName}::$_);", map ($backendMethodSignatures{$_}, @backendMethods));
608 my $mapEntries = join("\n", @mapEntries);
610 my $backendDispatcherBody = << "EOF";
611 void ${backendClassName}::dispatch(const String& message)
613 typedef void (${backendClassName}::*CallHandler)(long callId, InspectorObject* messageObject);
614 typedef HashMap<String, CallHandler> DispatchMap;
615 DEFINE_STATIC_LOCAL(DispatchMap, dispatchMap, );
618 if (dispatchMap.isEmpty()) {
622 RefPtr<InspectorValue> parsedMessage = InspectorValue::parseJSON(message);
623 if (!parsedMessage) {
624 reportProtocolError(callId, "Protocol Error: Invalid message format. Message should be in JSON format.");
628 RefPtr<InspectorObject> messageObject = parsedMessage->asObject();
629 if (!messageObject) {
630 reportProtocolError(callId, "Protocol Error: Invalid message format. The message should be a JSONified object.");
634 RefPtr<InspectorValue> commandValue = messageObject->get("command");
636 reportProtocolError(callId, "Protocol Error: Invalid message format. 'command' property wasn't found.");
641 if (!commandValue->asString(&command)) {
642 reportProtocolError(callId, "Protocol Error: Invalid message format. The type of 'command' property should be string.");
646 RefPtr<InspectorValue> callIdValue = messageObject->get("seq");
648 reportProtocolError(callId, "Protocol Error: Invalid message format. 'seq' property was not found in the request.");
652 if (!callIdValue->asNumber(&callId)) {
653 reportProtocolError(callId, "Protocol Error: Invalid message format. The type of 'seq' property should be number.");
657 HashMap<String, CallHandler>::iterator it = dispatchMap.find(command);
658 if (it == dispatchMap.end()) {
659 reportProtocolError(callId, makeString("Protocol Error: Invalid command was received. '", command, "' wasn't found."));
663 ((*this).*it->second)(callId, messageObject.get());
666 return split("\n", $backendDispatcherBody);
669 sub generateBackendMessageParser
671 my $messageParserBody = << "EOF";
672 bool ${backendClassName}::getCommandName(const String& message, String* result)
674 RefPtr<InspectorValue> value = InspectorValue::parseJSON(message);
678 RefPtr<InspectorObject> object = value->asObject();
682 RefPtr<InspectorValue> commandValue = object->get("command");
686 return commandValue->asString(result);
689 return split("\n", $messageParserBody);
692 sub collectBackendJSStubFunctions
694 my $interface = shift;
695 my @functions = grep(!$_->signature->extendedAttributes->{"event"}, @{$interface->functions});
696 my $domain = $interface->name;
698 foreach my $function (@functions) {
699 my $name = $function->signature->name;
700 my $argumentNames = join(",", map("\"" . $_->name . "\": \"" . typeTraits($_->type, "JSType") . "\"", grep($_->direction eq "in", @{$function->parameters})));
701 push(@backendJSStubs, " this._registerDelegate('{" .
703 "\"domain\": \"$domain\", " .
704 "\"command\": \"$name\", " .
705 "\"arguments\": {$argumentNames}" .
710 sub generateBackendStubJS
712 my $JSStubs = join("\n", @backendJSStubs);
713 my $inspectorBackendStubJS = << "EOF";
716 InspectorBackendStub = function()
718 this._lastCallbackId = 1;
719 this._pendingResponsesCount = 0;
720 this._callbacks = {};
721 this._domainDispatchers = {};
725 InspectorBackendStub.prototype = {
726 _wrap: function(callback)
728 var callbackId = this._lastCallbackId++;
729 ++this._pendingResponsesCount;
730 this._callbacks[callbackId] = callback || function() {};
734 _registerDelegate: function(commandInfo)
736 var commandObject = JSON.parse(commandInfo);
737 var agentName = commandObject.domain + "Agent";
738 if (!window[agentName])
739 window[agentName] = {};
740 window[agentName][commandObject.command] = this.sendMessageToBackend.bind(this, commandInfo);
743 sendMessageToBackend: function()
745 var args = Array.prototype.slice.call(arguments);
746 var request = JSON.parse(args.shift());
748 for (var key in request.arguments) {
749 if (args.length === 0) {
750 console.error("Protocol Error: Invalid number of arguments for '" + request.domain + "Agent." + request.command + "' call. It should have the next arguments '" + JSON.stringify(request.arguments) + "'.");
753 var value = args.shift();
754 if (request.arguments[key] && typeof value !== request.arguments[key]) {
755 console.error("Protocol Error: Invalid type of argument '" + key + "' for '" + request.domain + "Agent." + request.command + "' call. It should be '" + request.arguments[key] + "' but it is '" + typeof value + "'.");
758 request.arguments[key] = value;
762 if (args.length === 1) {
763 if (typeof args[0] !== "function" && typeof args[0] !== "undefined") {
764 console.error("Protocol Error: Optional callback argument for '" + request.domain + "Agent." + request.command + "' call should be a function but its type is '" + typeof args[0] + "'.");
769 request.seq = this._wrap(callback || function() {});
771 if (window.dumpInspectorProtocolMessages)
772 console.log("frontend: " + JSON.stringify(request));
774 var message = JSON.stringify(request);
775 InspectorFrontendHost.sendMessageToBackend(message);
778 registerDomainDispatcher: function(domain, dispatcher)
780 this._domainDispatchers[domain] = dispatcher;
783 dispatch: function(message)
785 if (window.dumpInspectorProtocolMessages)
786 console.log("backend: " + ((typeof message === "string") ? message : JSON.stringify(message)));
788 var messageObject = (typeof message === "string") ? JSON.parse(message) : message;
791 if (messageObject.data)
792 for (var key in messageObject.data)
793 arguments.push(messageObject.data[key]);
795 if ("seq" in messageObject) { // just a response for some request
796 if (messageObject.success)
797 this._callbacks[messageObject.seq].apply(null, arguments);
799 this.reportProtocolError(messageObject);
801 --this._pendingResponsesCount;
802 delete this._callbacks[messageObject.seq];
804 if (this._scripts && !this._pendingResponsesCount)
805 this.runAfterPendingDispatches();
810 if (messageObject.type === "event") {
811 if (!(messageObject.domain in this._domainDispatchers)) {
812 console.error("Protocol Error: the message is for non-existing domain '" + messageObject.domain + "'");
815 var dispatcher = this._domainDispatchers[messageObject.domain];
816 if (!(messageObject.event in dispatcher)) {
817 console.error("Protocol Error: Attempted to dispatch an unimplemented method '" + messageObject.domain + "." + messageObject.event + "'");
820 dispatcher[messageObject.event].apply(dispatcher, arguments);
824 reportProtocolError: function(messageObject)
826 console.error("Protocol Error: InspectorBackend request with seq = " + messageObject.seq + " failed.");
827 for (var i = 0; i < messageObject.errors.length; ++i)
828 console.error(" " + messageObject.errors[i]);
831 runAfterPendingDispatches: function(script)
837 this._scripts.push(script);
839 if (!this._pendingResponsesCount) {
840 var scripts = this._scripts;
842 for (var id = 0; id < scripts.length; ++id)
843 scripts[id].call(this);
848 InspectorBackend = new InspectorBackendStub();
851 return split("\n", $inspectorBackendStubJS);
856 my $className = shift;
858 my $constructor = shift;
859 my $constants = shift;
863 my $forwardHeaders = join("\n", sort(map("#include <" . typeTraits($_, "forwardHeader") . ">", grep(typeTraits($_, "forwardHeader"), keys %{$types}))));
864 my $forwardDeclarations = join("\n", sort(map("class " . typeTraits($_, "forward") . ";", grep(typeTraits($_, "forward"), keys %{$types}))));
865 my $constantDeclarations = join("\n", @{$constants});
866 my $methodsDeclarations = join("\n", @{$methods});
868 my $headerBody = << "EOF";
869 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
870 // Use of this source code is governed by a BSD-style license that can be
871 // found in the LICENSE file.
872 #ifndef ${className}_h
873 #define ${className}_h
877 namespace $namespace {
885 $constantDeclarations
891 } // namespace $namespace
892 #endif // !defined(${className}_h)
900 my $className = shift;
902 my $constants = shift;
905 my @sourceContent = split("\r", $licenseTemplate);
906 push(@sourceContent, "\n#include \"config.h\"");
907 push(@sourceContent, "#include \"$className.h\"");
908 push(@sourceContent, "#include <wtf/text/StringConcatenate.h>");
909 push(@sourceContent, "#include <wtf/text/CString.h>");
910 push(@sourceContent, "");
911 push(@sourceContent, "#if ENABLE(INSPECTOR)");
912 push(@sourceContent, "");
915 foreach my $type (keys %{$types}) {
916 $headers{"#include \"" . typeTraits($type, "header") . "\""} = 1 if !typeTraits($type, "header") eq "";
918 push(@sourceContent, sort keys %headers);
919 push(@sourceContent, "");
920 push(@sourceContent, "namespace $namespace {");
921 push(@sourceContent, "");
922 push(@sourceContent, join("\n", @{$constants}));
923 push(@sourceContent, "");
924 push(@sourceContent, @{$methods});
925 push(@sourceContent, "");
926 push(@sourceContent, "} // namespace $namespace");
927 push(@sourceContent, "");
928 push(@sourceContent, "#endif // ENABLE(INSPECTOR)");
929 push(@sourceContent, "");
930 return @sourceContent;
937 return $typeTransform{$type}->{$trait};
942 my $parameter = shift;
943 my $subtype = $parameter->extendedAttributes->{"type"};
945 my $pattern = typeTraits($parameter->type, "DocType");
946 return sprintf($pattern, "<$subtype>");
949 my $subtypeRef = $parameter->extendedAttributes->{"typeRef"};
951 my $pattern = typeTraits($parameter->type, "DocType");
952 return sprintf($pattern, "<<a href='#$subtypeRef'>" . $subtypeRef . "</a>>");
955 return "<" . typeTraits($parameter->type, "JSType") . ">";
960 my $parameter = shift;
962 my $result = $parameter->name . ": " . parameterDocType($parameter);
963 my $doc = $parameter->extendedAttributes->{"doc"};
965 $result = $result . " // " . $doc;
974 push(@backendMethodsImpl, generateBackendDispatcher());
975 push(@backendMethodsImpl, generateBackendReportProtocolError());
977 open(my $SOURCE, ">$outputDir/$frontendClassName.cpp") || die "Couldn't open file $outputDir/$frontendClassName.cpp";
978 print $SOURCE join("\n", generateSource($frontendClassName, \%frontendTypes, \@frontendConstantDefinitions, \@frontendMethodsImpl));
982 open(my $HEADER, ">$outputHeadersDir/$frontendClassName.h") || die "Couldn't open file $outputHeadersDir/$frontendClassName.h";
983 print $HEADER generateHeader($frontendClassName, \%frontendTypes, $frontendConstructor, \@frontendConstantDeclarations, \@frontendMethods, $frontendFooter);
987 # Make dispatcher methods private on the backend.
988 push(@backendConstantDeclarations, "");
989 push(@backendConstantDeclarations, "private:");
991 foreach my $type (keys %backendTypes) {
992 if (typeTraits($type, "JSONType")) {
993 push(@backendMethodsImpl, generateArgumentGetters($type));
997 push(@backendMethodsImpl, generateBackendMessageParser());
998 push(@backendMethodsImpl, "");
1000 push(@backendConstantDeclarations, "");
1002 open($SOURCE, ">$outputDir/$backendClassName.cpp") || die "Couldn't open file $outputDir/$backendClassName.cpp";
1003 print $SOURCE join("\n", generateSource($backendClassName, \%backendTypes, \@backendConstantDefinitions, \@backendMethodsImpl));
1007 open($HEADER, ">$outputHeadersDir/$backendClassName.h") || die "Couldn't open file $outputHeadersDir/$backendClassName.h";
1008 print $HEADER join("\n", generateHeader($backendClassName, \%backendTypes, $backendConstructor, \@backendConstantDeclarations, \@backendMethods, $backendFooter));
1012 open(my $JS_STUB, ">$outputDir/$backendJSStubName.js") || die "Couldn't open file $outputDir/$backendJSStubName.js";
1013 print $JS_STUB join("\n", generateBackendStubJS());
1017 open(my $DOCS, ">$outputDir/WebInspectorProtocol.html") || die "Couldn't open file $outputDir/WebInspectorProtocol.html";
1018 print $DOCS "<ol class='toc' style='list-style: none; padding: 0'>";
1019 print $DOCS join("\n", @documentationToc);
1020 print $DOCS "</ol>";
1021 print $DOCS join("\n", @documentationLines);