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

#include "chrome/browser/web_applications/commands/garbage_collect_storage_partitions_command.h"

#include <memory>
#include <string>
#include <unordered_set>

#include "base/check.h"
#include "base/files/file_path.h"
#include "base/functional/callback_helpers.h"
#include "base/functional/concurrent_closures.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/extensions_manager.h"
#include "chrome/browser/web_applications/locks/all_apps_lock.h"
#include "chrome/browser/web_applications/web_app_isolation_delegate.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"

namespace web_app {

GarbageCollectStoragePartitionsCommand::GarbageCollectStoragePartitionsCommand(
    Profile* profile,
    base::OnceClosure done)
    : WebAppCommand<AllAppsLock>("GarbageCollectStoragePartitionsCommand",
                                 AllAppsLockDescription(),
                                 std::move(done)),
      profile_(profile) {
  DCHECK(profile);
}

GarbageCollectStoragePartitionsCommand::
    ~GarbageCollectStoragePartitionsCommand() = default;

void GarbageCollectStoragePartitionsCommand::StartWithLock(
    std::unique_ptr<AllAppsLock> lock) {
  lock_ = std::move(lock);
  ResetStorageGarbageCollectPref();
}

void GarbageCollectStoragePartitionsCommand::ResetStorageGarbageCollectPref() {
  base::ConcurrentClosures concurrent;
  // TODO(crbug.com/40280176): change this pref to be stateful instead of
  // resetting to false early.
  profile_->GetPrefs()->SetBoolean(
      prefs::kShouldGarbageCollectStoragePartitions, false);
  profile_->GetPrefs()->CommitPendingWrite(concurrent.CreateClosure());
  std::move(concurrent)
      .Done(base::BindOnce(&GarbageCollectStoragePartitionsCommand::OnPrefReset,
                           weak_factory_.GetWeakPtr()));
}

void GarbageCollectStoragePartitionsCommand::OnPrefReset() {
  lock_->extensions_manager().OnExtensionSystemReady(base::BindOnce(
      &GarbageCollectStoragePartitionsCommand::DoGarbageCollection,
      weak_factory_.GetWeakPtr()));
}

void GarbageCollectStoragePartitionsCommand::DoGarbageCollection() {
  std::unordered_set<base::FilePath> allowlist;

  // InstallGate delays extension installations.
  install_gate_ =
      lock_->extensions_manager().RegisterGarbageCollectionInstallGate();

  // Get all paths from Extension system.
  allowlist.merge(lock_->extensions_manager().GetIsolatedStoragePaths());

  // Get all paths from Web App system.
  allowlist.merge(lock_->isolation_delegate().GetIsolatedStoragePaths());

  base::ListValue* debug_paths =
      GetMutableDebugValue().EnsureList("allow_list_paths");
  for (const auto& path : allowlist) {
    debug_paths->Append(path.LossyDisplayName());
  }

  profile_->GarbageCollectStoragePartitions(
      allowlist,
      base::BindOnce(&GarbageCollectStoragePartitionsCommand::OnSuccess,
                     weak_factory_.GetWeakPtr()));
}

void GarbageCollectStoragePartitionsCommand::OnSuccess() {
  CompleteAndSelfDestruct(CommandResult::kSuccess);
}

}  // namespace web_app
