| 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 "base/files/file_util_proxy.h" | 5 #include "base/files/file_proxy.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "base/task_runner.h" | 13 #include "base/task_runner.h" |
| 14 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 | 17 |
| 18 class FileHelper { |
| 19 public: |
| 20 FileHelper(FileProxy* proxy, File file) |
| 21 : file_(file.Pass()), |
| 22 proxy_(AsWeakPtr(proxy)), |
| 23 error_(File::FILE_ERROR_FAILED) { |
| 24 } |
| 25 |
| 26 void PassFile() { |
| 27 if (proxy_) |
| 28 proxy_->SetFile(file_.Pass()); |
| 29 } |
| 30 |
| 31 protected: |
| 32 File file_; |
| 33 WeakPtr<FileProxy> proxy_; |
| 34 File::Error error_; |
| 35 |
| 36 private: |
| 37 DISALLOW_COPY_AND_ASSIGN(FileHelper); |
| 38 }; |
| 39 |
| 18 namespace { | 40 namespace { |
| 19 | 41 |
| 20 void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback, | 42 class GenericFileHelper : public FileHelper { |
| 21 bool value) { | |
| 22 DCHECK(!callback.is_null()); | |
| 23 callback.Run(value ? File::FILE_OK : File::FILE_ERROR_FAILED); | |
| 24 } | |
| 25 | |
| 26 // Helper classes or routines for individual methods. | |
| 27 class CreateOrOpenHelper { | |
| 28 public: | 43 public: |
| 29 CreateOrOpenHelper(TaskRunner* task_runner, | 44 GenericFileHelper(FileProxy* proxy, File file) |
| 30 const FileUtilProxy::CloseTask& close_task) | 45 : FileHelper(proxy, file.Pass()) { |
| 31 : task_runner_(task_runner), | |
| 32 close_task_(close_task), | |
| 33 file_handle_(kInvalidPlatformFileValue), | |
| 34 created_(false), | |
| 35 error_(File::FILE_OK) {} | |
| 36 | |
| 37 ~CreateOrOpenHelper() { | |
| 38 if (file_handle_ != kInvalidPlatformFileValue) { | |
| 39 task_runner_->PostTask( | |
| 40 FROM_HERE, | |
| 41 base::Bind(base::IgnoreResult(close_task_), file_handle_)); | |
| 42 } | |
| 43 } | 46 } |
| 44 | 47 |
| 45 void RunWork(const FileUtilProxy::CreateOrOpenTask& task) { | 48 void Close() { |
| 46 error_ = task.Run(&file_handle_, &created_); | 49 file_.Close(); |
| 50 error_ = File::FILE_OK; |
| 47 } | 51 } |
| 48 | 52 |
| 49 void Reply(const FileUtilProxy::CreateOrOpenCallback& callback) { | 53 void SetTimes(Time last_access_time, Time last_modified_time) { |
| 50 DCHECK(!callback.is_null()); | 54 bool rv = file_.SetTimes(last_access_time, last_modified_time); |
| 51 callback.Run(error_, PassPlatformFile(&file_handle_), created_); | 55 error_ = rv ? File::FILE_OK : File::FILE_ERROR_FAILED; |
| 56 } |
| 57 |
| 58 void SetLength(int64 length) { |
| 59 if (file_.SetLength(length)) |
| 60 error_ = File::FILE_OK; |
| 61 } |
| 62 |
| 63 void Flush() { |
| 64 if (file_.Flush()) |
| 65 error_ = File::FILE_OK; |
| 66 } |
| 67 |
| 68 void Reply(const FileProxy::StatusCallback& callback) { |
| 69 PassFile(); |
| 70 if (!callback.is_null()) |
| 71 callback.Run(error_); |
| 52 } | 72 } |
| 53 | 73 |
| 54 private: | 74 private: |
| 55 scoped_refptr<TaskRunner> task_runner_; | 75 DISALLOW_COPY_AND_ASSIGN(GenericFileHelper); |
| 56 FileUtilProxy::CloseTask close_task_; | 76 }; |
| 57 PlatformFile file_handle_; | 77 |
| 58 bool created_; | 78 class CreateOrOpenHelper : public FileHelper { |
| 59 File::Error error_; | 79 public: |
| 80 CreateOrOpenHelper(FileProxy* proxy, File file) |
| 81 : FileHelper(proxy, file.Pass()) { |
| 82 } |
| 83 |
| 84 void RunWork(const FilePath& file_path, int file_flags) { |
| 85 file_.Initialize(file_path, file_flags); |
| 86 error_ = file_.IsValid() ? File::FILE_OK : file_.error_details(); |
| 87 } |
| 88 |
| 89 void Reply(const FileProxy::StatusCallback& callback) { |
| 90 DCHECK(!callback.is_null()); |
| 91 PassFile(); |
| 92 callback.Run(error_); |
| 93 } |
| 94 |
| 95 private: |
| 60 DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper); | 96 DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper); |
| 61 }; | 97 }; |
| 62 | 98 |
| 63 class CreateTemporaryHelper { | 99 class CreateTemporaryHelper : public FileHelper { |
| 64 public: | 100 public: |
| 65 explicit CreateTemporaryHelper(TaskRunner* task_runner) | 101 CreateTemporaryHelper(FileProxy* proxy, File file) |
| 66 : task_runner_(task_runner), | 102 : FileHelper(proxy, file.Pass()) { |
| 67 file_handle_(kInvalidPlatformFileValue), | |
| 68 error_(File::FILE_OK) {} | |
| 69 | |
| 70 ~CreateTemporaryHelper() { | |
| 71 if (file_handle_ != kInvalidPlatformFileValue) { | |
| 72 FileUtilProxy::Close( | |
| 73 task_runner_.get(), file_handle_, FileUtilProxy::StatusCallback()); | |
| 74 } | |
| 75 } | 103 } |
| 76 | 104 |
| 77 void RunWork(int additional_file_flags) { | 105 void RunWork(uint32 additional_file_flags) { |
| 78 // TODO(darin): file_util should have a variant of CreateTemporaryFile | 106 // TODO(darin): file_util should have a variant of CreateTemporaryFile |
| 79 // that returns a FilePath and a PlatformFile. | 107 // that returns a FilePath and a File. |
| 80 if (!base::CreateTemporaryFile(&file_path_)) { | 108 if (!CreateTemporaryFile(&file_path_)) { |
| 81 // TODO(davidben): base::CreateTemporaryFile should preserve the error | 109 // TODO(davidben): base::CreateTemporaryFile should preserve the error |
| 82 // code. | 110 // code. |
| 83 error_ = File::FILE_ERROR_FAILED; | 111 error_ = File::FILE_ERROR_FAILED; |
| 84 return; | 112 return; |
| 85 } | 113 } |
| 86 | 114 |
| 87 int file_flags = | 115 uint32 file_flags = File::FLAG_WRITE | |
| 88 PLATFORM_FILE_WRITE | | 116 File::FLAG_TEMPORARY | |
| 89 PLATFORM_FILE_TEMPORARY | | 117 File::FLAG_CREATE_ALWAYS | |
| 90 PLATFORM_FILE_CREATE_ALWAYS | | 118 additional_file_flags; |
| 91 additional_file_flags; | |
| 92 | 119 |
| 93 error_ = File::FILE_OK; | 120 file_.Initialize(file_path_, file_flags); |
| 94 // TODO(rvargas): Convert this code to use File. | 121 if (file_.IsValid()) { |
| 95 file_handle_ = | 122 error_ = File::FILE_OK; |
| 96 CreatePlatformFile(file_path_, file_flags, NULL, | 123 } else { |
| 97 reinterpret_cast<PlatformFileError*>(&error_)); | 124 error_ = file_.error_details(); |
| 98 if (error_ != File::FILE_OK) { | 125 DeleteFile(file_path_, false); |
| 99 base::DeleteFile(file_path_, false); | |
| 100 file_path_.clear(); | 126 file_path_.clear(); |
| 101 } | 127 } |
| 102 } | 128 } |
| 103 | 129 |
| 104 void Reply(const FileUtilProxy::CreateTemporaryCallback& callback) { | 130 void Reply(const FileProxy::CreateTemporaryCallback& callback) { |
| 105 DCHECK(!callback.is_null()); | 131 DCHECK(!callback.is_null()); |
| 106 callback.Run(error_, PassPlatformFile(&file_handle_), file_path_); | 132 PassFile(); |
| 133 callback.Run(error_, file_path_); |
| 107 } | 134 } |
| 108 | 135 |
| 109 private: | 136 private: |
| 110 scoped_refptr<TaskRunner> task_runner_; | |
| 111 PlatformFile file_handle_; | |
| 112 FilePath file_path_; | 137 FilePath file_path_; |
| 113 File::Error error_; | |
| 114 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper); | 138 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper); |
| 115 }; | 139 }; |
| 116 | 140 |
| 117 class GetFileInfoHelper { | 141 class GetInfoHelper : public FileHelper { |
| 118 public: | 142 public: |
| 119 GetFileInfoHelper() | 143 GetInfoHelper(FileProxy* proxy, File file) |
| 120 : error_(File::FILE_OK) {} | 144 : FileHelper(proxy, file.Pass()) { |
| 121 | |
| 122 void RunWorkForFilePath(const FilePath& file_path) { | |
| 123 if (!PathExists(file_path)) { | |
| 124 error_ = File::FILE_ERROR_NOT_FOUND; | |
| 125 return; | |
| 126 } | |
| 127 // TODO(rvargas): switch this file to base::File. | |
| 128 if (!GetFileInfo(file_path, reinterpret_cast<File::Info*>(&file_info_))) | |
| 129 error_ = File::FILE_ERROR_FAILED; | |
| 130 } | 145 } |
| 131 | 146 |
| 132 void RunWorkForPlatformFile(PlatformFile file) { | 147 void RunWork() { |
| 133 if (!GetPlatformFileInfo( | 148 if (file_.GetInfo(&file_info_)) |
| 134 file, reinterpret_cast<PlatformFileInfo*>(&file_info_))) { | 149 error_ = File::FILE_OK; |
| 135 error_ = File::FILE_ERROR_FAILED; | |
| 136 } | |
| 137 } | 150 } |
| 138 | 151 |
| 139 void Reply(const FileUtilProxy::GetFileInfoCallback& callback) { | 152 void Reply(const FileProxy::GetFileInfoCallback& callback) { |
| 140 if (!callback.is_null()) { | 153 PassFile(); |
| 141 callback.Run(error_, file_info_); | 154 DCHECK(!callback.is_null()); |
| 142 } | 155 callback.Run(error_, file_info_); |
| 143 } | 156 } |
| 144 | 157 |
| 145 private: | 158 private: |
| 146 File::Error error_; | |
| 147 File::Info file_info_; | 159 File::Info file_info_; |
| 148 DISALLOW_COPY_AND_ASSIGN(GetFileInfoHelper); | 160 DISALLOW_COPY_AND_ASSIGN(GetInfoHelper); |
| 149 }; | 161 }; |
| 150 | 162 |
| 151 class ReadHelper { | 163 class ReadHelper : public FileHelper { |
| 152 public: | 164 public: |
| 153 explicit ReadHelper(int bytes_to_read) | 165 ReadHelper(FileProxy* proxy, File file, int bytes_to_read) |
| 154 : buffer_(new char[bytes_to_read]), | 166 : FileHelper(proxy, file.Pass()), |
| 167 buffer_(new char[bytes_to_read]), |
| 155 bytes_to_read_(bytes_to_read), | 168 bytes_to_read_(bytes_to_read), |
| 156 bytes_read_(0) {} | 169 bytes_read_(0) { |
| 157 | |
| 158 void RunWork(PlatformFile file, int64 offset) { | |
| 159 bytes_read_ = ReadPlatformFile(file, offset, buffer_.get(), bytes_to_read_); | |
| 160 } | 170 } |
| 161 | 171 |
| 162 void Reply(const FileUtilProxy::ReadCallback& callback) { | 172 void RunWork(int64 offset) { |
| 163 if (!callback.is_null()) { | 173 bytes_read_ = file_.Read(offset, buffer_.get(), bytes_to_read_); |
| 164 File::Error error = | 174 error_ = (bytes_read_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK; |
| 165 (bytes_read_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK; | 175 } |
| 166 callback.Run(error, buffer_.get(), bytes_read_); | 176 |
| 167 } | 177 void Reply(const FileProxy::ReadCallback& callback) { |
| 178 PassFile(); |
| 179 DCHECK(!callback.is_null()); |
| 180 callback.Run(error_, buffer_.get(), bytes_read_); |
| 168 } | 181 } |
| 169 | 182 |
| 170 private: | 183 private: |
| 171 scoped_ptr<char[]> buffer_; | 184 scoped_ptr<char[]> buffer_; |
| 172 int bytes_to_read_; | 185 int bytes_to_read_; |
| 173 int bytes_read_; | 186 int bytes_read_; |
| 174 DISALLOW_COPY_AND_ASSIGN(ReadHelper); | 187 DISALLOW_COPY_AND_ASSIGN(ReadHelper); |
| 175 }; | 188 }; |
| 176 | 189 |
| 177 class WriteHelper { | 190 class WriteHelper : public FileHelper { |
| 178 public: | 191 public: |
| 179 WriteHelper(const char* buffer, int bytes_to_write) | 192 WriteHelper(FileProxy* proxy, |
| 180 : buffer_(new char[bytes_to_write]), | 193 File file, |
| 194 const char* buffer, int bytes_to_write) |
| 195 : FileHelper(proxy, file.Pass()), |
| 196 buffer_(new char[bytes_to_write]), |
| 181 bytes_to_write_(bytes_to_write), | 197 bytes_to_write_(bytes_to_write), |
| 182 bytes_written_(0) { | 198 bytes_written_(0) { |
| 183 memcpy(buffer_.get(), buffer, bytes_to_write); | 199 memcpy(buffer_.get(), buffer, bytes_to_write); |
| 184 } | 200 } |
| 185 | 201 |
| 186 void RunWork(PlatformFile file, int64 offset) { | 202 void RunWork(int64 offset) { |
| 187 bytes_written_ = WritePlatformFile(file, offset, buffer_.get(), | 203 bytes_written_ = file_.Write(offset, buffer_.get(), bytes_to_write_); |
| 188 bytes_to_write_); | 204 error_ = (bytes_written_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK; |
| 189 } | 205 } |
| 190 | 206 |
| 191 void Reply(const FileUtilProxy::WriteCallback& callback) { | 207 void Reply(const FileProxy::WriteCallback& callback) { |
| 192 if (!callback.is_null()) { | 208 PassFile(); |
| 193 File::Error error = | 209 if (!callback.is_null()) |
| 194 (bytes_written_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK; | 210 callback.Run(error_, bytes_written_); |
| 195 callback.Run(error, bytes_written_); | |
| 196 } | |
| 197 } | 211 } |
| 198 | 212 |
| 199 private: | 213 private: |
| 200 scoped_ptr<char[]> buffer_; | 214 scoped_ptr<char[]> buffer_; |
| 201 int bytes_to_write_; | 215 int bytes_to_write_; |
| 202 int bytes_written_; | 216 int bytes_written_; |
| 203 DISALLOW_COPY_AND_ASSIGN(WriteHelper); | 217 DISALLOW_COPY_AND_ASSIGN(WriteHelper); |
| 204 }; | 218 }; |
| 205 | 219 |
| 206 File::Error CreateOrOpenAdapter( | 220 } // namespace |
| 207 const FilePath& file_path, int file_flags, | 221 |
| 208 PlatformFile* file_handle, bool* created) { | 222 FileProxy::FileProxy() : task_runner_(NULL) { |
| 209 DCHECK(file_handle); | |
| 210 DCHECK(created); | |
| 211 if (!DirectoryExists(file_path.DirName())) { | |
| 212 // If its parent does not exist, should return NOT_FOUND error. | |
| 213 return File::FILE_ERROR_NOT_FOUND; | |
| 214 } | |
| 215 File::Error error = File::FILE_OK; | |
| 216 *file_handle = | |
| 217 CreatePlatformFile(file_path, file_flags, created, | |
| 218 reinterpret_cast<PlatformFileError*>(&error)); | |
| 219 return error; | |
| 220 } | 223 } |
| 221 | 224 |
| 222 File::Error CloseAdapter(PlatformFile file_handle) { | 225 FileProxy::FileProxy(TaskRunner* task_runner) : task_runner_(task_runner) { |
| 223 if (!ClosePlatformFile(file_handle)) { | |
| 224 return File::FILE_ERROR_FAILED; | |
| 225 } | |
| 226 return File::FILE_OK; | |
| 227 } | 226 } |
| 228 | 227 |
| 229 File::Error DeleteAdapter(const FilePath& file_path, bool recursive) { | 228 FileProxy::~FileProxy() { |
| 230 if (!PathExists(file_path)) { | |
| 231 return File::FILE_ERROR_NOT_FOUND; | |
| 232 } | |
| 233 if (!base::DeleteFile(file_path, recursive)) { | |
| 234 if (!recursive && !base::IsDirectoryEmpty(file_path)) { | |
| 235 return File::FILE_ERROR_NOT_EMPTY; | |
| 236 } | |
| 237 return File::FILE_ERROR_FAILED; | |
| 238 } | |
| 239 return File::FILE_OK; | |
| 240 } | 229 } |
| 241 | 230 |
| 242 } // namespace | 231 bool FileProxy::CreateOrOpen(const FilePath& file_path, |
| 243 | 232 uint32 file_flags, |
| 244 // static | 233 const StatusCallback& callback) { |
| 245 bool FileUtilProxy::CreateOrOpen( | 234 DCHECK(!file_.IsValid()); |
| 246 TaskRunner* task_runner, | 235 CreateOrOpenHelper* helper = new CreateOrOpenHelper(this, File()); |
| 247 const FilePath& file_path, int file_flags, | 236 return task_runner_->PostTaskAndReply( |
| 248 const CreateOrOpenCallback& callback) { | 237 FROM_HERE, |
| 249 return RelayCreateOrOpen( | 238 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), file_path, |
| 250 task_runner, | 239 file_flags), |
| 251 base::Bind(&CreateOrOpenAdapter, file_path, file_flags), | 240 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); |
| 252 base::Bind(&CloseAdapter), | |
| 253 callback); | |
| 254 } | 241 } |
| 255 | 242 |
| 256 // static | 243 bool FileProxy::CreateTemporary(uint32 additional_file_flags, |
| 257 bool FileUtilProxy::CreateTemporary( | 244 const CreateTemporaryCallback& callback) { |
| 258 TaskRunner* task_runner, | 245 DCHECK(!file_.IsValid()); |
| 259 int additional_file_flags, | 246 CreateTemporaryHelper* helper = new CreateTemporaryHelper(this, File()); |
| 260 const CreateTemporaryCallback& callback) { | 247 return task_runner_->PostTaskAndReply( |
| 261 CreateTemporaryHelper* helper = new CreateTemporaryHelper(task_runner); | |
| 262 return task_runner->PostTaskAndReply( | |
| 263 FROM_HERE, | 248 FROM_HERE, |
| 264 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), | 249 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), |
| 265 additional_file_flags), | 250 additional_file_flags), |
| 266 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); | 251 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); |
| 267 } | 252 } |
| 268 | 253 |
| 269 // static | 254 bool FileProxy::IsValid() const { |
| 270 bool FileUtilProxy::Close( | 255 return file_.IsValid(); |
| 271 TaskRunner* task_runner, | |
| 272 base::PlatformFile file_handle, | |
| 273 const StatusCallback& callback) { | |
| 274 return RelayClose( | |
| 275 task_runner, | |
| 276 base::Bind(&CloseAdapter), | |
| 277 file_handle, callback); | |
| 278 } | 256 } |
| 279 | 257 |
| 280 // Retrieves the information about a file. It is invalid to pass NULL for the | 258 File FileProxy::TakeFile() { |
| 281 // callback. | 259 return file_.Pass(); |
| 282 bool FileUtilProxy::GetFileInfo( | |
| 283 TaskRunner* task_runner, | |
| 284 const FilePath& file_path, | |
| 285 const GetFileInfoCallback& callback) { | |
| 286 GetFileInfoHelper* helper = new GetFileInfoHelper; | |
| 287 return task_runner->PostTaskAndReply( | |
| 288 FROM_HERE, | |
| 289 Bind(&GetFileInfoHelper::RunWorkForFilePath, | |
| 290 Unretained(helper), file_path), | |
| 291 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); | |
| 292 } | 260 } |
| 293 | 261 |
| 294 // static | 262 bool FileProxy::Close(const StatusCallback& callback) { |
| 295 bool FileUtilProxy::GetFileInfoFromPlatformFile( | 263 DCHECK(file_.IsValid()); |
| 296 TaskRunner* task_runner, | 264 GenericFileHelper* helper = new GenericFileHelper(this, file_.Pass()); |
| 297 PlatformFile file, | 265 return task_runner_->PostTaskAndReply( |
| 298 const GetFileInfoCallback& callback) { | |
| 299 GetFileInfoHelper* helper = new GetFileInfoHelper; | |
| 300 return task_runner->PostTaskAndReply( | |
| 301 FROM_HERE, | 266 FROM_HERE, |
| 302 Bind(&GetFileInfoHelper::RunWorkForPlatformFile, | 267 Bind(&GenericFileHelper::Close, Unretained(helper)), |
| 303 Unretained(helper), file), | 268 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); |
| 304 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); | |
| 305 } | 269 } |
| 306 | 270 |
| 307 // static | 271 bool FileProxy::GetInfo(const GetFileInfoCallback& callback) { |
| 308 bool FileUtilProxy::DeleteFile(TaskRunner* task_runner, | 272 DCHECK(file_.IsValid()); |
| 309 const FilePath& file_path, | 273 GetInfoHelper* helper = new GetInfoHelper(this, file_.Pass()); |
| 310 bool recursive, | 274 return task_runner_->PostTaskAndReply( |
| 311 const StatusCallback& callback) { | 275 FROM_HERE, |
| 312 return base::PostTaskAndReplyWithResult( | 276 Bind(&GetInfoHelper::RunWork, Unretained(helper)), |
| 313 task_runner, FROM_HERE, | 277 Bind(&GetInfoHelper::Reply, Owned(helper), callback)); |
| 314 Bind(&DeleteAdapter, file_path, recursive), | |
| 315 callback); | |
| 316 } | 278 } |
| 317 | 279 |
| 318 // static | 280 bool FileProxy::Read(int64 offset, |
| 319 bool FileUtilProxy::Read( | 281 int bytes_to_read, |
| 320 TaskRunner* task_runner, | 282 const ReadCallback& callback) { |
| 321 PlatformFile file, | 283 DCHECK(file_.IsValid()); |
| 322 int64 offset, | 284 if (bytes_to_read < 0) |
| 323 int bytes_to_read, | |
| 324 const ReadCallback& callback) { | |
| 325 if (bytes_to_read < 0) { | |
| 326 return false; | 285 return false; |
| 327 } | 286 |
| 328 ReadHelper* helper = new ReadHelper(bytes_to_read); | 287 ReadHelper* helper = new ReadHelper(this, file_.Pass(), bytes_to_read); |
| 329 return task_runner->PostTaskAndReply( | 288 return task_runner_->PostTaskAndReply( |
| 330 FROM_HERE, | 289 FROM_HERE, |
| 331 Bind(&ReadHelper::RunWork, Unretained(helper), file, offset), | 290 Bind(&ReadHelper::RunWork, Unretained(helper), offset), |
| 332 Bind(&ReadHelper::Reply, Owned(helper), callback)); | 291 Bind(&ReadHelper::Reply, Owned(helper), callback)); |
| 333 } | 292 } |
| 334 | 293 |
| 335 // static | 294 bool FileProxy::Write(int64 offset, |
| 336 bool FileUtilProxy::Write( | 295 const char* buffer, |
| 337 TaskRunner* task_runner, | 296 int bytes_to_write, |
| 338 PlatformFile file, | 297 const WriteCallback& callback) { |
| 339 int64 offset, | 298 DCHECK(file_.IsValid()); |
| 340 const char* buffer, | 299 if (bytes_to_write <= 0 || buffer == NULL) |
| 341 int bytes_to_write, | |
| 342 const WriteCallback& callback) { | |
| 343 if (bytes_to_write <= 0 || buffer == NULL) { | |
| 344 return false; | 300 return false; |
| 345 } | 301 |
| 346 WriteHelper* helper = new WriteHelper(buffer, bytes_to_write); | 302 WriteHelper* helper = |
| 347 return task_runner->PostTaskAndReply( | 303 new WriteHelper(this, file_.Pass(), buffer, bytes_to_write); |
| 304 return task_runner_->PostTaskAndReply( |
| 348 FROM_HERE, | 305 FROM_HERE, |
| 349 Bind(&WriteHelper::RunWork, Unretained(helper), file, offset), | 306 Bind(&WriteHelper::RunWork, Unretained(helper), offset), |
| 350 Bind(&WriteHelper::Reply, Owned(helper), callback)); | 307 Bind(&WriteHelper::Reply, Owned(helper), callback)); |
| 351 } | 308 } |
| 352 | 309 |
| 353 // static | 310 bool FileProxy::SetTimes(Time last_access_time, |
| 354 bool FileUtilProxy::Touch( | 311 Time last_modified_time, |
| 355 TaskRunner* task_runner, | 312 const StatusCallback& callback) { |
| 356 PlatformFile file, | 313 DCHECK(file_.IsValid()); |
| 357 const Time& last_access_time, | 314 GenericFileHelper* helper = new GenericFileHelper(this, file_.Pass()); |
| 358 const Time& last_modified_time, | 315 return task_runner_->PostTaskAndReply( |
| 359 const StatusCallback& callback) { | |
| 360 return base::PostTaskAndReplyWithResult( | |
| 361 task_runner, | |
| 362 FROM_HERE, | 316 FROM_HERE, |
| 363 Bind(&TouchPlatformFile, file, | 317 Bind(&GenericFileHelper::SetTimes, Unretained(helper), last_access_time, |
| 364 last_access_time, last_modified_time), | 318 last_modified_time), |
| 365 Bind(&CallWithTranslatedParameter, callback)); | 319 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); |
| 366 } | 320 } |
| 367 | 321 |
| 368 // static | 322 bool FileProxy::SetLength(int64 length, const StatusCallback& callback) { |
| 369 bool FileUtilProxy::Touch( | 323 DCHECK(file_.IsValid()); |
| 370 TaskRunner* task_runner, | 324 GenericFileHelper* helper = new GenericFileHelper(this, file_.Pass()); |
| 371 const FilePath& file_path, | 325 return task_runner_->PostTaskAndReply( |
| 372 const Time& last_access_time, | |
| 373 const Time& last_modified_time, | |
| 374 const StatusCallback& callback) { | |
| 375 return base::PostTaskAndReplyWithResult( | |
| 376 task_runner, | |
| 377 FROM_HERE, | 326 FROM_HERE, |
| 378 Bind(&TouchFile, file_path, last_access_time, last_modified_time), | 327 Bind(&GenericFileHelper::SetLength, Unretained(helper), length), |
| 379 Bind(&CallWithTranslatedParameter, callback)); | 328 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); |
| 380 } | 329 } |
| 381 | 330 |
| 382 // static | 331 bool FileProxy::Flush(const StatusCallback& callback) { |
| 383 bool FileUtilProxy::Truncate( | 332 DCHECK(file_.IsValid()); |
| 384 TaskRunner* task_runner, | 333 GenericFileHelper* helper = new GenericFileHelper(this, file_.Pass()); |
| 385 PlatformFile file, | 334 return task_runner_->PostTaskAndReply( |
| 386 int64 length, | |
| 387 const StatusCallback& callback) { | |
| 388 return base::PostTaskAndReplyWithResult( | |
| 389 task_runner, | |
| 390 FROM_HERE, | 335 FROM_HERE, |
| 391 Bind(&TruncatePlatformFile, file, length), | 336 Bind(&GenericFileHelper::Flush, Unretained(helper)), |
| 392 Bind(&CallWithTranslatedParameter, callback)); | 337 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); |
| 393 } | 338 } |
| 394 | 339 |
| 395 // static | 340 void FileProxy::SetFile(File file) { |
| 396 bool FileUtilProxy::Flush( | 341 DCHECK(!file_.IsValid()); |
| 397 TaskRunner* task_runner, | 342 file_ = file.Pass(); |
| 398 PlatformFile file, | |
| 399 const StatusCallback& callback) { | |
| 400 return base::PostTaskAndReplyWithResult( | |
| 401 task_runner, | |
| 402 FROM_HERE, | |
| 403 Bind(&FlushPlatformFile, file), | |
| 404 Bind(&CallWithTranslatedParameter, callback)); | |
| 405 } | |
| 406 | |
| 407 // static | |
| 408 bool FileUtilProxy::RelayCreateOrOpen( | |
| 409 TaskRunner* task_runner, | |
| 410 const CreateOrOpenTask& open_task, | |
| 411 const CloseTask& close_task, | |
| 412 const CreateOrOpenCallback& callback) { | |
| 413 CreateOrOpenHelper* helper = new CreateOrOpenHelper( | |
| 414 task_runner, close_task); | |
| 415 return task_runner->PostTaskAndReply( | |
| 416 FROM_HERE, | |
| 417 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task), | |
| 418 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); | |
| 419 } | |
| 420 | |
| 421 // static | |
| 422 bool FileUtilProxy::RelayClose( | |
| 423 TaskRunner* task_runner, | |
| 424 const CloseTask& close_task, | |
| 425 PlatformFile file_handle, | |
| 426 const StatusCallback& callback) { | |
| 427 return base::PostTaskAndReplyWithResult( | |
| 428 task_runner, FROM_HERE, Bind(close_task, file_handle), callback); | |
| 429 } | 343 } |
| 430 | 344 |
| 431 } // namespace base | 345 } // namespace base |
| OLD | NEW |