| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/chromeos/drive/sync/entry_update_performer.h" | 5 #include "chrome/browser/chromeos/drive/sync/entry_update_performer.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" |
| 8 #include "base/file_util.h" |
| 9 #include "chrome/browser/chromeos/drive/change_list_loader.h" |
| 7 #include "chrome/browser/chromeos/drive/drive.pb.h" | 10 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 8 #include "chrome/browser/chromeos/drive/file_cache.h" | 11 #include "chrome/browser/chromeos/drive/file_cache.h" |
| 9 #include "chrome/browser/chromeos/drive/file_system_util.h" | 12 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 10 #include "chrome/browser/chromeos/drive/job_scheduler.h" | 13 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
| 11 #include "chrome/browser/chromeos/drive/resource_metadata.h" | 14 #include "chrome/browser/chromeos/drive/resource_metadata.h" |
| 12 #include "chrome/browser/chromeos/drive/sync/entry_revert_performer.h" | 15 #include "chrome/browser/chromeos/drive/sync/entry_revert_performer.h" |
| 13 #include "chrome/browser/chromeos/drive/sync/remove_performer.h" | 16 #include "chrome/browser/chromeos/drive/sync/remove_performer.h" |
| 14 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 15 | 18 |
| 16 using content::BrowserThread; | 19 using content::BrowserThread; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 43 | 46 |
| 44 error = metadata->GetResourceEntryById(local_state->entry.parent_local_id(), | 47 error = metadata->GetResourceEntryById(local_state->entry.parent_local_id(), |
| 45 &local_state->parent_entry); | 48 &local_state->parent_entry); |
| 46 if (error != FILE_ERROR_OK) | 49 if (error != FILE_ERROR_OK) |
| 47 return error; | 50 return error; |
| 48 | 51 |
| 49 local_state->drive_file_path = metadata->GetFilePath(local_id); | 52 local_state->drive_file_path = metadata->GetFilePath(local_id); |
| 50 if (local_state->drive_file_path.empty()) | 53 if (local_state->drive_file_path.empty()) |
| 51 return FILE_ERROR_NOT_FOUND; | 54 return FILE_ERROR_NOT_FOUND; |
| 52 | 55 |
| 56 FileCacheEntry cache_entry; |
| 57 cache->GetCacheEntry(local_id, &cache_entry); |
| 58 if (!cache_entry.is_present() && local_state->entry.resource_id().empty()) { |
| 59 // Locally created file with no cache file, store an empty file. |
| 60 base::FilePath empty_file; |
| 61 if (!base::CreateTemporaryFile(&empty_file)) |
| 62 return FILE_ERROR_FAILED; |
| 63 error = cache->Store(local_id, std::string(), empty_file, |
| 64 FileCache::FILE_OPERATION_MOVE); |
| 65 if (error != FILE_ERROR_OK) |
| 66 return error; |
| 67 if (!cache->GetCacheEntry(local_id, &cache_entry)) |
| 68 return FILE_ERROR_NOT_FOUND; |
| 69 } |
| 70 |
| 53 // Check if content update is needed or not. | 71 // Check if content update is needed or not. |
| 54 FileCacheEntry cache_entry; | 72 if (cache_entry.is_dirty() && !cache->IsOpenedForWrite(local_id)) { |
| 55 if (cache->GetCacheEntry(local_id, &cache_entry) && | |
| 56 cache_entry.is_dirty() && | |
| 57 !cache->IsOpenedForWrite(local_id)) { | |
| 58 // Update cache entry's MD5 if needed. | 73 // Update cache entry's MD5 if needed. |
| 59 if (cache_entry.md5().empty()) { | 74 if (cache_entry.md5().empty()) { |
| 60 error = cache->UpdateMd5(local_id); | 75 error = cache->UpdateMd5(local_id); |
| 61 if (error != FILE_ERROR_OK) | 76 if (error != FILE_ERROR_OK) |
| 62 return error; | 77 return error; |
| 63 if (!cache->GetCacheEntry(local_id, &cache_entry)) | 78 if (!cache->GetCacheEntry(local_id, &cache_entry)) |
| 64 return FILE_ERROR_NOT_FOUND; | 79 return FILE_ERROR_NOT_FOUND; |
| 65 } | 80 } |
| 66 | 81 |
| 67 if (cache_entry.md5() == local_state->entry.file_specific_info().md5()) { | 82 if (cache_entry.md5() == local_state->entry.file_specific_info().md5()) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 case ResourceEntry::CLEAN: // Nothing to do. | 122 case ResourceEntry::CLEAN: // Nothing to do. |
| 108 case ResourceEntry::DIRTY: // Entry was edited again during the update. | 123 case ResourceEntry::DIRTY: // Entry was edited again during the update. |
| 109 break; | 124 break; |
| 110 | 125 |
| 111 case ResourceEntry::SYNCING: | 126 case ResourceEntry::SYNCING: |
| 112 entry.set_metadata_edit_state(ResourceEntry::CLEAN); | 127 entry.set_metadata_edit_state(ResourceEntry::CLEAN); |
| 113 break; | 128 break; |
| 114 } | 129 } |
| 115 if (!entry.file_info().is_directory()) | 130 if (!entry.file_info().is_directory()) |
| 116 entry.mutable_file_specific_info()->set_md5(resource_entry->file_md5()); | 131 entry.mutable_file_specific_info()->set_md5(resource_entry->file_md5()); |
| 132 entry.set_resource_id(resource_entry->resource_id()); |
| 117 error = metadata->RefreshEntry(entry); | 133 error = metadata->RefreshEntry(entry); |
| 118 if (error != FILE_ERROR_OK) | 134 if (error != FILE_ERROR_OK) |
| 119 return error; | 135 return error; |
| 120 | 136 |
| 121 // Clear dirty bit unless the file has been edited during update. | 137 // Clear dirty bit unless the file has been edited during update. |
| 122 FileCacheEntry cache_entry; | 138 FileCacheEntry cache_entry; |
| 123 if (cache->GetCacheEntry(local_id, &cache_entry) && | 139 if (cache->GetCacheEntry(local_id, &cache_entry) && |
| 124 cache_entry.md5() == entry.file_specific_info().md5()) { | 140 cache_entry.md5() == entry.file_specific_info().md5()) { |
| 125 error = cache->ClearDirty(local_id); | 141 error = cache->ClearDirty(local_id); |
| 126 if (error != FILE_ERROR_OK) | 142 if (error != FILE_ERROR_OK) |
| 127 return error; | 143 return error; |
| 128 } | 144 } |
| 129 return FILE_ERROR_OK; | 145 return FILE_ERROR_OK; |
| 130 } | 146 } |
| 131 | 147 |
| 132 } // namespace | 148 } // namespace |
| 133 | 149 |
| 134 EntryUpdatePerformer::EntryUpdatePerformer( | 150 EntryUpdatePerformer::EntryUpdatePerformer( |
| 135 base::SequencedTaskRunner* blocking_task_runner, | 151 base::SequencedTaskRunner* blocking_task_runner, |
| 136 file_system::OperationObserver* observer, | 152 file_system::OperationObserver* observer, |
| 137 JobScheduler* scheduler, | 153 JobScheduler* scheduler, |
| 138 ResourceMetadata* metadata, | 154 ResourceMetadata* metadata, |
| 139 FileCache* cache) | 155 FileCache* cache, |
| 156 ChangeListLoader* change_list_loader) |
| 140 : blocking_task_runner_(blocking_task_runner), | 157 : blocking_task_runner_(blocking_task_runner), |
| 141 scheduler_(scheduler), | 158 scheduler_(scheduler), |
| 142 metadata_(metadata), | 159 metadata_(metadata), |
| 143 cache_(cache), | 160 cache_(cache), |
| 161 change_list_loader_(change_list_loader), |
| 144 remove_performer_(new RemovePerformer(blocking_task_runner, | 162 remove_performer_(new RemovePerformer(blocking_task_runner, |
| 145 observer, | 163 observer, |
| 146 scheduler, | 164 scheduler, |
| 147 metadata)), | 165 metadata)), |
| 148 entry_revert_performer_(new EntryRevertPerformer(blocking_task_runner, | 166 entry_revert_performer_(new EntryRevertPerformer(blocking_task_runner, |
| 149 observer, | 167 observer, |
| 150 scheduler, | 168 scheduler, |
| 151 metadata)), | 169 metadata)), |
| 152 weak_ptr_factory_(this) { | 170 weak_ptr_factory_(this) { |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 return; | 211 return; |
| 194 } | 212 } |
| 195 | 213 |
| 196 base::Time last_modified = base::Time::FromInternalValue( | 214 base::Time last_modified = base::Time::FromInternalValue( |
| 197 local_state->entry.file_info().last_modified()); | 215 local_state->entry.file_info().last_modified()); |
| 198 base::Time last_accessed = base::Time::FromInternalValue( | 216 base::Time last_accessed = base::Time::FromInternalValue( |
| 199 local_state->entry.file_info().last_accessed()); | 217 local_state->entry.file_info().last_accessed()); |
| 200 | 218 |
| 201 // Perform content update. | 219 // Perform content update. |
| 202 if (local_state->should_content_update) { | 220 if (local_state->should_content_update) { |
| 203 drive::DriveUploader::UploadExistingFileOptions options; | 221 if (local_state->entry.resource_id().empty()) { |
| 204 options.title = local_state->entry.title(); | 222 drive::DriveUploader::UploadNewFileOptions options; |
| 205 options.parent_resource_id = local_state->parent_entry.resource_id(); | 223 options.modified_date = last_modified; |
| 206 options.modified_date = last_modified; | 224 options.last_viewed_by_me_date = last_accessed; |
| 207 options.last_viewed_by_me_date = last_accessed; | 225 scheduler_->UploadNewFile( |
| 208 scheduler_->UploadExistingFile( | 226 local_state->parent_entry.resource_id(), |
| 209 local_state->entry.resource_id(), | 227 local_state->drive_file_path, |
| 210 local_state->drive_file_path, | 228 local_state->cache_file_path, |
| 211 local_state->cache_file_path, | 229 local_state->entry.title(), |
| 212 local_state->entry.file_specific_info().content_mime_type(), | 230 local_state->entry.file_specific_info().content_mime_type(), |
| 213 options, | 231 options, |
| 214 context, | 232 context, |
| 215 base::Bind(&EntryUpdatePerformer::UpdateEntryAfterUpdateResource, | 233 base::Bind(&EntryUpdatePerformer::UpdateEntryAfterUpdateResource, |
| 216 weak_ptr_factory_.GetWeakPtr(), | 234 weak_ptr_factory_.GetWeakPtr(), |
| 217 context, | 235 context, |
| 218 callback, | 236 callback, |
| 219 local_state->entry.local_id())); | 237 local_state->entry.local_id(), |
| 238 base::Passed(change_list_loader_->GetLock()))); |
| 239 } else { |
| 240 drive::DriveUploader::UploadExistingFileOptions options; |
| 241 options.title = local_state->entry.title(); |
| 242 options.parent_resource_id = local_state->parent_entry.resource_id(); |
| 243 options.modified_date = last_modified; |
| 244 options.last_viewed_by_me_date = last_accessed; |
| 245 scheduler_->UploadExistingFile( |
| 246 local_state->entry.resource_id(), |
| 247 local_state->drive_file_path, |
| 248 local_state->cache_file_path, |
| 249 local_state->entry.file_specific_info().content_mime_type(), |
| 250 options, |
| 251 context, |
| 252 base::Bind(&EntryUpdatePerformer::UpdateEntryAfterUpdateResource, |
| 253 weak_ptr_factory_.GetWeakPtr(), |
| 254 context, |
| 255 callback, |
| 256 local_state->entry.local_id(), |
| 257 base::Passed(scoped_ptr<base::ScopedClosureRunner>()))); |
| 258 } |
| 220 return; | 259 return; |
| 221 } | 260 } |
| 222 | 261 |
| 223 // No need to perform update. | 262 // No need to perform update. |
| 224 if (local_state->entry.metadata_edit_state() == ResourceEntry::CLEAN) { | 263 if (local_state->entry.metadata_edit_state() == ResourceEntry::CLEAN) { |
| 225 callback.Run(FILE_ERROR_OK); | 264 callback.Run(FILE_ERROR_OK); |
| 226 return; | 265 return; |
| 227 } | 266 } |
| 228 | 267 |
| 229 // Perform metadata update. | 268 // Perform metadata update. |
| 230 scheduler_->UpdateResource( | 269 scheduler_->UpdateResource( |
| 231 local_state->entry.resource_id(), local_state->parent_entry.resource_id(), | 270 local_state->entry.resource_id(), local_state->parent_entry.resource_id(), |
| 232 local_state->entry.title(), last_modified, last_accessed, | 271 local_state->entry.title(), last_modified, last_accessed, |
| 233 context, | 272 context, |
| 234 base::Bind(&EntryUpdatePerformer::UpdateEntryAfterUpdateResource, | 273 base::Bind(&EntryUpdatePerformer::UpdateEntryAfterUpdateResource, |
| 235 weak_ptr_factory_.GetWeakPtr(), | 274 weak_ptr_factory_.GetWeakPtr(), |
| 236 context, callback, local_state->entry.local_id())); | 275 context, callback, local_state->entry.local_id(), |
| 276 base::Passed(scoped_ptr<base::ScopedClosureRunner>()))); |
| 237 } | 277 } |
| 238 | 278 |
| 239 void EntryUpdatePerformer::UpdateEntryAfterUpdateResource( | 279 void EntryUpdatePerformer::UpdateEntryAfterUpdateResource( |
| 240 const ClientContext& context, | 280 const ClientContext& context, |
| 241 const FileOperationCallback& callback, | 281 const FileOperationCallback& callback, |
| 242 const std::string& local_id, | 282 const std::string& local_id, |
| 283 scoped_ptr<base::ScopedClosureRunner> change_list_loader_lock, |
| 243 google_apis::GDataErrorCode status, | 284 google_apis::GDataErrorCode status, |
| 244 scoped_ptr<google_apis::ResourceEntry> resource_entry) { | 285 scoped_ptr<google_apis::ResourceEntry> resource_entry) { |
| 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 246 | 287 |
| 247 if (status == google_apis::HTTP_FORBIDDEN) { | 288 if (status == google_apis::HTTP_FORBIDDEN) { |
| 248 // Editing this entry is not allowed, revert local changes. | 289 // Editing this entry is not allowed, revert local changes. |
| 249 entry_revert_performer_->RevertEntry(local_id, context, callback); | 290 entry_revert_performer_->RevertEntry(local_id, context, callback); |
| 250 return; | 291 return; |
| 251 } | 292 } |
| 252 | 293 |
| 253 FileError error = GDataToFileError(status); | 294 FileError error = GDataToFileError(status); |
| 254 if (error != FILE_ERROR_OK) { | 295 if (error != FILE_ERROR_OK) { |
| 255 callback.Run(error); | 296 callback.Run(error); |
| 256 return; | 297 return; |
| 257 } | 298 } |
| 258 | 299 |
| 259 base::PostTaskAndReplyWithResult( | 300 base::PostTaskAndReplyWithResult( |
| 260 blocking_task_runner_.get(), | 301 blocking_task_runner_.get(), |
| 261 FROM_HERE, | 302 FROM_HERE, |
| 262 base::Bind(&FinishUpdate, | 303 base::Bind(&FinishUpdate, |
| 263 metadata_, cache_, local_id, base::Passed(&resource_entry)), | 304 metadata_, cache_, local_id, base::Passed(&resource_entry)), |
| 264 callback); | 305 callback); |
| 265 } | 306 } |
| 266 | 307 |
| 267 } // namespace internal | 308 } // namespace internal |
| 268 } // namespace drive | 309 } // namespace drive |
| OLD | NEW |