| 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_create_snapshot_(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_create_snapshot_); |
| 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_create_snapshot_ = true; |
| 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& read_closure, |
| 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_create_snapshot_); |
| 81 DCHECK(!local_file_reader_.get()); |
| 82 has_pending_create_snapshot_ = false; |
| 83 |
| 84 if (file_error != base::PLATFORM_FILE_OK) { |
| 85 callback.Run(LocalFileReader::PlatformFileErrorToNetError(file_error)); |
| 86 return; |
| 87 } |
| 88 |
| 89 // Keep the reference (if it's non-null) so that the file won't go away. |
| 90 snapshot_ref_ = file_ref; |
| 91 |
| 92 local_file_reader_.reset( |
| 93 new LocalFileReader(file_thread_proxy_, |
| 94 platform_path, |
| 95 initial_offset_, |
| 96 base::Time())); |
| 97 |
| 98 read_closure.Run(); |
| 99 } |
| 100 |
| 101 } // namespace fileapi |
| OLD | NEW |