2 # Copyright 2014 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.
11 output_cc_path
= sys
.argv
[2]
12 output_h_path
= sys
.argv
[3]
13 blink_protocol_path
= sys
.argv
[4]
14 browser_protocol_path
= sys
.argv
[5] if len(sys
.argv
) > 5 else None
16 template_h
= string
.Template("""\
17 // Copyright 2013 The Chromium Authors. All rights reserved.
18 // Use of this source code is governed by a BSD-style license that can be
19 // found in the LICENSE file.
21 #ifndef ${PACKAGE}_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
22 #define ${PACKAGE}_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
24 // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
26 // chrome/browser/devtools/devtools_protocol_constants_generator.py from
27 // third_party/WebKit/Source/devtools/protocol.json and
28 // content/browser/devtools/browser_protocol.json
35 extern const char kProtocolVersion[];
37 bool IsSupportedProtocolVersion(const std::string& version);
39 extern const char kResult[];
45 #endif // ${PACKAGE}_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
48 template_cc
= string
.Template("""\
49 // Copyright 2013 The Chromium Authors. All rights reserved.
50 // Use of this source code is governed by a BSD-style license that can be
51 // found in the LICENSE file.
53 // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
55 // chrome/browser/devtools/devtools_protocol_constants_generator.py from
56 // third_party/WebKit/Source/devtools/protocol.json and
57 // content/browser/devtools/browser_protocol.json
59 #include "base/strings/string_number_conversions.h"
60 #include "base/strings/string_split.h"
61 #include "base/strings/string_util.h"
62 #include "$package/browser/devtools/devtools_protocol_constants.h"
67 const char kProtocolVersion[] = "$major.$minor";
69 bool IsSupportedProtocolVersion(const std::string& version) {
70 std::vector<base::StringPiece> tokens = base::SplitStringPiece(
71 version, ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
73 return tokens.size() == 2 &&
74 base::StringToInt(tokens[0], &major) && major == $major &&
75 base::StringToInt(tokens[1], &minor) && minor <= $minor;
78 const char kResult[] = "result";
86 return s
[:1].capitalize() + s
[1:]
90 def CreateNamespace(domain_name
, data
, keys
, prefixes
, name
= None):
93 result
["kName"] = name
94 for i
, key
in enumerate(keys
):
96 for parameter
in data
[key
]:
97 parameter_name
= parameter
["name"];
98 result
[prefixes
[i
] + Capitalize(parameter_name
)] = parameter_name
99 if "enum" in parameter
:
100 enum_name
= Capitalize(parameter_name
)
101 result
[enum_name
] = {}
102 for enum
in parameter
["enum"]:
103 result
[enum_name
]["kEnum" + Capitalize(enum
)] = enum
105 if "$ref" in parameter
:
106 reference
= parameter
["$ref"]
107 if "items" in parameter
and "$ref" in parameter
["items"]:
108 reference
= parameter
["items"]["$ref"]
110 if not "." in reference
:
111 reference
= domain_name
+ "." + reference
112 references
.append(reference
)
115 def IsHandledInBrowser(item
):
116 return "handlers" in item
and "browser" in item
["handlers"]
118 def FormatContents(tree
, indent
, format_string
):
119 outer
= dict((key
, value
) for key
, value
in tree
.iteritems()
120 if not isinstance(value
, dict))
121 inner
= dict((key
, value
) for key
, value
in tree
.iteritems()
122 if isinstance(value
, dict))
124 body
+= "".join(indent
+ format_string
.format(key
, value
)
125 for (key
, value
) in sorted(outer
.items()))
126 body
+= "".join(FormatNamespace(key
, value
, indent
, format_string
)
127 for (key
, value
) in sorted(inner
.items()))
130 def FormatNamespace(title
, tree
, indent
, format_string
):
133 body
= '\n' + indent
+ "namespace " + title
+ " {\n"
134 body
+= FormatContents(tree
, indent
+ " ", format_string
)
135 body
+= indent
+ "} // " + title
+ "\n"
138 def CreateHeader(tree
, output_file
):
139 contents
= FormatContents(tree
, "", "extern const char {0}[];\n")
140 output_file
.write(template_h
.substitute({
141 "contents": contents
,
143 "PACKAGE": package
.upper()
146 def CreateBody(tree
, version
, output_file
):
147 contents
= FormatContents(tree
, "", "const char {0}[] = \"{1}\";\n")
148 output_file
.write(template_cc
.substitute({
149 "major": version
["major"],
150 "minor": version
["minor"],
151 "contents": contents
,
155 blink_protocol_data
= open(blink_protocol_path
).read()
156 blink_protocol
= json
.loads(blink_protocol_data
)
157 blink_version
= blink_protocol
["version"]
159 domains
= blink_protocol
["domains"]
161 if browser_protocol_path
:
162 browser_protocol_data
= open(browser_protocol_path
).read()
163 browser_protocol
= json
.loads(browser_protocol_data
)
164 domains
= domains
+ browser_protocol
["domains"]
168 for domain
in domains
:
170 domain_namespace_name
= Capitalize(domain
["domain"])
171 if "commands" in domain
:
172 for command
in domain
["commands"]:
173 if (IsHandledInBrowser(command
)):
174 domain_value
[command
["name"]] = CreateNamespace(domain
["domain"],
175 command
, ["parameters", "returns"], ["kParam", "kResponse"],
176 domain_namespace_name
+ "." + command
["name"])
178 if "events" in domain
:
179 for event
in domain
["events"]:
180 if IsHandledInBrowser(event
):
181 domain_value
[event
["name"]] = CreateNamespace(domain
["domain"],
182 event
, ["parameters"], ["kParam"],
183 domain_namespace_name
+ "." + event
["name"])
185 namespace_tree
[domain_namespace_name
] = domain_value
188 reference
= references
.pop();
189 path
= reference
.split(".");
190 parent_namespace
= namespace_tree
;
191 for path_segment
in path
[0:-1]:
192 if path_segment
not in parent_namespace
:
193 parent_namespace
[path_segment
] = {}
194 parent_namespace
= parent_namespace
[path_segment
]
195 if (path
[-1] not in parent_namespace
):
197 domain
= [d
for d
in domains
if d
["domain"] == path
[0]][0]
198 ref_type
= [t
for t
in domain
["types"] if t
["id"] == path
[1]][0]
199 parent_namespace
[ref_type
["id"]] = CreateNamespace(path
[0],
200 ref_type
, ["properties"], ["kParam"])
202 sys
.stderr
.write("Failed to resolve type [{0}].\n".format(reference
))
205 for (namespace_name
, namespace
) in namespace_tree
.items():
206 namespace
["kName"] = namespace_name
208 with
open(output_cc_path
, "w") as f
:
209 CreateBody(namespace_tree
, blink_version
, f
)
211 with
open(output_h_path
, "w") as f
:
212 CreateHeader(namespace_tree
, f
)