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/gdata/gdata_download_observer.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "chrome/browser/chromeos/gdata/gdata.pb.h" | 9 #include "chrome/browser/chromeos/gdata/gdata.pb.h" |
10 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h" | 10 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 private: | 51 private: |
52 GDataUploader* uploader_; | 52 GDataUploader* uploader_; |
53 int upload_id_; | 53 int upload_id_; |
54 FilePath virtual_dir_path_; | 54 FilePath virtual_dir_path_; |
55 scoped_ptr<DocumentEntry> entry_; | 55 scoped_ptr<DocumentEntry> entry_; |
56 | 56 |
57 DISALLOW_COPY_AND_ASSIGN(UploadingExternalData); | 57 DISALLOW_COPY_AND_ASSIGN(UploadingExternalData); |
58 }; | 58 }; |
59 | 59 |
60 // External Data stored in DownloadItem for gdata path. | 60 // External Data stored in DownloadItem for gdata path. |
61 class GDataExternalData : public DownloadItem::ExternalData { | 61 class GDataExternalData : public base::SupportsUserData::Data { |
62 public: | 62 public: |
63 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} | 63 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} |
64 virtual ~GDataExternalData() {} | 64 virtual ~GDataExternalData() {} |
65 | 65 |
66 const FilePath& file_path() const { return file_path_; } | 66 const FilePath& file_path() const { return file_path_; } |
67 | 67 |
68 private: | 68 private: |
69 FilePath file_path_; | 69 FilePath file_path_; |
70 }; | 70 }; |
71 | 71 |
72 // Extracts UploadingExternalData* from |download|. | 72 // Extracts UploadingExternalData* from |download|. |
73 UploadingExternalData* GetUploadingExternalData(DownloadItem* download) { | 73 UploadingExternalData* GetUploadingExternalData(DownloadItem* download) { |
74 return static_cast<UploadingExternalData*>( | 74 return static_cast<UploadingExternalData*>( |
75 download->GetExternalData(&kUploadingKey)); | 75 download->GetUserData(&kUploadingKey)); |
76 } | 76 } |
77 | 77 |
78 // Extracts GDataExternalData* from |download|. | 78 // Extracts GDataExternalData* from |download|. |
79 GDataExternalData* GetGDataExternalData(DownloadItem* download) { | 79 GDataExternalData* GetGDataExternalData(DownloadItem* download) { |
80 return static_cast<GDataExternalData*>( | 80 return static_cast<GDataExternalData*>( |
81 download->GetExternalData(&kGDataPathKey)); | 81 download->GetUserData(&kGDataPathKey)); |
82 } | 82 } |
83 | 83 |
84 void RunSubstituteGDataDownloadCallback( | 84 void RunSubstituteGDataDownloadCallback( |
85 const GDataDownloadObserver::SubstituteGDataDownloadPathCallback& callback, | 85 const GDataDownloadObserver::SubstituteGDataDownloadPathCallback& callback, |
86 const FilePath* file_path) { | 86 const FilePath* file_path) { |
87 callback.Run(*file_path); | 87 callback.Run(*file_path); |
88 } | 88 } |
89 | 89 |
90 gdata::GDataSystemService* GetSystemService(Profile* profile) { | 90 gdata::GDataSystemService* GetSystemService(Profile* profile) { |
91 gdata::GDataSystemService* system_service = | 91 gdata::GDataSystemService* system_service = |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 } | 226 } |
227 } | 227 } |
228 | 228 |
229 // static | 229 // static |
230 void GDataDownloadObserver::SetDownloadParams(const FilePath& gdata_path, | 230 void GDataDownloadObserver::SetDownloadParams(const FilePath& gdata_path, |
231 DownloadItem* download) { | 231 DownloadItem* download) { |
232 if (!download) | 232 if (!download) |
233 return; | 233 return; |
234 | 234 |
235 if (gdata::util::IsUnderGDataMountPoint(gdata_path)) { | 235 if (gdata::util::IsUnderGDataMountPoint(gdata_path)) { |
236 download->SetExternalData(&kGDataPathKey, | 236 download->SetUserData(&kGDataPathKey, |
237 new GDataExternalData(gdata_path)); | 237 new GDataExternalData(gdata_path)); |
238 download->SetDisplayName(gdata_path.BaseName()); | 238 download->SetDisplayName(gdata_path.BaseName()); |
239 download->SetIsTemporary(true); | 239 download->SetIsTemporary(true); |
240 } else if (IsGDataDownload(download)) { | 240 } else if (IsGDataDownload(download)) { |
241 // This may have been previously set if the default download folder is | 241 // This may have been previously set if the default download folder is |
242 // /drive, and the user has now changed the download target to a local | 242 // /drive, and the user has now changed the download target to a local |
243 // folder. | 243 // folder. |
244 download->SetExternalData(&kGDataPathKey, NULL); | 244 download->SetUserData(&kGDataPathKey, NULL); |
245 download->SetDisplayName(gdata_path); | 245 download->SetDisplayName(gdata_path); |
246 // TODO(achuith): This is not quite right. | 246 // TODO(achuith): This is not quite right. |
247 download->SetIsTemporary(false); | 247 download->SetIsTemporary(false); |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 // static | 251 // static |
252 FilePath GDataDownloadObserver::GetGDataPath(DownloadItem* download) { | 252 FilePath GDataDownloadObserver::GetGDataPath(DownloadItem* download) { |
253 GDataExternalData* data = GetGDataExternalData(download); | 253 GDataExternalData* data = GetGDataExternalData(download); |
254 // If data is NULL, we've somehow lost the gdata path selected by the file | 254 // If data is NULL, we've somehow lost the gdata path selected by the file |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 break; | 356 break; |
357 | 357 |
358 case DownloadItem::COMPLETE: | 358 case DownloadItem::COMPLETE: |
359 UploadDownloadItem(download); | 359 UploadDownloadItem(download); |
360 MoveFileToGDataCache(download); | 360 MoveFileToGDataCache(download); |
361 RemovePendingDownload(download); | 361 RemovePendingDownload(download); |
362 break; | 362 break; |
363 | 363 |
364 // TODO(achuith): Stop the pending upload and delete the file. | 364 // TODO(achuith): Stop the pending upload and delete the file. |
365 case DownloadItem::CANCELLED: | 365 case DownloadItem::CANCELLED: |
366 case DownloadItem::REMOVING: | |
367 case DownloadItem::INTERRUPTED: | 366 case DownloadItem::INTERRUPTED: |
368 RemovePendingDownload(download); | 367 RemovePendingDownload(download); |
369 break; | 368 break; |
370 | 369 |
371 default: | 370 default: |
372 NOTREACHED(); | 371 NOTREACHED(); |
373 } | 372 } |
374 | 373 |
375 DVLOG(1) << "Number of pending downloads=" << pending_downloads_.size(); | 374 DVLOG(1) << "Number of pending downloads=" << pending_downloads_.size(); |
376 } | 375 } |
377 | 376 |
| 377 void GDataDownloadObserver::OnDownloadDestroyed(DownloadItem* download) { |
| 378 RemovePendingDownload(download); |
| 379 } |
| 380 |
378 void GDataDownloadObserver::AddPendingDownload(DownloadItem* download) { | 381 void GDataDownloadObserver::AddPendingDownload(DownloadItem* download) { |
379 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
380 | 383 |
381 // Add ourself as an observer of this download if we've never seen it before. | 384 // Add ourself as an observer of this download if we've never seen it before. |
382 if (pending_downloads_.count(download->GetId()) == 0) { | 385 if (pending_downloads_.count(download->GetId()) == 0) { |
383 pending_downloads_[download->GetId()] = download; | 386 pending_downloads_[download->GetId()] = download; |
384 download->AddObserver(this); | 387 download->AddObserver(this); |
385 DVLOG(1) << "new download total bytes=" << download->GetTotalBytes() | 388 DVLOG(1) << "new download total bytes=" << download->GetTotalBytes() |
386 << ", full path=" << download->GetFullPath().value() | 389 << ", full path=" << download->GetFullPath().value() |
387 << ", mime type=" << download->GetMimeType(); | 390 << ", mime type=" << download->GetMimeType(); |
388 } | 391 } |
389 } | 392 } |
390 | 393 |
391 void GDataDownloadObserver::RemovePendingDownload(DownloadItem* download) { | 394 void GDataDownloadObserver::RemovePendingDownload(DownloadItem* download) { |
392 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 395 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
393 DCHECK(!download->IsInProgress()); | 396 DCHECK(!download->IsInProgress()); |
394 | 397 |
395 DownloadMap::iterator it = pending_downloads_.find(download->GetId()); | 398 DownloadMap::iterator it = pending_downloads_.find(download->GetId()); |
396 if (it != pending_downloads_.end()) { | 399 if (it != pending_downloads_.end()) { |
397 DetachFromDownload(download); | 400 DetachFromDownload(download); |
398 pending_downloads_.erase(it); | 401 pending_downloads_.erase(it); |
399 } | 402 } |
400 } | 403 } |
401 | 404 |
402 void GDataDownloadObserver::DetachFromDownload(DownloadItem* download) { | 405 void GDataDownloadObserver::DetachFromDownload(DownloadItem* download) { |
403 download->SetExternalData(&kUploadingKey, NULL); | 406 download->SetUserData(&kUploadingKey, NULL); |
404 download->SetExternalData(&kGDataPathKey, NULL); | 407 download->SetUserData(&kGDataPathKey, NULL); |
405 download->RemoveObserver(this); | 408 download->RemoveObserver(this); |
406 } | 409 } |
407 | 410 |
408 void GDataDownloadObserver::UploadDownloadItem(DownloadItem* download) { | 411 void GDataDownloadObserver::UploadDownloadItem(DownloadItem* download) { |
409 // Update metadata of ongoing upload. | 412 // Update metadata of ongoing upload. |
410 UpdateUpload(download); | 413 UpdateUpload(download); |
411 | 414 |
412 if (!ShouldUpload(download)) | 415 if (!ShouldUpload(download)) |
413 return; | 416 return; |
414 | 417 |
415 // Initialize uploading external data. | 418 // Initialize uploading external data. |
416 download->SetExternalData(&kUploadingKey, | 419 download->SetUserData(&kUploadingKey, |
417 new UploadingExternalData(gdata_uploader_)); | 420 new UploadingExternalData(gdata_uploader_)); |
418 | 421 |
419 // Create UploadFileInfo structure for the download item. | 422 // Create UploadFileInfo structure for the download item. |
420 CreateUploadFileInfo(download); | 423 CreateUploadFileInfo(download); |
421 } | 424 } |
422 | 425 |
423 void GDataDownloadObserver::UpdateUpload(DownloadItem* download) { | 426 void GDataDownloadObserver::UpdateUpload(DownloadItem* download) { |
424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
425 | 428 |
426 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 429 UploadingExternalData* upload_data = GetUploadingExternalData(download); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 // use the final target path when the download item is in COMPLETE state. | 573 // use the final target path when the download item is in COMPLETE state. |
571 file_system_->AddUploadedFile(UPLOAD_NEW_FILE, | 574 file_system_->AddUploadedFile(UPLOAD_NEW_FILE, |
572 upload_data->virtual_dir_path(), | 575 upload_data->virtual_dir_path(), |
573 entry.Pass(), | 576 entry.Pass(), |
574 download->GetTargetFilePath(), | 577 download->GetTargetFilePath(), |
575 GDataCache::FILE_OPERATION_MOVE, | 578 GDataCache::FILE_OPERATION_MOVE, |
576 base::Bind(&base::DoNothing)); | 579 base::Bind(&base::DoNothing)); |
577 } | 580 } |
578 | 581 |
579 } // namespace gdata | 582 } // namespace gdata |
OLD | NEW |