OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 STORAGE_BROWSER_BLOB_BLOB_READER_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_READER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 #include <map> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "net/base/completion_callback.h" |
| 16 #include "net/http/http_byte_range.h" |
| 17 #include "net/http/http_status_code.h" |
| 18 #include "storage/browser/storage_browser_export.h" |
| 19 |
| 20 class GURL; |
| 21 |
| 22 namespace base { |
| 23 class FilePath; |
| 24 class SequencedTaskRunner; |
| 25 class TaskRunner; |
| 26 class Time; |
| 27 } |
| 28 |
| 29 namespace net { |
| 30 class DrainableIOBuffer; |
| 31 class IOBuffer; |
| 32 } |
| 33 |
| 34 namespace storage { |
| 35 class BlobDataItem; |
| 36 class BlobDataHandle; |
| 37 class BlobDataSnapshot; |
| 38 class FileStreamReader; |
| 39 class FileSystemContext; |
| 40 |
| 41 // The blob reader is used to read a blob. This can only be used in the browser |
| 42 // process, and we need to be on the IO thread. |
| 43 // * There can only be one read happening at a time per reader. |
| 44 // * If a status of Status::NET_ERROR is returned, that means there was an |
| 45 // error and the net_error() variable contains the error code. |
| 46 // Use a BlobDataHandle to create an instance. |
| 47 class STORAGE_EXPORT BlobReader { |
| 48 public: |
| 49 class STORAGE_EXPORT FileStreamReaderProvider { |
| 50 public: |
| 51 virtual ~FileStreamReaderProvider(); |
| 52 |
| 53 virtual scoped_ptr<FileStreamReader> CreateForLocalFile( |
| 54 base::TaskRunner* task_runner, |
| 55 const base::FilePath& file_path, |
| 56 int64_t initial_offset, |
| 57 const base::Time& expected_modification_time) = 0; |
| 58 |
| 59 virtual scoped_ptr<FileStreamReader> CreateFileStreamReader( |
| 60 const GURL& filesystem_url, |
| 61 int64_t offset, |
| 62 int64_t max_bytes_to_read, |
| 63 const base::Time& expected_modification_time) = 0; |
| 64 }; |
| 65 enum class Status { NET_ERROR, IO_PENDING, DONE }; |
| 66 virtual ~BlobReader(); |
| 67 |
| 68 // This calculates the total size of the blob, and initializes the reading |
| 69 // cursor. |
| 70 // * This should only be called once per reader. |
| 71 // * Status::Done means that the total_size() value is populated and you can |
| 72 // continue to SetReadRange or Read. |
| 73 // * The 'done' callback is only called if Status::IO_PENDING is returned. |
| 74 // The callback value contains the error code or net::OK. Please use the |
| 75 // total_size() value to query the blob size, as it's uint64_t. |
| 76 Status CalculateSize(net::CompletionCallback done); |
| 77 |
| 78 // Used to set the read position. |
| 79 // * This should be called after CalculateSize and before Read. |
| 80 // * Range can only be set once. |
| 81 Status SetReadRange(uint64_t position, uint64_t length); |
| 82 |
| 83 // Reads a portion of the data. |
| 84 // * CalculateSize (and optionally SetReadRange) must be called beforehand. |
| 85 // * bytes_read is populated only if Status::DONE is returned. Otherwise the |
| 86 // bytes read (or error code) is populated in the 'done' callback. |
| 87 // * The done callback is only called if Status::IO_PENDING is returned. |
| 88 // * This method can be called multiple times. A bytes_read value (either from |
| 89 // the callback for Status::IO_PENDING or the bytes_read value for |
| 90 // Status::DONE) of 0 means we're finished reading. |
| 91 Status Read(net::IOBuffer* buffer, |
| 92 size_t dest_size, |
| 93 int* bytes_read, |
| 94 net::CompletionCallback done); |
| 95 |
| 96 // Kills reading and invalidates all callbacks. The reader cannot be used |
| 97 // after this call. |
| 98 void Kill(); |
| 99 |
| 100 // Returns if all of the blob's items are in memory. |
| 101 bool IsInMemory() const; |
| 102 |
| 103 // Returns the remaining bytes to be read in the blob. This is populated |
| 104 // after CalculateSize, and is modified by SetReadRange. |
| 105 uint64_t remaining_bytes() const { return remaining_bytes_; } |
| 106 |
| 107 // Returns the net error code if there was an error. Defaults to net::OK. |
| 108 int net_error() const { return net_error_; } |
| 109 |
| 110 // Returns the total size of the blob. This is populated after CalculateSize |
| 111 // is called. |
| 112 uint64_t total_size() const { return total_size_; } |
| 113 |
| 114 protected: |
| 115 friend class BlobDataHandle; |
| 116 friend class BlobReaderTest; |
| 117 |
| 118 BlobReader(const BlobDataHandle* blob_handle, |
| 119 scoped_ptr<FileStreamReaderProvider> file_stream_provider, |
| 120 base::SequencedTaskRunner* file_task_runner); |
| 121 |
| 122 bool total_size_calculated() const { return total_size_calculated_; } |
| 123 |
| 124 private: |
| 125 Status ReportError(int net_error); |
| 126 void InvalidateCallbacksAndDone(int net_error, net::CompletionCallback done); |
| 127 |
| 128 bool AddItemLength(size_t index, uint64_t length); |
| 129 bool ResolveFileItemLength(const BlobDataItem& item, |
| 130 int64_t total_length, |
| 131 uint64_t* output_length); |
| 132 void DidGetFileItemLength(size_t index, int64_t result); |
| 133 void DidCountSize(); |
| 134 |
| 135 // For reading the blob. |
| 136 // Returns if we're done, PENDING_IO if we're waiting on async. |
| 137 Status ReadLoop(int* bytes_read); |
| 138 // Called from asynchronously called methods to continue the read loop. |
| 139 void ContinueAsyncReadLoop(); |
| 140 // PENDING_IO means we're waiting on async. |
| 141 Status ReadItem(); |
| 142 void AdvanceItem(); |
| 143 void AdvanceBytesRead(int result); |
| 144 void ReadBytesItem(const BlobDataItem& item, int bytes_to_read); |
| 145 BlobReader::Status ReadFileItem(FileStreamReader* reader, int bytes_to_read); |
| 146 void DidReadFile(int result); |
| 147 void DeleteCurrentFileReader(); |
| 148 Status ReadDiskCacheEntryItem(const BlobDataItem& item, int bytes_to_read); |
| 149 void DidReadDiskCacheEntry(int result); |
| 150 int ComputeBytesToRead() const; |
| 151 int BytesReadCompleted(); |
| 152 |
| 153 // Returns a FileStreamReader for a blob item at |index|. |
| 154 // If the item at |index| is not of file this returns NULL. |
| 155 FileStreamReader* GetOrCreateFileReaderAtIndex(size_t index); |
| 156 // If the reader is null, then this basically performs a delete operation. |
| 157 void SetFileReaderAtIndex(size_t index, scoped_ptr<FileStreamReader> reader); |
| 158 // Creates a FileStreamReader for the item with additional_offset. |
| 159 scoped_ptr<FileStreamReader> CreateFileStreamReader( |
| 160 const BlobDataItem& item, |
| 161 uint64_t additional_offset); |
| 162 |
| 163 scoped_ptr<BlobDataSnapshot> blob_data_; |
| 164 scoped_ptr<FileStreamReaderProvider> file_stream_provider_; |
| 165 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 166 |
| 167 int net_error_; |
| 168 bool item_list_populated_; |
| 169 std::vector<uint64_t> item_length_list_; |
| 170 |
| 171 scoped_refptr<net::DrainableIOBuffer> read_buf_; |
| 172 |
| 173 bool total_size_calculated_; |
| 174 uint64_t total_size_; |
| 175 uint64_t remaining_bytes_; |
| 176 size_t pending_get_file_info_count_; |
| 177 std::map<size_t, FileStreamReader*> index_to_reader_; |
| 178 size_t current_item_index_; |
| 179 uint64_t current_item_offset_; |
| 180 |
| 181 bool io_pending_; |
| 182 |
| 183 net::CompletionCallback size_callback_; |
| 184 net::CompletionCallback read_callback_; |
| 185 |
| 186 base::WeakPtrFactory<BlobReader> weak_factory_; |
| 187 DISALLOW_COPY_AND_ASSIGN(BlobReader); |
| 188 }; |
| 189 |
| 190 } // namespace storage |
| 191 #endif // STORAGE_BROWSER_BLOB_BLOB_READER_H_ |
OLD | NEW |