OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 import code | |
6 import cpp_util | |
7 | |
8 import json | |
9 import os | |
10 | |
11 SOURCE_BASE_PATH = 'chrome/common/extensions/api' | |
miket_OOO
2012/03/20 21:52:16
I'd like to parameterize this eventually... would
asargent_no_longer_on_chrome
2012/03/20 23:34:07
Done.
| |
12 | |
13 class SchemaBundleGenerator(object): | |
miket_OOO
2012/03/20 21:52:16
Style: doc class purpose, please.
asargent_no_longer_on_chrome
2012/03/20 23:34:07
Done.
| |
14 | |
15 def __init__(self, model, api_defs, cpp_type_generator): | |
16 self._model = model | |
17 self._api_defs = api_defs | |
18 self._cpp_type_generator = cpp_type_generator | |
19 | |
20 def GenerateHeader(self, file_base, body_code): | |
21 """Generates a code.Code object for a header file | |
22 | |
23 Parameters: | |
24 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') | |
25 - |body_code| - the code to put in between the multiple inclusion guards""" | |
26 c = code.Code() | |
27 c.Append(cpp_util.CHROMIUM_LICENSE) | |
28 c.Append() | |
29 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH) | |
30 ifndef_name = cpp_util.GenerateIfndefName(SOURCE_BASE_PATH, file_base) | |
31 c.Append() | |
32 c.Append('#ifndef %s' % ifndef_name) | |
33 c.Append('#define %s' % ifndef_name) | |
34 c.Append('#pragma once') | |
35 c.Append() | |
36 c.Concat(body_code) | |
37 c.Append() | |
38 c.Append('#endif // %s' % ifndef_name) | |
39 c.Append() | |
40 return c | |
41 | |
42 | |
43 def GenerateAPIHeader(self): | |
44 """Generates the header for API registration / declaration""" | |
45 c = code.Code() | |
46 | |
47 c.Append('#include <string>') | |
48 c.Append() | |
49 c.Append('#include "base/basictypes.h"') | |
50 | |
51 for namespace in self._model.namespaces.values(): | |
52 namespace_name = namespace.name.replace( | |
53 "experimental.", "") | |
54 c.Append('#include "chrome/browser/extensions/api/%s/%s_api.h"' % ( | |
55 namespace_name, namespace_name)) | |
56 | |
57 c.Append() | |
58 c.Append("class ExtensionFunctionRegistry;") | |
59 c.Append() | |
60 | |
61 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | |
62 for namespace in self._model.namespaces.values(): | |
63 c.Append("// TODO(miket): emit code for %s" % (namespace.unix_name)) | |
64 c.Append() | |
65 c.Concat(self.GenerateFunctionRegistry()) | |
66 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | |
67 c.Append() | |
68 return self.GenerateHeader('generated_api', c) | |
69 | |
70 def GenerateFunctionRegistry(self): | |
71 c = code.Code() | |
72 c.Sblock("class GeneratedFunctionRegistry {") | |
73 c.Append("public:") | |
74 c.Sblock("static void RegisterAll(ExtensionFunctionRegistry* registry) {") | |
75 for namespace in self._model.namespaces.values(): | |
76 for function in namespace.functions.values(): | |
77 namespace_name = namespace.name.replace( | |
78 "experimental.", "").capitalize() | |
79 function_name = namespace_name + function.name.capitalize() | |
80 c.Append("registry->RegisterFunction<%sFunction>();" % ( | |
81 function_name)) | |
82 c.Eblock("}") | |
83 c.Eblock("};") | |
84 c.Append() | |
85 return c | |
86 | |
miket_OOO
2012/03/20 21:52:16
Stray CR
asargent_no_longer_on_chrome
2012/03/20 23:34:07
Done.
| |
87 | |
88 def GenerateSchemasHeader(self): | |
89 """Generates a code.Code object for the generated schemas .h file""" | |
90 c = code.Code() | |
91 c.Append('namespace base {') | |
92 c.Append('class ListValue;') | |
93 c.Append('}') | |
94 c.Append() | |
95 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | |
96 c.Append() | |
97 c.Sblock("class GeneratedSchemas {") | |
98 c.Append("public:") | |
99 c.Append("static base::ListValue* Get();") | |
100 c.Eblock("};"); | |
101 c.Append() | |
102 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | |
103 c.Append() | |
104 return self.GenerateHeader('generated_schemas', c) | |
105 | |
106 def GenerateSchemasCC(self): | |
107 """Generates a code.Code object for the generated schemas .cc file""" | |
108 c = code.Code() | |
109 c.Append(cpp_util.CHROMIUM_LICENSE) | |
110 c.Append() | |
111 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, | |
112 'generated_schemas.h'))) | |
113 c.Append() | |
114 c.Append('#include "base/json/json_reader.h"') | |
115 c.Append('#include "base/values.h"') | |
116 c.Append() | |
117 c.Append('using base::ListValue;') | |
118 c.Append() | |
119 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | |
120 c.Append() | |
121 c.Sblock('ListValue* GeneratedSchemas::Get() {') | |
122 c.Append('ListValue* list = new ListValue();') | |
123 for api in self._api_defs: | |
124 c.Sblock('{') | |
125 c.Sblock('const char tmp[] =') | |
126 json_content = json.dumps(api, indent=2) | |
127 json_content = json_content.replace('"', '\\"') | |
128 lines = json_content.split('\n') | |
129 for index,line in enumerate(lines): | |
130 line = '"%s"' % line | |
131 if index == len(lines) - 1: | |
132 line += ';' | |
133 c.Append(line) | |
134 c.Eblock() | |
135 c.Append('Value* val = base::JSONReader::Read(std::string(tmp), false);') | |
136 c.Append('list->Append(val);') | |
137 c.Eblock('}') | |
138 | |
139 c.Append('return list;') | |
140 c.Eblock('}') | |
141 c.Append() | |
142 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | |
143 c.Append() | |
144 return c | |
OLD | NEW |