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).
27 extern const char kResult[];
33 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
36 template_cc = string.Template("""\
37 // Copyright 2013 The Chromium Authors. All rights reserved.
38 // Use of this source code is governed by a BSD-style license that can be
39 // found in the LICENSE file.
41 // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
43 // content/browser/devtools/devtools_protocol_constants_generator.py from
44 // third_party/WebKit/Source/devtools/protocol.json and
45 // content/browser/devtools/browser_protocol.json).
47 #include "content/browser/devtools/devtools_protocol_constants.h"
52 const char kResult[] = "result";
60 return s[:1].capitalize() + s[1:]
64 def CreateNamespace(domain_name, data, keys, prefixes, name = None):
67 result["kName"] = name
68 for i, key in enumerate(keys):
70 for parameter in data[key]:
71 parameter_name = parameter["name"];
72 result[prefixes[i] + Capitalize(parameter_name)] = parameter_name
73 if "enum" in parameter:
74 enum_name = Capitalize(parameter_name)
75 result[enum_name] = {}
76 for enum in parameter["enum"]:
77 result[enum_name]["kEnum" + Capitalize(enum)] = enum
79 if "$ref" in parameter:
80 reference = parameter["$ref"]
81 if "items" in parameter and "$ref" in parameter["items"]:
82 reference = parameter["items"]["$ref"]
84 if not "." in reference:
85 reference = domain_name + "." + reference
86 references.append(reference)
89 def IsHandledInBrowser(item):
90 return "handlers" in item and "browser" in item["handlers"]
92 def FormatContents(tree, indent, format_string):
93 outer = dict((key, value) for key, value in tree.iteritems()
94 if not isinstance(value, dict))
95 inner = dict((key, value) for key, value in tree.iteritems()
96 if isinstance(value, dict))
98 body += "".join(indent + format_string.format(key, value)
99 for (key, value) in sorted(outer.items()))
100 body += "".join(FormatNamespace(key, value, indent, format_string)
101 for (key, value) in sorted(inner.items()))
104 def FormatNamespace(title, tree, indent, format_string):
107 body = '\n' + indent + "namespace " + title + " {\n"
108 body += FormatContents(tree, indent + " ", format_string)
109 body += indent + "} // " + title + "\n"
112 def CreateHeader(tree, output_file):
113 contents = FormatContents(tree, "", "extern const char {0}[];\n")
114 output_file.write(template_h.substitute({"contents": contents}))
116 def CreateBody(tree, output_file):
117 contents = FormatContents(tree, "", "const char {0}[] = \"{1}\";\n")
118 output_file.write(template_cc.substitute({"contents": contents}))
120 blink_protocol_data = open(sys.argv[1]).read()
121 browser_protocol_data = open(sys.argv[2]).read()
123 blink_protocol = json.loads(blink_protocol_data)
124 browser_protocol = json.loads(browser_protocol_data)
126 domains = blink_protocol["domains"] + browser_protocol["domains"]
130 for domain in domains:
132 domain_namespace_name = Capitalize(domain["domain"])
133 if "commands" in domain:
134 for command in domain["commands"]:
135 if (IsHandledInBrowser(command)):
136 domain_value[command["name"]] = CreateNamespace(domain["domain"],
137 command, ["parameters", "returns"], ["kParam", "kResponse"],
138 domain_namespace_name + "." + command["name"])
140 if "events" in domain:
141 for event in domain["events"]:
142 if IsHandledInBrowser(event):
143 domain_value[event["name"]] = CreateNamespace(domain["domain"],
144 event, ["parameters"], ["kParam"],
145 domain_namespace_name + "." + event["name"])
147 namespace_tree[domain_namespace_name] = domain_value
150 reference = references.pop();
151 path = reference.split(".");
152 parent_namespace = namespace_tree;
153 for path_segment in path[0:-1]:
154 if path_segment not in parent_namespace:
155 parent_namespace[path_segment] = {}
156 parent_namespace = parent_namespace[path_segment]
157 if (path[-1] not in parent_namespace):
159 domain = [d for d in domains if d["domain"] == path[0]][0]
160 ref_type = [t for t in domain["types"] if t["id"] == path[1]][0]
161 parent_namespace[ref_type["id"]] = CreateNamespace(path[0],
162 ref_type, ["properties"], ["kParam"])
164 sys.stderr.write("Failed to resolve type [{0}].\n".format(reference))
167 for (namespace_name, namespace) in namespace_tree.items():
168 namespace["kName"] = namespace_name
170 with open(sys.argv[3], "w") as f:
171 CreateBody(namespace_tree, f)
173 with open(sys.argv[4], "w") as f:
174 CreateHeader(namespace_tree, f)