| 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/blob/local_file_stream_reader.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/files/file_util_proxy.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/platform_file.h" | |
| 12 #include "base/task_runner.h" | |
| 13 #include "net/base/file_stream.h" | |
| 14 #include "net/base/io_buffer.h" | |
| 15 #include "net/base/net_errors.h" | |
| 16 | |
| 17 namespace webkit_blob { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 const int kOpenFlagsForRead = base::PLATFORM_FILE_OPEN | | |
| 22 base::PLATFORM_FILE_READ | | |
| 23 base::PLATFORM_FILE_ASYNC; | |
| 24 | |
| 25 // Verify if the underlying file has not been modified. | |
| 26 bool VerifySnapshotTime(const base::Time& expected_modification_time, | |
| 27 const base::PlatformFileInfo& file_info) { | |
| 28 return expected_modification_time.is_null() || | |
| 29 expected_modification_time.ToTimeT() == | |
| 30 file_info.last_modified.ToTimeT(); | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 LocalFileStreamReader::LocalFileStreamReader( | |
| 36 base::TaskRunner* task_runner, | |
| 37 const base::FilePath& file_path, | |
| 38 int64 initial_offset, | |
| 39 const base::Time& expected_modification_time) | |
| 40 : task_runner_(task_runner), | |
| 41 file_path_(file_path), | |
| 42 initial_offset_(initial_offset), | |
| 43 expected_modification_time_(expected_modification_time), | |
| 44 has_pending_open_(false), | |
| 45 weak_factory_(this) {} | |
| 46 | |
| 47 LocalFileStreamReader::~LocalFileStreamReader() { | |
| 48 } | |
| 49 | |
| 50 int LocalFileStreamReader::Read(net::IOBuffer* buf, int buf_len, | |
| 51 const net::CompletionCallback& callback) { | |
| 52 DCHECK(!has_pending_open_); | |
| 53 if (stream_impl_) | |
| 54 return stream_impl_->Read(buf, buf_len, callback); | |
| 55 return Open(base::Bind(&LocalFileStreamReader::DidOpenForRead, | |
| 56 weak_factory_.GetWeakPtr(), | |
| 57 make_scoped_refptr(buf), buf_len, callback)); | |
| 58 } | |
| 59 | |
| 60 int64 LocalFileStreamReader::GetLength( | |
| 61 const net::Int64CompletionCallback& callback) { | |
| 62 const bool posted = base::FileUtilProxy::GetFileInfo( | |
| 63 task_runner_, file_path_, | |
| 64 base::Bind(&LocalFileStreamReader::DidGetFileInfoForGetLength, | |
| 65 weak_factory_.GetWeakPtr(), callback)); | |
| 66 DCHECK(posted); | |
| 67 return net::ERR_IO_PENDING; | |
| 68 } | |
| 69 | |
| 70 int LocalFileStreamReader::Open(const net::CompletionCallback& callback) { | |
| 71 DCHECK(!has_pending_open_); | |
| 72 DCHECK(!stream_impl_.get()); | |
| 73 has_pending_open_ = true; | |
| 74 | |
| 75 // Call GetLength first to make it perform last-modified-time verification, | |
| 76 // and then call DidVerifyForOpen for do the rest. | |
| 77 return GetLength(base::Bind(&LocalFileStreamReader::DidVerifyForOpen, | |
| 78 weak_factory_.GetWeakPtr(), callback)); | |
| 79 } | |
| 80 | |
| 81 void LocalFileStreamReader::DidVerifyForOpen( | |
| 82 const net::CompletionCallback& callback, | |
| 83 int64 get_length_result) { | |
| 84 if (get_length_result < 0) { | |
| 85 callback.Run(static_cast<int>(get_length_result)); | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 stream_impl_.reset(new net::FileStream(NULL)); | |
| 90 const int result = stream_impl_->Open( | |
| 91 file_path_, kOpenFlagsForRead, | |
| 92 base::Bind(&LocalFileStreamReader::DidOpenFileStream, | |
| 93 weak_factory_.GetWeakPtr(), | |
| 94 callback)); | |
| 95 if (result != net::ERR_IO_PENDING) | |
| 96 callback.Run(result); | |
| 97 } | |
| 98 | |
| 99 void LocalFileStreamReader::DidOpenFileStream( | |
| 100 const net::CompletionCallback& callback, | |
| 101 int result) { | |
| 102 if (result != net::OK) { | |
| 103 callback.Run(result); | |
| 104 return; | |
| 105 } | |
| 106 result = stream_impl_->Seek( | |
| 107 net::FROM_BEGIN, initial_offset_, | |
| 108 base::Bind(&LocalFileStreamReader::DidSeekFileStream, | |
| 109 weak_factory_.GetWeakPtr(), | |
| 110 callback)); | |
| 111 if (result != net::ERR_IO_PENDING) { | |
| 112 callback.Run(result); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void LocalFileStreamReader::DidSeekFileStream( | |
| 117 const net::CompletionCallback& callback, | |
| 118 int64 seek_result) { | |
| 119 if (seek_result < 0) { | |
| 120 callback.Run(static_cast<int>(seek_result)); | |
| 121 return; | |
| 122 } | |
| 123 if (seek_result != initial_offset_) { | |
| 124 callback.Run(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); | |
| 125 return; | |
| 126 } | |
| 127 callback.Run(net::OK); | |
| 128 } | |
| 129 | |
| 130 void LocalFileStreamReader::DidOpenForRead( | |
| 131 net::IOBuffer* buf, | |
| 132 int buf_len, | |
| 133 const net::CompletionCallback& callback, | |
| 134 int open_result) { | |
| 135 DCHECK(has_pending_open_); | |
| 136 has_pending_open_ = false; | |
| 137 if (open_result != net::OK) { | |
| 138 stream_impl_.reset(); | |
| 139 callback.Run(open_result); | |
| 140 return; | |
| 141 } | |
| 142 DCHECK(stream_impl_.get()); | |
| 143 const int read_result = stream_impl_->Read(buf, buf_len, callback); | |
| 144 if (read_result != net::ERR_IO_PENDING) | |
| 145 callback.Run(read_result); | |
| 146 } | |
| 147 | |
| 148 void LocalFileStreamReader::DidGetFileInfoForGetLength( | |
| 149 const net::Int64CompletionCallback& callback, | |
| 150 base::PlatformFileError error, | |
| 151 const base::PlatformFileInfo& file_info) { | |
| 152 if (file_info.is_directory) { | |
| 153 callback.Run(net::ERR_FILE_NOT_FOUND); | |
| 154 return; | |
| 155 } | |
| 156 if (error != base::PLATFORM_FILE_OK) { | |
| 157 callback.Run(net::PlatformFileErrorToNetError(error)); | |
| 158 return; | |
| 159 } | |
| 160 if (!VerifySnapshotTime(expected_modification_time_, file_info)) { | |
| 161 callback.Run(net::ERR_UPLOAD_FILE_CHANGED); | |
| 162 return; | |
| 163 } | |
| 164 callback.Run(file_info.size); | |
| 165 } | |
| 166 | |
| 167 } // namespace webkit_blob | |
| OLD | NEW |