| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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/sync/content_update_performer.h" | |
| 6 | |
| 7 #include "base/platform_file.h" | |
| 8 #include "chrome/browser/chromeos/drive/drive.pb.h" | |
| 9 #include "chrome/browser/chromeos/drive/file_cache.h" | |
| 10 #include "chrome/browser/chromeos/drive/job_scheduler.h" | |
| 11 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h" | |
| 12 #include "chrome/browser/chromeos/drive/resource_metadata.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 | |
| 15 using content::BrowserThread; | |
| 16 | |
| 17 namespace drive { | |
| 18 namespace file_system { | |
| 19 | |
| 20 struct ContentUpdatePerformer::LocalState { | |
| 21 LocalState() : should_upload(false) { | |
| 22 } | |
| 23 | |
| 24 std::string local_id; | |
| 25 ResourceEntry entry; | |
| 26 base::FilePath drive_file_path; | |
| 27 base::FilePath cache_file_path; | |
| 28 bool should_upload; | |
| 29 }; | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 // Gets locally stored information about the specified file. | |
| 34 FileError GetFileLocalState(internal::ResourceMetadata* metadata, | |
| 35 internal::FileCache* cache, | |
| 36 ContentUpdatePerformer::LocalState* local_state) { | |
| 37 FileError error = metadata->GetResourceEntryById(local_state->local_id, | |
| 38 &local_state->entry); | |
| 39 if (error != FILE_ERROR_OK) | |
| 40 return error; | |
| 41 | |
| 42 if (local_state->entry.file_info().is_directory()) | |
| 43 return FILE_ERROR_NOT_A_FILE; | |
| 44 | |
| 45 local_state->drive_file_path = metadata->GetFilePath(local_state->local_id); | |
| 46 if (local_state->drive_file_path.empty()) | |
| 47 return FILE_ERROR_NOT_FOUND; | |
| 48 | |
| 49 error = cache->GetFile(local_state->local_id, &local_state->cache_file_path); | |
| 50 if (error != FILE_ERROR_OK) | |
| 51 return error; | |
| 52 | |
| 53 // Do not upload the file if it's still opened. | |
| 54 if (cache->IsOpenedForWrite(local_state->local_id)) { | |
| 55 local_state->should_upload = false; | |
| 56 return FILE_ERROR_OK; | |
| 57 } | |
| 58 | |
| 59 FileCacheEntry cache_entry; | |
| 60 if (!cache->GetCacheEntry(local_state->local_id, &cache_entry)) | |
| 61 return FILE_ERROR_NOT_FOUND; | |
| 62 | |
| 63 // Update cache entry's MD5 if needed. | |
| 64 if (cache_entry.md5().empty()) { | |
| 65 error = cache->UpdateMd5(local_state->local_id); | |
| 66 if (error != FILE_ERROR_OK) | |
| 67 return error; | |
| 68 if (!cache->GetCacheEntry(local_state->local_id, &cache_entry)) | |
| 69 return FILE_ERROR_NOT_FOUND; | |
| 70 } | |
| 71 | |
| 72 local_state->should_upload = | |
| 73 (cache_entry.md5() != local_state->entry.file_specific_info().md5()); | |
| 74 if (!local_state->should_upload) | |
| 75 return cache->ClearDirty(local_state->local_id); | |
| 76 return FILE_ERROR_OK; | |
| 77 } | |
| 78 | |
| 79 // Updates locally stored information about the specified file. | |
| 80 FileError UpdateFileLocalState( | |
| 81 internal::ResourceMetadata* metadata, | |
| 82 internal::FileCache* cache, | |
| 83 const std::string& local_id, | |
| 84 scoped_ptr<google_apis::ResourceEntry> resource_entry) { | |
| 85 ResourceEntry existing_entry; | |
| 86 FileError error = metadata->GetResourceEntryById(local_id, &existing_entry); | |
| 87 if (error != FILE_ERROR_OK) | |
| 88 return error; | |
| 89 | |
| 90 ResourceEntry entry; | |
| 91 std::string parent_resource_id; | |
| 92 if (!ConvertToResourceEntry(*resource_entry, &entry, &parent_resource_id)) | |
| 93 return FILE_ERROR_NOT_A_FILE; | |
| 94 | |
| 95 entry.set_local_id(local_id); | |
| 96 entry.set_parent_local_id(existing_entry.parent_local_id()); | |
| 97 | |
| 98 error = metadata->RefreshEntry(entry); | |
| 99 if (error != FILE_ERROR_OK) | |
| 100 return error; | |
| 101 | |
| 102 FileCacheEntry cache_entry; | |
| 103 if (!cache->GetCacheEntry(local_id, &cache_entry)) | |
| 104 return FILE_ERROR_NOT_FOUND; | |
| 105 | |
| 106 // Do not clear dirty bit if the file has been edited during update. | |
| 107 if (cache_entry.md5() != entry.file_specific_info().md5()) | |
| 108 return FILE_ERROR_OK; | |
| 109 return cache->ClearDirty(local_id); | |
| 110 } | |
| 111 | |
| 112 } // namespace | |
| 113 | |
| 114 ContentUpdatePerformer::ContentUpdatePerformer( | |
| 115 base::SequencedTaskRunner* blocking_task_runner, | |
| 116 JobScheduler* scheduler, | |
| 117 internal::ResourceMetadata* metadata, | |
| 118 internal::FileCache* cache) | |
| 119 : blocking_task_runner_(blocking_task_runner), | |
| 120 scheduler_(scheduler), | |
| 121 metadata_(metadata), | |
| 122 cache_(cache), | |
| 123 weak_ptr_factory_(this) { | |
| 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 125 } | |
| 126 | |
| 127 ContentUpdatePerformer::~ContentUpdatePerformer() { | |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 129 } | |
| 130 | |
| 131 void ContentUpdatePerformer::UpdateFileByLocalId( | |
| 132 const std::string& local_id, | |
| 133 const ClientContext& context, | |
| 134 const FileOperationCallback& callback) { | |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 136 DCHECK(!callback.is_null()); | |
| 137 | |
| 138 LocalState* local_state = new LocalState; | |
| 139 local_state->local_id = local_id; | |
| 140 base::PostTaskAndReplyWithResult( | |
| 141 blocking_task_runner_.get(), | |
| 142 FROM_HERE, | |
| 143 base::Bind(&GetFileLocalState, | |
| 144 metadata_, | |
| 145 cache_, | |
| 146 local_state), | |
| 147 base::Bind(&ContentUpdatePerformer::UpdateFileAfterGetLocalState, | |
| 148 weak_ptr_factory_.GetWeakPtr(), | |
| 149 context, | |
| 150 callback, | |
| 151 base::Owned(local_state))); | |
| 152 } | |
| 153 | |
| 154 void ContentUpdatePerformer::UpdateFileAfterGetLocalState( | |
| 155 const ClientContext& context, | |
| 156 const FileOperationCallback& callback, | |
| 157 const LocalState* local_state, | |
| 158 FileError error) { | |
| 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 160 DCHECK(!callback.is_null()); | |
| 161 | |
| 162 if (error != FILE_ERROR_OK || !local_state->should_upload) { | |
| 163 callback.Run(error); | |
| 164 return; | |
| 165 } | |
| 166 | |
| 167 scheduler_->UploadExistingFile( | |
| 168 local_state->entry.resource_id(), | |
| 169 local_state->drive_file_path, | |
| 170 local_state->cache_file_path, | |
| 171 local_state->entry.file_specific_info().content_mime_type(), | |
| 172 "", // etag | |
| 173 context, | |
| 174 base::Bind(&ContentUpdatePerformer::UpdateFileAfterUpload, | |
| 175 weak_ptr_factory_.GetWeakPtr(), | |
| 176 callback, | |
| 177 local_state->local_id)); | |
| 178 } | |
| 179 | |
| 180 void ContentUpdatePerformer::UpdateFileAfterUpload( | |
| 181 const FileOperationCallback& callback, | |
| 182 const std::string& local_id, | |
| 183 google_apis::GDataErrorCode error, | |
| 184 scoped_ptr<google_apis::ResourceEntry> resource_entry) { | |
| 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 186 DCHECK(!callback.is_null()); | |
| 187 | |
| 188 FileError drive_error = GDataToFileError(error); | |
| 189 if (drive_error != FILE_ERROR_OK) { | |
| 190 callback.Run(drive_error); | |
| 191 return; | |
| 192 } | |
| 193 | |
| 194 base::PostTaskAndReplyWithResult( | |
| 195 blocking_task_runner_.get(), | |
| 196 FROM_HERE, | |
| 197 base::Bind(&UpdateFileLocalState, | |
| 198 metadata_, | |
| 199 cache_, | |
| 200 local_id, | |
| 201 base::Passed(&resource_entry)), | |
| 202 callback); | |
| 203 } | |
| 204 | |
| 205 } // namespace file_system | |
| 206 } // namespace drive | |
| OLD | NEW |