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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 base::Bind(&OnEntryFound, profile, gdata_dir_path, | 162 base::Bind(&OnEntryFound, profile, gdata_dir_path, |
163 substitute_callback)); | 163 substitute_callback)); |
164 } else { | 164 } else { |
165 // TODO(achuith): Handle this. | 165 // TODO(achuith): Handle this. |
166 NOTREACHED(); | 166 NOTREACHED(); |
167 } | 167 } |
168 } | 168 } |
169 | 169 |
170 } // namespace | 170 } // namespace |
171 | 171 |
172 GDataDownloadObserver::GDataDownloadObserver() | 172 GDataDownloadObserver::GDataDownloadObserver( |
173 : gdata_uploader_(NULL), | 173 GDataUploader* uploader, |
174 GDataFileSystem* file_system) | |
175 : gdata_uploader_(uploader), | |
176 file_system_(file_system), | |
174 download_manager_(NULL), | 177 download_manager_(NULL), |
175 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 178 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
176 } | 179 } |
177 | 180 |
178 GDataDownloadObserver::~GDataDownloadObserver() { | 181 GDataDownloadObserver::~GDataDownloadObserver() { |
179 if (download_manager_) | 182 if (download_manager_) |
180 download_manager_->RemoveObserver(this); | 183 download_manager_->RemoveObserver(this); |
181 | 184 |
182 for (DownloadMap::iterator iter = pending_downloads_.begin(); | 185 for (DownloadMap::iterator iter = pending_downloads_.begin(); |
183 iter != pending_downloads_.end(); ++iter) { | 186 iter != pending_downloads_.end(); ++iter) { |
184 DetachFromDownload(iter->second); | 187 DetachFromDownload(iter->second); |
185 } | 188 } |
186 } | 189 } |
187 | 190 |
188 void GDataDownloadObserver::Initialize( | 191 void GDataDownloadObserver::Initialize( |
189 GDataUploader* gdata_uploader, | |
190 DownloadManager* download_manager, | 192 DownloadManager* download_manager, |
191 const FilePath& gdata_tmp_download_path) { | 193 const FilePath& gdata_tmp_download_path) { |
192 DCHECK(gdata_uploader); | |
193 DCHECK(!gdata_tmp_download_path.empty()); | 194 DCHECK(!gdata_tmp_download_path.empty()); |
194 gdata_uploader_ = gdata_uploader; | |
195 download_manager_ = download_manager; | 195 download_manager_ = download_manager; |
196 if (download_manager_) | 196 if (download_manager_) |
197 download_manager_->AddObserver(this); | 197 download_manager_->AddObserver(this); |
198 gdata_tmp_download_path_ = gdata_tmp_download_path; | 198 gdata_tmp_download_path_ = gdata_tmp_download_path; |
199 } | 199 } |
200 | 200 |
201 // static | 201 // static |
202 void GDataDownloadObserver::SubstituteGDataDownloadPath(Profile* profile, | 202 void GDataDownloadObserver::SubstituteGDataDownloadPath(Profile* profile, |
203 const FilePath& gdata_path, content::DownloadItem* download, | 203 const FilePath& gdata_path, content::DownloadItem* download, |
204 const SubstituteGDataDownloadPathCallback& callback) { | 204 const SubstituteGDataDownloadPathCallback& callback) { |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 download->GetId()); | 467 download->GetId()); |
468 | 468 |
469 return upload_file_info.Pass(); | 469 return upload_file_info.Pass(); |
470 } | 470 } |
471 | 471 |
472 void GDataDownloadObserver::OnUploadComplete( | 472 void GDataDownloadObserver::OnUploadComplete( |
473 int32 download_id, | 473 int32 download_id, |
474 base::PlatformFileError error, | 474 base::PlatformFileError error, |
475 scoped_ptr<UploadFileInfo> upload_file_info) { | 475 scoped_ptr<UploadFileInfo> upload_file_info) { |
476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
477 DCHECK(upload_file_info.get()); | |
478 | |
477 DownloadMap::iterator iter = pending_downloads_.find(download_id); | 479 DownloadMap::iterator iter = pending_downloads_.find(download_id); |
478 if (iter == pending_downloads_.end()) { | 480 if (iter == pending_downloads_.end()) { |
479 DVLOG(1) << "Pending download not found" << download_id; | 481 DVLOG(1) << "Pending download not found" << download_id; |
480 return; | 482 return; |
481 } | 483 } |
482 DVLOG(1) << "Completing upload for download ID " << download_id; | 484 DVLOG(1) << "Completing upload for download ID " << download_id; |
483 DownloadItem* download_item = iter->second; | 485 DownloadItem* download_item = iter->second; |
484 UploadingExternalData* upload_data = GetUploadingExternalData(download_item); | 486 UploadingExternalData* upload_data = GetUploadingExternalData(download_item); |
485 DCHECK(upload_data); | 487 DCHECK(upload_data); |
486 upload_data->CompleteDownload(); | 488 upload_data->CompleteDownload(); |
489 | |
490 // After the download item is complete, move the file to gdata cache. | |
491 if (error == base::PLATFORM_FILE_OK && upload_file_info->entry.get()) { | |
492 // Save a local copy of the UploadFileInfo pointer. Depending on order of | |
493 // argument evaluation, scoped_ptr<T>::release() may invalidate the scoped | |
494 // pointer before it can be dereferenced to access its members. | |
495 const UploadFileInfo* upload_file_info_ptr = upload_file_info.get(); | |
496 // Note that the CompleteDownload() call may rename the downloaded file so | |
497 // call GetTargetFilePath() here to obtain the final path. | |
498 const FilePath& file_content_path = download_item->GetTargetFilePath(); | |
achuithb
2012/07/10 16:58:44
Are we sure that the file has been renamed at this
hshi1
2012/07/10 22:43:36
You're right, the renaming is asynchronous. Observ
| |
499 file_system_->AddUploadedFile( | |
500 UPLOAD_NEW_FILE, | |
501 upload_file_info_ptr->gdata_path.DirName(), | |
502 upload_file_info_ptr->entry.get(), | |
503 file_content_path, | |
504 GDataCache::FILE_OPERATION_MOVE, | |
505 base::Bind(&base::DeletePointer<UploadFileInfo>, | |
506 upload_file_info.release())); | |
507 } | |
487 } | 508 } |
488 | 509 |
489 } // namespace gdata | 510 } // namespace gdata |
OLD | NEW |