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 12 matching lines...) Expand all Loading... | |
23 | 23 |
24 namespace gdata { | 24 namespace gdata { |
25 namespace { | 25 namespace { |
26 | 26 |
27 // Threshold file size after which we stream the file. | 27 // Threshold file size after which we stream the file. |
28 const int64 kStreamingFileSize = 1 << 20; // 1MB | 28 const int64 kStreamingFileSize = 1 << 20; // 1MB |
29 | 29 |
30 // Keys for DownloadItem::ExternalData. | 30 // Keys for DownloadItem::ExternalData. |
31 const char kUploadingKey[] = "Uploading"; | 31 const char kUploadingKey[] = "Uploading"; |
32 const char kGDataPathKey[] = "GDataPath"; | 32 const char kGDataPathKey[] = "GDataPath"; |
33 const char kCachePathKey[] = "CachePath"; | |
33 | 34 |
34 // External Data stored in DownloadItem for ongoing uploads. | 35 // External Data stored in DownloadItem for ongoing uploads. |
35 class UploadingExternalData : public DownloadCompletionBlocker { | 36 class UploadingExternalData : public DownloadCompletionBlocker { |
36 public: | 37 public: |
37 UploadingExternalData(GDataUploader* uploader, int upload_id) | 38 UploadingExternalData(GDataUploader* uploader, int upload_id) |
38 : uploader_(uploader), | 39 : uploader_(uploader), |
39 upload_id_(upload_id) { | 40 upload_id_(upload_id) { |
40 } | 41 } |
41 virtual ~UploadingExternalData() {} | 42 virtual ~UploadingExternalData() {} |
42 | 43 |
(...skipping 12 matching lines...) Expand all Loading... | |
55 public: | 56 public: |
56 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} | 57 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} |
57 virtual ~GDataExternalData() {} | 58 virtual ~GDataExternalData() {} |
58 | 59 |
59 const FilePath& file_path() const { return file_path_; } | 60 const FilePath& file_path() const { return file_path_; } |
60 | 61 |
61 private: | 62 private: |
62 FilePath file_path_; | 63 FilePath file_path_; |
63 }; | 64 }; |
64 | 65 |
66 // External Data stored in DownloadItem for information required to move the | |
67 // downloaded file to gdata cache. | |
68 class GDataCacheExternalData : public DownloadItem::ExternalData { | |
achuithb
2012/07/10 23:04:35
Why not add entry_ to UploadingExternalData? It's
hshi1
2012/07/10 23:46:57
Sounds good, done.
On 2012/07/10 23:04:35, achuith
| |
69 public: | |
70 explicit GDataCacheExternalData(const FilePath& virtual_dir_path, | |
71 scoped_ptr<DocumentEntry> entry) | |
72 : virtual_dir_path_(virtual_dir_path), | |
73 entry_(entry.release()) { | |
74 } | |
75 virtual ~GDataCacheExternalData() {} | |
76 | |
77 const FilePath& virtual_dir_path() const { return virtual_dir_path_; } | |
78 DocumentEntry* entry() const { return entry_.get(); } | |
achuithb
2012/07/10 23:04:35
Is this used?
hshi1
2012/07/10 23:46:57
Yes it is used in a DCHECK in MoveFileToGDataCache
| |
79 scoped_ptr<DocumentEntry> entry_passed() { return entry_.Pass(); } | |
80 | |
81 private: | |
82 FilePath virtual_dir_path_; | |
83 scoped_ptr<DocumentEntry> entry_; | |
84 }; | |
85 | |
65 // Extracts UploadingExternalData* from |download|. | 86 // Extracts UploadingExternalData* from |download|. |
66 UploadingExternalData* GetUploadingExternalData(DownloadItem* download) { | 87 UploadingExternalData* GetUploadingExternalData(DownloadItem* download) { |
67 return static_cast<UploadingExternalData*>( | 88 return static_cast<UploadingExternalData*>( |
68 download->GetExternalData(&kUploadingKey)); | 89 download->GetExternalData(&kUploadingKey)); |
69 } | 90 } |
70 | 91 |
71 // Extracts GDataExternalData* from |download|. | 92 // Extracts GDataExternalData* from |download|. |
72 GDataExternalData* GetGDataExternalData(DownloadItem* download) { | 93 GDataExternalData* GetGDataExternalData(DownloadItem* download) { |
73 return static_cast<GDataExternalData*>( | 94 return static_cast<GDataExternalData*>( |
74 download->GetExternalData(&kGDataPathKey)); | 95 download->GetExternalData(&kGDataPathKey)); |
75 } | 96 } |
76 | 97 |
98 // Extracts GDataCacheExternalData* from |download|. | |
99 GDataCacheExternalData* GetGDataCacheExternalData(DownloadItem* download) { | |
100 return static_cast<GDataCacheExternalData*>( | |
101 download->GetExternalData(&kCachePathKey)); | |
102 } | |
103 | |
77 void RunSubstituteGDataDownloadCallback( | 104 void RunSubstituteGDataDownloadCallback( |
78 const GDataDownloadObserver::SubstituteGDataDownloadPathCallback& callback, | 105 const GDataDownloadObserver::SubstituteGDataDownloadPathCallback& callback, |
79 const FilePath* file_path) { | 106 const FilePath* file_path) { |
80 callback.Run(*file_path); | 107 callback.Run(*file_path); |
81 } | 108 } |
82 | 109 |
83 gdata::GDataSystemService* GetSystemService(Profile* profile) { | 110 gdata::GDataSystemService* GetSystemService(Profile* profile) { |
84 gdata::GDataSystemService* system_service = | 111 gdata::GDataSystemService* system_service = |
85 gdata::GDataSystemServiceFactory::GetForProfile( | 112 gdata::GDataSystemServiceFactory::GetForProfile( |
86 profile ? profile : ProfileManager::GetDefaultProfile()); | 113 profile ? profile : ProfileManager::GetDefaultProfile()); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 base::Bind(&OnEntryFound, profile, gdata_dir_path, | 189 base::Bind(&OnEntryFound, profile, gdata_dir_path, |
163 substitute_callback)); | 190 substitute_callback)); |
164 } else { | 191 } else { |
165 // TODO(achuith): Handle this. | 192 // TODO(achuith): Handle this. |
166 NOTREACHED(); | 193 NOTREACHED(); |
167 } | 194 } |
168 } | 195 } |
169 | 196 |
170 } // namespace | 197 } // namespace |
171 | 198 |
172 GDataDownloadObserver::GDataDownloadObserver() | 199 GDataDownloadObserver::GDataDownloadObserver( |
173 : gdata_uploader_(NULL), | 200 GDataUploader* uploader, |
201 GDataFileSystem* file_system) | |
202 : gdata_uploader_(uploader), | |
203 file_system_(file_system), | |
174 download_manager_(NULL), | 204 download_manager_(NULL), |
175 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 205 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
176 } | 206 } |
177 | 207 |
178 GDataDownloadObserver::~GDataDownloadObserver() { | 208 GDataDownloadObserver::~GDataDownloadObserver() { |
179 if (download_manager_) | 209 if (download_manager_) |
180 download_manager_->RemoveObserver(this); | 210 download_manager_->RemoveObserver(this); |
181 | 211 |
182 for (DownloadMap::iterator iter = pending_downloads_.begin(); | 212 for (DownloadMap::iterator iter = pending_downloads_.begin(); |
183 iter != pending_downloads_.end(); ++iter) { | 213 iter != pending_downloads_.end(); ++iter) { |
184 DetachFromDownload(iter->second); | 214 DetachFromDownload(iter->second); |
185 } | 215 } |
186 } | 216 } |
187 | 217 |
188 void GDataDownloadObserver::Initialize( | 218 void GDataDownloadObserver::Initialize( |
189 GDataUploader* gdata_uploader, | |
190 DownloadManager* download_manager, | 219 DownloadManager* download_manager, |
191 const FilePath& gdata_tmp_download_path) { | 220 const FilePath& gdata_tmp_download_path) { |
192 DCHECK(gdata_uploader); | |
193 DCHECK(!gdata_tmp_download_path.empty()); | 221 DCHECK(!gdata_tmp_download_path.empty()); |
194 gdata_uploader_ = gdata_uploader; | |
195 download_manager_ = download_manager; | 222 download_manager_ = download_manager; |
196 if (download_manager_) | 223 if (download_manager_) |
197 download_manager_->AddObserver(this); | 224 download_manager_->AddObserver(this); |
198 gdata_tmp_download_path_ = gdata_tmp_download_path; | 225 gdata_tmp_download_path_ = gdata_tmp_download_path; |
199 } | 226 } |
200 | 227 |
201 // static | 228 // static |
202 void GDataDownloadObserver::SubstituteGDataDownloadPath(Profile* profile, | 229 void GDataDownloadObserver::SubstituteGDataDownloadPath(Profile* profile, |
203 const FilePath& gdata_path, content::DownloadItem* download, | 230 const FilePath& gdata_path, content::DownloadItem* download, |
204 const SubstituteGDataDownloadPathCallback& callback) { | 231 const SubstituteGDataDownloadPathCallback& callback) { |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
346 | 373 |
347 const DownloadItem::DownloadState state = download->GetState(); | 374 const DownloadItem::DownloadState state = download->GetState(); |
348 switch (state) { | 375 switch (state) { |
349 case DownloadItem::IN_PROGRESS: | 376 case DownloadItem::IN_PROGRESS: |
350 AddPendingDownload(download); | 377 AddPendingDownload(download); |
351 UploadDownloadItem(download); | 378 UploadDownloadItem(download); |
352 break; | 379 break; |
353 | 380 |
354 case DownloadItem::COMPLETE: | 381 case DownloadItem::COMPLETE: |
355 UploadDownloadItem(download); | 382 UploadDownloadItem(download); |
383 MoveDownloadedFileToCache(download); | |
356 RemovePendingDownload(download); | 384 RemovePendingDownload(download); |
357 break; | 385 break; |
358 | 386 |
359 // TODO(achuith): Stop the pending upload and delete the file. | 387 // TODO(achuith): Stop the pending upload and delete the file. |
360 case DownloadItem::CANCELLED: | 388 case DownloadItem::CANCELLED: |
361 case DownloadItem::REMOVING: | 389 case DownloadItem::REMOVING: |
362 case DownloadItem::INTERRUPTED: | 390 case DownloadItem::INTERRUPTED: |
363 RemovePendingDownload(download); | 391 RemovePendingDownload(download); |
364 break; | 392 break; |
365 | 393 |
366 default: | 394 default: |
367 NOTREACHED(); | 395 NOTREACHED(); |
368 } | 396 } |
397 | |
369 DVLOG(1) << "Number of pending downloads=" << pending_downloads_.size(); | 398 DVLOG(1) << "Number of pending downloads=" << pending_downloads_.size(); |
370 } | 399 } |
371 | 400 |
372 void GDataDownloadObserver::AddPendingDownload(DownloadItem* download) { | 401 void GDataDownloadObserver::AddPendingDownload(DownloadItem* download) { |
373 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
374 | 403 |
375 // Add ourself as an observer of this download if we've never seen it before. | 404 // Add ourself as an observer of this download if we've never seen it before. |
376 if (pending_downloads_.count(download->GetId()) == 0) { | 405 if (pending_downloads_.count(download->GetId()) == 0) { |
377 pending_downloads_[download->GetId()] = download; | 406 pending_downloads_[download->GetId()] = download; |
378 download->AddObserver(this); | 407 download->AddObserver(this); |
(...skipping 10 matching lines...) Expand all Loading... | |
389 DownloadMap::iterator it = pending_downloads_.find(download->GetId()); | 418 DownloadMap::iterator it = pending_downloads_.find(download->GetId()); |
390 if (it != pending_downloads_.end()) { | 419 if (it != pending_downloads_.end()) { |
391 DetachFromDownload(download); | 420 DetachFromDownload(download); |
392 pending_downloads_.erase(it); | 421 pending_downloads_.erase(it); |
393 } | 422 } |
394 } | 423 } |
395 | 424 |
396 void GDataDownloadObserver::DetachFromDownload(DownloadItem* download) { | 425 void GDataDownloadObserver::DetachFromDownload(DownloadItem* download) { |
397 download->SetExternalData(&kUploadingKey, NULL); | 426 download->SetExternalData(&kUploadingKey, NULL); |
398 download->SetExternalData(&kGDataPathKey, NULL); | 427 download->SetExternalData(&kGDataPathKey, NULL); |
428 download->SetExternalData(&kCachePathKey, NULL); | |
399 download->RemoveObserver(this); | 429 download->RemoveObserver(this); |
400 } | 430 } |
401 | 431 |
402 void GDataDownloadObserver::UploadDownloadItem(DownloadItem* download) { | 432 void GDataDownloadObserver::UploadDownloadItem(DownloadItem* download) { |
403 // Update metadata of ongoing upload. | 433 // Update metadata of ongoing upload. |
404 UpdateUpload(download); | 434 UpdateUpload(download); |
405 | 435 |
406 if (!ShouldUpload(download)) | 436 if (!ShouldUpload(download)) |
407 return; | 437 return; |
408 | 438 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 download->GetId()); | 497 download->GetId()); |
468 | 498 |
469 return upload_file_info.Pass(); | 499 return upload_file_info.Pass(); |
470 } | 500 } |
471 | 501 |
472 void GDataDownloadObserver::OnUploadComplete( | 502 void GDataDownloadObserver::OnUploadComplete( |
473 int32 download_id, | 503 int32 download_id, |
474 base::PlatformFileError error, | 504 base::PlatformFileError error, |
475 scoped_ptr<UploadFileInfo> upload_file_info) { | 505 scoped_ptr<UploadFileInfo> upload_file_info) { |
476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
507 DCHECK(upload_file_info.get()); | |
508 DCHECK(upload_file_info->entry.get()); | |
509 | |
510 // Look up the DownloadItem for the |download_id|. | |
477 DownloadMap::iterator iter = pending_downloads_.find(download_id); | 511 DownloadMap::iterator iter = pending_downloads_.find(download_id); |
478 if (iter == pending_downloads_.end()) { | 512 if (iter == pending_downloads_.end()) { |
479 DVLOG(1) << "Pending download not found" << download_id; | 513 DVLOG(1) << "Pending download not found" << download_id; |
480 return; | 514 return; |
481 } | 515 } |
482 DVLOG(1) << "Completing upload for download ID " << download_id; | 516 DVLOG(1) << "Completing upload for download ID " << download_id; |
483 DownloadItem* download_item = iter->second; | 517 DownloadItem* download_item = iter->second; |
518 | |
519 // Fill GDataCacheExternalData struct for use by MoveDownloadedFileToCache(). | |
520 download_item->SetExternalData(&kCachePathKey, | |
521 new GDataCacheExternalData( | |
522 upload_file_info->gdata_path.DirName(), | |
523 upload_file_info->entry.Pass())); | |
524 | |
525 // Allow the download item to complete. | |
484 UploadingExternalData* upload_data = GetUploadingExternalData(download_item); | 526 UploadingExternalData* upload_data = GetUploadingExternalData(download_item); |
485 DCHECK(upload_data); | 527 DCHECK(upload_data); |
486 upload_data->CompleteDownload(); | 528 upload_data->CompleteDownload(); |
487 } | 529 } |
488 | 530 |
531 void GDataDownloadObserver::MoveDownloadedFileToCache(DownloadItem* download) { | |
532 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
533 | |
534 GDataCacheExternalData* data = GetGDataCacheExternalData(download); | |
535 if (!data || !data->entry()) { | |
536 NOTREACHED(); | |
537 return; | |
538 } | |
539 | |
540 // Take ownership of the DocumentEntry object. It will be released upon | |
541 // completion of the AddUploadedFile() call below. | |
542 DocumentEntry* entry = data->entry_passed().release(); | |
543 | |
544 // Move downloaded file to gdata cache. Note that |content_file_path| should | |
545 // use the final target path when the download item is in COMPLETE state. | |
546 file_system_->AddUploadedFile(UPLOAD_NEW_FILE, | |
547 data->virtual_dir_path(), | |
548 entry, | |
549 download->GetTargetFilePath(), | |
550 GDataCache::FILE_OPERATION_MOVE, | |
551 base::Bind(&base::DeletePointer<DocumentEntry>, | |
552 entry)); | |
553 } | |
554 | |
489 } // namespace gdata | 555 } // namespace gdata |
OLD | NEW |