| 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 24 matching lines...) Expand all Loading... |
| 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, cpp_namespace): | 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 | 43 self._cpp_namespace = cpp_namespace |
| 44 | 44 |
| 45 self.api_cc_generator = _APICCGenerator(self) |
| 45 self.api_h_generator = _APIHGenerator(self) | 46 self.api_h_generator = _APIHGenerator(self) |
| 46 self.schemas_cc_generator = _SchemasCCGenerator(self) | 47 self.schemas_cc_generator = _SchemasCCGenerator(self) |
| 47 self.schemas_h_generator = _SchemasHGenerator(self) | 48 self.schemas_h_generator = _SchemasHGenerator(self) |
| 48 | 49 |
| 49 def GenerateHeader(self, file_base, body_code): | 50 def _GenerateHeader(self, file_base, body_code): |
| 50 """Generates a code.Code object for a header file | 51 """Generates a code.Code object for a header file |
| 51 | 52 |
| 52 Parameters: | 53 Parameters: |
| 53 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') | 54 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') |
| 54 - |body_code| - the code to put in between the multiple inclusion guards""" | 55 - |body_code| - the code to put in between the multiple inclusion guards""" |
| 55 c = code.Code() | 56 c = code.Code() |
| 56 c.Append(cpp_util.CHROMIUM_LICENSE) | 57 c.Append(cpp_util.CHROMIUM_LICENSE) |
| 57 c.Append() | 58 c.Append() |
| 58 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH) | 59 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH) |
| 59 ifndef_name = cpp_util.GenerateIfndefName(SOURCE_BASE_PATH, file_base) | 60 ifndef_name = cpp_util.GenerateIfndefName(SOURCE_BASE_PATH, file_base) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 81 raise ValueError("Unsupported platform ifdef: %s" % platform.name) | 82 raise ValueError("Unsupported platform ifdef: %s" % platform.name) |
| 82 return ' and '.join(ifdefs) | 83 return ' and '.join(ifdefs) |
| 83 | 84 |
| 84 def _GetNamespaceFunctions(self, namespace): | 85 def _GetNamespaceFunctions(self, namespace): |
| 85 functions = list(namespace.functions.values()) | 86 functions = list(namespace.functions.values()) |
| 86 if namespace.compiler_options.get("generate_type_functions", False): | 87 if namespace.compiler_options.get("generate_type_functions", False): |
| 87 for type_ in namespace.types.values(): | 88 for type_ in namespace.types.values(): |
| 88 functions += list(type_.functions.values()) | 89 functions += list(type_.functions.values()) |
| 89 return functions | 90 return functions |
| 90 | 91 |
| 91 def GenerateFunctionRegistry(self): | 92 def _GenerateFunctionRegistryRegisterAll(self): |
| 92 c = code.Code() | 93 c = code.Code() |
| 93 c.Sblock("class GeneratedFunctionRegistry {") | 94 c.Append('// static') |
| 94 c.Append(" public:") | 95 c.Sblock('void GeneratedFunctionRegistry::RegisterAll(' |
| 95 c.Sblock("static void RegisterAll(ExtensionFunctionRegistry* registry) {") | 96 'ExtensionFunctionRegistry* registry) {') |
| 96 for namespace in self._model.namespaces.values(): | 97 for namespace in self._model.namespaces.values(): |
| 97 namespace_ifdefs = self._GetPlatformIfdefs(namespace) | 98 namespace_ifdefs = self._GetPlatformIfdefs(namespace) |
| 98 if namespace_ifdefs is not None: | 99 if namespace_ifdefs is not None: |
| 99 c.Append("#if %s" % namespace_ifdefs, indent_level=0) | 100 c.Append("#if %s" % namespace_ifdefs, indent_level=0) |
| 100 | 101 |
| 101 namespace_name = CapitalizeFirstLetter(namespace.name.replace( | 102 namespace_name = CapitalizeFirstLetter(namespace.name.replace( |
| 102 "experimental.", "")) | 103 "experimental.", "")) |
| 103 for function in self._GetNamespaceFunctions(namespace): | 104 for function in self._GetNamespaceFunctions(namespace): |
| 104 if function.nocompile: | 105 if function.nocompile: |
| 105 continue | 106 continue |
| 106 function_ifdefs = self._GetPlatformIfdefs(function) | 107 function_ifdefs = self._GetPlatformIfdefs(function) |
| 107 if function_ifdefs is not None: | 108 if function_ifdefs is not None: |
| 108 c.Append("#if %s" % function_ifdefs, indent_level=0) | 109 c.Append("#if %s" % function_ifdefs, indent_level=0) |
| 109 | 110 |
| 110 function_name = JsFunctionNameToClassName(namespace.name, function.name) | 111 function_name = JsFunctionNameToClassName(namespace.name, function.name) |
| 111 c.Append("registry->RegisterFunction<%sFunction>();" % ( | 112 c.Append("registry->RegisterFunction<%sFunction>();" % ( |
| 112 function_name)) | 113 function_name)) |
| 113 | 114 |
| 114 if function_ifdefs is not None: | 115 if function_ifdefs is not None: |
| 115 c.Append("#endif // %s" % function_ifdefs, indent_level=0) | 116 c.Append("#endif // %s" % function_ifdefs, indent_level=0) |
| 116 | 117 |
| 117 if namespace_ifdefs is not None: | 118 if namespace_ifdefs is not None: |
| 118 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) | 119 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) |
| 119 c.Eblock("}") | 120 c.Eblock("}") |
| 120 c.Eblock("};") | |
| 121 c.Append() | |
| 122 return c | 121 return c |
| 123 | 122 |
| 124 class _APIHGenerator(object): | 123 class _APIHGenerator(object): |
| 125 """Generates the header for API registration / declaration""" | 124 """Generates the header for API registration / declaration""" |
| 126 def __init__(self, cpp_bundle): | 125 def __init__(self, cpp_bundle): |
| 127 self._bundle = cpp_bundle | 126 self._bundle = cpp_bundle |
| 128 | 127 |
| 129 def Generate(self, namespace): | 128 def Generate(self, namespace): |
| 130 c = code.Code() | 129 c = code.Code() |
| 131 | 130 |
| 132 c.Append('#include <string>') | 131 c.Append('#include <string>') |
| 133 c.Append() | 132 c.Append() |
| 134 c.Append('#include "base/basictypes.h"') | 133 c.Append('#include "base/basictypes.h"') |
| 134 c.Append() |
| 135 c.Append("class ExtensionFunctionRegistry;") |
| 136 c.Append() |
| 137 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
| 138 c.Append() |
| 139 c.Append('class GeneratedFunctionRegistry {') |
| 140 c.Sblock(' public:') |
| 141 c.Append('static void RegisterAll(' |
| 142 'ExtensionFunctionRegistry* registry);') |
| 143 c.Eblock('};'); |
| 144 c.Append() |
| 145 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
| 146 return self._bundle._GenerateHeader('generated_api', c) |
| 135 | 147 |
| 148 class _APICCGenerator(object): |
| 149 """Generates a code.Code object for the generated API .cc file""" |
| 150 |
| 151 def __init__(self, cpp_bundle): |
| 152 self._bundle = cpp_bundle |
| 153 |
| 154 def Generate(self, namespace): |
| 155 c = code.Code() |
| 156 c.Append(cpp_util.CHROMIUM_LICENSE) |
| 157 c.Append() |
| 158 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, |
| 159 'generated_api.h'))) |
| 160 c.Append() |
| 136 for namespace in self._bundle._model.namespaces.values(): | 161 for namespace in self._bundle._model.namespaces.values(): |
| 137 namespace_name = namespace.unix_name.replace("experimental_", "") | 162 namespace_name = namespace.unix_name.replace("experimental_", "") |
| 138 implementation_header = namespace.compiler_options.get( | 163 implementation_header = namespace.compiler_options.get( |
| 139 "implemented_in", | 164 "implemented_in", |
| 140 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, | 165 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, |
| 141 namespace_name)) | 166 namespace_name)) |
| 142 if not os.path.exists( | 167 if not os.path.exists( |
| 143 os.path.join(self._bundle._root, | 168 os.path.join(self._bundle._root, |
| 144 os.path.normpath(implementation_header))): | 169 os.path.normpath(implementation_header))): |
| 145 if "implemented_in" in namespace.compiler_options: | 170 if "implemented_in" in namespace.compiler_options: |
| 146 raise ValueError('Header file for namespace "%s" specified in ' | 171 raise ValueError('Header file for namespace "%s" specified in ' |
| 147 'compiler_options not found: %s' % | 172 'compiler_options not found: %s' % |
| 148 (namespace.unix_name, implementation_header)) | 173 (namespace.unix_name, implementation_header)) |
| 149 continue | 174 continue |
| 150 ifdefs = self._bundle._GetPlatformIfdefs(namespace) | 175 ifdefs = self._bundle._GetPlatformIfdefs(namespace) |
| 151 if ifdefs is not None: | 176 if ifdefs is not None: |
| 152 c.Append("#if %s" % ifdefs, indent_level=0) | 177 c.Append("#if %s" % ifdefs, indent_level=0) |
| 153 | 178 |
| 154 c.Append('#include "%s"' % implementation_header) | 179 c.Append('#include "%s"' % implementation_header) |
| 155 | 180 |
| 156 if ifdefs is not None: | 181 if ifdefs is not None: |
| 157 c.Append("#endif // %s" % ifdefs, indent_level=0) | 182 c.Append("#endif // %s" % ifdefs, indent_level=0) |
| 158 | |
| 159 c.Append() | 183 c.Append() |
| 160 c.Append("class ExtensionFunctionRegistry;") | 184 c.Append('#include ' |
| 185 '"chrome/browser/extensions/extension_function_registry.h"') |
| 161 c.Append() | 186 c.Append() |
| 162 | |
| 163 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | 187 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() | 188 c.Append() |
| 167 c.Concat(self._bundle.GenerateFunctionRegistry()) | 189 c.Concat(self._bundle._GenerateFunctionRegistryRegisterAll()) |
| 190 c.Append() |
| 168 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 191 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
| 169 c.Append() | 192 c.Append() |
| 170 return self._bundle.GenerateHeader('generated_api', c) | 193 return c |
| 171 | 194 |
| 172 class _SchemasHGenerator(object): | 195 class _SchemasHGenerator(object): |
| 173 """Generates a code.Code object for the generated schemas .h file""" | 196 """Generates a code.Code object for the generated schemas .h file""" |
| 174 def __init__(self, cpp_bundle): | 197 def __init__(self, cpp_bundle): |
| 175 self._bundle = cpp_bundle | 198 self._bundle = cpp_bundle |
| 176 | 199 |
| 177 def Generate(self, namespace): | 200 def Generate(self, namespace): |
| 178 c = code.Code() | 201 c = code.Code() |
| 179 c.Append('#include <map>') | 202 c.Append('#include <map>') |
| 180 c.Append('#include <string>') | 203 c.Append('#include <string>') |
| 181 c.Append(); | 204 c.Append(); |
| 182 c.Append('#include "base/string_piece.h"') | 205 c.Append('#include "base/string_piece.h"') |
| 183 c.Append() | 206 c.Append() |
| 184 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | 207 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
| 185 c.Append() | 208 c.Append() |
| 186 c.Sblock('class GeneratedSchemas {') | 209 c.Append('class GeneratedSchemas {') |
| 187 c.Append(' public:') | 210 c.Sblock(' public:') |
| 188 c.Append('// Puts all API schemas in |schemas|.') | 211 c.Append('// Puts all API schemas in |schemas|.') |
| 189 c.Append('static void Get(' | 212 c.Append('static void Get(' |
| 190 'std::map<std::string, base::StringPiece>* schemas);') | 213 'std::map<std::string, base::StringPiece>* schemas);') |
| 191 c.Eblock('};'); | 214 c.Eblock('};'); |
| 192 c.Append() | 215 c.Append() |
| 193 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 216 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
| 194 c.Append() | 217 return self._bundle._GenerateHeader('generated_schemas', c) |
| 195 return self._bundle.GenerateHeader('generated_schemas', c) | |
| 196 | 218 |
| 197 class _SchemasCCGenerator(object): | 219 class _SchemasCCGenerator(object): |
| 198 """Generates a code.Code object for the generated schemas .cc file""" | 220 """Generates a code.Code object for the generated schemas .cc file""" |
| 199 | 221 |
| 200 def __init__(self, cpp_bundle): | 222 def __init__(self, cpp_bundle): |
| 201 self._bundle = cpp_bundle | 223 self._bundle = cpp_bundle |
| 202 | 224 |
| 203 def Generate(self, namespace): | 225 def Generate(self, namespace): |
| 204 c = code.Code() | 226 c = code.Code() |
| 205 c.Append(cpp_util.CHROMIUM_LICENSE) | 227 c.Append(cpp_util.CHROMIUM_LICENSE) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 219 separators=(',', ':')) | 241 separators=(',', ':')) |
| 220 # Escape all double-quotes and backslashes. For this to output a valid | 242 # Escape all double-quotes and backslashes. For this to output a valid |
| 221 # JSON C string, we need to escape \ and ". | 243 # JSON C string, we need to escape \ and ". |
| 222 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') | 244 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') |
| 223 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) | 245 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) |
| 224 c.Eblock('}') | 246 c.Eblock('}') |
| 225 c.Append() | 247 c.Append() |
| 226 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 248 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
| 227 c.Append() | 249 c.Append() |
| 228 return c | 250 return c |
| OLD | NEW |