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