Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 #include "webkit/fileapi/file_system_file_reader.h" | |
| 6 | |
| 7 #include "base/file_util_proxy.h" | |
| 8 #include "base/message_loop_proxy.h" | |
| 9 #include "base/platform_file.h" | |
| 10 #include "net/base/file_stream.h" | |
| 11 #include "net/base/io_buffer.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "webkit/blob/local_file_reader.h" | |
| 14 #include "webkit/fileapi/file_system_context.h" | |
| 15 #include "webkit/fileapi/file_system_operation_interface.h" | |
| 16 | |
| 17 using webkit_blob::LocalFileReader; | |
| 18 | |
| 19 namespace fileapi { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 void ReadAdapter(base::WeakPtr<FileSystemFileReader> reader, | |
| 24 net::IOBuffer* buf, int buf_len, | |
| 25 const net::CompletionCallback& callback) { | |
| 26 if (!reader.get()) | |
| 27 return; | |
| 28 int rv = reader->Read(buf, buf_len, callback); | |
| 29 if (rv != net::ERR_IO_PENDING) | |
| 30 callback.Run(rv); | |
| 31 } | |
| 32 | |
| 33 } | |
| 34 | |
| 35 FileSystemFileReader::FileSystemFileReader( | |
| 36 base::MessageLoopProxy* file_thread_proxy, | |
| 37 FileSystemContext* file_system_context, | |
| 38 const GURL& url, | |
| 39 int64 initial_offset) | |
| 40 : file_thread_proxy_(file_thread_proxy), | |
| 41 file_system_context_(file_system_context), | |
| 42 url_(url), | |
| 43 initial_offset_(initial_offset), | |
| 44 has_pending_operation_(false), | |
| 45 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 46 } | |
| 47 | |
| 48 FileSystemFileReader::~FileSystemFileReader() { | |
| 49 } | |
| 50 | |
| 51 int FileSystemFileReader::Read( | |
| 52 net::IOBuffer* buf, int buf_len, | |
| 53 const net::CompletionCallback& callback) { | |
| 54 if (local_file_reader_.get()) | |
| 55 return local_file_reader_->Read(buf, buf_len, callback); | |
| 56 DCHECK(!has_pending_operation_); | |
| 57 FileSystemOperationInterface* operation = | |
| 58 file_system_context_->CreateFileSystemOperation( | |
| 59 url_, file_thread_proxy_); | |
| 60 if (!operation) | |
| 61 return net::ERR_INVALID_URL; | |
| 62 has_pending_operation_ = true; | |
|
michaeln
2012/04/13 21:14:59
maybe clarify this referes to a pending snapshot a
kinuko
2012/04/16 10:24:21
Done.
| |
| 63 operation->CreateSnapshotFile( | |
| 64 url_, | |
| 65 base::Bind(&FileSystemFileReader::DidCreateSnapshot, | |
| 66 weak_factory_.GetWeakPtr(), | |
| 67 base::Bind(&ReadAdapter, weak_factory_.GetWeakPtr(), | |
| 68 make_scoped_refptr(buf), buf_len, callback), | |
| 69 callback)); | |
| 70 return net::ERR_IO_PENDING; | |
| 71 } | |
| 72 | |
| 73 void FileSystemFileReader::DidCreateSnapshot( | |
| 74 const base::Closure& post_task_closure, | |
|
michaeln
2012/04/13 20:24:51
? maybe call this the 'read_closure' or 'read_cont
kinuko
2012/04/16 10:24:21
Done.
| |
| 75 const net::CompletionCallback& callback, | |
| 76 base::PlatformFileError file_error, | |
| 77 const base::PlatformFileInfo& file_info, | |
| 78 const FilePath& platform_path, | |
| 79 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { | |
| 80 DCHECK(has_pending_operation_); | |
| 81 DCHECK(!local_file_reader_.get()); | |
| 82 has_pending_operation_ = false; | |
| 83 | |
| 84 if (file_error != base::PLATFORM_FILE_OK) { | |
| 85 weak_factory_.InvalidateWeakPtrs(); | |
|
michaeln
2012/04/13 20:24:51
Same question about why call invalidate, is it imp
kinuko
2012/04/16 10:24:21
Makes sense, dropped the line.
| |
| 86 callback.Run(LocalFileReader::PlatformFileErrorToNetError(file_error)); | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 // Keep the reference (if it's non-null) so that the file won't go away. | |
| 91 snapshot_ref_ = file_ref; | |
| 92 | |
| 93 local_file_reader_.reset( | |
| 94 new LocalFileReader(file_thread_proxy_, | |
| 95 platform_path, | |
| 96 initial_offset_, | |
| 97 base::Time())); | |
| 98 | |
| 99 post_task_closure.Run(); | |
| 100 weak_factory_.InvalidateWeakPtrs(); | |
| 101 } | |
| 102 | |
| 103 } // namespace fileapi | |
| OLD | NEW |