blob: a8343cb1e8f194eaa0d7f8c37a36ebef5e701ab2 [file] [log] [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_INDEXED_DB_BLOB_READER_H_
#define CONTENT_BROWSER_INDEXED_DB_BLOB_READER_H_
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "components/services/storage/public/mojom/blob_storage_context.mojom.h"
#include "content/browser/indexed_db/indexed_db_external_object.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "services/network/public/mojom/data_pipe_getter.mojom.h"
#include "third_party/blink/public/mojom/blob/blob.mojom.h"
namespace content::indexed_db {
// This class reads files from the IDB blob store, which are just plain files on
// disk, and serves them through the mojom Blob interface. It does this in
// multiple ways:
//
// * For the most common case, i.e. reading a blob with blink::mojom::ReadAll(),
// this class reads from disk and pipes bytes directly to the renderer.
// * In cases where the blob registry needs to read bytes, such as for copying
// to a local file or to serve data for blob:// URLs, this class reads from
// disk and returns the bytes via `BlobDataItemReader`.
class BlobReader : public blink::mojom::Blob,
public network::mojom::DataPipeGetter,
public storage::mojom::BlobDataItemReader {
public:
BlobReader(const IndexedDBExternalObject& blob_info,
storage::mojom::BlobStorageContext& blob_registry,
base::OnceClosure on_last_receiver_disconnected);
~BlobReader() override;
BlobReader(const BlobReader&) = delete;
BlobReader& operator=(const BlobReader&) = delete;
// blink::mojom::Blob:
void Clone(mojo::PendingReceiver<blink::mojom::Blob> receiver) override;
void AsDataPipeGetter(
mojo::PendingReceiver<network::mojom::DataPipeGetter> receiver) override;
void ReadRange(
uint64_t offset,
uint64_t length,
mojo::ScopedDataPipeProducerHandle handle,
mojo::PendingRemote<blink::mojom::BlobReaderClient> client) override;
void ReadAll(
mojo::ScopedDataPipeProducerHandle handle,
mojo::PendingRemote<blink::mojom::BlobReaderClient> client) override;
void Load(
mojo::PendingReceiver<network::mojom::URLLoader> loader,
const std::string& method,
const net::HttpRequestHeaders& headers,
mojo::PendingRemote<network::mojom::URLLoaderClient> client) override;
void ReadSideData(blink::mojom::Blob::ReadSideDataCallback callback) override;
void CaptureSnapshot(CaptureSnapshotCallback callback) override;
void GetInternalUUID(GetInternalUUIDCallback callback) override;
// network::mojom::DataPipeGetter:
void Clone(
mojo::PendingReceiver<network::mojom::DataPipeGetter> receiver) override;
void Read(mojo::ScopedDataPipeProducerHandle pipe,
network::mojom::DataPipeGetter::ReadCallback callback) override;
// storage::mojom::BlobDataItemReader:
void Read(uint64_t offset,
uint64_t length,
mojo::ScopedDataPipeProducerHandle pipe,
storage::mojom::BlobDataItemReader::ReadCallback callback) override;
void ReadSideData(storage::mojom::BlobDataItemReader::ReadSideDataCallback
callback) override;
private:
void OnReaderDisconnect();
void OnMojoDisconnect();
// This UUID is used for both the blob that's served via `blink::mojom::Blob`
// and the blob in the registry. This is crucial because operations such as
// copying the blob to a new file do so by identifying the blob to the blob
// registry using the UUID.
std::string uuid_;
// This is the expected length of the file, which comes from the LevelDB
// record. This acts on a cap on the number of bytes to be read from the file.
// It can even be zero, in which case even a missing file will be treated as
// normal (non-error).
uint64_t blob_length_;
const base::FilePath file_path_;
mojo::ReceiverSet<blink::mojom::Blob> receivers_;
mojo::ReceiverSet<network::mojom::DataPipeGetter> data_pipe_getter_receivers_;
mojo::Receiver<storage::mojom::BlobDataItemReader> reader_{this};
mojo::Remote<blink::mojom::Blob> registry_blob_;
base::OnceClosure on_last_receiver_disconnected_;
};
} // namespace content::indexed_db
#endif // CONTENT_BROWSER_INDEXED_DB_BLOB_READER_H_