| 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/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "chrome/browser/chromeos/gdata/gdata_upload_file_info.h" |
| 8 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" | 9 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" |
| 9 #include "chrome/browser/chromeos/gdata/gdata_upload_file_info.h" | |
| 10 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 10 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
| 11 #include "chrome/browser/download/download_completion_blocker.h" |
| 11 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
| 12 | 13 |
| 13 using content::BrowserThread; | 14 using content::BrowserThread; |
| 14 using content::DownloadManager; | 15 using content::DownloadManager; |
| 15 using content::DownloadItem; | 16 using content::DownloadItem; |
| 16 | 17 |
| 17 namespace gdata { | 18 namespace gdata { |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Threshold file size after which we stream the file. | 21 // Threshold file size after which we stream the file. |
| 21 const int64 kStreamingFileSize = 1 << 20; // 1MB | 22 const int64 kStreamingFileSize = 1 << 20; // 1MB |
| 22 | 23 |
| 23 // Keys for DownloadItem::ExternalData. | 24 // Keys for DownloadItem::ExternalData. |
| 24 const char kUploadingKey[] = "Uploading"; | 25 const char kUploadingKey[] = "Uploading"; |
| 25 const char kGDataPathKey[] = "GDataPath"; | 26 const char kGDataPathKey[] = "GDataPath"; |
| 26 | 27 |
| 27 // External Data stored in DownloadItem for ongoing uploads. | 28 // External Data stored in DownloadItem for ongoing uploads. |
| 28 class UploadingExternalData : public DownloadItem::ExternalData { | 29 class UploadingExternalData : public DownloadCompletionBlocker { |
| 29 public: | 30 public: |
| 30 UploadingExternalData(GDataUploader* uploader, int upload_id) | 31 UploadingExternalData(GDataUploader* uploader, int upload_id) |
| 31 : uploader_(uploader), | 32 : uploader_(uploader), |
| 32 upload_id_(upload_id), | 33 upload_id_(upload_id) { |
| 33 is_complete_(false) { | |
| 34 } | 34 } |
| 35 virtual ~UploadingExternalData() {} | 35 virtual ~UploadingExternalData() {} |
| 36 | 36 |
| 37 void MarkAsComplete() { is_complete_ = true; } | |
| 38 | |
| 39 int upload_id() const { return upload_id_; } | 37 int upload_id() const { return upload_id_; } |
| 40 bool is_complete() const { return is_complete_; } | |
| 41 GDataUploader* uploader() { return uploader_; } | 38 GDataUploader* uploader() { return uploader_; } |
| 42 | 39 |
| 43 private: | 40 private: |
| 44 GDataUploader* uploader_; | 41 GDataUploader* uploader_; |
| 45 int upload_id_; | 42 int upload_id_; |
| 46 bool is_complete_; | 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(UploadingExternalData); |
| 47 }; | 45 }; |
| 48 | 46 |
| 49 // External Data stored in DownloadItem for gdata path. | 47 // External Data stored in DownloadItem for gdata path. |
| 50 class GDataExternalData : public DownloadItem::ExternalData { | 48 class GDataExternalData : public DownloadItem::ExternalData { |
| 51 public: | 49 public: |
| 52 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} | 50 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} |
| 53 virtual ~GDataExternalData() {} | 51 virtual ~GDataExternalData() {} |
| 54 | 52 |
| 55 const FilePath& file_path() const { return file_path_; } | 53 const FilePath& file_path() const { return file_path_; } |
| 56 | 54 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 } | 117 } |
| 120 | 118 |
| 121 // static | 119 // static |
| 122 bool GDataDownloadObserver::IsGDataDownload(DownloadItem* download) { | 120 bool GDataDownloadObserver::IsGDataDownload(DownloadItem* download) { |
| 123 // We use the existence of the GDataExternalData object in download as a | 121 // We use the existence of the GDataExternalData object in download as a |
| 124 // signal that this is a GDataDownload. | 122 // signal that this is a GDataDownload. |
| 125 return !!GetGDataExternalData(download); | 123 return !!GetGDataExternalData(download); |
| 126 } | 124 } |
| 127 | 125 |
| 128 // static | 126 // static |
| 129 bool GDataDownloadObserver::IsReadyToComplete(DownloadItem* download) { | 127 bool GDataDownloadObserver::IsReadyToComplete( |
| 128 DownloadItem* download, |
| 129 const base::Closure& complete_callback) { |
| 130 // |download| is ready for completion (as far as GData is concerned) if: | 130 // |download| is ready for completion (as far as GData is concerned) if: |
| 131 // 1. It's not a GData download. | 131 // 1. It's not a GData download. |
| 132 // - or - | 132 // - or - |
| 133 // 2. The upload has completed. | 133 // 2. The upload has completed. |
| 134 if (!IsGDataDownload(download)) |
| 135 return true; |
| 134 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 136 UploadingExternalData* upload_data = GetUploadingExternalData(download); |
| 135 return !IsGDataDownload(download) || | 137 DCHECK(upload_data); |
| 136 (upload_data && upload_data->is_complete()); | 138 if (upload_data->is_complete()) |
| 139 return true; |
| 140 upload_data->set_complete_callback(complete_callback); |
| 141 return false; |
| 137 } | 142 } |
| 138 | 143 |
| 139 // static | 144 // static |
| 140 int64 GDataDownloadObserver::GetUploadedBytes(DownloadItem* download) { | |
| 141 UploadingExternalData* upload_data = GetUploadingExternalData(download); | |
| 142 if (!upload_data || !upload_data->uploader()) | |
| 143 return 0; | |
| 144 return upload_data->uploader()->GetUploadedBytes(upload_data->upload_id()); | |
| 145 } | |
| 146 | |
| 147 // static | |
| 148 int GDataDownloadObserver::PercentComplete(DownloadItem* download) { | 145 int GDataDownloadObserver::PercentComplete(DownloadItem* download) { |
| 149 // Progress is unknown until the upload starts. | 146 // Progress is unknown until the upload starts. |
| 150 if (!GetUploadingExternalData(download)) | 147 if (!GetUploadingExternalData(download)) |
| 151 return -1; | 148 return -1; |
| 152 int64 complete = GetUploadedBytes(download); | 149 int64 complete = GetUploadedBytes(download); |
| 153 // Once AllDataSaved() is true, we can use the GetReceivedBytes() as the total | 150 // Once AllDataSaved() is true, we can use the GetReceivedBytes() as the total |
| 154 // size. GetTotalBytes() may be set to 0 if there was a mismatch between the | 151 // size. GetTotalBytes() may be set to 0 if there was a mismatch between the |
| 155 // count of received bytes and the size of the download as given by the | 152 // count of received bytes and the size of the download as given by the |
| 156 // Content-Length header. | 153 // Content-Length header. |
| 157 int64 total = (download->AllDataSaved() ? download->GetReceivedBytes() : | 154 int64 total = (download->AllDataSaved() ? download->GetReceivedBytes() : |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 void GDataDownloadObserver::OnUploadComplete(int32 download_id, | 325 void GDataDownloadObserver::OnUploadComplete(int32 download_id, |
| 329 base::PlatformFileError error, | 326 base::PlatformFileError error, |
| 330 UploadFileInfo* upload_file_info) { | 327 UploadFileInfo* upload_file_info) { |
| 331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 328 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 332 DownloadMap::iterator iter = pending_downloads_.find(download_id); | 329 DownloadMap::iterator iter = pending_downloads_.find(download_id); |
| 333 if (iter == pending_downloads_.end()) { | 330 if (iter == pending_downloads_.end()) { |
| 334 DVLOG(1) << "Pending download not found" << download_id; | 331 DVLOG(1) << "Pending download not found" << download_id; |
| 335 return; | 332 return; |
| 336 } | 333 } |
| 337 DVLOG(1) << "Completing upload for download ID " << download_id; | 334 DVLOG(1) << "Completing upload for download ID " << download_id; |
| 338 DownloadItem* download = iter->second; | 335 DownloadItem* download_item = iter->second; |
| 339 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 336 UploadingExternalData* upload_data = GetUploadingExternalData(download_item); |
| 340 DCHECK(upload_data); | 337 DCHECK(upload_data); |
| 341 upload_data->MarkAsComplete(); | 338 upload_data->CompleteDownload(); |
| 342 download->MaybeCompleteDownload(); | |
| 343 // MaybeCompleteDownload() only works for non-SavePackage downloads. | |
| 344 // SavePackage::Finish() is the SavePackage equivalent of | |
| 345 // MaybeCompleteDownload(). MHTML SavePackages may upload to GData (see | |
| 346 // SavePackageFilePickerChromeOS), so MHTML SavePackages use | |
| 347 // OnDownloadUpdated() to wait for the upload to complete before calling | |
| 348 // Finish(). Call UpdateObservers() manually now in case this download is an | |
| 349 // MHTML SavePackage. | |
| 350 download->UpdateObservers(); | |
| 351 } | 339 } |
| 352 | 340 |
| 353 } // namespace gdata | 341 } // namespace gdata |
| OLD | NEW |