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 1193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1204 // Otherwise, the move operation involves three steps: | 1204 // Otherwise, the move operation involves three steps: |
1205 // 1. Renames the file at |src_file_path| to basename(|dest_file_path|) | 1205 // 1. Renames the file at |src_file_path| to basename(|dest_file_path|) |
1206 // within the same directory. The rename operation is a no-op if | 1206 // within the same directory. The rename operation is a no-op if |
1207 // basename(|src_file_path|) equals to basename(|dest_file_path|). | 1207 // basename(|src_file_path|) equals to basename(|dest_file_path|). |
1208 // 2. Removes the file from its parent directory (the file is not deleted), | 1208 // 2. Removes the file from its parent directory (the file is not deleted), |
1209 // which effectively moves the file to the root directory. | 1209 // which effectively moves the file to the root directory. |
1210 // 3. Adds the file to the parent directory of |dest_file_path|, which | 1210 // 3. Adds the file to the parent directory of |dest_file_path|, which |
1211 // effectively moves the file from the root directory to the parent | 1211 // effectively moves the file from the root directory to the parent |
1212 // directory of |dest_file_path|. | 1212 // directory of |dest_file_path|. |
1213 FileMoveCallback add_file_to_directory_callback = | 1213 FileMoveCallback add_file_to_directory_callback = |
1214 base::Bind(&GDataFileSystem::AddEntryToDirectory, | 1214 base::Bind(&GDataFileSystem::MoveEntryFromRootDirectory, |
1215 ui_weak_ptr_, | 1215 ui_weak_ptr_, |
1216 dest_file_path.DirName(), | 1216 dest_file_path.DirName(), |
1217 callback); | 1217 callback); |
1218 | 1218 |
1219 FileMoveCallback remove_file_from_directory_callback = | 1219 FileMoveCallback remove_file_from_directory_callback = |
1220 base::Bind(&GDataFileSystem::RemoveEntryFromDirectory, | 1220 base::Bind(&GDataFileSystem::RemoveEntryFromDirectory, |
1221 ui_weak_ptr_, | 1221 ui_weak_ptr_, |
1222 src_file_path.DirName(), | 1222 src_file_path.DirName(), |
1223 add_file_to_directory_callback); | 1223 add_file_to_directory_callback); |
1224 | 1224 |
1225 Rename(src_file_path, dest_file_path.BaseName().value(), | 1225 Rename(src_file_path, dest_file_path.BaseName().value(), |
1226 remove_file_from_directory_callback); | 1226 remove_file_from_directory_callback); |
1227 } | 1227 } |
1228 | 1228 |
1229 void GDataFileSystem::AddEntryToDirectory( | 1229 void GDataFileSystem::MoveEntryFromRootDirectory( |
1230 const FilePath& dir_path, | 1230 const FilePath& dir_path, |
1231 const FileOperationCallback& callback, | 1231 const FileOperationCallback& callback, |
1232 GDataFileError error, | 1232 GDataFileError error, |
1233 const FilePath& file_path) { | 1233 const FilePath& file_path) { |
1234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1235 DCHECK(!callback.is_null()); |
| 1236 DCHECK_EQ(kGDataRootDirectory, file_path.DirName().value()); |
1235 | 1237 |
1236 GDataEntry* entry = directory_service_->FindEntryByPathSync(file_path); | 1238 GDataEntry* entry = directory_service_->FindEntryByPathSync(file_path); |
1237 GDataEntry* dir_entry = directory_service_->FindEntryByPathSync(dir_path); | 1239 GDataEntry* dir_entry = directory_service_->FindEntryByPathSync(dir_path); |
1238 if (error == GDATA_FILE_OK) { | 1240 if (error == GDATA_FILE_OK) { |
1239 if (!entry || !dir_entry) { | 1241 if (!entry || !dir_entry) { |
1240 error = GDATA_FILE_ERROR_NOT_FOUND; | 1242 error = GDATA_FILE_ERROR_NOT_FOUND; |
1241 } else { | 1243 } else { |
1242 if (!dir_entry->AsGDataDirectory()) | 1244 if (!dir_entry->AsGDataDirectory()) |
1243 error = GDATA_FILE_ERROR_NOT_A_DIRECTORY; | 1245 error = GDATA_FILE_ERROR_NOT_A_DIRECTORY; |
1244 } | 1246 } |
1245 } | 1247 } |
1246 | 1248 |
1247 // Returns if there is an error or |dir_path| is the root directory. | 1249 // Returns if there is an error or |dir_path| is the root directory. |
1248 if (error != GDATA_FILE_OK || | 1250 if (error != GDATA_FILE_OK || |
1249 dir_entry->resource_id() == kGDataRootDirectoryResourceId) { | 1251 dir_entry->resource_id() == kGDataRootDirectoryResourceId) { |
1250 if (!callback.is_null()) | 1252 callback.Run(error); |
1251 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, error)); | |
1252 | |
1253 return; | 1253 return; |
1254 } | 1254 } |
1255 | 1255 |
1256 documents_service_->AddResourceToDirectory( | 1256 documents_service_->AddResourceToDirectory( |
1257 dir_entry->content_url(), | 1257 dir_entry->content_url(), |
1258 entry->edit_url(), | 1258 entry->edit_url(), |
1259 base::Bind(&GDataFileSystem::OnAddEntryToDirectoryCompleted, | 1259 base::Bind(&GDataFileSystem::OnMoveEntryFromRootDirectoryCompleted, |
1260 ui_weak_ptr_, | 1260 ui_weak_ptr_, |
1261 callback, | 1261 callback, |
1262 file_path, | 1262 file_path, |
1263 dir_path)); | 1263 dir_path)); |
1264 } | 1264 } |
1265 | 1265 |
1266 void GDataFileSystem::RemoveEntryFromDirectory( | 1266 void GDataFileSystem::RemoveEntryFromDirectory( |
1267 const FilePath& dir_path, | 1267 const FilePath& dir_path, |
1268 const FileMoveCallback& callback, | 1268 const FileMoveCallback& callback, |
1269 GDataFileError error, | 1269 GDataFileError error, |
(...skipping 1212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2482 | 2482 |
2483 GDataEntry* entry = GDataEntry::FromDocumentEntry( | 2483 GDataEntry* entry = GDataEntry::FromDocumentEntry( |
2484 NULL, doc_entry.get(), directory_service_.get()); | 2484 NULL, doc_entry.get(), directory_service_.get()); |
2485 if (!entry) { | 2485 if (!entry) { |
2486 callback.Run(GDATA_FILE_ERROR_FAILED); | 2486 callback.Run(GDATA_FILE_ERROR_FAILED); |
2487 return; | 2487 return; |
2488 } | 2488 } |
2489 | 2489 |
2490 // |entry| was added in the root directory on the server, so we should | 2490 // |entry| was added in the root directory on the server, so we should |
2491 // first add it to |root_| to mirror the state and then move it to the | 2491 // first add it to |root_| to mirror the state and then move it to the |
2492 // destination directory by AddEntryToDirectory(). | 2492 // destination directory by MoveEntryFromRootDirectory(). |
2493 directory_service_->root()->AddEntry(entry); | 2493 directory_service_->root()->AddEntry(entry); |
2494 AddEntryToDirectory(dir_path, callback, GDATA_FILE_OK, entry->GetFilePath()); | 2494 MoveEntryFromRootDirectory(dir_path, |
| 2495 callback, |
| 2496 GDATA_FILE_OK, |
| 2497 entry->GetFilePath()); |
2495 } | 2498 } |
2496 | 2499 |
2497 void GDataFileSystem::OnAddEntryToDirectoryCompleted( | 2500 void GDataFileSystem::OnMoveEntryFromRootDirectoryCompleted( |
2498 const FileOperationCallback& callback, | 2501 const FileOperationCallback& callback, |
2499 const FilePath& file_path, | 2502 const FilePath& file_path, |
2500 const FilePath& dir_path, | 2503 const FilePath& dir_path, |
2501 GDataErrorCode status, | 2504 GDataErrorCode status, |
2502 const GURL& document_url) { | 2505 const GURL& document_url) { |
2503 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 2506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 2507 DCHECK(!callback.is_null()); |
2504 | 2508 |
2505 GDataFileError error = util::GDataToGDataFileError(status); | 2509 GDataFileError error = util::GDataToGDataFileError(status); |
2506 if (error == GDATA_FILE_OK) { | 2510 if (error == GDATA_FILE_OK) { |
2507 GDataEntry* entry = directory_service_->FindEntryByPathSync(file_path); | 2511 GDataEntry* entry = directory_service_->FindEntryByPathSync(file_path); |
2508 if (entry) { | 2512 if (entry) { |
2509 DCHECK_EQ(directory_service_->root(), entry->parent()); | 2513 DCHECK_EQ(directory_service_->root(), entry->parent()); |
2510 directory_service_->MoveEntryToDirectory(dir_path, entry, | 2514 directory_service_->MoveEntryToDirectory(dir_path, entry, |
2511 base::Bind( | 2515 base::Bind( |
2512 &GDataFileSystem::OnMoveEntryToDirectoryWithFileOperationCallback, | 2516 &GDataFileSystem::OnMoveEntryToDirectoryWithFileOperationCallback, |
2513 ui_weak_ptr_, | 2517 ui_weak_ptr_, |
2514 callback)); | 2518 callback)); |
2515 return; | 2519 return; |
2516 } else { | 2520 } else { |
2517 error = GDATA_FILE_ERROR_NOT_FOUND; | 2521 error = GDATA_FILE_ERROR_NOT_FOUND; |
2518 } | 2522 } |
2519 } | 2523 } |
2520 | 2524 |
2521 if (!callback.is_null()) | 2525 callback.Run(error); |
2522 callback.Run(error); | |
2523 } | 2526 } |
2524 | 2527 |
2525 void GDataFileSystem::OnRemovedDocument( | 2528 void GDataFileSystem::OnRemovedDocument( |
2526 const FileOperationCallback& callback, | 2529 const FileOperationCallback& callback, |
2527 const FilePath& file_path, | 2530 const FilePath& file_path, |
2528 GDataErrorCode status, | 2531 GDataErrorCode status, |
2529 const GURL& document_url) { | 2532 const GURL& document_url) { |
2530 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 2533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
2531 | 2534 |
2532 GDataFileError error = util::GDataToGDataFileError(status); | 2535 GDataFileError error = util::GDataToGDataFileError(status); |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2722 OnDirectoryChanged(moved_file_path.DirName()); | 2725 OnDirectoryChanged(moved_file_path.DirName()); |
2723 | 2726 |
2724 if (!callback.is_null()) | 2727 if (!callback.is_null()) |
2725 callback.Run(error, moved_file_path); | 2728 callback.Run(error, moved_file_path); |
2726 } | 2729 } |
2727 | 2730 |
2728 void GDataFileSystem::OnMoveEntryToDirectoryWithFileOperationCallback( | 2731 void GDataFileSystem::OnMoveEntryToDirectoryWithFileOperationCallback( |
2729 const FileOperationCallback& callback, | 2732 const FileOperationCallback& callback, |
2730 GDataFileError error, | 2733 GDataFileError error, |
2731 const FilePath& moved_file_path) { | 2734 const FilePath& moved_file_path) { |
| 2735 DCHECK(!callback.is_null()); |
| 2736 |
2732 if (error == GDATA_FILE_OK) | 2737 if (error == GDATA_FILE_OK) |
2733 OnDirectoryChanged(moved_file_path.DirName()); | 2738 OnDirectoryChanged(moved_file_path.DirName()); |
2734 | 2739 |
2735 if (!callback.is_null()) | 2740 callback.Run(error); |
2736 callback.Run(error); | |
2737 } | 2741 } |
2738 | 2742 |
2739 GDataFileError GDataFileSystem::RemoveEntryFromFileSystem( | 2743 GDataFileError GDataFileSystem::RemoveEntryFromFileSystem( |
2740 const FilePath& file_path) { | 2744 const FilePath& file_path) { |
2741 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 2745 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
2742 | 2746 |
2743 std::string resource_id; | 2747 std::string resource_id; |
2744 GDataFileError error = RemoveEntryFromGData(file_path, &resource_id); | 2748 GDataFileError error = RemoveEntryFromGData(file_path, &resource_id); |
2745 if (error != GDATA_FILE_OK) | 2749 if (error != GDATA_FILE_OK) |
2746 return error; | 2750 return error; |
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3445 } | 3449 } |
3446 | 3450 |
3447 PlatformFileInfoProto entry_file_info; | 3451 PlatformFileInfoProto entry_file_info; |
3448 GDataEntry::ConvertPlatformFileInfoToProto(*file_info, &entry_file_info); | 3452 GDataEntry::ConvertPlatformFileInfoToProto(*file_info, &entry_file_info); |
3449 *entry_proto->mutable_file_info() = entry_file_info; | 3453 *entry_proto->mutable_file_info() = entry_file_info; |
3450 if (!callback.is_null()) | 3454 if (!callback.is_null()) |
3451 callback.Run(GDATA_FILE_OK, entry_proto.Pass()); | 3455 callback.Run(GDATA_FILE_OK, entry_proto.Pass()); |
3452 } | 3456 } |
3453 | 3457 |
3454 } // namespace gdata | 3458 } // namespace gdata |
OLD | NEW |