Skip to content

Commit

Permalink
Generate enum validation for Mojo Java bindings
Browse files Browse the repository at this point in the history
BUG=581392
TESTED='build/android/test_runner.py instrumentation --test-apk MojoTest'

Review-Url: https://codereview.chromium.org/1968373002
Cr-Commit-Position: refs/heads/master@{#394036}
  • Loading branch information
tibell authored and Commit bot committed May 17, 2016
1 parent 481a8ec commit 24920a6
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ public boolean accept(File pathname) {
if (pathname.getName().startsWith("conformance_mthd13_good_2")) {
return false;
}
// TODO(yzshen): skip enum validation tests because the feature is
// not supported in Java yet. crbug.com/581392
if (pathname.getName().indexOf("enum") != -1) {
return false;
}
return pathname.isFile() && pathname.getName().startsWith(mPrefix)
&& pathname.getName().endsWith(".data");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[dist4]message_header // num_bytes
[u4]0 // version
[u4]0 // interface ID
[u4]16 // name
[u4]0 // flags
[u4]0 // padding
[anchr]message_header

[dist4]method16_params // num_bytes
[u4]0 // version
[dist8]map_data_ptr // param0
[anchr]method16_params

[anchr]map_data_ptr
[dist4]map_data_struct_header // num_bytes
[u4]0 // version
[dist8]key_array_ptr
[dist8]value_array_ptr
[anchr]map_data_struct_header

[anchr]key_array_ptr
[dist4]key_array_member // num_bytes
[u4]2 // num_elements
[u4]0x5678 // Unknown value is not allowed for non-extensible
// enum.
[u4]1
[anchr]key_array_member

[anchr]value_array_ptr
[dist4]value_array_member // num_bytes
[u4]2 // num_elements
[u4]0x5678 // Unknown value is allowed for extensible enum.
[u4]1
[anchr]value_array_member
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VALIDATION_ERROR_UNKNOWN_ENUM_VALUE
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ interface ConformanceTestInterface {
Method13(InterfaceA? param0, uint32 param1, InterfaceA? param2);
Method14(EnumA param0, EnumB param1);
Method15(array<EnumA>? param0, array<EnumB>? param1);
Method16(map<EnumA, EnumB>? param0);
};

struct BasicStruct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ if (decoder{{level+1}} == null) {
{{variable}} = {{kind|java_type}}.decode(decoder{{level}}, {{offset}});
{% else %}
{{variable}} = decoder{{level}}.{{kind|decode_method(offset, bit)}};
{% if kind|is_array_kind and kind.kind|is_enum_kind %}
{% if kind|is_nullable_kind %}
if ({{variable}} != null) {
{% else %}
{
{% endif %}
for (int i{{level}} = 0; i{{level}} < {{variable}}.length; ++i{{level}}) {
{{kind.kind|java_class_for_enum}}.validate({{variable}}[i{{level}}]);
}
}
{% elif kind|is_enum_kind %}
{{kind|java_class_for_enum}}.validate({{variable}});
{% endif %}
{% endif %}
{% endmacro %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ public {{ 'static ' if not top_level }}final class {{enum|name}} {
public static final int {{field|name}} = {{enum_value(enum, field, loop.index0)}};
{% endfor %}

private static final boolean IS_EXTENSIBLE = {% if enum.extensible %}true{% else %}false{% endif %};

public static boolean isKnownValue(int value) {
switch (value) {
{%- for enum_field in enum.fields|groupby('numeric_value') %}
case {{enum_field[0]}}:
{%- endfor %}
return true;
}
return false;
}

public static void validate(int value) {
if (IS_EXTENSIBLE || isKnownValue(value))
return;

throw new DeserializationException("Invalid enum value.");
}

private {{enum|name}}() {}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
package {{package}};

import org.chromium.base.annotations.SuppressFBWarnings;
import org.chromium.mojo.bindings.DeserializationException;
6 changes: 6 additions & 0 deletions mojo/public/tools/bindings/generators/mojom_java_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ def _GetNameHierachy(kind):
elements += _GetNameHierachy(kind)
return '.'.join(elements)

@contextfilter
def GetJavaClassForEnum(context, kind):
return GetNameForKind(context, kind)

def GetBoxedJavaType(context, kind, with_generics=True):
unboxed_type = GetJavaType(context, kind, False, with_generics)
if unboxed_type in _java_primitive_to_boxed_type:
Expand Down Expand Up @@ -416,6 +420,7 @@ class Generator(generator.Generator):
'interface_response_name': GetInterfaceResponseName,
'is_array_kind': mojom.IsArrayKind,
'is_any_handle_kind': mojom.IsAnyHandleKind,
"is_enum_kind": mojom.IsEnumKind,
'is_interface_request_kind': mojom.IsInterfaceRequestKind,
'is_map_kind': mojom.IsMapKind,
'is_nullable_kind': mojom.IsNullableKind,
Expand All @@ -424,6 +429,7 @@ class Generator(generator.Generator):
'is_struct_kind': mojom.IsStructKind,
'is_union_array_kind': IsUnionArrayKind,
'is_union_kind': mojom.IsUnionKind,
'java_class_for_enum': GetJavaClassForEnum,
'java_true_false': GetJavaTrueFalse,
'java_type': GetJavaType,
'method_ordinal_name': GetMethodOrdinalName,
Expand Down

0 comments on commit 24920a6

Please sign in to comment.