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/move_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/move_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_change.h" |
9 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" | 10 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" |
10 #include "chrome/browser/chromeos/drive/resource_metadata.h" | 11 #include "chrome/browser/chromeos/drive/resource_metadata.h" |
11 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
12 | 13 |
13 using content::BrowserThread; | 14 using content::BrowserThread; |
14 | 15 |
15 namespace drive { | 16 namespace drive { |
16 namespace file_system { | 17 namespace file_system { |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Looks up ResourceEntry for source entry and the destination directory. | 20 // Looks up ResourceEntry for source entry and the destination directory. |
20 FileError UpdateLocalState(internal::ResourceMetadata* metadata, | 21 FileError UpdateLocalState(internal::ResourceMetadata* metadata, |
21 const base::FilePath& src_path, | 22 const base::FilePath& src_path, |
22 const base::FilePath& dest_path, | 23 const base::FilePath& dest_path, |
23 std::set<base::FilePath>* changed_directories, | 24 FileChange* changed_files, |
24 std::string* local_id) { | 25 std::string* local_id) { |
25 ResourceEntry entry; | 26 ResourceEntry entry; |
26 FileError error = metadata->GetResourceEntryByPath(src_path, &entry); | 27 FileError error = metadata->GetResourceEntryByPath(src_path, &entry); |
27 if (error != FILE_ERROR_OK) | 28 if (error != FILE_ERROR_OK) |
28 return error; | 29 return error; |
29 *local_id = entry.local_id(); | 30 *local_id = entry.local_id(); |
30 | 31 |
31 ResourceEntry parent_entry; | 32 ResourceEntry parent_entry; |
32 error = metadata->GetResourceEntryByPath(dest_path.DirName(), &parent_entry); | 33 error = metadata->GetResourceEntryByPath(dest_path.DirName(), &parent_entry); |
33 if (error != FILE_ERROR_OK) | 34 if (error != FILE_ERROR_OK) |
34 return error; | 35 return error; |
35 | 36 |
36 // The parent must be a directory. | 37 // The parent must be a directory. |
37 if (!parent_entry.file_info().is_directory()) | 38 if (!parent_entry.file_info().is_directory()) |
38 return FILE_ERROR_NOT_A_DIRECTORY; | 39 return FILE_ERROR_NOT_A_DIRECTORY; |
39 | 40 |
| 41 changed_files->Update(src_path, entry, FileChange::DELETE); |
| 42 |
40 // Strip the extension for a hosted document if necessary. | 43 // Strip the extension for a hosted document if necessary. |
41 const std::string new_extension = | 44 const std::string new_extension = |
42 base::FilePath(dest_path.Extension()).AsUTF8Unsafe(); | 45 base::FilePath(dest_path.Extension()).AsUTF8Unsafe(); |
43 const bool has_hosted_document_extension = | 46 const bool has_hosted_document_extension = |
44 entry.has_file_specific_info() && | 47 entry.has_file_specific_info() && |
45 entry.file_specific_info().is_hosted_document() && | 48 entry.file_specific_info().is_hosted_document() && |
46 new_extension == entry.file_specific_info().document_extension(); | 49 new_extension == entry.file_specific_info().document_extension(); |
47 const std::string new_title = | 50 const std::string new_title = |
48 has_hosted_document_extension ? | 51 has_hosted_document_extension ? |
49 dest_path.BaseName().RemoveExtension().AsUTF8Unsafe() : | 52 dest_path.BaseName().RemoveExtension().AsUTF8Unsafe() : |
50 dest_path.BaseName().AsUTF8Unsafe(); | 53 dest_path.BaseName().AsUTF8Unsafe(); |
51 | 54 |
52 entry.set_title(new_title); | 55 entry.set_title(new_title); |
53 entry.set_parent_local_id(parent_entry.local_id()); | 56 entry.set_parent_local_id(parent_entry.local_id()); |
54 entry.set_metadata_edit_state(ResourceEntry::DIRTY); | 57 entry.set_metadata_edit_state(ResourceEntry::DIRTY); |
55 entry.set_modification_date(base::Time::Now().ToInternalValue()); | 58 entry.set_modification_date(base::Time::Now().ToInternalValue()); |
56 error = metadata->RefreshEntry(entry); | 59 error = metadata->RefreshEntry(entry); |
57 if (error != FILE_ERROR_OK) | 60 if (error != FILE_ERROR_OK) |
58 return error; | 61 return error; |
59 | 62 |
60 changed_directories->insert(src_path.DirName()); | 63 changed_files->Update(dest_path, entry, FileChange::ADD_OR_UPDATE); |
61 changed_directories->insert(dest_path.DirName()); | |
62 return FILE_ERROR_OK; | 64 return FILE_ERROR_OK; |
63 } | 65 } |
64 | 66 |
65 } // namespace | 67 } // namespace |
66 | 68 |
67 MoveOperation::MoveOperation(base::SequencedTaskRunner* blocking_task_runner, | 69 MoveOperation::MoveOperation(base::SequencedTaskRunner* blocking_task_runner, |
68 OperationObserver* observer, | 70 OperationObserver* observer, |
69 internal::ResourceMetadata* metadata) | 71 internal::ResourceMetadata* metadata) |
70 : blocking_task_runner_(blocking_task_runner), | 72 : blocking_task_runner_(blocking_task_runner), |
71 observer_(observer), | 73 observer_(observer), |
72 metadata_(metadata), | 74 metadata_(metadata), |
73 weak_ptr_factory_(this) { | 75 weak_ptr_factory_(this) { |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
75 } | 77 } |
76 | 78 |
77 MoveOperation::~MoveOperation() { | 79 MoveOperation::~MoveOperation() { |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
79 } | 81 } |
80 | 82 |
81 void MoveOperation::Move(const base::FilePath& src_file_path, | 83 void MoveOperation::Move(const base::FilePath& src_file_path, |
82 const base::FilePath& dest_file_path, | 84 const base::FilePath& dest_file_path, |
83 const FileOperationCallback& callback) { | 85 const FileOperationCallback& callback) { |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
85 DCHECK(!callback.is_null()); | 87 DCHECK(!callback.is_null()); |
86 | 88 |
87 std::set<base::FilePath>* changed_directories = new std::set<base::FilePath>; | 89 FileChange* changed_files = new FileChange; |
88 std::string* local_id = new std::string; | 90 std::string* local_id = new std::string; |
89 base::PostTaskAndReplyWithResult( | 91 base::PostTaskAndReplyWithResult( |
90 blocking_task_runner_.get(), | 92 blocking_task_runner_.get(), |
91 FROM_HERE, | 93 FROM_HERE, |
92 base::Bind(&UpdateLocalState, | 94 base::Bind(&UpdateLocalState, |
93 metadata_, | 95 metadata_, |
94 src_file_path, | 96 src_file_path, |
95 dest_file_path, | 97 dest_file_path, |
96 changed_directories, | 98 changed_files, |
97 local_id), | 99 local_id), |
98 base::Bind(&MoveOperation::MoveAfterUpdateLocalState, | 100 base::Bind(&MoveOperation::MoveAfterUpdateLocalState, |
99 weak_ptr_factory_.GetWeakPtr(), | 101 weak_ptr_factory_.GetWeakPtr(), |
100 callback, | 102 callback, |
101 base::Owned(changed_directories), | 103 base::Owned(changed_files), |
102 base::Owned(local_id))); | 104 base::Owned(local_id))); |
103 } | 105 } |
104 | 106 |
105 void MoveOperation::MoveAfterUpdateLocalState( | 107 void MoveOperation::MoveAfterUpdateLocalState( |
106 const FileOperationCallback& callback, | 108 const FileOperationCallback& callback, |
107 const std::set<base::FilePath>* changed_directories, | 109 const FileChange* changed_files, |
108 const std::string* local_id, | 110 const std::string* local_id, |
109 FileError error) { | 111 FileError error) { |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
111 if (error == FILE_ERROR_OK) { | 113 if (error == FILE_ERROR_OK) { |
112 // Notify the change of directory. | 114 // Notify the change of directory. |
113 for (std::set<base::FilePath>::const_iterator it = | 115 observer_->OnFileChangedByOperation(*changed_files); |
114 changed_directories->begin(); | |
115 it != changed_directories->end(); ++it) | |
116 observer_->OnDirectoryChangedByOperation(*it); | |
117 | |
118 observer_->OnEntryUpdatedByOperation(*local_id); | 116 observer_->OnEntryUpdatedByOperation(*local_id); |
119 } | 117 } |
120 callback.Run(error); | 118 callback.Run(error); |
121 } | 119 } |
122 | 120 |
123 } // namespace file_system | 121 } // namespace file_system |
124 } // namespace drive | 122 } // namespace drive |
OLD | NEW |