#ifndef BASE_PROCESS_CURRENT_PROCESS_H_
#define BASE_PROCESS_CURRENT_PROCESS_H_
#include <atomic>
#include <string>
#include "base/base_export.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/process/process_handle.h"
#include "base/synchronization/lock.h"
#include "base/tracing/protos/chrome_enums.pbzero.h"
#include "build/buildflag.h"
namespace tracing {
class TraceEventDataSource;
class CustomEventRecorder;
class TrackNameRecorder;
}
namespace mojo::core {
class Channel;
}
namespace network {
class ContentDecodingInterceptor;
}
namespace base {
namespace test {
class CurrentProcessForTest;
}
#if BUILDFLAG(IS_ANDROID)
class PlatformThreadPriorityMonitor;
#endif
using CurrentProcessType = perfetto::protos::chrome_enums::pbzero::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 class ::tracing::TrackNameRecorder;
friend class ::mojo::core::Channel;
friend class ::network::ContentDecodingInterceptor;
};
CurrentProcessType GetType(TypeKey key) {
return process_type_.load(std::memory_order_relaxed);
}
ShortProcessType GetShortType(TypeKey key);
class NameKey {
private:
NameKey() = default;
#if BUILDFLAG(IS_ANDROID)
friend class ::base::PlatformThreadPriorityMonitor;
#endif
friend class ::base::test::CurrentProcessForTest;
friend class ::tracing::TraceEventDataSource;
friend class ::tracing::TrackNameRecorder;
};
std::string GetName(NameKey key) {
AutoLock lock(lock_);
return process_name_;
}
class BASE_EXPORT Delegate {
public:
virtual void OnProcessNameChanged(const std::string& process_name,
CurrentProcessType process_type) = 0;
protected:
~Delegate() = default;
};
void SetProcessType(CurrentProcessType process_type);
void SetDelegate(Delegate* delegate, NameKey key);
bool IsProcessNameEmpty() const {
AutoLock lock(lock_);
return process_name_.empty();
}
private:
friend class base::NoDestructor<CurrentProcess>;
CurrentProcess() = default;
void SetProcessNameAndType(const std::string& process_name,
CurrentProcessType process_type);
mutable Lock lock_;
std::string process_name_;
std::atomic<CurrentProcessType> process_type_;
raw_ptr<Delegate> delegate_;
};
}
#endif