Passing enum values through Mojo

173 views
Skip to first unread message

Veranika Liaukevich

unread,
Apr 11, 2018, 2:54:20 PM4/11/18
to chromi...@chromium.org
Hi chromium-dev,

What is the recommended way to typemap enums that are passed through mojo and validate their values?

We have an enum, which is already defined somewhere in the code. We would like to pass it through mojo. I see several ways to do so, but I don't like any of them:
  1. Use int32 to pass the value around
    • Cons: need to validate the values in each place where it is used
  2. Duplicate the enum definition in .mojom file and use typemapping between two types and to validate the passed values.
    • Cons: duplicating enums is ugly by itself, need to ensure two definitions stay in sync
  3. Use "[Native] enum EnumType" to define typemapping
    • Seems to be to be the best possible option. But it requires using IPC_ENUM_TRAITS macros, which appears to be deprecated.
  4. Create a custom mojo structure (struct EnumType { int32 value;} ) and type-map it to the already defined enum type.
Any suggestions?

Robert Sesek

unread,
Apr 11, 2018, 3:23:35 PM4/11/18
to vera...@chromium.org, chromium-dev
Hi Veranika,

Can you move the declaration of the enum? Security Team recently updated the best practices around this: the recommendation is to, if possible, put the definition of the enum in the .mojom, and have code reference the values directly from the generated bindings. Otherwise, option (2) using mojo::EnumTraits<T,U> is the common practice, even though it does result in duplication.

- Robert

rsesek / @chromium.org

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAN1eAnce4pUfCx6Dgtj22oUDGwbZgJGNq%3DDYrtoHgvbSyzB2tQ%40mail.gmail.com.

Veranika Liaukevich

unread,
Apr 11, 2018, 3:43:23 PM4/11/18
to Robert Sesek, chromium-dev
The original enum is also used for data reporting and is defined in a .proto file. Moving the declaration to .mojom sadly wouldn't work.

Is it possible to write a unit test of sorts to ensure that duplicated enums in proto and in mojo stay in sync? Protobuf provides EnumType_MIN, EnumType_MAX and EnumType_IsValid(), which effectively allows to iterate through all defined values. Is there a similar thing in mojo?

Daniel Cheng

unread,
Apr 11, 2018, 5:16:34 PM4/11/18
to vera...@chromium.org, rse...@chromium.org, Chromium-dev
On Wed, Apr 11, 2018 at 12:43 PM Veranika Liaukevich <vera...@chromium.org> wrote:
The original enum is also used for data reporting and is defined in a .proto file. Moving the declaration to .mojom sadly wouldn't work.

Is it possible to write a unit test of sorts to ensure that duplicated enums in proto and in mojo stay in sync? Protobuf provides EnumType_MIN, EnumType_MAX and EnumType_IsValid(), which effectively allows to iterate through all defined values. Is there a similar thing in mojo?

​Mojo now emits EnumType::kMaxValue​; it doesn't emit EnumType::kMinValue currently, though doing so would be trivial. It also has a helper for determining if a given enum value is valid: https://cs.chromium.org/chromium/src/out/Debug/gen/content/common/frame.mojom-shared.h?rcl=da86d15f7497bd91008863b1c9b56d3787a6a224&l=113 for an example.

So it should be possible to make this work, though Mojo enums are scoped so 'iterating' through them involves a lot of casting.

Daniel
 

Veranika Liaukevich

unread,
Apr 11, 2018, 7:25:22 PM4/11/18
to Daniel Cheng, rse...@chromium.org, Chromium-dev
Thanks!

Is there any specific reason why approach #4 is not advisable?
Reply all
Reply to author
Forward
0 new messages