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_uploader.h" | 8 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" |
9 #include "chrome/browser/chromeos/gdata/gdata_upload_file_info.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" |
(...skipping 16 matching lines...) Expand all Loading... | |
27 // External Data stored in DownloadItem for ongoing uploads. | 27 // External Data stored in DownloadItem for ongoing uploads. |
28 class UploadingExternalData : public DownloadItem::ExternalData { | 28 class UploadingExternalData : public DownloadItem::ExternalData { |
29 public: | 29 public: |
30 UploadingExternalData(GDataUploader* uploader, int upload_id) | 30 UploadingExternalData(GDataUploader* uploader, int upload_id) |
31 : uploader_(uploader), | 31 : uploader_(uploader), |
32 upload_id_(upload_id), | 32 upload_id_(upload_id), |
33 is_complete_(false) { | 33 is_complete_(false) { |
34 } | 34 } |
35 virtual ~UploadingExternalData() {} | 35 virtual ~UploadingExternalData() {} |
36 | 36 |
37 void MarkAsComplete() { is_complete_ = true; } | 37 // |cb| will be called when this Upload is marked as complete. |
38 void set_complete_callback(const base::Closure& cb) { | |
39 if (!is_complete_) | |
40 complete_cb_ = cb; | |
41 } | |
42 | |
43 void MarkAsComplete() { | |
asanka
2012/05/01 15:54:10
Nit: Perhaps rename to something like CompleteDown
benjhayden
2012/05/01 18:02:52
Done.
| |
44 is_complete_ = true; | |
45 if (!complete_cb_.is_null()) { | |
46 complete_cb_.Run(); | |
47 complete_cb_.Reset(); | |
48 } | |
49 } | |
38 | 50 |
39 int upload_id() const { return upload_id_; } | 51 int upload_id() const { return upload_id_; } |
40 bool is_complete() const { return is_complete_; } | 52 bool is_complete() const { return is_complete_; } |
41 GDataUploader* uploader() { return uploader_; } | 53 GDataUploader* uploader() { return uploader_; } |
42 | 54 |
43 private: | 55 private: |
44 GDataUploader* uploader_; | 56 GDataUploader* uploader_; |
57 base::Closure complete_cb_; | |
45 int upload_id_; | 58 int upload_id_; |
46 bool is_complete_; | 59 bool is_complete_; |
47 }; | 60 }; |
48 | 61 |
49 // External Data stored in DownloadItem for gdata path. | 62 // External Data stored in DownloadItem for gdata path. |
50 class GDataExternalData : public DownloadItem::ExternalData { | 63 class GDataExternalData : public DownloadItem::ExternalData { |
51 public: | 64 public: |
52 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} | 65 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} |
53 virtual ~GDataExternalData() {} | 66 virtual ~GDataExternalData() {} |
54 | 67 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 } | 132 } |
120 | 133 |
121 // static | 134 // static |
122 bool GDataDownloadObserver::IsGDataDownload(DownloadItem* download) { | 135 bool GDataDownloadObserver::IsGDataDownload(DownloadItem* download) { |
123 // We use the existence of the GDataExternalData object in download as a | 136 // We use the existence of the GDataExternalData object in download as a |
124 // signal that this is a GDataDownload. | 137 // signal that this is a GDataDownload. |
125 return !!GetGDataExternalData(download); | 138 return !!GetGDataExternalData(download); |
126 } | 139 } |
127 | 140 |
128 // static | 141 // static |
129 bool GDataDownloadObserver::IsReadyToComplete(DownloadItem* download) { | 142 bool GDataDownloadObserver::IsReadyToComplete( |
143 DownloadItem* download, | |
144 const base::Closure& maybe_complete_download) { | |
asanka
2012/05/01 15:54:10
Nit: Just call it complete_cb or something generic
benjhayden
2012/05/01 18:02:52
Done.
| |
130 // |download| is ready for completion (as far as GData is concerned) if: | 145 // |download| is ready for completion (as far as GData is concerned) if: |
131 // 1. It's not a GData download. | 146 // 1. It's not a GData download. |
132 // - or - | 147 // - or - |
133 // 2. The upload has completed. | 148 // 2. The upload has completed. |
134 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 149 UploadingExternalData* upload_data = GetUploadingExternalData(download); |
135 return !IsGDataDownload(download) || | 150 if (!IsGDataDownload(download) || |
136 (upload_data && upload_data->is_complete()); | 151 (upload_data && upload_data->is_complete())) |
asanka
2012/05/01 15:54:10
Could upload_data be NULL at this point? It should
benjhayden
2012/05/01 18:02:52
Done.
| |
152 return true; | |
153 if (!maybe_complete_download.is_null()) | |
154 upload_data->set_complete_cb(maybe_complete_download); | |
155 return false; | |
137 } | 156 } |
138 | 157 |
139 // static | 158 // static |
140 int64 GDataDownloadObserver::GetUploadedBytes(DownloadItem* download) { | 159 int64 GDataDownloadObserver::GetUploadedBytes(DownloadItem* download) { |
141 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 160 UploadingExternalData* upload_data = GetUploadingExternalData(download); |
142 if (!upload_data || !upload_data->uploader()) | 161 if (!upload_data || !upload_data->uploader()) |
143 return 0; | 162 return 0; |
144 return upload_data->uploader()->GetUploadedBytes(upload_data->upload_id()); | 163 return upload_data->uploader()->GetUploadedBytes(upload_data->upload_id()); |
145 } | 164 } |
146 | 165 |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
328 void GDataDownloadObserver::OnUploadComplete(int32 download_id, | 347 void GDataDownloadObserver::OnUploadComplete(int32 download_id, |
329 base::PlatformFileError error, | 348 base::PlatformFileError error, |
330 UploadFileInfo* upload_file_info) { | 349 UploadFileInfo* upload_file_info) { |
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
332 DownloadMap::iterator iter = pending_downloads_.find(download_id); | 351 DownloadMap::iterator iter = pending_downloads_.find(download_id); |
333 if (iter == pending_downloads_.end()) { | 352 if (iter == pending_downloads_.end()) { |
334 DVLOG(1) << "Pending download not found" << download_id; | 353 DVLOG(1) << "Pending download not found" << download_id; |
335 return; | 354 return; |
336 } | 355 } |
337 DVLOG(1) << "Completing upload for download ID " << download_id; | 356 DVLOG(1) << "Completing upload for download ID " << download_id; |
338 DownloadItem* download = iter->second; | 357 DownloadItem* download_item = iter->second; |
339 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 358 UploadingExternalData* upload_data = GetUploadingExternalData(download_item); |
340 DCHECK(upload_data); | 359 DCHECK(upload_data); |
341 upload_data->MarkAsComplete(); | 360 upload_data->MarkAsComplete(); |
342 download->MaybeCompleteDownload(); | |
343 } | 361 } |
344 | 362 |
345 } // namespace gdata | 363 } // namespace gdata |
OLD | NEW |