| 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_reader.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/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 // static | |
| 36 int LocalFileReader::PlatformFileErrorToNetError( | |
| 37 base::PlatformFileError file_error) { | |
| 38 switch (file_error) { | |
| 39 case base::PLATFORM_FILE_OK: | |
| 40 return net::OK; | |
| 41 case base::PLATFORM_FILE_ERROR_NOT_FOUND: | |
| 42 return net::ERR_FILE_NOT_FOUND; | |
| 43 case base::PLATFORM_FILE_ERROR_ACCESS_DENIED: | |
| 44 return net::ERR_ACCESS_DENIED; | |
| 45 default: | |
| 46 return net::ERR_FAILED; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 LocalFileReader::LocalFileReader( | |
| 51 base::TaskRunner* task_runner, | |
| 52 const FilePath& file_path, | |
| 53 int64 initial_offset, | |
| 54 const base::Time& expected_modification_time) | |
| 55 : task_runner_(task_runner), | |
| 56 file_path_(file_path), | |
| 57 initial_offset_(initial_offset), | |
| 58 expected_modification_time_(expected_modification_time), | |
| 59 has_pending_open_(false), | |
| 60 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | |
| 61 | |
| 62 LocalFileReader::~LocalFileReader() { | |
| 63 } | |
| 64 | |
| 65 int LocalFileReader::Read(net::IOBuffer* buf, int buf_len, | |
| 66 const net::CompletionCallback& callback) { | |
| 67 DCHECK(!has_pending_open_); | |
| 68 if (stream_impl_.get()) | |
| 69 return stream_impl_->Read(buf, buf_len, callback); | |
| 70 return Open(base::Bind(&LocalFileReader::DidOpenForRead, | |
| 71 weak_factory_.GetWeakPtr(), | |
| 72 make_scoped_refptr(buf), buf_len, callback)); | |
| 73 } | |
| 74 | |
| 75 int LocalFileReader::GetLength(const net::Int64CompletionCallback& callback) { | |
| 76 const bool posted = base::FileUtilProxy::GetFileInfo( | |
| 77 task_runner_, file_path_, | |
| 78 base::Bind(&LocalFileReader::DidGetFileInfoForGetLength, | |
| 79 weak_factory_.GetWeakPtr(), callback)); | |
| 80 DCHECK(posted); | |
| 81 return net::ERR_IO_PENDING; | |
| 82 } | |
| 83 | |
| 84 int LocalFileReader::Open(const net::CompletionCallback& callback) { | |
| 85 DCHECK(!has_pending_open_); | |
| 86 DCHECK(!stream_impl_.get()); | |
| 87 has_pending_open_ = true; | |
| 88 | |
| 89 // Call GetLength first to make it perform last-modified-time verification, | |
| 90 // and then call DidVerifyForOpen for do the rest. | |
| 91 return GetLength(base::Bind(&LocalFileReader::DidVerifyForOpen, | |
| 92 weak_factory_.GetWeakPtr(), callback)); | |
| 93 } | |
| 94 | |
| 95 void LocalFileReader::DidVerifyForOpen( | |
| 96 const net::CompletionCallback& callback, | |
| 97 int64 get_length_result) { | |
| 98 if (get_length_result < 0) { | |
| 99 callback.Run(static_cast<int>(get_length_result)); | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 stream_impl_.reset(new net::FileStream(NULL)); | |
| 104 const int result = stream_impl_->Open( | |
| 105 file_path_, kOpenFlagsForRead, | |
| 106 base::Bind(&LocalFileReader::DidOpenFileStream, | |
| 107 weak_factory_.GetWeakPtr(), | |
| 108 callback)); | |
| 109 if (result != net::ERR_IO_PENDING) | |
| 110 callback.Run(result); | |
| 111 } | |
| 112 | |
| 113 void LocalFileReader::DidOpenFileStream( | |
| 114 const net::CompletionCallback& callback, | |
| 115 int result) { | |
| 116 if (result != net::OK) { | |
| 117 callback.Run(result); | |
| 118 return; | |
| 119 } | |
| 120 result = stream_impl_->Seek(net::FROM_BEGIN, initial_offset_, | |
| 121 base::Bind(&LocalFileReader::DidSeekFileStream, | |
| 122 weak_factory_.GetWeakPtr(), | |
| 123 callback)); | |
| 124 if (result != net::ERR_IO_PENDING) { | |
| 125 callback.Run(result); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void LocalFileReader::DidSeekFileStream( | |
| 130 const net::CompletionCallback& callback, | |
| 131 int64 seek_result) { | |
| 132 if (seek_result < 0) { | |
| 133 callback.Run(static_cast<int>(seek_result)); | |
| 134 return; | |
| 135 } | |
| 136 if (seek_result != initial_offset_) { | |
| 137 callback.Run(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); | |
| 138 return; | |
| 139 } | |
| 140 callback.Run(net::OK); | |
| 141 } | |
| 142 | |
| 143 void LocalFileReader::DidOpenForRead(net::IOBuffer* buf, | |
| 144 int buf_len, | |
| 145 const net::CompletionCallback& callback, | |
| 146 int open_result) { | |
| 147 DCHECK(has_pending_open_); | |
| 148 has_pending_open_ = false; | |
| 149 if (open_result != net::OK) { | |
| 150 stream_impl_.reset(); | |
| 151 callback.Run(open_result); | |
| 152 return; | |
| 153 } | |
| 154 DCHECK(stream_impl_.get()); | |
| 155 const int read_result = stream_impl_->Read(buf, buf_len, callback); | |
| 156 if (read_result != net::ERR_IO_PENDING) | |
| 157 callback.Run(read_result); | |
| 158 } | |
| 159 | |
| 160 void LocalFileReader::DidGetFileInfoForGetLength( | |
| 161 const net::Int64CompletionCallback& callback, | |
| 162 base::PlatformFileError error, | |
| 163 const base::PlatformFileInfo& file_info) { | |
| 164 if (file_info.is_directory) { | |
| 165 callback.Run(net::ERR_FILE_NOT_FOUND); | |
| 166 return; | |
| 167 } | |
| 168 if (error != base::PLATFORM_FILE_OK) { | |
| 169 callback.Run(LocalFileReader::PlatformFileErrorToNetError(error)); | |
| 170 return; | |
| 171 } | |
| 172 if (!VerifySnapshotTime(expected_modification_time_, file_info)) { | |
| 173 callback.Run(net::ERR_UPLOAD_FILE_CHANGED); | |
| 174 return; | |
| 175 } | |
| 176 callback.Run(file_info.size); | |
| 177 } | |
| 178 | |
| 179 } // namespace webkit_blob | |
| OLD | NEW |