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