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/get_file_for_saving_operatio
n.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chrome/browser/chromeos/drive/file_system/create_file_operation.h" |
| 9 #include "chrome/browser/chromeos/drive/file_system/download_operation.h" |
| 10 #include "chrome/browser/chromeos/drive/file_write_watcher.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 |
| 13 using content::BrowserThread; |
| 14 |
| 15 namespace drive { |
| 16 namespace file_system { |
| 17 |
| 18 GetFileForSavingOperation::GetFileForSavingOperation( |
| 19 base::SequencedTaskRunner* blocking_task_runner, |
| 20 OperationObserver* observer, |
| 21 JobScheduler* scheduler, |
| 22 internal::ResourceMetadata* metadata, |
| 23 internal::FileCache* cache, |
| 24 const base::FilePath& temporary_file_directory) |
| 25 : create_file_operation_(new CreateFileOperation(blocking_task_runner, |
| 26 observer, |
| 27 scheduler, |
| 28 metadata, |
| 29 cache)), |
| 30 download_operation_(new DownloadOperation(blocking_task_runner, |
| 31 observer, |
| 32 scheduler, |
| 33 metadata, |
| 34 cache, |
| 35 temporary_file_directory)), |
| 36 file_write_watcher_(new internal::FileWriteWatcher(observer)), |
| 37 cache_(cache), |
| 38 weak_ptr_factory_(this) { |
| 39 } |
| 40 |
| 41 GetFileForSavingOperation::~GetFileForSavingOperation() { |
| 42 } |
| 43 |
| 44 void GetFileForSavingOperation::GetFileForSaving( |
| 45 const base::FilePath& file_path, |
| 46 const GetFileCallback& callback) { |
| 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 48 DCHECK(!callback.is_null()); |
| 49 |
| 50 create_file_operation_->CreateFile( |
| 51 file_path, |
| 52 false, // error_if_already_exists |
| 53 base::Bind(&GetFileForSavingOperation::GetFileForSavingAfterCreate, |
| 54 weak_ptr_factory_.GetWeakPtr(), |
| 55 file_path, |
| 56 callback)); |
| 57 } |
| 58 |
| 59 void GetFileForSavingOperation::GetFileForSavingAfterCreate( |
| 60 const base::FilePath& file_path, |
| 61 const GetFileCallback& callback, |
| 62 FileError error) { |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 64 DCHECK(!callback.is_null()); |
| 65 |
| 66 if (error != FILE_ERROR_OK) { |
| 67 callback.Run(error, base::FilePath(), scoped_ptr<ResourceEntry>()); |
| 68 return; |
| 69 } |
| 70 |
| 71 download_operation_->EnsureFileDownloadedByPath( |
| 72 file_path, |
| 73 ClientContext(USER_INITIATED), |
| 74 GetFileContentInitializedCallback(), |
| 75 google_apis::GetContentCallback(), |
| 76 base::Bind(&GetFileForSavingOperation::GetFileForSavingAfterDownload, |
| 77 weak_ptr_factory_.GetWeakPtr(), |
| 78 callback)); |
| 79 } |
| 80 |
| 81 void GetFileForSavingOperation::GetFileForSavingAfterDownload( |
| 82 const GetFileCallback& callback, |
| 83 FileError error, |
| 84 const base::FilePath& cache_path, |
| 85 scoped_ptr<ResourceEntry> entry) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 87 DCHECK(!callback.is_null()); |
| 88 |
| 89 if (error != FILE_ERROR_OK) { |
| 90 callback.Run(error, base::FilePath(), scoped_ptr<ResourceEntry>()); |
| 91 return; |
| 92 } |
| 93 |
| 94 const std::string& resource_id = entry->resource_id(); |
| 95 cache_->MarkDirtyOnUIThread( |
| 96 resource_id, |
| 97 base::Bind(&GetFileForSavingOperation::GetFileForSavingAfterMarkDirty, |
| 98 weak_ptr_factory_.GetWeakPtr(), |
| 99 callback, |
| 100 cache_path, |
| 101 base::Passed(&entry))); |
| 102 } |
| 103 |
| 104 void GetFileForSavingOperation::GetFileForSavingAfterMarkDirty( |
| 105 const GetFileCallback& callback, |
| 106 const base::FilePath& cache_path, |
| 107 scoped_ptr<ResourceEntry> entry, |
| 108 FileError error) { |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 110 DCHECK(!callback.is_null()); |
| 111 |
| 112 if (error != FILE_ERROR_OK) { |
| 113 callback.Run(error, base::FilePath(), scoped_ptr<ResourceEntry>()); |
| 114 return; |
| 115 } |
| 116 |
| 117 const std::string& resource_id = entry->resource_id(); |
| 118 file_write_watcher_->StartWatch( |
| 119 cache_path, |
| 120 resource_id, |
| 121 base::Bind(&GetFileForSavingOperation::GetFileForSavingAfterWatch, |
| 122 weak_ptr_factory_.GetWeakPtr(), |
| 123 callback, |
| 124 cache_path, |
| 125 base::Passed(&entry))); |
| 126 } |
| 127 |
| 128 void GetFileForSavingOperation::GetFileForSavingAfterWatch( |
| 129 const GetFileCallback& callback, |
| 130 const base::FilePath& cache_path, |
| 131 scoped_ptr<ResourceEntry> entry, |
| 132 bool success) { |
| 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 134 DCHECK(!callback.is_null()); |
| 135 |
| 136 if (!success) { |
| 137 callback.Run(FILE_ERROR_FAILED, |
| 138 base::FilePath(), scoped_ptr<ResourceEntry>()); |
| 139 return; |
| 140 } |
| 141 |
| 142 callback.Run(FILE_ERROR_OK, cache_path, entry.Pass()); |
| 143 } |
| 144 |
| 145 } // namespace file_system |
| 146 } // namespace drive |
OLD | NEW |