| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_READER_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_READER_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_READER_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_READER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
| 16 #include "net/base/net_errors.h" |
| 16 #include "storage/browser/storage_browser_export.h" | 17 #include "storage/browser/storage_browser_export.h" |
| 17 | 18 |
| 18 class GURL; | 19 class GURL; |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 class FilePath; | 22 class FilePath; |
| 22 class SequencedTaskRunner; | 23 class SequencedTaskRunner; |
| 23 class TaskRunner; | 24 class TaskRunner; |
| 24 class Time; | 25 class Time; |
| 25 } | 26 } |
| 26 | 27 |
| 27 namespace net { | 28 namespace net { |
| 28 class DrainableIOBuffer; | 29 class DrainableIOBuffer; |
| 29 class IOBuffer; | 30 class IOBuffer; |
| 30 } | 31 } |
| 31 | 32 |
| 32 namespace storage { | 33 namespace storage { |
| 33 class BlobDataItem; | 34 class BlobDataItem; |
| 34 class BlobDataHandle; | 35 class BlobDataHandle; |
| 35 class BlobDataSnapshot; | 36 class BlobDataSnapshot; |
| 36 class FileStreamReader; | 37 class FileStreamReader; |
| 37 class FileSystemContext; | 38 class FileSystemContext; |
| 38 | 39 |
| 39 // The blob reader is used to read a blob. This can only be used in the browser | 40 // The blob reader is used to read a blob. This can only be used in the browser |
| 40 // process, and we need to be on the IO thread. | 41 // process, and we need to be on the IO thread. |
| 41 // * There can only be one read happening at a time per reader. | 42 // * There can only be one read happening at a time per reader. |
| 42 // * If a status of Status::NET_ERROR is returned, that means there was an | 43 // * If a status of Status::NET_ERROR is returned, that means there was an |
| 43 // error and the net_error() variable contains the error code. | 44 // error and the net_error() variable contains the error code. |
| 45 // * To reuse a reader on the same blob, call the Reset() function. |
| 44 // Use a BlobDataHandle to create an instance. | 46 // Use a BlobDataHandle to create an instance. |
| 45 class STORAGE_EXPORT BlobReader { | 47 class STORAGE_EXPORT BlobReader { |
| 46 public: | 48 public: |
| 47 class STORAGE_EXPORT FileStreamReaderProvider { | 49 class STORAGE_EXPORT FileStreamReaderProvider { |
| 48 public: | 50 public: |
| 49 virtual ~FileStreamReaderProvider(); | 51 virtual ~FileStreamReaderProvider(); |
| 50 | 52 |
| 51 virtual scoped_ptr<FileStreamReader> CreateForLocalFile( | 53 virtual scoped_ptr<FileStreamReader> CreateForLocalFile( |
| 52 base::TaskRunner* task_runner, | 54 base::TaskRunner* task_runner, |
| 53 const base::FilePath& file_path, | 55 const base::FilePath& file_path, |
| 54 int64_t initial_offset, | 56 int64_t initial_offset, |
| 55 const base::Time& expected_modification_time) = 0; | 57 const base::Time& expected_modification_time) = 0; |
| 56 | 58 |
| 57 virtual scoped_ptr<FileStreamReader> CreateFileStreamReader( | 59 virtual scoped_ptr<FileStreamReader> CreateFileStreamReader( |
| 58 const GURL& filesystem_url, | 60 const GURL& filesystem_url, |
| 59 int64_t offset, | 61 int64_t offset, |
| 60 int64_t max_bytes_to_read, | 62 int64_t max_bytes_to_read, |
| 61 const base::Time& expected_modification_time) = 0; | 63 const base::Time& expected_modification_time) = 0; |
| 62 }; | 64 }; |
| 63 enum class Status { NET_ERROR, IO_PENDING, DONE }; | 65 enum class Status { NET_ERROR, IO_PENDING, DONE }; |
| 64 virtual ~BlobReader(); | 66 virtual ~BlobReader(); |
| 65 | 67 |
| 66 // This calculates the total size of the blob, and initializes the reading | 68 // This calculates the total size of the blob, and initializes the reading |
| 67 // cursor. | 69 // cursor. |
| 68 // * This should only be called once per reader. | 70 // * This should only be called once per reader, unless 'Reset' has been |
| 71 // called. |
| 69 // * Status::Done means that the total_size() value is populated and you can | 72 // * Status::Done means that the total_size() value is populated and you can |
| 70 // continue to SetReadRange or Read. | 73 // continue to SetReadRange or Read. |
| 71 // * The 'done' callback is only called if Status::IO_PENDING is returned. | 74 // * The 'done' callback is only called if Status::IO_PENDING is returned. |
| 72 // The callback value contains the error code or net::OK. Please use the | 75 // The callback value contains the error code or net::OK. Please use the |
| 73 // total_size() value to query the blob size, as it's uint64_t. | 76 // total_size() value to query the blob size, as it's uint64_t. |
| 74 Status CalculateSize(const net::CompletionCallback& done); | 77 Status CalculateSize(const net::CompletionCallback& done); |
| 75 | 78 |
| 76 // Used to set the read position. | 79 // Used to set the read position. |
| 77 // * This should be called after CalculateSize and before Read. | 80 // * This should be called after CalculateSize and before Read. |
| 78 // * Range can only be set once. | 81 // * Range can only be set once. |
| 79 Status SetReadRange(uint64_t position, uint64_t length); | 82 Status SetReadRange(uint64_t position, uint64_t length); |
| 80 | 83 |
| 81 // Reads a portion of the data. | 84 // Reads a portion of the data. |
| 82 // * CalculateSize (and optionally SetReadRange) must be called beforehand. | 85 // * CalculateSize (and optionally SetReadRange) must be called beforehand. |
| 83 // * bytes_read is populated only if Status::DONE is returned. Otherwise the | 86 // * bytes_read is populated only if Status::DONE is returned. Otherwise the |
| 84 // bytes read (or error code) is populated in the 'done' callback. | 87 // bytes read (or error code) is populated in the 'done' callback. |
| 85 // * The done callback is only called if Status::IO_PENDING is returned. | 88 // * The done callback is only called if Status::IO_PENDING is returned. |
| 86 // * This method can be called multiple times. A bytes_read value (either from | 89 // * This method can be called multiple times. A bytes_read value (either from |
| 87 // the callback for Status::IO_PENDING or the bytes_read value for | 90 // the callback for Status::IO_PENDING or the bytes_read value for |
| 88 // Status::DONE) of 0 means we're finished reading. | 91 // Status::DONE) of 0 means we're finished reading. |
| 89 Status Read(net::IOBuffer* buffer, | 92 Status Read(net::IOBuffer* buffer, |
| 90 size_t dest_size, | 93 size_t dest_size, |
| 91 int* bytes_read, | 94 int* bytes_read, |
| 92 net::CompletionCallback done); | 95 net::CompletionCallback done); |
| 93 | 96 |
| 94 // Kills reading and invalidates all callbacks. The reader cannot be used | 97 // Kills reading and invalidates all callbacks. The reader cannot be used |
| 95 // after this call. | 98 // after this call. |
| 96 void Kill(); | 99 void Kill(); |
| 97 | 100 |
| 101 // This resets the reader so we can use it again. Usually used in conjunction |
| 102 // with reader_used(). This will invalidate all callbacks. |
| 103 void Reset(); |
| 104 |
| 98 // Returns if all of the blob's items are in memory. | 105 // Returns if all of the blob's items are in memory. |
| 99 bool IsInMemory() const; | 106 bool IsInMemory() const; |
| 100 | 107 |
| 101 // Returns the remaining bytes to be read in the blob. This is populated | 108 // Returns the remaining bytes to be read in the blob. This is populated |
| 102 // after CalculateSize, and is modified by SetReadRange. | 109 // after CalculateSize, and is modified by SetReadRange. |
| 103 uint64_t remaining_bytes() const { return remaining_bytes_; } | 110 uint64_t remaining_bytes() const { return remaining_bytes_; } |
| 104 | 111 |
| 105 // Returns the net error code if there was an error. Defaults to net::OK. | 112 // Returns the net error code if there was an error. Defaults to net::OK. |
| 106 int net_error() const { return net_error_; } | 113 int net_error() const { return net_error_; } |
| 107 | 114 |
| 108 // Returns the total size of the blob. This is populated after CalculateSize | 115 // Returns the total size of the blob. This is populated after CalculateSize |
| 109 // is called. | 116 // is called. |
| 110 uint64_t total_size() const { return total_size_; } | 117 uint64_t total_size() const { return total_size_; } |
| 111 | 118 |
| 119 // Returns if this reader has been used yet. |
| 120 bool reader_used() const { return reader_used_; } |
| 121 |
| 112 protected: | 122 protected: |
| 113 friend class BlobDataHandle; | 123 friend class BlobDataHandle; |
| 114 friend class BlobReaderTest; | 124 friend class BlobReaderTest; |
| 115 | 125 |
| 116 BlobReader(const BlobDataHandle* blob_handle, | 126 BlobReader(const BlobDataHandle* blob_handle, |
| 117 scoped_ptr<FileStreamReaderProvider> file_stream_provider, | 127 scoped_ptr<FileStreamReaderProvider> file_stream_provider, |
| 118 base::SequencedTaskRunner* file_task_runner); | 128 base::SequencedTaskRunner* file_task_runner); |
| 119 | 129 |
| 120 bool total_size_calculated() const { return total_size_calculated_; } | 130 bool total_size_calculated() const { return total_size_calculated_; } |
| 121 | 131 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 void SetFileReaderAtIndex(size_t index, scoped_ptr<FileStreamReader> reader); | 166 void SetFileReaderAtIndex(size_t index, scoped_ptr<FileStreamReader> reader); |
| 157 // Creates a FileStreamReader for the item with additional_offset. | 167 // Creates a FileStreamReader for the item with additional_offset. |
| 158 scoped_ptr<FileStreamReader> CreateFileStreamReader( | 168 scoped_ptr<FileStreamReader> CreateFileStreamReader( |
| 159 const BlobDataItem& item, | 169 const BlobDataItem& item, |
| 160 uint64_t additional_offset); | 170 uint64_t additional_offset); |
| 161 | 171 |
| 162 scoped_ptr<BlobDataSnapshot> blob_data_; | 172 scoped_ptr<BlobDataSnapshot> blob_data_; |
| 163 scoped_ptr<FileStreamReaderProvider> file_stream_provider_; | 173 scoped_ptr<FileStreamReaderProvider> file_stream_provider_; |
| 164 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | 174 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 165 | 175 |
| 166 int net_error_; | 176 int net_error_ = net::OK; |
| 167 bool item_list_populated_ = false; | |
| 168 std::vector<uint64_t> item_length_list_; | 177 std::vector<uint64_t> item_length_list_; |
| 169 | 178 |
| 170 scoped_refptr<net::DrainableIOBuffer> read_buf_; | 179 scoped_refptr<net::DrainableIOBuffer> read_buf_; |
| 171 | 180 |
| 181 bool reader_used_ = false; |
| 172 bool total_size_calculated_ = false; | 182 bool total_size_calculated_ = false; |
| 173 uint64_t total_size_ = 0; | 183 uint64_t total_size_ = 0; |
| 174 uint64_t remaining_bytes_ = 0; | 184 uint64_t remaining_bytes_ = 0; |
| 175 size_t pending_get_file_info_count_ = 0; | 185 size_t pending_get_file_info_count_ = 0; |
| 176 std::map<size_t, FileStreamReader*> index_to_reader_; | 186 std::map<size_t, scoped_ptr<FileStreamReader>> index_to_reader_; |
| 177 size_t current_item_index_ = 0; | 187 size_t current_item_index_ = 0; |
| 178 uint64_t current_item_offset_ = 0; | 188 uint64_t current_item_offset_ = 0; |
| 179 | 189 |
| 180 bool io_pending_ = false; | 190 bool io_pending_ = false; |
| 181 | 191 |
| 182 net::CompletionCallback size_callback_; | 192 net::CompletionCallback size_callback_; |
| 183 net::CompletionCallback read_callback_; | 193 net::CompletionCallback read_callback_; |
| 184 | 194 |
| 185 base::WeakPtrFactory<BlobReader> weak_factory_; | 195 base::WeakPtrFactory<BlobReader> weak_factory_; |
| 186 DISALLOW_COPY_AND_ASSIGN(BlobReader); | 196 DISALLOW_COPY_AND_ASSIGN(BlobReader); |
| 187 }; | 197 }; |
| 188 | 198 |
| 189 } // namespace storage | 199 } // namespace storage |
| 190 #endif // STORAGE_BROWSER_BLOB_BLOB_READER_H_ | 200 #endif // STORAGE_BROWSER_BLOB_BLOB_READER_H_ |
| OLD | NEW |