| 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 DCHECK(!notifier_incognito_); |
| 152 notifier_incognito_.reset(new AllDownloadItemNotifier(download_manager, |
| 153 this)); |
| 154 } |
| 155 |
| 149 void DownloadHandler::SubstituteDriveDownloadPath( | 156 void DownloadHandler::SubstituteDriveDownloadPath( |
| 150 const base::FilePath& drive_path, | 157 const base::FilePath& drive_path, |
| 151 content::DownloadItem* download, | 158 content::DownloadItem* download, |
| 152 const SubstituteDriveDownloadPathCallback& callback) { | 159 const SubstituteDriveDownloadPathCallback& callback) { |
| 153 DVLOG(1) << "SubstituteDriveDownloadPath " << drive_path.value(); | 160 DVLOG(1) << "SubstituteDriveDownloadPath " << drive_path.value(); |
| 154 | 161 |
| 155 SetDownloadParams(drive_path, download); | 162 SetDownloadParams(drive_path, download); |
| 156 | 163 |
| 157 if (util::IsUnderDriveMountPoint(drive_path)) { | 164 if (util::IsUnderDriveMountPoint(drive_path)) { |
| 158 // Prepare the destination directory. | 165 // Prepare the destination directory. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 | 223 |
| 217 void DownloadHandler::OnDownloadCreated(DownloadManager* manager, | 224 void DownloadHandler::OnDownloadCreated(DownloadManager* manager, |
| 218 DownloadItem* download) { | 225 DownloadItem* download) { |
| 219 // Remove any persisted Drive DownloadItem. crbug.com/171384 | 226 // Remove any persisted Drive DownloadItem. crbug.com/171384 |
| 220 if (IsPersistedDriveDownload(drive_tmp_download_path_, download)) { | 227 if (IsPersistedDriveDownload(drive_tmp_download_path_, download)) { |
| 221 // Remove download later, since doing it here results in a crash. | 228 // Remove download later, since doing it here results in a crash. |
| 222 BrowserThread::PostTask(BrowserThread::UI, | 229 BrowserThread::PostTask(BrowserThread::UI, |
| 223 FROM_HERE, | 230 FROM_HERE, |
| 224 base::Bind(&DownloadHandler::RemoveDownload, | 231 base::Bind(&DownloadHandler::RemoveDownload, |
| 225 weak_ptr_factory_.GetWeakPtr(), | 232 weak_ptr_factory_.GetWeakPtr(), |
| 233 static_cast<void*>(manager), |
| 226 download->GetId())); | 234 download->GetId())); |
| 227 } | 235 } |
| 228 } | 236 } |
| 229 | 237 |
| 230 void DownloadHandler::RemoveDownload(int id) { | 238 void DownloadHandler::RemoveDownload(void* manager_id, int id) { |
| 231 DownloadManager* manager = notifier_->GetManager(); | 239 DownloadManager* manager = GetDownloadManager(manager_id); |
| 232 if (!manager) | 240 if (!manager) |
| 233 return; | 241 return; |
| 234 DownloadItem* download = manager->GetDownload(id); | 242 DownloadItem* download = manager->GetDownload(id); |
| 235 if (!download) | 243 if (!download) |
| 236 return; | 244 return; |
| 237 download->Remove(); | 245 download->Remove(); |
| 238 } | 246 } |
| 239 | 247 |
| 240 void DownloadHandler::OnDownloadUpdated( | 248 void DownloadHandler::OnDownloadUpdated( |
| 241 DownloadManager* manager, DownloadItem* download) { | 249 DownloadManager* manager, DownloadItem* download) { |
| 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 243 | 251 |
| 244 // Only accept downloads that have the Drive meta data associated with them. | 252 // Only accept downloads that have the Drive meta data associated with them. |
| 245 DriveUserData* data = GetDriveUserData(download); | 253 DriveUserData* data = GetDriveUserData(download); |
| 246 if (!drive_tmp_download_path_.IsParent(download->GetTargetFilePath()) || | 254 if (!drive_tmp_download_path_.IsParent(download->GetTargetFilePath()) || |
| 247 !data || | 255 !data || |
| 248 data->is_complete()) | 256 data->is_complete()) |
| 249 return; | 257 return; |
| 250 | 258 |
| 251 switch (download->GetState()) { | 259 switch (download->GetState()) { |
| 252 case DownloadItem::IN_PROGRESS: | 260 case DownloadItem::IN_PROGRESS: |
| 253 break; | 261 break; |
| 254 | 262 |
| 255 case DownloadItem::COMPLETE: | 263 case DownloadItem::COMPLETE: |
| 256 UploadDownloadItem(download); | 264 UploadDownloadItem(manager, download); |
| 257 data->set_complete(); | 265 data->set_complete(); |
| 258 break; | 266 break; |
| 259 | 267 |
| 260 case DownloadItem::CANCELLED: | 268 case DownloadItem::CANCELLED: |
| 261 download->SetUserData(&kDrivePathKey, NULL); | 269 download->SetUserData(&kDrivePathKey, NULL); |
| 262 break; | 270 break; |
| 263 | 271 |
| 264 case DownloadItem::INTERRUPTED: | 272 case DownloadItem::INTERRUPTED: |
| 265 // Interrupted downloads can be resumed. Keep the Drive user data around | 273 // 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 | 274 // so that it can be used when the download resumes. The download is truly |
| (...skipping 15 matching lines...) Expand all Loading... |
| 282 FROM_HERE, | 290 FROM_HERE, |
| 283 base::Bind(&GetDriveTempDownloadPath, drive_tmp_download_path_), | 291 base::Bind(&GetDriveTempDownloadPath, drive_tmp_download_path_), |
| 284 callback); | 292 callback); |
| 285 } else { | 293 } else { |
| 286 LOG(WARNING) << "Failed to create directory, error = " | 294 LOG(WARNING) << "Failed to create directory, error = " |
| 287 << FileErrorToString(error); | 295 << FileErrorToString(error); |
| 288 callback.Run(base::FilePath()); | 296 callback.Run(base::FilePath()); |
| 289 } | 297 } |
| 290 } | 298 } |
| 291 | 299 |
| 292 void DownloadHandler::UploadDownloadItem(DownloadItem* download) { | 300 void DownloadHandler::UploadDownloadItem(DownloadManager* manager, |
| 301 DownloadItem* download) { |
| 293 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); | 302 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); |
| 294 base::FilePath* cache_file_path = new base::FilePath; | 303 base::FilePath* cache_file_path = new base::FilePath; |
| 295 WriteOnCacheFileAndReply( | 304 WriteOnCacheFileAndReply( |
| 296 file_system_, | 305 file_system_, |
| 297 util::ExtractDrivePath(GetTargetPath(download)), | 306 util::ExtractDrivePath(GetTargetPath(download)), |
| 298 download->GetMimeType(), | 307 download->GetMimeType(), |
| 299 base::Bind(&MoveDownloadedFile, download->GetTargetFilePath(), | 308 base::Bind(&MoveDownloadedFile, download->GetTargetFilePath(), |
| 300 cache_file_path), | 309 cache_file_path), |
| 301 base::Bind(&DownloadHandler::SetCacheFilePath, | 310 base::Bind(&DownloadHandler::SetCacheFilePath, |
| 302 weak_ptr_factory_.GetWeakPtr(), | 311 weak_ptr_factory_.GetWeakPtr(), |
| 312 static_cast<void*>(manager), |
| 303 download->GetId(), | 313 download->GetId(), |
| 304 base::Owned(cache_file_path))); | 314 base::Owned(cache_file_path))); |
| 305 } | 315 } |
| 306 | 316 |
| 307 void DownloadHandler::SetCacheFilePath(int id, | 317 void DownloadHandler::SetCacheFilePath(void* manager_id, |
| 318 int id, |
| 308 const base::FilePath* cache_file_path, | 319 const base::FilePath* cache_file_path, |
| 309 FileError error) { | 320 FileError error) { |
| 310 if (error != FILE_ERROR_OK) | 321 if (error != FILE_ERROR_OK) |
| 311 return; | 322 return; |
| 312 DownloadManager* manager = notifier_->GetManager(); | 323 DownloadManager* manager = GetDownloadManager(manager_id); |
| 313 if (!manager) | 324 if (!manager) |
| 314 return; | 325 return; |
| 315 DownloadItem* download = manager->GetDownload(id); | 326 DownloadItem* download = manager->GetDownload(id); |
| 316 if (!download) | 327 if (!download) |
| 317 return; | 328 return; |
| 318 DriveUserData* data = GetDriveUserData(download); | 329 DriveUserData* data = GetDriveUserData(download); |
| 319 if (!data) | 330 if (!data) |
| 320 return; | 331 return; |
| 321 data->set_cache_file_path(*cache_file_path); | 332 data->set_cache_file_path(*cache_file_path); |
| 322 } | 333 } |
| 323 | 334 |
| 335 DownloadManager* DownloadHandler::GetDownloadManager(void* manager_id) { |
| 336 if (manager_id == notifier_->GetManager()) |
| 337 return notifier_->GetManager(); |
| 338 if (notifier_incognito_ && manager_id == notifier_incognito_->GetManager()) |
| 339 return notifier_incognito_->GetManager(); |
| 340 return NULL; |
| 341 } |
| 324 | 342 |
| 325 } // namespace drive | 343 } // namespace drive |
| OLD | NEW |