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 model import Platforms |
8 from schema_util import CapitalizeFirstLetter | 8 from schema_util import CapitalizeFirstLetter |
| 9 from schema_util import JsEventNameToHistogramValue |
9 from schema_util import JsFunctionNameToClassName | 10 from schema_util import JsFunctionNameToClassName |
10 | 11 |
11 import json | 12 import json |
12 import os | 13 import os |
13 import re | 14 import re |
14 | 15 |
15 # TODO(miket/asargent) - parameterize this. | 16 # TODO(miket/asargent) - parameterize this. |
16 SOURCE_BASE_PATH = 'chrome/common/extensions/api' | 17 SOURCE_BASE_PATH = 'chrome/common/extensions/api' |
17 | 18 |
18 def _RemoveDescriptions(node): | 19 def _RemoveDescriptions(node): |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 c.Append("#if %s" % function_ifdefs, indent_level=0) | 90 c.Append("#if %s" % function_ifdefs, indent_level=0) |
90 | 91 |
91 function_name = JsFunctionNameToClassName(namespace_name, function.name) | 92 function_name = JsFunctionNameToClassName(namespace_name, function.name) |
92 c.Append("registry->RegisterFunction<%sFunction>();" % ( | 93 c.Append("registry->RegisterFunction<%sFunction>();" % ( |
93 function_name)) | 94 function_name)) |
94 | 95 |
95 if function_ifdefs is not None: | 96 if function_ifdefs is not None: |
96 c.Append("#endif // %s" % function_ifdefs, indent_level=0) | 97 c.Append("#endif // %s" % function_ifdefs, indent_level=0) |
97 return c | 98 return c |
98 | 99 |
| 100 def _GenerateEventHistogramMapAddAll(self): |
| 101 c = code.Code() |
| 102 c.Append('// static') |
| 103 c.Append('void GeneratedEventHistogramMap::AddAll(std::map<std::string,') |
| 104 c.Sblock(' extensions::events::HistogramValue>& map) {') |
| 105 for namespace in self._model.namespaces.values(): |
| 106 for event in namespace.events.values(): |
| 107 if event.nocompile: |
| 108 continue |
| 109 event_name = "%s.%s" % (namespace.name, event.name) |
| 110 histogram_value = event.histogram_name |
| 111 if not histogram_value: |
| 112 histogram_value = JsEventNameToHistogramValue(namespace.name, |
| 113 event.name) |
| 114 c.Append('map.insert(std::make_pair("%s", extensions::events::%s));' % |
| 115 (event_name, histogram_value)) |
| 116 |
| 117 c.Eblock("}") |
| 118 return c |
| 119 |
99 def _GenerateFunctionRegistryRegisterAll(self): | 120 def _GenerateFunctionRegistryRegisterAll(self): |
100 c = code.Code() | 121 c = code.Code() |
101 c.Append('// static') | 122 c.Append('// static') |
102 c.Sblock('void GeneratedFunctionRegistry::RegisterAll(' | 123 c.Sblock('void GeneratedFunctionRegistry::RegisterAll(' |
103 'ExtensionFunctionRegistry* registry) {') | 124 'ExtensionFunctionRegistry* registry) {') |
104 for namespace in self._model.namespaces.values(): | 125 for namespace in self._model.namespaces.values(): |
105 namespace_ifdefs = self._GetPlatformIfdefs(namespace) | 126 namespace_ifdefs = self._GetPlatformIfdefs(namespace) |
106 if namespace_ifdefs is not None: | 127 if namespace_ifdefs is not None: |
107 c.Append("#if %s" % namespace_ifdefs, indent_level=0) | 128 c.Append("#if %s" % namespace_ifdefs, indent_level=0) |
108 | 129 |
(...skipping 19 matching lines...) Expand all Loading... |
128 return c | 149 return c |
129 | 150 |
130 class _APIHGenerator(object): | 151 class _APIHGenerator(object): |
131 """Generates the header for API registration / declaration""" | 152 """Generates the header for API registration / declaration""" |
132 def __init__(self, cpp_bundle): | 153 def __init__(self, cpp_bundle): |
133 self._bundle = cpp_bundle | 154 self._bundle = cpp_bundle |
134 | 155 |
135 def Generate(self, namespace): | 156 def Generate(self, namespace): |
136 c = code.Code() | 157 c = code.Code() |
137 | 158 |
| 159 c.Append('#include <map>') |
138 c.Append('#include <string>') | 160 c.Append('#include <string>') |
139 c.Append() | 161 c.Append() |
140 c.Append('#include "base/basictypes.h"') | 162 c.Append('#include "base/basictypes.h"') |
| 163 |
| 164 c.Append('#include ' |
| 165 '"chrome/browser/extensions/extension_event_histogram_value.h"') |
| 166 c.Append('#include ' |
| 167 '"chrome/browser/extensions/extension_function_registry.h"') |
141 c.Append() | 168 c.Append() |
142 c.Append("class ExtensionFunctionRegistry;") | 169 c.Append("class ExtensionFunctionRegistry;") |
143 c.Append() | 170 c.Append() |
144 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | 171 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
145 c.Append() | 172 c.Append() |
146 c.Append('class GeneratedFunctionRegistry {') | 173 c.Append('class GeneratedFunctionRegistry {') |
147 c.Sblock(' public:') | 174 c.Sblock(' public:') |
148 c.Append('static void RegisterAll(' | 175 c.Append('static void RegisterAll(' |
149 'ExtensionFunctionRegistry* registry);') | 176 'ExtensionFunctionRegistry* registry);') |
150 c.Eblock('};'); | 177 c.Eblock('};'); |
151 c.Append() | 178 c.Append() |
| 179 c.Append('class GeneratedEventHistogramMap {') |
| 180 c.Sblock(' public:') |
| 181 c.Append('static void AddAll(std::map<std::string,') |
| 182 c.Append(' extensions::events::HistogramValue>& map);') |
| 183 c.Eblock('};'); |
| 184 c.Append() |
152 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 185 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
153 return self._bundle._GenerateHeader('generated_api', c) | 186 return self._bundle._GenerateHeader('generated_api', c) |
154 | 187 |
155 class _APICCGenerator(object): | 188 class _APICCGenerator(object): |
156 """Generates a code.Code object for the generated API .cc file""" | 189 """Generates a code.Code object for the generated API .cc file""" |
157 | 190 |
158 def __init__(self, cpp_bundle): | 191 def __init__(self, cpp_bundle): |
159 self._bundle = cpp_bundle | 192 self._bundle = cpp_bundle |
160 | 193 |
161 def Generate(self, namespace): | 194 def Generate(self, namespace): |
(...skipping 26 matching lines...) Expand all Loading... |
188 if ifdefs is not None: | 221 if ifdefs is not None: |
189 c.Append("#endif // %s" % ifdefs, indent_level=0) | 222 c.Append("#endif // %s" % ifdefs, indent_level=0) |
190 c.Append() | 223 c.Append() |
191 c.Append('#include ' | 224 c.Append('#include ' |
192 '"chrome/browser/extensions/extension_function_registry.h"') | 225 '"chrome/browser/extensions/extension_function_registry.h"') |
193 c.Append() | 226 c.Append() |
194 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | 227 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
195 c.Append() | 228 c.Append() |
196 c.Concat(self._bundle._GenerateFunctionRegistryRegisterAll()) | 229 c.Concat(self._bundle._GenerateFunctionRegistryRegisterAll()) |
197 c.Append() | 230 c.Append() |
| 231 c.Concat(self._bundle._GenerateEventHistogramMapAddAll()) |
| 232 c.Append() |
198 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 233 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
199 c.Append() | 234 c.Append() |
200 return c | 235 return c |
201 | 236 |
202 class _SchemasHGenerator(object): | 237 class _SchemasHGenerator(object): |
203 """Generates a code.Code object for the generated schemas .h file""" | 238 """Generates a code.Code object for the generated schemas .h file""" |
204 def __init__(self, cpp_bundle): | 239 def __init__(self, cpp_bundle): |
205 self._bundle = cpp_bundle | 240 self._bundle = cpp_bundle |
206 | 241 |
207 def Generate(self, namespace): | 242 def Generate(self, namespace): |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 separators=(',', ':')) | 283 separators=(',', ':')) |
249 # Escape all double-quotes and backslashes. For this to output a valid | 284 # Escape all double-quotes and backslashes. For this to output a valid |
250 # JSON C string, we need to escape \ and ". | 285 # JSON C string, we need to escape \ and ". |
251 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') | 286 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') |
252 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) | 287 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) |
253 c.Eblock('}') | 288 c.Eblock('}') |
254 c.Append() | 289 c.Append() |
255 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 290 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
256 c.Append() | 291 c.Append() |
257 return c | 292 return c |
OLD | NEW |