Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_protocol_constants_generator.py
blob7e53ac1c9a13f86d0d6d878c5ca8f600c45d511c
1 #!/usr/bin/python
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.
6 import sys
7 import string
8 import json
10 package = sys.argv[1]
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.
25 // Generated by
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
30 #include <string>
32 namespace $package {
33 namespace devtools {
35 extern const char kProtocolVersion[];
37 bool IsSupportedProtocolVersion(const std::string& version);
39 extern const char kResult[];
40 $contents
42 } // devtools
43 } // $package
45 #endif // ${PACKAGE}_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
46 """)
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.
54 // Generated by
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"
64 namespace $package {
65 namespace devtools {
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);
72 int major, minor;
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";
79 $contents
81 } // devtools
82 } // $package
83 """)
85 def Capitalize(s):
86 return s[:1].capitalize() + s[1:]
88 references = []
90 def CreateNamespace(domain_name, data, keys, prefixes, name = None):
91 result = {}
92 if name:
93 result["kName"] = name
94 for i, key in enumerate(keys):
95 if key in data:
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
104 reference = ""
105 if "$ref" in parameter:
106 reference = parameter["$ref"]
107 if "items" in parameter and "$ref" in parameter["items"]:
108 reference = parameter["items"]["$ref"]
109 if reference:
110 if not "." in reference:
111 reference = domain_name + "." + reference
112 references.append(reference)
113 return result
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))
123 body = ""
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()))
128 return body
130 def FormatNamespace(title, tree, indent, format_string):
131 if (not tree):
132 return ""
133 body = '\n' + indent + "namespace " + title + " {\n"
134 body += FormatContents(tree, indent + " ", format_string)
135 body += indent + "} // " + title + "\n"
136 return body
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,
142 "package": package,
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,
152 "package": package
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"]
166 namespace_tree = {}
168 for domain in domains:
169 domain_value = {}
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"])
184 if domain_value:
185 namespace_tree[domain_namespace_name] = domain_value
187 while (references):
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):
196 try:
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"])
201 except IndexError:
202 sys.stderr.write("Failed to resolve type [{0}].\n".format(reference))
203 sys.exit(1)
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)