Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(404)

Side by Side Diff: storage/browser/blob/blob_reader.h

Issue 1337153002: [Blob] BlobReader class & tests, and removal of all redundant reading. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: windows fix Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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. 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 FileStreamReaderProvider {
50 public:
51 virtual ~FileStreamReaderProvider();
52
53 virtual FileStreamReader* CreateForLocalFile(
michaeln 2015/09/17 00:45:37 probably should use scoped_ptr<FileStreamReader> a
dmurph 2015/09/19 00:33:43 Done.
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 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 size or error code, but please use the
75 // total_size() value to query the blob size, as it's uint64_t.
michaeln 2015/09/17 00:45:37 since the callback can't return the size if the va
dmurph 2015/09/19 00:33:43 Sounds good, it'll just return net::OK when succes
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 is Status::DONE is returned. Otherwise the
michaeln 2015/09/17 00:45:37 typo: only if
dmurph 2015/09/19 00:33:43 Done.
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::SingleThreadTaskRunner* 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 void ContinueAsyncReadLoop();
139 // PENDING_IO means we're waiting on async.
140 Status ReadItem();
141 void AdvanceItem();
142 void AdvanceBytesRead(int result);
143 void ReadBytesItem(const BlobDataItem& item, int bytes_to_read);
144 BlobReader::Status ReadFileItem(FileStreamReader* reader, int bytes_to_read);
145 void DidReadFile(int result);
146 void DeleteCurrentFileReader();
147 BlobReader::Status ReadDiskCacheEntryItem(const BlobDataItem& item,
michaeln 2015/09/17 00:45:37 BlobReader:: prefix not needed
dmurph 2015/09/19 00:33:43 Done.
148 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* FileReaderAtIndex(size_t index);
156 void SetFileReaderAtIndex(size_t index, FileStreamReader* reader);
157 // Creates a FileStreamReader for the item with additional_offset.
158 FileStreamReader* CreateFileStreamReader(const BlobDataItem& item,
159 uint64_t additional_offset);
160
161 const BlobDataHandle* blob_handle_;
michaeln 2015/09/17 00:45:37 why a rawptr? oh, see .cc comments, is this really
dmurph 2015/09/19 00:33:43 Done.
162 scoped_ptr<BlobDataSnapshot> blob_data_;
163 scoped_ptr<FileStreamReaderProvider> file_stream_provider_;
164 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
165
166 int net_error_;
167 bool item_list_populated_;
168 std::vector<uint64_t> item_length_list_;
169
170 scoped_refptr<net::DrainableIOBuffer> read_buf_;
171
172 bool total_size_calculated_;
173 uint64_t total_size_;
174 uint64_t remaining_bytes_;
175 size_t pending_get_file_info_count_;
176 std::map<size_t, FileStreamReader*> index_to_reader_;
177 size_t current_item_index_;
178 uint64_t current_item_offset_;
179
180 bool io_pending_;
181
182 net::CompletionCallback size_done_;
183 net::CompletionCallback read_done_;
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698