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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: tools/json_schema_compiler/cpp_bundle_generator.py
diff --git a/tools/json_schema_compiler/cpp_bundle_generator.py b/tools/json_schema_compiler/cpp_bundle_generator.py
index 453e4466cc03a240007ce3f26c959897c03770a8..cc42a637cb2d647f209c67dd3f299adfdb37081d 100644
--- a/tools/json_schema_compiler/cpp_bundle_generator.py
+++ b/tools/json_schema_compiler/cpp_bundle_generator.py
@@ -215,14 +215,20 @@ class _SchemasHGenerator(object):
c.Append()
c.Append('class GeneratedSchemas {')
c.Sblock(' public:')
- c.Append('// Puts all API schemas in |schemas|.')
- c.Append('static void Get('
- 'std::map<std::string, base::StringPiece>* schemas);')
+ c.Append('// Determines if schema named |name| is generated.')
+ c.Append('static bool IsGenerated(std::string name);')
+ 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.
+ c.Append('static base::StringPiece Get(const std::string& name);')
c.Eblock('};');
c.Append()
c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
return self._bundle._GenerateHeader('generated_schemas', c)
+def _FormatName(name):
+ return re.sub('_[a-z]',
+ lambda m: m.group(0)[1].upper(),
+ 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.
+
class _SchemasCCGenerator(object):
"""Generates a code.Code object for the generated schemas .cc file"""
@@ -236,11 +242,9 @@ class _SchemasCCGenerator(object):
c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH,
'generated_schemas.h')))
c.Append()
- c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
+ c.Append('#include "base/lazy_instance.h"')
c.Append()
- c.Append('// static')
- c.Sblock('void GeneratedSchemas::Get('
- 'std::map<std::string, base::StringPiece>* schemas) {')
+ c.Append('namespace {')
for api in self._bundle._api_defs:
namespace = self._bundle._model.namespaces[api.get('namespace')]
# JSON parsing code expects lists of schemas, so dump a singleton list.
@@ -249,7 +253,35 @@ class _SchemasCCGenerator(object):
# Escape all double-quotes and backslashes. For this to output a valid
# JSON C string, we need to escape \ and ".
json_content = json_content.replace('\\', '\\\\').replace('"', '\\"')
- c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content))
+ c.Append('const char k%s[] = "%s";' % (_FormatName(namespace.name),
+ json_content))
+ c.Append('}')
+ c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
+ c.Append()
+ c.Sblock('struct Static {')
+ c.Sblock('Static() {')
+ for api in self._bundle._api_defs:
+ namespace = self._bundle._model.namespaces[api.get('namespace')]
+ c.Append('schemas["%s"] = k%s;' % (namespace.name,
+ _FormatName(namespace.name)))
+ c.Eblock('}');
+ c.Append()
+ 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
+ c.Eblock('};');
+ c.Append()
+ c.Append('base::LazyInstance<Static> g_lazy_instance;')
+ c.Append()
+ c.Append('// static')
+ c.Sblock('base::StringPiece GeneratedSchemas::Get('
+ 'const std::string& name) {')
+ c.Append('if (IsGenerated(name))')
+ c.Append(' return g_lazy_instance.Get().schemas[name];')
+ c.Append('return "";');
not at google - send to devlin 2013/05/23 00:09:40 ternary?
cduvall 2013/05/24 03:13:49 Done.
+ c.Eblock('}')
+ c.Append()
+ c.Append('// static')
+ c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {')
+ c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;')
c.Eblock('}')
c.Append()
c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
« 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