| 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 os | |
| 6 import datetime | |
| 7 import util | |
| 8 | |
| 9 H_FILE_TEMPLATE = \ | |
| 10 """// Copyright %(year)d The Chromium Authors. All rights reserved. | |
| 11 // Use of this source code is governed by a BSD-style license that can be | |
| 12 // found in the LICENSE file. | |
| 13 | |
| 14 // NOTE: this file is generated from "%(source)s". Do not modify directly. | |
| 15 | |
| 16 #ifndef %(include_guard)s | |
| 17 #define %(include_guard)s | |
| 18 | |
| 19 #include "base/macros.h" | |
| 20 #include "components/webui_generator/view_model.h" | |
| 21 #include "%(export_h_include_path)s" | |
| 22 | |
| 23 namespace content { | |
| 24 class BrowserContext; | |
| 25 } | |
| 26 | |
| 27 %(children_forward_declarations)s | |
| 28 | |
| 29 namespace gen { | |
| 30 | |
| 31 class %(export_macro)s %(class_name)s : public ::webui_generator::ViewModel { | |
| 32 public: | |
| 33 using FactoryFunction = %(class_name)s* (*)(content::BrowserContext*); | |
| 34 | |
| 35 %(context_keys)s | |
| 36 | |
| 37 static %(class_name)s* Create(content::BrowserContext* context); | |
| 38 static void SetFactory(FactoryFunction factory); | |
| 39 | |
| 40 %(class_name)s(); | |
| 41 | |
| 42 %(children_getters)s | |
| 43 | |
| 44 %(context_getters)s | |
| 45 | |
| 46 %(event_handlers)s | |
| 47 | |
| 48 void Initialize() override {} | |
| 49 void OnAfterChildrenReady() override {} | |
| 50 void OnViewBound() override {} | |
| 51 | |
| 52 private: | |
| 53 void OnEvent(const std::string& event) final; | |
| 54 | |
| 55 static FactoryFunction factory_function_; | |
| 56 }; | |
| 57 | |
| 58 } // namespace gen | |
| 59 | |
| 60 #endif // %(include_guard)s | |
| 61 """ | |
| 62 | |
| 63 CHILD_FORWARD_DECLARATION_TEMPLATE = \ | |
| 64 """namespace gen { | |
| 65 class %(child_type)s; | |
| 66 } | |
| 67 """ | |
| 68 CONTEXT_KEY_DECLARATION_TEMPLATE = \ | |
| 69 """ static const char %s[];""" | |
| 70 | |
| 71 CHILD_GETTER_DECLARATION_TEMPLATE = \ | |
| 72 """ virtual gen::%(child_type)s* %(method_name)s() const;"""; | |
| 73 | |
| 74 CONTEXT_VALUE_GETTER_DECLARATION_TEMPLATE = \ | |
| 75 """ %(type)s %(method_name)s() const;""" | |
| 76 | |
| 77 EVENT_HANDLER_DECLARATION_TEMPLATE = \ | |
| 78 """ virtual void %s() = 0;""" | |
| 79 | |
| 80 CC_FILE_TEMPLATE = \ | |
| 81 """// Copyright %(year)d The Chromium Authors. All rights reserved. | |
| 82 // Use of this source code is governed by a BSD-style license that can be | |
| 83 // found in the LICENSE file. | |
| 84 | |
| 85 // NOTE: this file is generated from "%(source)s". Do not modify directly. | |
| 86 | |
| 87 #include "%(header_path)s" | |
| 88 | |
| 89 #include "base/logging.h" | |
| 90 #include "components/webui_generator/view.h" | |
| 91 %(children_includes)s | |
| 92 | |
| 93 namespace gen { | |
| 94 | |
| 95 %(context_keys)s | |
| 96 | |
| 97 %(class_name)s::FactoryFunction %(class_name)s::factory_function_; | |
| 98 | |
| 99 %(class_name)s* %(class_name)s::Create(content::BrowserContext* context) { | |
| 100 CHECK(factory_function_) << "Factory function for %(class_name)s was not " | |
| 101 "set."; | |
| 102 return factory_function_(context); | |
| 103 } | |
| 104 | |
| 105 void %(class_name)s::SetFactory(FactoryFunction factory) { | |
| 106 factory_function_ = factory; | |
| 107 } | |
| 108 | |
| 109 %(class_name)s::%(class_name)s() { | |
| 110 %(constructor_body)s | |
| 111 } | |
| 112 | |
| 113 %(children_getters)s | |
| 114 | |
| 115 %(context_getters)s | |
| 116 | |
| 117 void %(class_name)s::OnEvent(const std::string& event) { | |
| 118 %(event_dispatcher_body)s | |
| 119 LOG(ERROR) << "Unknown event '" << event << "'"; | |
| 120 } | |
| 121 | |
| 122 } // namespace gen | |
| 123 """ | |
| 124 | |
| 125 CONTEXT_KEY_DEFINITION_TEMPLATE = \ | |
| 126 """const char %(class_name)s::%(name)s[] = "%(value)s";""" | |
| 127 | |
| 128 CHILD_GETTER_DEFINITION_TEMPLATE = \ | |
| 129 """gen::%(child_type)s* %(class_name)s::%(method_name)s() const { | |
| 130 return static_cast<gen::%(child_type)s*>( | |
| 131 view()->GetChild("%(child_id)s")->GetViewModel()); | |
| 132 } | |
| 133 """ | |
| 134 | |
| 135 CONTEXT_VALUE_GETTER_DEFINITION_TEMPLATE = \ | |
| 136 """%(type)s %(class_name)s::%(method_name)s() const { | |
| 137 return context().%(context_getter)s(%(key_constant)s); | |
| 138 } | |
| 139 """ | |
| 140 | |
| 141 FIELD_TYPE_TO_GETTER_TYPE = { | |
| 142 'boolean': 'bool', | |
| 143 'integer': 'int', | |
| 144 'double': 'double', | |
| 145 'string': 'std::string', | |
| 146 'string_list': 'login::StringList' | |
| 147 } | |
| 148 | |
| 149 DISPATCH_EVENT_TEMPLATE = \ | |
| 150 """ if (event == "%(event_id)s") { | |
| 151 %(method_name)s(); | |
| 152 return; | |
| 153 }""" | |
| 154 | |
| 155 def GetCommonSubistitutions(declaration): | |
| 156 subs = {} | |
| 157 subs['year'] = datetime.date.today().year | |
| 158 subs['class_name'] = declaration.view_model_class | |
| 159 subs['source'] = declaration.path | |
| 160 return subs | |
| 161 | |
| 162 def FieldNameToConstantName(field_name): | |
| 163 return 'kContextKey' + util.ToUpperCamelCase(field_name) | |
| 164 | |
| 165 def GenContextKeysDeclarations(fields): | |
| 166 return '\n'.join( | |
| 167 (CONTEXT_KEY_DECLARATION_TEMPLATE % \ | |
| 168 FieldNameToConstantName(f.name) for f in fields)) | |
| 169 | |
| 170 def GenChildrenForwardDeclarations(children): | |
| 171 lines = [] | |
| 172 for declaration in set(children.itervalues()): | |
| 173 lines.append(CHILD_FORWARD_DECLARATION_TEMPLATE % { | |
| 174 'child_type': declaration.view_model_class | |
| 175 }) | |
| 176 return '\n'.join(lines) | |
| 177 | |
| 178 def ChildIdToChildGetterName(id): | |
| 179 return 'Get%s' % util.ToUpperCamelCase(id) | |
| 180 | |
| 181 def GenChildrenGettersDeclarations(children): | |
| 182 lines = [] | |
| 183 for id, declaration in children.iteritems(): | |
| 184 lines.append(CHILD_GETTER_DECLARATION_TEMPLATE % { | |
| 185 'child_type': declaration.view_model_class, | |
| 186 'method_name': ChildIdToChildGetterName(id) | |
| 187 }) | |
| 188 return '\n'.join(lines) | |
| 189 | |
| 190 def FieldNameToGetterName(field_name): | |
| 191 return 'Get%s' % util.ToUpperCamelCase(field_name) | |
| 192 | |
| 193 def GenContextGettersDeclarations(context_fields): | |
| 194 lines = [] | |
| 195 for field in context_fields: | |
| 196 lines.append(CONTEXT_VALUE_GETTER_DECLARATION_TEMPLATE % { | |
| 197 'type': FIELD_TYPE_TO_GETTER_TYPE[field.type], | |
| 198 'method_name': FieldNameToGetterName(field.name) | |
| 199 }) | |
| 200 return '\n'.join(lines) | |
| 201 | |
| 202 def EventIdToMethodName(event): | |
| 203 return 'On' + util.ToUpperCamelCase(event) | |
| 204 | |
| 205 def GenEventHandlersDeclarations(events): | |
| 206 lines = [] | |
| 207 for event in events: | |
| 208 lines.append(EVENT_HANDLER_DECLARATION_TEMPLATE % | |
| 209 EventIdToMethodName(event)) | |
| 210 return '\n'.join(lines) | |
| 211 | |
| 212 def GenHFile(declaration): | |
| 213 subs = GetCommonSubistitutions(declaration) | |
| 214 subs['include_guard'] = \ | |
| 215 util.PathToIncludeGuard(declaration.view_model_include_path) | |
| 216 subs['context_keys'] = GenContextKeysDeclarations(declaration.fields) | |
| 217 subs['children_forward_declarations'] = \ | |
| 218 GenChildrenForwardDeclarations(declaration.children) | |
| 219 subs['children_getters'] = \ | |
| 220 GenChildrenGettersDeclarations(declaration.children); | |
| 221 subs['context_getters'] = \ | |
| 222 GenContextGettersDeclarations(declaration.fields); | |
| 223 subs['event_handlers'] = GenEventHandlersDeclarations(declaration.events) | |
| 224 subs['export_h_include_path'] = declaration.export_h_include_path | |
| 225 subs['export_macro'] = declaration.component_export_macro | |
| 226 return H_FILE_TEMPLATE % subs | |
| 227 | |
| 228 def GenContextKeysDefinitions(declaration): | |
| 229 lines = [] | |
| 230 for field in declaration.fields: | |
| 231 definition = CONTEXT_KEY_DEFINITION_TEMPLATE % { | |
| 232 'class_name': declaration.view_model_class, | |
| 233 'name': FieldNameToConstantName(field.name), | |
| 234 'value': field.id | |
| 235 } | |
| 236 lines.append(definition) | |
| 237 return '\n'.join(lines) | |
| 238 | |
| 239 def GenChildrenIncludes(children): | |
| 240 lines = [] | |
| 241 for declaration in set(children.itervalues()): | |
| 242 lines.append('#include "%s"' % declaration.view_model_include_path) | |
| 243 return '\n'.join(lines) | |
| 244 | |
| 245 def GenContextFieldInitialization(field): | |
| 246 lines = [] | |
| 247 key_constant = FieldNameToConstantName(field.name) | |
| 248 setter_method = 'Set' + util.ToUpperCamelCase(field.type) | |
| 249 if field.type == 'string_list': | |
| 250 lines.append(' {') | |
| 251 lines.append(' std::vector<std::string> defaults;') | |
| 252 for s in field.default_value: | |
| 253 lines.append(' defaults.push_back("%s");' % s) | |
| 254 lines.append(' context().%s(%s, defaults);' % | |
| 255 (setter_method, key_constant)) | |
| 256 lines.append(' }') | |
| 257 else: | |
| 258 setter = ' context().%s(%s, ' % (setter_method, key_constant) | |
| 259 if field.type in ['integer', 'double']: | |
| 260 setter += str(field.default_value) | |
| 261 elif field.type == 'boolean': | |
| 262 setter += 'true' if field.default_value else 'false' | |
| 263 else: | |
| 264 assert field.type == 'string' | |
| 265 setter += '"%s"' % field.default_value | |
| 266 setter += ");" | |
| 267 lines.append(setter) | |
| 268 return '\n'.join(lines) | |
| 269 | |
| 270 def GenChildrenGettersDefenitions(declaration): | |
| 271 lines = [] | |
| 272 for id, child in declaration.children.iteritems(): | |
| 273 lines.append(CHILD_GETTER_DEFINITION_TEMPLATE % { | |
| 274 'child_type': child.view_model_class, | |
| 275 'class_name': declaration.view_model_class, | |
| 276 'method_name': ChildIdToChildGetterName(id), | |
| 277 'child_id': id | |
| 278 }); | |
| 279 return '\n'.join(lines) | |
| 280 | |
| 281 def GenContextGettersDefinitions(declaration): | |
| 282 lines = [] | |
| 283 for field in declaration.fields: | |
| 284 lines.append(CONTEXT_VALUE_GETTER_DEFINITION_TEMPLATE % { | |
| 285 'type': FIELD_TYPE_TO_GETTER_TYPE[field.type], | |
| 286 'class_name': declaration.view_model_class, | |
| 287 'method_name': FieldNameToGetterName(field.name), | |
| 288 'context_getter': 'Get' + util.ToUpperCamelCase( | |
| 289 field.type), | |
| 290 'key_constant': FieldNameToConstantName(field.name) | |
| 291 }); | |
| 292 return '\n'.join(lines) | |
| 293 | |
| 294 def GenEventDispatcherBody(events): | |
| 295 lines = [] | |
| 296 for event in events: | |
| 297 lines.append(DISPATCH_EVENT_TEMPLATE % { | |
| 298 'event_id': util.ToLowerCamelCase(event), | |
| 299 'method_name': EventIdToMethodName(event) | |
| 300 }); | |
| 301 return '\n'.join(lines) | |
| 302 | |
| 303 def GenCCFile(declaration): | |
| 304 subs = GetCommonSubistitutions(declaration) | |
| 305 subs['header_path'] = declaration.view_model_include_path | |
| 306 subs['context_keys'] = GenContextKeysDefinitions(declaration) | |
| 307 subs['children_includes'] = GenChildrenIncludes(declaration.children) | |
| 308 initializations = [GenContextFieldInitialization(field) \ | |
| 309 for field in declaration.fields] | |
| 310 initializations.append(' base::DictionaryValue diff;'); | |
| 311 initializations.append(' context().GetChangesAndReset(&diff);'); | |
| 312 subs['constructor_body'] = '\n'.join(initializations) | |
| 313 subs['children_getters'] = GenChildrenGettersDefenitions(declaration) | |
| 314 subs['context_getters'] = GenContextGettersDefinitions(declaration) | |
| 315 subs['event_dispatcher_body'] = GenEventDispatcherBody(declaration.events) | |
| 316 return CC_FILE_TEMPLATE % subs | |
| 317 | |
| 318 def ListOutputs(declaration, destination): | |
| 319 dirname = os.path.join(destination, os.path.dirname(declaration.path)) | |
| 320 h_file_path = os.path.join(dirname, declaration.view_model_h_name) | |
| 321 cc_file_path = os.path.join(dirname, declaration.view_model_cc_name) | |
| 322 return [h_file_path, cc_file_path] | |
| 323 | |
| 324 def Gen(declaration, destination): | |
| 325 h_file_path, cc_file_path = ListOutputs(declaration, destination) | |
| 326 util.CreateDirIfNotExists(os.path.dirname(h_file_path)) | |
| 327 open(h_file_path, 'w').write(GenHFile(declaration)) | |
| 328 open(cc_file_path, 'w').write(GenCCFile(declaration)) | |
| 329 | |
| OLD | NEW |