1*635a8641SAndroid Build Coastguard WorkerFrom 9693d3707e16f783b97dcf0a7340ded0427aa3c0 Mon Sep 17 00:00:00 2001 2*635a8641SAndroid Build Coastguard WorkerFrom: Soshun Naito <[email protected]> 3*635a8641SAndroid Build Coastguard WorkerDate: Wed, 7 Aug 2024 03:47:23 +0000 4*635a8641SAndroid Build Coastguard WorkerSubject: [PATCH] Mojo: Introduce [Uuid] attribute 5*635a8641SAndroid Build Coastguard Worker 6*635a8641SAndroid Build Coastguard WorkerThis is a cherry-pick of the CL: https://crrev.com/c/2462378 7*635a8641SAndroid Build Coastguard WorkerIt adds Uuid_ attribute to the corresponding cpp interface when we add 8*635a8641SAndroid Build Coastguard Worker[Uuid=<UUID>] to the mojo interface. 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard WorkerBug: b:357737923, b:40152372 11*635a8641SAndroid Build Coastguard WorkerTest: m libmojo 12*635a8641SAndroid Build Coastguard WorkerChange-Id: I927361da96eba66420f6c95777cba43b0055baec 13*635a8641SAndroid Build Coastguard Worker--- 14*635a8641SAndroid Build Coastguard Worker mojo/public/tools/bindings/README.md | 7 +++++++ 15*635a8641SAndroid Build Coastguard Worker .../cpp_templates/interface_declaration.tmpl | 4 ++++ 16*635a8641SAndroid Build Coastguard Worker .../cpp_templates/interface_definition.tmpl | 3 +++ 17*635a8641SAndroid Build Coastguard Worker .../bindings/generators/mojom_cpp_generator.py | 5 ++++- 18*635a8641SAndroid Build Coastguard Worker .../bindings/pylib/mojom/generate/module.py | 17 +++++++++++++++++ 19*635a8641SAndroid Build Coastguard Worker 5 files changed, 35 insertions(+), 1 deletion(-) 20*635a8641SAndroid Build Coastguard Worker 21*635a8641SAndroid Build Coastguard Workerdiff --git a/mojo/public/tools/bindings/README.md b/mojo/public/tools/bindings/README.md 22*635a8641SAndroid Build Coastguard Workerindex d1ffc448e..ce291ae0e 100644 23*635a8641SAndroid Build Coastguard Worker--- a/mojo/public/tools/bindings/README.md 24*635a8641SAndroid Build Coastguard Worker+++ b/mojo/public/tools/bindings/README.md 25*635a8641SAndroid Build Coastguard Worker@@ -395,6 +395,13 @@ interesting attributes supported today. 26*635a8641SAndroid Build Coastguard Worker field, enum value, interface method, or method parameter was introduced. 27*635a8641SAndroid Build Coastguard Worker See [Versioning](#Versioning) for more details. 28*635a8641SAndroid Build Coastguard Worker 29*635a8641SAndroid Build Coastguard Worker+**`[Uuid=<UUID>]`** 30*635a8641SAndroid Build Coastguard Worker+: Specifies a UUID to be associated with a given interface. The UUID is 31*635a8641SAndroid Build Coastguard Worker+ intended to remain stable across all changes to the interface definition, 32*635a8641SAndroid Build Coastguard Worker+ including name changes. The value given for this attribute should be a 33*635a8641SAndroid Build Coastguard Worker+ standard UUID string representation as specified by RFC 4122. New UUIDs can 34*635a8641SAndroid Build Coastguard Worker+ be generated with common tools such as `uuidgen`. 35*635a8641SAndroid Build Coastguard Worker+ 36*635a8641SAndroid Build Coastguard Worker **`[EnableIf=value]`** 37*635a8641SAndroid Build Coastguard Worker : The `EnableIf` attribute is used to conditionally enable definitions when 38*635a8641SAndroid Build Coastguard Worker the mojom is parsed. If the `mojom` target in the GN file does not include 39*635a8641SAndroid Build Coastguard Workerdiff --git a/mojo/public/tools/bindings/generators/cpp_templates/interface_declaration.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/interface_declaration.tmpl 40*635a8641SAndroid Build Coastguard Workerindex 193d380e7..bd007ab2a 100644 41*635a8641SAndroid Build Coastguard Worker--- a/mojo/public/tools/bindings/generators/cpp_templates/interface_declaration.tmpl 42*635a8641SAndroid Build Coastguard Worker+++ b/mojo/public/tools/bindings/generators/cpp_templates/interface_declaration.tmpl 43*635a8641SAndroid Build Coastguard Worker@@ -13,6 +13,10 @@ class {{export_attribute}} {{interface.name}} 44*635a8641SAndroid Build Coastguard Worker : public {{interface.name}}InterfaceBase { 45*635a8641SAndroid Build Coastguard Worker public: 46*635a8641SAndroid Build Coastguard Worker static const char Name_[]; 47*635a8641SAndroid Build Coastguard Worker+{%- if interface.uuid %} 48*635a8641SAndroid Build Coastguard Worker+ static constexpr base::Token Uuid_{ {{interface.uuid[0]}}ULL, 49*635a8641SAndroid Build Coastguard Worker+ {{interface.uuid[1]}}ULL }; 50*635a8641SAndroid Build Coastguard Worker+{%- endif %} 51*635a8641SAndroid Build Coastguard Worker static constexpr uint32_t Version_ = {{interface.version}}; 52*635a8641SAndroid Build Coastguard Worker static constexpr bool PassesAssociatedKinds_ = {% if interface|passes_associated_kinds %}true{% else %}false{% endif %}; 53*635a8641SAndroid Build Coastguard Worker static constexpr bool HasSyncMethods_ = {% if interface|has_sync_methods %}true{% else %}false{% endif %}; 54*635a8641SAndroid Build Coastguard Workerdiff --git a/mojo/public/tools/bindings/generators/cpp_templates/interface_definition.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/interface_definition.tmpl 55*635a8641SAndroid Build Coastguard Workerindex 72c3101c1..bc3200bf6 100644 56*635a8641SAndroid Build Coastguard Worker--- a/mojo/public/tools/bindings/generators/cpp_templates/interface_definition.tmpl 57*635a8641SAndroid Build Coastguard Worker+++ b/mojo/public/tools/bindings/generators/cpp_templates/interface_definition.tmpl 58*635a8641SAndroid Build Coastguard Worker@@ -32,6 +32,9 @@ std::move(p_{{param.name}}) 59*635a8641SAndroid Build Coastguard Worker 60*635a8641SAndroid Build Coastguard Worker {#--- Begin #} 61*635a8641SAndroid Build Coastguard Worker const char {{class_name}}::Name_[] = "{{namespace}}.{{class_name}}"; 62*635a8641SAndroid Build Coastguard Worker+{%- if interface.uuid %} 63*635a8641SAndroid Build Coastguard Worker+constexpr base::Token {{class_name}}::Uuid_; 64*635a8641SAndroid Build Coastguard Worker+{%- endif %} 65*635a8641SAndroid Build Coastguard Worker 66*635a8641SAndroid Build Coastguard Worker {#--- Constants #} 67*635a8641SAndroid Build Coastguard Worker {%- for constant in interface.constants %} 68*635a8641SAndroid Build Coastguard Workerdiff --git a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py 69*635a8641SAndroid Build Coastguard Workerindex 97bc827c9..b6519a80b 100644 70*635a8641SAndroid Build Coastguard Worker--- a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py 71*635a8641SAndroid Build Coastguard Worker+++ b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py 72*635a8641SAndroid Build Coastguard Worker@@ -256,16 +256,19 @@ class Generator(generator.Generator): 73*635a8641SAndroid Build Coastguard Worker return used_typemaps 74*635a8641SAndroid Build Coastguard Worker 75*635a8641SAndroid Build Coastguard Worker def _GetExtraPublicHeaders(self): 76*635a8641SAndroid Build Coastguard Worker+ headers = set() 77*635a8641SAndroid Build Coastguard Worker+ 78*635a8641SAndroid Build Coastguard Worker all_enums = list(self.module.enums) 79*635a8641SAndroid Build Coastguard Worker for struct in self.module.structs: 80*635a8641SAndroid Build Coastguard Worker all_enums.extend(struct.enums) 81*635a8641SAndroid Build Coastguard Worker for interface in self.module.interfaces: 82*635a8641SAndroid Build Coastguard Worker all_enums.extend(interface.enums) 83*635a8641SAndroid Build Coastguard Worker+ if interface.uuid: 84*635a8641SAndroid Build Coastguard Worker+ headers.add('base/token.h') 85*635a8641SAndroid Build Coastguard Worker 86*635a8641SAndroid Build Coastguard Worker types = set(self._GetFullMojomNameForKind(typename) 87*635a8641SAndroid Build Coastguard Worker for typename in 88*635a8641SAndroid Build Coastguard Worker self.module.structs + all_enums + self.module.unions) 89*635a8641SAndroid Build Coastguard Worker- headers = set() 90*635a8641SAndroid Build Coastguard Worker for typename, typemap in self.typemap.items(): 91*635a8641SAndroid Build Coastguard Worker if typename in types: 92*635a8641SAndroid Build Coastguard Worker headers.update(typemap.get("public_headers", [])) 93*635a8641SAndroid Build Coastguard Workerdiff --git a/mojo/public/tools/bindings/pylib/mojom/generate/module.py b/mojo/public/tools/bindings/pylib/mojom/generate/module.py 94*635a8641SAndroid Build Coastguard Workerindex aeeb4fce0..6a48791e5 100644 95*635a8641SAndroid Build Coastguard Worker--- a/mojo/public/tools/bindings/pylib/mojom/generate/module.py 96*635a8641SAndroid Build Coastguard Worker+++ b/mojo/public/tools/bindings/pylib/mojom/generate/module.py 97*635a8641SAndroid Build Coastguard Worker@@ -12,6 +12,8 @@ 98*635a8641SAndroid Build Coastguard Worker # method = interface.AddMethod('Tat', 0) 99*635a8641SAndroid Build Coastguard Worker # method.AddParameter('baz', 0, mojom.INT32) 100*635a8641SAndroid Build Coastguard Worker 101*635a8641SAndroid Build Coastguard Worker+from uuid import UUID 102*635a8641SAndroid Build Coastguard Worker+ 103*635a8641SAndroid Build Coastguard Worker # We use our own version of __repr__ when displaying the AST, as the 104*635a8641SAndroid Build Coastguard Worker # AST currently doesn't capture which nodes are reference (e.g. to 105*635a8641SAndroid Build Coastguard Worker # types) and which nodes are definitions. This allows us to e.g. print 106*635a8641SAndroid Build Coastguard Worker@@ -224,6 +226,7 @@ PRIMITIVES = ( 107*635a8641SAndroid Build Coastguard Worker ATTRIBUTE_MIN_VERSION = 'MinVersion' 108*635a8641SAndroid Build Coastguard Worker ATTRIBUTE_EXTENSIBLE = 'Extensible' 109*635a8641SAndroid Build Coastguard Worker ATTRIBUTE_SYNC = 'Sync' 110*635a8641SAndroid Build Coastguard Worker+ATTRIBUTE_UUID = 'Uuid' 111*635a8641SAndroid Build Coastguard Worker 112*635a8641SAndroid Build Coastguard Worker 113*635a8641SAndroid Build Coastguard Worker class NamedValue(object): 114*635a8641SAndroid Build Coastguard Worker@@ -642,6 +645,20 @@ class Interface(ReferenceKind): 115*635a8641SAndroid Build Coastguard Worker for constant in self.constants: 116*635a8641SAndroid Build Coastguard Worker constant.Stylize(stylizer) 117*635a8641SAndroid Build Coastguard Worker 118*635a8641SAndroid Build Coastguard Worker+ @property 119*635a8641SAndroid Build Coastguard Worker+ def uuid(self): 120*635a8641SAndroid Build Coastguard Worker+ uuid_str = self.attributes.get(ATTRIBUTE_UUID) if self.attributes else None 121*635a8641SAndroid Build Coastguard Worker+ if uuid_str is None: 122*635a8641SAndroid Build Coastguard Worker+ return None 123*635a8641SAndroid Build Coastguard Worker+ 124*635a8641SAndroid Build Coastguard Worker+ try: 125*635a8641SAndroid Build Coastguard Worker+ u = UUID(uuid_str) 126*635a8641SAndroid Build Coastguard Worker+ except: 127*635a8641SAndroid Build Coastguard Worker+ raise ValueError('Invalid format for Uuid attribute on interface {}. ' 128*635a8641SAndroid Build Coastguard Worker+ 'Expected standard RFC 4122 string representation of ' 129*635a8641SAndroid Build Coastguard Worker+ 'a UUID.'.format(self.mojom_name)) 130*635a8641SAndroid Build Coastguard Worker+ return (int(u.hex[:16], 16), int(u.hex[16:], 16)) 131*635a8641SAndroid Build Coastguard Worker+ 132*635a8641SAndroid Build Coastguard Worker 133*635a8641SAndroid Build Coastguard Worker class AssociatedInterface(ReferenceKind): 134*635a8641SAndroid Build Coastguard Worker ReferenceKind.AddSharedProperty('kind') 135*635a8641SAndroid Build Coastguard Worker-- 136*635a8641SAndroid Build Coastguard Worker2.46.0.rc2.264.g509ed76dc8-goog 137*635a8641SAndroid Build Coastguard Worker 138