This source file includes following definitions.
- UpdateLocalStateAfterUnparent
- RemoveEntryOnUIThread
- weak_ptr_factory_
- TryToRemoveLocally
- RemoveAfterGetResourceEntry
- TrashResource
- TrashResourceAfterUpdateRemoteState
- UnparentResource
- UnparentResourceAfterGetResourceEntry
- UnparentResourceAfterUpdateRemoteState
#include "chrome/browser/chromeos/drive/sync/remove_performer.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/file_system_util.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 "chrome/browser/chromeos/drive/sync/entry_revert_performer.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace drive {
namespace internal {
namespace {
FileError UpdateLocalStateAfterUnparent(ResourceMetadata* metadata,
const std::string& local_id) {
ResourceEntry entry;
FileError error = metadata->GetResourceEntryById(local_id, &entry);
if (error != FILE_ERROR_OK)
return error;
entry.set_parent_local_id(util::kDriveOtherDirLocalId);
return metadata->RefreshEntry(entry);
}
void RemoveEntryOnUIThread(base::SequencedTaskRunner* blocking_task_runner,
ResourceMetadata* metadata,
const std::string& local_id,
const FileOperationCallback& callback) {
base::PostTaskAndReplyWithResult(
blocking_task_runner,
FROM_HERE,
base::Bind(&ResourceMetadata::RemoveEntry,
base::Unretained(metadata), local_id),
callback);
}
}
RemovePerformer::RemovePerformer(
base::SequencedTaskRunner* blocking_task_runner,
file_system::OperationObserver* observer,
JobScheduler* scheduler,
ResourceMetadata* metadata)
: blocking_task_runner_(blocking_task_runner),
observer_(observer),
scheduler_(scheduler),
metadata_(metadata),
entry_revert_performer_(new EntryRevertPerformer(blocking_task_runner,
observer,
scheduler,
metadata)),
weak_ptr_factory_(this) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
RemovePerformer::~RemovePerformer() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
FileError TryToRemoveLocally(ResourceMetadata* metadata,
const std::string& local_id,
ResourceEntry* entry) {
FileError error = metadata->GetResourceEntryById(local_id, entry);
if (error != FILE_ERROR_OK || !entry->resource_id().empty())
return error;
return metadata->RemoveEntry(local_id);
}
void RemovePerformer::Remove(const std::string& local_id,
const ClientContext& context,
const FileOperationCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
ResourceEntry* entry = new ResourceEntry;
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(&TryToRemoveLocally, metadata_, local_id, entry),
base::Bind(&RemovePerformer::RemoveAfterGetResourceEntry,
weak_ptr_factory_.GetWeakPtr(),
context,
callback,
base::Owned(entry)));
}
void RemovePerformer::RemoveAfterGetResourceEntry(
const ClientContext& context,
const FileOperationCallback& callback,
const ResourceEntry* entry,
FileError error) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
if (error != FILE_ERROR_OK || entry->resource_id().empty()) {
callback.Run(error);
return;
}
if (entry->shared_with_me()) {
UnparentResource(context, callback, entry->resource_id(),
entry->local_id());
} else {
TrashResource(context, callback, entry->resource_id(), entry->local_id());
}
}
void RemovePerformer::TrashResource(const ClientContext& context,
const FileOperationCallback& callback,
const std::string& resource_id,
const std::string& local_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
scheduler_->TrashResource(
resource_id,
context,
base::Bind(&RemovePerformer::TrashResourceAfterUpdateRemoteState,
weak_ptr_factory_.GetWeakPtr(), context, callback, local_id));
}
void RemovePerformer::TrashResourceAfterUpdateRemoteState(
const ClientContext& context,
const FileOperationCallback& callback,
const std::string& local_id,
google_apis::GDataErrorCode status) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
if (status == google_apis::HTTP_FORBIDDEN) {
entry_revert_performer_->RevertEntry(local_id, context, callback);
observer_->OnDriveSyncError(
file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION, local_id);
return;
}
FileError error = GDataToFileError(status);
if (error == FILE_ERROR_NOT_FOUND) {
RemoveEntryOnUIThread(blocking_task_runner_.get(), metadata_, local_id,
callback);
return;
}
callback.Run(error);
}
void RemovePerformer::UnparentResource(const ClientContext& context,
const FileOperationCallback& callback,
const std::string& resource_id,
const std::string& local_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
scheduler_->GetResourceEntry(
resource_id,
context,
base::Bind(&RemovePerformer::UnparentResourceAfterGetResourceEntry,
weak_ptr_factory_.GetWeakPtr(), context, callback, local_id));
}
void RemovePerformer::UnparentResourceAfterGetResourceEntry(
const ClientContext& context,
const FileOperationCallback& callback,
const std::string& local_id,
google_apis::GDataErrorCode status,
scoped_ptr<google_apis::ResourceEntry> resource_entry) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
FileError error = GDataToFileError(status);
if (error == FILE_ERROR_NOT_FOUND) {
RemoveEntryOnUIThread(blocking_task_runner_.get(), metadata_, local_id,
callback);
return;
}
if (error != FILE_ERROR_OK) {
callback.Run(error);
return;
}
ResourceEntry entry;
std::string parent_resource_id;
if (!ConvertToResourceEntry(*resource_entry, &entry, &parent_resource_id)) {
callback.Run(FILE_ERROR_NOT_A_FILE);
return;
}
if (!entry.shared_with_me()) {
UnparentResourceAfterUpdateRemoteState(callback, local_id,
google_apis::HTTP_CONFLICT);
return;
}
if (parent_resource_id.empty()) {
UnparentResourceAfterUpdateRemoteState(callback, local_id,
google_apis::HTTP_NO_CONTENT);
return;
}
scheduler_->RemoveResourceFromDirectory(
parent_resource_id,
entry.resource_id(),
context,
base::Bind(&RemovePerformer::UnparentResourceAfterUpdateRemoteState,
weak_ptr_factory_.GetWeakPtr(), callback, local_id));
}
void RemovePerformer::UnparentResourceAfterUpdateRemoteState(
const FileOperationCallback& callback,
const std::string& local_id,
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::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(&UpdateLocalStateAfterUnparent, metadata_, local_id),
callback);
}
}
}