| 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/drive/drive_download_observer.h" | 5 #include "chrome/browser/chromeos/drive/drive_download_observer.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/supports_user_data.h" | 12 #include "base/supports_user_data.h" |
| 13 #include "chrome/browser/chromeos/drive/drive.pb.h" | 13 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 14 #include "chrome/browser/chromeos/drive/drive_file_system_interface.h" | 14 #include "chrome/browser/chromeos/drive/drive_file_system_interface.h" |
| 15 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 15 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
| 16 #include "chrome/browser/chromeos/drive/drive_service_interface.h" | 16 #include "chrome/browser/chromeos/drive/drive_service_interface.h" |
| 17 #include "chrome/browser/chromeos/drive/drive_system_service.h" | 17 #include "chrome/browser/chromeos/drive/drive_system_service.h" |
| 18 #include "chrome/browser/download/download_completion_blocker.h" | 18 #include "chrome/browser/download/download_completion_blocker.h" |
| 19 #include "chrome/browser/google_apis/gdata_util.h" | 19 #include "chrome/browser/google_apis/gdata_util.h" |
| 20 #include "chrome/browser/google_apis/gdata_wapi_parser.h" | 20 #include "chrome/browser/google_apis/gdata_wapi_parser.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | 21 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 23 | 23 |
| 24 using content::BrowserThread; | 24 using content::BrowserThread; |
| 25 using content::DownloadManager; | 25 using content::DownloadManager; |
| 26 using content::DownloadItem; | 26 using content::DownloadItem; |
| 27 | 27 |
| 28 namespace gdata { | 28 namespace drive { |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // Threshold file size after which we stream the file. | 31 // Threshold file size after which we stream the file. |
| 32 const int64 kStreamingFileSize = 1 << 20; // 1MB | 32 const int64 kStreamingFileSize = 1 << 20; // 1MB |
| 33 | 33 |
| 34 // Keys for base::SupportsUserData::Data. | 34 // Keys for base::SupportsUserData::Data. |
| 35 const char kUploadingKey[] = "Uploading"; | 35 const char kUploadingKey[] = "Uploading"; |
| 36 const char kGDataPathKey[] = "GDataPath"; | 36 const char kGDataPathKey[] = "GDataPath"; |
| 37 | 37 |
| 38 // User Data stored in DownloadItem for ongoing uploads. | 38 // User Data stored in DownloadItem for ongoing uploads. |
| 39 class UploadingUserData : public DownloadCompletionBlocker { | 39 class UploadingUserData : public DownloadCompletionBlocker { |
| 40 public: | 40 public: |
| 41 explicit UploadingUserData(DriveUploader* uploader) | 41 explicit UploadingUserData(DriveUploader* uploader) |
| 42 : uploader_(uploader), | 42 : uploader_(uploader), |
| 43 upload_id_(-1), | 43 upload_id_(-1), |
| 44 is_overwrite_(false) { | 44 is_overwrite_(false) { |
| 45 } | 45 } |
| 46 virtual ~UploadingUserData() {} | 46 virtual ~UploadingUserData() {} |
| 47 | 47 |
| 48 DriveUploader* uploader() const { return uploader_; } | 48 DriveUploader* uploader() const { return uploader_; } |
| 49 void set_upload_id(int upload_id) { upload_id_ = upload_id; } | 49 void set_upload_id(int upload_id) { upload_id_ = upload_id; } |
| 50 int upload_id() const { return upload_id_; } | 50 int upload_id() const { return upload_id_; } |
| 51 void set_virtual_dir_path(const FilePath& path) { virtual_dir_path_ = path; } | 51 void set_virtual_dir_path(const FilePath& path) { virtual_dir_path_ = path; } |
| 52 const FilePath& virtual_dir_path() const { return virtual_dir_path_; } | 52 const FilePath& virtual_dir_path() const { return virtual_dir_path_; } |
| 53 void set_entry(scoped_ptr<DocumentEntry> entry) { entry_ = entry.Pass(); } | 53 void set_entry(scoped_ptr<gdata::DocumentEntry> entry) { |
| 54 scoped_ptr<DocumentEntry> entry_passed() { return entry_.Pass(); } | 54 entry_ = entry.Pass(); |
| 55 } |
| 56 scoped_ptr<gdata::DocumentEntry> entry_passed() { return entry_.Pass(); } |
| 55 void set_overwrite(bool overwrite) { is_overwrite_ = overwrite; } | 57 void set_overwrite(bool overwrite) { is_overwrite_ = overwrite; } |
| 56 bool is_overwrite() const { return is_overwrite_; } | 58 bool is_overwrite() const { return is_overwrite_; } |
| 57 void set_resource_id(const std::string& resource_id) { | 59 void set_resource_id(const std::string& resource_id) { |
| 58 resource_id_ = resource_id; | 60 resource_id_ = resource_id; |
| 59 } | 61 } |
| 60 const std::string& resource_id() const { return resource_id_; } | 62 const std::string& resource_id() const { return resource_id_; } |
| 61 void set_md5(const std::string& md5) { md5_ = md5; } | 63 void set_md5(const std::string& md5) { md5_ = md5; } |
| 62 const std::string& md5() const { return md5_; } | 64 const std::string& md5() const { return md5_; } |
| 63 | 65 |
| 64 private: | 66 private: |
| 65 DriveUploader* uploader_; | 67 DriveUploader* uploader_; |
| 66 int upload_id_; | 68 int upload_id_; |
| 67 FilePath virtual_dir_path_; | 69 FilePath virtual_dir_path_; |
| 68 scoped_ptr<DocumentEntry> entry_; | 70 scoped_ptr<gdata::DocumentEntry> entry_; |
| 69 bool is_overwrite_; | 71 bool is_overwrite_; |
| 70 std::string resource_id_; | 72 std::string resource_id_; |
| 71 std::string md5_; | 73 std::string md5_; |
| 72 | 74 |
| 73 DISALLOW_COPY_AND_ASSIGN(UploadingUserData); | 75 DISALLOW_COPY_AND_ASSIGN(UploadingUserData); |
| 74 }; | 76 }; |
| 75 | 77 |
| 76 // User Data stored in DownloadItem for drive path. | 78 // User Data stored in DownloadItem for drive path. |
| 77 class DriveUserData : public base::SupportsUserData::Data { | 79 class DriveUserData : public base::SupportsUserData::Data { |
| 78 public: | 80 public: |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 } else { | 174 } else { |
| 173 // TODO(achuith): Handle this. | 175 // TODO(achuith): Handle this. |
| 174 NOTREACHED(); | 176 NOTREACHED(); |
| 175 } | 177 } |
| 176 } | 178 } |
| 177 | 179 |
| 178 // Callback for DriveServiceInterface::Authenticate. | 180 // Callback for DriveServiceInterface::Authenticate. |
| 179 void OnAuthenticate(Profile* profile, | 181 void OnAuthenticate(Profile* profile, |
| 180 const FilePath& drive_path, | 182 const FilePath& drive_path, |
| 181 const base::Closure& substitute_callback, | 183 const base::Closure& substitute_callback, |
| 182 GDataErrorCode error, | 184 gdata::GDataErrorCode error, |
| 183 const std::string& token) { | 185 const std::string& token) { |
| 184 DVLOG(1) << "OnAuthenticate"; | 186 DVLOG(1) << "OnAuthenticate"; |
| 185 | 187 |
| 186 if (error == HTTP_SUCCESS) { | 188 if (error == gdata::HTTP_SUCCESS) { |
| 187 const FilePath drive_dir_path = | 189 const FilePath drive_dir_path = |
| 188 util::ExtractDrivePath(drive_path.DirName()); | 190 util::ExtractDrivePath(drive_path.DirName()); |
| 189 // Ensure the directory exists. This also forces DriveFileSystem to | 191 // Ensure the directory exists. This also forces DriveFileSystem to |
| 190 // initialize DriveRootDirectory. | 192 // initialize DriveRootDirectory. |
| 191 GetSystemService(profile)->file_system()->GetEntryInfoByPath( | 193 GetSystemService(profile)->file_system()->GetEntryInfoByPath( |
| 192 drive_dir_path, | 194 drive_dir_path, |
| 193 base::Bind(&OnEntryFound, profile, drive_dir_path, | 195 base::Bind(&OnEntryFound, profile, drive_dir_path, |
| 194 substitute_callback)); | 196 substitute_callback)); |
| 195 } else { | 197 } else { |
| 196 // TODO(achuith): Handle this. | 198 // TODO(achuith): Handle this. |
| (...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 | 659 |
| 658 // Store upload id in the uploading data structure. | 660 // Store upload id in the uploading data structure. |
| 659 upload_data->set_upload_id(upload_id); | 661 upload_data->set_upload_id(upload_id); |
| 660 } | 662 } |
| 661 | 663 |
| 662 void DriveDownloadObserver::OnUploadComplete( | 664 void DriveDownloadObserver::OnUploadComplete( |
| 663 int32 download_id, | 665 int32 download_id, |
| 664 DriveFileError error, | 666 DriveFileError error, |
| 665 const FilePath& drive_path, | 667 const FilePath& drive_path, |
| 666 const FilePath& file_path, | 668 const FilePath& file_path, |
| 667 scoped_ptr<DocumentEntry> document_entry) { | 669 scoped_ptr<gdata::DocumentEntry> document_entry) { |
| 668 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 670 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 669 DCHECK(document_entry.get()); | 671 DCHECK(document_entry.get()); |
| 670 | 672 |
| 671 // Look up the DownloadItem for the |download_id|. | 673 // Look up the DownloadItem for the |download_id|. |
| 672 DownloadMap::iterator iter = pending_downloads_.find(download_id); | 674 DownloadMap::iterator iter = pending_downloads_.find(download_id); |
| 673 if (iter == pending_downloads_.end()) { | 675 if (iter == pending_downloads_.end()) { |
| 674 DVLOG(1) << "Pending download not found" << download_id; | 676 DVLOG(1) << "Pending download not found" << download_id; |
| 675 return; | 677 return; |
| 676 } | 678 } |
| 677 DVLOG(1) << "Completing upload for download ID " << download_id; | 679 DVLOG(1) << "Completing upload for download ID " << download_id; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 691 | 693 |
| 692 void DriveDownloadObserver::MoveFileToDriveCache(DownloadItem* download) { | 694 void DriveDownloadObserver::MoveFileToDriveCache(DownloadItem* download) { |
| 693 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 695 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 694 | 696 |
| 695 UploadingUserData* upload_data = GetUploadingUserData(download); | 697 UploadingUserData* upload_data = GetUploadingUserData(download); |
| 696 if (!upload_data) { | 698 if (!upload_data) { |
| 697 NOTREACHED(); | 699 NOTREACHED(); |
| 698 return; | 700 return; |
| 699 } | 701 } |
| 700 | 702 |
| 701 // Pass ownership of the DocumentEntry object. | 703 // Pass ownership of the gdata::DocumentEntry object. |
| 702 scoped_ptr<DocumentEntry> entry = upload_data->entry_passed(); | 704 scoped_ptr<gdata::DocumentEntry> entry = upload_data->entry_passed(); |
| 703 if (!entry.get()) { | 705 if (!entry.get()) { |
| 704 NOTREACHED(); | 706 NOTREACHED(); |
| 705 return; | 707 return; |
| 706 } | 708 } |
| 707 | 709 |
| 708 if (upload_data->is_overwrite()) { | 710 if (upload_data->is_overwrite()) { |
| 709 file_system_->UpdateEntryData(upload_data->resource_id(), | 711 file_system_->UpdateEntryData(upload_data->resource_id(), |
| 710 upload_data->md5(), | 712 upload_data->md5(), |
| 711 entry.Pass(), | 713 entry.Pass(), |
| 712 download->GetTargetFilePath(), | 714 download->GetTargetFilePath(), |
| 713 base::Bind(&base::DoNothing)); | 715 base::Bind(&base::DoNothing)); |
| 714 } else { | 716 } else { |
| 715 // Move downloaded file to drive cache. Note that |content_file_path| should | 717 // Move downloaded file to drive cache. Note that |content_file_path| should |
| 716 // use the final target path (download->GetTargetFilePath()) when the | 718 // use the final target path (download->GetTargetFilePath()) when the |
| 717 // download item has transitioned to the DownloadItem::COMPLETE state. | 719 // download item has transitioned to the DownloadItem::COMPLETE state. |
| 718 file_system_->AddUploadedFile(UPLOAD_NEW_FILE, | 720 file_system_->AddUploadedFile(gdata::UPLOAD_NEW_FILE, |
| 719 upload_data->virtual_dir_path(), | 721 upload_data->virtual_dir_path(), |
| 720 entry.Pass(), | 722 entry.Pass(), |
| 721 download->GetTargetFilePath(), | 723 download->GetTargetFilePath(), |
| 722 DriveCache::FILE_OPERATION_MOVE, | 724 DriveCache::FILE_OPERATION_MOVE, |
| 723 base::Bind(&base::DoNothing)); | 725 base::Bind(&base::DoNothing)); |
| 724 } | 726 } |
| 725 } | 727 } |
| 726 | 728 |
| 727 DriveDownloadObserver::UploaderParams::UploaderParams() | 729 DriveDownloadObserver::UploaderParams::UploaderParams() |
| 728 : file_size(0), | 730 : file_size(0), |
| 729 content_length(-1), | 731 content_length(-1), |
| 730 all_bytes_present(false) { | 732 all_bytes_present(false) { |
| 731 } | 733 } |
| 732 | 734 |
| 733 DriveDownloadObserver::UploaderParams::~UploaderParams() { | 735 DriveDownloadObserver::UploaderParams::~UploaderParams() { |
| 734 } | 736 } |
| 735 | 737 |
| 736 // Useful for printf debugging. | 738 // Useful for printf debugging. |
| 737 std::string DriveDownloadObserver::UploaderParams::DebugString() const { | 739 std::string DriveDownloadObserver::UploaderParams::DebugString() const { |
| 738 return "title=[" + title + | 740 return "title=[" + title + |
| 739 "], file_path=[" + file_path.value() + | 741 "], file_path=[" + file_path.value() + |
| 740 "], content_type=[" + content_type + | 742 "], content_type=[" + content_type + |
| 741 "], content_length=[" + base::UintToString(content_length) + | 743 "], content_length=[" + base::UintToString(content_length) + |
| 742 "], upload_location=[" + upload_location.possibly_invalid_spec() + | 744 "], upload_location=[" + upload_location.possibly_invalid_spec() + |
| 743 "], drive_path=[" + drive_path.value() + | 745 "], drive_path=[" + drive_path.value() + |
| 744 "], all_bytes_present=[" + (all_bytes_present ? "true" : "false") + | 746 "], all_bytes_present=[" + (all_bytes_present ? "true" : "false") + |
| 745 "]"; | 747 "]"; |
| 746 } | 748 } |
| 747 | 749 |
| 748 } // namespace gdata | 750 } // namespace drive |
| OLD | NEW |