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