def is_move_only_kind(kind): if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): return True if mojom.IsArrayKind(kind): return is_move_only_kind(kind.kind) if mojom.IsMapKind(kind): return (is_move_only_kind(kind.value_kind) or is_move_only_kind(kind.key_kind)) if mojom.IsAnyHandleOrInterfaceKind(kind): return True return False
def IsMoveOnlyKind(kind): if IsTypemappedKind(kind): if mojom.IsEnumKind(kind): return False return _current_typemap[GetFullMojomNameForKind(kind)]["move_only"] if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): return True if mojom.IsArrayKind(kind): return IsMoveOnlyKind(kind.kind) if mojom.IsMapKind(kind): return IsMoveOnlyKind(kind.value_kind) if mojom.IsAnyHandleOrInterfaceKind(kind): return True return False
def _IsMoveOnlyKind(self, kind): if self._IsTypemappedKind(kind): if mojom.IsEnumKind(kind): return False return self.typemap[self._GetFullMojomNameForKind(kind)]["move_only"] if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): return True if mojom.IsArrayKind(kind): return self._IsMoveOnlyKind(kind.kind) if mojom.IsMapKind(kind): return (self._IsMoveOnlyKind(kind.value_kind) or self._IsMoveOnlyKind(kind.key_kind)) if mojom.IsAnyHandleOrInterfaceKind(kind): return True return False
def _WriteInputParamForTracingImpl(generator, kind, cpp_parameter_name, output_context): """Generates lines of C++ to log a parameter into TracedValue |output_context.value|. Use |output_context.name| if |output_context| is of inhereted type from _OutputContext. Args: kind: {Kind} The kind of the parameter (corresponds to its C++ type). cpp_parameter_name: {string} The actual C++ variable name corresponding to the mojom parameter |parameter_name|. Can be a valid C++ expression (e.g., dereferenced variable |"(*var)"|). output_context: {_OutputContext} Represents the TracedValue* variable to be written into. Possibly also holds the mojo parameter name corresponding to |cpp_parameter_name|. Yields: {string} C++ lines of code that trace a |cpp_parameter_name| into |output_context.value|. """ def _WrapIfNullable(inner_lines): """Check if kind is nullable if so yield code to check whether it has value. Args: inner_lines: {function} Function taking single argument and returning iterable. If kind is nullable, yield from this method with |cpp_parameter_name+'.value()'| otherwise yield with the parameter |cpp_parameter_name|. Args from the surrounding method: kind cpp_parameter_name output_context.AddSingleValue """ if mojom.IsNullableKind(kind): yield 'if (%s.has_value()) {' % cpp_parameter_name for line in inner_lines(cpp_parameter_name + '.value()'): yield ' ' + line yield '} else {' yield ' ' + output_context.AddSingleValue('String', '"base::nullopt"') yield '}' else: # |yield from| is introduced in Python3.3. for line in inner_lines(cpp_parameter_name): yield line # TODO(crbug.com/1103623): Support more involved types. if mojom.IsEnumKind(kind): if generator._IsTypemappedKind(kind) or IsNativeOnlyKind(kind): yield output_context.AddSingleValue( 'Integer', 'static_cast<int>(%s)' % cpp_parameter_name) else: yield output_context.AddSingleValue( 'String', 'base::trace_event::ValueToString(%s)' % cpp_parameter_name) return if mojom.IsStringKind(kind): if generator.for_blink: # WTF::String is nullable on its own. yield output_context.AddSingleValue('String', '%s.Utf8()' % cpp_parameter_name) return # The type might be base::Optional<std::string> or std::string. for line in _WrapIfNullable(lambda cpp_parameter_name: [ output_context.AddSingleValue('String', cpp_parameter_name) ]): yield line return if kind == mojom.BOOL: yield output_context.AddSingleValue('Boolean', cpp_parameter_name) return # TODO(crbug.com/1103623): Make TracedValue support int64_t, then move to # mojom.IsIntegralKind. if kind in [mojom.INT8, mojom.UINT8, mojom.INT16, mojom.UINT16, mojom.INT32]: # Parameter is representable as 32bit int. yield output_context.AddSingleValue('Integer', cpp_parameter_name) return if kind in [mojom.UINT32, mojom.INT64, mojom.UINT64]: yield output_context.AddSingleValue( 'String', 'base::NumberToString(%s)' % cpp_parameter_name) return if mojom.IsFloatKind(kind) or mojom.IsDoubleKind(kind): yield output_context.AddSingleValue('Double', cpp_parameter_name) return if (mojom.IsStructKind(kind) and not generator._IsTypemappedKind(kind) and not IsNativeOnlyKind(kind)): yield 'if (%s.is_null()) {' % cpp_parameter_name yield ' ' + output_context.AddSingleValue('String', '"nullptr"') yield '} else {' yield ' ' + output_context.BeginContainer('Dictionary') yield ' ' + output_context.AsValueInto(cpp_parameter_name) yield ' ' + output_context.EndContainer('Dictionary') yield '}' return if mojom.IsArrayKind(kind): iterator_name = 'item' loop_body = _WriteInputParamForTracingImpl(generator=generator, kind=kind.kind, cpp_parameter_name=iterator_name, output_context=_ArrayItem( output_context.value)) loop_generator = lambda cpp_parameter_name: output_context.TraceContainer( container_type='Array', iterator_name=iterator_name, container_name=cpp_parameter_name, loop_body=loop_body) # Array might be a nullable kind. for line in _WrapIfNullable(loop_generator): yield line return def _TraceEventToString(cpp_parameter_name=cpp_parameter_name, kind=kind): return 'base::trace_event::ValueToString(%s, "<value of type %s>")' % ( cpp_parameter_name, generator._GetCppWrapperParamType(kind)) if mojom.IsMapKind(kind): iterator_name = 'item' if generator.for_blink: # WTF::HashMap<,> key_access = '.key' value_access = '.value' else: # base::flat_map<,> key_access = '.first' value_access = '.second' loop_body = _WriteInputParamForTracingImpl( generator=generator, kind=kind.value_kind, cpp_parameter_name=iterator_name + value_access, output_context=_DictionaryItemWithCopiedKey( value=output_context.value, name=_TraceEventToString(cpp_parameter_name=iterator_name + key_access, kind=kind.key_kind))) loop_generator = lambda cpp_parameter_name: output_context.TraceContainer( container_type="Dictionary", iterator_name=iterator_name, container_name=cpp_parameter_name, loop_body=loop_body) # Dictionary might be a nullable kind. for line in _WrapIfNullable(loop_generator): yield line return if (mojom.IsInterfaceRequestKind(kind) or mojom.IsAssociatedInterfaceRequestKind(kind)): yield output_context.AddSingleValue('Boolean', cpp_parameter_name + '.is_pending()') return if (mojom.IsAnyHandleOrInterfaceKind(kind) and not mojom.IsInterfaceKind(kind)): yield output_context.AddSingleValue('Boolean', cpp_parameter_name + '.is_valid()') return """ The case |mojom.IsInterfaceKind(kind)| is not covered. |mojom.IsInterfaceKind(kind) == True| for the following types: |mojo::InterfacePtrInfo|, |mojo::InterfacePtr|. There is |mojo::InterfacePtrInfo::is_valid|, but not |mojo::InterfacePtrInfo::is_bound|. There is |mojo::InterfacePtr::is_bound|, but not |mojo::InterfacePtr::is_valid|. Both |mojo::InterfacePtrInfo| and |mojo::InterfacePtr| are deprecated. """ yield output_context.AddSingleValue('String', _TraceEventToString())
def IsAnyHandleOrInterfaceField(field): return mojom.IsAnyHandleOrInterfaceKind(field.kind)