Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1132)

Unified Diff: chrome/browser/chromeos/drive/file_system/move_operation.cc

Issue 98513003: drive: Support offline move/rename (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/drive/file_system/move_operation.cc
diff --git a/chrome/browser/chromeos/drive/file_system/move_operation.cc b/chrome/browser/chromeos/drive/file_system/move_operation.cc
index b76c91e1d5c8e080b08d8a5f39ca67d46ee1cdee..f902084abc10c08218dea7b3ab1760dbd195f273 100644
--- a/chrome/browser/chromeos/drive/file_system/move_operation.cc
+++ b/chrome/browser/chromeos/drive/file_system/move_operation.cc
@@ -4,10 +4,9 @@
#include "chrome/browser/chromeos/drive/file_system/move_operation.h"
+#include "base/sequenced_task_runner.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
-#include "chrome/browser/chromeos/drive/job_scheduler.h"
-#include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
#include "chrome/browser/chromeos/drive/resource_metadata.h"
#include "content/public/browser/browser_thread.h"
@@ -18,64 +17,57 @@ namespace file_system {
namespace {
// Looks up ResourceEntry for source entry and the destination directory.
-FileError PrepareMove(internal::ResourceMetadata* metadata,
- const base::FilePath& src_path,
- const base::FilePath& dest_parent_path,
- ResourceEntry* src_entry,
- ResourceEntry* src_parent_entry,
- ResourceEntry* dest_parent_entry) {
- FileError error = metadata->GetResourceEntryByPath(src_path, src_entry);
+FileError UpdateLocalState(internal::ResourceMetadata* metadata,
+ const base::FilePath& src_path,
+ const base::FilePath& dest_path,
+ bool preserve_last_modified,
+ std::string* local_id) {
+ ResourceEntry entry;
+ FileError error = metadata->GetResourceEntryByPath(src_path, &entry);
if (error != FILE_ERROR_OK)
return error;
+ *local_id = entry.local_id();
- error = metadata->GetResourceEntryById(src_entry->parent_local_id(),
- src_parent_entry);
+ ResourceEntry parent_entry;
+ error = metadata->GetResourceEntryByPath(dest_path.DirName(), &parent_entry);
if (error != FILE_ERROR_OK)
return error;
- return metadata->GetResourceEntryByPath(dest_parent_path, dest_parent_entry);
-}
+ // The parent must be a directory.
+ if (!parent_entry.file_info().is_directory())
+ return FILE_ERROR_NOT_A_DIRECTORY;
-// Refreshes the corresponding entry in the metadata with the given one.
-FileError RefreshEntry(internal::ResourceMetadata* metadata,
- scoped_ptr<google_apis::ResourceEntry> resource_entry) {
- ResourceEntry entry;
- std::string parent_resource_id;
- if (!ConvertToResourceEntry(*resource_entry, &entry, &parent_resource_id))
- return FILE_ERROR_FAILED;
-
- std::string parent_local_id;
- FileError error = metadata->GetIdByResourceId(parent_resource_id,
- &parent_local_id);
- if (error != FILE_ERROR_OK)
- return error;
- entry.set_parent_local_id(parent_local_id);
+ // Strip the extension for a hosted document if necessary.
+ const std::string new_extension =
+ base::FilePath(dest_path.Extension()).AsUTF8Unsafe();
+ const bool has_hosted_document_extension =
+ entry.has_file_specific_info() &&
+ entry.file_specific_info().is_hosted_document() &&
+ new_extension == entry.file_specific_info().document_extension();
+ const std::string new_title =
+ has_hosted_document_extension ?
+ dest_path.BaseName().RemoveExtension().AsUTF8Unsafe() :
+ dest_path.BaseName().AsUTF8Unsafe();
- std::string local_id;
- error = metadata->GetIdByResourceId(entry.resource_id(), &local_id);
- if (error != FILE_ERROR_OK)
- return error;
- entry.set_local_id(local_id);
+ // Update last_modified.
+ if (!preserve_last_modified) {
+ entry.mutable_file_info()->set_last_modified(
+ base::Time::Now().ToInternalValue());
+ }
+ entry.set_title(new_title);
+ entry.set_parent_local_id(parent_entry.local_id());
+ entry.set_metadata_edit_state(ResourceEntry::DIRTY);
return metadata->RefreshEntry(entry);
}
} // namespace
-struct MoveOperation::MoveParams {
- base::FilePath src_file_path;
- base::FilePath dest_file_path;
- bool preserve_last_modified;
- FileOperationCallback callback;
-};
-
MoveOperation::MoveOperation(base::SequencedTaskRunner* blocking_task_runner,
OperationObserver* observer,
- JobScheduler* scheduler,
internal::ResourceMetadata* metadata)
: blocking_task_runner_(blocking_task_runner),
observer_(observer),
- scheduler_(scheduler),
metadata_(metadata),
weak_ptr_factory_(this) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
@@ -92,104 +84,38 @@ void MoveOperation::Move(const base::FilePath& src_file_path,
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
- MoveParams params;
- params.src_file_path = src_file_path;
- params.dest_file_path = dest_file_path;
- params.preserve_last_modified = preserve_last_modified;
- params.callback = callback;
-
- scoped_ptr<ResourceEntry> src_entry(new ResourceEntry);
- scoped_ptr<ResourceEntry> src_parent_entry(new ResourceEntry);
- scoped_ptr<ResourceEntry> dest_parent_entry(new ResourceEntry);
- ResourceEntry* src_entry_ptr = src_entry.get();
- ResourceEntry* src_parent_entry_ptr = src_parent_entry.get();
- ResourceEntry* dest_parent_entry_ptr = dest_parent_entry.get();
+ std::string* local_id = new std::string;
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
- base::Bind(&PrepareMove,
- metadata_, src_file_path, dest_file_path.DirName(),
- src_entry_ptr, src_parent_entry_ptr, dest_parent_entry_ptr),
- base::Bind(&MoveOperation::MoveAfterPrepare,
- weak_ptr_factory_.GetWeakPtr(), params,
- base::Passed(&src_entry),
- base::Passed(&src_parent_entry),
- base::Passed(&dest_parent_entry)));
+ base::Bind(&UpdateLocalState,
+ metadata_,
+ src_file_path,
+ dest_file_path,
+ preserve_last_modified,
+ local_id),
+ base::Bind(&MoveOperation::MoveAfterUpdateLocalState,
+ weak_ptr_factory_.GetWeakPtr(),
+ src_file_path,
+ dest_file_path,
+ callback,
+ base::Owned(local_id)));
}
-void MoveOperation::MoveAfterPrepare(
- const MoveParams& params,
- scoped_ptr<ResourceEntry> src_entry,
- scoped_ptr<ResourceEntry> src_parent_entry,
- scoped_ptr<ResourceEntry> dest_parent_entry,
+void MoveOperation::MoveAfterUpdateLocalState(
+ const base::FilePath& src_file_path,
+ const base::FilePath& dest_file_path,
+ const FileOperationCallback& callback,
+ const std::string* local_id,
FileError error) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- DCHECK(!params.callback.is_null());
-
- if (error != FILE_ERROR_OK) {
- params.callback.Run(error);
- return;
- }
-
- if (!dest_parent_entry->file_info().is_directory()) {
- // The parent of the destination is not a directory.
- params.callback.Run(FILE_ERROR_NOT_A_DIRECTORY);
- return;
- }
-
- // Strip the extension for a hosted document if necessary.
- const bool has_hosted_document_extension =
- src_entry->has_file_specific_info() &&
- src_entry->file_specific_info().is_hosted_document() &&
- params.dest_file_path.Extension() ==
- src_entry->file_specific_info().document_extension();
- const std::string new_title =
- has_hosted_document_extension ?
- params.dest_file_path.BaseName().RemoveExtension().AsUTF8Unsafe() :
- params.dest_file_path.BaseName().AsUTF8Unsafe();
-
- base::Time last_modified =
- params.preserve_last_modified ?
- base::Time::FromInternalValue(src_entry->file_info().last_modified()) :
- base::Time();
-
- scheduler_->UpdateResource(
- src_entry->resource_id(), dest_parent_entry->resource_id(),
- new_title, last_modified, base::Time(),
- base::Bind(&MoveOperation::MoveAfterUpdateResource,
- weak_ptr_factory_.GetWeakPtr(), params));
-}
-
-void MoveOperation::MoveAfterUpdateResource(
- const MoveParams& params,
- google_apis::GDataErrorCode status,
- scoped_ptr<google_apis::ResourceEntry> resource_entry) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-
- const FileError error = GDataToFileError(status);
- if (error != FILE_ERROR_OK) {
- params.callback.Run(error);
- return;
- }
-
- base::PostTaskAndReplyWithResult(
- blocking_task_runner_.get(),
- FROM_HERE,
- base::Bind(&RefreshEntry, metadata_, base::Passed(&resource_entry)),
- base::Bind(&MoveOperation::MoveAfterRefreshEntry,
- weak_ptr_factory_.GetWeakPtr(), params));
-}
-
-void MoveOperation::MoveAfterRefreshEntry(const MoveParams& params,
- FileError error) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (error == FILE_ERROR_OK) {
// Notify the change of directory.
- observer_->OnDirectoryChangedByOperation(params.src_file_path.DirName());
- observer_->OnDirectoryChangedByOperation(params.dest_file_path.DirName());
+ observer_->OnDirectoryChangedByOperation(src_file_path.DirName());
+ observer_->OnDirectoryChangedByOperation(dest_file_path.DirName());
+ observer_->OnEntryUpdatedByOperation(*local_id);
}
-
- params.callback.Run(error);
+ callback.Run(error);
}
} // namespace file_system

Powered by Google App Engine
This is Rietveld 408576698