| 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 JsFunctionNameToClassName | 9 from schema_util import JsFunctionNameToClassName |
| 10 | 10 |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 c = code.Code() | 208 c = code.Code() |
| 209 c.Append('#include <map>') | 209 c.Append('#include <map>') |
| 210 c.Append('#include <string>') | 210 c.Append('#include <string>') |
| 211 c.Append(); | 211 c.Append(); |
| 212 c.Append('#include "base/strings/string_piece.h"') | 212 c.Append('#include "base/strings/string_piece.h"') |
| 213 c.Append() | 213 c.Append() |
| 214 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | 214 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
| 215 c.Append() | 215 c.Append() |
| 216 c.Append('class GeneratedSchemas {') | 216 c.Append('class GeneratedSchemas {') |
| 217 c.Sblock(' public:') | 217 c.Sblock(' public:') |
| 218 c.Append('// Puts all API schemas in |schemas|.') | 218 c.Append('// Determines if schema named |name| is generated.') |
| 219 c.Append('static void Get(' | 219 c.Append('static bool IsGenerated(std::string name);') |
| 220 'std::map<std::string, base::StringPiece>* schemas);') | 220 c.Append() |
| 221 c.Append('// Gets the API schema named |name|.') |
| 222 c.Append('static base::StringPiece Get(const std::string& name);') |
| 221 c.Eblock('};'); | 223 c.Eblock('};'); |
| 222 c.Append() | 224 c.Append() |
| 223 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 225 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
| 224 return self._bundle._GenerateHeader('generated_schemas', c) | 226 return self._bundle._GenerateHeader('generated_schemas', c) |
| 225 | 227 |
| 228 def _FormatNameAsConstant(name): |
| 229 """Formats a name to be a C++ constant of the form kConstantName""" |
| 230 name = '%s%s' % (name[0].upper(), name[1:]) |
| 231 return 'k%s' % re.sub('_[a-z]', |
| 232 lambda m: m.group(0)[1].upper(), |
| 233 name.replace('.', '_')) |
| 234 |
| 226 class _SchemasCCGenerator(object): | 235 class _SchemasCCGenerator(object): |
| 227 """Generates a code.Code object for the generated schemas .cc file""" | 236 """Generates a code.Code object for the generated schemas .cc file""" |
| 228 | 237 |
| 229 def __init__(self, cpp_bundle): | 238 def __init__(self, cpp_bundle): |
| 230 self._bundle = cpp_bundle | 239 self._bundle = cpp_bundle |
| 231 | 240 |
| 232 def Generate(self, namespace): | 241 def Generate(self, namespace): |
| 233 c = code.Code() | 242 c = code.Code() |
| 234 c.Append(cpp_util.CHROMIUM_LICENSE) | 243 c.Append(cpp_util.CHROMIUM_LICENSE) |
| 235 c.Append() | 244 c.Append() |
| 236 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, | 245 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, |
| 237 'generated_schemas.h'))) | 246 'generated_schemas.h'))) |
| 238 c.Append() | 247 c.Append() |
| 239 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | 248 c.Append('#include "base/lazy_instance.h"') |
| 240 c.Append() | 249 c.Append() |
| 241 c.Append('// static') | 250 c.Append('namespace {') |
| 242 c.Sblock('void GeneratedSchemas::Get(' | |
| 243 'std::map<std::string, base::StringPiece>* schemas) {') | |
| 244 for api in self._bundle._api_defs: | 251 for api in self._bundle._api_defs: |
| 245 namespace = self._bundle._model.namespaces[api.get('namespace')] | 252 namespace = self._bundle._model.namespaces[api.get('namespace')] |
| 246 # JSON parsing code expects lists of schemas, so dump a singleton list. | 253 # JSON parsing code expects lists of schemas, so dump a singleton list. |
| 247 json_content = json.dumps([_RemoveDescriptions(api)], | 254 json_content = json.dumps([_RemoveDescriptions(api)], |
| 248 separators=(',', ':')) | 255 separators=(',', ':')) |
| 249 # Escape all double-quotes and backslashes. For this to output a valid | 256 # Escape all double-quotes and backslashes. For this to output a valid |
| 250 # JSON C string, we need to escape \ and ". | 257 # JSON C string, we need to escape \ and ". |
| 251 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') | 258 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') |
| 252 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) | 259 c.Append('const char %s[] = "%s";' % |
| 260 (_FormatNameAsConstant(namespace.name), json_content)) |
| 261 c.Append('}') |
| 262 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
| 263 c.Append() |
| 264 c.Sblock('struct Static {') |
| 265 c.Sblock('Static() {') |
| 266 for api in self._bundle._api_defs: |
| 267 namespace = self._bundle._model.namespaces[api.get('namespace')] |
| 268 c.Append('schemas["%s"] = %s;' % (namespace.name, |
| 269 _FormatNameAsConstant(namespace.name))) |
| 270 c.Eblock('}'); |
| 271 c.Append() |
| 272 c.Append('std::map<std::string, const char*> schemas;') |
| 273 c.Eblock('};'); |
| 274 c.Append() |
| 275 c.Append('base::LazyInstance<Static> g_lazy_instance;') |
| 276 c.Append() |
| 277 c.Append('// static') |
| 278 c.Sblock('base::StringPiece GeneratedSchemas::Get(' |
| 279 'const std::string& name) {') |
| 280 c.Append('return IsGenerated(name) ? ' |
| 281 'g_lazy_instance.Get().schemas[name] : "";') |
| 282 c.Eblock('}') |
| 283 c.Append() |
| 284 c.Append('// static') |
| 285 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {') |
| 286 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') |
| 253 c.Eblock('}') | 287 c.Eblock('}') |
| 254 c.Append() | 288 c.Append() |
| 255 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 289 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
| 256 c.Append() | 290 c.Append() |
| 257 return c | 291 return c |
| OLD | NEW |