| 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 |
| 11 import json | 11 import json |
| 12 import os | 12 import os |
| 13 import re | 13 import re |
| 14 | 14 |
| 15 # TODO(miket/asargent) - parameterize this. | |
| 16 SOURCE_BASE_PATH = 'chrome/common/extensions/api' | |
| 17 | 15 |
| 18 def _RemoveDescriptions(node): | 16 def _RemoveDescriptions(node): |
| 19 """Returns a copy of |schema| with "description" fields removed. | 17 """Returns a copy of |schema| with "description" fields removed. |
| 20 """ | 18 """ |
| 21 if isinstance(node, dict): | 19 if isinstance(node, dict): |
| 22 result = {} | 20 result = {} |
| 23 for key, value in node.items(): | 21 for key, value in node.items(): |
| 24 # Some schemas actually have properties called "description", so only | 22 # Some schemas actually have properties called "description", so only |
| 25 # remove descriptions that have string values. | 23 # remove descriptions that have string values. |
| 26 if key == 'description' and isinstance(value, basestring): | 24 if key == 'description' and isinstance(value, basestring): |
| 27 continue | 25 continue |
| 28 result[key] = _RemoveDescriptions(value) | 26 result[key] = _RemoveDescriptions(value) |
| 29 return result | 27 return result |
| 30 if isinstance(node, list): | 28 if isinstance(node, list): |
| 31 return [_RemoveDescriptions(v) for v in node] | 29 return [_RemoveDescriptions(v) for v in node] |
| 32 return node | 30 return node |
| 33 | 31 |
| 34 | 32 |
| 35 class CppBundleGenerator(object): | 33 class CppBundleGenerator(object): |
| 36 """This class contains methods to generate code based on multiple schemas. | 34 """This class contains methods to generate code based on multiple schemas. |
| 37 """ | 35 """ |
| 38 | 36 |
| 39 def __init__(self, root, model, api_defs, cpp_type_generator, cpp_namespace): | 37 def __init__(self, |
| 38 root, |
| 39 model, |
| 40 api_defs, |
| 41 cpp_type_generator, |
| 42 cpp_namespace, |
| 43 source_file_dir): |
| 40 self._root = root | 44 self._root = root |
| 41 self._model = model | 45 self._model = model |
| 42 self._api_defs = api_defs | 46 self._api_defs = api_defs |
| 43 self._cpp_type_generator = cpp_type_generator | 47 self._cpp_type_generator = cpp_type_generator |
| 44 self._cpp_namespace = cpp_namespace | 48 self._cpp_namespace = cpp_namespace |
| 49 self._source_file_dir = source_file_dir |
| 45 | 50 |
| 46 self.api_cc_generator = _APICCGenerator(self) | 51 self.api_cc_generator = _APICCGenerator(self) |
| 47 self.api_h_generator = _APIHGenerator(self) | 52 self.api_h_generator = _APIHGenerator(self) |
| 48 self.schemas_cc_generator = _SchemasCCGenerator(self) | 53 self.schemas_cc_generator = _SchemasCCGenerator(self) |
| 49 self.schemas_h_generator = _SchemasHGenerator(self) | 54 self.schemas_h_generator = _SchemasHGenerator(self) |
| 50 | 55 |
| 51 def _GenerateHeader(self, file_base, body_code): | 56 def _GenerateHeader(self, file_base, body_code): |
| 52 """Generates a code.Code object for a header file | 57 """Generates a code.Code object for a header file |
| 53 | 58 |
| 54 Parameters: | 59 Parameters: |
| 55 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') | 60 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') |
| 56 - |body_code| - the code to put in between the multiple inclusion guards""" | 61 - |body_code| - the code to put in between the multiple inclusion guards""" |
| 57 c = code.Code() | 62 c = code.Code() |
| 58 c.Append(cpp_util.CHROMIUM_LICENSE) | 63 c.Append(cpp_util.CHROMIUM_LICENSE) |
| 59 c.Append() | 64 c.Append() |
| 60 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH) | 65 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % self._source_file_dir) |
| 61 ifndef_name = cpp_util.GenerateIfndefName(SOURCE_BASE_PATH, file_base) | 66 ifndef_name = cpp_util.GenerateIfndefName(self._source_file_dir, file_base) |
| 62 c.Append() | 67 c.Append() |
| 63 c.Append('#ifndef %s' % ifndef_name) | 68 c.Append('#ifndef %s' % ifndef_name) |
| 64 c.Append('#define %s' % ifndef_name) | 69 c.Append('#define %s' % ifndef_name) |
| 65 c.Append() | 70 c.Append() |
| 66 c.Concat(body_code) | 71 c.Concat(body_code) |
| 67 c.Append() | 72 c.Append() |
| 68 c.Append('#endif // %s' % ifndef_name) | 73 c.Append('#endif // %s' % ifndef_name) |
| 69 c.Append() | 74 c.Append() |
| 70 return c | 75 return c |
| 71 | 76 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 class _APICCGenerator(object): | 163 class _APICCGenerator(object): |
| 159 """Generates a code.Code object for the generated API .cc file""" | 164 """Generates a code.Code object for the generated API .cc file""" |
| 160 | 165 |
| 161 def __init__(self, cpp_bundle): | 166 def __init__(self, cpp_bundle): |
| 162 self._bundle = cpp_bundle | 167 self._bundle = cpp_bundle |
| 163 | 168 |
| 164 def Generate(self, namespace): | 169 def Generate(self, namespace): |
| 165 c = code.Code() | 170 c = code.Code() |
| 166 c.Append(cpp_util.CHROMIUM_LICENSE) | 171 c.Append(cpp_util.CHROMIUM_LICENSE) |
| 167 c.Append() | 172 c.Append() |
| 168 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, | 173 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, |
| 169 'generated_api.h'))) | 174 'generated_api.h'))) |
| 170 c.Append() | 175 c.Append() |
| 171 for namespace in self._bundle._model.namespaces.values(): | 176 for namespace in self._bundle._model.namespaces.values(): |
| 172 namespace_name = namespace.unix_name.replace("experimental_", "") | 177 namespace_name = namespace.unix_name.replace("experimental_", "") |
| 173 implementation_header = namespace.compiler_options.get( | 178 implementation_header = namespace.compiler_options.get( |
| 174 "implemented_in", | 179 "implemented_in", |
| 175 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, | 180 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, |
| 176 namespace_name)) | 181 namespace_name)) |
| 177 if not os.path.exists( | 182 if not os.path.exists( |
| 178 os.path.join(self._bundle._root, | 183 os.path.join(self._bundle._root, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 class _SchemasCCGenerator(object): | 246 class _SchemasCCGenerator(object): |
| 242 """Generates a code.Code object for the generated schemas .cc file""" | 247 """Generates a code.Code object for the generated schemas .cc file""" |
| 243 | 248 |
| 244 def __init__(self, cpp_bundle): | 249 def __init__(self, cpp_bundle): |
| 245 self._bundle = cpp_bundle | 250 self._bundle = cpp_bundle |
| 246 | 251 |
| 247 def Generate(self, namespace): | 252 def Generate(self, namespace): |
| 248 c = code.Code() | 253 c = code.Code() |
| 249 c.Append(cpp_util.CHROMIUM_LICENSE) | 254 c.Append(cpp_util.CHROMIUM_LICENSE) |
| 250 c.Append() | 255 c.Append() |
| 251 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, | 256 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, |
| 252 'generated_schemas.h'))) | 257 'generated_schemas.h'))) |
| 253 c.Append() | 258 c.Append() |
| 254 c.Append('#include "base/lazy_instance.h"') | 259 c.Append('#include "base/lazy_instance.h"') |
| 255 c.Append() | 260 c.Append() |
| 256 c.Append('namespace {') | 261 c.Append('namespace {') |
| 257 for api in self._bundle._api_defs: | 262 for api in self._bundle._api_defs: |
| 258 namespace = self._bundle._model.namespaces[api.get('namespace')] | 263 namespace = self._bundle._model.namespaces[api.get('namespace')] |
| 259 # JSON parsing code expects lists of schemas, so dump a singleton list. | 264 # JSON parsing code expects lists of schemas, so dump a singleton list. |
| 260 json_content = json.dumps([_RemoveDescriptions(api)], | 265 json_content = json.dumps([_RemoveDescriptions(api)], |
| 261 separators=(',', ':')) | 266 separators=(',', ':')) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 288 c.Eblock('}') | 293 c.Eblock('}') |
| 289 c.Append() | 294 c.Append() |
| 290 c.Append('// static') | 295 c.Append('// static') |
| 291 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {') | 296 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {') |
| 292 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') | 297 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') |
| 293 c.Eblock('}') | 298 c.Eblock('}') |
| 294 c.Append() | 299 c.Append() |
| 295 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 300 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
| 296 c.Append() | 301 c.Append() |
| 297 return c | 302 return c |
| OLD | NEW |