| 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 SingleThreadTaskRunner; |
| 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. There can only be one read happening |
| 42 // at a time per reader. |
| 43 class STORAGE_EXPORT BlobReader { |
| 44 public: |
| 45 class FileStreamReaderProvider { |
| 46 public: |
| 47 virtual ~FileStreamReaderProvider(); |
| 48 |
| 49 virtual FileStreamReader* CreateForLocalFile( |
| 50 base::TaskRunner* task_runner, |
| 51 const base::FilePath& file_path, |
| 52 int64_t initial_offset, |
| 53 const base::Time& expected_modification_time) = 0; |
| 54 |
| 55 virtual FileStreamReader* CreateFileStreamReader( |
| 56 const GURL& filesystem_url, |
| 57 int64_t offset, |
| 58 int64_t max_bytes_to_read, |
| 59 const base::Time& expected_modification_time) = 0; |
| 60 }; |
| 61 enum class Status { NET_ERROR, PENDING_IO, DONE }; |
| 62 virtual ~BlobReader(); |
| 63 |
| 64 // Done is only called if net::IO_PENDING is returned. |
| 65 // This calculates the total size of the blob, and initializes the reading |
| 66 // cursor. This can only be called once on the reader. |
| 67 Status CalculateSize(net::CompletionCallback done); |
| 68 |
| 69 // Used to set the read position. |
| 70 Status SetReadRange(uint64_t position, uint64_t length); |
| 71 |
| 72 // Done is called only if net::IO_PENDING is returned. |
| 73 // Reads a portion of the data. |
| 74 Status Read(net::IOBuffer* buffer, |
| 75 size_t dest_size, |
| 76 int* bytes_read, |
| 77 net::CompletionCallback done); |
| 78 |
| 79 void Kill(); |
| 80 |
| 81 bool IsInMemory() const; |
| 82 |
| 83 uint64_t remaining_bytes() const { return remaining_bytes_; } |
| 84 |
| 85 int net_error() const { return net_error_; } |
| 86 |
| 87 bool total_size_calculated() const { return total_size_calculated_; } |
| 88 |
| 89 uint64_t total_size() const { return total_size_; } |
| 90 |
| 91 protected: |
| 92 friend class BlobDataHandle; |
| 93 friend class BlobReaderTest; |
| 94 BlobReader(const BlobDataHandle* blob_handle, |
| 95 scoped_ptr<FileStreamReaderProvider> file_stream_provider, |
| 96 base::SingleThreadTaskRunner* file_task_runner); |
| 97 |
| 98 private: |
| 99 Status ReportError(int net_error); |
| 100 void InvalidateCallbacksAndDone(int net_error, net::CompletionCallback done); |
| 101 |
| 102 bool AddItemLength(size_t index, uint64_t length); |
| 103 bool ResolveFileItemLength(const BlobDataItem& item, |
| 104 int64_t total_length, |
| 105 uint64_t* output_length); |
| 106 void DidGetFileItemLength(size_t index, int64_t result); |
| 107 void DidCountSize(); |
| 108 |
| 109 // For reading the blob. |
| 110 // Returns if we're done, PENDING_IO if we're waiting on async. |
| 111 Status ReadLoop(int* bytes_read); |
| 112 void ContinueAsyncReadLoop(); |
| 113 // PENDING_IO means we're waiting on async. |
| 114 Status ReadItem(); |
| 115 void AdvanceItem(); |
| 116 void AdvanceBytesRead(int result); |
| 117 void ReadBytesItem(const BlobDataItem& item, int bytes_to_read); |
| 118 BlobReader::Status ReadFileItem(FileStreamReader* reader, int bytes_to_read); |
| 119 void DidReadFile(int result); |
| 120 void DeleteCurrentFileReader(); |
| 121 BlobReader::Status ReadDiskCacheEntryItem(const BlobDataItem& item, |
| 122 int bytes_to_read); |
| 123 void DidReadDiskCacheEntry(int result); |
| 124 int ComputeBytesToRead() const; |
| 125 int BytesReadCompleted(); |
| 126 |
| 127 // Returns a FileStreamReader for a blob item at |index|. |
| 128 // If the item at |index| is not of file this returns NULL. |
| 129 FileStreamReader* FileReaderAtIndex(size_t index); |
| 130 void SetFileReaderAtIndex(size_t index, FileStreamReader* reader); |
| 131 // Creates a FileStreamReader for the item with additional_offset. |
| 132 FileStreamReader* CreateFileStreamReader(const BlobDataItem& item, |
| 133 uint64_t additional_offset); |
| 134 |
| 135 const BlobDataHandle* blob_handle_; |
| 136 scoped_ptr<BlobDataSnapshot> blob_data_; |
| 137 scoped_ptr<FileStreamReaderProvider> file_stream_provider_; |
| 138 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
| 139 |
| 140 int net_error_; |
| 141 bool item_list_populated_; |
| 142 std::vector<uint64_t> item_length_list_; |
| 143 |
| 144 scoped_refptr<net::DrainableIOBuffer> read_buf_; |
| 145 |
| 146 bool total_size_calculated_; |
| 147 uint64_t total_size_; |
| 148 uint64_t remaining_bytes_; |
| 149 size_t pending_get_file_info_count_; |
| 150 std::map<size_t, FileStreamReader*> index_to_reader_; |
| 151 size_t current_item_index_; |
| 152 uint64_t current_item_offset_; |
| 153 |
| 154 bool io_pending_; |
| 155 bool read_is_async_; |
| 156 |
| 157 net::CompletionCallback size_done_; |
| 158 net::CompletionCallback read_done_; |
| 159 |
| 160 base::WeakPtrFactory<BlobReader> weak_factory_; |
| 161 DISALLOW_COPY_AND_ASSIGN(BlobReader); |
| 162 }; |
| 163 |
| 164 } // namespace storage |
| 165 #endif // STORAGE_BROWSER_BLOB_BLOB_READER_H_ |
| OLD | NEW |