2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
10 template_h
= string
.Template("""\
11 // Copyright 2013 The Chromium Authors. All rights reserved.
12 // Use of this source code is governed by a BSD-style license that can be
13 // found in the LICENSE file.
15 #ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
16 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
18 // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
20 // content/browser/devtools/devtools_protocol_constants_generator.py from
21 // third_party/WebKit/Source/devtools/protocol.json and
22 // content/browser/devtools/browser_protocol.json).
29 extern const char kProtocolVersion[];
31 bool IsSupportedProtocolVersion(const std::string& version);
33 extern const char kResult[];
39 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
42 template_cc
= string
.Template("""\
43 // Copyright 2013 The Chromium Authors. All rights reserved.
44 // Use of this source code is governed by a BSD-style license that can be
45 // found in the LICENSE file.
47 // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
49 // content/browser/devtools/devtools_protocol_constants_generator.py from
50 // third_party/WebKit/Source/devtools/protocol.json and
51 // content/browser/devtools/browser_protocol.json).
53 #include "base/strings/string_number_conversions.h"
54 #include "base/strings/string_util.h"
55 #include "content/browser/devtools/devtools_protocol_constants.h"
60 const char kProtocolVersion[] = "$major.$minor";
62 bool IsSupportedProtocolVersion(const std::string& version) {
63 std::vector<std::string> tokens;
64 Tokenize(version, ".", &tokens);
66 return tokens.size() == 2 &&
67 base::StringToInt(tokens[0], &major) && major == $major &&
68 base::StringToInt(tokens[1], &minor) && minor <= $minor;
71 const char kResult[] = "result";
79 return s
[:1].capitalize() + s
[1:]
83 def CreateNamespace(domain_name
, data
, keys
, prefixes
, name
= None):
86 result
["kName"] = name
87 for i
, key
in enumerate(keys
):
89 for parameter
in data
[key
]:
90 parameter_name
= parameter
["name"];
91 result
[prefixes
[i
] + Capitalize(parameter_name
)] = parameter_name
92 if "enum" in parameter
:
93 enum_name
= Capitalize(parameter_name
)
94 result
[enum_name
] = {}
95 for enum
in parameter
["enum"]:
96 result
[enum_name
]["kEnum" + Capitalize(enum
)] = enum
98 if "$ref" in parameter
:
99 reference
= parameter
["$ref"]
100 if "items" in parameter
and "$ref" in parameter
["items"]:
101 reference
= parameter
["items"]["$ref"]
103 if not "." in reference
:
104 reference
= domain_name
+ "." + reference
105 references
.append(reference
)
108 def IsHandledInBrowser(item
):
109 return "handlers" in item
and "browser" in item
["handlers"]
111 def FormatContents(tree
, indent
, format_string
):
112 outer
= dict((key
, value
) for key
, value
in tree
.iteritems()
113 if not isinstance(value
, dict))
114 inner
= dict((key
, value
) for key
, value
in tree
.iteritems()
115 if isinstance(value
, dict))
117 body
+= "".join(indent
+ format_string
.format(key
, value
)
118 for (key
, value
) in sorted(outer
.items()))
119 body
+= "".join(FormatNamespace(key
, value
, indent
, format_string
)
120 for (key
, value
) in sorted(inner
.items()))
123 def FormatNamespace(title
, tree
, indent
, format_string
):
126 body
= '\n' + indent
+ "namespace " + title
+ " {\n"
127 body
+= FormatContents(tree
, indent
+ " ", format_string
)
128 body
+= indent
+ "} // " + title
+ "\n"
131 def CreateHeader(tree
, output_file
):
132 contents
= FormatContents(tree
, "", "extern const char {0}[];\n")
133 output_file
.write(template_h
.substitute({"contents": contents
}))
135 def CreateBody(tree
, version
, output_file
):
136 contents
= FormatContents(tree
, "", "const char {0}[] = \"{1}\";\n")
137 output_file
.write(template_cc
.substitute({
138 "major": version
["major"],
139 "minor": version
["minor"],
143 blink_protocol_data
= open(sys
.argv
[1]).read()
144 browser_protocol_data
= open(sys
.argv
[2]).read()
146 blink_protocol
= json
.loads(blink_protocol_data
)
147 browser_protocol
= json
.loads(browser_protocol_data
)
149 blink_version
= blink_protocol
["version"]
151 domains
= blink_protocol
["domains"] + browser_protocol
["domains"]
155 for domain
in domains
:
157 domain_namespace_name
= Capitalize(domain
["domain"])
158 if "commands" in domain
:
159 for command
in domain
["commands"]:
160 if (IsHandledInBrowser(command
)):
161 domain_value
[command
["name"]] = CreateNamespace(domain
["domain"],
162 command
, ["parameters", "returns"], ["kParam", "kResponse"],
163 domain_namespace_name
+ "." + command
["name"])
165 if "events" in domain
:
166 for event
in domain
["events"]:
167 if IsHandledInBrowser(event
):
168 domain_value
[event
["name"]] = CreateNamespace(domain
["domain"],
169 event
, ["parameters"], ["kParam"],
170 domain_namespace_name
+ "." + event
["name"])
172 namespace_tree
[domain_namespace_name
] = domain_value
175 reference
= references
.pop();
176 path
= reference
.split(".");
177 parent_namespace
= namespace_tree
;
178 for path_segment
in path
[0:-1]:
179 if path_segment
not in parent_namespace
:
180 parent_namespace
[path_segment
] = {}
181 parent_namespace
= parent_namespace
[path_segment
]
182 if (path
[-1] not in parent_namespace
):
184 domain
= [d
for d
in domains
if d
["domain"] == path
[0]][0]
185 ref_type
= [t
for t
in domain
["types"] if t
["id"] == path
[1]][0]
186 parent_namespace
[ref_type
["id"]] = CreateNamespace(path
[0],
187 ref_type
, ["properties"], ["kParam"])
189 sys
.stderr
.write("Failed to resolve type [{0}].\n".format(reference
))
192 for (namespace_name
, namespace
) in namespace_tree
.items():
193 namespace
["kName"] = namespace_name
195 with
open(sys
.argv
[3], "w") as f
:
196 CreateBody(namespace_tree
, blink_version
, f
)
198 with
open(sys
.argv
[4], "w") as f
:
199 CreateHeader(namespace_tree
, f
)