chromium/chrome/browser/extensions/api/image_writer_private/operation_manager.cc

// Copyright 2013 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/extensions/api/image_writer_private/operation_manager.h"

#include <utility>

#include "base/lazy_instance.h"
#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/image_writer_private/destroy_partitions_operation.h"
#include "chrome/browser/extensions/api/image_writer_private/error_constants.h"
#include "chrome/browser/extensions/api/image_writer_private/operation.h"
#include "chrome/browser/extensions/api/image_writer_private/write_from_file_operation.h"
#include "chrome/browser/extensions/api/image_writer_private/write_from_url_operation.h"
#include "chrome/browser/extensions/event_router_forwarder.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_host.h"
#include "extensions/common/extension_id.h"
#include "mojo/public/cpp/bindings/pending_remote.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/crosapi/crosapi_ash.h"
#include "chrome/browser/ash/crosapi/crosapi_manager.h"
#include "chrome/browser/ash/crosapi/image_writer_ash.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chromeos/crosapi/mojom/image_writer.mojom.h"
#endif

image_writer_api;

namespace extensions {
namespace image_writer {

namespace {
#if BUILDFLAG(IS_CHROMEOS_ASH)
crosapi::mojom::Stage ToMojo(image_writer_api::Stage stage) {
  switch (stage) {
    case image_writer_api::Stage::kConfirmation:
      return crosapi::mojom::Stage::kConfirmation;
    case image_writer_api::Stage::kDownload:
      return crosapi::mojom::Stage::kDownload;
    case image_writer_api::Stage::kVerifyDownload:
      return crosapi::mojom::Stage::kVerifyDownload;
    case image_writer_api::Stage::kUnzip:
      return crosapi::mojom::Stage::kUnzip;
    case image_writer_api::Stage::kWrite:
      return crosapi::mojom::Stage::kWrite;
    case image_writer_api::Stage::kVerifyWrite:
      return crosapi::mojom::Stage::kVerifyWrite;
    case image_writer_api::Stage::kUnknown:
    case image_writer_api::Stage::kNone:
      return crosapi::mojom::Stage::kUnknown;
  }
}

bool IsRemoteClientToken(const ExtensionId& id) {
  // CrosapiManager is not initialized for unit test cases, since we have
  // not enabled unit tests for Lacros.
  // TODO(crbug.com/40773848): Always expect CrosapiManager::IsInitialized()
  // once we enable unit test with Lacros integration.
  if (!crosapi::CrosapiManager::IsInitialized())
    return false;

  return crosapi::CrosapiManager::Get()
      ->crosapi_ash()
      ->image_writer_ash()
      ->IsRemoteClientToken(id);
}

void DispatchOnWriteProgressToRemoteClient(
    const std::string& client_token_string,
    image_writer_api::Stage stage,
    int progress) {
  // CrosapiManager is not initialized for unit test cases, since we have
  // not enabled unit tests for Lacros.
  // TODO(crbug.com/40773848): Always expect CrosapiManager::IsInitialized()
  // once we enable unit test with Lacros integration.
  if (crosapi::CrosapiManager::IsInitialized()) {
    crosapi::CrosapiManager::Get()
        ->crosapi_ash()
        ->image_writer_ash()
        ->DispatchOnWriteProgressEvent(client_token_string, ToMojo(stage),
                                       progress);
  }
}

void DispatchOnWriteCompleteToRemoteClient(
    const std::string& client_token_string) {
  if (crosapi::CrosapiManager::IsInitialized()) {
    crosapi::CrosapiManager::Get()
        ->crosapi_ash()
        ->image_writer_ash()
        ->DispatchOnWriteCompleteEvent(client_token_string);
  }
}

void DispatchOnWriteErrorToRemoteClient(const std::string& client_token_string,
                                        image_writer_api::Stage stage,
                                        uint32_t percent_complete,
                                        const std::string& error) {
  if (crosapi::CrosapiManager::IsInitialized()) {
    crosapi::CrosapiManager::Get()
        ->crosapi_ash()
        ->image_writer_ash()
        ->DispatchOnWriteErrorEvent(client_token_string, ToMojo(stage),
                                    percent_complete, error);
  }
}
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
}  // namespace

BrowserThread;

OperationManager::OperationManager(content::BrowserContext* context)
    :{}

OperationManager::~OperationManager() = default;

void OperationManager::Shutdown() {}

void OperationManager::StartWriteFromUrl(
    const ExtensionId& extension_id,
    GURL url,
    const std::string& hash,
    const std::string& device_path,
    Operation::StartWriteCallback callback) {}

void OperationManager::StartWriteFromFile(
    const ExtensionId& extension_id,
    const base::FilePath& path,
    const std::string& device_path,
    Operation::StartWriteCallback callback) {}

void OperationManager::CancelWrite(const ExtensionId& extension_id,
                                   Operation::CancelWriteCallback callback) {}

void OperationManager::DestroyPartitions(
    const ExtensionId& extension_id,
    const std::string& device_path,
    Operation::StartWriteCallback callback) {}

void OperationManager::OnProgress(const ExtensionId& extension_id,
                                  image_writer_api::Stage stage,
                                  int progress) {}

void OperationManager::OnComplete(const ExtensionId& extension_id) {}

void OperationManager::OnError(const ExtensionId& extension_id,
                               image_writer_api::Stage stage,
                               int progress,
                               const std::string& error_message) {}

base::FilePath OperationManager::GetAssociatedDownloadFolder() {}

Operation* OperationManager::GetOperation(const ExtensionId& extension_id) {}

void OperationManager::DeleteOperation(const ExtensionId& extension_id) {}

void OperationManager::OnExtensionUnloaded(
    content::BrowserContext* browser_context,
    const Extension* extension,
    UnloadedExtensionReason reason) {}

void OperationManager::OnShutdown(ExtensionRegistry* registry) {}

void OperationManager::OnBackgroundHostClose(const ExtensionId& extension_id) {}

void OperationManager::OnProcessManagerShutdown(ProcessManager* manager) {}

void OperationManager::OnExtensionProcessTerminated(
    const Extension* extension) {}

OperationManager* OperationManager::Get(content::BrowserContext* context) {}

static base::LazyInstance<BrowserContextKeyedAPIFactory<OperationManager>>::
    DestructorAtExit g_operation_manager_factory =;

BrowserContextKeyedAPIFactory<OperationManager>*
OperationManager::GetFactoryInstance() {}

}  // namespace image_writer
}  // namespace extensions