chromium/base/trace_event/traced_value_support.h

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_TRACE_EVENT_TRACED_VALUE_SUPPORT_H_
#define BASE_TRACE_EVENT_TRACED_VALUE_SUPPORT_H_

#include <optional>
#include <string_view>

#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_proto.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value.h"

// This file contains specialisations for trace serialisation for key
// widely-used //base classes. As these specialisations require full definition
// of perfetto::TracedValue and almost every source unit in Chromium requires
// one of these //base concepts, include specialiazations here and expose them
// to the users including trace_event.h, rather than adding a dependency from
// scoped_refptr.h et al on traced_value.h.

namespace perfetto {

// If T is serialisable into a trace, scoped_refptr<T> is serialisable as well.
TraceFormatTraits<scoped_refptr<T>, perfetto::check_traced_value_support_t<T>>;

// If T is serialisable into a trace, base::WeakPtr<T> is serialisable as well.
TraceFormatTraits< ::base::WeakPtr<T>, perfetto::check_traced_value_support_t<T>>;

// If T is serialisable into a trace, std::optional<T> is serialisable as well.
// Note that we need definitions for both std::optional<T>& and
// const std::optional<T>& (unlike scoped_refptr and WeakPtr above), as
// dereferencing const scoped_refptr<T>& gives you T, while dereferencing const
// std::optional<T>& gives you const T&.
TraceFormatTraits< ::std::optional<T>, perfetto::check_traced_value_support_t<T>>;

// If T is serialisable into a trace, raw_ptr<T> is serialisable as well.
TraceFormatTraits< ::base::raw_ptr<T, Traits>, perfetto::check_traced_value_support_t<T>>;

// If T is serialisable into a trace, raw_ref<T> is serialisable as well.
TraceFormatTraits< ::base::raw_ref<T, Traits>, perfetto::check_traced_value_support_t<T>>;

// Time-related classes.
// TODO(altimin): Make them first-class primitives in TracedValue and Perfetto
// UI.
template <>
struct TraceFormatTraits<::base::TimeDelta> {};

template <>
struct TraceFormatTraits<::base::TimeTicks> {};

template <>
struct TraceFormatTraits<::base::Time> {};

// base::UnguessableToken.
// TODO(altimin): Add first-class primitive, which will allow to show a
// human-comprehensible alias for all unguessable tokens instead.
template <>
struct TraceFormatTraits<::base::UnguessableToken> {};

// UTF-16 string support.
template <>
struct TraceFormatTraits<std::u16string> {};

TraceFormatTraits<char16_t[N]>;

template <>
struct TraceFormatTraits<const char16_t*> {};

// Wide string support.
template <>
struct TraceFormatTraits<std::wstring> {};

TraceFormatTraits<wchar_t[N]>;

template <>
struct TraceFormatTraits<const wchar_t*> {};

// std::string_view support.
template <>
struct TraceFormatTraits<::std::u16string_view> {};

template <>
struct TraceFormatTraits<::std::wstring_view> {};

}  // namespace perfetto

#endif  // BASE_TRACE_EVENT_TRACED_VALUE_SUPPORT_H_