Index: chrome/browser/chromeos/gdata/gdata_file_system.cc |
=================================================================== |
--- chrome/browser/chromeos/gdata/gdata_file_system.cc (revision 151008) |
+++ chrome/browser/chromeos/gdata/gdata_file_system.cc (working copy) |
@@ -502,6 +502,33 @@ |
const FileOperationCallback callback; |
}; |
+// GDataFileSystem::AddUploadedFileParams implementation. |
+struct GDataFileSystem::AddUploadedFileParams { |
+ AddUploadedFileParams(UploadMode upload_mode, |
+ GDataDirectory* parent_dir, |
+ scoped_ptr<GDataEntry> new_entry, |
+ const FilePath& file_content_path, |
+ GDataCache::FileOperationType cache_operation, |
+ const base::Closure& callback) |
+ : upload_mode(upload_mode), |
+ parent_dir(parent_dir), |
+ new_entry(new_entry.Pass()), |
+ file_content_path(file_content_path), |
+ cache_operation(cache_operation), |
+ callback(callback) { |
+ } |
+ |
+ UploadMode upload_mode; |
+ GDataDirectory* parent_dir; |
+ scoped_ptr<GDataEntry> new_entry; |
+ FilePath file_content_path; |
+ GDataCache::FileOperationType cache_operation; |
+ base::Closure callback; |
+ std::string resource_id; |
+ std::string md5; |
+}; |
+ |
+ |
// GDataFileSystem class implementation. |
GDataFileSystem::GDataFileSystem( |
@@ -1210,13 +1237,13 @@ |
// 3. Adds the file to the parent directory of |dest_file_path|, which |
// effectively moves the file from the root directory to the parent |
// directory of |dest_file_path|. |
- FileMoveCallback add_file_to_directory_callback = |
+ const FileMoveCallback add_file_to_directory_callback = |
base::Bind(&GDataFileSystem::MoveEntryFromRootDirectory, |
ui_weak_ptr_, |
dest_file_path.DirName(), |
callback); |
- FileMoveCallback remove_file_from_directory_callback = |
+ const FileMoveCallback remove_file_from_directory_callback = |
base::Bind(&GDataFileSystem::RemoveEntryFromDirectory, |
ui_weak_ptr_, |
src_file_path.DirName(), |
@@ -1987,47 +2014,13 @@ |
return; |
} |
- directory_service_->GetEntryByResourceIdAsync(params->directory_resource_id, |
- base::Bind(&GDataFileSystem::RequestDirectoryRefreshByEntry, |
- ui_weak_ptr_, |
- directory_path, |
- params->directory_resource_id, |
- file_map)); |
+ directory_service_->RefreshDirectory( |
+ params->directory_resource_id, |
+ file_map, |
+ base::Bind(&GDataFileSystem::OnDirectoryChangeFileMoveCallback, |
+ ui_weak_ptr_)); |
} |
-void GDataFileSystem::RequestDirectoryRefreshByEntry( |
- const FilePath& directory_path, |
- const std::string& directory_resource_id, |
- const FileResourceIdMap& file_map, |
- GDataEntry* directory_entry) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- |
- if (!directory_entry || !directory_entry->AsGDataDirectory()) { |
- LOG(ERROR) << "Directory entry is gone: " << directory_path.value() |
- << ": " << directory_resource_id; |
- return; |
- } |
- GDataDirectory* directory = directory_entry->AsGDataDirectory(); |
- |
- // Remove the existing files. |
- directory->RemoveChildFiles(); |
- // Go through all entries generated by the feed and add files. |
- for (FileResourceIdMap::const_iterator it = file_map.begin(); |
- it != file_map.end(); ++it) { |
- scoped_ptr<GDataEntry> entry(it->second); |
- // Skip if it's not a file (i.e. directory). |
- if (!entry->AsGDataFile()) |
- continue; |
- directory->AddEntry(entry.release()); |
- } |
- |
- // Note that there may be no change in the directory, but it's expensive to |
- // check if the new metadata matches the existing one, so we just always |
- // notify that the directory is changed. |
- OnDirectoryChanged(directory_path); |
- DVLOG(1) << "Directory refreshed: " << directory_path.value(); |
-} |
- |
void GDataFileSystem::UpdateFileByResourceId( |
const std::string& resource_id, |
const FileOperationCallback& callback) { |
@@ -2486,11 +2479,13 @@ |
// |entry| was added in the root directory on the server, so we should |
// first add it to |root_| to mirror the state and then move it to the |
// destination directory by MoveEntryFromRootDirectory(). |
- directory_service_->root()->AddEntry(entry); |
- MoveEntryFromRootDirectory(dir_path, |
- callback, |
- GDATA_FILE_OK, |
- entry->GetFilePath()); |
+ directory_service_->AddEntryToDirectory( |
+ directory_service_->root(), |
+ entry, |
+ base::Bind(&GDataFileSystem::MoveEntryFromRootDirectory, |
+ ui_weak_ptr_, |
+ dir_path, |
+ callback)); |
} |
void GDataFileSystem::OnMoveEntryFromRootDirectoryCompleted( |
@@ -2507,7 +2502,9 @@ |
GDataEntry* entry = directory_service_->FindEntryByPathSync(file_path); |
if (entry) { |
DCHECK_EQ(directory_service_->root(), entry->parent()); |
- directory_service_->MoveEntryToDirectory(dir_path, entry, |
+ directory_service_->MoveEntryToDirectory( |
+ dir_path, |
+ entry, |
base::Bind( |
&GDataFileSystem::OnMoveEntryToDirectoryWithFileOperationCallback, |
ui_weak_ptr_, |
@@ -2736,6 +2733,13 @@ |
callback.Run(error); |
} |
+void GDataFileSystem::OnDirectoryChangeFileMoveCallback( |
+ GDataFileError error, |
+ const FilePath& directory_path) { |
+ if (error == GDATA_FILE_OK) |
+ OnDirectoryChanged(directory_path); |
+} |
+ |
GDataFileError GDataFileSystem::RemoveEntryFromFileSystem( |
const FilePath& file_path) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
@@ -2752,15 +2756,17 @@ |
return GDATA_FILE_OK; |
} |
-// static |
-void GDataFileSystem::RemoveStaleEntryOnUpload(const std::string& resource_id, |
- GDataDirectory* parent_dir, |
- GDataEntry* existing_entry) { |
+void GDataFileSystem::RemoveStaleEntryOnUpload( |
+ const std::string& resource_id, |
+ GDataDirectory* parent_dir, |
+ const FileMoveCallback& callback, |
+ GDataEntry* existing_entry) { |
if (existing_entry && |
// This should always match, but just in case. |
existing_entry->parent() == parent_dir) { |
- parent_dir->RemoveEntry(existing_entry); |
+ directory_service_->RemoveEntryFromParent(existing_entry, callback); |
} else { |
+ callback.Run(GDATA_FILE_ERROR_NOT_FOUND, FilePath()); |
LOG(ERROR) << "Entry for the existing file not found: " << resource_id; |
} |
} |
@@ -2828,9 +2834,12 @@ |
if (!new_entry) |
return GDATA_FILE_ERROR_FAILED; |
- parent_dir->AddEntry(new_entry); |
- |
- OnDirectoryChanged(directory_path); |
+ directory_service_->AddEntryToDirectory( |
+ parent_dir, |
+ new_entry, |
+ base::Bind(&GDataFileSystem::OnMoveEntryToDirectoryWithFileMoveCallback, |
+ ui_weak_ptr_, |
+ FileMoveCallback())); |
return GDATA_FILE_OK; |
} |
@@ -2888,10 +2897,10 @@ |
if (entry->AsGDataFile()) |
*resource_id = entry->AsGDataFile()->resource_id(); |
- GDataDirectory* parent_dir = entry->parent(); |
- parent_dir->RemoveEntry(entry); |
- |
- OnDirectoryChanged(parent_dir->GetFilePath()); |
+ directory_service_->RemoveEntryFromParent( |
+ entry, |
+ base::Bind(&GDataFileSystem::OnDirectoryChangeFileMoveCallback, |
+ ui_weak_ptr_)); |
return GDATA_FILE_OK; |
} |
@@ -2950,38 +2959,75 @@ |
if (!new_entry.get()) |
return; |
+ const std::string& resource_id = new_entry->resource_id(); |
+ AddUploadedFileParams* params = new AddUploadedFileParams( |
+ upload_mode, parent_dir, new_entry.Pass(), file_content_path, |
+ cache_operation, callback_runner.Release()); |
satorux1
2012/08/14 06:11:32
nit: could you list parameters vertically?
achuithb
2012/08/14 23:33:59
Done.
|
+ |
+ const FileMoveCallback file_move_callback = |
+ base::Bind(&GDataFileSystem::ContinueAddUploadedFile, |
+ ui_weak_ptr_, params); |
+ |
if (upload_mode == UPLOAD_EXISTING_FILE) { |
// Remove an existing entry, which should be present. |
- const std::string& resource_id = new_entry->resource_id(); |
- directory_service_->GetEntryByResourceIdAsync(resource_id, |
- base::Bind(&RemoveStaleEntryOnUpload, resource_id, parent_dir)); |
+ directory_service_->GetEntryByResourceIdAsync( |
+ resource_id, |
+ base::Bind(&GDataFileSystem::RemoveStaleEntryOnUpload, |
+ ui_weak_ptr_, |
+ resource_id, |
+ parent_dir, |
+ file_move_callback)); |
+ } else { |
+ file_move_callback.Run(GDATA_FILE_OK, FilePath()); |
} |
+} |
- GDataFile* file = new_entry->AsGDataFile(); |
+void GDataFileSystem::ContinueAddUploadedFile( |
+ AddUploadedFileParams* params, |
+ GDataFileError error, |
+ const FilePath& file_path) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK_EQ(GDATA_FILE_OK, error); |
+ DCHECK(params->new_entry.get()); |
+ GDataFile* file = params->new_entry->AsGDataFile(); |
DCHECK(file); |
- const std::string& resource_id = file->resource_id(); |
- const std::string& md5 = file->file_md5(); |
- parent_dir->AddEntry(new_entry.release()); |
- OnDirectoryChanged(virtual_dir_path); |
+ params->resource_id = file->resource_id(); |
+ params->md5 = file->file_md5(); |
+ directory_service_->AddEntryToDirectory( |
+ params->parent_dir, |
+ params->new_entry.release(), |
+ base::Bind(&GDataFileSystem::OnMoveEntryToDirectoryWithFileMoveCallback, |
+ ui_weak_ptr_, |
+ base::Bind(&GDataFileSystem::AddUploadedFileToCache, |
+ ui_weak_ptr_, |
+ base::Owned(params)))); |
+} |
- if (upload_mode == UPLOAD_NEW_FILE) { |
+void GDataFileSystem::AddUploadedFileToCache( |
+ AddUploadedFileParams* params, |
+ GDataFileError error, |
+ const FilePath& file_path) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ if (params->upload_mode == UPLOAD_NEW_FILE) { |
// Add the file to the cache if we have uploaded a new file. |
- cache_->StoreOnUIThread(resource_id, |
- md5, |
- file_content_path, |
- cache_operation, |
+ cache_->StoreOnUIThread(params->resource_id, |
+ params->md5, |
+ params->file_content_path, |
+ params->cache_operation, |
base::Bind(&OnCacheUpdatedForAddUploadedFile, |
- callback_runner.Release())); |
- } else if (upload_mode == UPLOAD_EXISTING_FILE) { |
+ params->callback)); |
+ } else if (params->upload_mode == UPLOAD_EXISTING_FILE) { |
// Clear the dirty bit if we have updated an existing file. |
- cache_->ClearDirtyOnUIThread(resource_id, |
- md5, |
+ cache_->ClearDirtyOnUIThread(params->resource_id, |
+ params->md5, |
base::Bind(&OnCacheUpdatedForAddUploadedFile, |
- callback_runner.Release())); |
+ params->callback)); |
} else { |
- NOTREACHED() << "Unexpected upload mode: " << upload_mode; |
+ NOTREACHED() << "Unexpected upload mode: " << params->upload_mode; |
} |
+ params->callback.Run(); |
} |
void GDataFileSystem::Observe(int type, |