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

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: Addressed comments. 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
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 scoped_refptr<net::IOBuffer> buffer,
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 }
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 pending_closure.Run(); 201 pending_closure.Run();
200 } 202 }
201 203
202 int FileStreamReader::Read(net::IOBuffer* buffer, 204 int FileStreamReader::Read(net::IOBuffer* buffer,
203 int buffer_length, 205 int buffer_length,
204 const net::CompletionCallback& callback) { 206 const net::CompletionCallback& callback) {
205 // Lazily initialize with the first call to Read(). 207 // Lazily initialize with the first call to Read().
206 if (!file_handle_) { 208 if (!file_handle_) {
207 Initialize(base::Bind(&FileStreamReader::ReadAfterInitialized, 209 Initialize(base::Bind(&FileStreamReader::ReadAfterInitialized,
208 weak_ptr_factory_.GetWeakPtr(), 210 weak_ptr_factory_.GetWeakPtr(),
209 buffer, 211 make_scoped_refptr(buffer),
210 buffer_length, 212 buffer_length,
211 callback), 213 callback),
212 base::Bind(&Int64ToIntCompletionCallback, callback)); 214 base::Bind(&Int64ToIntCompletionCallback, callback));
213 return net::ERR_IO_PENDING; 215 return net::ERR_IO_PENDING;
214 } 216 }
215 217
216 ReadAfterInitialized(buffer, buffer_length, callback); 218 ReadAfterInitialized(buffer, buffer_length, callback);
217 return net::ERR_IO_PENDING; 219 return net::ERR_IO_PENDING;
218 } 220 }
219 221
220 int64 FileStreamReader::GetLength( 222 int64 FileStreamReader::GetLength(
221 const net::Int64CompletionCallback& callback) { 223 const net::Int64CompletionCallback& callback) {
222 // Lazily initialize with the first call to GetLength(). 224 // Lazily initialize with the first call to GetLength().
223 if (!file_handle_) { 225 if (!file_handle_) {
224 Initialize(base::Bind(&FileStreamReader::GetLengthAfterInitialized, 226 Initialize(base::Bind(&FileStreamReader::GetLengthAfterInitialized,
225 weak_ptr_factory_.GetWeakPtr(), 227 weak_ptr_factory_.GetWeakPtr(),
226 callback), 228 callback),
227 callback); 229 callback);
228 return net::ERR_IO_PENDING; 230 return net::ERR_IO_PENDING;
229 } 231 }
230 232
231 GetLengthAfterInitialized(callback); 233 GetLengthAfterInitialized(callback);
232 return net::ERR_IO_PENDING; 234 return net::ERR_IO_PENDING;
233 } 235 }
234 236
235 void FileStreamReader::ReadAfterInitialized( 237 void FileStreamReader::ReadAfterInitialized(
236 net::IOBuffer* buffer, 238 scoped_refptr<net::IOBuffer> buffer,
237 int buffer_length, 239 int buffer_length,
238 const net::CompletionCallback& callback) { 240 const net::CompletionCallback& callback) {
239 DCHECK_CURRENTLY_ON(BrowserThread::IO); 241 DCHECK_CURRENTLY_ON(BrowserThread::IO);
240 242
241 // If the file system got unmounted, then abort the reading operation. 243 // If the file system got unmounted, then abort the reading operation.
242 if (!file_handle_) { 244 if (!file_handle_) {
243 callback.Run(net::ERR_ABORTED); 245 callback.Run(net::ERR_ABORTED);
244 return; 246 return;
245 } 247 }
246 248
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 callback.Run(net::FileErrorToNetError(result)); 323 callback.Run(net::FileErrorToNetError(result));
322 return; 324 return;
323 } 325 }
324 326
325 DCHECK_EQ(result, base::File::FILE_OK); 327 DCHECK_EQ(result, base::File::FILE_OK);
326 callback.Run(file_info.size); 328 callback.Run(file_info.size);
327 } 329 }
328 330
329 } // namespace file_system_provider 331 } // namespace file_system_provider
330 } // namespace chromeos 332 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698