Chromium Code Reviews| 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 23 matching lines...) Expand all Loading... | |
| 34 class SchemaBundleGenerator(object): | 34 class SchemaBundleGenerator(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): |
| 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 | 43 |
| 44 def GenerateHeader(self, file_base, body_code): | 44 def GenerateHeader(self, file_base, body_code): |
|
not at google - send to devlin
2013/02/04 16:57:24
_GenerateHeader
| |
| 45 """Generates a code.Code object for a header file | 45 """Generates a code.Code object for a header file |
| 46 | 46 |
| 47 Parameters: | 47 Parameters: |
| 48 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') | 48 - |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""" | 49 - |body_code| - the code to put in between the multiple inclusion guards""" |
| 50 c = code.Code() | 50 c = code.Code() |
| 51 c.Append(cpp_util.CHROMIUM_LICENSE) | 51 c.Append(cpp_util.CHROMIUM_LICENSE) |
| 52 c.Append() | 52 c.Append() |
| 53 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH) | 53 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH) |
| 54 ifndef_name = cpp_util.GenerateIfndefName(SOURCE_BASE_PATH, file_base) | 54 ifndef_name = cpp_util.GenerateIfndefName(SOURCE_BASE_PATH, file_base) |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 76 raise ValueError("Unsupported platform ifdef: %s" % platform.name) | 76 raise ValueError("Unsupported platform ifdef: %s" % platform.name) |
| 77 return ' and '.join(ifdefs) | 77 return ' and '.join(ifdefs) |
| 78 | 78 |
| 79 def GenerateAPIHeader(self): | 79 def GenerateAPIHeader(self): |
| 80 """Generates the header for API registration / declaration""" | 80 """Generates the header for API registration / declaration""" |
| 81 c = code.Code() | 81 c = code.Code() |
| 82 | 82 |
| 83 c.Append('#include <string>') | 83 c.Append('#include <string>') |
| 84 c.Append() | 84 c.Append() |
| 85 c.Append('#include "base/basictypes.h"') | 85 c.Append('#include "base/basictypes.h"') |
| 86 c.Append() | |
| 87 c.Append("class ExtensionFunctionRegistry;") | |
| 88 c.Append() | |
| 89 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | |
| 90 for namespace in self._model.namespaces.values(): | |
| 91 c.Append("// TODO(miket): emit code for %s" % (namespace.unix_name)) | |
|
not at google - send to devlin
2013/02/04 16:57:24
I don't know what this TODO means and it has no bu
| |
| 92 c.Append() | |
| 93 c.Sblock('class GeneratedFunctionRegistry {') | |
| 94 c.Append(' public:') | |
| 95 c.Append('static void RegisterAll(' | |
| 96 'ExtensionFunctionRegistry* registry);') | |
|
not at google - send to devlin
2013/02/04 16:57:24
C++ indentation here needs fixing (and line above)
| |
| 97 c.Eblock('};'); | |
| 98 c.Append() | |
| 99 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | |
| 100 c.Append() | |
| 101 return self.GenerateHeader('generated_api', c) | |
| 86 | 102 |
| 103 def GenerateAPICC(self): | |
| 104 """Generates a code.Code object for the generated API .cc file""" | |
| 105 c = code.Code() | |
| 106 c.Append(cpp_util.CHROMIUM_LICENSE) | |
| 107 c.Append() | |
| 108 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, | |
| 109 'generated_api.h'))) | |
| 110 c.Append() | |
| 87 for namespace in self._model.namespaces.values(): | 111 for namespace in self._model.namespaces.values(): |
| 88 namespace_name = namespace.unix_name.replace("experimental_", "") | 112 namespace_name = namespace.unix_name.replace("experimental_", "") |
| 89 implementation_header = namespace.compiler_options.get( | 113 implementation_header = namespace.compiler_options.get( |
| 90 "implemented_in", | 114 "implemented_in", |
| 91 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, | 115 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, |
| 92 namespace_name)) | 116 namespace_name)) |
| 93 if not os.path.exists( | 117 if not os.path.exists( |
| 94 os.path.join(self._root, os.path.normpath(implementation_header))): | 118 os.path.join(self._root, os.path.normpath(implementation_header))): |
| 95 if "implemented_in" in namespace.compiler_options: | 119 if "implemented_in" in namespace.compiler_options: |
| 96 raise ValueError('Header file for namespace "%s" specified in ' | 120 raise ValueError('Header file for namespace "%s" specified in ' |
| 97 'compiler_options not found: %s' % | 121 'compiler_options not found: %s' % |
| 98 (namespace.unix_name, implementation_header)) | 122 (namespace.unix_name, implementation_header)) |
| 99 continue | 123 continue |
| 100 ifdefs = self._GetPlatformIfdefs(namespace) | 124 ifdefs = self._GetPlatformIfdefs(namespace) |
| 101 if ifdefs is not None: | 125 if ifdefs is not None: |
| 102 c.Append("#if %s" % ifdefs, indent_level=0) | 126 c.Append("#if %s" % ifdefs, indent_level=0) |
| 103 | 127 |
| 104 c.Append('#include "%s"' % implementation_header) | 128 c.Append('#include "%s"' % implementation_header) |
| 105 | 129 |
| 106 if ifdefs is not None: | 130 if ifdefs is not None: |
| 107 c.Append("#endif // %s" % ifdefs, indent_level=0) | 131 c.Append("#endif // %s" % ifdefs, indent_level=0) |
| 108 | |
| 109 c.Append() | 132 c.Append() |
| 110 c.Append("class ExtensionFunctionRegistry;") | 133 c.Append('#include ' |
| 134 '"chrome/browser/extensions/extension_function_registry.h"') | |
| 111 c.Append() | 135 c.Append() |
| 112 | |
| 113 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | 136 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() | 137 c.Append() |
| 117 c.Concat(self.GenerateFunctionRegistry()) | 138 c.Concat(self.GenerateFunctionRegistryRegisterAll()) |
| 139 c.Append() | |
| 118 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 140 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
| 119 c.Append() | 141 c.Append() |
| 120 return self.GenerateHeader('generated_api', c) | 142 return c |
| 121 | 143 |
| 122 def _GetNamespaceFunctions(self, namespace): | 144 def _GetNamespaceFunctions(self, namespace): |
| 123 functions = list(namespace.functions.values()) | 145 functions = list(namespace.functions.values()) |
| 124 if namespace.compiler_options.get("generate_type_functions", False): | 146 if namespace.compiler_options.get("generate_type_functions", False): |
| 125 for type_ in namespace.types.values(): | 147 for type_ in namespace.types.values(): |
| 126 functions += list(type_.functions.values()) | 148 functions += list(type_.functions.values()) |
| 127 return functions | 149 return functions |
| 128 | 150 |
| 129 def GenerateFunctionRegistry(self): | 151 def GenerateFunctionRegistryRegisterAll(self): |
|
not at google - send to devlin
2013/02/04 16:57:24
_GenerateFunctionRegistryRegisterAll
| |
| 130 c = code.Code() | 152 c = code.Code() |
| 131 c.Sblock("class GeneratedFunctionRegistry {") | 153 c.Append('// static') |
| 132 c.Append(" public:") | 154 c.Sblock('void GeneratedFunctionRegistry::RegisterAll(' |
| 133 c.Sblock("static void RegisterAll(ExtensionFunctionRegistry* registry) {") | 155 'ExtensionFunctionRegistry* registry) {') |
| 134 for namespace in self._model.namespaces.values(): | 156 for namespace in self._model.namespaces.values(): |
| 135 namespace_ifdefs = self._GetPlatformIfdefs(namespace) | 157 namespace_ifdefs = self._GetPlatformIfdefs(namespace) |
| 136 if namespace_ifdefs is not None: | 158 if namespace_ifdefs is not None: |
| 137 c.Append("#if %s" % namespace_ifdefs, indent_level=0) | 159 c.Append("#if %s" % namespace_ifdefs, indent_level=0) |
| 138 | 160 |
| 139 namespace_name = CapitalizeFirstLetter(namespace.name.replace( | 161 namespace_name = CapitalizeFirstLetter(namespace.name.replace( |
| 140 "experimental.", "")) | 162 "experimental.", "")) |
| 141 for function in self._GetNamespaceFunctions(namespace): | 163 for function in self._GetNamespaceFunctions(namespace): |
| 142 if function.nocompile: | 164 if function.nocompile: |
| 143 continue | 165 continue |
| 144 function_ifdefs = self._GetPlatformIfdefs(function) | 166 function_ifdefs = self._GetPlatformIfdefs(function) |
| 145 if function_ifdefs is not None: | 167 if function_ifdefs is not None: |
| 146 c.Append("#if %s" % function_ifdefs, indent_level=0) | 168 c.Append("#if %s" % function_ifdefs, indent_level=0) |
| 147 | 169 |
| 148 function_name = JsFunctionNameToClassName(namespace.name, function.name) | 170 function_name = JsFunctionNameToClassName(namespace.name, function.name) |
| 149 c.Append("registry->RegisterFunction<%sFunction>();" % ( | 171 c.Append("registry->RegisterFunction<%sFunction>();" % ( |
| 150 function_name)) | 172 function_name)) |
| 151 | 173 |
| 152 if function_ifdefs is not None: | 174 if function_ifdefs is not None: |
| 153 c.Append("#endif // %s" % function_ifdefs, indent_level=0) | 175 c.Append("#endif // %s" % function_ifdefs, indent_level=0) |
| 154 | 176 |
| 155 if namespace_ifdefs is not None: | 177 if namespace_ifdefs is not None: |
| 156 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) | 178 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) |
| 157 c.Eblock("}") | 179 c.Eblock("}") |
| 158 c.Eblock("};") | |
| 159 c.Append() | |
| 160 return c | 180 return c |
| 161 | 181 |
| 162 def GenerateSchemasHeader(self): | 182 def GenerateSchemasHeader(self): |
| 163 """Generates a code.Code object for the generated schemas .h file""" | 183 """Generates a code.Code object for the generated schemas .h file""" |
| 164 c = code.Code() | 184 c = code.Code() |
| 165 c.Append('#include <map>') | 185 c.Append('#include <map>') |
| 166 c.Append('#include <string>') | 186 c.Append('#include <string>') |
| 167 c.Append(); | 187 c.Append(); |
| 168 c.Append('#include "base/string_piece.h"') | 188 c.Append('#include "base/string_piece.h"') |
| 169 c.Append() | 189 c.Append() |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 200 separators=(',', ':')) | 220 separators=(',', ':')) |
| 201 # Escape all double-quotes and backslashes. For this to output a valid | 221 # Escape all double-quotes and backslashes. For this to output a valid |
| 202 # JSON C string, we need to escape \ and ". | 222 # JSON C string, we need to escape \ and ". |
| 203 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') | 223 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') |
| 204 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) | 224 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) |
| 205 c.Eblock('}') | 225 c.Eblock('}') |
| 206 c.Append() | 226 c.Append() |
| 207 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 227 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
| 208 c.Append() | 228 c.Append() |
| 209 return c | 229 return c |
| OLD | NEW |