| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/blob/local_file_reader.h" | 5 #include "webkit/blob/local_file_reader.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/file_util_proxy.h" | 8 #include "base/file_util_proxy.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
| 12 #include "net/base/file_stream.h" | 12 #include "net/base/file_stream.h" |
| 13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 | 15 |
| 16 namespace webkit_blob { | 16 namespace webkit_blob { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const int kOpenFlagsForRead = base::PLATFORM_FILE_OPEN | | 20 const int kOpenFlagsForRead = base::PLATFORM_FILE_OPEN | |
| 21 base::PLATFORM_FILE_READ | | 21 base::PLATFORM_FILE_READ | |
| 22 base::PLATFORM_FILE_ASYNC; | 22 base::PLATFORM_FILE_ASYNC; |
| 23 | 23 |
| 24 int PlatformFileErrorToNetError(base::PlatformFileError file_error) { | |
| 25 switch (file_error) { | |
| 26 case base::PLATFORM_FILE_OK: | |
| 27 return net::OK; | |
| 28 case base::PLATFORM_FILE_ERROR_NOT_FOUND: | |
| 29 return net::ERR_FILE_NOT_FOUND; | |
| 30 case base::PLATFORM_FILE_ERROR_ACCESS_DENIED: | |
| 31 return net::ERR_ACCESS_DENIED; | |
| 32 default: | |
| 33 return net::ERR_FAILED; | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 // Verify if the underlying file has not been modified. | 24 // Verify if the underlying file has not been modified. |
| 38 bool VerifySnapshotTime(const base::Time& expected_modification_time, | 25 bool VerifySnapshotTime(const base::Time& expected_modification_time, |
| 39 const base::PlatformFileInfo& file_info) { | 26 const base::PlatformFileInfo& file_info) { |
| 40 return expected_modification_time.is_null() || | 27 return expected_modification_time.is_null() || |
| 41 expected_modification_time.ToTimeT() == | 28 expected_modification_time.ToTimeT() == |
| 42 file_info.last_modified.ToTimeT(); | 29 file_info.last_modified.ToTimeT(); |
| 43 } | 30 } |
| 44 | 31 |
| 45 void DidGetFileInfoForGetLength(const net::Int64CompletionCallback& callback, | 32 void DidGetFileInfoForGetLength(const net::Int64CompletionCallback& callback, |
| 46 const base::Time& expected_modification_time, | 33 const base::Time& expected_modification_time, |
| 47 int64 initial_offset, | |
| 48 base::PlatformFileError error, | 34 base::PlatformFileError error, |
| 49 const base::PlatformFileInfo& file_info) { | 35 const base::PlatformFileInfo& file_info) { |
| 50 if (file_info.is_directory) { | 36 if (file_info.is_directory) { |
| 51 callback.Run(net::ERR_FILE_NOT_FOUND); | 37 callback.Run(net::ERR_FILE_NOT_FOUND); |
| 52 return; | 38 return; |
| 53 } | 39 } |
| 54 if (error != base::PLATFORM_FILE_OK) { | 40 if (error != base::PLATFORM_FILE_OK) { |
| 55 callback.Run(PlatformFileErrorToNetError(error)); | 41 callback.Run(LocalFileReader::PlatformFileErrorToNetError(error)); |
| 56 return; | 42 return; |
| 57 } | 43 } |
| 58 if (!VerifySnapshotTime(expected_modification_time, file_info)) { | 44 if (!VerifySnapshotTime(expected_modification_time, file_info)) { |
| 59 callback.Run(net::ERR_UPLOAD_FILE_CHANGED); | 45 callback.Run(net::ERR_UPLOAD_FILE_CHANGED); |
| 60 return; | 46 return; |
| 61 } | 47 } |
| 62 callback.Run(file_info.size - initial_offset); | 48 callback.Run(file_info.size); |
| 63 } | 49 } |
| 64 | 50 |
| 65 void DidSeekFile(const LocalFileReader::OpenFileStreamCallback& callback, | 51 void DidSeekFile(const LocalFileReader::OpenFileStreamCallback& callback, |
| 66 scoped_ptr<net::FileStream> stream_impl, | 52 scoped_ptr<net::FileStream> stream_impl, |
| 67 int64 initial_offset, | 53 int64 initial_offset, |
| 68 int64 new_offset) { | 54 int64 new_offset) { |
| 69 int result = net::OK; | 55 int result = net::OK; |
| 70 if (new_offset < 0) | 56 if (new_offset < 0) |
| 71 result = static_cast<int>(new_offset); | 57 result = static_cast<int>(new_offset); |
| 72 else if (new_offset != initial_offset) | 58 else if (new_offset != initial_offset) |
| 73 result = net::ERR_REQUEST_RANGE_NOT_SATISFIABLE; | 59 result = net::ERR_REQUEST_RANGE_NOT_SATISFIABLE; |
| 74 callback.Run(result, stream_impl.Pass()); | 60 callback.Run(result, stream_impl.Pass()); |
| 75 } | 61 } |
| 76 | 62 |
| 77 void EmptyCompletionCallback(int) {} | 63 void EmptyCompletionCallback(int) {} |
| 78 | 64 |
| 79 } // namespace | 65 } // namespace |
| 80 | 66 |
| 67 // static |
| 68 int LocalFileReader::PlatformFileErrorToNetError( |
| 69 base::PlatformFileError file_error) { |
| 70 switch (file_error) { |
| 71 case base::PLATFORM_FILE_OK: |
| 72 return net::OK; |
| 73 case base::PLATFORM_FILE_ERROR_NOT_FOUND: |
| 74 return net::ERR_FILE_NOT_FOUND; |
| 75 case base::PLATFORM_FILE_ERROR_ACCESS_DENIED: |
| 76 return net::ERR_ACCESS_DENIED; |
| 77 default: |
| 78 return net::ERR_FAILED; |
| 79 } |
| 80 } |
| 81 |
| 81 // A helper class to open, verify and seek a file stream for a given path. | 82 // A helper class to open, verify and seek a file stream for a given path. |
| 82 class LocalFileReader::OpenFileStreamHelper { | 83 class LocalFileReader::OpenFileStreamHelper { |
| 83 public: | 84 public: |
| 84 OpenFileStreamHelper(base::MessageLoopProxy* file_thread_proxy) | 85 OpenFileStreamHelper(base::MessageLoopProxy* file_thread_proxy) |
| 85 : file_thread_proxy_(file_thread_proxy), | 86 : file_thread_proxy_(file_thread_proxy), |
| 86 file_handle_(base::kInvalidPlatformFileValue), | 87 file_handle_(base::kInvalidPlatformFileValue), |
| 87 result_(net::OK) {} | 88 result_(net::OK) {} |
| 88 ~OpenFileStreamHelper() { | 89 ~OpenFileStreamHelper() { |
| 89 if (file_handle_ != base::kInvalidPlatformFileValue) { | 90 if (file_handle_ != base::kInvalidPlatformFileValue) { |
| 90 file_thread_proxy_->PostTask( | 91 file_thread_proxy_->PostTask( |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 if (stream_impl_.get()) | 167 if (stream_impl_.get()) |
| 167 return stream_impl_->Read(buf, buf_len, callback); | 168 return stream_impl_->Read(buf, buf_len, callback); |
| 168 return Open(base::Bind(&LocalFileReader::DidOpen, weak_factory_.GetWeakPtr(), | 169 return Open(base::Bind(&LocalFileReader::DidOpen, weak_factory_.GetWeakPtr(), |
| 169 make_scoped_refptr(buf), buf_len, callback)); | 170 make_scoped_refptr(buf), buf_len, callback)); |
| 170 } | 171 } |
| 171 | 172 |
| 172 int LocalFileReader::GetLength(const net::Int64CompletionCallback& callback) { | 173 int LocalFileReader::GetLength(const net::Int64CompletionCallback& callback) { |
| 173 const bool posted = base::FileUtilProxy::GetFileInfo( | 174 const bool posted = base::FileUtilProxy::GetFileInfo( |
| 174 file_thread_proxy_, file_path_, | 175 file_thread_proxy_, file_path_, |
| 175 base::Bind(&DidGetFileInfoForGetLength, callback, | 176 base::Bind(&DidGetFileInfoForGetLength, callback, |
| 176 expected_modification_time_, initial_offset_)); | 177 expected_modification_time_)); |
| 177 DCHECK(posted); | 178 DCHECK(posted); |
| 178 return net::ERR_IO_PENDING; | 179 return net::ERR_IO_PENDING; |
| 179 } | 180 } |
| 180 | 181 |
| 181 int LocalFileReader::Open(const OpenFileStreamCallback& callback) { | 182 int LocalFileReader::Open(const OpenFileStreamCallback& callback) { |
| 182 DCHECK(!has_pending_open_); | 183 DCHECK(!has_pending_open_); |
| 183 DCHECK(!stream_impl_.get()); | 184 DCHECK(!stream_impl_.get()); |
| 184 has_pending_open_ = true; | 185 has_pending_open_ = true; |
| 185 OpenFileStreamHelper* helper = new OpenFileStreamHelper(file_thread_proxy_); | 186 OpenFileStreamHelper* helper = new OpenFileStreamHelper(file_thread_proxy_); |
| 186 const bool posted = file_thread_proxy_->PostTaskAndReply( | 187 const bool posted = file_thread_proxy_->PostTaskAndReply( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 207 return; | 208 return; |
| 208 } | 209 } |
| 209 DCHECK(stream_impl.get()); | 210 DCHECK(stream_impl.get()); |
| 210 stream_impl_ = stream_impl.Pass(); | 211 stream_impl_ = stream_impl.Pass(); |
| 211 const int read_error = stream_impl_->Read(buf, buf_len, callback); | 212 const int read_error = stream_impl_->Read(buf, buf_len, callback); |
| 212 if (read_error != net::ERR_IO_PENDING) | 213 if (read_error != net::ERR_IO_PENDING) |
| 213 callback.Run(read_error); | 214 callback.Run(read_error); |
| 214 } | 215 } |
| 215 | 216 |
| 216 } // namespace webkit_blob | 217 } // namespace webkit_blob |
| OLD | NEW |