// Copyright 2022 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef COMPONENTS_WEB_PACKAGE_SHARED_FILE_H_ #define COMPONENTS_WEB_PACKAGE_SHARED_FILE_H_ #include #include "base/callback.h" #include "base/files/file.h" #include "base/files/file_path.h" #include "base/memory/ref_counted.h" #include "mojo/public/cpp/system/data_pipe_producer.h" namespace web_package { // A simple wrapper class to share a single base::File instance among multiple // SharedFileDataSource instances. class SharedFile final : public base::RefCountedThreadSafe { public: // The callback passed to the constructor will run on a thread that allows // blocking disk IO. explicit SharedFile( base::OnceCallback()> open_file_callback); SharedFile(const SharedFile&) = delete; SharedFile& operator=(const SharedFile&) = delete; void DuplicateFile(base::OnceCallback callback); base::File* operator->(); class SharedFileDataSource final : public mojo::DataPipeProducer::DataSource { public: SharedFileDataSource(scoped_refptr file, uint64_t offset, uint64_t length); SharedFileDataSource(const SharedFileDataSource&) = delete; SharedFileDataSource& operator=(const SharedFileDataSource&) = delete; ~SharedFileDataSource() override; private: // Implements mojo::DataPipeProducer::DataSource. Following methods are // called on a blockable sequenced task runner. uint64_t GetLength() const override; ReadResult Read(uint64_t offset, base::span buffer) override; scoped_refptr file_; MojoResult error_; const uint64_t offset_; const uint64_t length_; }; std::unique_ptr CreateDataSource(uint64_t offset, uint64_t length); private: friend class base::RefCountedThreadSafe; ~SharedFile(); void SetFile(std::unique_ptr file); base::FilePath file_path_; std::unique_ptr file_; base::OnceCallback duplicate_callback_; }; } // namespace web_package #endif // COMPONENTS_WEB_PACKAGE_SHARED_FILE_H_