OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 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 datetime |
| 6 import json |
| 7 import os |
| 8 import os.path |
| 9 import util |
| 10 import html_view |
| 11 |
| 12 H_FILE_TEMPLATE = \ |
| 13 """// Copyright %(year)d The Chromium Authors. All rights reserved. |
| 14 // Use of this source code is governed by a BSD-style license that can be |
| 15 // found in the LICENSE file. |
| 16 |
| 17 // NOTE: this file is generated from "%(source)s". Do not modify directly. |
| 18 |
| 19 #ifndef %(include_guard)s |
| 20 #define %(include_guard)s |
| 21 |
| 22 #include "base/macros.h" |
| 23 #include "components/wug/web_ui_view.h" |
| 24 |
| 25 namespace %(namespace)s { |
| 26 |
| 27 class %(class_name)s : public ::wug::WebUIView { |
| 28 public: |
| 29 %(class_name)s(content::WebUI* web_ui); |
| 30 %(class_name)s(content::WebUI* web_ui, const std::string& id); |
| 31 protected: |
| 32 void AddLocalizedValues(::login::LocalizedValuesBuilder* builder) override; |
| 33 void AddResources(ResourcesMap* resources_map) override; |
| 34 void CreateAndAddChildren() override; |
| 35 ::wug::ViewModel* CreateViewModel() override; |
| 36 std::string GetType() override; |
| 37 |
| 38 private: |
| 39 DISALLOW_COPY_AND_ASSIGN(%(class_name)s); |
| 40 }; |
| 41 |
| 42 } // namespace %(namespace)s |
| 43 |
| 44 #endif // %(include_guard)s |
| 45 """ |
| 46 |
| 47 CC_FILE_TEMPLATE = \ |
| 48 """// Copyright %(year)d The Chromium Authors. All rights reserved. |
| 49 // Use of this source code is governed by a BSD-style license that can be |
| 50 // found in the LICENSE file. |
| 51 |
| 52 // NOTE: this file is generated from "%(source)s". Do not modify directly. |
| 53 |
| 54 #include "%(header_path)s" |
| 55 |
| 56 #include "content/public/browser/web_ui_data_source.h" |
| 57 #include "content/public/browser/web_contents.h" |
| 58 #include "components/login/localized_values_builder.h" |
| 59 #include "grit/components_strings.h" |
| 60 %(includes)s |
| 61 |
| 62 namespace { |
| 63 |
| 64 const char kHTMLDoc[] = "%(html_doc)s"; |
| 65 const char kJSDoc[] = "%(js_doc)s"; |
| 66 |
| 67 } // namespace |
| 68 |
| 69 namespace %(namespace)s { |
| 70 |
| 71 %(class_name)s::%(class_name)s(content::WebUI* web_ui) |
| 72 : wug::WebUIView(web_ui, "WUG_ROOT") { |
| 73 } |
| 74 |
| 75 %(class_name)s::%(class_name)s(content::WebUI* web_ui, const std::string& id) |
| 76 : wug::WebUIView(web_ui, id) { |
| 77 } |
| 78 |
| 79 void %(class_name)s::AddLocalizedValues( |
| 80 ::login::LocalizedValuesBuilder* builder) { |
| 81 %(add_localized_values_body)s |
| 82 } |
| 83 |
| 84 void %(class_name)s::AddResources(ResourcesMap* resources_map) { |
| 85 %(add_resources_body)s |
| 86 } |
| 87 |
| 88 void %(class_name)s::CreateAndAddChildren() { |
| 89 %(create_and_add_children_body)s |
| 90 } |
| 91 |
| 92 ::wug::ViewModel* %(class_name)s::CreateViewModel() { |
| 93 %(create_view_model_body)s |
| 94 } |
| 95 |
| 96 std::string %(class_name)s::GetType() { |
| 97 %(get_type_body)s |
| 98 } |
| 99 |
| 100 } // namespace %(namespace)s |
| 101 """ |
| 102 |
| 103 ADD_LOCALIZED_VALUE_TEMPLATE = \ |
| 104 """ builder->Add("%(string_id)s", %(resource_id)s);""" |
| 105 |
| 106 ADD_RESOURCE_TEMPLATE = \ |
| 107 """ (*resources_map)["%(path)s"] = scoped_refptr<base::RefCountedMemory>( |
| 108 new base::RefCountedStaticMemory(%(const)s, arraysize(%(const)s) - 1));""" |
| 109 |
| 110 CREATE_AND_ADD_CHILD_TEMPLATE = \ |
| 111 """ AddChild(new %(child_ns)s::%(child_class)s(web_ui(), "%(child_id)s"));""" |
| 112 |
| 113 def GetCommonSubistitutions(declaration): |
| 114 subs = {} |
| 115 subs['year'] = datetime.date.today().year |
| 116 subs['namespace'] = declaration.namespace |
| 117 subs['class_name'] = declaration.webui_view_class |
| 118 subs['source'] = declaration.path |
| 119 return subs |
| 120 |
| 121 |
| 122 def GenHFile(declaration): |
| 123 subs = GetCommonSubistitutions(declaration) |
| 124 subs['include_guard'] = util.PathToIncludeGuard( |
| 125 declaration.webui_view_include_path) |
| 126 return H_FILE_TEMPLATE % subs |
| 127 |
| 128 def GenIncludes(declaration): |
| 129 lines = [] |
| 130 lines.append('#include "%s"' % declaration.view_model_include_path) |
| 131 children_declarations = set(declaration.children.itervalues()) |
| 132 for child in children_declarations: |
| 133 lines.append('#include "%s"' % child.webui_view_include_path) |
| 134 return '\n'.join(lines) |
| 135 |
| 136 def GenAddLocalizedValuesBody(declaration): |
| 137 lines = [] |
| 138 resource_id_prefix = "IDS_WUG_" + declaration.type.upper() + "_" |
| 139 for id in declaration.strings: |
| 140 resource_id = resource_id_prefix + id.upper() |
| 141 subs = { |
| 142 'string_id': util.ToLowerCamelCase(id), |
| 143 'resource_id': resource_id |
| 144 } |
| 145 lines.append(ADD_LOCALIZED_VALUE_TEMPLATE % subs) |
| 146 return '\n'.join(lines) |
| 147 |
| 148 def GenAddResourcesBody(declaration): |
| 149 lines = [] |
| 150 html_path = declaration.html_view_html_include_path |
| 151 lines.append(ADD_RESOURCE_TEMPLATE % { 'path': html_path, |
| 152 'const': 'kHTMLDoc' }) |
| 153 js_path = declaration.html_view_js_include_path |
| 154 lines.append(ADD_RESOURCE_TEMPLATE % { 'path': js_path, |
| 155 'const': 'kJSDoc' }) |
| 156 return '\n'.join(lines) |
| 157 |
| 158 def GenCreateAndAddChildrenBody(children): |
| 159 lines = [] |
| 160 for id, declaration in children.iteritems(): |
| 161 subs = { |
| 162 'child_ns': declaration.namespace, |
| 163 'child_class': declaration.webui_view_class, |
| 164 'child_id': id |
| 165 } |
| 166 lines.append(CREATE_AND_ADD_CHILD_TEMPLATE % subs) |
| 167 return '\n'.join(lines) |
| 168 |
| 169 def GenCreateViewModelBody(type, namespace): |
| 170 return ' return %s::%s::Create(web_ui()->GetWebContents()->' \ |
| 171 'GetBrowserContext());' % \ |
| 172 (namespace, util.ToUpperCamelCase(type) + 'ViewModel') |
| 173 |
| 174 def EscapeStringForCLiteral(string): |
| 175 return json.dumps(string)[1:][:-1] |
| 176 |
| 177 def GenCCFile(declaration): |
| 178 subs = GetCommonSubistitutions(declaration) |
| 179 subs['header_path'] = declaration.webui_view_include_path |
| 180 subs['includes'] = GenIncludes(declaration) |
| 181 subs['add_localized_values_body'] = \ |
| 182 GenAddLocalizedValuesBody(declaration) |
| 183 subs['add_resources_body'] = \ |
| 184 GenAddResourcesBody(declaration) |
| 185 subs['create_and_add_children_body'] = \ |
| 186 GenCreateAndAddChildrenBody(declaration.children) |
| 187 subs['create_view_model_body'] = \ |
| 188 GenCreateViewModelBody(declaration.type, declaration.namespace) |
| 189 subs['get_type_body'] = ' return "%s";' % declaration.type |
| 190 subs['html_doc'] = EscapeStringForCLiteral(html_view.GenHTMLFile(declaration)) |
| 191 subs['js_doc'] = EscapeStringForCLiteral(html_view.GenJSFile(declaration)) |
| 192 return CC_FILE_TEMPLATE % subs |
| 193 |
| 194 def ListOutputs(declaration, destination): |
| 195 dirname = os.path.join(destination, os.path.dirname(declaration.path)) |
| 196 h_file_path = os.path.join(dirname, declaration.webui_view_h_name) |
| 197 cc_file_path = os.path.join(dirname, declaration.webui_view_cc_name) |
| 198 return [h_file_path, cc_file_path] |
| 199 |
| 200 def Gen(declaration, destination): |
| 201 h_file_path, cc_file_path = ListOutputs(declaration, destination) |
| 202 util.CreateDirIfNotExists(os.path.dirname(h_file_path)) |
| 203 open(h_file_path, 'w').write(GenHFile(declaration)) |
| 204 open(cc_file_path, 'w').write(GenCCFile(declaration)) |
| 205 |
OLD | NEW |