| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 "chrome/browser/chromeos/drive/file_system/close_file_operation.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/message_loop/message_loop_proxy.h" | |
| 9 #include "base/sequenced_task_runner.h" | |
| 10 #include "chrome/browser/chromeos/drive/drive.pb.h" | |
| 11 #include "chrome/browser/chromeos/drive/file_errors.h" | |
| 12 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" | |
| 13 #include "chrome/browser/chromeos/drive/resource_metadata.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 | |
| 16 using content::BrowserThread; | |
| 17 | |
| 18 namespace drive { | |
| 19 namespace file_system { | |
| 20 | |
| 21 CloseFileOperation::CloseFileOperation( | |
| 22 base::SequencedTaskRunner* blocking_task_runner, | |
| 23 OperationObserver* observer, | |
| 24 internal::ResourceMetadata* metadata, | |
| 25 std::map<base::FilePath, int>* open_files) | |
| 26 : blocking_task_runner_(blocking_task_runner), | |
| 27 observer_(observer), | |
| 28 metadata_(metadata), | |
| 29 open_files_(open_files), | |
| 30 weak_ptr_factory_(this) { | |
| 31 } | |
| 32 | |
| 33 CloseFileOperation::~CloseFileOperation() { | |
| 34 } | |
| 35 | |
| 36 void CloseFileOperation::CloseFile(const base::FilePath& file_path, | |
| 37 const FileOperationCallback& callback) { | |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 39 DCHECK(!callback.is_null()); | |
| 40 | |
| 41 if (open_files_->find(file_path) == open_files_->end()) { | |
| 42 // The file is not being opened. | |
| 43 base::MessageLoopProxy::current()->PostTask( | |
| 44 FROM_HERE, base::Bind(callback, FILE_ERROR_NOT_FOUND)); | |
| 45 return; | |
| 46 } | |
| 47 | |
| 48 ResourceEntry* entry = new ResourceEntry; | |
| 49 base::PostTaskAndReplyWithResult( | |
| 50 blocking_task_runner_.get(), | |
| 51 FROM_HERE, | |
| 52 base::Bind(&internal::ResourceMetadata::GetResourceEntryByPath, | |
| 53 base::Unretained(metadata_), file_path, entry), | |
| 54 base::Bind(&CloseFileOperation::CloseFileAfterGetResourceEntry, | |
| 55 weak_ptr_factory_.GetWeakPtr(), | |
| 56 file_path, callback, base::Owned(entry))); | |
| 57 } | |
| 58 | |
| 59 void CloseFileOperation::CloseFileAfterGetResourceEntry( | |
| 60 const base::FilePath& file_path, | |
| 61 const FileOperationCallback& callback, | |
| 62 const ResourceEntry* entry, | |
| 63 FileError error) { | |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 65 DCHECK(!callback.is_null()); | |
| 66 DCHECK(entry); | |
| 67 | |
| 68 if (error == FILE_ERROR_OK && entry->file_info().is_directory()) | |
| 69 error = FILE_ERROR_NOT_FOUND; | |
| 70 | |
| 71 DCHECK_GT((*open_files_)[file_path], 0); | |
| 72 if (--(*open_files_)[file_path] == 0) { | |
| 73 // All clients closes this file, so notify to upload the file. | |
| 74 open_files_->erase(file_path); | |
| 75 observer_->OnCacheFileUploadNeededByOperation(entry->resource_id()); | |
| 76 } | |
| 77 | |
| 78 // Then invokes the user-supplied callback function. | |
| 79 callback.Run(error); | |
| 80 } | |
| 81 | |
| 82 } // namespace file_system | |
| 83 } // namespace drive | |
| OLD | NEW |