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/update_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/update_operation.h" |
6 | 6 |
7 #include "chrome/browser/chromeos/drive/drive.pb.h" | 7 #include "chrome/browser/chromeos/drive/drive.pb.h" |
8 #include "chrome/browser/chromeos/drive/file_cache.h" | 8 #include "chrome/browser/chromeos/drive/file_cache.h" |
9 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" | 9 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" |
10 #include "chrome/browser/chromeos/drive/file_system_util.h" | 10 #include "chrome/browser/chromeos/drive/file_system_util.h" |
11 #include "chrome/browser/chromeos/drive/job_scheduler.h" | 11 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
12 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h" | 12 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h" |
13 #include "chrome/browser/chromeos/drive/resource_metadata.h" | 13 #include "chrome/browser/chromeos/drive/resource_metadata.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 | 15 |
16 using content::BrowserThread; | 16 using content::BrowserThread; |
17 | 17 |
18 namespace drive { | 18 namespace drive { |
19 namespace file_system { | 19 namespace file_system { |
20 | 20 |
21 UpdateOperation::UpdateOperation(OperationObserver* observer, | 21 namespace { |
22 JobScheduler* scheduler, | 22 |
23 internal::ResourceMetadata* metadata, | 23 // Gets locally stored information about the specified file. |
24 internal::FileCache* cache) | 24 FileError GetFileLocalState(internal::ResourceMetadata* metadata, |
25 : observer_(observer), | 25 internal::FileCache* cache, |
| 26 const std::string& resource_id, |
| 27 ResourceEntry* entry, |
| 28 base::FilePath* drive_file_path, |
| 29 base::FilePath* cache_file_path) { |
| 30 FileError error = metadata->GetResourceEntryById(resource_id, NULL, entry); |
| 31 if (error != FILE_ERROR_OK) |
| 32 return error; |
| 33 |
| 34 if (entry->file_info().is_directory()) |
| 35 return FILE_ERROR_NOT_A_FILE; |
| 36 |
| 37 *drive_file_path = metadata->GetFilePath(resource_id); |
| 38 if (drive_file_path->empty()) |
| 39 return FILE_ERROR_NOT_FOUND; |
| 40 |
| 41 return cache->GetFile(resource_id, |
| 42 entry->file_specific_info().file_md5(), |
| 43 cache_file_path); |
| 44 } |
| 45 |
| 46 // Updates locally stored information about the specified file. |
| 47 FileError UpdateFileLocalState( |
| 48 internal::ResourceMetadata* metadata, |
| 49 internal::FileCache* cache, |
| 50 scoped_ptr<google_apis::ResourceEntry> resource_entry, |
| 51 base::FilePath* drive_file_path) { |
| 52 const ResourceEntry& entry = ConvertToResourceEntry(*resource_entry); |
| 53 if (entry.resource_id().empty()) |
| 54 return FILE_ERROR_NOT_A_FILE; |
| 55 |
| 56 FileError error = metadata->RefreshEntry(entry, NULL, NULL); |
| 57 if (error != FILE_ERROR_OK) |
| 58 return error; |
| 59 |
| 60 *drive_file_path = metadata->GetFilePath(entry.resource_id()); |
| 61 if (drive_file_path->empty()) |
| 62 return FILE_ERROR_NOT_FOUND; |
| 63 |
| 64 // Clear the dirty bit if we have updated an existing file. |
| 65 return cache->ClearDirty(entry.resource_id(), |
| 66 entry.file_specific_info().file_md5()); |
| 67 } |
| 68 |
| 69 } // namespace |
| 70 |
| 71 UpdateOperation::UpdateOperation( |
| 72 base::SequencedTaskRunner* blocking_task_runner, |
| 73 OperationObserver* observer, |
| 74 JobScheduler* scheduler, |
| 75 internal::ResourceMetadata* metadata, |
| 76 internal::FileCache* cache) |
| 77 : blocking_task_runner_(blocking_task_runner), |
| 78 observer_(observer), |
26 scheduler_(scheduler), | 79 scheduler_(scheduler), |
27 metadata_(metadata), | 80 metadata_(metadata), |
28 cache_(cache), | 81 cache_(cache), |
29 weak_ptr_factory_(this) { | 82 weak_ptr_factory_(this) { |
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
31 } | 84 } |
32 | 85 |
33 UpdateOperation::~UpdateOperation() { | 86 UpdateOperation::~UpdateOperation() { |
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
35 } | 88 } |
36 | 89 |
37 void UpdateOperation::UpdateFileByResourceId( | 90 void UpdateOperation::UpdateFileByResourceId( |
38 const std::string& resource_id, | 91 const std::string& resource_id, |
39 DriveClientContext context, | 92 DriveClientContext context, |
40 const FileOperationCallback& callback) { | 93 const FileOperationCallback& callback) { |
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
42 DCHECK(!callback.is_null()); | 95 DCHECK(!callback.is_null()); |
43 | 96 |
44 metadata_->GetResourceEntryByIdOnUIThread( | 97 ResourceEntry* entry = new ResourceEntry; |
45 resource_id, | 98 base::FilePath* drive_file_path = new base::FilePath; |
46 base::Bind(&UpdateOperation::UpdateFileAfterGetEntryInfo, | 99 base::FilePath* cache_file_path = new base::FilePath; |
| 100 base::PostTaskAndReplyWithResult( |
| 101 blocking_task_runner_, |
| 102 FROM_HERE, |
| 103 base::Bind(&GetFileLocalState, |
| 104 metadata_, |
| 105 cache_, |
| 106 resource_id, |
| 107 entry, |
| 108 drive_file_path, |
| 109 cache_file_path), |
| 110 base::Bind(&UpdateOperation::UpdateFileAfterGetLocalState, |
47 weak_ptr_factory_.GetWeakPtr(), | 111 weak_ptr_factory_.GetWeakPtr(), |
48 context, | 112 context, |
49 callback)); | 113 callback, |
| 114 base::Owned(entry), |
| 115 base::Owned(drive_file_path), |
| 116 base::Owned(cache_file_path))); |
50 } | 117 } |
51 | 118 |
52 void UpdateOperation::UpdateFileAfterGetEntryInfo( | 119 void UpdateOperation::UpdateFileAfterGetLocalState( |
53 DriveClientContext context, | 120 DriveClientContext context, |
54 const FileOperationCallback& callback, | 121 const FileOperationCallback& callback, |
55 FileError error, | 122 const ResourceEntry* entry, |
56 const base::FilePath& drive_file_path, | 123 const base::FilePath* drive_file_path, |
57 scoped_ptr<ResourceEntry> entry) { | 124 const base::FilePath* cache_file_path, |
| 125 FileError error) { |
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
59 DCHECK(!callback.is_null()); | 127 DCHECK(!callback.is_null()); |
60 | 128 |
61 if (error != FILE_ERROR_OK) { | |
62 callback.Run(error); | |
63 return; | |
64 } | |
65 | |
66 DCHECK(entry); | |
67 if (entry->file_info().is_directory()) { | |
68 callback.Run(FILE_ERROR_NOT_A_FILE); | |
69 return; | |
70 } | |
71 | |
72 // Extract a pointer before we call Pass() so we can use it below. | |
73 ResourceEntry* entry_ptr = entry.get(); | |
74 cache_->GetFileOnUIThread( | |
75 entry_ptr->resource_id(), | |
76 entry_ptr->file_specific_info().file_md5(), | |
77 base::Bind(&UpdateOperation::UpdateFileAfterGetFile, | |
78 weak_ptr_factory_.GetWeakPtr(), | |
79 context, | |
80 callback, | |
81 drive_file_path, | |
82 base::Passed(&entry))); | |
83 } | |
84 | |
85 void UpdateOperation::UpdateFileAfterGetFile( | |
86 DriveClientContext context, | |
87 const FileOperationCallback& callback, | |
88 const base::FilePath& drive_file_path, | |
89 scoped_ptr<ResourceEntry> entry, | |
90 FileError error, | |
91 const base::FilePath& cache_file_path) { | |
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
93 DCHECK(!callback.is_null()); | |
94 | |
95 if (error != FILE_ERROR_OK) { | 129 if (error != FILE_ERROR_OK) { |
96 callback.Run(error); | 130 callback.Run(error); |
97 return; | 131 return; |
98 } | 132 } |
99 | 133 |
100 scheduler_->UploadExistingFile( | 134 scheduler_->UploadExistingFile( |
101 entry->resource_id(), | 135 entry->resource_id(), |
102 drive_file_path, | 136 *drive_file_path, |
103 cache_file_path, | 137 *cache_file_path, |
104 entry->file_specific_info().content_mime_type(), | 138 entry->file_specific_info().content_mime_type(), |
105 "", // etag | 139 "", // etag |
106 context, | 140 context, |
107 base::Bind(&UpdateOperation::UpdateFileAfterUpload, | 141 base::Bind(&UpdateOperation::UpdateFileAfterUpload, |
108 weak_ptr_factory_.GetWeakPtr(), | 142 weak_ptr_factory_.GetWeakPtr(), |
109 callback)); | 143 callback)); |
110 } | 144 } |
111 | 145 |
112 void UpdateOperation::UpdateFileAfterUpload( | 146 void UpdateOperation::UpdateFileAfterUpload( |
113 const FileOperationCallback& callback, | 147 const FileOperationCallback& callback, |
114 google_apis::GDataErrorCode error, | 148 google_apis::GDataErrorCode error, |
115 scoped_ptr<google_apis::ResourceEntry> resource_entry) { | 149 scoped_ptr<google_apis::ResourceEntry> resource_entry) { |
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
117 DCHECK(!callback.is_null()); | 151 DCHECK(!callback.is_null()); |
118 | 152 |
119 FileError drive_error = util::GDataToFileError(error); | 153 FileError drive_error = util::GDataToFileError(error); |
120 if (drive_error != FILE_ERROR_OK) { | 154 if (drive_error != FILE_ERROR_OK) { |
121 callback.Run(drive_error); | 155 callback.Run(drive_error); |
122 return; | 156 return; |
123 } | 157 } |
124 | 158 |
125 metadata_->RefreshEntryOnUIThread( | 159 base::FilePath* drive_file_path = new base::FilePath; |
126 ConvertToResourceEntry(*resource_entry), | 160 base::PostTaskAndReplyWithResult( |
127 base::Bind(&UpdateOperation::UpdateFileAfterRefresh, | 161 blocking_task_runner_, |
128 weak_ptr_factory_.GetWeakPtr(), callback)); | 162 FROM_HERE, |
| 163 base::Bind(&UpdateFileLocalState, |
| 164 metadata_, |
| 165 cache_, |
| 166 base::Passed(&resource_entry), |
| 167 drive_file_path), |
| 168 base::Bind(&UpdateOperation::UpdateFileAfterUpdateLocalState, |
| 169 weak_ptr_factory_.GetWeakPtr(), |
| 170 callback, |
| 171 base::Owned(drive_file_path))); |
129 } | 172 } |
130 | 173 |
131 void UpdateOperation::UpdateFileAfterRefresh( | 174 void UpdateOperation::UpdateFileAfterUpdateLocalState( |
132 const FileOperationCallback& callback, | 175 const FileOperationCallback& callback, |
133 FileError error, | 176 const base::FilePath* drive_file_path, |
134 const base::FilePath& drive_file_path, | 177 FileError error) { |
135 scoped_ptr<ResourceEntry> entry) { | |
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
137 DCHECK(!callback.is_null()); | 179 DCHECK(!callback.is_null()); |
138 | 180 |
139 if (error != FILE_ERROR_OK) { | 181 if (error != FILE_ERROR_OK) { |
140 callback.Run(error); | 182 callback.Run(error); |
141 return; | 183 return; |
142 } | 184 } |
143 | 185 observer_->OnDirectoryChangedByOperation(drive_file_path->DirName()); |
144 DCHECK(entry); | 186 callback.Run(FILE_ERROR_OK); |
145 DCHECK(entry->has_resource_id()); | |
146 DCHECK(entry->has_file_specific_info()); | |
147 DCHECK(entry->file_specific_info().has_file_md5()); | |
148 | |
149 observer_->OnDirectoryChangedByOperation(drive_file_path.DirName()); | |
150 | |
151 // Clear the dirty bit if we have updated an existing file. | |
152 cache_->ClearDirtyOnUIThread(entry->resource_id(), | |
153 entry->file_specific_info().file_md5(), | |
154 callback); | |
155 } | 187 } |
156 | 188 |
157 } // namespace file_system | 189 } // namespace file_system |
158 } // namespace drive | 190 } // namespace drive |
OLD | NEW |