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/wug/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 %(namespace)s { |
| 30 |
| 31 class %(export_macro)s %(class_name)s : public ::wug::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 %(namespace)s |
| 59 |
| 60 #endif // %(include_guard)s |
| 61 """ |
| 62 |
| 63 CHILD_FORWARD_DECLARATION_TEMPLATE = \ |
| 64 """namespace %(ns)s { |
| 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 %(ns)s::%(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/wug/view.h" |
| 91 %(children_includes)s |
| 92 |
| 93 namespace %(namespace)s { |
| 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 %(namespace)s |
| 123 """ |
| 124 |
| 125 CONTEXT_KEY_DEFINITION_TEMPLATE = \ |
| 126 """const char %(class_name)s::%(name)s[] = "%(value)s";""" |
| 127 |
| 128 CHILD_GETTER_DEFINITION_TEMPLATE = \ |
| 129 """%(ns)s::%(child_type)s* %(class_name)s::%(method_name)s() const { |
| 130 return static_cast<%(ns)s::%(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['namespace'] = declaration.namespace |
| 159 subs['class_name'] = declaration.view_model_class |
| 160 subs['source'] = declaration.path |
| 161 return subs |
| 162 |
| 163 def FieldNameToConstantName(field_name): |
| 164 return 'kContextKey' + util.ToUpperCamelCase(field_name) |
| 165 |
| 166 def GenContextKeysDeclarations(fields): |
| 167 return '\n'.join( |
| 168 (CONTEXT_KEY_DECLARATION_TEMPLATE % \ |
| 169 FieldNameToConstantName(f.name) for f in fields)) |
| 170 |
| 171 def GenChildrenForwardDeclarations(children): |
| 172 lines = [] |
| 173 for declaration in set(children.itervalues()): |
| 174 lines.append(CHILD_FORWARD_DECLARATION_TEMPLATE % { |
| 175 'ns': declaration.namespace, |
| 176 'child_type': declaration.view_model_class |
| 177 }) |
| 178 return '\n'.join(lines) |
| 179 |
| 180 def ChildIdToChildGetterName(id): |
| 181 return 'Get%s' % util.ToUpperCamelCase(id) |
| 182 |
| 183 def GenChildrenGettersDeclarations(children): |
| 184 lines = [] |
| 185 for id, declaration in children.iteritems(): |
| 186 lines.append(CHILD_GETTER_DECLARATION_TEMPLATE % { |
| 187 'ns': declaration.namespace, |
| 188 'child_type': declaration.view_model_class, |
| 189 'method_name': ChildIdToChildGetterName(id) |
| 190 }) |
| 191 return '\n'.join(lines) |
| 192 |
| 193 def FieldNameToGetterName(field_name): |
| 194 return 'Get%s' % util.ToUpperCamelCase(field_name) |
| 195 |
| 196 def GenContextGettersDeclarations(context_fields): |
| 197 lines = [] |
| 198 for field in context_fields: |
| 199 lines.append(CONTEXT_VALUE_GETTER_DECLARATION_TEMPLATE % { |
| 200 'type': FIELD_TYPE_TO_GETTER_TYPE[field.type], |
| 201 'method_name': FieldNameToGetterName(field.name) |
| 202 }) |
| 203 return '\n'.join(lines) |
| 204 |
| 205 def EventIdToMethodName(event): |
| 206 return 'On' + util.ToUpperCamelCase(event) |
| 207 |
| 208 def GenEventHandlersDeclarations(events): |
| 209 lines = [] |
| 210 for event in events: |
| 211 lines.append(EVENT_HANDLER_DECLARATION_TEMPLATE % |
| 212 EventIdToMethodName(event)) |
| 213 return '\n'.join(lines) |
| 214 |
| 215 def GenHFile(declaration): |
| 216 subs = GetCommonSubistitutions(declaration) |
| 217 subs['include_guard'] = \ |
| 218 util.PathToIncludeGuard(declaration.view_model_include_path) |
| 219 subs['context_keys'] = GenContextKeysDeclarations(declaration.fields) |
| 220 subs['children_forward_declarations'] = \ |
| 221 GenChildrenForwardDeclarations(declaration.children) |
| 222 subs['children_getters'] = \ |
| 223 GenChildrenGettersDeclarations(declaration.children); |
| 224 subs['context_getters'] = \ |
| 225 GenContextGettersDeclarations(declaration.fields); |
| 226 subs['event_handlers'] = GenEventHandlersDeclarations(declaration.events) |
| 227 subs['export_h_include_path'] = declaration.export_h_include_path |
| 228 subs['export_macro'] = declaration.component_export_macro |
| 229 return H_FILE_TEMPLATE % subs |
| 230 |
| 231 def GenContextKeysDefinitions(declaration): |
| 232 lines = [] |
| 233 for field in declaration.fields: |
| 234 definition = CONTEXT_KEY_DEFINITION_TEMPLATE % { |
| 235 'class_name': declaration.view_model_class, |
| 236 'name': FieldNameToConstantName(field.name), |
| 237 'value': field.id |
| 238 } |
| 239 lines.append(definition) |
| 240 return '\n'.join(lines) |
| 241 |
| 242 def GenChildrenIncludes(children): |
| 243 lines = [] |
| 244 for declaration in set(children.itervalues()): |
| 245 lines.append('#include "%s"' % declaration.view_model_include_path) |
| 246 return '\n'.join(lines) |
| 247 |
| 248 def GenContextFieldInitialization(field): |
| 249 lines = [] |
| 250 key_constant = FieldNameToConstantName(field.name) |
| 251 setter_method = 'Set' + util.ToUpperCamelCase(field.type) |
| 252 if field.type == 'string_list': |
| 253 lines.append(' {') |
| 254 lines.append(' std::vector<std::string> defaults;') |
| 255 for s in field.default_value: |
| 256 lines.append(' defaults.push_back("%s");' % s) |
| 257 lines.append(' context().%s(%s, defaults);' % |
| 258 (setter_method, key_constant)) |
| 259 lines.append(' }') |
| 260 else: |
| 261 setter = ' context().%s(%s, ' % (setter_method, key_constant) |
| 262 if field.type in ['integer', 'double']: |
| 263 setter += str(field.default_value) |
| 264 elif field.type == 'boolean': |
| 265 setter += 'true' if field.default_value else 'false' |
| 266 else: |
| 267 assert field.type == 'string' |
| 268 setter += '"%s"' % field.default_value |
| 269 setter += ");" |
| 270 lines.append(setter) |
| 271 return '\n'.join(lines) |
| 272 |
| 273 def GenChildrenGettersDefenitions(declaration): |
| 274 lines = [] |
| 275 for id, child in declaration.children.iteritems(): |
| 276 lines.append(CHILD_GETTER_DEFINITION_TEMPLATE % { |
| 277 'ns': child.namespace, |
| 278 'child_type': child.view_model_class, |
| 279 'class_name': declaration.view_model_class, |
| 280 'method_name': ChildIdToChildGetterName(id), |
| 281 'child_id': id |
| 282 }); |
| 283 return '\n'.join(lines) |
| 284 |
| 285 def GenContextGettersDefinitions(declaration): |
| 286 lines = [] |
| 287 for field in declaration.fields: |
| 288 lines.append(CONTEXT_VALUE_GETTER_DEFINITION_TEMPLATE % { |
| 289 'type': FIELD_TYPE_TO_GETTER_TYPE[field.type], |
| 290 'class_name': declaration.view_model_class, |
| 291 'method_name': FieldNameToGetterName(field.name), |
| 292 'context_getter': 'Get' + util.ToUpperCamelCase( |
| 293 field.type), |
| 294 'key_constant': FieldNameToConstantName(field.name) |
| 295 }); |
| 296 return '\n'.join(lines) |
| 297 |
| 298 def GenEventDispatcherBody(events): |
| 299 lines = [] |
| 300 for event in events: |
| 301 lines.append(DISPATCH_EVENT_TEMPLATE % { |
| 302 'event_id': util.ToLowerCamelCase(event), |
| 303 'method_name': EventIdToMethodName(event) |
| 304 }); |
| 305 return '\n'.join(lines) |
| 306 |
| 307 def GenCCFile(declaration): |
| 308 subs = GetCommonSubistitutions(declaration) |
| 309 subs['header_path'] = declaration.view_model_include_path |
| 310 subs['context_keys'] = GenContextKeysDefinitions(declaration) |
| 311 subs['children_includes'] = GenChildrenIncludes(declaration.children) |
| 312 initializations = [GenContextFieldInitialization(field) \ |
| 313 for field in declaration.fields] |
| 314 initializations.append(' base::DictionaryValue diff;'); |
| 315 initializations.append(' context().GetChangesAndReset(&diff);'); |
| 316 subs['constructor_body'] = '\n'.join(initializations) |
| 317 subs['children_getters'] = GenChildrenGettersDefenitions(declaration) |
| 318 subs['context_getters'] = GenContextGettersDefinitions(declaration) |
| 319 subs['event_dispatcher_body'] = GenEventDispatcherBody(declaration.events) |
| 320 return CC_FILE_TEMPLATE % subs |
| 321 |
| 322 def ListOutputs(declaration, destination): |
| 323 dirname = os.path.join(destination, os.path.dirname(declaration.path)) |
| 324 h_file_path = os.path.join(dirname, declaration.view_model_h_name) |
| 325 cc_file_path = os.path.join(dirname, declaration.view_model_cc_name) |
| 326 return [h_file_path, cc_file_path] |
| 327 |
| 328 def Gen(declaration, destination): |
| 329 h_file_path, cc_file_path = ListOutputs(declaration, destination) |
| 330 util.CreateDirIfNotExists(os.path.dirname(h_file_path)) |
| 331 open(h_file_path, 'w').write(GenHFile(declaration)) |
| 332 open(cc_file_path, 'w').write(GenCCFile(declaration)) |
| 333 |
OLD | NEW |