Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: tools/json_schema_compiler/cpp_bundle_generator.py

Issue 15091002: Lazily load API schemas from resource files and convert all APIs to features (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: optimize and "parent" property Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 c = code.Code() 208 c = code.Code()
209 c.Append('#include <map>') 209 c.Append('#include <map>')
210 c.Append('#include <string>') 210 c.Append('#include <string>')
211 c.Append(); 211 c.Append();
212 c.Append('#include "base/strings/string_piece.h"') 212 c.Append('#include "base/strings/string_piece.h"')
213 c.Append() 213 c.Append()
214 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 214 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
215 c.Append() 215 c.Append()
216 c.Append('class GeneratedSchemas {') 216 c.Append('class GeneratedSchemas {')
217 c.Sblock(' public:') 217 c.Sblock(' public:')
218 c.Append('// Puts all API schemas in |schemas|.') 218 c.Append('// Determines if schema named |name| is generated.')
219 c.Append('static void Get(' 219 c.Append('static bool IsGenerated(std::string name);')
220 'std::map<std::string, base::StringPiece>* schemas);') 220 c.Append('// Gets the API schema named |name|.')
not at google - send to devlin 2013/05/23 00:09:40 nit: c.Append() before this
cduvall 2013/05/24 03:13:49 Done.
221 c.Append('static base::StringPiece Get(const std::string& name);')
221 c.Eblock('};'); 222 c.Eblock('};');
222 c.Append() 223 c.Append()
223 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 224 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
224 return self._bundle._GenerateHeader('generated_schemas', c) 225 return self._bundle._GenerateHeader('generated_schemas', c)
225 226
227 def _FormatName(name):
228 return re.sub('_[a-z]',
229 lambda m: m.group(0)[1].upper(),
230 name.replace('.', '_')).capitalize()
not at google - send to devlin 2013/05/23 00:09:40 what does this do? can you use model.UnixName inst
cduvall 2013/05/24 03:13:49 Formats the name as a constant. I thought it would
not at google - send to devlin 2013/05/24 19:09:18 sgtm, thanks.
231
226 class _SchemasCCGenerator(object): 232 class _SchemasCCGenerator(object):
227 """Generates a code.Code object for the generated schemas .cc file""" 233 """Generates a code.Code object for the generated schemas .cc file"""
228 234
229 def __init__(self, cpp_bundle): 235 def __init__(self, cpp_bundle):
230 self._bundle = cpp_bundle 236 self._bundle = cpp_bundle
231 237
232 def Generate(self, namespace): 238 def Generate(self, namespace):
233 c = code.Code() 239 c = code.Code()
234 c.Append(cpp_util.CHROMIUM_LICENSE) 240 c.Append(cpp_util.CHROMIUM_LICENSE)
235 c.Append() 241 c.Append()
236 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, 242 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH,
237 'generated_schemas.h'))) 243 'generated_schemas.h')))
238 c.Append() 244 c.Append()
239 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 245 c.Append('#include "base/lazy_instance.h"')
240 c.Append() 246 c.Append()
241 c.Append('// static') 247 c.Append('namespace {')
242 c.Sblock('void GeneratedSchemas::Get('
243 'std::map<std::string, base::StringPiece>* schemas) {')
244 for api in self._bundle._api_defs: 248 for api in self._bundle._api_defs:
245 namespace = self._bundle._model.namespaces[api.get('namespace')] 249 namespace = self._bundle._model.namespaces[api.get('namespace')]
246 # JSON parsing code expects lists of schemas, so dump a singleton list. 250 # JSON parsing code expects lists of schemas, so dump a singleton list.
247 json_content = json.dumps([_RemoveDescriptions(api)], 251 json_content = json.dumps([_RemoveDescriptions(api)],
248 separators=(',', ':')) 252 separators=(',', ':'))
249 # Escape all double-quotes and backslashes. For this to output a valid 253 # Escape all double-quotes and backslashes. For this to output a valid
250 # JSON C string, we need to escape \ and ". 254 # JSON C string, we need to escape \ and ".
251 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') 255 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"')
252 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) 256 c.Append('const char k%s[] = "%s";' % (_FormatName(namespace.name),
257 json_content))
258 c.Append('}')
259 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
260 c.Append()
261 c.Sblock('struct Static {')
262 c.Sblock('Static() {')
263 for api in self._bundle._api_defs:
264 namespace = self._bundle._model.namespaces[api.get('namespace')]
265 c.Append('schemas["%s"] = k%s;' % (namespace.name,
266 _FormatName(namespace.name)))
267 c.Eblock('}');
268 c.Append()
269 c.Append('std::map<std::string, const char*> schemas;')
not at google - send to devlin 2013/05/23 00:09:40 would have preferred this whole class to be a lazy
270 c.Eblock('};');
271 c.Append()
272 c.Append('base::LazyInstance<Static> g_lazy_instance;')
273 c.Append()
274 c.Append('// static')
275 c.Sblock('base::StringPiece GeneratedSchemas::Get('
276 'const std::string& name) {')
277 c.Append('if (IsGenerated(name))')
278 c.Append(' return g_lazy_instance.Get().schemas[name];')
279 c.Append('return "";');
not at google - send to devlin 2013/05/23 00:09:40 ternary?
cduvall 2013/05/24 03:13:49 Done.
280 c.Eblock('}')
281 c.Append()
282 c.Append('// static')
283 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {')
284 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;')
253 c.Eblock('}') 285 c.Eblock('}')
254 c.Append() 286 c.Append()
255 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 287 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
256 c.Append() 288 c.Append()
257 return c 289 return c
OLDNEW
« chrome/renderer/extensions/dispatcher.cc ('K') | « chrome/renderer/extensions/v8_schema_registry.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698