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

#ifndef CHROME_BROWSER_TASK_MANAGER_PROVIDERS_CHILD_PROCESS_TASK_H_
#define CHROME_BROWSER_TASK_MANAGER_PROVIDERS_CHILD_PROCESS_TASK_H_

#include <stdint.h>

#include <memory>

#include "base/byte_size.h"
#include "chrome/browser/task_manager/providers/task.h"
#include "chrome/common/buildflags.h"

class ProcessResourceUsage;

namespace content {
struct ChildProcessData;
}  // namespace content

namespace task_manager {

// Represents several types of the browser's child processes such as
// a plugin or a GPU process, ... etc.
class ChildProcessTask : public Task {
 public:
  // ChildProcessData has a ProcessType but that's not always granular enough to
  // correctly determine what string to show as the name of the task. This enum
  // is used to provide that information.
  //
  // Please consult Task Manager OWNERs when adding a new ProcessSubType.
  // There is a dependency between Task Manager categories and a processes
  // subtype.
  enum class ProcessSubtype {
    kNoSubtype,
    // The "spare" render process, a render process used so that there is always
    // a render process ready to go.
    kSpareRenderProcess,
    // A render process used for chrome://glic.
    kGlicRenderProcess,
    // A render process that is unknown and for which no provider is available.
    // Should not be used; all processes should be shown in the Task Manager.
    // See https://crbug.com/40528867 .
    kUnknownRenderProcess,
  };

  // Creates a child process task given its |data| which is
  // received from observing |content::BrowserChildProcessObserver|.
  ChildProcessTask(const content::ChildProcessData& data,
                   ProcessSubtype subtype);

  ChildProcessTask(const ChildProcessTask&) = delete;
  ChildProcessTask& operator=(const ChildProcessTask&) = delete;
  ~ChildProcessTask() override;

  // task_manager::Task:
  bool IsKillable() override;
  bool Kill() override;
  void Refresh(const base::TimeDelta& update_interval,
               int64_t refresh_flags) override;
  Type GetType() const override;
  SubType GetSubType() const override;
  int GetChildProcessUniqueID() const override;
  std::optional<base::ByteSize> GetV8MemoryAllocated() const override;
  std::optional<base::ByteSize> GetV8MemoryUsed() const override;

 private:
  static gfx::ImageSkia* s_icon_;

  // The Mojo service wrapper that will provide us with the V8 memory usage of
  // the browser child process represented by this object.
  std::unique_ptr<ProcessResourceUsage> process_resources_sampler_;

  // The allocated and used V8 memory.
  std::optional<base::ByteSize> v8_memory_allocated_;
  std::optional<base::ByteSize> v8_memory_used_;

  // The unique ID of the child process. It is not the PID of the process.
  // See |content::ChildProcessData::id|.
  const int unique_child_process_id_;

  // The type of the child process. See |content::ProcessType|.
  const int process_type_;

  // The subtype of the child process.
  const ProcessSubtype process_subtype_;

  // Depending on the |process_type_|, determines whether this task uses V8
  // memory or not.
  const bool uses_v8_memory_;
};

}  // namespace task_manager

#endif  // CHROME_BROWSER_TASK_MANAGER_PROVIDERS_CHILD_PROCESS_TASK_H_
