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 | |
8 from schema_util import CapitalizeFirstLetter | 7 from schema_util import CapitalizeFirstLetter |
9 from schema_util import JsFunctionNameToClassName | 8 from schema_util import JsFunctionNameToClassName |
10 | 9 |
11 import json | 10 import json |
12 import os | 11 import os |
13 import re | 12 import re |
14 | 13 |
15 # TODO(miket/asargent) - parameterize this. | 14 # TODO(miket/asargent) - parameterize this. |
16 SOURCE_BASE_PATH = 'chrome/common/extensions/api' | 15 SOURCE_BASE_PATH = 'chrome/common/extensions/api' |
17 | 16 |
(...skipping 20 matching lines...) Expand all Loading... |
38 c.Append() | 37 c.Append() |
39 c.Append('#ifndef %s' % ifndef_name) | 38 c.Append('#ifndef %s' % ifndef_name) |
40 c.Append('#define %s' % ifndef_name) | 39 c.Append('#define %s' % ifndef_name) |
41 c.Append() | 40 c.Append() |
42 c.Concat(body_code) | 41 c.Concat(body_code) |
43 c.Append() | 42 c.Append() |
44 c.Append('#endif // %s' % ifndef_name) | 43 c.Append('#endif // %s' % ifndef_name) |
45 c.Append() | 44 c.Append() |
46 return c | 45 return c |
47 | 46 |
48 def _GetPlatformIfdefs(self, model_object): | |
49 """Generates the "defined" conditional for an #if check if |model_object| | |
50 has platform restrictions. Returns None if there are no restrictions. | |
51 """ | |
52 if model_object.platforms is None: | |
53 return None | |
54 ifdefs = [] | |
55 for platform in model_object.platforms: | |
56 if platform == Platforms.CHROMEOS: | |
57 ifdefs.append('defined(OS_CHROMEOS)') | |
58 else: | |
59 raise ValueError("Unsupported platform ifdef: %s" % platform.name) | |
60 return ' and '.join(ifdefs) | |
61 | |
62 def GenerateAPIHeader(self): | 47 def GenerateAPIHeader(self): |
63 """Generates the header for API registration / declaration""" | 48 """Generates the header for API registration / declaration""" |
64 c = code.Code() | 49 c = code.Code() |
65 | 50 |
66 c.Append('#include <string>') | 51 c.Append('#include <string>') |
67 c.Append() | 52 c.Append() |
68 c.Append('#include "base/basictypes.h"') | 53 c.Append('#include "base/basictypes.h"') |
69 | 54 |
70 for namespace in self._model.namespaces.values(): | 55 for namespace in self._model.namespaces.values(): |
71 ifdefs = self._GetPlatformIfdefs(namespace) | |
72 if ifdefs is not None: | |
73 c.Append("#if %s" % ifdefs, indent_level=0) | |
74 | |
75 namespace_name = namespace.unix_name.replace("experimental_", "") | 56 namespace_name = namespace.unix_name.replace("experimental_", "") |
76 implementation_header = namespace.compiler_options.get( | 57 c.Append('#include "chrome/browser/extensions/api/%s/%s_api.h"' % ( |
77 "implemented_in", | 58 namespace_name, namespace_name)) |
78 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, | |
79 namespace_name)) | |
80 c.Append('#include "%s"' % implementation_header) | |
81 | |
82 if ifdefs is not None: | |
83 c.Append("#endif // %s" % ifdefs, indent_level=0) | |
84 | 59 |
85 c.Append() | 60 c.Append() |
86 c.Append("class ExtensionFunctionRegistry;") | 61 c.Append("class ExtensionFunctionRegistry;") |
87 c.Append() | 62 c.Append() |
88 | 63 |
89 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | 64 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) |
90 for namespace in self._model.namespaces.values(): | 65 for namespace in self._model.namespaces.values(): |
91 c.Append("// TODO(miket): emit code for %s" % (namespace.unix_name)) | 66 c.Append("// TODO(miket): emit code for %s" % (namespace.unix_name)) |
92 c.Append() | 67 c.Append() |
93 c.Concat(self.GenerateFunctionRegistry()) | 68 c.Concat(self.GenerateFunctionRegistry()) |
94 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 69 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
95 c.Append() | 70 c.Append() |
96 return self.GenerateHeader('generated_api', c) | 71 return self.GenerateHeader('generated_api', c) |
97 | 72 |
98 def _GetNamespaceFunctions(self, namespace): | |
99 functions = list(namespace.functions.values()) | |
100 if namespace.compiler_options.get("generate_type_functions", False): | |
101 for type_ in namespace.types.values(): | |
102 functions += list(type_.functions.values()) | |
103 return functions | |
104 | |
105 def GenerateFunctionRegistry(self): | 73 def GenerateFunctionRegistry(self): |
106 c = code.Code() | 74 c = code.Code() |
107 c.Sblock("class GeneratedFunctionRegistry {") | 75 c.Sblock("class GeneratedFunctionRegistry {") |
108 c.Append(" public:") | 76 c.Append("public:") |
109 c.Sblock("static void RegisterAll(ExtensionFunctionRegistry* registry) {") | 77 c.Sblock("static void RegisterAll(ExtensionFunctionRegistry* registry) {") |
110 for namespace in self._model.namespaces.values(): | 78 for namespace in self._model.namespaces.values(): |
111 namespace_ifdefs = self._GetPlatformIfdefs(namespace) | |
112 if namespace_ifdefs is not None: | |
113 c.Append("#if %s" % namespace_ifdefs, indent_level=0) | |
114 | |
115 namespace_name = CapitalizeFirstLetter(namespace.name.replace( | 79 namespace_name = CapitalizeFirstLetter(namespace.name.replace( |
116 "experimental.", "")) | 80 "experimental.", "")) |
117 for function in self._GetNamespaceFunctions(namespace): | 81 for function in namespace.functions.values(): |
118 if function.nocompile: | 82 if function.nocompile: |
119 continue | 83 continue |
120 function_ifdefs = self._GetPlatformIfdefs(function) | |
121 if function_ifdefs is not None: | |
122 c.Append("#if %s" % function_ifdefs, indent_level=0) | |
123 | |
124 function_name = JsFunctionNameToClassName(namespace.name, function.name) | 84 function_name = JsFunctionNameToClassName(namespace.name, function.name) |
125 c.Append("registry->RegisterFunction<%sFunction>();" % ( | 85 c.Append("registry->RegisterFunction<%sFunction>();" % ( |
126 function_name)) | 86 function_name)) |
127 | |
128 if function_ifdefs is not None: | |
129 c.Append("#endif // %s" % function_ifdefs, indent_level=0) | |
130 | |
131 if namespace_ifdefs is not None: | |
132 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) | |
133 c.Eblock("}") | 87 c.Eblock("}") |
134 c.Eblock("};") | 88 c.Eblock("};") |
135 c.Append() | 89 c.Append() |
136 return c | 90 return c |
137 | 91 |
138 def GenerateSchemasHeader(self): | 92 def GenerateSchemasHeader(self): |
139 """Generates a code.Code object for the generated schemas .h file""" | 93 """Generates a code.Code object for the generated schemas .h file""" |
140 c = code.Code() | 94 c = code.Code() |
141 c.Append('#include <map>') | 95 c.Append('#include <map>') |
142 c.Append('#include <string>') | 96 c.Append('#include <string>') |
143 c.Append(); | 97 c.Append(); |
144 c.Append('#include "base/string_piece.h"') | 98 c.Append('#include "base/string_piece.h"') |
145 c.Append() | 99 c.Append() |
146 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | 100 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) |
147 c.Append() | 101 c.Append() |
148 c.Sblock('class GeneratedSchemas {') | 102 c.Sblock('class GeneratedSchemas {') |
149 c.Append(' public:') | 103 c.Append('public:') |
150 c.Append('// Puts all API schemas in |schemas|.') | 104 c.Append('// Puts all API schemas in |schemas|.') |
151 c.Append('static void Get(' | 105 c.Append('static void Get(' |
152 'std::map<std::string, base::StringPiece>* schemas);') | 106 'std::map<std::string, base::StringPiece>* schemas);') |
153 c.Eblock('};'); | 107 c.Eblock('};'); |
154 c.Append() | 108 c.Append() |
155 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 109 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
156 c.Append() | 110 c.Append() |
157 return self.GenerateHeader('generated_schemas', c) | 111 return self.GenerateHeader('generated_schemas', c) |
158 | 112 |
159 def GenerateSchemasCC(self): | 113 def GenerateSchemasCC(self): |
(...skipping 21 matching lines...) Expand all Loading... |
181 for index, line in enumerate(lines): | 135 for index, line in enumerate(lines): |
182 line = ' "%s"' % line | 136 line = ' "%s"' % line |
183 if index == len(lines) - 1: | 137 if index == len(lines) - 1: |
184 line += ';' | 138 line += ';' |
185 c.Append(line) | 139 c.Append(line) |
186 c.Eblock('}') | 140 c.Eblock('}') |
187 c.Append() | 141 c.Append() |
188 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 142 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
189 c.Append() | 143 c.Append() |
190 return c | 144 return c |
OLD | NEW |