// Copyright 2014 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/services/media_gallery_util/ipc_data_source.h"

#include <algorithm>

#include "base/functional/bind.h"
#include "base/task/single_thread_task_runner.h"

IPCDataSource::IPCDataSource(
    mojo::PendingRemote<chrome::mojom::MediaDataSource> media_data_source,
    int64_t total_size)
    : media_data_source_(std::move(media_data_source)),
      total_size_(total_size),
      utility_task_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()) {
  DETACH_FROM_THREAD(data_source_thread_checker_);
}

IPCDataSource::~IPCDataSource() {
  DCHECK_CALLED_ON_VALID_THREAD(utility_thread_checker_);
}

void IPCDataSource::Stop() {
  DCHECK_CALLED_ON_VALID_THREAD(data_source_thread_checker_);
}

void IPCDataSource::Abort() {
  DCHECK_CALLED_ON_VALID_THREAD(data_source_thread_checker_);
}

void IPCDataSource::Read(int64_t position,
                         base::span<uint8_t> destination,
                         DataSource::ReadCB callback) {
  DCHECK_CALLED_ON_VALID_THREAD(data_source_thread_checker_);

  utility_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&IPCDataSource::ReadMediaData, base::Unretained(this),
                     destination, std::move(callback), position));
}

bool IPCDataSource::GetSize(int64_t* size_out) {
  DCHECK_CALLED_ON_VALID_THREAD(data_source_thread_checker_);
  *size_out = total_size_;
  return true;
}

bool IPCDataSource::IsStreaming() const {
  DCHECK_CALLED_ON_VALID_THREAD(data_source_thread_checker_);
  return false;
}

void IPCDataSource::SetBitrate(int bitrate) {
  DCHECK_CALLED_ON_VALID_THREAD(data_source_thread_checker_);
}

void IPCDataSource::ReadMediaData(base::span<uint8_t> destination,
                                  DataSource::ReadCB callback,
                                  int64_t position) {
  DCHECK_CALLED_ON_VALID_THREAD(utility_thread_checker_);
  CHECK_GE(total_size_, 0);
  CHECK_GE(position, 0);

  // Cap position and size within bounds.
  position = std::min(position, total_size_);
  int64_t clamped_size = std::min(
      base::checked_cast<int64_t>(destination.size()), total_size_ - position);

  media_data_source_->Read(
      position, clamped_size,
      base::BindOnce(&IPCDataSource::ReadDone, base::Unretained(this),
                     destination, std::move(callback)));
}

void IPCDataSource::ReadDone(base::span<uint8_t> destination,
                             DataSource::ReadCB callback,
                             const std::vector<uint8_t>& data) {
  DCHECK_CALLED_ON_VALID_THREAD(utility_thread_checker_);

  destination.copy_prefix_from(data);
  std::move(callback).Run(data.size());
}

bool IPCDataSource::PassedTimingAllowOriginCheck() {
  // The mojo ipc channel doesn't support this yet, so cautiously return false,
  // for now.
  // TODO(crbug.com/40243452): Rework this method to be asynchronous, if
  // possible, so that the mojo interface can be queried.
  return false;
}

bool IPCDataSource::WouldTaintOrigin() const {
  // The mojo ipc channel doesn't support this yet, so cautiously return true,
  // for now.
  // TODO(crbug.com/40243452): Rework this method to be asynchronous, if
  // possible, so that the mojo interface can be queried.
  return true;
}
