OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import code | 5 import code |
6 import cpp_util | 6 import cpp_util |
7 from model import Platforms | 7 from model import Platforms |
8 from schema_util import CapitalizeFirstLetter | 8 from schema_util import CapitalizeFirstLetter |
9 from schema_util import JsFunctionNameToClassName | 9 from schema_util import JsFunctionNameToClassName |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... |
24 # Some schemas actually have properties called "description", so only | 24 # Some schemas actually have properties called "description", so only |
25 # remove descriptions that have string values. | 25 # remove descriptions that have string values. |
26 if key == 'description' and isinstance(value, basestring): | 26 if key == 'description' and isinstance(value, basestring): |
27 continue | 27 continue |
28 result[key] = _RemoveDescriptions(value) | 28 result[key] = _RemoveDescriptions(value) |
29 return result | 29 return result |
30 if isinstance(node, list): | 30 if isinstance(node, list): |
31 return [_RemoveDescriptions(v) for v in node] | 31 return [_RemoveDescriptions(v) for v in node] |
32 return node | 32 return node |
33 | 33 |
34 class SchemaBundleGenerator(object): | 34 class CppBundleGenerator(object): |
35 """This class contains methods to generate code based on multiple schemas. | 35 """This class contains methods to generate code based on multiple schemas. |
36 """ | 36 """ |
37 | 37 |
38 def __init__(self, root, model, api_defs, cpp_type_generator): | 38 def __init__(self, root, model, api_defs, cpp_type_generator, cpp_namespace): |
39 self._root = root; | 39 self._root = root; |
40 self._model = model | 40 self._model = model |
41 self._api_defs = api_defs | 41 self._api_defs = api_defs |
42 self._cpp_type_generator = cpp_type_generator | 42 self._cpp_type_generator = cpp_type_generator |
| 43 self._cpp_namespace = cpp_namespace |
| 44 |
| 45 self.api_h_generator = _APIHGenerator(self) |
| 46 self.schemas_cc_generator = _SchemasCCGenerator(self) |
| 47 self.schemas_h_generator = _SchemasHGenerator(self) |
43 | 48 |
44 def GenerateHeader(self, file_base, body_code): | 49 def GenerateHeader(self, file_base, body_code): |
45 """Generates a code.Code object for a header file | 50 """Generates a code.Code object for a header file |
46 | 51 |
47 Parameters: | 52 Parameters: |
48 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') | 53 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') |
49 - |body_code| - the code to put in between the multiple inclusion guards""" | 54 - |body_code| - the code to put in between the multiple inclusion guards""" |
50 c = code.Code() | 55 c = code.Code() |
51 c.Append(cpp_util.CHROMIUM_LICENSE) | 56 c.Append(cpp_util.CHROMIUM_LICENSE) |
52 c.Append() | 57 c.Append() |
(...skipping 16 matching lines...) Expand all Loading... |
69 if model_object.platforms is None: | 74 if model_object.platforms is None: |
70 return None | 75 return None |
71 ifdefs = [] | 76 ifdefs = [] |
72 for platform in model_object.platforms: | 77 for platform in model_object.platforms: |
73 if platform == Platforms.CHROMEOS: | 78 if platform == Platforms.CHROMEOS: |
74 ifdefs.append('defined(OS_CHROMEOS)') | 79 ifdefs.append('defined(OS_CHROMEOS)') |
75 else: | 80 else: |
76 raise ValueError("Unsupported platform ifdef: %s" % platform.name) | 81 raise ValueError("Unsupported platform ifdef: %s" % platform.name) |
77 return ' and '.join(ifdefs) | 82 return ' and '.join(ifdefs) |
78 | 83 |
79 def GenerateAPIHeader(self): | |
80 """Generates the header for API registration / declaration""" | |
81 c = code.Code() | |
82 | |
83 c.Append('#include <string>') | |
84 c.Append() | |
85 c.Append('#include "base/basictypes.h"') | |
86 | |
87 for namespace in self._model.namespaces.values(): | |
88 namespace_name = namespace.unix_name.replace("experimental_", "") | |
89 implementation_header = namespace.compiler_options.get( | |
90 "implemented_in", | |
91 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, | |
92 namespace_name)) | |
93 if not os.path.exists( | |
94 os.path.join(self._root, os.path.normpath(implementation_header))): | |
95 if "implemented_in" in namespace.compiler_options: | |
96 raise ValueError('Header file for namespace "%s" specified in ' | |
97 'compiler_options not found: %s' % | |
98 (namespace.unix_name, implementation_header)) | |
99 continue | |
100 ifdefs = self._GetPlatformIfdefs(namespace) | |
101 if ifdefs is not None: | |
102 c.Append("#if %s" % ifdefs, indent_level=0) | |
103 | |
104 c.Append('#include "%s"' % implementation_header) | |
105 | |
106 if ifdefs is not None: | |
107 c.Append("#endif // %s" % ifdefs, indent_level=0) | |
108 | |
109 c.Append() | |
110 c.Append("class ExtensionFunctionRegistry;") | |
111 c.Append() | |
112 | |
113 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | |
114 for namespace in self._model.namespaces.values(): | |
115 c.Append("// TODO(miket): emit code for %s" % (namespace.unix_name)) | |
116 c.Append() | |
117 c.Concat(self.GenerateFunctionRegistry()) | |
118 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | |
119 c.Append() | |
120 return self.GenerateHeader('generated_api', c) | |
121 | |
122 def _GetNamespaceFunctions(self, namespace): | 84 def _GetNamespaceFunctions(self, namespace): |
123 functions = list(namespace.functions.values()) | 85 functions = list(namespace.functions.values()) |
124 if namespace.compiler_options.get("generate_type_functions", False): | 86 if namespace.compiler_options.get("generate_type_functions", False): |
125 for type_ in namespace.types.values(): | 87 for type_ in namespace.types.values(): |
126 functions += list(type_.functions.values()) | 88 functions += list(type_.functions.values()) |
127 return functions | 89 return functions |
128 | 90 |
129 def GenerateFunctionRegistry(self): | 91 def GenerateFunctionRegistry(self): |
130 c = code.Code() | 92 c = code.Code() |
131 c.Sblock("class GeneratedFunctionRegistry {") | 93 c.Sblock("class GeneratedFunctionRegistry {") |
(...skipping 20 matching lines...) Expand all Loading... |
152 if function_ifdefs is not None: | 114 if function_ifdefs is not None: |
153 c.Append("#endif // %s" % function_ifdefs, indent_level=0) | 115 c.Append("#endif // %s" % function_ifdefs, indent_level=0) |
154 | 116 |
155 if namespace_ifdefs is not None: | 117 if namespace_ifdefs is not None: |
156 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) | 118 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) |
157 c.Eblock("}") | 119 c.Eblock("}") |
158 c.Eblock("};") | 120 c.Eblock("};") |
159 c.Append() | 121 c.Append() |
160 return c | 122 return c |
161 | 123 |
162 def GenerateSchemasHeader(self): | 124 class _APIHGenerator(object): |
163 """Generates a code.Code object for the generated schemas .h file""" | 125 """Generates the header for API registration / declaration""" |
| 126 def __init__(self, cpp_bundle): |
| 127 self._bundle = cpp_bundle |
| 128 |
| 129 def Generate(self, namespace): |
| 130 c = code.Code() |
| 131 |
| 132 c.Append('#include <string>') |
| 133 c.Append() |
| 134 c.Append('#include "base/basictypes.h"') |
| 135 |
| 136 for namespace in self._bundle._model.namespaces.values(): |
| 137 namespace_name = namespace.unix_name.replace("experimental_", "") |
| 138 implementation_header = namespace.compiler_options.get( |
| 139 "implemented_in", |
| 140 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, |
| 141 namespace_name)) |
| 142 if not os.path.exists( |
| 143 os.path.join(self._bundle._root, |
| 144 os.path.normpath(implementation_header))): |
| 145 if "implemented_in" in namespace.compiler_options: |
| 146 raise ValueError('Header file for namespace "%s" specified in ' |
| 147 'compiler_options not found: %s' % |
| 148 (namespace.unix_name, implementation_header)) |
| 149 continue |
| 150 ifdefs = self._bundle._GetPlatformIfdefs(namespace) |
| 151 if ifdefs is not None: |
| 152 c.Append("#if %s" % ifdefs, indent_level=0) |
| 153 |
| 154 c.Append('#include "%s"' % implementation_header) |
| 155 |
| 156 if ifdefs is not None: |
| 157 c.Append("#endif // %s" % ifdefs, indent_level=0) |
| 158 |
| 159 c.Append() |
| 160 c.Append("class ExtensionFunctionRegistry;") |
| 161 c.Append() |
| 162 |
| 163 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
| 164 for namespace in self._bundle._model.namespaces.values(): |
| 165 c.Append("// TODO(miket): emit code for %s" % (namespace.unix_name)) |
| 166 c.Append() |
| 167 c.Concat(self._bundle.GenerateFunctionRegistry()) |
| 168 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
| 169 c.Append() |
| 170 return self._bundle.GenerateHeader('generated_api', c) |
| 171 |
| 172 class _SchemasHGenerator(object): |
| 173 """Generates a code.Code object for the generated schemas .h file""" |
| 174 def __init__(self, cpp_bundle): |
| 175 self._bundle = cpp_bundle |
| 176 |
| 177 def Generate(self, namespace): |
164 c = code.Code() | 178 c = code.Code() |
165 c.Append('#include <map>') | 179 c.Append('#include <map>') |
166 c.Append('#include <string>') | 180 c.Append('#include <string>') |
167 c.Append(); | 181 c.Append(); |
168 c.Append('#include "base/string_piece.h"') | 182 c.Append('#include "base/string_piece.h"') |
169 c.Append() | 183 c.Append() |
170 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | 184 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
171 c.Append() | 185 c.Append() |
172 c.Sblock('class GeneratedSchemas {') | 186 c.Sblock('class GeneratedSchemas {') |
173 c.Append(' public:') | 187 c.Append(' public:') |
174 c.Append('// Puts all API schemas in |schemas|.') | 188 c.Append('// Puts all API schemas in |schemas|.') |
175 c.Append('static void Get(' | 189 c.Append('static void Get(' |
176 'std::map<std::string, base::StringPiece>* schemas);') | 190 'std::map<std::string, base::StringPiece>* schemas);') |
177 c.Eblock('};'); | 191 c.Eblock('};'); |
178 c.Append() | 192 c.Append() |
179 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 193 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
180 c.Append() | 194 c.Append() |
181 return self.GenerateHeader('generated_schemas', c) | 195 return self._bundle.GenerateHeader('generated_schemas', c) |
182 | 196 |
183 def GenerateSchemasCC(self): | 197 class _SchemasCCGenerator(object): |
184 """Generates a code.Code object for the generated schemas .cc file""" | 198 """Generates a code.Code object for the generated schemas .cc file""" |
| 199 |
| 200 def __init__(self, cpp_bundle): |
| 201 self._bundle = cpp_bundle |
| 202 |
| 203 def Generate(self, namespace): |
185 c = code.Code() | 204 c = code.Code() |
186 c.Append(cpp_util.CHROMIUM_LICENSE) | 205 c.Append(cpp_util.CHROMIUM_LICENSE) |
187 c.Append() | 206 c.Append() |
188 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, | 207 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, |
189 'generated_schemas.h'))) | 208 'generated_schemas.h'))) |
190 c.Append() | 209 c.Append() |
191 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | 210 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
192 c.Append() | 211 c.Append() |
193 c.Append('// static') | 212 c.Append('// static') |
194 c.Sblock('void GeneratedSchemas::Get(' | 213 c.Sblock('void GeneratedSchemas::Get(' |
195 'std::map<std::string, base::StringPiece>* schemas) {') | 214 'std::map<std::string, base::StringPiece>* schemas) {') |
196 for api in self._api_defs: | 215 for api in self._bundle._api_defs: |
197 namespace = self._model.namespaces[api.get('namespace')] | 216 namespace = self._bundle._model.namespaces[api.get('namespace')] |
198 # JSON parsing code expects lists of schemas, so dump a singleton list. | 217 # JSON parsing code expects lists of schemas, so dump a singleton list. |
199 json_content = json.dumps([_RemoveDescriptions(api)], | 218 json_content = json.dumps([_RemoveDescriptions(api)], |
200 separators=(',', ':')) | 219 separators=(',', ':')) |
201 # Escape all double-quotes and backslashes. For this to output a valid | 220 # Escape all double-quotes and backslashes. For this to output a valid |
202 # JSON C string, we need to escape \ and ". | 221 # JSON C string, we need to escape \ and ". |
203 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') | 222 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') |
204 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) | 223 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) |
205 c.Eblock('}') | 224 c.Eblock('}') |
206 c.Append() | 225 c.Append() |
207 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 226 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
208 c.Append() | 227 c.Append() |
209 return c | 228 return c |
OLD | NEW |