Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: mojo/public/tools/bindings/generators/mojom_cpp_generator.py

Issue 2036623002: Validate map keys in C++ Mojo bindings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/tools/bindings/generators/cpp_templates/validation_macros.tmpl ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Generates C++ source files from a mojom.Module.""" 5 """Generates C++ source files from a mojom.Module."""
6 6
7 import mojom.generate.generator as generator 7 import mojom.generate.generator as generator
8 import mojom.generate.module as mojom 8 import mojom.generate.module as mojom
9 import mojom.generate.pack as pack 9 import mojom.generate.pack as pack
10 from mojom.generate.template_expander import UseJinja 10 from mojom.generate.template_expander import UseJinja
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 return False 376 return False
377 return True 377 return True
378 378
379 def ShouldInlineUnion(union): 379 def ShouldInlineUnion(union):
380 return not any(mojom.IsMoveOnlyKind(field.kind) for field in union.fields) 380 return not any(mojom.IsMoveOnlyKind(field.kind) for field in union.fields)
381 381
382 def GetArrayValidateParamsCtorArgs(kind): 382 def GetArrayValidateParamsCtorArgs(kind):
383 if mojom.IsStringKind(kind): 383 if mojom.IsStringKind(kind):
384 expected_num_elements = 0 384 expected_num_elements = 0
385 element_is_nullable = False 385 element_is_nullable = False
386 key_validate_params = "nullptr"
386 element_validate_params = "nullptr" 387 element_validate_params = "nullptr"
387 enum_validate_func = "nullptr" 388 enum_validate_func = "nullptr"
388 elif mojom.IsMapKind(kind): 389 elif mojom.IsMapKind(kind):
389 expected_num_elements = 0 390 expected_num_elements = 0
390 element_is_nullable = mojom.IsNullableKind(kind.value_kind) 391 element_is_nullable = False
391 element_validate_params = GetNewArrayValidateParams(kind.value_kind) 392 key_validate_params = GetNewArrayValidateParams(mojom.Array(
393 kind=kind.key_kind))
394 element_validate_params = GetNewArrayValidateParams(mojom.Array(
395 kind=kind.value_kind))
392 enum_validate_func = "nullptr" 396 enum_validate_func = "nullptr"
393 else: 397 else: # mojom.IsArrayKind(kind)
394 expected_num_elements = generator.ExpectedArraySize(kind) or 0 398 expected_num_elements = generator.ExpectedArraySize(kind) or 0
395 element_is_nullable = mojom.IsNullableKind(kind.kind) 399 element_is_nullable = mojom.IsNullableKind(kind.kind)
400 key_validate_params = "nullptr"
396 element_validate_params = GetNewArrayValidateParams(kind.kind) 401 element_validate_params = GetNewArrayValidateParams(kind.kind)
397 if mojom.IsEnumKind(kind.kind): 402 if mojom.IsEnumKind(kind.kind):
398 enum_validate_func = ("%s::Validate" % 403 enum_validate_func = ("%s::Validate" %
399 GetQualifiedNameForKind(kind.kind, internal=True)) 404 GetQualifiedNameForKind(kind.kind, internal=True))
400 else: 405 else:
401 enum_validate_func = "nullptr" 406 enum_validate_func = "nullptr"
402 407
403 if enum_validate_func == "nullptr": 408 if enum_validate_func == "nullptr":
404 return "%d, %s, %s" % (expected_num_elements, 409 if key_validate_params == "nullptr":
405 "true" if element_is_nullable else "false", 410 return "%d, %s, %s" % (expected_num_elements,
406 element_validate_params) 411 "true" if element_is_nullable else "false",
412 element_validate_params)
413 else:
414 return "%s, %s" % (key_validate_params, element_validate_params)
407 else: 415 else:
408 return "%d, %s" % (expected_num_elements, enum_validate_func) 416 return "%d, %s" % (expected_num_elements, enum_validate_func)
409 417
410 def GetNewArrayValidateParams(kind): 418 def GetNewArrayValidateParams(kind):
411 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and 419 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and
412 not mojom.IsStringKind(kind)): 420 not mojom.IsStringKind(kind)):
413 return "nullptr" 421 return "nullptr"
414 422
415 return "new mojo::internal::ArrayValidateParams(%s)" % ( 423 return "new mojo::internal::ArrayValidateParams(%s)" % (
416 GetArrayValidateParamsCtorArgs(kind)) 424 GetArrayValidateParamsCtorArgs(kind))
417 425
418 def GetMapValidateParamsCtorArgs(value_kind):
419 # Unlike GetArrayValidateParams, we are given the wrapped kind, instead of
420 # the raw array kind. So we wrap the return value of GetArrayValidateParams.
421 element_is_nullable = mojom.IsNullableKind(value_kind)
422 return "0, %s, %s" % ("true" if element_is_nullable else "false",
423 GetNewArrayValidateParams(value_kind))
424
425 class Generator(generator.Generator): 426 class Generator(generator.Generator):
426 427
427 cpp_filters = { 428 cpp_filters = {
428 "constant_value": ConstantValue, 429 "constant_value": ConstantValue,
429 "cpp_wrapper_param_type": GetCppWrapperParamType, 430 "cpp_wrapper_param_type": GetCppWrapperParamType,
430 "cpp_field_type": GetCppFieldType, 431 "cpp_field_type": GetCppFieldType,
431 "cpp_union_field_type": GetCppUnionFieldType, 432 "cpp_union_field_type": GetCppUnionFieldType,
432 "cpp_pod_type": GetCppPodType, 433 "cpp_pod_type": GetCppPodType,
433 "cpp_union_getter_return_type": GetUnionGetterReturnType, 434 "cpp_union_getter_return_type": GetUnionGetterReturnType,
434 "cpp_wrapper_type": GetCppWrapperType, 435 "cpp_wrapper_type": GetCppWrapperType,
435 "default_value": DefaultValue, 436 "default_value": DefaultValue,
436 "expression_to_text": ExpressionToText, 437 "expression_to_text": ExpressionToText,
437 "get_array_validate_params_ctor_args": GetArrayValidateParamsCtorArgs, 438 "get_array_validate_params_ctor_args": GetArrayValidateParamsCtorArgs,
438 "get_map_validate_params_ctor_args": GetMapValidateParamsCtorArgs,
439 "get_name_for_kind": GetNameForKind, 439 "get_name_for_kind": GetNameForKind,
440 "get_pad": pack.GetPad, 440 "get_pad": pack.GetPad,
441 "get_qualified_name_for_kind": GetQualifiedNameForKind, 441 "get_qualified_name_for_kind": GetQualifiedNameForKind,
442 "has_callbacks": mojom.HasCallbacks, 442 "has_callbacks": mojom.HasCallbacks,
443 "has_sync_methods": mojom.HasSyncMethods, 443 "has_sync_methods": mojom.HasSyncMethods,
444 "should_inline": ShouldInlineStruct, 444 "should_inline": ShouldInlineStruct,
445 "should_inline_union": ShouldInlineUnion, 445 "should_inline_union": ShouldInlineUnion,
446 "is_array_kind": mojom.IsArrayKind, 446 "is_array_kind": mojom.IsArrayKind,
447 "is_cloneable_kind": IsCloneableKind, 447 "is_cloneable_kind": IsCloneableKind,
448 "is_enum_kind": mojom.IsEnumKind, 448 "is_enum_kind": mojom.IsEnumKind,
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 _for_blink = self.for_blink 526 _for_blink = self.for_blink
527 global _variant 527 global _variant
528 _variant = self.variant 528 _variant = self.variant
529 suffix = "-%s" % self.variant if self.variant else "" 529 suffix = "-%s" % self.variant if self.variant else ""
530 self.Write(self.GenerateModuleHeader(), 530 self.Write(self.GenerateModuleHeader(),
531 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) 531 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix)))
532 self.Write(self.GenerateModuleInternalHeader(), 532 self.Write(self.GenerateModuleInternalHeader(),
533 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) 533 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix)))
534 self.Write(self.GenerateModuleSource(), 534 self.Write(self.GenerateModuleSource(),
535 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) 535 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix)))
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/generators/cpp_templates/validation_macros.tmpl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698