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 // Be very careful to avoid calling cb twice, or more than one cb! | |
40 if (!is_complete_) | |
41 complete_cb_ = cb; | |
42 } | |
43 | |
44 void CompleteDownload() { | |
45 is_complete_ = true; | |
46 if (!complete_cb_.is_null()) { | |
47 complete_cb_.Run(); | |
48 complete_cb_.Reset(); | |
49 } | |
50 } | |
38 | 51 |
39 int upload_id() const { return upload_id_; } | 52 int upload_id() const { return upload_id_; } |
40 bool is_complete() const { return is_complete_; } | 53 bool is_complete() const { return is_complete_; } |
41 GDataUploader* uploader() { return uploader_; } | 54 GDataUploader* uploader() { return uploader_; } |
42 | 55 |
43 private: | 56 private: |
44 GDataUploader* uploader_; | 57 GDataUploader* uploader_; |
58 base::Closure complete_cb_; | |
45 int upload_id_; | 59 int upload_id_; |
46 bool is_complete_; | 60 bool is_complete_; |
47 }; | 61 }; |
48 | 62 |
49 // External Data stored in DownloadItem for gdata path. | 63 // External Data stored in DownloadItem for gdata path. |
50 class GDataExternalData : public DownloadItem::ExternalData { | 64 class GDataExternalData : public DownloadItem::ExternalData { |
51 public: | 65 public: |
52 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} | 66 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} |
53 virtual ~GDataExternalData() {} | 67 virtual ~GDataExternalData() {} |
54 | 68 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 } | 133 } |
120 | 134 |
121 // static | 135 // static |
122 bool GDataDownloadObserver::IsGDataDownload(DownloadItem* download) { | 136 bool GDataDownloadObserver::IsGDataDownload(DownloadItem* download) { |
123 // We use the existence of the GDataExternalData object in download as a | 137 // We use the existence of the GDataExternalData object in download as a |
124 // signal that this is a GDataDownload. | 138 // signal that this is a GDataDownload. |
125 return !!GetGDataExternalData(download); | 139 return !!GetGDataExternalData(download); |
126 } | 140 } |
127 | 141 |
128 // static | 142 // static |
129 bool GDataDownloadObserver::IsReadyToComplete(DownloadItem* download) { | 143 bool GDataDownloadObserver::IsReadyToComplete( |
144 DownloadItem* download, | |
145 const base::Closure& maybe_complete_download) { | |
Randy Smith (Not in Mondays)
2012/05/01 18:21:49
Same naming issue. The phrase "maybe complete dow
benjhayden
2012/05/01 19:00:30
Done.
| |
130 // |download| is ready for completion (as far as GData is concerned) if: | 146 // |download| is ready for completion (as far as GData is concerned) if: |
131 // 1. It's not a GData download. | 147 // 1. It's not a GData download. |
132 // - or - | 148 // - or - |
133 // 2. The upload has completed. | 149 // 2. The upload has completed. |
150 if (!IsGDataDownload(download)) | |
151 return true; | |
134 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 152 UploadingExternalData* upload_data = GetUploadingExternalData(download); |
135 return !IsGDataDownload(download) || | 153 DCHECK(upload_data); |
136 (upload_data && upload_data->is_complete()); | 154 if (upload_data->is_complete()) |
155 return true; | |
156 if (!maybe_complete_download.is_null()) | |
Randy Smith (Not in Mondays)
2012/05/01 18:21:49
Don't you want this to be unilateral? Doing it th
benjhayden
2012/05/01 19:00:30
I did this intentionally in case another caller ca
Randy Smith (Not in Mondays)
2012/05/02 18:14:07
This feels like a hacky interface design--we're no
benjhayden
2012/05/03 19:21:14
Done.
| |
157 upload_data->set_complete_callback(maybe_complete_download); | |
158 return false; | |
137 } | 159 } |
138 | 160 |
139 // static | 161 // static |
140 int64 GDataDownloadObserver::GetUploadedBytes(DownloadItem* download) { | 162 int64 GDataDownloadObserver::GetUploadedBytes(DownloadItem* download) { |
141 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 163 UploadingExternalData* upload_data = GetUploadingExternalData(download); |
142 if (!upload_data || !upload_data->uploader()) | 164 if (!upload_data || !upload_data->uploader()) |
143 return 0; | 165 return 0; |
144 return upload_data->uploader()->GetUploadedBytes(upload_data->upload_id()); | 166 return upload_data->uploader()->GetUploadedBytes(upload_data->upload_id()); |
145 } | 167 } |
146 | 168 |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
328 void GDataDownloadObserver::OnUploadComplete(int32 download_id, | 350 void GDataDownloadObserver::OnUploadComplete(int32 download_id, |
329 base::PlatformFileError error, | 351 base::PlatformFileError error, |
330 UploadFileInfo* upload_file_info) { | 352 UploadFileInfo* upload_file_info) { |
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
332 DownloadMap::iterator iter = pending_downloads_.find(download_id); | 354 DownloadMap::iterator iter = pending_downloads_.find(download_id); |
333 if (iter == pending_downloads_.end()) { | 355 if (iter == pending_downloads_.end()) { |
334 DVLOG(1) << "Pending download not found" << download_id; | 356 DVLOG(1) << "Pending download not found" << download_id; |
335 return; | 357 return; |
336 } | 358 } |
337 DVLOG(1) << "Completing upload for download ID " << download_id; | 359 DVLOG(1) << "Completing upload for download ID " << download_id; |
338 DownloadItem* download = iter->second; | 360 DownloadItem* download_item = iter->second; |
339 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 361 UploadingExternalData* upload_data = GetUploadingExternalData(download_item); |
340 DCHECK(upload_data); | 362 DCHECK(upload_data); |
341 upload_data->MarkAsComplete(); | 363 upload_data->CompleteDownload(); |
342 download->MaybeCompleteDownload(); | |
343 } | 364 } |
344 | 365 |
345 } // namespace gdata | 366 } // namespace gdata |
OLD | NEW |