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