StructTraits not being included in generated mojom

343 views
Skip to first unread message

Justin Lulejian

unread,
Mar 11, 2025, 8:46:54 PMMar 11
to chromium-mojo
Hi chromium-mojo!

I'm trying to implement a StructTraits template to ensure we do some custom mojom validation on a commonly passed mojom string. However, I'm hitting a compile-time error that is indicating a problem with how the mojom generation is occuring, but I'm not sure why. I saw a previous post that mentioned this error, but I wasn't sure how to apply the suggestions to this change.

My impl is in [Extensions] Create a mojom ExtensionId for message validation. and the error I'm getting is pasted below. Essentially it will allow us to serialize/deserialize from extensions::mojom::ExtensionId to extensions::ExtensionId (a std::string) and automatically apply some custom validation (`crx_file::id_util::IdIsValid()`) so we don't have to do that in many different message receivers. 

Would anyone be able to suggest any fixes to satisfy this error? Some thoughts and things I tried are:
  1. My interpretation of the error is that the generated mojom is not importing my new extension_id_mojom_traits.h/.cc StructTraits implementation, is that true? I followed the example of url_pattern_set_mojom_traits.h and it seems to be the same-ish...
  2. Is this because the right side of my StructTraits is a std::string (extensions::ExtensionId), which is too generic? I tried to create a custom struct instead of using extensions::ExtensionId, but got the same error...
  3. I tried including #include "mojo/public/cpp/bindings/string_traits_stl.h" so that the generated mojom knows how to deserialize std::string, but still got the same error.

Error: 

  1. stderr:
  2. In file included from gen/extensions/common/mojom/frame.mojom.cc:12:
  3. In file included from gen/extensions/common/mojom/frame.mojom.h:20:
  4. In file included from ../../mojo/public/cpp/bindings/lib/serialization.h:20:
  5. In file included from ../../mojo/public/cpp/bindings/lib/array_serialization.h:23:
  6. In file included from ../../mojo/public/cpp/bindings/array_data_view.h:13:
  7. In file included from ../../mojo/public/cpp/bindings/lib/array_internal.h:30:
  8. In file included from ../../mojo/public/cpp/bindings/lib/validation_util.h:13:
  9. In file included from ../../mojo/public/cpp/bindings/lib/serialization_util.h:14:
  10. In file included from ../../mojo/public/cpp/bindings/lib/serialization_forward.h:20:
  11. ../../mojo/public/cpp/bindings/struct_traits.h:146:17: error: static assertion failed due to requirement 'internal::AlwaysFalse<std::string>::value': Cannot find the mojo::StructTraits specialization. Did you forget to include the corresponding header file?
  12.   146 |   static_assert(internal::AlwaysFalse<T>::value,
  13.       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. gen/extensions/common/mojom/extension_id.mojom-shared.h:106:14: note: in instantiation of template class 'mojo::StructTraits<extensions::mojom::ExtensionIdDataView, std::string>' requested here
  15.   106 |     decltype(Traits::id(input)) in_id = Traits::id(input);
  16.       |              ^
  17. ../../mojo/public/cpp/bindings/lib/serialization_forward.h:52:68: note: in instantiation of member function 'mojo::internal::Serializer<extensions::mojom::ExtensionIdDataView, const std::string>::Serialize' requested here
  18.    52 |     Serializer<MojomType, std::remove_reference_t<InputUserType>>::Serialize(
  19.       |                                                                    ^
  20. gen/extensions/common/mojom/frame.mojom.cc:2262:19: note: in instantiation of function template specialization 'mojo::internal::Serialize<extensions::mojom::ExtensionIdDataView, const std::string &, mojo::internal::MessageFragment<extensions::mojom::internal::ExtensionId_Data> &>' requested here
  21.  2262 |   mojo::internal::Serialize<::extensions::mojom::ExtensionIdDataView>(
  22.       |                   ^
  23. 1 error generated.
Best regards,
Justin

Fred Shih

unread,
Mar 11, 2025, 9:16:23 PMMar 11
to chromium-mojo, Justin Lulejian
Ooops, clicked the wrong button. Adding reply to group for future readers...

Hi Justin,

I believe you are missing the DataView in your StructTrait template parameter. It should be:

template <>
class StructTraits<extensions::mojom::ExtensionIdDataView, ::extensions::ExtensionId> {
   ...
}

Sam McNally

unread,
Mar 11, 2025, 9:17:32 PMMar 11
to Justin Lulejian, chromium-mojo
I think your problem may be that you've got extension_id_mojom_traits.h #include-ing "extensions/common/mojom/extension_id.mojom.h". Generated mojom headers #include any traits headers so you end up with an include cycle. You'll want to #include "extensions/common/mojom/extension_id.mojom-shared.h" instead. One other thing that may cause trouble is your StructTraits template specialisation should use extensions::mojom::ExtensionIdDataView for the first template parameter. I'd recommend using something like https://source.chromium.org/chromium/chromium/src/+/main:url/mojom/origin_mojom_traits.h as an example of the right sort of form to follow.

--
You received this message because you are subscribed to the Google Groups "chromium-mojo" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-moj...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/chromium-mojo/e435eb05-b837-4519-ab24-c5025c7f560cn%40chromium.org.

Justin Lulejian

unread,
Mar 12, 2025, 11:08:59 AMMar 12
to Sam McNally, chromium-mojo
Thank you Fred and Sam! extensions::mojom::ExtensionIdDataView was the issue, it wasn't finding the StructTraits because mine was for the wrong source type. 

I'll also include extension_id.mojom-shared.h instead of extension_id.mojom.h to avoid any future (since it didn't cause a problem in this change?) include cycles.
--
Best regards,
Justin

Justin Lulejian | SWE - Chrome Extensions, US-REMOTE-CT | jlul...@google.com
Reply all
Reply to author
Forward
0 new messages