Repository: electron/electron
File: shell/browser/api/electron_api_session.cc (C++)

Code before:

BrowsingDataRemover::DataType GetDataTypeMask(
    const std::vector<std::string>& data_types) {
  BrowsingDataRemover::DataType mask = 0u;
  for (const auto& type : data_types) {
    if (const auto* val = base::FindOrNull(kDataTypeLookup, type))
      mask |= *val;
  }
  return mask;
}

std::vector<std::string> GetDataTypesFromMask(
    BrowsingDataRemover::DataType mask) {
  std::vector<std::string> results;
  for (const auto [type, flag] : kDataTypeLookup) {
    if (mask & flag) {
      results.emplace_back(type);
    }
  }
  return results;
}

// Represents a task to clear browsing data for the `clearData` API method.
//
// This type manages its own lifetime through gin_helper::CleanedUpAtExit,
// ensuring it's destroyed before the node environment shuts down.
class ClearDataTask : public gin_helper::CleanedUpAtExit {
 public:
  // Starts running a task. This function will return before the task is
  // finished, but will resolve or reject the |promise| when it finishes.
  static void Run(
      BrowsingDataRemover* remover,
      gin_helper::Promise<void> promise,
      BrowsingDataRemover::DataType data_type_mask,
      std::vector<url::Origin> origins,
      BrowsingDataFilterBuilder::Mode filter_mode,
      BrowsingDataFilterBuilder::OriginMatchingMode origin_matching_mode) {
    auto* task = new ClearDataTask(std::move(promise));

    // This method counts as an operation. This is important so we can call
    // `OnOperationFinished` at the end of this method as a fallback if all the
    // other operations finished while this method was still executing
    task->operations_running_ = 1;

    // Cookies are scoped more broadly than other types of data, so if we are
    // filtering then we need to do it at the registrable domain level
    if (!origins.empty() &&
        data_type_mask & BrowsingDataRemover::DATA_TYPE_COOKIES) {
      data_type_mask &= ~BrowsingDataRemover::DATA_TYPE_COOKIES;

      auto cookies_filter_builder =

Review comment (refactor):
```suggestion
// This type manages its own lifetime,
// 1) deleting itself once all the operations created by this task are completed.
// 2) through gin_helper::CleanedUpAtExit, ensuring it's destroyed before the node environment shuts down.
```

Code after:

BrowsingDataRemover::DataType GetDataTypeMask(
    const std::vector<std::string>& data_types) {
  BrowsingDataRemover::DataType mask = 0u;
  for (const auto& type : data_types) {
    if (const auto* val = base::FindOrNull(kDataTypeLookup, type))
      mask |= *val;
  }
  return mask;
}

std::vector<std::string> GetDataTypesFromMask(
    BrowsingDataRemover::DataType mask) {
  std::vector<std::string> results;
  for (const auto [type, flag] : kDataTypeLookup) {
    if (mask & flag) {
      results.emplace_back(type);
    }
  }
  return results;
}

// Represents a task to clear browsing data for the `clearData` API method.
//
// This type manages its own lifetime,
// 1) deleting itself once all the operations created by this task are
// completed. 2) through gin_helper::CleanedUpAtExit, ensuring it's destroyed
// before the node environment shuts down.
class ClearDataTask : public gin_helper::CleanedUpAtExit {
 public:
  // Starts running a task. This function will return before the task is
  // finished, but will resolve or reject the |promise| when it finishes.
  static void Run(
      BrowsingDataRemover* remover,
      gin_helper::Promise<void> promise,
      BrowsingDataRemover::DataType data_type_mask,
      std::vector<url::Origin> origins,
      BrowsingDataFilterBuilder::Mode filter_mode,
      BrowsingDataFilterBuilder::OriginMatchingMode origin_matching_mode) {
    auto* task = new ClearDataTask(std::move(promise));

    // This method counts as an operation. This is important so we can call
    // `OnOperationFinished` at the end of this method as a fallback if all the
    // other operations finished while this method was still executing
    task->operations_running_ = 1;

    // Cookies are scoped more broadly than other types of data, so if we are
    // filtering then we need to do it at the registrable domain level
    if (!origins.empty() &&
        data_type_mask & BrowsingDataRemover::DATA_TYPE_COOKIES) {
      data_type_mask &= ~BrowsingDataRemover::DATA_TYPE_COOKIES;