| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 GDataDownloadObserver::~GDataDownloadObserver() { | 81 GDataDownloadObserver::~GDataDownloadObserver() { |
| 82 if (download_manager_) | 82 if (download_manager_) |
| 83 download_manager_->RemoveObserver(this); | 83 download_manager_->RemoveObserver(this); |
| 84 | 84 |
| 85 for (DownloadMap::iterator iter = pending_downloads_.begin(); | 85 for (DownloadMap::iterator iter = pending_downloads_.begin(); |
| 86 iter != pending_downloads_.end(); ++iter) { | 86 iter != pending_downloads_.end(); ++iter) { |
| 87 DetachFromDownload(iter->second); | 87 DetachFromDownload(iter->second); |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 void GDataDownloadObserver::Initialize(const FilePath& temp_download_path, | 91 void GDataDownloadObserver::Initialize( |
| 92 GDataUploader* gdata_uploader, | 92 GDataUploader* gdata_uploader, |
| 93 DownloadManager* download_manager) { | 93 DownloadManager* download_manager, |
| 94 temp_download_path_ = temp_download_path; | 94 const FilePath& gdata_tmp_download_path) { |
| 95 DCHECK(gdata_uploader); |
| 96 DCHECK(!gdata_tmp_download_path.empty()); |
| 95 gdata_uploader_ = gdata_uploader; | 97 gdata_uploader_ = gdata_uploader; |
| 96 download_manager_ = download_manager; | 98 download_manager_ = download_manager; |
| 97 if (download_manager_) | 99 if (download_manager_) |
| 98 download_manager_->AddObserver(this); | 100 download_manager_->AddObserver(this); |
| 101 gdata_tmp_download_path_ = gdata_tmp_download_path; |
| 99 } | 102 } |
| 100 | 103 |
| 101 // static | 104 // static |
| 102 void GDataDownloadObserver::SetGDataPath(DownloadItem* download, | 105 void GDataDownloadObserver::SetGDataPath(DownloadItem* download, |
| 103 const FilePath& path) { | 106 const FilePath& path) { |
| 104 if (download) | 107 if (download) |
| 105 download->SetExternalData(&kGDataPathKey, | 108 download->SetExternalData(&kGDataPathKey, |
| 106 new GDataExternalData(path)); | 109 new GDataExternalData(path)); |
| 107 } | 110 } |
| 108 | 111 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 DownloadManager* download_manager) { | 180 DownloadManager* download_manager) { |
| 178 download_manager->RemoveObserver(this); | 181 download_manager->RemoveObserver(this); |
| 179 download_manager_ = NULL; | 182 download_manager_ = NULL; |
| 180 } | 183 } |
| 181 | 184 |
| 182 void GDataDownloadObserver::ModelChanged(DownloadManager* download_manager) { | 185 void GDataDownloadObserver::ModelChanged(DownloadManager* download_manager) { |
| 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 184 | 187 |
| 185 DownloadManager::DownloadVector downloads; | 188 DownloadManager::DownloadVector downloads; |
| 186 // GData downloads are considered temporary downloads. | 189 // GData downloads are considered temporary downloads. |
| 187 download_manager->GetTemporaryDownloads(temp_download_path_, | 190 download_manager->GetTemporaryDownloads(gdata_tmp_download_path_, |
| 188 &downloads); | 191 &downloads); |
| 189 for (size_t i = 0; i < downloads.size(); ++i) { | 192 for (size_t i = 0; i < downloads.size(); ++i) { |
| 190 // Only accept downloads that have the GData meta data associated with | 193 // Only accept downloads that have the GData meta data associated with |
| 191 // them. Otherwise we might trip over non-GData downloads being saved to | 194 // them. Otherwise we might trip over non-GData downloads being saved to |
| 192 // temp_download_path_. | 195 // gdata_tmp_download_path_. |
| 193 if (IsGDataDownload(downloads[i])) | 196 if (IsGDataDownload(downloads[i])) |
| 194 OnDownloadUpdated(downloads[i]); | 197 OnDownloadUpdated(downloads[i]); |
| 195 } | 198 } |
| 196 } | 199 } |
| 197 | 200 |
| 198 void GDataDownloadObserver::OnDownloadUpdated(DownloadItem* download) { | 201 void GDataDownloadObserver::OnDownloadUpdated(DownloadItem* download) { |
| 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 200 | 203 |
| 201 const DownloadItem::DownloadState state = download->GetState(); | 204 const DownloadItem::DownloadState state = download->GetState(); |
| 202 switch (state) { | 205 switch (state) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 download->RemoveObserver(this); | 256 download->RemoveObserver(this); |
| 254 } | 257 } |
| 255 | 258 |
| 256 void GDataDownloadObserver::UploadDownloadItem(DownloadItem* download) { | 259 void GDataDownloadObserver::UploadDownloadItem(DownloadItem* download) { |
| 257 // Update metadata of ongoing upload. | 260 // Update metadata of ongoing upload. |
| 258 UpdateUpload(download); | 261 UpdateUpload(download); |
| 259 | 262 |
| 260 if (!ShouldUpload(download)) | 263 if (!ShouldUpload(download)) |
| 261 return; | 264 return; |
| 262 | 265 |
| 263 UploadFileInfo* upload_file_info = CreateUploadFileInfo(download); | 266 scoped_ptr<UploadFileInfo> upload_file_info = CreateUploadFileInfo(download); |
| 264 gdata_uploader_->UploadFile(upload_file_info); | 267 const int upload_id = gdata_uploader_->UploadFile(upload_file_info.Pass()); |
| 265 | 268 |
| 269 // TODO(achuith): Fix this. |
| 266 // We won't know the upload ID until the after the GDataUploader::UploadFile() | 270 // We won't know the upload ID until the after the GDataUploader::UploadFile() |
| 267 // call. | 271 // call. |
| 268 download->SetExternalData(&kUploadingKey, | 272 download->SetExternalData(&kUploadingKey, |
| 269 new UploadingExternalData(gdata_uploader_, upload_file_info->upload_id)); | 273 new UploadingExternalData(gdata_uploader_, upload_id)); |
| 270 } | 274 } |
| 271 | 275 |
| 272 void GDataDownloadObserver::UpdateUpload(DownloadItem* download) { | 276 void GDataDownloadObserver::UpdateUpload(DownloadItem* download) { |
| 273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 274 | 278 |
| 275 UploadingExternalData* external_data = GetUploadingExternalData(download); | 279 UploadingExternalData* upload_data = GetUploadingExternalData(download); |
| 276 if (!external_data) { | 280 if (!upload_data) { |
| 277 DVLOG(1) << "No UploadingExternalData for download " << download->GetId(); | 281 DVLOG(1) << "No UploadingExternalData for download " << download->GetId(); |
| 278 return; | 282 return; |
| 279 } | 283 } |
| 280 | 284 |
| 281 gdata_uploader_->UpdateUpload(external_data->upload_id(), download); | 285 gdata_uploader_->UpdateUpload(upload_data->upload_id(), download); |
| 282 } | 286 } |
| 283 | 287 |
| 284 bool GDataDownloadObserver::ShouldUpload(DownloadItem* download) { | 288 bool GDataDownloadObserver::ShouldUpload(DownloadItem* download) { |
| 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 286 | 290 |
| 287 // Upload if the item is in pending_downloads_, | 291 // Upload if the item is in pending_downloads_, |
| 288 // is complete or large enough to stream, and, | 292 // is complete or large enough to stream, and, |
| 289 // is not already being uploaded. | 293 // is not already being uploaded. |
| 290 return pending_downloads_.count(download->GetId()) != 0 && | 294 return pending_downloads_.count(download->GetId()) != 0 && |
| 291 (download->AllDataSaved() || | 295 (download->AllDataSaved() || |
| 292 download->GetReceivedBytes() > kStreamingFileSize) && | 296 download->GetReceivedBytes() > kStreamingFileSize) && |
| 293 GetUploadingExternalData(download) == NULL; | 297 GetUploadingExternalData(download) == NULL; |
| 294 } | 298 } |
| 295 | 299 |
| 296 UploadFileInfo* GDataDownloadObserver::CreateUploadFileInfo( | 300 scoped_ptr<UploadFileInfo> GDataDownloadObserver::CreateUploadFileInfo( |
| 297 DownloadItem* download) { | 301 DownloadItem* download) { |
| 298 UploadFileInfo* upload_file_info = new UploadFileInfo(); | 302 scoped_ptr<UploadFileInfo> upload_file_info(new UploadFileInfo()); |
| 299 | 303 |
| 300 // GetFullPath will be a temporary location if we're streaming. | 304 // GetFullPath will be a temporary location if we're streaming. |
| 301 upload_file_info->file_path = download->GetFullPath(); | 305 upload_file_info->file_path = download->GetFullPath(); |
| 302 upload_file_info->file_size = download->GetReceivedBytes(); | 306 upload_file_info->file_size = download->GetReceivedBytes(); |
| 303 | 307 |
| 304 // Extract the final path from DownloadItem. | 308 // Extract the final path from DownloadItem. |
| 305 upload_file_info->gdata_path = GetGDataPath(download); | 309 upload_file_info->gdata_path = GetGDataPath(download); |
| 306 | 310 |
| 307 // Use the file name as the title. | 311 // Use the file name as the title. |
| 308 upload_file_info->title = upload_file_info->gdata_path.BaseName().value(); | 312 upload_file_info->title = upload_file_info->gdata_path.BaseName().value(); |
| 309 upload_file_info->content_type = download->GetMimeType(); | 313 upload_file_info->content_type = download->GetMimeType(); |
| 310 // GData api handles -1 as unknown file length. | 314 // GData api handles -1 as unknown file length. |
| 311 upload_file_info->content_length = download->AllDataSaved() ? | 315 upload_file_info->content_length = download->AllDataSaved() ? |
| 312 download->GetReceivedBytes() : -1; | 316 download->GetReceivedBytes() : -1; |
| 313 | 317 |
| 314 upload_file_info->all_bytes_present = download->AllDataSaved(); | 318 upload_file_info->all_bytes_present = download->AllDataSaved(); |
| 315 | 319 |
| 316 upload_file_info->completion_callback = | 320 upload_file_info->completion_callback = |
| 317 base::Bind(&GDataDownloadObserver::OnUploadComplete, | 321 base::Bind(&GDataDownloadObserver::OnUploadComplete, |
| 318 weak_ptr_factory_.GetWeakPtr(), download->GetId()); | 322 weak_ptr_factory_.GetWeakPtr(), |
| 323 download->GetId()); |
| 319 | 324 |
| 320 return upload_file_info; | 325 return upload_file_info.Pass(); |
| 321 } | 326 } |
| 322 | 327 |
| 323 void GDataDownloadObserver::OnUploadComplete(int32 download_id, | 328 void GDataDownloadObserver::OnUploadComplete(int32 download_id, |
| 324 base::PlatformFileError error, | 329 base::PlatformFileError error, |
| 325 DocumentEntry* unused_entry) { | 330 UploadFileInfo* upload_file_info) { |
| 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 327 DownloadMap::iterator iter = pending_downloads_.find(download_id); | 332 DownloadMap::iterator iter = pending_downloads_.find(download_id); |
| 328 if (iter == pending_downloads_.end()) { | 333 if (iter == pending_downloads_.end()) { |
| 329 DVLOG(1) << "Pending download not found" << download_id; | 334 DVLOG(1) << "Pending download not found" << download_id; |
| 330 return; | 335 return; |
| 331 } | 336 } |
| 332 DVLOG(1) << "Completing upload for download ID " << download_id; | 337 DVLOG(1) << "Completing upload for download ID " << download_id; |
| 333 DownloadItem* download = iter->second; | 338 DownloadItem* download = iter->second; |
| 334 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 339 UploadingExternalData* upload_data = GetUploadingExternalData(download); |
| 335 DCHECK(upload_data); | 340 DCHECK(upload_data); |
| 336 upload_data->MarkAsComplete(); | 341 upload_data->MarkAsComplete(); |
| 337 download->MaybeCompleteDownload(); | 342 download->MaybeCompleteDownload(); |
| 338 } | 343 } |
| 339 | 344 |
| 340 } // namespace gdata | 345 } // namespace gdata |
| OLD | NEW |