OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "chrome/browser/media_galleries/fileapi/readahead_file_stream_reader.h" | 5 #include "chrome/browser/media_galleries/fileapi/readahead_file_stream_reader.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 int ReadaheadFileStreamReader::Read( | 32 int ReadaheadFileStreamReader::Read( |
33 net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { | 33 net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { |
34 DCHECK(!pending_sink_buffer_.get()); | 34 DCHECK(!pending_sink_buffer_.get()); |
35 DCHECK(pending_read_callback_.is_null()); | 35 DCHECK(pending_read_callback_.is_null()); |
36 | 36 |
37 ReadFromSourceIfNeeded(); | 37 ReadFromSourceIfNeeded(); |
38 | 38 |
39 scoped_refptr<net::DrainableIOBuffer> sink = | 39 scoped_refptr<net::DrainableIOBuffer> sink = |
40 new net::DrainableIOBuffer(buf, buf_len); | 40 new net::DrainableIOBuffer(buf, buf_len); |
41 int result = FinishReadFromCacheOrStoredError(sink); | 41 int result = FinishReadFromCacheOrStoredError(sink.get()); |
42 | 42 |
43 // We are waiting for an source read to complete, so save the request. | 43 // We are waiting for an source read to complete, so save the request. |
44 if (result == net::ERR_IO_PENDING) { | 44 if (result == net::ERR_IO_PENDING) { |
45 DCHECK(!pending_sink_buffer_.get()); | 45 DCHECK(!pending_sink_buffer_.get()); |
46 DCHECK(pending_read_callback_.is_null()); | 46 DCHECK(pending_read_callback_.is_null()); |
47 pending_sink_buffer_ = sink; | 47 pending_sink_buffer_ = sink; |
48 pending_read_callback_ = callback; | 48 pending_read_callback_ = callback; |
49 } | 49 } |
50 | 50 |
51 return result; | 51 return result; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 void ReadaheadFileStreamReader::ReadFromSourceIfNeeded() { | 96 void ReadaheadFileStreamReader::ReadFromSourceIfNeeded() { |
97 if (!source_.get() || source_has_pending_read_ || | 97 if (!source_.get() || source_has_pending_read_ || |
98 buffers_.size() >= kDesiredNumberOfBuffers) { | 98 buffers_.size() >= kDesiredNumberOfBuffers) { |
99 return; | 99 return; |
100 } | 100 } |
101 | 101 |
102 source_has_pending_read_ = true; | 102 source_has_pending_read_ = true; |
103 | 103 |
104 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize)); | 104 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize)); |
105 int result = source_->Read( | 105 int result = source_->Read( |
106 buf, | 106 buf.get(), |
107 kBufferSize, | 107 kBufferSize, |
108 base::Bind(&ReadaheadFileStreamReader::OnFinishReadFromSource, | 108 base::Bind(&ReadaheadFileStreamReader::OnFinishReadFromSource, |
109 weak_factory_.GetWeakPtr(), buf)); | 109 weak_factory_.GetWeakPtr(), |
| 110 buf)); |
110 | 111 |
111 if (result != net::ERR_IO_PENDING) | 112 if (result != net::ERR_IO_PENDING) |
112 OnFinishReadFromSource(buf, result); | 113 OnFinishReadFromSource(buf.get(), result); |
113 } | 114 } |
114 | 115 |
115 void ReadaheadFileStreamReader::OnFinishReadFromSource(net::IOBuffer* buf, | 116 void ReadaheadFileStreamReader::OnFinishReadFromSource(net::IOBuffer* buf, |
116 int result) { | 117 int result) { |
117 DCHECK(result != net::ERR_IO_PENDING); | 118 DCHECK(result != net::ERR_IO_PENDING); |
118 DCHECK(source_has_pending_read_); | 119 DCHECK(source_has_pending_read_); |
119 source_has_pending_read_ = false; | 120 source_has_pending_read_ = false; |
120 | 121 |
121 // Either store the data read from |source_|, or store the error code. | 122 // Either store the data read from |source_|, or store the error code. |
122 if (result > 0) { | 123 if (result > 0) { |
(...skipping 11 matching lines...) Expand all Loading... |
134 if (pending_sink_buffer_.get()) { | 135 if (pending_sink_buffer_.get()) { |
135 DCHECK(!pending_read_callback_.is_null()); | 136 DCHECK(!pending_read_callback_.is_null()); |
136 | 137 |
137 // Free the pending callback before running it, as the callback often | 138 // Free the pending callback before running it, as the callback often |
138 // dispatches another read. | 139 // dispatches another read. |
139 scoped_refptr<net::DrainableIOBuffer> sink = pending_sink_buffer_; | 140 scoped_refptr<net::DrainableIOBuffer> sink = pending_sink_buffer_; |
140 pending_sink_buffer_ = NULL; | 141 pending_sink_buffer_ = NULL; |
141 net::CompletionCallback completion_callback = pending_read_callback_; | 142 net::CompletionCallback completion_callback = pending_read_callback_; |
142 pending_read_callback_.Reset(); | 143 pending_read_callback_.Reset(); |
143 | 144 |
144 completion_callback.Run(FinishReadFromCacheOrStoredError(sink)); | 145 completion_callback.Run(FinishReadFromCacheOrStoredError(sink.get())); |
145 } | 146 } |
146 } | 147 } |
OLD | NEW |