#ifndef BASE_PROCESS_CURRENT_PROCESS_H_
#define BASE_PROCESS_CURRENT_PROCESS_H_
#include <atomic>
#include <string>
#include "base/base_export.h"
#include "base/no_destructor.h"
#include "base/process/process_handle.h"
#include "base/synchronization/lock.h"
#include "base/trace_event/base_tracing.h"
#include "build/buildflag.h"
namespace tracing {
class TraceEventDataSource;
class CustomEventRecorder;
void SetProcessTrackDescriptor(int64_t process_start_timestamp);
}
namespace mojo::core {
class Channel;
}
namespace base {
namespace test {
class CurrentProcessForTest;
}
using CurrentProcessType =
perfetto::protos::pbzero::ChromeProcessDescriptor::ProcessType;
enum class ShortProcessType {
kUnspecified = 0,
kBrowser = 1,
kRenderer = 2,
kUtility = 3,
kZygote = 4,
kSandboxHelper = 5,
kGpu = 6,
kPpapiPlugin = 7,
kPpapiBroker = 8,
kServiceNetwork = 9,
kServiceStorage = 10,
kService = 11,
kRendererExtension = 12,
kMaxValue = kRendererExtension,
};
class BASE_EXPORT CurrentProcess {
public:
static CurrentProcess& GetInstance();
CurrentProcess(const CurrentProcess&) = delete;
CurrentProcess& operator=(const CurrentProcess&) = delete;
~CurrentProcess();
bool operator==(const CurrentProcess& other) const;
class TypeKey {
private:
TypeKey() = default;
friend class ::base::test::CurrentProcessForTest;
friend class ::tracing::TraceEventDataSource;
friend class ::tracing::CustomEventRecorder;
friend void ::tracing::SetProcessTrackDescriptor(
int64_t process_start_timestamp);
friend class ::mojo::core::Channel;
};
CurrentProcessType GetType(TypeKey key) {
return static_cast<CurrentProcessType>(
process_type_.load(std::memory_order_relaxed));
}
ShortProcessType GetShortType(TypeKey key);
class NameKey {
private:
NameKey() = default;
friend class ::base::test::CurrentProcessForTest;
friend class ::tracing::TraceEventDataSource;
friend void ::tracing::SetProcessTrackDescriptor(
int64_t process_start_timestamp);
};
std::string GetName(NameKey key) {
AutoLock lock(lock_);
return process_name_;
}
void SetProcessType(CurrentProcessType process_type);
void SetProcessNameAndType(const std::string& process_name,
CurrentProcessType process_type);
bool IsProcessNameEmpty() const {
AutoLock lock(lock_);
return process_name_.empty();
}
private:
friend class base::NoDestructor<CurrentProcess>;
CurrentProcess() = default;
mutable Lock lock_;
std::string process_name_;
std::atomic<CurrentProcessType> process_type_;
};
}
#endif