1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_INDEXED_DB_FILE_STREAM_READER_TO_DATA_PIPE_H_ 6 #define CONTENT_BROWSER_INDEXED_DB_FILE_STREAM_READER_TO_DATA_PIPE_H_ 7 8 #include "base/callback_forward.h" 9 #include "base/memory/weak_ptr.h" 10 #include "content/common/content_export.h" 11 #include "mojo/public/cpp/system/data_pipe.h" 12 #include "mojo/public/cpp/system/simple_watcher.h" 13 #include "services/network/public/cpp/net_adapters.h" 14 #include "storage/browser/file_system/file_stream_reader.h" 15 16 namespace content { 17 18 // A convenient adapter class to read out data from a FileStreamReader 19 // and write them into a data pipe. 20 class CONTENT_EXPORT FileStreamReaderToDataPipe { 21 public: 22 // Reads out the data from |reader| and write into |dest|. 23 // Can be called from any sequence. 24 FileStreamReaderToDataPipe(std::unique_ptr<storage::FileStreamReader> reader, 25 mojo::ScopedDataPipeProducerHandle dest); 26 ~FileStreamReaderToDataPipe(); 27 28 // Start reading the source. After this call, FileStreamReaderToDataPipe is 29 // now bound to the current sequence and will make internal callbacks (and 30 // reader) callbacks on this sequence. 31 void Start(base::OnceCallback<void(int)> completion_callback, 32 uint64_t read_length); TransferredBytes()33 int64_t TransferredBytes() const { return transferred_bytes_; } 34 35 private: 36 void ReadMore(); 37 void DidRead(int result); 38 39 void OnDataPipeWritable(MojoResult result); 40 void OnDataPipeClosed(MojoResult result); 41 void OnComplete(int result); 42 43 std::unique_ptr<storage::FileStreamReader> reader_; 44 mojo::ScopedDataPipeProducerHandle dest_; 45 base::OnceCallback<void(int)> completion_callback_; 46 uint64_t transferred_bytes_ = 0; 47 uint64_t read_length_ = 0; 48 49 scoped_refptr<network::NetToMojoPendingBuffer> pending_write_; 50 // Optional so that its construction can be deferred. 51 base::Optional<mojo::SimpleWatcher> writable_handle_watcher_; 52 53 base::WeakPtrFactory<FileStreamReaderToDataPipe> weak_factory_{this}; 54 }; 55 56 } // namespace content 57 58 #endif // CONTENT_BROWSER_INDEXED_DB_FILE_STREAM_READER_TO_DATA_PIPE_H_ 59