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