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