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