1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198

base / process / current_process.cc [blame]

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

#include "base/process/current_process.h"

namespace base {

namespace {

const char* GetNameForProcessType(CurrentProcessType process_type) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
  switch (process_type) {
    case CurrentProcessType::PROCESS_UNSPECIFIED:
      return "Null";
    case CurrentProcessType::PROCESS_BROWSER:
      return "Browser";
    case CurrentProcessType::PROCESS_RENDERER:
      return "Renderer";
    case CurrentProcessType::PROCESS_UTILITY:
      return "Utility";
    case CurrentProcessType::PROCESS_ZYGOTE:
      return "Zygote";
    case CurrentProcessType::PROCESS_SANDBOX_HELPER:
      return "SandboxHelper";
    case CurrentProcessType::PROCESS_GPU:
      return "GPU Process";
    case CurrentProcessType::PROCESS_PPAPI_PLUGIN:
      return "PPAPI Process";
    case CurrentProcessType::PROCESS_PPAPI_BROKER:
      return "PPAPI Broker Process";
    case CurrentProcessType::PROCESS_SERVICE_NETWORK:
      return "Service: network.mojom.NetworkService";
    case CurrentProcessType::PROCESS_SERVICE_TRACING:
      return "Service: tracing.mojom.TracingService";
    case CurrentProcessType::PROCESS_SERVICE_STORAGE:
      return "Service: storage.mojom.StorageService";
    case CurrentProcessType::PROCESS_SERVICE_AUDIO:
      return "Service: audio.mojom.AudioService";
    case CurrentProcessType::PROCESS_SERVICE_DATA_DECODER:
      return "Service: data_decoder.mojom.DataDecoderService";
    case CurrentProcessType::PROCESS_SERVICE_UTIL_WIN:
      return "Service: chrome.mojom.UtilWin";
    case CurrentProcessType::PROCESS_SERVICE_PROXY_RESOLVER:
      return "Service: proxy_resolver.mojom.ProxyResolverFactory";
    case CurrentProcessType::PROCESS_SERVICE_CDM:
      return "Service: media.mojom.CdmServiceBroker";
    case CurrentProcessType::PROCESS_SERVICE_MEDIA_FOUNDATION:
      return "Service: media.mojom.MediaFoundationServiceBroker";
    case CurrentProcessType::PROCESS_SERVICE_VIDEO_CAPTURE:
      return "Service: video_capture.mojom.VideoCaptureService";
    case CurrentProcessType::PROCESS_SERVICE_UNZIPPER:
      return "Service: unzip.mojom.Unzipper";
    case CurrentProcessType::PROCESS_SERVICE_MIRRORING:
      return "Service: mirroring.mojom.MirroringService";
    case CurrentProcessType::PROCESS_SERVICE_FILEPATCHER:
      return "Service: patch.mojom.FilePatcher";
    case CurrentProcessType::PROCESS_SERVICE_TTS:
      return "Service: chromeos.tts.mojom.TtsService";
    case CurrentProcessType::PROCESS_SERVICE_PRINTING:
      return "Service: printing.mojom.PrintingService";
    case CurrentProcessType::PROCESS_SERVICE_QUARANTINE:
      return "Service: quarantine.mojom.Quarantine";
    case CurrentProcessType::PROCESS_SERVICE_CROS_LOCALSEARCH:
      return "Service: chromeos.local_search_service.mojom.LocalSearchService";
    case CurrentProcessType::PROCESS_SERVICE_CROS_ASSISTANT_AUDIO_DECODER:
      return "Service: chromeos.assistant.mojom.AssistantAudioDecoderFactory";
    case CurrentProcessType::PROCESS_SERVICE_FILEUTIL:
      return "Service: chrome.mojom.FileUtilService";
    case CurrentProcessType::PROCESS_SERVICE_PRINTCOMPOSITOR:
      return "Service: printing.mojom.PrintCompositor";
    case CurrentProcessType::PROCESS_SERVICE_PAINTPREVIEW:
      return "Service: paint_preview.mojom.PaintPreviewCompositorCollection";
    case CurrentProcessType::PROCESS_SERVICE_SPEECHRECOGNITION:
      return "Service: media.mojom.SpeechRecognitionService";
    case CurrentProcessType::PROCESS_SERVICE_XRDEVICE:
      return "Service: device.mojom.XRDeviceService";
    case CurrentProcessType::PROCESS_SERVICE_READICON:
      return "Service: chrome.mojom.UtilReadIcon";
    case CurrentProcessType::PROCESS_SERVICE_LANGUAGEDETECTION:
      return "Service: language_detection.mojom.LanguageDetectionService";
    case CurrentProcessType::PROCESS_SERVICE_SHARING:
      return "Service: sharing.mojom.Sharing";
    case CurrentProcessType::PROCESS_SERVICE_MEDIAPARSER:
      return "Service: chrome.mojom.MediaParserFactory";
    case CurrentProcessType::PROCESS_SERVICE_QRCODEGENERATOR:
      return "Service: qrcode_generator.mojom.QRCodeGeneratorService";
    case CurrentProcessType::PROCESS_SERVICE_PROFILEIMPORT:
      return "Service: chrome.mojom.ProfileImport";
    case CurrentProcessType::PROCESS_SERVICE_IME:
      return "Service: chromeos.ime.mojom.ImeService";
    case CurrentProcessType::PROCESS_SERVICE_RECORDING:
      return "Service: recording.mojom.RecordingService";
    case CurrentProcessType::PROCESS_SERVICE_SHAPEDETECTION:
      return "Service: shape_detection.mojom.ShapeDetectionService";
    case CurrentProcessType::PROCESS_RENDERER_EXTENSION:
      return "Extension Renderer";
  }
#else
  return "Null";
#endif  // BUILDFLAG(ENABLE_BASE_TRACING)
}

}  // namespace

// Used for logging histograms for IPC metrics based on their process type.
ShortProcessType CurrentProcess::GetShortType(TypeKey key) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
  CurrentProcessType process = static_cast<CurrentProcessType>(
      process_type_.load(std::memory_order_relaxed));
  switch (process) {
    case CurrentProcessType::PROCESS_UNSPECIFIED:
      return ShortProcessType::kUnspecified;
    case CurrentProcessType::PROCESS_BROWSER:
      return ShortProcessType::kBrowser;
    case CurrentProcessType::PROCESS_RENDERER:
      return ShortProcessType::kRenderer;
    case CurrentProcessType::PROCESS_UTILITY:
      return ShortProcessType::kUtility;
    case CurrentProcessType::PROCESS_ZYGOTE:
      return ShortProcessType::kZygote;
    case CurrentProcessType::PROCESS_SANDBOX_HELPER:
      return ShortProcessType::kSandboxHelper;
    case CurrentProcessType::PROCESS_GPU:
      return ShortProcessType::kGpu;
    case CurrentProcessType::PROCESS_PPAPI_PLUGIN:
      return ShortProcessType::kPpapiPlugin;
    case CurrentProcessType::PROCESS_PPAPI_BROKER:
      return ShortProcessType::kPpapiBroker;
    case CurrentProcessType::PROCESS_SERVICE_NETWORK:
      return ShortProcessType::kServiceNetwork;
    case CurrentProcessType::PROCESS_SERVICE_STORAGE:
      return ShortProcessType::kServiceStorage;
    case CurrentProcessType::PROCESS_RENDERER_EXTENSION:
      return ShortProcessType::kRendererExtension;
    case CurrentProcessType::PROCESS_SERVICE_TRACING:
    case CurrentProcessType::PROCESS_SERVICE_AUDIO:
    case CurrentProcessType::PROCESS_SERVICE_DATA_DECODER:
    case CurrentProcessType::PROCESS_SERVICE_UTIL_WIN:
    case CurrentProcessType::PROCESS_SERVICE_PROXY_RESOLVER:
    case CurrentProcessType::PROCESS_SERVICE_CDM:
    case CurrentProcessType::PROCESS_SERVICE_MEDIA_FOUNDATION:
    case CurrentProcessType::PROCESS_SERVICE_VIDEO_CAPTURE:
    case CurrentProcessType::PROCESS_SERVICE_UNZIPPER:
    case CurrentProcessType::PROCESS_SERVICE_MIRRORING:
    case CurrentProcessType::PROCESS_SERVICE_FILEPATCHER:
    case CurrentProcessType::PROCESS_SERVICE_TTS:
    case CurrentProcessType::PROCESS_SERVICE_PRINTING:
    case CurrentProcessType::PROCESS_SERVICE_QUARANTINE:
    case CurrentProcessType::PROCESS_SERVICE_CROS_LOCALSEARCH:
    case CurrentProcessType::PROCESS_SERVICE_CROS_ASSISTANT_AUDIO_DECODER:
    case CurrentProcessType::PROCESS_SERVICE_FILEUTIL:
    case CurrentProcessType::PROCESS_SERVICE_PRINTCOMPOSITOR:
    case CurrentProcessType::PROCESS_SERVICE_PAINTPREVIEW:
    case CurrentProcessType::PROCESS_SERVICE_SPEECHRECOGNITION:
    case CurrentProcessType::PROCESS_SERVICE_XRDEVICE:
    case CurrentProcessType::PROCESS_SERVICE_READICON:
    case CurrentProcessType::PROCESS_SERVICE_LANGUAGEDETECTION:
    case CurrentProcessType::PROCESS_SERVICE_SHARING:
    case CurrentProcessType::PROCESS_SERVICE_MEDIAPARSER:
    case CurrentProcessType::PROCESS_SERVICE_QRCODEGENERATOR:
    case CurrentProcessType::PROCESS_SERVICE_PROFILEIMPORT:
    case CurrentProcessType::PROCESS_SERVICE_IME:
    case CurrentProcessType::PROCESS_SERVICE_RECORDING:
    case CurrentProcessType::PROCESS_SERVICE_SHAPEDETECTION:
      return ShortProcessType::kService;
  }
#else
  return ShortProcessType::kUnspecified;
#endif
}

// static
CurrentProcess& CurrentProcess::GetInstance() {
  static base::NoDestructor<CurrentProcess> instance;
  return *instance;
}

void CurrentProcess::SetProcessType(CurrentProcessType process_type) {
  std::string process_name = GetNameForProcessType(process_type);
  CurrentProcess::GetInstance().SetProcessNameAndType(process_name,
                                                      process_type);
}

void CurrentProcess::SetProcessNameAndType(const std::string& process_name,
                                           CurrentProcessType process_type) {
  {
    AutoLock lock(lock_);
    process_name_ = process_name;
    process_type_.store(static_cast<CurrentProcessType>(process_type),
                        std::memory_order_relaxed);
  }
#if BUILDFLAG(ENABLE_BASE_TRACING)
  trace_event::TraceLog::GetInstance()->OnSetProcessName(process_name);
#endif
}

}  // namespace base