chromium/chrome/browser/extensions/api/image_writer_private/image_writer_private_api.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/image_writer_private_api.h"

#include <utility>

#include "base/functional/bind.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/api/image_writer_private/error_constants.h"
#include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/file_handlers/app_file_handler_util.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/components/disks/disks_prefs.h"
#endif

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chrome/browser/extensions/api/image_writer_private/image_writer_controller_lacros.h"
#endif

image_writer_api;

namespace extensions {

namespace {

#if BUILDFLAG(IS_CHROMEOS_LACROS)
api::image_writer_private::RemovableStorageDevice FromMojo(
    const crosapi::mojom::RemovableStorageDevicePtr& mojo_device) {
  api::image_writer_private::RemovableStorageDevice device;
  device.storage_unit_id = mojo_device->storage_unit_id;
  device.capacity = mojo_device->capacity;
  device.vendor = mojo_device->vendor;
  device.model = mojo_device->model;
  device.removable = mojo_device->removable;
  return device;
}
#endif  // BUILDFLAG(IS_CHROMOS_LACROS)

}  // namespace

ImageWriterPrivateBaseFunction::ImageWriterPrivateBaseFunction() = default;

ImageWriterPrivateBaseFunction::~ImageWriterPrivateBaseFunction() = default;

#if BUILDFLAG(IS_CHROMEOS_LACROS)
void ImageWriterPrivateBaseFunction::OnComplete(
    const std::optional<std::string>& error) {
  if (error)
    Respond(Error(error.value()));
  else
    Respond(NoArguments());
}
#else
void ImageWriterPrivateBaseFunction::OnComplete(bool success,
                                                const std::string& error) {}
#endif  // BUILDFLAG(IS_CHROMOS_LACROS)

ImageWriterPrivateWriteFromUrlFunction::
    ImageWriterPrivateWriteFromUrlFunction() = default;

ImageWriterPrivateWriteFromUrlFunction::
    ~ImageWriterPrivateWriteFromUrlFunction() = default;

ExtensionFunction::ResponseAction
ImageWriterPrivateWriteFromUrlFunction::Run() {}

ImageWriterPrivateWriteFromFileFunction::
    ImageWriterPrivateWriteFromFileFunction() = default;

ImageWriterPrivateWriteFromFileFunction::
    ~ImageWriterPrivateWriteFromFileFunction() = default;

ExtensionFunction::ResponseAction
ImageWriterPrivateWriteFromFileFunction::Run() {}

ImageWriterPrivateCancelWriteFunction::ImageWriterPrivateCancelWriteFunction() =
    default;

ImageWriterPrivateCancelWriteFunction::
    ~ImageWriterPrivateCancelWriteFunction() = default;

ExtensionFunction::ResponseAction ImageWriterPrivateCancelWriteFunction::Run() {}

ImageWriterPrivateDestroyPartitionsFunction::
    ImageWriterPrivateDestroyPartitionsFunction() = default;

ImageWriterPrivateDestroyPartitionsFunction::
    ~ImageWriterPrivateDestroyPartitionsFunction() = default;

ExtensionFunction::ResponseAction
ImageWriterPrivateDestroyPartitionsFunction::Run() {}

ImageWriterPrivateListRemovableStorageDevicesFunction::
    ImageWriterPrivateListRemovableStorageDevicesFunction() = default;

ImageWriterPrivateListRemovableStorageDevicesFunction::
    ~ImageWriterPrivateListRemovableStorageDevicesFunction() = default;

ExtensionFunction::ResponseAction
ImageWriterPrivateListRemovableStorageDevicesFunction::Run() {}

void ImageWriterPrivateListRemovableStorageDevicesFunction::OnDeviceListReady(
    scoped_refptr<StorageDeviceList> device_list) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS)
void ImageWriterPrivateListRemovableStorageDevicesFunction::
    OnCrosapiDeviceListReady(
        std::optional<std::vector<crosapi::mojom::RemovableStorageDevicePtr>>
            mojo_devices) {
  if (!mojo_devices) {
    Respond(Error(image_writer::error::kDeviceListError));
    return;
  }

  std::vector<api::image_writer_private::RemovableStorageDevice> devices;
  for (const auto& mojo_device : mojo_devices.value())
    devices.push_back(FromMojo(mojo_device));

  Respond(ArgumentList(
      image_writer_api::ListRemovableStorageDevices::Results::Create(devices)));
}
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)

}  // namespace extensions