Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_download_observer.cc

Issue 10263019: DownloadManagerDelegate::ShouldCompleteDownload(callback) (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: merge Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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_callback_ = cb;
42 }
43
44 void CompleteDownload() {
45 is_complete_ = true;
46 if (!complete_callback_.is_null()) {
47 complete_callback_.Run();
48 complete_callback_.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_callback_;
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
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& complete_callback) {
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 upload_data->set_complete_callback(complete_callback);
157 return false;
137 } 158 }
138 159
139 // static 160 // static
140 int64 GDataDownloadObserver::GetUploadedBytes(DownloadItem* download) { 161 int64 GDataDownloadObserver::GetUploadedBytes(DownloadItem* download) {
141 UploadingExternalData* upload_data = GetUploadingExternalData(download); 162 UploadingExternalData* upload_data = GetUploadingExternalData(download);
142 if (!upload_data || !upload_data->uploader()) 163 if (!upload_data || !upload_data->uploader())
143 return 0; 164 return 0;
144 return upload_data->uploader()->GetUploadedBytes(upload_data->upload_id()); 165 return upload_data->uploader()->GetUploadedBytes(upload_data->upload_id());
145 } 166 }
146 167
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 void GDataDownloadObserver::OnUploadComplete(int32 download_id, 349 void GDataDownloadObserver::OnUploadComplete(int32 download_id,
329 base::PlatformFileError error, 350 base::PlatformFileError error,
330 UploadFileInfo* upload_file_info) { 351 UploadFileInfo* upload_file_info) {
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
332 DownloadMap::iterator iter = pending_downloads_.find(download_id); 353 DownloadMap::iterator iter = pending_downloads_.find(download_id);
333 if (iter == pending_downloads_.end()) { 354 if (iter == pending_downloads_.end()) {
334 DVLOG(1) << "Pending download not found" << download_id; 355 DVLOG(1) << "Pending download not found" << download_id;
335 return; 356 return;
336 } 357 }
337 DVLOG(1) << "Completing upload for download ID " << download_id; 358 DVLOG(1) << "Completing upload for download ID " << download_id;
338 DownloadItem* download = iter->second; 359 DownloadItem* download_item = iter->second;
339 UploadingExternalData* upload_data = GetUploadingExternalData(download); 360 UploadingExternalData* upload_data = GetUploadingExternalData(download_item);
340 DCHECK(upload_data); 361 DCHECK(upload_data);
341 upload_data->MarkAsComplete(); 362 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 } 363 }
352 364
353 } // namespace gdata 365 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698