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/download_handler.h" | 5 #include "chrome/browser/chromeos/drive/download_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/supports_user_data.h" | 9 #include "base/supports_user_data.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 | 134 |
135 drive_tmp_download_path_ = drive_tmp_download_path; | 135 drive_tmp_download_path_ = drive_tmp_download_path; |
136 | 136 |
137 if (download_manager) { | 137 if (download_manager) { |
138 notifier_.reset(new AllDownloadItemNotifier(download_manager, this)); | 138 notifier_.reset(new AllDownloadItemNotifier(download_manager, this)); |
139 // Remove any persisted Drive DownloadItem. crbug.com/171384 | 139 // Remove any persisted Drive DownloadItem. crbug.com/171384 |
140 content::DownloadManager::DownloadVector downloads; | 140 content::DownloadManager::DownloadVector downloads; |
141 download_manager->GetAllDownloads(&downloads); | 141 download_manager->GetAllDownloads(&downloads); |
142 for (size_t i = 0; i < downloads.size(); ++i) { | 142 for (size_t i = 0; i < downloads.size(); ++i) { |
143 if (IsPersistedDriveDownload(drive_tmp_download_path_, downloads[i])) | 143 if (IsPersistedDriveDownload(drive_tmp_download_path_, downloads[i])) |
144 RemoveDownload(downloads[i]->GetId()); | 144 downloads[i]->Remove(); |
145 } | 145 } |
146 } | 146 } |
147 } | 147 } |
148 | 148 |
149 void DownloadHandler::ObserveIncognitoDownloadManager( | |
150 DownloadManager* download_manager) { | |
151 if (!notifier_incognito_) { | |
hashimoto
2014/06/03 06:10:32
nit: DCHECK(!notifier_incognito_) is enough?
kinaba
2014/06/03 06:50:32
Done.
| |
152 notifier_incognito_.reset(new AllDownloadItemNotifier(download_manager, | |
153 this)); | |
154 } | |
155 } | |
156 | |
149 void DownloadHandler::SubstituteDriveDownloadPath( | 157 void DownloadHandler::SubstituteDriveDownloadPath( |
150 const base::FilePath& drive_path, | 158 const base::FilePath& drive_path, |
151 content::DownloadItem* download, | 159 content::DownloadItem* download, |
152 const SubstituteDriveDownloadPathCallback& callback) { | 160 const SubstituteDriveDownloadPathCallback& callback) { |
153 DVLOG(1) << "SubstituteDriveDownloadPath " << drive_path.value(); | 161 DVLOG(1) << "SubstituteDriveDownloadPath " << drive_path.value(); |
154 | 162 |
155 SetDownloadParams(drive_path, download); | 163 SetDownloadParams(drive_path, download); |
156 | 164 |
157 if (util::IsUnderDriveMountPoint(drive_path)) { | 165 if (util::IsUnderDriveMountPoint(drive_path)) { |
158 // Prepare the destination directory. | 166 // Prepare the destination directory. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 | 224 |
217 void DownloadHandler::OnDownloadCreated(DownloadManager* manager, | 225 void DownloadHandler::OnDownloadCreated(DownloadManager* manager, |
218 DownloadItem* download) { | 226 DownloadItem* download) { |
219 // Remove any persisted Drive DownloadItem. crbug.com/171384 | 227 // Remove any persisted Drive DownloadItem. crbug.com/171384 |
220 if (IsPersistedDriveDownload(drive_tmp_download_path_, download)) { | 228 if (IsPersistedDriveDownload(drive_tmp_download_path_, download)) { |
221 // Remove download later, since doing it here results in a crash. | 229 // Remove download later, since doing it here results in a crash. |
222 BrowserThread::PostTask(BrowserThread::UI, | 230 BrowserThread::PostTask(BrowserThread::UI, |
223 FROM_HERE, | 231 FROM_HERE, |
224 base::Bind(&DownloadHandler::RemoveDownload, | 232 base::Bind(&DownloadHandler::RemoveDownload, |
225 weak_ptr_factory_.GetWeakPtr(), | 233 weak_ptr_factory_.GetWeakPtr(), |
234 static_cast<void*>(manager), | |
226 download->GetId())); | 235 download->GetId())); |
227 } | 236 } |
228 } | 237 } |
229 | 238 |
230 void DownloadHandler::RemoveDownload(int id) { | 239 void DownloadHandler::RemoveDownload(void* manager_id, int id) { |
231 DownloadManager* manager = notifier_->GetManager(); | 240 // During the asynchronous task posting, the original download manager may |
241 // have gone. To verify the validity, we compare with managers held by | |
242 // notifiers which are ensured to be alive. | |
243 DownloadManager* manager = NULL; | |
244 if (manager_id == notifier_->GetManager()) | |
245 manager = notifier_->GetManager(); | |
246 else if (notifier_incognito_ && manager == notifier_incognito_->GetManager()) | |
247 manager = notifier_incognito_->GetManager(); | |
248 | |
232 if (!manager) | 249 if (!manager) |
233 return; | 250 return; |
234 DownloadItem* download = manager->GetDownload(id); | 251 DownloadItem* download = manager->GetDownload(id); |
235 if (!download) | 252 if (!download) |
236 return; | 253 return; |
237 download->Remove(); | 254 download->Remove(); |
238 } | 255 } |
239 | 256 |
240 void DownloadHandler::OnDownloadUpdated( | 257 void DownloadHandler::OnDownloadUpdated( |
241 DownloadManager* manager, DownloadItem* download) { | 258 DownloadManager* manager, DownloadItem* download) { |
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
243 | 260 |
244 // Only accept downloads that have the Drive meta data associated with them. | 261 // Only accept downloads that have the Drive meta data associated with them. |
245 DriveUserData* data = GetDriveUserData(download); | 262 DriveUserData* data = GetDriveUserData(download); |
246 if (!drive_tmp_download_path_.IsParent(download->GetTargetFilePath()) || | 263 if (!drive_tmp_download_path_.IsParent(download->GetTargetFilePath()) || |
247 !data || | 264 !data || |
248 data->is_complete()) | 265 data->is_complete()) |
249 return; | 266 return; |
250 | 267 |
251 switch (download->GetState()) { | 268 switch (download->GetState()) { |
252 case DownloadItem::IN_PROGRESS: | 269 case DownloadItem::IN_PROGRESS: |
253 break; | 270 break; |
254 | 271 |
255 case DownloadItem::COMPLETE: | 272 case DownloadItem::COMPLETE: |
256 UploadDownloadItem(download); | 273 UploadDownloadItem(manager, download); |
257 data->set_complete(); | 274 data->set_complete(); |
258 break; | 275 break; |
259 | 276 |
260 case DownloadItem::CANCELLED: | 277 case DownloadItem::CANCELLED: |
261 download->SetUserData(&kDrivePathKey, NULL); | 278 download->SetUserData(&kDrivePathKey, NULL); |
262 break; | 279 break; |
263 | 280 |
264 case DownloadItem::INTERRUPTED: | 281 case DownloadItem::INTERRUPTED: |
265 // Interrupted downloads can be resumed. Keep the Drive user data around | 282 // Interrupted downloads can be resumed. Keep the Drive user data around |
266 // so that it can be used when the download resumes. The download is truly | 283 // so that it can be used when the download resumes. The download is truly |
(...skipping 15 matching lines...) Expand all Loading... | |
282 FROM_HERE, | 299 FROM_HERE, |
283 base::Bind(&GetDriveTempDownloadPath, drive_tmp_download_path_), | 300 base::Bind(&GetDriveTempDownloadPath, drive_tmp_download_path_), |
284 callback); | 301 callback); |
285 } else { | 302 } else { |
286 LOG(WARNING) << "Failed to create directory, error = " | 303 LOG(WARNING) << "Failed to create directory, error = " |
287 << FileErrorToString(error); | 304 << FileErrorToString(error); |
288 callback.Run(base::FilePath()); | 305 callback.Run(base::FilePath()); |
289 } | 306 } |
290 } | 307 } |
291 | 308 |
292 void DownloadHandler::UploadDownloadItem(DownloadItem* download) { | 309 void DownloadHandler::UploadDownloadItem(DownloadManager* manager, |
310 DownloadItem* download) { | |
293 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); | 311 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); |
294 base::FilePath* cache_file_path = new base::FilePath; | 312 base::FilePath* cache_file_path = new base::FilePath; |
295 WriteOnCacheFileAndReply( | 313 WriteOnCacheFileAndReply( |
296 file_system_, | 314 file_system_, |
297 util::ExtractDrivePath(GetTargetPath(download)), | 315 util::ExtractDrivePath(GetTargetPath(download)), |
298 download->GetMimeType(), | 316 download->GetMimeType(), |
299 base::Bind(&MoveDownloadedFile, download->GetTargetFilePath(), | 317 base::Bind(&MoveDownloadedFile, download->GetTargetFilePath(), |
300 cache_file_path), | 318 cache_file_path), |
301 base::Bind(&DownloadHandler::SetCacheFilePath, | 319 base::Bind(&DownloadHandler::SetCacheFilePath, |
302 weak_ptr_factory_.GetWeakPtr(), | 320 weak_ptr_factory_.GetWeakPtr(), |
321 static_cast<void*>(manager), | |
303 download->GetId(), | 322 download->GetId(), |
304 base::Owned(cache_file_path))); | 323 base::Owned(cache_file_path))); |
305 } | 324 } |
306 | 325 |
307 void DownloadHandler::SetCacheFilePath(int id, | 326 void DownloadHandler::SetCacheFilePath(void* manager_id, |
327 int id, | |
308 const base::FilePath* cache_file_path, | 328 const base::FilePath* cache_file_path, |
309 FileError error) { | 329 FileError error) { |
330 // Validate the download manager. See the comment in RemoveDownload(). | |
331 DownloadManager* manager = NULL; | |
332 if (manager_id == notifier_->GetManager()) | |
333 manager = notifier_->GetManager(); | |
334 else if (notifier_incognito_ && manager == notifier_incognito_->GetManager()) | |
335 manager = notifier_incognito_->GetManager(); | |
hashimoto
2014/06/03 06:10:32
nit: How about adding a private method which maps
kinaba
2014/06/03 06:50:32
Done.
| |
336 | |
310 if (error != FILE_ERROR_OK) | 337 if (error != FILE_ERROR_OK) |
311 return; | 338 return; |
312 DownloadManager* manager = notifier_->GetManager(); | |
313 if (!manager) | 339 if (!manager) |
314 return; | 340 return; |
315 DownloadItem* download = manager->GetDownload(id); | 341 DownloadItem* download = manager->GetDownload(id); |
316 if (!download) | 342 if (!download) |
317 return; | 343 return; |
318 DriveUserData* data = GetDriveUserData(download); | 344 DriveUserData* data = GetDriveUserData(download); |
319 if (!data) | 345 if (!data) |
320 return; | 346 return; |
321 data->set_cache_file_path(*cache_file_path); | 347 data->set_cache_file_path(*cache_file_path); |
322 } | 348 } |
323 | 349 |
324 | 350 |
325 } // namespace drive | 351 } // namespace drive |
OLD | NEW |