OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 from code import Code | 5 from code import Code |
6 from model import PropertyType | 6 from model import PropertyType |
7 import cpp_util | 7 import cpp_util |
8 import schema_util | 8 import schema_util |
9 import util_cc_helper | 9 import util_cc_helper |
10 from cpp_namespace_environment import CppNamespaceEnvironment | 10 from cpp_namespace_environment import CppNamespaceEnvironment |
(...skipping 25 matching lines...) Expand all Loading... |
36 self._namespace.environment.namespace_pattern, | 36 self._namespace.environment.namespace_pattern, |
37 self._namespace.unix_name) | 37 self._namespace.unix_name) |
38 | 38 |
39 c = Code() | 39 c = Code() |
40 (c.Append(cpp_util.CHROMIUM_LICENSE) | 40 (c.Append(cpp_util.CHROMIUM_LICENSE) |
41 .Append() | 41 .Append() |
42 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 42 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
43 .Append() | 43 .Append() |
44 .Append(self._util_cc_helper.GetIncludePath()) | 44 .Append(self._util_cc_helper.GetIncludePath()) |
45 .Append('#include "base/logging.h"') | 45 .Append('#include "base/logging.h"') |
| 46 .Append('#include "base/memory/ptr_util.h"') |
46 .Append('#include "base/strings/string_number_conversions.h"') | 47 .Append('#include "base/strings/string_number_conversions.h"') |
47 .Append('#include "base/strings/utf_string_conversions.h"') | 48 .Append('#include "base/strings/utf_string_conversions.h"') |
48 .Append('#include "%s/%s.h"' % | 49 .Append('#include "%s/%s.h"' % |
49 (self._namespace.source_file_dir, self._namespace.short_filename)) | 50 (self._namespace.source_file_dir, self._namespace.short_filename)) |
50 .Append('#include <set>') | 51 .Append('#include <set>') |
51 .Append('#include <utility>') | 52 .Append('#include <utility>') |
52 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) | 53 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) |
53 .Append() | 54 .Append() |
54 .Append('using base::UTF8ToUTF16;') | 55 .Append('using base::UTF8ToUTF16;') |
55 .Append() | 56 .Append() |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 .Concat(self._GenerateError( | 311 .Concat(self._GenerateError( |
311 '"expected dictionary, got " + ' + | 312 '"expected dictionary, got " + ' + |
312 self._util_cc_helper.GetValueTypeString('value'))) | 313 self._util_cc_helper.GetValueTypeString('value'))) |
313 .Append('return false;') | 314 .Append('return false;') |
314 .Eblock('}')) | 315 .Eblock('}')) |
315 | 316 |
316 if type_.properties or type_.additional_properties is not None: | 317 if type_.properties or type_.additional_properties is not None: |
317 c.Append('const base::DictionaryValue* dict = ' | 318 c.Append('const base::DictionaryValue* dict = ' |
318 'static_cast<const base::DictionaryValue*>(&value);') | 319 'static_cast<const base::DictionaryValue*>(&value);') |
319 if self._generate_error_messages: | 320 if self._generate_error_messages: |
320 c.Append('std::set<std::string> keys;') | 321 c.Append('std::set<std::string> keys;') |
321 for prop in type_.properties.itervalues(): | 322 for prop in type_.properties.itervalues(): |
322 c.Concat(self._InitializePropertyToDefault(prop, 'out')) | 323 c.Concat(self._InitializePropertyToDefault(prop, 'out')) |
323 for prop in type_.properties.itervalues(): | 324 for prop in type_.properties.itervalues(): |
324 if self._generate_error_messages: | 325 if self._generate_error_messages: |
325 c.Append('keys.insert("%s");' % (prop.name)) | 326 c.Append('keys.insert("%s");' % (prop.name)) |
326 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) | 327 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) |
327 # Check for extra values. | 328 # Check for extra values. |
328 if self._generate_error_messages: | 329 if self._generate_error_messages: |
329 (c.Sblock('for (base::DictionaryValue::Iterator it(*dict); ' | 330 (c.Sblock('for (base::DictionaryValue::Iterator it(*dict); ' |
330 '!it.IsAtEnd(); it.Advance()) {') | 331 '!it.IsAtEnd(); it.Advance()) {') |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 """ | 502 """ |
502 c = Code() | 503 c = Code() |
503 c.Sblock('std::unique_ptr<base::Value> %s::ToValue() const {' % | 504 c.Sblock('std::unique_ptr<base::Value> %s::ToValue() const {' % |
504 cpp_namespace) | 505 cpp_namespace) |
505 c.Append('std::unique_ptr<base::Value> result;') | 506 c.Append('std::unique_ptr<base::Value> result;') |
506 for choice in type_.choices: | 507 for choice in type_.choices: |
507 choice_var = 'as_%s' % choice.unix_name | 508 choice_var = 'as_%s' % choice.unix_name |
508 # Enums cannot be wrapped with scoped_ptr, but the XXX_NONE enum value | 509 # Enums cannot be wrapped with scoped_ptr, but the XXX_NONE enum value |
509 # is equal to 0. | 510 # is equal to 0. |
510 (c.Sblock('if (%s) {' % choice_var) | 511 (c.Sblock('if (%s) {' % choice_var) |
511 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % | 512 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % |
512 type_.unix_name) | 513 type_.unix_name).Cblock(self._CreateValueFromType( |
513 .Cblock(self._CreateValueFromType('result.reset(%s);', | 514 'result = %s;', choice.name, choice, choice_var, True)) |
514 choice.name, | 515 .Eblock('}')) |
515 choice, | |
516 choice_var, | |
517 True)) | |
518 .Eblock('}') | |
519 ) | |
520 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % | 516 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % |
521 type_.unix_name) | 517 type_.unix_name).Append('return result;').Eblock('}')) |
522 .Append('return result;') | |
523 .Eblock('}') | |
524 ) | |
525 return c | 518 return c |
526 | 519 |
527 def _GenerateFunction(self, function): | 520 def _GenerateFunction(self, function): |
528 """Generates the definitions for function structs. | 521 """Generates the definitions for function structs. |
529 """ | 522 """ |
530 c = Code() | 523 c = Code() |
531 | 524 |
532 # TODO(kalman): use function.unix_name not Classname. | 525 # TODO(kalman): use function.unix_name not Classname. |
533 function_namespace = cpp_util.Classname(function.name) | 526 function_namespace = cpp_util.Classname(function.name) |
534 # Windows has a #define for SendMessage, so to avoid any issues, we need | 527 # Windows has a #define for SendMessage, so to avoid any issues, we need |
(...skipping 28 matching lines...) Expand all Loading... |
563 (c.Append('namespace %s {' % event_namespace) | 556 (c.Append('namespace %s {' % event_namespace) |
564 .Append() | 557 .Append() |
565 .Cblock(self._GenerateEventNameConstant(event)) | 558 .Cblock(self._GenerateEventNameConstant(event)) |
566 .Cblock(self._GenerateCreateCallbackArguments(None, event)) | 559 .Cblock(self._GenerateCreateCallbackArguments(None, event)) |
567 .Append('} // namespace %s' % event_namespace) | 560 .Append('} // namespace %s' % event_namespace) |
568 ) | 561 ) |
569 return c | 562 return c |
570 | 563 |
571 def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False): | 564 def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False): |
572 """Creates a base::Value given a type. Generated code passes ownership | 565 """Creates a base::Value given a type. Generated code passes ownership |
573 to caller. | 566 to caller via std::unique_ptr. |
574 | 567 |
575 var: variable or variable* | 568 var: variable or variable* |
576 | 569 |
577 E.g for std::string, generate new base::StringValue(var) | 570 E.g for std::string, generate new base::StringValue(var) |
578 """ | 571 """ |
579 c = Code() | 572 c = Code() |
580 underlying_type = self._type_helper.FollowRef(type_) | 573 underlying_type = self._type_helper.FollowRef(type_) |
581 if underlying_type.property_type == PropertyType.ARRAY: | 574 if underlying_type.property_type == PropertyType.ARRAY: |
582 # Enums are treated specially because C++ templating thinks that they're | 575 # Enums are treated specially because C++ templating thinks that they're |
583 # ints, but really they're strings. So we create a vector of strings and | 576 # ints, but really they're strings. So we create a vector of strings and |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 """Generates the statement to create a base::Value given a type. | 610 """Generates the statement to create a base::Value given a type. |
618 | 611 |
619 type_: The type of the values being converted. | 612 type_: The type of the values being converted. |
620 var: The name of the variable. | 613 var: The name of the variable. |
621 is_ptr: Whether |type_| is optional. | 614 is_ptr: Whether |type_| is optional. |
622 """ | 615 """ |
623 underlying_type = self._type_helper.FollowRef(type_) | 616 underlying_type = self._type_helper.FollowRef(type_) |
624 if (underlying_type.property_type == PropertyType.CHOICES or | 617 if (underlying_type.property_type == PropertyType.CHOICES or |
625 underlying_type.property_type == PropertyType.OBJECT): | 618 underlying_type.property_type == PropertyType.OBJECT): |
626 if is_ptr: | 619 if is_ptr: |
627 return '(%s)->ToValue().release()' % var | 620 return '(%s)->ToValue()' % var |
628 else: | 621 else: |
629 return '(%s).ToValue().release()' % var | 622 return '(%s).ToValue()' % var |
630 elif (underlying_type.property_type == PropertyType.ANY or | 623 elif (underlying_type.property_type == PropertyType.ANY or |
631 underlying_type.property_type == PropertyType.FUNCTION): | 624 underlying_type.property_type == PropertyType.FUNCTION): |
632 if is_ptr: | 625 if is_ptr: |
633 vardot = '(%s)->' % var | 626 vardot = '(%s)->' % var |
634 else: | 627 else: |
635 vardot = '(%s).' % var | 628 vardot = '(%s).' % var |
636 return '%sDeepCopy()' % vardot | 629 return '%sCreateDeepCopy()' % vardot |
637 elif underlying_type.property_type == PropertyType.ENUM: | 630 elif underlying_type.property_type == PropertyType.ENUM: |
638 maybe_namespace = '' | 631 maybe_namespace = '' |
639 if type_.property_type == PropertyType.REF: | 632 if type_.property_type == PropertyType.REF: |
640 maybe_namespace = '%s::' % underlying_type.namespace.unix_name | 633 maybe_namespace = '%s::' % underlying_type.namespace.unix_name |
641 return 'new base::StringValue(%sToString(%s))' % (maybe_namespace, var) | 634 return 'base::WrapUnique(new base::StringValue(%sToString(%s)))' % ( |
| 635 maybe_namespace, var) |
642 elif underlying_type.property_type == PropertyType.BINARY: | 636 elif underlying_type.property_type == PropertyType.BINARY: |
643 if is_ptr: | 637 if is_ptr: |
644 vardot = var + '->' | 638 vardot = var + '->' |
645 else: | 639 else: |
646 vardot = var + '.' | 640 vardot = var + '.' |
647 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(),' | 641 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(),' |
648 ' %ssize())' % (vardot, vardot)) | 642 ' %ssize())' % (vardot, vardot)) |
649 elif underlying_type.property_type == PropertyType.ARRAY: | 643 elif underlying_type.property_type == PropertyType.ARRAY: |
650 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( | 644 return '%s' % self._util_cc_helper.CreateValueFromArray( |
651 var, | 645 var, |
652 is_ptr) | 646 is_ptr) |
653 elif underlying_type.property_type.is_fundamental: | 647 elif underlying_type.property_type.is_fundamental: |
654 if is_ptr: | 648 if is_ptr: |
655 var = '*%s' % var | 649 var = '*%s' % var |
656 if underlying_type.property_type == PropertyType.STRING: | 650 if underlying_type.property_type == PropertyType.STRING: |
657 return 'new base::StringValue(%s)' % var | 651 return 'base::WrapUnique(new base::StringValue(%s))' % var |
658 else: | 652 else: |
659 return 'new base::FundamentalValue(%s)' % var | 653 return 'base::WrapUnique(new base::FundamentalValue(%s))' % var |
660 else: | 654 else: |
661 raise NotImplementedError('Conversion of %s to base::Value not ' | 655 raise NotImplementedError('Conversion of %s to base::Value not ' |
662 'implemented' % repr(type_.type_)) | 656 'implemented' % repr(type_.type_)) |
663 | 657 |
664 def _GenerateParamsCheck(self, function, var): | 658 def _GenerateParamsCheck(self, function, var): |
665 """Generates a check for the correct number of arguments when creating | 659 """Generates a check for the correct number of arguments when creating |
666 Params. | 660 Params. |
667 """ | 661 """ |
668 c = Code() | 662 c = Code() |
669 num_required = 0 | 663 num_required = 0 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
833 .Eblock('}') | 827 .Eblock('}') |
834 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( | 828 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( |
835 ('*dictionary', '&%(dst_var)s'))) | 829 ('*dictionary', '&%(dst_var)s'))) |
836 .Append(' return %(failure_value)s;') | 830 .Append(' return %(failure_value)s;') |
837 .Append('}') | 831 .Append('}') |
838 ) | 832 ) |
839 elif underlying_type.property_type == PropertyType.FUNCTION: | 833 elif underlying_type.property_type == PropertyType.FUNCTION: |
840 if is_ptr: | 834 if is_ptr: |
841 c.Append('%(dst_var)s.reset(new base::DictionaryValue());') | 835 c.Append('%(dst_var)s.reset(new base::DictionaryValue());') |
842 elif underlying_type.property_type == PropertyType.ANY: | 836 elif underlying_type.property_type == PropertyType.ANY: |
843 c.Append('%(dst_var)s.reset(%(src_var)s->DeepCopy());') | 837 c.Append('%(dst_var)s = %(src_var)s->CreateDeepCopy();') |
844 elif underlying_type.property_type == PropertyType.ARRAY: | 838 elif underlying_type.property_type == PropertyType.ARRAY: |
845 # util_cc_helper deals with optional and required arrays | 839 # util_cc_helper deals with optional and required arrays |
846 (c.Append('const base::ListValue* list = NULL;') | 840 (c.Append('const base::ListValue* list = NULL;') |
847 .Sblock('if (!%(src_var)s->GetAsList(&list)) {') | 841 .Sblock('if (!%(src_var)s->GetAsList(&list)) {') |
848 .Concat(self._GenerateError( | 842 .Concat(self._GenerateError( |
849 '"\'%%(key)s\': expected list, got " + ' + | 843 '"\'%%(key)s\': expected list, got " + ' + |
850 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) | 844 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) |
851 ) | 845 ) |
852 if is_ptr and self._generate_error_messages: | 846 if is_ptr and self._generate_error_messages: |
853 c.Append('%(dst_var)s.reset();') | 847 c.Append('%(dst_var)s.reset();') |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1156 if self._generate_error_messages: | 1150 if self._generate_error_messages: |
1157 params = list(params) + ['base::string16* error'] | 1151 params = list(params) + ['base::string16* error'] |
1158 return ', '.join(str(p) for p in params) | 1152 return ', '.join(str(p) for p in params) |
1159 | 1153 |
1160 def _GenerateArgs(self, args): | 1154 def _GenerateArgs(self, args): |
1161 """Builds the argument list for a function, given an array of arguments. | 1155 """Builds the argument list for a function, given an array of arguments. |
1162 """ | 1156 """ |
1163 if self._generate_error_messages: | 1157 if self._generate_error_messages: |
1164 args = list(args) + ['error'] | 1158 args = list(args) + ['error'] |
1165 return ', '.join(str(a) for a in args) | 1159 return ', '.join(str(a) for a in args) |
OLD | NEW |