OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/gdata/gdata_file_system.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 // Ditto for FileOperationCallback | 227 // Ditto for FileOperationCallback |
228 void RunFileOperationCallbackHelper( | 228 void RunFileOperationCallbackHelper( |
229 const FileOperationCallback& callback, | 229 const FileOperationCallback& callback, |
230 base::PlatformFileError* error) { | 230 base::PlatformFileError* error) { |
231 DCHECK(error); | 231 DCHECK(error); |
232 | 232 |
233 if (!callback.is_null()) | 233 if (!callback.is_null()) |
234 callback.Run(*error); | 234 callback.Run(*error); |
235 } | 235 } |
236 | 236 |
| 237 // Callback for StoreToCache invoked by AddUploadedFileOnUIThread. |
| 238 void OnStoreToCacheForAddUploadedFile( |
| 239 const base::Closure& callback, |
| 240 base::PlatformFileError /* error */, |
| 241 const std::string& /* resource_id */, |
| 242 const std::string& /* md5 */) { |
| 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 244 if (!callback.is_null()) |
| 245 callback.Run(); |
| 246 } |
| 247 |
| 248 // Helper function called upon completion of AddUploadFile invoked by |
| 249 // OnTransferCompleted. |
| 250 void OnAddUploadFileCompleted( |
| 251 const FileOperationCallback& callback, |
| 252 base::PlatformFileError error, |
| 253 scoped_ptr<UploadFileInfo> /* upload_file_info */){ |
| 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 255 if (!callback.is_null()) |
| 256 callback.Run(error); |
| 257 } |
| 258 |
237 // Used to implement GetCacheState. | 259 // Used to implement GetCacheState. |
238 void RunGetCacheStateCallbackHelper( | 260 void RunGetCacheStateCallbackHelper( |
239 const GetCacheStateCallback& callback, | 261 const GetCacheStateCallback& callback, |
240 GDataCache::CacheEntry* cache_entry, | 262 GDataCache::CacheEntry* cache_entry, |
241 bool* success) { | 263 bool* success) { |
242 DCHECK(cache_entry); | 264 DCHECK(cache_entry); |
243 DCHECK(success); | 265 DCHECK(success); |
244 if (callback.is_null()) | 266 if (callback.is_null()) |
245 return; | 267 return; |
246 | 268 |
(...skipping 897 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1144 base::Bind(&GDataFileSystem::OnTransferCompleted, | 1166 base::Bind(&GDataFileSystem::OnTransferCompleted, |
1145 ui_weak_ptr_, | 1167 ui_weak_ptr_, |
1146 callback); | 1168 callback); |
1147 | 1169 |
1148 service->uploader()->UploadFile(scoped_ptr<UploadFileInfo>(upload_file_info)); | 1170 service->uploader()->UploadFile(scoped_ptr<UploadFileInfo>(upload_file_info)); |
1149 } | 1171 } |
1150 | 1172 |
1151 void GDataFileSystem::OnTransferCompleted( | 1173 void GDataFileSystem::OnTransferCompleted( |
1152 const FileOperationCallback& callback, | 1174 const FileOperationCallback& callback, |
1153 base::PlatformFileError error, | 1175 base::PlatformFileError error, |
1154 UploadFileInfo* upload_file_info) { | 1176 scoped_ptr<UploadFileInfo> upload_file_info) { |
1155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1156 DCHECK(upload_file_info); | 1178 DCHECK(upload_file_info.get()); |
1157 | 1179 |
1158 if (error == base::PLATFORM_FILE_OK && upload_file_info->entry.get()) { | 1180 if (error == base::PLATFORM_FILE_OK && upload_file_info->entry.get()) { |
1159 AddUploadedFile(upload_file_info->gdata_path.DirName(), | 1181 // Save a local copy of the UploadFileInfo pointer. Depending on order of |
1160 upload_file_info->entry.get(), | 1182 // argument evaluation, base::Passed() may invalidate the scoped pointer |
1161 upload_file_info->file_path, | 1183 // |upload_file_info| before it can be dereferenced to access its members. |
1162 GDataCache::FILE_OPERATION_COPY); | 1184 const UploadFileInfo* upload_file_info_ptr = upload_file_info.get(); |
| 1185 AddUploadedFile(upload_file_info_ptr->gdata_path.DirName(), |
| 1186 upload_file_info_ptr->entry.get(), |
| 1187 upload_file_info_ptr->file_path, |
| 1188 GDataCache::FILE_OPERATION_COPY, |
| 1189 base::Bind(&OnAddUploadFileCompleted, |
| 1190 callback, |
| 1191 error, |
| 1192 base::Passed(&upload_file_info))); |
| 1193 } else if (!callback.is_null()) { |
| 1194 callback.Run(error); |
1163 } | 1195 } |
1164 if (!callback.is_null()) | |
1165 callback.Run(error); | |
1166 | |
1167 // In case of error upload_file_info will be deleted by the uploader. | |
1168 if (error != base::PLATFORM_FILE_OK) | |
1169 return; | |
1170 | |
1171 // TODO(achuith): GDataFileSystem should not have to call DeleteUpload. | |
1172 GDataSystemService* service = | |
1173 GDataSystemServiceFactory::GetForProfile(profile_); | |
1174 if (service) | |
1175 service->uploader()->DeleteUpload(upload_file_info); | |
1176 } | 1196 } |
1177 | 1197 |
1178 void GDataFileSystem::Copy(const FilePath& src_file_path, | 1198 void GDataFileSystem::Copy(const FilePath& src_file_path, |
1179 const FilePath& dest_file_path, | 1199 const FilePath& dest_file_path, |
1180 const FileOperationCallback& callback) { | 1200 const FileOperationCallback& callback) { |
1181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 1201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
1182 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 1202 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
1183 RunTaskOnUIThread(base::Bind(&GDataFileSystem::CopyOnUIThread, | 1203 RunTaskOnUIThread(base::Bind(&GDataFileSystem::CopyOnUIThread, |
1184 ui_weak_ptr_, | 1204 ui_weak_ptr_, |
1185 src_file_path, | 1205 src_file_path, |
(...skipping 2290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3476 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 3496 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
3477 | 3497 |
3478 NotifyDirectoryChanged(parent_dir->GetFilePath()); | 3498 NotifyDirectoryChanged(parent_dir->GetFilePath()); |
3479 return base::PLATFORM_FILE_OK; | 3499 return base::PLATFORM_FILE_OK; |
3480 } | 3500 } |
3481 | 3501 |
3482 void GDataFileSystem::AddUploadedFile( | 3502 void GDataFileSystem::AddUploadedFile( |
3483 const FilePath& virtual_dir_path, | 3503 const FilePath& virtual_dir_path, |
3484 DocumentEntry* entry, | 3504 DocumentEntry* entry, |
3485 const FilePath& file_content_path, | 3505 const FilePath& file_content_path, |
3486 GDataCache::FileOperationType cache_operation) { | 3506 GDataCache::FileOperationType cache_operation, |
| 3507 const base::Closure& callback) { |
3487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 3508 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
3488 | 3509 |
| 3510 // Post a task to the same thread, rather than calling it here, as |
| 3511 // AddUploadedFile() is asynchronous. |
| 3512 base::MessageLoopProxy::current()->PostTask( |
| 3513 FROM_HERE, |
| 3514 base::Bind(&GDataFileSystem::AddUploadedFileOnUIThread, |
| 3515 ui_weak_ptr_, |
| 3516 virtual_dir_path, |
| 3517 entry, |
| 3518 file_content_path, |
| 3519 cache_operation, |
| 3520 callback)); |
| 3521 } |
| 3522 |
| 3523 void GDataFileSystem::AddUploadedFileOnUIThread( |
| 3524 const FilePath& virtual_dir_path, |
| 3525 DocumentEntry* entry, |
| 3526 const FilePath& file_content_path, |
| 3527 GDataCache::FileOperationType cache_operation, |
| 3528 const base::Closure& callback) { |
| 3529 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 3530 DCHECK(!callback.is_null()); |
| 3531 |
3489 if (!entry) { | 3532 if (!entry) { |
3490 NOTREACHED(); | 3533 NOTREACHED(); |
| 3534 callback.Run(); |
3491 return; | 3535 return; |
3492 } | 3536 } |
3493 | 3537 |
3494 GDataEntry* dir_entry = GetGDataEntryByPath(virtual_dir_path); | 3538 GDataEntry* dir_entry = GetGDataEntryByPath(virtual_dir_path); |
3495 if (!dir_entry) | 3539 if (!dir_entry) { |
| 3540 callback.Run(); |
3496 return; | 3541 return; |
| 3542 } |
3497 | 3543 |
3498 GDataDirectory* parent_dir = dir_entry->AsGDataDirectory(); | 3544 GDataDirectory* parent_dir = dir_entry->AsGDataDirectory(); |
3499 if (!parent_dir) | 3545 if (!parent_dir) { |
| 3546 callback.Run(); |
3500 return; | 3547 return; |
| 3548 } |
3501 | 3549 |
3502 scoped_ptr<GDataEntry> new_entry( | 3550 scoped_ptr<GDataEntry> new_entry( |
3503 GDataEntry::FromDocumentEntry(parent_dir, entry, root_.get())); | 3551 GDataEntry::FromDocumentEntry(parent_dir, entry, root_.get())); |
3504 if (!new_entry.get()) | 3552 if (!new_entry.get()) { |
| 3553 callback.Run(); |
3505 return; | 3554 return; |
| 3555 } |
3506 | 3556 |
3507 GDataFile* file = new_entry->AsGDataFile(); | 3557 GDataFile* file = new_entry->AsGDataFile(); |
3508 DCHECK(file); | 3558 DCHECK(file); |
3509 const std::string& resource_id = file->resource_id(); | 3559 const std::string& resource_id = file->resource_id(); |
3510 const std::string& md5 = file->file_md5(); | 3560 const std::string& md5 = file->file_md5(); |
3511 parent_dir->AddEntry(new_entry.release()); | 3561 parent_dir->AddEntry(new_entry.release()); |
3512 | 3562 |
3513 NotifyDirectoryChanged(virtual_dir_path); | 3563 NotifyDirectoryChanged(virtual_dir_path); |
3514 | 3564 |
3515 cache_->StoreOnUIThread(resource_id, md5, file_content_path, cache_operation, | 3565 cache_->StoreOnUIThread(resource_id, md5, file_content_path, cache_operation, |
3516 CacheOperationCallback()); | 3566 base::Bind(&OnStoreToCacheForAddUploadedFile, |
| 3567 callback)); |
3517 } | 3568 } |
3518 | 3569 |
3519 void GDataFileSystem::Observe(int type, | 3570 void GDataFileSystem::Observe(int type, |
3520 const content::NotificationSource& source, | 3571 const content::NotificationSource& source, |
3521 const content::NotificationDetails& details) { | 3572 const content::NotificationDetails& details) { |
3522 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 3573 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
3523 | 3574 |
3524 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | 3575 if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
3525 PrefService* pref_service = profile_->GetPrefs(); | 3576 PrefService* pref_service = profile_->GetPrefs(); |
3526 std::string* pref_name = content::Details<std::string>(details).ptr(); | 3577 std::string* pref_name = content::Details<std::string>(details).ptr(); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3777 base::PlatformFileError error, | 3828 base::PlatformFileError error, |
3778 const std::string& resource_id, | 3829 const std::string& resource_id, |
3779 const std::string& md5) { | 3830 const std::string& md5) { |
3780 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 3831 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
3781 | 3832 |
3782 if (!callback.is_null()) | 3833 if (!callback.is_null()) |
3783 callback.Run(error); | 3834 callback.Run(error); |
3784 } | 3835 } |
3785 | 3836 |
3786 } // namespace gdata | 3837 } // namespace gdata |
OLD | NEW |