OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/drive/file_system/remove_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/remove_operation.h" |
6 | 6 |
7 #include "base/sequenced_task_runner.h" | 7 #include "base/sequenced_task_runner.h" |
8 #include "chrome/browser/chromeos/drive/drive.pb.h" | 8 #include "chrome/browser/chromeos/drive/drive.pb.h" |
9 #include "chrome/browser/chromeos/drive/file_cache.h" | 9 #include "chrome/browser/chromeos/drive/file_cache.h" |
| 10 #include "chrome/browser/chromeos/drive/file_change.h" |
10 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" | 11 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" |
11 #include "chrome/browser/chromeos/drive/file_system_util.h" | 12 #include "chrome/browser/chromeos/drive/file_system_util.h" |
12 #include "chrome/browser/chromeos/drive/resource_metadata.h" | 13 #include "chrome/browser/chromeos/drive/resource_metadata.h" |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 | 15 |
15 using content::BrowserThread; | 16 using content::BrowserThread; |
16 | 17 |
17 namespace drive { | 18 namespace drive { |
18 namespace file_system { | 19 namespace file_system { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // Removes cache file and moves the metadata entry to the trash. | 23 // Removes cache file and moves the metadata entry to the trash. |
23 FileError UpdateLocalState(internal::ResourceMetadata* metadata, | 24 FileError UpdateLocalState(internal::ResourceMetadata* metadata, |
24 internal::FileCache* cache, | 25 internal::FileCache* cache, |
25 const base::FilePath& path, | 26 const base::FilePath& path, |
26 bool is_recursive, | 27 bool is_recursive, |
27 std::string* local_id, | 28 std::string* local_id, |
28 base::FilePath* changed_directory_path) { | 29 ResourceEntry* entry, |
| 30 base::FilePath* changed_path) { |
29 FileError error = metadata->GetIdByPath(path, local_id); | 31 FileError error = metadata->GetIdByPath(path, local_id); |
30 if (error != FILE_ERROR_OK) | 32 if (error != FILE_ERROR_OK) |
31 return error; | 33 return error; |
32 | 34 |
33 ResourceEntry entry; | 35 error = metadata->GetResourceEntryById(*local_id, entry); |
34 error = metadata->GetResourceEntryById(*local_id, &entry); | |
35 if (error != FILE_ERROR_OK) | 36 if (error != FILE_ERROR_OK) |
36 return error; | 37 return error; |
37 | 38 |
38 if (entry.file_info().is_directory() && !is_recursive) { | 39 if (entry->file_info().is_directory() && !is_recursive) { |
39 // Check emptiness of the directory. | 40 // Check emptiness of the directory. |
40 ResourceEntryVector entries; | 41 ResourceEntryVector entries; |
41 error = metadata->ReadDirectoryByPath(path, &entries); | 42 error = metadata->ReadDirectoryByPath(path, &entries); |
42 if (error != FILE_ERROR_OK) | 43 if (error != FILE_ERROR_OK) |
43 return error; | 44 return error; |
44 if (!entries.empty()) | 45 if (!entries.empty()) |
45 return FILE_ERROR_NOT_EMPTY; | 46 return FILE_ERROR_NOT_EMPTY; |
46 } | 47 } |
47 | 48 |
48 error = cache->Remove(*local_id); | 49 error = cache->Remove(*local_id); |
49 if (error != FILE_ERROR_OK) | 50 if (error != FILE_ERROR_OK) |
50 return error; | 51 return error; |
51 | 52 |
52 *changed_directory_path = path.DirName(); | 53 *changed_path = path; |
53 | 54 |
54 // Move to the trash. | 55 // Move to the trash. |
55 entry.set_parent_local_id(util::kDriveTrashDirLocalId); | 56 entry->set_parent_local_id(util::kDriveTrashDirLocalId); |
56 return metadata->RefreshEntry(entry); | 57 return metadata->RefreshEntry(*entry); |
57 } | 58 } |
58 | 59 |
59 } // namespace | 60 } // namespace |
60 | 61 |
61 RemoveOperation::RemoveOperation( | 62 RemoveOperation::RemoveOperation( |
62 base::SequencedTaskRunner* blocking_task_runner, | 63 base::SequencedTaskRunner* blocking_task_runner, |
63 OperationObserver* observer, | 64 OperationObserver* observer, |
64 internal::ResourceMetadata* metadata, | 65 internal::ResourceMetadata* metadata, |
65 internal::FileCache* cache) | 66 internal::FileCache* cache) |
66 : blocking_task_runner_(blocking_task_runner), | 67 : blocking_task_runner_(blocking_task_runner), |
67 observer_(observer), | 68 observer_(observer), |
68 metadata_(metadata), | 69 metadata_(metadata), |
69 cache_(cache), | 70 cache_(cache), |
70 weak_ptr_factory_(this) { | 71 weak_ptr_factory_(this) { |
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
72 } | 73 } |
73 | 74 |
74 RemoveOperation::~RemoveOperation() { | 75 RemoveOperation::~RemoveOperation() { |
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
76 } | 77 } |
77 | 78 |
78 void RemoveOperation::Remove(const base::FilePath& path, | 79 void RemoveOperation::Remove(const base::FilePath& path, |
79 bool is_recursive, | 80 bool is_recursive, |
80 const FileOperationCallback& callback) { | 81 const FileOperationCallback& callback) { |
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
82 DCHECK(!callback.is_null()); | 83 DCHECK(!callback.is_null()); |
83 | 84 |
84 std::string* local_id = new std::string; | 85 std::string* local_id = new std::string; |
85 base::FilePath* changed_directory_path = new base::FilePath; | 86 base::FilePath* changed_path = new base::FilePath; |
| 87 ResourceEntry* entry = new ResourceEntry; |
86 base::PostTaskAndReplyWithResult( | 88 base::PostTaskAndReplyWithResult( |
87 blocking_task_runner_.get(), | 89 blocking_task_runner_.get(), |
88 FROM_HERE, | 90 FROM_HERE, |
89 base::Bind(&UpdateLocalState, | 91 base::Bind(&UpdateLocalState, |
90 metadata_, | 92 metadata_, |
91 cache_, | 93 cache_, |
92 path, | 94 path, |
93 is_recursive, | 95 is_recursive, |
94 local_id, | 96 local_id, |
95 changed_directory_path), | 97 entry, |
| 98 changed_path), |
96 base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState, | 99 base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState, |
97 weak_ptr_factory_.GetWeakPtr(), | 100 weak_ptr_factory_.GetWeakPtr(), |
98 callback, | 101 callback, |
99 base::Owned(local_id), | 102 base::Owned(local_id), |
100 base::Owned(changed_directory_path))); | 103 base::Owned(entry), |
| 104 base::Owned(changed_path))); |
101 } | 105 } |
102 | 106 |
103 void RemoveOperation::RemoveAfterUpdateLocalState( | 107 void RemoveOperation::RemoveAfterUpdateLocalState( |
104 const FileOperationCallback& callback, | 108 const FileOperationCallback& callback, |
105 const std::string* local_id, | 109 const std::string* local_id, |
106 const base::FilePath* changed_directory_path, | 110 const ResourceEntry* entry, |
| 111 const base::FilePath* changed_path, |
107 FileError error) { | 112 FileError error) { |
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
109 DCHECK(!callback.is_null()); | 114 DCHECK(!callback.is_null()); |
110 | 115 |
111 if (error == FILE_ERROR_OK) { | 116 if (!changed_path->empty()) { |
112 observer_->OnDirectoryChangedByOperation(*changed_directory_path); | 117 FileChange changed_file; |
113 observer_->OnEntryUpdatedByOperation(*local_id); | 118 changed_file.Update(*changed_path, *entry, FileChange::DELETE); |
| 119 if (error == FILE_ERROR_OK) { |
| 120 observer_->OnFileChangedByOperation(changed_file); |
| 121 observer_->OnEntryUpdatedByOperation(*local_id); |
| 122 } |
114 } | 123 } |
115 | 124 |
116 callback.Run(error); | 125 callback.Run(error); |
117 } | 126 } |
118 | 127 |
119 } // namespace file_system | 128 } // namespace file_system |
120 } // namespace drive | 129 } // namespace drive |
OLD | NEW |