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 %(namespace)s { | |
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: | |
Nikita (slow)
2015/02/26 14:18:20
nit: insert empty line.
dzhioev (left Google)
2015/03/02 11:02:31
Done.
| |
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 %(namespace)s | |
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 %(namespace)s { | |
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 %(namespace)s | |
102 """ | |
103 | |
104 ADD_LOCALIZED_VALUE_TEMPLATE = \ | |
105 """ builder->Add("%(string_id)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 %(child_ns)s::%(child_class)s(web_ui(), "%(child_id)s"));""" | |
113 | |
114 def GetCommonSubistitutions(declaration): | |
115 subs = {} | |
116 subs['year'] = datetime.date.today().year | |
117 subs['namespace'] = declaration.namespace | |
118 subs['class_name'] = declaration.webui_view_class | |
119 subs['source'] = declaration.path | |
120 return subs | |
121 | |
122 | |
123 def GenHFile(declaration): | |
124 subs = GetCommonSubistitutions(declaration) | |
125 subs['include_guard'] = util.PathToIncludeGuard( | |
126 declaration.webui_view_include_path) | |
127 subs['export_h_include_path'] = declaration.export_h_include_path | |
128 subs['export_macro'] = declaration.component_export_macro | |
129 return H_FILE_TEMPLATE % subs | |
130 | |
131 def GenIncludes(declaration): | |
132 lines = [] | |
133 lines.append('#include "%s"' % declaration.view_model_include_path) | |
134 children_declarations = set(declaration.children.itervalues()) | |
135 for child in children_declarations: | |
136 lines.append('#include "%s"' % child.webui_view_include_path) | |
137 return '\n'.join(lines) | |
138 | |
139 def GenAddLocalizedValuesBody(declaration): | |
140 lines = [] | |
141 resource_id_prefix = "IDS_WUG_" + declaration.type.upper() + "_" | |
142 for id in declaration.strings: | |
143 resource_id = resource_id_prefix + id.upper() | |
144 subs = { | |
145 'string_id': util.ToLowerCamelCase(id), | |
146 'resource_id': resource_id | |
147 } | |
148 lines.append(ADD_LOCALIZED_VALUE_TEMPLATE % subs) | |
149 return '\n'.join(lines) | |
150 | |
151 def GenAddResourcesBody(declaration): | |
152 lines = [] | |
153 html_path = declaration.html_view_html_include_path | |
154 lines.append(ADD_RESOURCE_TEMPLATE % { 'path': html_path, | |
155 'const': 'kHTMLDoc' }) | |
156 js_path = declaration.html_view_js_include_path | |
157 lines.append(ADD_RESOURCE_TEMPLATE % { 'path': js_path, | |
158 'const': 'kJSDoc' }) | |
159 return '\n'.join(lines) | |
160 | |
161 def GenCreateAndAddChildrenBody(children): | |
162 lines = [] | |
163 for id, declaration in children.iteritems(): | |
164 subs = { | |
165 'child_ns': declaration.namespace, | |
166 'child_class': declaration.webui_view_class, | |
167 'child_id': id | |
168 } | |
169 lines.append(CREATE_AND_ADD_CHILD_TEMPLATE % subs) | |
170 return '\n'.join(lines) | |
171 | |
172 def GenCreateViewModelBody(type, namespace): | |
173 return ' return %s::%s::Create(web_ui()->GetWebContents()->' \ | |
174 'GetBrowserContext());' % \ | |
175 (namespace, util.ToUpperCamelCase(type) + 'ViewModel') | |
176 | |
177 def EscapeStringForCLiteral(string): | |
178 return json.dumps(string)[1:][:-1] | |
179 | |
180 def GenCCFile(declaration): | |
181 subs = GetCommonSubistitutions(declaration) | |
182 subs['header_path'] = declaration.webui_view_include_path | |
183 subs['includes'] = GenIncludes(declaration) | |
184 subs['add_localized_values_body'] = \ | |
185 GenAddLocalizedValuesBody(declaration) | |
186 subs['add_resources_body'] = \ | |
187 GenAddResourcesBody(declaration) | |
188 subs['create_and_add_children_body'] = \ | |
189 GenCreateAndAddChildrenBody(declaration.children) | |
190 subs['create_view_model_body'] = \ | |
191 GenCreateViewModelBody(declaration.type, declaration.namespace) | |
192 subs['get_type_body'] = ' return "%s";' % declaration.type | |
193 subs['html_doc'] = EscapeStringForCLiteral(html_view.GenHTMLFile(declaration)) | |
194 subs['js_doc'] = EscapeStringForCLiteral(html_view.GenJSFile(declaration)) | |
195 return CC_FILE_TEMPLATE % subs | |
196 | |
197 def ListOutputs(declaration, destination): | |
198 dirname = os.path.join(destination, os.path.dirname(declaration.path)) | |
199 h_file_path = os.path.join(dirname, declaration.webui_view_h_name) | |
200 cc_file_path = os.path.join(dirname, declaration.webui_view_cc_name) | |
201 return [h_file_path, cc_file_path] | |
202 | |
203 def Gen(declaration, destination): | |
204 h_file_path, cc_file_path = ListOutputs(declaration, destination) | |
205 util.CreateDirIfNotExists(os.path.dirname(h_file_path)) | |
206 open(h_file_path, 'w').write(GenHFile(declaration)) | |
207 open(cc_file_path, 'w').write(GenCCFile(declaration)) | |
208 | |
OLD | NEW |