Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 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_util_proxy.h" | |
| 6 | |
| 7 #include "base/message_loop_proxy.h" | |
| 8 #include "webkit/fileapi/file_system_context.h" | |
| 9 #include "webkit/fileapi/file_system_file_util.h" | |
| 10 #include "webkit/fileapi/file_system_operation_context.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 class MessageLoopRelay | |
| 15 : public base::RefCountedThreadSafe<MessageLoopRelay> { | |
| 16 public: | |
| 17 // FileSystemOperationContext is passed by value from the IO thread to the | |
| 18 // File thread. | |
| 19 explicit MessageLoopRelay(const fileapi::FileSystemOperationContext& context) | |
| 20 : origin_message_loop_proxy_( | |
| 21 base::MessageLoopProxy::CreateForCurrentThread()), | |
| 22 error_code_(base::PLATFORM_FILE_OK), | |
| 23 context_(context) { | |
| 24 } | |
| 25 | |
| 26 bool Start(scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | |
| 27 const tracked_objects::Location& from_here) { | |
| 28 return message_loop_proxy->PostTask( | |
| 29 from_here, | |
| 30 NewRunnableMethod(this, &MessageLoopRelay::ProcessOnTargetThread)); | |
| 31 } | |
| 32 | |
| 33 protected: | |
| 34 friend class base::RefCountedThreadSafe<MessageLoopRelay>; | |
| 35 virtual ~MessageLoopRelay() {} | |
| 36 | |
| 37 // Called to perform work on the FILE thread. | |
| 38 virtual void RunWork() = 0; | |
| 39 | |
| 40 // Called to notify the callback on the origin thread. | |
| 41 virtual void RunCallback() = 0; | |
| 42 | |
| 43 void set_error_code(base::PlatformFileError error_code) { | |
| 44 error_code_ = error_code; | |
| 45 } | |
| 46 | |
| 47 base::PlatformFileError error_code() const { | |
| 48 return error_code_; | |
| 49 } | |
| 50 | |
| 51 fileapi::FileSystemOperationContext* context() { | |
| 52 return &context_; | |
| 53 } | |
| 54 | |
| 55 fileapi::FileSystemFileUtil* file_system_file_util() const { | |
| 56 return context_.file_system_file_util(); | |
| 57 } | |
| 58 | |
| 59 private: | |
| 60 void ProcessOnTargetThread() { | |
| 61 RunWork(); | |
| 62 origin_message_loop_proxy_->PostTask( | |
| 63 FROM_HERE, | |
| 64 NewRunnableMethod(this, &MessageLoopRelay::RunCallback)); | |
| 65 } | |
| 66 | |
| 67 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; | |
| 68 base::PlatformFileError error_code_; | |
| 69 fileapi::FileSystemOperationContext context_; | |
| 70 fileapi::FileSystemFileUtil* file_system_file_util_; | |
| 71 }; | |
| 72 | |
| 73 class RelayCreateOrOpen : public MessageLoopRelay { | |
| 74 public: | |
| 75 RelayCreateOrOpen( | |
| 76 const fileapi::FileSystemOperationContext& context, | |
| 77 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | |
| 78 const FilePath& file_path, | |
| 79 int file_flags, | |
| 80 fileapi::FileSystemFileUtilProxy::CreateOrOpenCallback* callback) | |
| 81 : MessageLoopRelay(context), | |
| 82 message_loop_proxy_(message_loop_proxy), | |
| 83 file_path_(file_path), | |
| 84 file_flags_(file_flags), | |
| 85 callback_(callback), | |
| 86 file_handle_(base::kInvalidPlatformFileValue), | |
| 87 created_(false) { | |
| 88 DCHECK(callback); | |
| 89 } | |
| 90 | |
| 91 protected: | |
| 92 virtual ~RelayCreateOrOpen() { | |
| 93 if (file_handle_ != base::kInvalidPlatformFileValue) | |
| 94 fileapi::FileSystemFileUtilProxy::Close(*context(), | |
|
ericu
2011/03/04 01:05:34
That * looks like a bug.
| |
| 95 message_loop_proxy_, file_handle_, NULL); | |
| 96 } | |
| 97 | |
| 98 virtual void RunWork() { | |
| 99 set_error_code( | |
| 100 file_system_file_util()->CreateOrOpen( | |
| 101 context(), file_path_, file_flags_, &file_handle_, &created_)); | |
| 102 } | |
| 103 | |
| 104 virtual void RunCallback() { | |
| 105 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), | |
| 106 created_); | |
| 107 delete callback_; | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | |
| 112 FilePath file_path_; | |
| 113 int file_flags_; | |
| 114 fileapi::FileSystemFileUtilProxy::CreateOrOpenCallback* callback_; | |
| 115 base::PlatformFile file_handle_; | |
| 116 bool created_; | |
| 117 }; | |
| 118 | |
| 119 class RelayWithStatusCallback : public MessageLoopRelay { | |
| 120 public: | |
| 121 RelayWithStatusCallback( | |
| 122 const fileapi::FileSystemOperationContext& context, | |
| 123 fileapi::FileSystemFileUtilProxy::StatusCallback* callback) | |
| 124 : MessageLoopRelay(context), | |
| 125 callback_(callback) { | |
| 126 // It is OK for callback to be NULL. | |
| 127 } | |
| 128 | |
| 129 protected: | |
| 130 virtual void RunCallback() { | |
| 131 // The caller may not have been interested in the result. | |
| 132 if (callback_) { | |
| 133 callback_->Run(error_code()); | |
| 134 delete callback_; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 private: | |
| 139 fileapi::FileSystemFileUtilProxy::StatusCallback* callback_; | |
| 140 }; | |
| 141 | |
| 142 class RelayClose : public RelayWithStatusCallback { | |
| 143 public: | |
| 144 RelayClose(const fileapi::FileSystemOperationContext& context, | |
| 145 base::PlatformFile file_handle, | |
| 146 fileapi::FileSystemFileUtilProxy::StatusCallback* callback) | |
| 147 : RelayWithStatusCallback(context, callback), | |
| 148 file_handle_(file_handle) { | |
| 149 } | |
| 150 | |
| 151 protected: | |
| 152 virtual void RunWork() { | |
| 153 set_error_code( | |
| 154 file_system_file_util()->Close(context(), file_handle_)); | |
| 155 } | |
| 156 | |
| 157 private: | |
| 158 base::PlatformFile file_handle_; | |
| 159 }; | |
| 160 | |
| 161 class RelayEnsureFileExists : public MessageLoopRelay { | |
| 162 public: | |
| 163 RelayEnsureFileExists( | |
| 164 const fileapi::FileSystemOperationContext& context, | |
| 165 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | |
| 166 const FilePath& file_path, | |
| 167 fileapi::FileSystemFileUtilProxy::EnsureFileExistsCallback* callback) | |
| 168 : MessageLoopRelay(context), | |
| 169 message_loop_proxy_(message_loop_proxy), | |
| 170 file_path_(file_path), | |
| 171 callback_(callback), | |
| 172 created_(false) { | |
| 173 DCHECK(callback); | |
| 174 } | |
| 175 | |
| 176 protected: | |
| 177 virtual void RunWork() { | |
| 178 set_error_code( | |
| 179 file_system_file_util()->EnsureFileExists( | |
| 180 context(), file_path_, &created_)); | |
| 181 } | |
| 182 | |
| 183 virtual void RunCallback() { | |
| 184 callback_->Run(error_code(), created_); | |
| 185 delete callback_; | |
| 186 } | |
| 187 | |
| 188 private: | |
| 189 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | |
| 190 FilePath file_path_; | |
| 191 fileapi::FileSystemFileUtilProxy::EnsureFileExistsCallback* callback_; | |
| 192 bool created_; | |
| 193 }; | |
| 194 | |
| 195 class RelayGetFileInfo : public MessageLoopRelay { | |
| 196 public: | |
| 197 RelayGetFileInfo( | |
| 198 const fileapi::FileSystemOperationContext& context, | |
| 199 const FilePath& file_path, | |
| 200 fileapi::FileSystemFileUtilProxy::GetFileInfoCallback* callback) | |
| 201 : MessageLoopRelay(context), | |
| 202 callback_(callback), | |
| 203 file_path_(file_path) { | |
| 204 DCHECK(callback); | |
| 205 } | |
| 206 | |
| 207 protected: | |
| 208 virtual void RunWork() { | |
| 209 set_error_code( | |
| 210 file_system_file_util()->GetFileInfo( | |
| 211 context(), file_path_, &file_info_)); | |
| 212 } | |
| 213 | |
| 214 virtual void RunCallback() { | |
| 215 callback_->Run(error_code(), file_info_); | |
| 216 delete callback_; | |
| 217 } | |
| 218 | |
| 219 private: | |
| 220 fileapi::FileSystemFileUtilProxy::GetFileInfoCallback* callback_; | |
| 221 FilePath file_path_; | |
| 222 base::PlatformFileInfo file_info_; | |
| 223 }; | |
| 224 | |
| 225 class RelayReadDirectory : public MessageLoopRelay { | |
| 226 public: | |
| 227 RelayReadDirectory( | |
| 228 const fileapi::FileSystemOperationContext& context, | |
| 229 const FilePath& file_path, | |
| 230 fileapi::FileSystemFileUtilProxy::ReadDirectoryCallback* callback) | |
| 231 : MessageLoopRelay(context), | |
| 232 callback_(callback), file_path_(file_path) { | |
| 233 DCHECK(callback); | |
| 234 } | |
| 235 | |
| 236 protected: | |
| 237 virtual void RunWork() { | |
| 238 // TODO(kkanetkar): Implement directory read in multiple chunks. | |
| 239 set_error_code( | |
| 240 file_system_file_util()->ReadDirectory( | |
| 241 context(), file_path_, &entries_)); | |
| 242 } | |
| 243 | |
| 244 virtual void RunCallback() { | |
| 245 callback_->Run(error_code(), entries_); | |
| 246 delete callback_; | |
| 247 } | |
| 248 | |
| 249 private: | |
| 250 fileapi::FileSystemFileUtilProxy::ReadDirectoryCallback* callback_; | |
| 251 FilePath file_path_; | |
| 252 std::vector<base::FileUtilProxy::Entry> entries_; | |
| 253 }; | |
| 254 | |
| 255 class RelayCreateDirectory : public RelayWithStatusCallback { | |
| 256 public: | |
| 257 RelayCreateDirectory( | |
| 258 const fileapi::FileSystemOperationContext& context, | |
| 259 const FilePath& file_path, | |
| 260 bool exclusive, | |
| 261 fileapi::FileSystemFileUtilProxy::StatusCallback* callback) | |
| 262 : RelayWithStatusCallback(context, callback), | |
| 263 file_path_(file_path), | |
| 264 exclusive_(exclusive) { | |
| 265 } | |
| 266 | |
| 267 protected: | |
| 268 virtual void RunWork() { | |
| 269 set_error_code( | |
| 270 file_system_file_util()->CreateDirectory( | |
| 271 context(), file_path_, exclusive_)); | |
| 272 } | |
| 273 | |
| 274 private: | |
| 275 FilePath file_path_; | |
| 276 bool exclusive_; | |
| 277 }; | |
| 278 | |
| 279 class RelayCopy : public RelayWithStatusCallback { | |
| 280 public: | |
| 281 RelayCopy(const fileapi::FileSystemOperationContext& context, | |
| 282 const FilePath& src_file_path, | |
| 283 const FilePath& dest_file_path, | |
| 284 fileapi::FileSystemFileUtilProxy::StatusCallback* callback) | |
| 285 : RelayWithStatusCallback(context, callback), | |
| 286 src_file_path_(src_file_path), | |
| 287 dest_file_path_(dest_file_path) { | |
| 288 } | |
| 289 | |
| 290 protected: | |
| 291 virtual void RunWork() { | |
| 292 set_error_code( | |
| 293 file_system_file_util()->Copy( | |
| 294 context(), src_file_path_, dest_file_path_)); | |
| 295 } | |
| 296 | |
| 297 private: | |
| 298 FilePath src_file_path_; | |
| 299 FilePath dest_file_path_; | |
| 300 }; | |
| 301 | |
| 302 class RelayMove : public RelayWithStatusCallback { | |
| 303 public: | |
| 304 RelayMove(const fileapi::FileSystemOperationContext& context, | |
| 305 const FilePath& src_file_path, | |
| 306 const FilePath& dest_file_path, | |
| 307 fileapi::FileSystemFileUtilProxy::StatusCallback* callback) | |
| 308 : RelayWithStatusCallback(context, callback), | |
| 309 src_file_path_(src_file_path), | |
| 310 dest_file_path_(dest_file_path) { | |
| 311 } | |
| 312 | |
| 313 protected: | |
| 314 virtual void RunWork() { | |
| 315 set_error_code( | |
| 316 file_system_file_util()->Move( | |
| 317 context(), src_file_path_, dest_file_path_)); | |
| 318 } | |
| 319 | |
| 320 private: | |
| 321 FilePath src_file_path_; | |
| 322 FilePath dest_file_path_; | |
| 323 }; | |
| 324 | |
| 325 class RelayDelete : public RelayWithStatusCallback { | |
| 326 public: | |
| 327 RelayDelete(const fileapi::FileSystemOperationContext& context, | |
| 328 const FilePath& file_path, | |
| 329 bool recursive, | |
| 330 fileapi::FileSystemFileUtilProxy::StatusCallback* callback) | |
| 331 : RelayWithStatusCallback(context, callback), | |
| 332 file_path_(file_path), | |
| 333 recursive_(recursive) { | |
| 334 } | |
| 335 | |
| 336 protected: | |
| 337 virtual void RunWork() { | |
| 338 set_error_code( | |
| 339 file_system_file_util()->Delete( | |
| 340 context(), file_path_, recursive_)); | |
| 341 } | |
| 342 | |
| 343 private: | |
| 344 FilePath file_path_; | |
| 345 bool recursive_; | |
| 346 }; | |
| 347 | |
| 348 class RelayTouchFilePath : public RelayWithStatusCallback { | |
| 349 public: | |
| 350 RelayTouchFilePath(const fileapi::FileSystemOperationContext& context, | |
| 351 const FilePath& file_path, | |
| 352 const base::Time& last_access_time, | |
| 353 const base::Time& last_modified_time, | |
| 354 fileapi::FileSystemFileUtilProxy::StatusCallback* callback) | |
| 355 : RelayWithStatusCallback(context, callback), | |
| 356 file_path_(file_path), | |
| 357 last_access_time_(last_access_time), | |
| 358 last_modified_time_(last_modified_time) { | |
| 359 } | |
| 360 | |
| 361 protected: | |
| 362 virtual void RunWork() { | |
| 363 set_error_code( | |
| 364 file_system_file_util()->Touch( | |
| 365 context(), file_path_, last_access_time_, last_modified_time_)); | |
| 366 } | |
| 367 | |
| 368 private: | |
| 369 FilePath file_path_; | |
| 370 base::Time last_access_time_; | |
| 371 base::Time last_modified_time_; | |
| 372 }; | |
| 373 | |
| 374 class RelayTruncate : public RelayWithStatusCallback { | |
| 375 public: | |
| 376 RelayTruncate(const fileapi::FileSystemOperationContext& context, | |
| 377 const FilePath& file_path, | |
| 378 int64 length, | |
| 379 fileapi::FileSystemFileUtilProxy::StatusCallback* callback) | |
| 380 : RelayWithStatusCallback(context, callback), | |
| 381 file_path_(file_path), | |
| 382 length_(length) { | |
| 383 } | |
| 384 | |
| 385 protected: | |
| 386 virtual void RunWork() { | |
| 387 set_error_code( | |
| 388 file_system_file_util()->Truncate(context(), file_path_, length_)); | |
| 389 } | |
| 390 | |
| 391 private: | |
| 392 FilePath file_path_; | |
| 393 int64 length_; | |
| 394 }; | |
| 395 | |
| 396 bool Start(const tracked_objects::Location& from_here, | |
| 397 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | |
| 398 scoped_refptr<MessageLoopRelay> relay) { | |
| 399 return relay->Start(message_loop_proxy, from_here); | |
| 400 } | |
| 401 | |
| 402 } // namespace | |
| 403 | |
| 404 namespace fileapi { | |
| 405 | |
| 406 // static | |
| 407 bool FileSystemFileUtilProxy::CreateOrOpen( | |
| 408 const FileSystemOperationContext& context, | |
| 409 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 410 const FilePath& file_path, int file_flags, | |
| 411 CreateOrOpenCallback* callback) { | |
| 412 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen(context, | |
| 413 message_loop_proxy, file_path, file_flags, callback)); | |
| 414 } | |
| 415 | |
| 416 // static | |
| 417 bool FileSystemFileUtilProxy::Close( | |
| 418 const FileSystemOperationContext& context, | |
| 419 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 420 base::PlatformFile file_handle, | |
| 421 StatusCallback* callback) { | |
| 422 return Start(FROM_HERE, message_loop_proxy, | |
| 423 new RelayClose(context, file_handle, callback)); | |
| 424 } | |
| 425 | |
| 426 // static | |
| 427 bool FileSystemFileUtilProxy::EnsureFileExists( | |
| 428 const FileSystemOperationContext& context, | |
| 429 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 430 const FilePath& file_path, | |
| 431 EnsureFileExistsCallback* callback) { | |
| 432 return Start(FROM_HERE, message_loop_proxy, new RelayEnsureFileExists( | |
| 433 context, message_loop_proxy, file_path, callback)); | |
| 434 } | |
| 435 | |
| 436 // Retrieves the information about a file. It is invalid to pass NULL for the | |
| 437 // callback. | |
| 438 bool FileSystemFileUtilProxy::GetFileInfo( | |
| 439 const FileSystemOperationContext& context, | |
| 440 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 441 const FilePath& file_path, | |
| 442 GetFileInfoCallback* callback) { | |
| 443 return Start(FROM_HERE, message_loop_proxy, new RelayGetFileInfo(context, | |
| 444 file_path, callback)); | |
| 445 } | |
| 446 | |
| 447 // static | |
| 448 bool FileSystemFileUtilProxy::ReadDirectory( | |
| 449 const FileSystemOperationContext& context, | |
| 450 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 451 const FilePath& file_path, | |
| 452 ReadDirectoryCallback* callback) { | |
| 453 return Start(FROM_HERE, message_loop_proxy, new RelayReadDirectory(context, | |
| 454 file_path, callback)); | |
| 455 } | |
| 456 | |
| 457 // static | |
| 458 bool FileSystemFileUtilProxy::CreateDirectory( | |
| 459 const FileSystemOperationContext& context, | |
| 460 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 461 const FilePath& file_path, | |
| 462 bool exclusive, | |
| 463 StatusCallback* callback) { | |
| 464 return Start(FROM_HERE, message_loop_proxy, new RelayCreateDirectory( | |
| 465 context, file_path, exclusive, callback)); | |
| 466 } | |
| 467 | |
| 468 // static | |
| 469 bool FileSystemFileUtilProxy::Copy( | |
| 470 const FileSystemOperationContext& context, | |
| 471 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 472 const FilePath& src_file_path, | |
| 473 const FilePath& dest_file_path, | |
| 474 StatusCallback* callback) { | |
| 475 return Start(FROM_HERE, message_loop_proxy, | |
| 476 new RelayCopy(context, src_file_path, dest_file_path, | |
| 477 callback)); | |
| 478 } | |
| 479 | |
| 480 // static | |
| 481 bool FileSystemFileUtilProxy::Move( | |
| 482 const FileSystemOperationContext& context, | |
| 483 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 484 const FilePath& src_file_path, | |
| 485 const FilePath& dest_file_path, | |
| 486 StatusCallback* callback) { | |
| 487 return Start(FROM_HERE, message_loop_proxy, | |
| 488 new RelayMove(context, src_file_path, dest_file_path, | |
| 489 callback)); | |
| 490 } | |
| 491 | |
| 492 // static | |
| 493 bool FileSystemFileUtilProxy::Delete( | |
| 494 const FileSystemOperationContext& context, | |
| 495 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 496 const FilePath& file_path, | |
| 497 bool recursive, | |
| 498 StatusCallback* callback) { | |
| 499 return Start(FROM_HERE, message_loop_proxy, | |
| 500 new RelayDelete(context, file_path, recursive, callback)); | |
| 501 } | |
| 502 | |
| 503 // static | |
| 504 bool FileSystemFileUtilProxy::Touch( | |
| 505 const FileSystemOperationContext& context, | |
| 506 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 507 const FilePath& file_path, | |
| 508 const base::Time& last_access_time, | |
| 509 const base::Time& last_modified_time, | |
| 510 StatusCallback* callback) { | |
| 511 return Start(FROM_HERE, message_loop_proxy, | |
| 512 new RelayTouchFilePath(context, file_path, last_access_time, | |
| 513 last_modified_time, callback)); | |
| 514 } | |
| 515 | |
| 516 // static | |
| 517 bool FileSystemFileUtilProxy::Truncate( | |
| 518 const FileSystemOperationContext& context, | |
| 519 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 520 const FilePath& path, | |
| 521 int64 length, | |
| 522 StatusCallback* callback) { | |
| 523 return Start(FROM_HERE, message_loop_proxy, | |
| 524 new RelayTruncate(context, path, length, callback)); | |
| 525 } | |
| 526 | |
| 527 } // namespace fileapi | |
| OLD | NEW |