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
3 # found in the LICENSE file.
7 from model
import Platforms
8 from schema_util
import CapitalizeFirstLetter
9 from schema_util
import JsFunctionNameToClassName
16 def _RemoveDescriptions(node
):
17 """Returns a copy of |schema| with "description" fields removed.
19 if isinstance(node
, dict):
21 for key
, value
in node
.items():
22 # Some schemas actually have properties called "description", so only
23 # remove descriptions that have string values.
24 if key
== 'description' and isinstance(value
, basestring
):
26 result
[key
] = _RemoveDescriptions(value
)
28 if isinstance(node
, list):
29 return [_RemoveDescriptions(v
) for v
in node
]
33 class CppBundleGenerator(object):
34 """This class contains methods to generate code based on multiple schemas.
42 cpp_namespace_pattern
,
47 self
._api
_defs
= api_defs
48 self
._cpp
_type
_generator
= cpp_type_generator
49 self
._source
_file
_dir
= source_file_dir
50 self
._impl
_dir
= impl_dir
52 # Hack: assume that the C++ namespace for the bundle is the namespace of the
53 # files without the last component of the namespace. A cleaner way to do
54 # this would be to make it a separate variable in the gyp file.
55 self
._cpp
_namespace
= cpp_namespace_pattern
.rsplit('::', 1)[0]
57 self
.api_cc_generator
= _APICCGenerator(self
)
58 self
.api_h_generator
= _APIHGenerator(self
)
59 self
.schemas_cc_generator
= _SchemasCCGenerator(self
)
60 self
.schemas_h_generator
= _SchemasHGenerator(self
)
62 def _GenerateHeader(self
, file_base
, body_code
):
63 """Generates a code.Code object for a header file
66 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h')
67 - |body_code| - the code to put in between the multiple inclusion guards"""
69 c
.Append(cpp_util
.CHROMIUM_LICENSE
)
71 c
.Append(cpp_util
.GENERATED_BUNDLE_FILE_MESSAGE
% self
._source
_file
_dir
)
72 ifndef_name
= cpp_util
.GenerateIfndefName(
73 '%s/%s.h' % (self
._source
_file
_dir
, file_base
))
75 c
.Append('#ifndef %s' % ifndef_name
)
76 c
.Append('#define %s' % ifndef_name
)
80 c
.Append('#endif // %s' % ifndef_name
)
84 def _GetPlatformIfdefs(self
, model_object
):
85 """Generates the "defined" conditional for an #if check if |model_object|
86 has platform restrictions. Returns None if there are no restrictions.
88 if model_object
.platforms
is None:
91 for platform
in model_object
.platforms
:
92 if platform
== Platforms
.CHROMEOS
:
93 ifdefs
.append('defined(OS_CHROMEOS)')
94 elif platform
== Platforms
.LINUX
:
95 ifdefs
.append('defined(OS_LINUX)')
96 elif platform
== Platforms
.MAC
:
97 ifdefs
.append('defined(OS_MACOSX)')
98 elif platform
== Platforms
.WIN
:
99 ifdefs
.append('defined(OS_WIN)')
101 raise ValueError("Unsupported platform ifdef: %s" % platform
.name
)
102 return ' || '.join(ifdefs
)
104 def _GenerateRegisterFunctions(self
, namespace_name
, function
):
106 function_ifdefs
= self
._GetPlatformIfdefs
(function
)
107 if function_ifdefs
is not None:
108 c
.Append("#if %s" % function_ifdefs
, indent_level
=0)
110 function_name
= JsFunctionNameToClassName(namespace_name
, function
.name
)
111 c
.Append("registry->RegisterFunction<%sFunction>();" % (
114 if function_ifdefs
is not None:
115 c
.Append("#endif // %s" % function_ifdefs
, indent_level
=0)
118 def _GenerateFunctionRegistryRegisterAll(self
):
120 c
.Append('// static')
121 c
.Sblock('void GeneratedFunctionRegistry::RegisterAll('
122 'ExtensionFunctionRegistry* registry) {')
123 for namespace
in self
._model
.namespaces
.values():
124 namespace_ifdefs
= self
._GetPlatformIfdefs
(namespace
)
125 if namespace_ifdefs
is not None:
126 c
.Append("#if %s" % namespace_ifdefs
, indent_level
=0)
128 for function
in namespace
.functions
.values():
129 if function
.nocompile
:
131 c
.Concat(self
._GenerateRegisterFunctions
(namespace
.name
, function
))
133 for type_
in namespace
.types
.values():
134 for function
in type_
.functions
.values():
135 if function
.nocompile
:
137 namespace_types_name
= JsFunctionNameToClassName(
138 namespace
.name
, type_
.name
)
139 c
.Concat(self
._GenerateRegisterFunctions
(namespace_types_name
,
142 if namespace_ifdefs
is not None:
143 c
.Append("#endif // %s" % namespace_ifdefs
, indent_level
=0)
148 class _APIHGenerator(object):
149 """Generates the header for API registration / declaration"""
150 def __init__(self
, cpp_bundle
):
151 self
._bundle
= cpp_bundle
153 def Generate(self
, _
): # namespace not relevant, this is a bundle
156 c
.Append('#include <string>')
158 c
.Append('#include "base/basictypes.h"')
160 c
.Append("class ExtensionFunctionRegistry;")
162 c
.Concat(cpp_util
.OpenNamespace(self
._bundle
._cpp
_namespace
))
164 c
.Append('class GeneratedFunctionRegistry {')
166 c
.Append('static void RegisterAll('
167 'ExtensionFunctionRegistry* registry);')
170 c
.Concat(cpp_util
.CloseNamespace(self
._bundle
._cpp
_namespace
))
171 return self
._bundle
._GenerateHeader
('generated_api', c
)
174 class _APICCGenerator(object):
175 """Generates a code.Code object for the generated API .cc file"""
177 def __init__(self
, cpp_bundle
):
178 self
._bundle
= cpp_bundle
180 def Generate(self
, _
): # namespace not relevant, this is a bundle
182 c
.Append(cpp_util
.CHROMIUM_LICENSE
)
184 c
.Append('#include "%s"' % (
185 os
.path
.join(self
._bundle
._impl
_dir
,
186 'generated_api_registration.h')))
188 for namespace
in self
._bundle
._model
.namespaces
.values():
189 namespace_name
= namespace
.unix_name
.replace("experimental_", "")
190 implementation_header
= namespace
.compiler_options
.get(
192 "%s/%s/%s_api.h" % (self
._bundle
._impl
_dir
,
195 if not os
.path
.exists(
196 os
.path
.join(self
._bundle
._root
,
197 os
.path
.normpath(implementation_header
))):
198 if "implemented_in" in namespace
.compiler_options
:
199 raise ValueError('Header file for namespace "%s" specified in '
200 'compiler_options not found: %s' %
201 (namespace
.unix_name
, implementation_header
))
203 ifdefs
= self
._bundle
._GetPlatformIfdefs
(namespace
)
204 if ifdefs
is not None:
205 c
.Append("#if %s" % ifdefs
, indent_level
=0)
207 c
.Append('#include "%s"' % implementation_header
)
209 if ifdefs
is not None:
210 c
.Append("#endif // %s" % ifdefs
, indent_level
=0)
213 '"extensions/browser/extension_function_registry.h"')
215 c
.Concat(cpp_util
.OpenNamespace(self
._bundle
._cpp
_namespace
))
217 c
.Concat(self
._bundle
._GenerateFunctionRegistryRegisterAll
())
219 c
.Concat(cpp_util
.CloseNamespace(self
._bundle
._cpp
_namespace
))
224 class _SchemasHGenerator(object):
225 """Generates a code.Code object for the generated schemas .h file"""
226 def __init__(self
, cpp_bundle
):
227 self
._bundle
= cpp_bundle
229 def Generate(self
, _
): # namespace not relevant, this is a bundle
231 c
.Append('#include <map>')
232 c
.Append('#include <string>')
234 c
.Append('#include "base/strings/string_piece.h"')
236 c
.Concat(cpp_util
.OpenNamespace(self
._bundle
._cpp
_namespace
))
238 c
.Append('class GeneratedSchemas {')
240 c
.Append('// Determines if schema named |name| is generated.')
241 c
.Append('static bool IsGenerated(std::string name);')
243 c
.Append('// Gets the API schema named |name|.')
244 c
.Append('static base::StringPiece Get(const std::string& name);')
247 c
.Concat(cpp_util
.CloseNamespace(self
._bundle
._cpp
_namespace
))
248 return self
._bundle
._GenerateHeader
('generated_schemas', c
)
251 def _FormatNameAsConstant(name
):
252 """Formats a name to be a C++ constant of the form kConstantName"""
253 name
= '%s%s' % (name
[0].upper(), name
[1:])
254 return 'k%s' % re
.sub('_[a-z]',
255 lambda m
: m
.group(0)[1].upper(),
256 name
.replace('.', '_'))
259 class _SchemasCCGenerator(object):
260 """Generates a code.Code object for the generated schemas .cc file"""
262 def __init__(self
, cpp_bundle
):
263 self
._bundle
= cpp_bundle
265 def Generate(self
, _
): # namespace not relevant, this is a bundle
267 c
.Append(cpp_util
.CHROMIUM_LICENSE
)
269 c
.Append('#include "%s"' % (os
.path
.join(self
._bundle
._source
_file
_dir
,
270 'generated_schemas.h')))
272 c
.Append('#include "base/lazy_instance.h"')
274 c
.Append('namespace {')
275 for api
in self
._bundle
._api
_defs
:
276 namespace
= self
._bundle
._model
.namespaces
[api
.get('namespace')]
277 # JSON parsing code expects lists of schemas, so dump a singleton list.
278 json_content
= json
.dumps([_RemoveDescriptions(api
)],
279 separators
=(',', ':'))
280 # Escape all double-quotes and backslashes. For this to output a valid
281 # JSON C string, we need to escape \ and ". Note that some schemas are
282 # too large to compile on windows. Split the JSON up into several
283 # strings, since apparently that helps.
285 segments
= [json_content
[i
:i
+ max_length
].replace('\\', '\\\\')
287 for i
in xrange(0, len(json_content
), max_length
)]
288 c
.Append('const char %s[] = "%s";' %
289 (_FormatNameAsConstant(namespace
.name
), '" "'.join(segments
)))
291 c
.Concat(cpp_util
.OpenNamespace(self
._bundle
._cpp
_namespace
))
293 c
.Sblock('struct Static {')
294 c
.Sblock('Static() {')
295 for api
in self
._bundle
._api
_defs
:
296 namespace
= self
._bundle
._model
.namespaces
[api
.get('namespace')]
297 c
.Append('schemas["%s"] = %s;' % (namespace
.name
,
298 _FormatNameAsConstant(namespace
.name
)))
301 c
.Append('std::map<std::string, const char*> schemas;')
304 c
.Append('base::LazyInstance<Static> g_lazy_instance;')
306 c
.Append('// static')
307 c
.Sblock('base::StringPiece GeneratedSchemas::Get('
308 'const std::string& name) {')
309 c
.Append('return IsGenerated(name) ? '
310 'g_lazy_instance.Get().schemas[name] : "";')
313 c
.Append('// static')
314 c
.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {')
315 c
.Append('return g_lazy_instance.Get().schemas.count(name) > 0;')
318 c
.Concat(cpp_util
.CloseNamespace(self
._bundle
._cpp
_namespace
))