diff options
Diffstat (limited to 'WebCore/inspector/CodeGeneratorInspector.pm')
-rw-r--r-- | WebCore/inspector/CodeGeneratorInspector.pm | 132 |
1 files changed, 104 insertions, 28 deletions
diff --git a/WebCore/inspector/CodeGeneratorInspector.pm b/WebCore/inspector/CodeGeneratorInspector.pm index 9d0af53..cd9052a 100644 --- a/WebCore/inspector/CodeGeneratorInspector.pm +++ b/WebCore/inspector/CodeGeneratorInspector.pm @@ -47,30 +47,27 @@ $typeTransform{"PassRefPtr"} = { }; $typeTransform{"Object"} = { "param" => "PassRefPtr<InspectorObject>", - "retVal" => "PassRefPtr<InspectorObject>", "variable" => "RefPtr<InspectorObject>", "defaultValue" => "InspectorObject::create()", "forward" => "InspectorObject", "header" => "InspectorValues.h", - "accessorSuffix" => "" + "accessorSuffix" => "Object" }; $typeTransform{"Array"} = { "param" => "PassRefPtr<InspectorArray>", - "retVal" => "PassRefPtr<InspectorArray>", "variable" => "RefPtr<InspectorArray>", "defaultValue" => "InspectorArray::create()", "forward" => "InspectorArray", "header" => "InspectorValues.h", - "accessorSuffix" => "" + "accessorSuffix" => "Array" }; $typeTransform{"Value"} = { "param" => "PassRefPtr<InspectorValue>", - "retVal" => "PassRefPtr<InspectorValue>", "variable" => "RefPtr<InspectorValue>", "defaultValue" => "InspectorValue::null()", "forward" => "InspectorValue", "header" => "InspectorValues.h", - "accessorSuffix" => "" + "accessorSuffix" => "Value" }; $typeTransform{"String"} = { "param" => "const String&", @@ -120,7 +117,6 @@ $typeTransform{"boolean"} = { "accessorSuffix" => "Bool" }; $typeTransform{"void"} = { - "retVal" => "void", "forward" => "", "header" => "" }; @@ -142,17 +138,23 @@ my $verbose; my $namespace; my $backendClassName; +my $backendJSStubName; my %backendTypes; my %backendMethods; my @backendMethodsImpl; my $backendConstructor; +my @backendConstantDeclarations; +my @backendConstantDefinitions; my $backendFooter; +my @backendStubJS; my $frontendClassName; my %frontendTypes; my %frontendMethods; my @frontendMethodsImpl; my $frontendConstructor; +my @frontendConstantDeclarations; +my @frontendConstantDefinitions; my $frontendFooter; my $callId = new domSignature(); # it is just structure for describing parameters from IDLStructure.pm. @@ -204,11 +206,12 @@ sub GenerateInterface $frontendTypes{"PassRefPtr"} = 1; $backendClassName = $className . "BackendDispatcher"; + $backendJSStubName = $className . "BackendStub"; my @backendHead; push(@backendHead, " ${backendClassName}(InspectorController* inspectorController) : m_inspectorController(inspectorController) { }"); push(@backendHead, " void reportProtocolError(const long callId, const String& method, const String& errorText) const;"); push(@backendHead, " void dispatch(const String& message);"); - push(@backendHead, "private:"); + push(@backendHead, " static bool getCommandName(const String& message, String* result);"); $backendConstructor = join("\n", @backendHead); $backendFooter = " InspectorController* m_inspectorController;"; $backendTypes{"Controller"} = 1; @@ -216,8 +219,13 @@ sub GenerateInterface $backendTypes{"PassRefPtr"} = 1; $backendTypes{"Array"} = 1; - generateBackendPrivateFunctions(); + push(@backendMethodsImpl, generateBackendPrivateFunctions()); + push(@backendMethodsImpl, generateBackendMessageParser()); generateFunctions($interface); + + # Make dispatcher methods private on the backend. + push(@backendConstantDeclarations, ""); + push(@backendConstantDeclarations, "private:"); } sub generateFunctions @@ -225,23 +233,25 @@ sub generateFunctions my $interface = shift; foreach my $function (@{$interface->functions}) { - generateFrontendFunction($function); - generateBackendFunction($function); + if ($function->signature->extendedAttributes->{"notify"}) { + generateFrontendFunction($function); + } else { + generateBackendFunction($function); + } } push(@backendMethodsImpl, generateBackendDispatcher()); push(@backendMethodsImpl, generateBackendReportProtocolError()); + + @backendStubJS = generateBackendStubJS($interface); } sub generateFrontendFunction { my $function = shift; - my $notify = $function->signature->extendedAttributes->{"notify"}; - return if !$notify; - my $functionName = $notify ? $function->signature->name : "did" . ucfirst($function->signature->name); + my $functionName = $function->signature->name; my @argsFiltered = grep($_->direction eq "out", @{$function->parameters}); # just keep only out parameters for frontend interface. - unshift(@argsFiltered, $callId) if !$notify; # Add callId as the first argument for all frontend did* methods. map($frontendTypes{$_->type} = 1, @argsFiltered); # register required types. my $arguments = join(", ", map($typeTransform{$_->type}->{"param"} . " " . $_->name, @argsFiltered)); # prepare arguments for function signature. my @pushArguments = map(" arguments->push" . $typeTransform{$_->type}->{"accessorSuffix"} . "(" . $_->name . ");", @argsFiltered); @@ -254,7 +264,7 @@ sub generateFrontendFunction push(@function, "void ${frontendClassName}::${functionName}(${arguments})"); push(@function, "{"); push(@function, " RefPtr<InspectorArray> arguments = InspectorArray::create();"); - push(@function, " arguments->pushString(\"" . ($notify ? $functionName : "processResponse") . "\");"); + push(@function, " arguments->pushString(\"$functionName\");"); push(@function, @pushArguments); push(@function, " m_inspectorClient->sendMessageToFrontend(arguments->toJSONString());"); @@ -277,16 +287,18 @@ static String formatWrongArgumentTypeMessage(unsigned position, const char* name return String::format("Failed to convert parameter %d (%s) to %s", position, name, expectedType); } EOF - push(@backendMethodsImpl, $privateFunctions); + return split("\n", $privateFunctions); } sub generateBackendFunction { my $function = shift; - return if $function->signature->extendedAttributes->{"notify"}; my $functionName = $function->signature->name; + push(@backendConstantDeclarations, " static const char* ${functionName}Cmd;"); + push(@backendConstantDefinitions, "const char* ${backendClassName}::${functionName}Cmd = \"${functionName}\";"); + map($backendTypes{$_->type} = 1, @{$function->parameters}); # register required types my @inArgs = grep($_->direction eq "in", @{$function->parameters}); my @outArgs = grep($_->direction eq "out", @{$function->parameters}); @@ -298,7 +310,6 @@ sub generateBackendFunction my @function; push(@function, "void ${backendClassName}::${functionName}(PassRefPtr<InspectorArray> args)"); push(@function, "{"); - push(@function, " DEFINE_STATIC_LOCAL(String, backendFunctionName, (\"$functionName\"));"); push(@function, " long callId = 0;"); push(@function, ""); @@ -306,7 +317,7 @@ sub generateBackendFunction my $expectedParametersCountWithMethodName = scalar(@inArgs) + 1; push(@function, " if (args->length() != $expectedParametersCountWithMethodName) {"); push(@function, " ASSERT_NOT_REACHED();"); - push(@function, " reportProtocolError(callId, backendFunctionName, formatWrongArgumentsCountMessage(args->length() - 1, $expectedParametersCount));"); + push(@function, " reportProtocolError(callId, ${functionName}Cmd, formatWrongArgumentsCountMessage(args->length() - 1, $expectedParametersCount));"); push(@function, " return;"); push(@function, " }"); push(@function, ""); @@ -314,11 +325,11 @@ sub generateBackendFunction my $i = 1; # zero element is the method name. foreach my $parameter (@inArgs) { my $type = $parameter->type; - my $argumentType = $typeTransform{$type}->{$typeTransform{$type}->{"retVal"} ? "retVal" : "variable"}; + my $argumentType = $typeTransform{$type}->{"variable"}; push(@function, " $argumentType " . $parameter->name . ";") if !($parameter->name eq "callId"); push(@function, " if (!args->get($i)->as" . $typeTransform{$type}->{"accessorSuffix"} . "(&" . $parameter->name . ")) {"); push(@function, " ASSERT_NOT_REACHED();"); - push(@function, " reportProtocolError(callId, backendFunctionName, formatWrongArgumentTypeMessage($i, \"" . $parameter->name . "\", \"$type\"));"); + push(@function, " reportProtocolError(callId, ${functionName}Cmd, formatWrongArgumentTypeMessage($i, \"" . $parameter->name . "\", \"$type\"));"); push(@function, " return;"); push(@function, " }"); push(@function, ""); @@ -329,7 +340,7 @@ sub generateBackendFunction my $handlerAccessor = $typeTransform{$handler}->{"handlerAccessor"}; $backendTypes{$handler} = 1; push(@function, " if (!$handlerAccessor) {"); - push(@function, " reportProtocolError(callId, backendFunctionName, \"Error: $handler handler is not available.\");"); + push(@function, " reportProtocolError(callId, ${functionName}Cmd, \"Error: $handler handler is not available.\");"); push(@function, " return;"); push(@function, " }"); push(@function, ""); @@ -383,7 +394,7 @@ sub generateBackendDispatcher { my @body; my @methods = map($backendMethods{$_}, keys %backendMethods); - my @mapEntries = map(" dispatchMap.add(\"$_\", &${backendClassName}::$_);", @methods); + my @mapEntries = map(" dispatchMap.add(${_}Cmd, &${backendClassName}::$_);", @methods); my $mapEntries = join("\n", @mapEntries); my $backendDispatcherBody = << "EOF"; @@ -436,16 +447,72 @@ EOF return split("\n", $backendDispatcherBody); } +sub generateBackendMessageParser +{ + my $messageParserBody = << "EOF"; +bool ${backendClassName}::getCommandName(const String& message, String* result) +{ + RefPtr<InspectorValue> value = InspectorValue::parseJSON(message); + if (!value) + return false; + RefPtr<InspectorArray> array = value->asArray(); + if (!array) + return false; + + if (!array->length()) + return false; + return array->get(0)->asString(result); +} +EOF + return split("\n", $messageParserBody); +} + +sub generateBackendStubJS +{ + my $interface = shift; + my @backendFunctions = grep(!$_->signature->extendedAttributes->{"notify"}, @{$interface->functions}); + my @JSStubs = map(" this._registerDelegate(\"" . $_->signature->name . "\");", @backendFunctions); + + my $JSStubs = join("\n", @JSStubs); + my $inspectorBackendStubJS = << "EOF"; +$licenseTemplate + +WebInspector.InspectorBackendStub = function() +{ +$JSStubs +} + +WebInspector.InspectorBackendStub.prototype = { + _registerDelegate: function(methodName) + { + this[methodName] = this.sendMessageToBackend.bind(this, methodName); + }, + + sendMessageToBackend: function() + { + var message = JSON.stringify(Array.prototype.slice.call(arguments)); + InspectorFrontendHost.sendMessageToBackend(message); + } +} + +InspectorBackend = new WebInspector.InspectorBackendStub(); + +EOF + return split("\n", $inspectorBackendStubJS); +} + sub generateHeader { my $className = shift; my $types = shift; my $constructor = shift; + my $constants = shift; my $methods = shift; my $footer = shift; my $forwardHeaders = join("\n", sort(map("#include <" . $typeTransform{$_}->{"forwardHeader"} . ">", grep($typeTransform{$_}->{"forwardHeader"}, keys %{$types})))); my $forwardDeclarations = join("\n", sort(map("class " . $typeTransform{$_}->{"forward"} . ";", grep($typeTransform{$_}->{"forward"}, keys %{$types})))); + my $constantDeclarations = join("\n", @{$constants}); my $methodsDeclarations = join("\n", keys %{$methods}); my $headerBody = << "EOF"; @@ -465,6 +532,7 @@ class $className { public: $constructor +$constantDeclarations $methodsDeclarations private: @@ -482,6 +550,7 @@ sub generateSource { my $className = shift; my $types = shift; + my $constants = shift; my $methods = shift; my @sourceContent = split("\r", $licenseTemplate); @@ -499,6 +568,8 @@ sub generateSource push(@sourceContent, ""); push(@sourceContent, "namespace $namespace {"); push(@sourceContent, ""); + push (@sourceContent, join("\n", @{$constants})); + push(@sourceContent, ""); push(@sourceContent, @{$methods}); push(@sourceContent, ""); push(@sourceContent, "} // namespace $namespace"); @@ -513,24 +584,29 @@ sub finish my $object = shift; open(my $SOURCE, ">$outputDir/$frontendClassName.cpp") || die "Couldn't open file $outputDir/$frontendClassName.cpp"; - print $SOURCE join("\n", generateSource($frontendClassName, \%frontendTypes, \@frontendMethodsImpl)); + print $SOURCE join("\n", generateSource($frontendClassName, \%frontendTypes, \@frontendConstantDefinitions, \@frontendMethodsImpl)); close($SOURCE); undef($SOURCE); open(my $HEADER, ">$outputHeadersDir/$frontendClassName.h") || die "Couldn't open file $outputHeadersDir/$frontendClassName.h"; - print $HEADER generateHeader($frontendClassName, \%frontendTypes, $frontendConstructor, \%frontendMethods, $frontendFooter); + print $HEADER generateHeader($frontendClassName, \%frontendTypes, $frontendConstructor, \@frontendConstantDeclarations, \%frontendMethods, $frontendFooter); close($HEADER); undef($HEADER); open($SOURCE, ">$outputDir/$backendClassName.cpp") || die "Couldn't open file $outputDir/$backendClassName.cpp"; - print $SOURCE join("\n", generateSource($backendClassName, \%backendTypes, \@backendMethodsImpl)); + print $SOURCE join("\n", generateSource($backendClassName, \%backendTypes, \@backendConstantDefinitions, \@backendMethodsImpl)); close($SOURCE); undef($SOURCE); open($HEADER, ">$outputHeadersDir/$backendClassName.h") || die "Couldn't open file $outputHeadersDir/$backendClassName.h"; - print $HEADER join("\n", generateHeader($backendClassName, \%backendTypes, $backendConstructor, \%backendMethods, $backendFooter)); + print $HEADER join("\n", generateHeader($backendClassName, \%backendTypes, $backendConstructor, \@backendConstantDeclarations, \%backendMethods, $backendFooter)); close($HEADER); undef($HEADER); + + open(my $JS_STUB, ">$outputDir/$backendJSStubName.js") || die "Couldn't open file $outputDir/$backendJSStubName.js"; + print $JS_STUB join("\n", @backendStubJS); + close($JS_STUB); + undef($JS_STUB); } 1; |