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

Side by Side Diff: chrome/browser/chromeos/file_system_provider/fileapi/file_stream_reader.cc

Issue 301973007: [fsp] Fix crash when reading files. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/chromeos/file_system_provider/operations/read_file.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/chromeos/file_system_provider/fileapi/file_stream_reade r.h" 5 #include "chrome/browser/chromeos/file_system_provider/fileapi/file_stream_reade r.h"
6 6
7 #include "base/files/file.h" 7 #include "base/files/file.h"
8 #include "base/memory/ref_counted.h"
8 #include "chrome/browser/chromeos/file_system_provider/fileapi/provider_async_fi le_util.h" 9 #include "chrome/browser/chromeos/file_system_provider/fileapi/provider_async_fi le_util.h"
9 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" 10 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h"
10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte rface.h" 11 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte rface.h"
11 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h" 14 #include "net/base/net_errors.h"
13 15
14 using content::BrowserThread; 16 using content::BrowserThread;
15 17
16 namespace chromeos { 18 namespace chromeos {
17 namespace file_system_provider { 19 namespace file_system_provider {
18 namespace { 20 namespace {
19 21
20 // Dicards the callback from CloseFile(). 22 // Dicards the callback from CloseFile().
21 void EmptyStatusCallback(base::File::Error /* result */) { 23 void EmptyStatusCallback(base::File::Error /* result */) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 file_system->CloseFile(file_handle, base::Bind(&EmptyStatusCallback)); 77 file_system->CloseFile(file_handle, base::Bind(&EmptyStatusCallback));
76 } 78 }
77 79
78 // Requests reading contents of a file. In case of either success or a failure 80 // Requests reading contents of a file. In case of either success or a failure
79 // |callback| is executed. It can be called many times, until |has_next| is set 81 // |callback| is executed. It can be called many times, until |has_next| is set
80 // to false. This function guarantees that it will succeed only if the file has 82 // to false. This function guarantees that it will succeed only if the file has
81 // not been changed while reading. Must be called on UI thread. 83 // not been changed while reading. Must be called on UI thread.
82 void ReadFileOnUIThread( 84 void ReadFileOnUIThread(
83 base::WeakPtr<ProvidedFileSystemInterface> file_system, 85 base::WeakPtr<ProvidedFileSystemInterface> file_system,
84 int file_handle, 86 int file_handle,
85 net::IOBuffer* buffer, 87 net::IOBuffer* buffer,
kinaba 2014/05/30 04:42:10 What about taking scoped_refptr<net::IOBuffer> as
mtomasz 2014/06/02 00:47:52 Done.
86 int64 offset, 88 int64 offset,
87 int length, 89 int length,
88 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { 90 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) {
89 DCHECK_CURRENTLY_ON(BrowserThread::UI); 91 DCHECK_CURRENTLY_ON(BrowserThread::UI);
90 92
91 // If the file system got unmounted, then abort the reading operation. 93 // If the file system got unmounted, then abort the reading operation.
92 if (!file_system.get()) { 94 if (!file_system.get()) {
93 callback.Run(0, false /* has_next */, base::File::FILE_ERROR_ABORT); 95 callback.Run(0, false /* has_next */, base::File::FILE_ERROR_ABORT);
94 return; 96 return;
95 } 97 }
96 98
97 file_system->ReadFile(file_handle, buffer, offset, length, callback); 99 file_system->ReadFile(
100 file_handle, make_scoped_refptr(buffer), offset, length, callback);
98 } 101 }
99 102
100 // Forward the completion callback to IO thread. 103 // Forward the completion callback to IO thread.
101 void OnReadChunkReceivedOnUIThread( 104 void OnReadChunkReceivedOnUIThread(
102 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& 105 const ProvidedFileSystemInterface::ReadChunkReceivedCallback&
103 chunk_received_callback, 106 chunk_received_callback,
104 int chunk_length, 107 int chunk_length,
105 bool has_next, 108 bool has_next,
106 base::File::Error result) { 109 base::File::Error result) {
107 DCHECK_CURRENTLY_ON(BrowserThread::UI); 110 DCHECK_CURRENTLY_ON(BrowserThread::UI);
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 pending_closure.Run(); 202 pending_closure.Run();
200 } 203 }
201 204
202 int FileStreamReader::Read(net::IOBuffer* buffer, 205 int FileStreamReader::Read(net::IOBuffer* buffer,
203 int buffer_length, 206 int buffer_length,
204 const net::CompletionCallback& callback) { 207 const net::CompletionCallback& callback) {
205 // Lazily initialize with the first call to Read(). 208 // Lazily initialize with the first call to Read().
206 if (!file_handle_) { 209 if (!file_handle_) {
207 Initialize(base::Bind(&FileStreamReader::ReadAfterInitialized, 210 Initialize(base::Bind(&FileStreamReader::ReadAfterInitialized,
208 weak_ptr_factory_.GetWeakPtr(), 211 weak_ptr_factory_.GetWeakPtr(),
209 buffer, 212 make_scoped_refptr(buffer),
210 buffer_length, 213 buffer_length,
211 callback), 214 callback),
212 base::Bind(&Int64ToIntCompletionCallback, callback)); 215 base::Bind(&Int64ToIntCompletionCallback, callback));
213 return net::ERR_IO_PENDING; 216 return net::ERR_IO_PENDING;
214 } 217 }
215 218
216 ReadAfterInitialized(buffer, buffer_length, callback); 219 ReadAfterInitialized(buffer, buffer_length, callback);
217 return net::ERR_IO_PENDING; 220 return net::ERR_IO_PENDING;
218 } 221 }
219 222
220 int64 FileStreamReader::GetLength( 223 int64 FileStreamReader::GetLength(
221 const net::Int64CompletionCallback& callback) { 224 const net::Int64CompletionCallback& callback) {
222 // Lazily initialize with the first call to GetLength(). 225 // Lazily initialize with the first call to GetLength().
223 if (!file_handle_) { 226 if (!file_handle_) {
224 Initialize(base::Bind(&FileStreamReader::GetLengthAfterInitialized, 227 Initialize(base::Bind(&FileStreamReader::GetLengthAfterInitialized,
225 weak_ptr_factory_.GetWeakPtr(), 228 weak_ptr_factory_.GetWeakPtr(),
226 callback), 229 callback),
227 callback); 230 callback);
228 return net::ERR_IO_PENDING; 231 return net::ERR_IO_PENDING;
229 } 232 }
230 233
231 GetLengthAfterInitialized(callback); 234 GetLengthAfterInitialized(callback);
232 return net::ERR_IO_PENDING; 235 return net::ERR_IO_PENDING;
233 } 236 }
234 237
235 void FileStreamReader::ReadAfterInitialized( 238 void FileStreamReader::ReadAfterInitialized(
236 net::IOBuffer* buffer, 239 net::IOBuffer* buffer,
kinaba 2014/05/30 04:42:10 ditto.
mtomasz 2014/06/02 00:47:52 Done.
237 int buffer_length, 240 int buffer_length,
238 const net::CompletionCallback& callback) { 241 const net::CompletionCallback& callback) {
239 DCHECK_CURRENTLY_ON(BrowserThread::IO); 242 DCHECK_CURRENTLY_ON(BrowserThread::IO);
240 243
241 // If the file system got unmounted, then abort the reading operation. 244 // If the file system got unmounted, then abort the reading operation.
242 if (!file_handle_) { 245 if (!file_handle_) {
243 callback.Run(net::ERR_ABORTED); 246 callback.Run(net::ERR_ABORTED);
244 return; 247 return;
245 } 248 }
246 249
247 current_length_ = 0; 250 current_length_ = 0;
248 BrowserThread::PostTask( 251 BrowserThread::PostTask(
249 BrowserThread::UI, 252 BrowserThread::UI,
250 FROM_HERE, 253 FROM_HERE,
251 base::Bind(&ReadFileOnUIThread, 254 base::Bind(&ReadFileOnUIThread,
252 file_system_, 255 file_system_,
253 file_handle_, 256 file_handle_,
254 buffer, 257 make_scoped_refptr(buffer),
255 current_offset_, 258 current_offset_,
256 buffer_length, 259 buffer_length,
257 base::Bind(&OnReadChunkReceivedOnUIThread, 260 base::Bind(&OnReadChunkReceivedOnUIThread,
258 base::Bind(&FileStreamReader::OnReadChunkReceived, 261 base::Bind(&FileStreamReader::OnReadChunkReceived,
259 weak_ptr_factory_.GetWeakPtr(), 262 weak_ptr_factory_.GetWeakPtr(),
260 callback)))); 263 callback))));
261 } 264 }
262 265
263 void FileStreamReader::GetLengthAfterInitialized( 266 void FileStreamReader::GetLengthAfterInitialized(
264 const net::Int64CompletionCallback& callback) { 267 const net::Int64CompletionCallback& callback) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 callback.Run(net::FileErrorToNetError(result)); 324 callback.Run(net::FileErrorToNetError(result));
322 return; 325 return;
323 } 326 }
324 327
325 DCHECK_EQ(result, base::File::FILE_OK); 328 DCHECK_EQ(result, base::File::FILE_OK);
326 callback.Run(file_info.size); 329 callback.Run(file_info.size);
327 } 330 }
328 331
329 } // namespace file_system_provider 332 } // namespace file_system_provider
330 } // namespace chromeos 333 } // namespace chromeos
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/chromeos/file_system_provider/operations/read_file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698