Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_PROCESS_CURRENT_PROCESS_H_ |
| 6 | #define BASE_PROCESS_CURRENT_PROCESS_H_ |
| 7 | |
David Benjamin | dc19d42 | 2023-03-14 16:31:57 | [diff] [blame] | 8 | #include <atomic> |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 9 | #include <string> |
David Benjamin | dc19d42 | 2023-03-14 16:31:57 | [diff] [blame] | 10 | |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 11 | #include "base/base_export.h" |
Etienne Pierre-doray | 2a39a4e3 | 2025-02-26 19:42:16 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 13 | #include "base/no_destructor.h" |
| 14 | #include "base/process/process_handle.h" |
Byoungchan Lee | ff42d8d | 2022-12-05 08:22:36 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 16 | #include "build/buildflag.h" |
Etienne Pierre-doray | fc7952f0 | 2025-06-06 00:04:33 | [diff] [blame] | 17 | #include "third_party/perfetto/protos/perfetto/trace/track_event/chrome_process_descriptor.pbzero.h" |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 18 | |
Aman Verma | 0bf8c11 | 2022-11-15 13:01:24 | [diff] [blame] | 19 | namespace tracing { |
| 20 | class TraceEventDataSource; |
| 21 | class CustomEventRecorder; |
Etienne Pierre-doray | 2a39a4e3 | 2025-02-26 19:42:16 | [diff] [blame] | 22 | class TrackNameRecorder; |
Aman Verma | 0bf8c11 | 2022-11-15 13:01:24 | [diff] [blame] | 23 | } // namespace tracing |
| 24 | |
Aman Verma | e64a2feb | 2022-11-30 10:52:48 | [diff] [blame] | 25 | namespace mojo::core { |
| 26 | class Channel; |
| 27 | } |
| 28 | |
Tsuyoshi Horo | 41f81f2 | 2025-04-04 00:23:45 | [diff] [blame] | 29 | namespace network { |
| 30 | class ContentDecodingInterceptor; |
| 31 | } // namespace network |
| 32 | |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 33 | namespace base { |
| 34 | namespace test { |
| 35 | class CurrentProcessForTest; |
Aman Verma | e64a2feb | 2022-11-30 10:52:48 | [diff] [blame] | 36 | } // namespace test |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 37 | |
| 38 | using CurrentProcessType = |
| 39 | perfetto::protos::pbzero::ChromeProcessDescriptor::ProcessType; |
| 40 | |
Aman Verma | e64a2feb | 2022-11-30 10:52:48 | [diff] [blame] | 41 | // These values are persisted to logs. Entries should not be renumbered and |
| 42 | // numeric values should never be reused. |
| 43 | // Use coalesced service process for recording histograms. |
| 44 | enum class ShortProcessType { |
| 45 | kUnspecified = 0, |
| 46 | kBrowser = 1, |
| 47 | kRenderer = 2, |
| 48 | kUtility = 3, |
| 49 | kZygote = 4, |
| 50 | kSandboxHelper = 5, |
| 51 | kGpu = 6, |
| 52 | kPpapiPlugin = 7, |
| 53 | kPpapiBroker = 8, |
| 54 | kServiceNetwork = 9, |
| 55 | kServiceStorage = 10, |
| 56 | kService = 11, |
| 57 | kRendererExtension = 12, |
| 58 | kMaxValue = kRendererExtension, |
| 59 | }; |
| 60 | |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 61 | // CurrentProcess class provides access to set of current process properties |
| 62 | // which are accessible only from the process itself (e.g. ProcessType, |
| 63 | // ProcessName). |
| 64 | // See base::CurrentThread for access to properties of the running |
| 65 | // thread and base::Process::Current for the properties which are known both |
| 66 | // from within and without the process (e.g. pid). |
| 67 | class BASE_EXPORT CurrentProcess { |
| 68 | public: |
| 69 | static CurrentProcess& GetInstance(); |
| 70 | |
| 71 | CurrentProcess(const CurrentProcess&) = delete; |
| 72 | CurrentProcess& operator=(const CurrentProcess&) = delete; |
| 73 | ~CurrentProcess(); |
| 74 | |
| 75 | bool operator==(const CurrentProcess& other) const; |
| 76 | |
| 77 | class TypeKey { |
| 78 | private: |
| 79 | TypeKey() = default; |
| 80 | friend class ::base::test::CurrentProcessForTest; |
Aman Verma | 0bf8c11 | 2022-11-15 13:01:24 | [diff] [blame] | 81 | friend class ::tracing::TraceEventDataSource; |
| 82 | friend class ::tracing::CustomEventRecorder; |
Etienne Pierre-doray | 2a39a4e3 | 2025-02-26 19:42:16 | [diff] [blame] | 83 | friend class ::tracing::TrackNameRecorder; |
Aman Verma | e64a2feb | 2022-11-30 10:52:48 | [diff] [blame] | 84 | friend class ::mojo::core::Channel; |
Tsuyoshi Horo | 41f81f2 | 2025-04-04 00:23:45 | [diff] [blame] | 85 | friend class ::network::ContentDecodingInterceptor; |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 86 | }; |
| 87 | // Returns an enum corresponding to the type of the current process (e.g. |
| 88 | // browser / renderer / utility / etc). It can be used in metrics or tracing |
| 89 | // code — for example, to split a number of low-level events with |
| 90 | // process-type-agnostic implementation (e.g. number of posted tasks) by |
| 91 | // process type for diagnostic purposes. |
| 92 | // To avoid layering violations (i.e. //base or other low-level code modifying |
| 93 | // its behaviour based on the //chrome or //content-level concepts like a |
| 94 | // "browser" or "renderer" process), the access to this function is controlled |
| 95 | // by an explicit list. |
| 96 | CurrentProcessType GetType(TypeKey key) { |
Etienne Pierre-doray | 2a39a4e3 | 2025-02-26 19:42:16 | [diff] [blame] | 97 | return process_type_.load(std::memory_order_relaxed); |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 98 | } |
| 99 | |
Aman Verma | e64a2feb | 2022-11-30 10:52:48 | [diff] [blame] | 100 | ShortProcessType GetShortType(TypeKey key); |
| 101 | |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 102 | class NameKey { |
| 103 | private: |
| 104 | NameKey() = default; |
| 105 | friend class ::base::test::CurrentProcessForTest; |
Aman Verma | 0bf8c11 | 2022-11-15 13:01:24 | [diff] [blame] | 106 | friend class ::tracing::TraceEventDataSource; |
Etienne Pierre-doray | 2a39a4e3 | 2025-02-26 19:42:16 | [diff] [blame] | 107 | friend class ::tracing::TrackNameRecorder; |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 108 | }; |
| 109 | std::string GetName(NameKey key) { |
| 110 | AutoLock lock(lock_); |
| 111 | return process_name_; |
| 112 | } |
| 113 | |
Etienne Pierre-doray | 2a39a4e3 | 2025-02-26 19:42:16 | [diff] [blame] | 114 | class BASE_EXPORT Delegate { |
| 115 | public: |
| 116 | // Called on the main thread of the process whose name is changing, |
| 117 | // immediately after the name is set. |
| 118 | virtual void OnProcessNameChanged(const std::string& process_name, |
| 119 | CurrentProcessType process_type) = 0; |
| 120 | |
| 121 | protected: |
| 122 | ~Delegate() = default; |
| 123 | }; |
| 124 | |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 125 | // Sets the name and type of the process for the metrics and tracing. This |
| 126 | // function should be called as early as possible in the process's lifetime |
| 127 | // before starting any threads, typically in *Main() function. Provide |
| 128 | // process_name as an argument if it can't be trivially derived from the |
| 129 | // process type. |
| 130 | void SetProcessType(CurrentProcessType process_type); |
Etienne Pierre-doray | 2a39a4e3 | 2025-02-26 19:42:16 | [diff] [blame] | 131 | |
| 132 | // `delegate` might racily be invoked after resetting, thus its lifetime must |
| 133 | // match `CurrentProcess`. |
| 134 | void SetDelegate(Delegate* delegate, NameKey key); |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 135 | |
| 136 | bool IsProcessNameEmpty() const { |
| 137 | AutoLock lock(lock_); |
| 138 | return process_name_.empty(); |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | friend class base::NoDestructor<CurrentProcess>; |
| 143 | |
| 144 | CurrentProcess() = default; |
| 145 | |
Etienne Pierre-doray | 2a39a4e3 | 2025-02-26 19:42:16 | [diff] [blame] | 146 | void SetProcessNameAndType(const std::string& process_name, |
| 147 | CurrentProcessType process_type); |
| 148 | |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 149 | mutable Lock lock_; |
| 150 | std::string process_name_; |
| 151 | // The process_type_ is set at the startup before processes start running. |
| 152 | // However, since it runs in multi-threaded environment and if has to be |
| 153 | // changed later, we would want well-defined behaviour even if one thread |
| 154 | // writes while another reads. There are some processes (e.g. Service process) |
| 155 | // where we don't have a guarantee that it will be called early enough in the |
| 156 | // process's lifetime, thus we use std::atomic here. |
| 157 | std::atomic<CurrentProcessType> process_type_; |
Etienne Pierre-doray | 2a39a4e3 | 2025-02-26 19:42:16 | [diff] [blame] | 158 | |
| 159 | raw_ptr<Delegate> delegate_; |
Aman Verma | 327f9b4 | 2022-11-08 12:44:40 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } // namespace base |
| 163 | |
| 164 | #endif // BASE_PROCESS_CURRENT_PROCESS_H_ |