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

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

Issue 68543002: drive: Support offline delete (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move to drive/other and mark as deleted Created 7 years, 1 month 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/remove_operation.cc
diff --git a/chrome/browser/chromeos/drive/file_system/remove_operation.cc b/chrome/browser/chromeos/drive/file_system/remove_operation.cc
index 8c6296a7979445dbacafb541ea365a0d0a02cf88..5aa445a17e1a60cd03434e79b4c2ce806f6e7a5b 100644
--- a/chrome/browser/chromeos/drive/file_system/remove_operation.cc
+++ b/chrome/browser/chromeos/drive/file_system/remove_operation.cc
@@ -9,7 +9,7 @@
#include "chrome/browser/chromeos/drive/file_cache.h"
#include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
-#include "chrome/browser/chromeos/drive/job_scheduler.h"
+#include "chrome/browser/chromeos/drive/resource_metadata.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
@@ -19,79 +19,71 @@ namespace file_system {
namespace {
-// Checks local metadata state before requesting remote delete.
-// |parent_resource_id| is set to the resource ID of the parent directory of
-// |path|. If it is a special folder like drive/other, empty string is set.
-// |entry| is the resource entry for the |path|.
-FileError CheckLocalState(internal::ResourceMetadata* metadata,
- const base::FilePath& path,
- bool is_recursive,
- std::string* parent_resource_id,
- ResourceEntry* entry) {
- std::string local_id;
- FileError error = metadata->GetIdByPath(path, &local_id);
+// Recursively mark entries as deleted.
+FileError SetDeletedRecursively(internal::ResourceMetadata* metadata,
+ const ResourceEntry& entry) {
+ ResourceEntry new_entry(entry);
+ new_entry.set_deleted(true);
+ FileError error = metadata->RefreshEntry(new_entry);
if (error != FILE_ERROR_OK)
return error;
- error = metadata->GetResourceEntryById(local_id, entry);
- if (error != FILE_ERROR_OK)
- return error;
-
- if (entry->file_info().is_directory() && !is_recursive) {
- // Check emptiness of the directory.
+ if (entry.file_info().is_directory()) {
ResourceEntryVector entries;
- error = metadata->ReadDirectoryByPath(path, &entries);
+ error = metadata->ReadDirectoryById(entry.local_id(), &entries);
if (error != FILE_ERROR_OK)
return error;
- if (!entries.empty())
- return FILE_ERROR_NOT_EMPTY;
- }
- // Get the resource_id of the parent folder. If it is a special folder that
- // does not have the server side ID, returns an empty string (not an error).
- if (util::IsSpecialResourceId(entry->parent_local_id())) {
- *parent_resource_id = "";
- } else {
- ResourceEntry parent_entry;
- error = metadata->GetResourceEntryById(entry->parent_local_id(),
- &parent_entry);
- if (error != FILE_ERROR_OK)
+ for (size_t i = 0; i < entries.size(); ++i) {
+ error = SetDeletedRecursively(metadata, entries[i]);
+ if (error != FILE_ERROR_OK)
return error;
- *parent_resource_id = parent_entry.resource_id();
+ }
}
-
return FILE_ERROR_OK;
}
-// Updates local metadata and cache state after remote delete.
-FileError UpdateLocalStateAfterDelete(internal::ResourceMetadata* metadata,
- internal::FileCache* cache,
- const std::string& local_id,
- base::FilePath* changed_directory_path) {
- *changed_directory_path = metadata->GetFilePath(local_id).DirName();
- FileError error = metadata->RemoveEntry(local_id);
+// Removes cache file and moves the metadata entry to "other" directory and mark
+// the entry as deleted.
+FileError UpdateLocalState(internal::ResourceMetadata* metadata,
+ internal::FileCache* cache,
+ const base::FilePath& path,
+ bool is_recursive,
+ std::string* local_id,
+ base::FilePath* changed_directory_path) {
+ FileError error = metadata->GetIdByPath(path, local_id);
if (error != FILE_ERROR_OK)
return error;
- error = cache->Remove(local_id);
- DLOG_IF(ERROR, error != FILE_ERROR_OK) << "Failed to remove: " << local_id;
+ ResourceEntry entry;
+ error = metadata->GetResourceEntryById(*local_id, &entry);
+ if (error != FILE_ERROR_OK)
+ return error;
- return FILE_ERROR_OK;
-}
+ if (entry.file_info().is_directory() && !is_recursive) {
+ // Check emptiness of the directory.
+ ResourceEntryVector entries;
+ error = metadata->ReadDirectoryById(*local_id, &entries);
+ if (error != FILE_ERROR_OK)
+ return error;
+ if (!entries.empty())
+ return FILE_ERROR_NOT_EMPTY;
+ }
-// Updates local metadata and after remote unparenting.
-FileError UpdateLocalStateAfterUnparent(
- internal::ResourceMetadata* metadata,
- const std::string& local_id,
- base::FilePath* changed_directory_path) {
- *changed_directory_path = metadata->GetFilePath(local_id).DirName();
+ *changed_directory_path = metadata->GetFilePath(*local_id).DirName();
- ResourceEntry entry;
- FileError error = metadata->GetResourceEntryById(local_id, &entry);
+ // Move to "other" directory.
+ entry.set_parent_local_id(util::kDriveOtherDirLocalId);
+ error = metadata->RefreshEntry(entry);
if (error != FILE_ERROR_OK)
return error;
- entry.set_parent_local_id(util::kDriveOtherDirLocalId);
- return metadata->RefreshEntry(entry);
+
+ // Mark as deleted recursively.
+ error = SetDeletedRecursively(metadata, entry);
+ if (error != FILE_ERROR_OK)
+ return error;
+
+ return cache->Remove(*local_id);
}
} // namespace
@@ -99,12 +91,10 @@ FileError UpdateLocalStateAfterUnparent(
RemoveOperation::RemoveOperation(
base::SequencedTaskRunner* blocking_task_runner,
OperationObserver* observer,
- JobScheduler* scheduler,
internal::ResourceMetadata* metadata,
internal::FileCache* cache)
: blocking_task_runner_(blocking_task_runner),
observer_(observer),
- scheduler_(scheduler),
metadata_(metadata),
cache_(cache),
weak_ptr_factory_(this) {
@@ -121,103 +111,37 @@ void RemoveOperation::Remove(const base::FilePath& path,
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
- std::string* parent_resource_id = new std::string;
- ResourceEntry* entry = new ResourceEntry;
+ std::string* local_id = new std::string;
+ base::FilePath* changed_directory_path = new base::FilePath;
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
- base::Bind(&CheckLocalState,
+ base::Bind(&UpdateLocalState,
metadata_,
+ cache_,
path,
is_recursive,
- parent_resource_id,
- entry),
- base::Bind(&RemoveOperation::RemoveAfterCheckLocalState,
- weak_ptr_factory_.GetWeakPtr(),
- callback,
- base::Owned(parent_resource_id),
- base::Owned(entry)));
-}
-
-void RemoveOperation::RemoveAfterCheckLocalState(
- const FileOperationCallback& callback,
- const std::string* parent_resource_id,
- const ResourceEntry* entry,
- FileError error) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- DCHECK(!callback.is_null());
-
- if (error != FILE_ERROR_OK) {
- callback.Run(error);
- return;
- }
-
- // To match with the behavior of drive.google.com:
- // Removal of shared entries under MyDrive is just removing from the parent.
- // The entry will stay in shared-with-me (in other words, in "drive/other".)
- //
- // TODO(kinaba): to be more precise, we might be better to branch by whether
- // or not the current account is an owner of the file. The code below is
- // written under the assumption that |shared_with_me| coincides with that.
- if (entry->shared_with_me() && !parent_resource_id->empty()) {
- scheduler_->RemoveResourceFromDirectory(
- *parent_resource_id,
- entry->resource_id(),
- ClientContext(USER_INITIATED),
- base::Bind(&RemoveOperation::RemoveAfterUpdateRemoteState,
- weak_ptr_factory_.GetWeakPtr(),
- callback,
- base::Bind(&UpdateLocalStateAfterUnparent,
- metadata_,
- entry->local_id())));
- } else {
- // Otherwise try sending the entry to trash.
- scheduler_->DeleteResource(
- entry->resource_id(),
- ClientContext(USER_INITIATED),
- base::Bind(&RemoveOperation::RemoveAfterUpdateRemoteState,
- weak_ptr_factory_.GetWeakPtr(),
- callback,
- base::Bind(&UpdateLocalStateAfterDelete,
- metadata_,
- cache_,
- entry->local_id())));
- }
-}
-
-void RemoveOperation::RemoveAfterUpdateRemoteState(
- const FileOperationCallback& callback,
- const base::Callback<FileError(base::FilePath*)>& local_update_task,
- google_apis::GDataErrorCode status) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- DCHECK(!callback.is_null());
-
- FileError error = GDataToFileError(status);
- if (error != FILE_ERROR_OK) {
- callback.Run(error);
- return;
- }
-
- base::FilePath* changed_directory_path = new base::FilePath;
- base::PostTaskAndReplyWithResult(
- blocking_task_runner_.get(),
- FROM_HERE,
- base::Bind(local_update_task, changed_directory_path),
+ local_id,
+ changed_directory_path),
base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState,
weak_ptr_factory_.GetWeakPtr(),
callback,
+ base::Owned(local_id),
base::Owned(changed_directory_path)));
}
void RemoveOperation::RemoveAfterUpdateLocalState(
const FileOperationCallback& callback,
+ const std::string* local_id,
const base::FilePath* changed_directory_path,
FileError error) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
- if (error == FILE_ERROR_OK)
+ if (error == FILE_ERROR_OK) {
observer_->OnDirectoryChangedByOperation(*changed_directory_path);
+ observer_->OnEntryRemovedByOperation(*local_id);
+ }
callback.Run(error);
}

Powered by Google App Engine
This is Rietveld 408576698