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