Ensure low-memory renderers retry failed loads correctly.
[chromium-blink-merge.git] / components / drive / file_system / remove_operation.cc
blob9cfc56c405a62b17e505de0c49f187a1cec97c1f
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
3 // found in the LICENSE file.
5 #include "components/drive/file_system/remove_operation.h"
7 #include "base/sequenced_task_runner.h"
8 #include "components/drive/drive.pb.h"
9 #include "components/drive/file_cache.h"
10 #include "components/drive/file_change.h"
11 #include "components/drive/file_system/operation_delegate.h"
12 #include "components/drive/file_system_core_util.h"
13 #include "components/drive/job_scheduler.h"
14 #include "components/drive/resource_metadata.h"
16 namespace drive {
17 namespace file_system {
19 namespace {
21 // Removes cache file and moves the metadata entry to the trash.
22 FileError UpdateLocalState(internal::ResourceMetadata* metadata,
23 internal::FileCache* cache,
24 const base::FilePath& path,
25 bool is_recursive,
26 std::string* local_id,
27 ResourceEntry* entry,
28 base::FilePath* changed_path) {
29 FileError error = metadata->GetIdByPath(path, local_id);
30 if (error != FILE_ERROR_OK)
31 return error;
33 error = metadata->GetResourceEntryById(*local_id, entry);
34 if (error != FILE_ERROR_OK)
35 return error;
37 if (entry->file_info().is_directory() && !is_recursive) {
38 // Check emptiness of the directory.
39 ResourceEntryVector entries;
40 error = metadata->ReadDirectoryByPath(path, &entries);
41 if (error != FILE_ERROR_OK)
42 return error;
43 if (!entries.empty())
44 return FILE_ERROR_NOT_EMPTY;
47 error = cache->Remove(*local_id);
48 if (error != FILE_ERROR_OK)
49 return error;
51 *changed_path = path;
53 // Move to the trash.
54 entry->set_parent_local_id(util::kDriveTrashDirLocalId);
55 return metadata->RefreshEntry(*entry);
58 } // namespace
60 RemoveOperation::RemoveOperation(
61 base::SequencedTaskRunner* blocking_task_runner,
62 OperationDelegate* delegate,
63 internal::ResourceMetadata* metadata,
64 internal::FileCache* cache)
65 : blocking_task_runner_(blocking_task_runner),
66 delegate_(delegate),
67 metadata_(metadata),
68 cache_(cache),
69 weak_ptr_factory_(this) {
72 RemoveOperation::~RemoveOperation() {
73 DCHECK(thread_checker_.CalledOnValidThread());
76 void RemoveOperation::Remove(const base::FilePath& path,
77 bool is_recursive,
78 const FileOperationCallback& callback) {
79 DCHECK(thread_checker_.CalledOnValidThread());
80 DCHECK(!callback.is_null());
82 std::string* local_id = new std::string;
83 base::FilePath* changed_path = new base::FilePath;
84 ResourceEntry* entry = new ResourceEntry;
85 base::PostTaskAndReplyWithResult(
86 blocking_task_runner_.get(),
87 FROM_HERE,
88 base::Bind(&UpdateLocalState,
89 metadata_,
90 cache_,
91 path,
92 is_recursive,
93 local_id,
94 entry,
95 changed_path),
96 base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState,
97 weak_ptr_factory_.GetWeakPtr(),
98 callback,
99 base::Owned(local_id),
100 base::Owned(entry),
101 base::Owned(changed_path)));
104 void RemoveOperation::RemoveAfterUpdateLocalState(
105 const FileOperationCallback& callback,
106 const std::string* local_id,
107 const ResourceEntry* entry,
108 const base::FilePath* changed_path,
109 FileError error) {
110 DCHECK(thread_checker_.CalledOnValidThread());
111 DCHECK(!callback.is_null());
113 if (!changed_path->empty()) {
114 FileChange changed_file;
115 changed_file.Update(*changed_path, *entry, FileChange::CHANGE_TYPE_DELETE);
116 if (error == FILE_ERROR_OK) {
117 delegate_->OnFileChangedByOperation(changed_file);
118 delegate_->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED),
119 *local_id);
123 callback.Run(error);
126 } // namespace file_system
127 } // namespace drive