| 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" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 virtual ~DriveUserData() {} | 84 virtual ~DriveUserData() {} |
| 85 | 85 |
| 86 const FilePath& file_path() const { return file_path_; } | 86 const FilePath& file_path() const { return file_path_; } |
| 87 | 87 |
| 88 private: | 88 private: |
| 89 FilePath file_path_; | 89 FilePath file_path_; |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 // Extracts UploadingUserData* from |download|. | 92 // Extracts UploadingUserData* from |download|. |
| 93 UploadingUserData* GetUploadingUserData(DownloadItem* download) { | 93 UploadingUserData* GetUploadingUserData(DownloadItem* download) { |
| 94 return static_cast<UploadingUserData*>( | 94 return static_cast<UploadingUserData*>(download->GetUserData(&kUploadingKey)); |
| 95 download->GetUserData(&kUploadingKey)); | |
| 96 } | 95 } |
| 97 | 96 |
| 98 const UploadingUserData* GetUploadingUserData(const DownloadItem* download) { | 97 const UploadingUserData* GetUploadingUserData(const DownloadItem* download) { |
| 99 return static_cast<const UploadingUserData*>( | 98 return static_cast<const UploadingUserData*>( |
| 100 download->GetUserData(&kUploadingKey)); | 99 download->GetUserData(&kUploadingKey)); |
| 101 } | 100 } |
| 102 | 101 |
| 103 // Extracts DriveUserData* from |download|. | 102 // Extracts DriveUserData* from |download|. |
| 104 DriveUserData* GetDriveUserData(DownloadItem* download) { | 103 DriveUserData* GetDriveUserData(DownloadItem* download) { |
| 105 return static_cast<DriveUserData*>( | 104 return static_cast<DriveUserData*>(download->GetUserData(&kGDataPathKey)); |
| 106 download->GetUserData(&kGDataPathKey)); | |
| 107 } | 105 } |
| 108 | 106 |
| 109 const DriveUserData* GetDriveUserData(const DownloadItem* download) { | 107 const DriveUserData* GetDriveUserData(const DownloadItem* download) { |
| 110 return static_cast<const DriveUserData*>( | 108 return static_cast<const DriveUserData*>( |
| 111 download->GetUserData(&kGDataPathKey)); | 109 download->GetUserData(&kGDataPathKey)); |
| 112 } | 110 } |
| 113 | 111 |
| 114 void RunSubstituteDriveDownloadCallback( | 112 void RunSubstituteDriveDownloadCallback( |
| 115 const DriveDownloadObserver::SubstituteDriveDownloadPathCallback& callback, | 113 const DriveDownloadObserver::SubstituteDriveDownloadPathCallback& callback, |
| 116 const FilePath* file_path) { | 114 const FilePath* file_path) { |
| 117 callback.Run(*file_path); | 115 callback.Run(*file_path); |
| 118 } | 116 } |
| 119 | 117 |
| 120 DriveSystemService* GetSystemService(Profile* profile) { | 118 DriveSystemService* GetSystemService(Profile* profile) { |
| 121 DriveSystemService* system_service = | 119 DriveSystemService* system_service = |
| 122 DriveSystemServiceFactory::GetForProfile( | 120 DriveSystemServiceFactory::GetForProfile( |
| 123 profile ? profile : ProfileManager::GetDefaultProfile()); | 121 profile ? profile : ProfileManager::GetDefaultProfile()); |
| 124 DCHECK(system_service); | 122 DCHECK(system_service); |
| 125 return system_service; | 123 return system_service; |
| 126 } | 124 } |
| 127 | 125 |
| 126 // Creates a temporary file |drive_tmp_download_path| in |
| 127 // |drive_tmp_download_dir|. Must be called on a thread that allows file |
| 128 // operations. |
| 129 void GetDriveTempDownloadPath(const FilePath& drive_tmp_download_dir, |
| 130 FilePath* drive_tmp_download_path) { |
| 131 bool created = file_util::CreateDirectory(drive_tmp_download_dir); |
| 132 DCHECK(created) << "Can not create temp download directory at " |
| 133 << drive_tmp_download_dir.value(); |
| 134 created = file_util::CreateTemporaryFileInDir(drive_tmp_download_dir, |
| 135 drive_tmp_download_path); |
| 136 DCHECK(created) << "Temporary download file creation failed"; |
| 137 } |
| 138 |
| 128 // Substitutes virtual drive path for local temporary path. | 139 // Substitutes virtual drive path for local temporary path. |
| 129 void SubstituteDriveDownloadPathInternal( | 140 void SubstituteDriveDownloadPathInternal( |
| 130 Profile* profile, | 141 Profile* profile, |
| 131 const DriveDownloadObserver::SubstituteDriveDownloadPathCallback& | 142 const DriveDownloadObserver::SubstituteDriveDownloadPathCallback& |
| 132 callback) { | 143 callback) { |
| 133 DVLOG(1) << "SubstituteDriveDownloadPathInternal"; | 144 DVLOG(1) << "SubstituteDriveDownloadPathInternal"; |
| 134 | 145 |
| 135 const FilePath drive_tmp_download_dir = GetSystemService(profile)->cache()-> | 146 const FilePath drive_tmp_download_dir = GetSystemService(profile)->cache()-> |
| 136 GetCacheDirectoryPath(DriveCache::CACHE_TYPE_TMP_DOWNLOADS); | 147 GetCacheDirectoryPath(DriveCache::CACHE_TYPE_TMP_DOWNLOADS); |
| 137 | 148 |
| 138 // Swap the drive path with a local path. Local path must be created | 149 // Swap the drive path with a local path. Local path must be created |
| 139 // on a blocking thread. | 150 // on a blocking thread. |
| 140 FilePath* drive_tmp_download_path(new FilePath()); | 151 FilePath* drive_tmp_download_path(new FilePath()); |
| 141 BrowserThread::GetBlockingPool()->PostTaskAndReply( | 152 BrowserThread::GetBlockingPool()->PostTaskAndReply( |
| 142 FROM_HERE, | 153 FROM_HERE, |
| 143 base::Bind(&DriveDownloadObserver::GetDriveTempDownloadPath, | 154 base::Bind(&GetDriveTempDownloadPath, |
| 144 drive_tmp_download_dir, | 155 drive_tmp_download_dir, |
| 145 drive_tmp_download_path), | 156 drive_tmp_download_path), |
| 146 base::Bind(&RunSubstituteDriveDownloadCallback, | 157 base::Bind(&RunSubstituteDriveDownloadCallback, |
| 147 callback, | 158 callback, |
| 148 base::Owned(drive_tmp_download_path))); | 159 base::Owned(drive_tmp_download_path))); |
| 149 } | 160 } |
| 150 | 161 |
| 151 // Callback for DriveFileSystem::CreateDirectory. | 162 // Callback for DriveFileSystem::CreateDirectory. |
| 152 void OnCreateDirectory( | 163 void OnCreateDirectory( |
| 153 Profile* profile, | 164 Profile* profile, |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 // count of received bytes and the size of the download as given by the | 327 // count of received bytes and the size of the download as given by the |
| 317 // Content-Length header. | 328 // Content-Length header. |
| 318 int64 total = (download->AllDataSaved() ? download->GetReceivedBytes() : | 329 int64 total = (download->AllDataSaved() ? download->GetReceivedBytes() : |
| 319 download->GetTotalBytes()); | 330 download->GetTotalBytes()); |
| 320 DCHECK(total <= 0 || complete < total); | 331 DCHECK(total <= 0 || complete < total); |
| 321 if (total > 0) | 332 if (total > 0) |
| 322 return static_cast<int>((complete * 100.0) / total); | 333 return static_cast<int>((complete * 100.0) / total); |
| 323 return -1; | 334 return -1; |
| 324 } | 335 } |
| 325 | 336 |
| 326 // |drive_tmp_download_path| is set to a temporary local download path in | |
| 327 // ~/GCache/v1/tmp/downloads/ | |
| 328 // static | |
| 329 void DriveDownloadObserver::GetDriveTempDownloadPath( | |
| 330 const FilePath& drive_tmp_download_dir, | |
| 331 FilePath* drive_tmp_download_path) { | |
| 332 bool created = file_util::CreateDirectory(drive_tmp_download_dir); | |
| 333 DCHECK(created) << "Can not create temp download directory at " | |
| 334 << drive_tmp_download_dir.value(); | |
| 335 created = file_util::CreateTemporaryFileInDir(drive_tmp_download_dir, | |
| 336 drive_tmp_download_path); | |
| 337 DCHECK(created) << "Temporary download file creation failed"; | |
| 338 } | |
| 339 | |
| 340 void DriveDownloadObserver::ManagerGoingDown( | 337 void DriveDownloadObserver::ManagerGoingDown( |
| 341 DownloadManager* download_manager) { | 338 DownloadManager* download_manager) { |
| 342 download_manager->RemoveObserver(this); | 339 download_manager->RemoveObserver(this); |
| 343 download_manager_ = NULL; | 340 download_manager_ = NULL; |
| 344 } | 341 } |
| 345 | 342 |
| 346 void DriveDownloadObserver::ModelChanged(DownloadManager* download_manager) { | 343 void DriveDownloadObserver::ModelChanged(DownloadManager* download_manager) { |
| 347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 348 | 345 |
| 349 DownloadManager::DownloadVector downloads; | 346 DownloadManager::DownloadVector downloads; |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 726 "], file_path=[" + file_path.value() + | 723 "], file_path=[" + file_path.value() + |
| 727 "], content_type=[" + content_type + | 724 "], content_type=[" + content_type + |
| 728 "], content_length=[" + base::UintToString(content_length) + | 725 "], content_length=[" + base::UintToString(content_length) + |
| 729 "], upload_location=[" + upload_location.possibly_invalid_spec() + | 726 "], upload_location=[" + upload_location.possibly_invalid_spec() + |
| 730 "], drive_path=[" + drive_path.value() + | 727 "], drive_path=[" + drive_path.value() + |
| 731 "], all_bytes_present=[" + (all_bytes_present ? "true" : "false") + | 728 "], all_bytes_present=[" + (all_bytes_present ? "true" : "false") + |
| 732 "]"; | 729 "]"; |
| 733 } | 730 } |
| 734 | 731 |
| 735 } // namespace drive | 732 } // namespace drive |
| OLD | NEW |