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

Code before:
    void OnBrowsingDataRemoverDone(
        BrowsingDataRemover::DataType failed_data_types) override {
      task_->OnOperationFinished(this, failed_data_types);
    }

   private:
    raw_ptr<ClearDataTask> task_;
    base::ScopedObservation<BrowsingDataRemover, BrowsingDataRemover::Observer>
        observation_{this};
  };

  explicit ClearDataTask(gin_helper::Promise<void> promise)
      : promise_(std::move(promise)) {}


  static void StartOperation(
      ClearDataTask* task,
      BrowsingDataRemover* remover,
      BrowsingDataRemover::DataType data_type_mask,
      std::unique_ptr<BrowsingDataFilterBuilder> filter_builder) {
    // Track this operation
    task->operations_running_ += 1;

    auto operation = std::make_unique<ClearDataOperation>(task, remover);
    operation->Start(remover, data_type_mask, std::move(filter_builder));
    task->operations_.push_back(std::move(operation));
  }

  void OnOperationFinished(
      ClearDataOperation* operation,
      std::optional<BrowsingDataRemover::DataType> failed_data_types) {
    DCHECK_GT(operations_running_, 0);
    operations_running_ -= 1;

    if (failed_data_types.has_value()) {
      failed_data_types_ |= failed_data_types.value();
    }

    if (operation) {
      operations_.erase(
          std::remove_if(
              operations_.begin(), operations_.end(),
              [operation](const std::unique_ptr<ClearDataOperation>& op) {
                return op.get() == operation;
              }),
          operations_.end());
    }

    // If this is the last operation, then the task is finished
    if (operations_running_ == 0) {
      OnTaskFinished();

Review comment (bug):
should be swapped to avoid any race condition mentioned in https://github.com/electron/electron/pull/47361/files#r2131774096, there is a use after move here so that should be fixed as well.

```suggestion
    auto& operation = task->operations_.emplace_back(std::make_unique<ClearDataOperation>(task, remover));
    operation->Start(remover, data_type_mask, std::move(filter_builder));
```

Code after:
    // BrowsingDataRemover::Observer:
    void OnBrowsingDataRemoverDone(
        BrowsingDataRemover::DataType failed_data_types) override {
      task_->OnOperationFinished(this, failed_data_types);
    }

   private:
    raw_ptr<ClearDataTask> task_;
    base::ScopedObservation<BrowsingDataRemover, BrowsingDataRemover::Observer>
        observation_{this};
  };

  explicit ClearDataTask(gin_helper::Promise<void> promise)
      : promise_(std::move(promise)) {}

  static void StartOperation(
      ClearDataTask* task,
      BrowsingDataRemover* remover,
      BrowsingDataRemover::DataType data_type_mask,
      std::unique_ptr<BrowsingDataFilterBuilder> filter_builder) {
    // Track this operation
    task->operations_running_ += 1;

    auto& operation = task->operations_.emplace_back(
        std::make_unique<ClearDataOperation>(task, remover));
    operation->Start(remover, data_type_mask, std::move(filter_builder));
  }

  void OnOperationFinished(
      ClearDataOperation* operation,
      std::optional<BrowsingDataRemover::DataType> failed_data_types) {
    DCHECK_GT(operations_running_, 0);
    operations_running_ -= 1;

    if (failed_data_types.has_value()) {
      failed_data_types_ |= failed_data_types.value();
    }

    if (operation) {
      operations_.erase(
          std::remove_if(
              operations_.begin(), operations_.end(),
              [operation](const std::unique_ptr<ClearDataOperation>& op) {
                return op.get() == operation;
              }),
          operations_.end());
    }

    // If this is the last operation, then the task is finished
    if (operations_running_ == 0) {
      OnTaskFinished();