diff options
author | yzshen <yzshen@chromium.org> | 2016-03-15 07:26:22 +0900 |
---|---|---|
committer | Qijiang Fan <fqj@google.com> | 2020-06-05 02:44:02 +0900 |
commit | 6f546f5b30ebc96984b86fe48012997649ed8281 (patch) | |
tree | 6f1cc5bb8ede63f93104e163faff1bef1ccda344 /mojo/public/tools/bindings | |
parent | 499a0ffa847ed097689b8776b17a8e443b7f4bf9 (diff) | |
download | libchrome-6f546f5b30ebc96984b86fe48012997649ed8281.tar.gz |
Mojo C++ bindings: support mapping mojo string to WTF::String.
The generator now supports a "--for_blink" flag. When it is specified, the generator will map mojo string to WTF::String.
BUG=583738
Review URL: https://codereview.chromium.org/1751563002
Cr-Commit-Position: refs/heads/master@{#381091}
CrOS-Libchrome-Original-Commit: 791f9b24ca1758ced39235bda1a4e0411801309c
Diffstat (limited to 'mojo/public/tools/bindings')
8 files changed, 41 insertions, 13 deletions
diff --git a/mojo/public/tools/bindings/generators/cpp_templates/interface_declaration.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/interface_declaration.tmpl index dddbbd319e..76888c7908 100644 --- a/mojo/public/tools/bindings/generators/cpp_templates/interface_declaration.tmpl +++ b/mojo/public/tools/bindings/generators/cpp_templates/interface_declaration.tmpl @@ -53,7 +53,7 @@ class {{interface.name}} { } {%- endif %} - using {{method.name}}Callback = {{interface_macros.declare_callback(method)}}; + using {{method.name}}Callback = {{interface_macros.declare_callback(method, for_blink)}}; {%- endif %} virtual void {{method.name}}({{interface_macros.declare_request_params("", method)}}) = 0; {%- endfor %} diff --git a/mojo/public/tools/bindings/generators/cpp_templates/interface_macros.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/interface_macros.tmpl index b00b1dad5a..f6056e80e0 100644 --- a/mojo/public/tools/bindings/generators/cpp_templates/interface_macros.tmpl +++ b/mojo/public/tools/bindings/generators/cpp_templates/interface_macros.tmpl @@ -5,13 +5,13 @@ {%- endfor %} {%- endmacro %} -{%- macro declare_callback(method) -%} +{%- macro declare_callback(method, for_blink) -%} mojo::Callback<void( {%- for param in method.response_parameters -%} {#- TODO(yzshen): Previously |cpp_result_type| is used here incorrectly. The outcome is we will use mojo::String instead of const mojo::String&. Preserve the behavior temporarily. #} -{%- if not param.kind|is_string_kind -%} +{%- if (not param.kind|is_string_kind) or for_blink -%} {{param.kind|cpp_const_wrapper_type}} {%- else -%} mojo::String diff --git a/mojo/public/tools/bindings/generators/cpp_templates/module.cc.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/module.cc.tmpl index 8c9c0fb076..4d1dc90eec 100644 --- a/mojo/public/tools/bindings/generators/cpp_templates/module.cc.tmpl +++ b/mojo/public/tools/bindings/generators/cpp_templates/module.cc.tmpl @@ -36,6 +36,10 @@ #include "mojo/public/cpp/bindings/lib/validation_util.h" #include "mojo/public/interfaces/bindings/interface_control_messages.mojom.h" +{%- if for_blink %} +#include "mojo/public/cpp/bindings/lib/wtf_serialization.h" +{%- endif %} + {%- for namespace in namespaces_as_array %} namespace {{namespace}} { {%- endfor %} diff --git a/mojo/public/tools/bindings/generators/cpp_templates/module.h.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/module.h.tmpl index af93b8455c..8046116eb9 100644 --- a/mojo/public/tools/bindings/generators/cpp_templates/module.h.tmpl +++ b/mojo/public/tools/bindings/generators/cpp_templates/module.h.tmpl @@ -32,13 +32,17 @@ #include "mojo/public/cpp/bindings/map.h" #include "mojo/public/cpp/bindings/message_filter.h" #include "mojo/public/cpp/bindings/no_interface.h" -#include "mojo/public/cpp/bindings/string.h" #include "mojo/public/cpp/bindings/struct_ptr.h" #include "mojo/public/cpp/bindings/struct_traits.h" #include "{{variant_path}}-internal.h" {%- for import in imports %} #include "{{import.module.path}}.h" {%- endfor %} +{%- if not for_blink %} +#include "mojo/public/cpp/bindings/string.h" +{%- else %} +#include "third_party/WebKit/Source/wtf/text/WTFString.h" +{%- endif %} {%- for namespace in namespaces_as_array %} namespace {{namespace}} { diff --git a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py index c2d3a1c3d8..44337403ab 100644 --- a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py +++ b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py @@ -42,9 +42,10 @@ _kind_to_cpp_literal_suffix = { mojom.UINT64: "ULL", } -# TODO(rockot): Get rid of this global. This requires some refactoring of the +# TODO(rockot): Get rid of these globals. This requires some refactoring of the # generator library code so that filters can use the generator as context. _current_typemap = {} +_for_blink = False def ConstantValue(constant): @@ -56,9 +57,10 @@ def DefaultValue(field): assert field.default == "default" return "%s::New()" % GetNameForKind(field.kind) return ExpressionToText(field.default, kind=field.kind) - if (mojom.IsStringKind(field.kind) or mojom.IsArrayKind(field.kind) or - mojom.IsMapKind(field.kind)): + if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind): return "nullptr"; + if mojom.IsStringKind(field.kind): + return "" if _for_blink else "nullptr" return "" def NamespaceToArray(namespace): @@ -187,7 +189,7 @@ def GetCppArrayArgWrapperType(kind): raise Exception("Arrays of associated interface requests not yet " "supported!") if mojom.IsStringKind(kind): - return "mojo::String" + return "WTF::String" if _for_blink else "mojo::String" if mojom.IsGenericHandleKind(kind): return "mojo::ScopedHandle" if mojom.IsDataPipeConsumerKind(kind): @@ -223,7 +225,7 @@ def GetCppResultWrapperType(kind): if mojom.IsAssociatedInterfaceRequestKind(kind): return "%sAssociatedRequest" % GetNameForKind(kind.kind) if mojom.IsStringKind(kind): - return "mojo::String" + return "WTF::String" if _for_blink else "mojo::String" if mojom.IsGenericHandleKind(kind): return "mojo::ScopedHandle" if mojom.IsDataPipeConsumerKind(kind): @@ -265,7 +267,7 @@ def GetCppWrapperType(kind): if mojom.IsAssociatedInterfaceRequestKind(kind): return "%sAssociatedRequest" % GetNameForKind(kind.kind) if mojom.IsStringKind(kind): - return "mojo::String" + return "WTF::String" if _for_blink else "mojo::String" if mojom.IsGenericHandleKind(kind): return "mojo::ScopedHandle" if mojom.IsDataPipeConsumerKind(kind): @@ -301,7 +303,7 @@ def GetCppConstWrapperType(kind): if mojom.IsEnumKind(kind): return GetNameForKind(kind) if mojom.IsStringKind(kind): - return "const mojo::String&" + return "const WTF::String&" if _for_blink else "const mojo::String&" if mojom.IsGenericHandleKind(kind): return "mojo::ScopedHandle" if mojom.IsDataPipeConsumerKind(kind): @@ -525,6 +527,7 @@ class Generator(generator.Generator): "interfaces": self.GetInterfaces(), "variant": self.variant, "extra_headers": self.GetExtraHeaders(), + "for_blink": self.for_blink, } @staticmethod @@ -550,6 +553,8 @@ class Generator(generator.Generator): def GenerateFiles(self, args): global _current_typemap _current_typemap = self.typemap + global _for_blink + _for_blink = self.for_blink suffix = "-%s" % self.variant if self.variant else "" self.Write(self.GenerateModuleHeader(), self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) diff --git a/mojo/public/tools/bindings/mojom.gni b/mojo/public/tools/bindings/mojom.gni index d8923dd146..9ff1af1265 100644 --- a/mojo/public/tools/bindings/mojom.gni +++ b/mojo/public/tools/bindings/mojom.gni @@ -59,6 +59,9 @@ mojom_generator_sources = [ # testonly (optional) # # visibility (optional) +# +# for_blink (optional, C++ only) +# Use WTF types as generated type for mojo string/array/map. template("mojom") { assert( defined(invoker.sources) || defined(invoker.deps) || @@ -158,6 +161,10 @@ template("mojom") { } inputs += invoker.typemaps } + + if (defined(invoker.for_blink) && invoker.for_blink) { + args += [ "--for_blink" ] + } } } @@ -245,6 +252,9 @@ template("mojom") { full_name = get_label_info(d, "label_no_toolchain") deps += [ "${full_name}_${cpp_sources_suffix}" ] } + if (defined(invoker.for_blink) && invoker.for_blink) { + public_deps += [ "//mojo/public/cpp/bindings:wtf_support" ] + } } } diff --git a/mojo/public/tools/bindings/mojom_bindings_generator.py b/mojo/public/tools/bindings/mojom_bindings_generator.py index 18b9496daf..130e730abc 100755 --- a/mojo/public/tools/bindings/mojom_bindings_generator.py +++ b/mojo/public/tools/bindings/mojom_bindings_generator.py @@ -143,7 +143,8 @@ class MojomProcessor(object): for language, generator_module in generator_modules.iteritems(): generator = generator_module.Generator( module, args.output_dir, typemap=self._typemap.get(language, {}), - variant=args.variant, bytecode_path=args.bytecode_path) + variant=args.variant, bytecode_path=args.bytecode_path, + for_blink=args.for_blink) filtered_args = [] if hasattr(generator_module, 'GENERATOR_PREFIX'): prefix = '--' + generator_module.GENERATOR_PREFIX + '_' @@ -252,6 +253,9 @@ def main(): "the path from which to load template bytecode; to generate template " "bytecode, run %s precompile BYTECODE_PATH" % os.path.basename( sys.argv[0]))) + generate_parser.add_argument("--for_blink", action="store_true", + help="Use WTF types as generated types for mojo " + "string/array/map.") generate_parser.set_defaults(func=_Generate) precompile_parser = subparsers.add_parser("precompile", diff --git a/mojo/public/tools/bindings/pylib/mojom/generate/generator.py b/mojo/public/tools/bindings/pylib/mojom/generate/generator.py index 0ecc615c18..a97d2eac3a 100644 --- a/mojo/public/tools/bindings/pylib/mojom/generate/generator.py +++ b/mojo/public/tools/bindings/pylib/mojom/generate/generator.py @@ -37,12 +37,13 @@ class Generator(object): # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all # files to stdout. def __init__(self, module, output_dir=None, typemap=None, variant=None, - bytecode_path=None): + bytecode_path=None, for_blink=False): self.module = module self.output_dir = output_dir self.typemap = typemap or {} self.variant = variant self.bytecode_path = bytecode_path + self.for_blink = for_blink def GetStructsFromMethods(self): result = [] |