Index: chrome/browser/chromeos/drive/download_handler.cc |
diff --git a/chrome/browser/chromeos/drive/download_handler.cc b/chrome/browser/chromeos/drive/download_handler.cc |
index 06bc182a95e4b6ab800ca456ebabdf87543cdf75..7f336aa5cf8d628c827f193ecea87d0201623606 100644 |
--- a/chrome/browser/chromeos/drive/download_handler.cc |
+++ b/chrome/browser/chromeos/drive/download_handler.cc |
@@ -8,6 +8,7 @@ |
#include "base/files/file_util.h" |
#include "base/strings/string_util.h" |
#include "base/supports_user_data.h" |
+#include "base/thread_task_runner_handle.h" |
#include "base/threading/sequenced_worker_pool.h" |
#include "chrome/browser/chromeos/drive/drive_integration_service.h" |
#include "chrome/browser/chromeos/drive/file_system_util.h" |
@@ -34,6 +35,10 @@ const char kDrivePathKey[] = "DrivePath"; |
const char* kGenericMimeTypes[] = {"text/html", "text/plain", |
"application/octet-stream"}; |
+const int64 kDefaultRequestSpace = 512 * (1 << 20); // 512MB |
hashimoto
2015/10/28 09:06:15
How was this value chosen? (based on some statisti
yawano
2015/10/28 09:29:44
No, this is not based on some statistics.
This va
|
+ |
+const base::TimeDelta kFreeDiskSpaceDelay = base::TimeDelta::FromSeconds(5); |
+ |
// User Data stored in DownloadItem for drive path. |
class DriveUserData : public base::SupportsUserData::Data { |
public: |
@@ -125,12 +130,15 @@ std::string FilterOutGenericMimeType(const std::string& mime_type) { |
return mime_type; |
} |
+void IgnoreFreeDiskSpaceIfNeededForCallback(bool /*result*/) {} |
+ |
} // namespace |
DownloadHandler::DownloadHandler(FileSystemInterface* file_system) |
: file_system_(file_system), |
- weak_ptr_factory_(this) { |
-} |
+ pending_request_space_(0), |
+ free_disk_space_delay_(kFreeDiskSpaceDelay), |
+ weak_ptr_factory_(this) {} |
DownloadHandler::~DownloadHandler() { |
} |
@@ -237,8 +245,44 @@ void DownloadHandler::CheckForFileExistence( |
callback)); |
} |
+void DownloadHandler::SetFreeDiskSpaceDelayForTesting( |
+ const base::TimeDelta& delay) { |
+ free_disk_space_delay_ = delay; |
+} |
+ |
+// TODO(yawano): support multiple in-progress downloads. |
hashimoto
2015/10/28 09:06:15
Why don't you support multiple downloads now?
yawano
2015/10/28 09:29:44
I thought it will make this CL larger. But I think
|
+void DownloadHandler::FreeDiskSpaceIfNeeded(const DownloadItem* download, |
+ bool immediate) { |
+ if (download->IsDone()) |
+ return; |
+ |
+ const int64 total_bytes = download->GetTotalBytes(); |
+ const int64 request_space = total_bytes == 0 |
hashimoto
2015/10/28 09:06:15
Please add a comment why we need to handle total_b
yawano
2015/10/29 02:52:43
Done.
|
+ ? kDefaultRequestSpace |
hashimoto
2015/10/28 09:06:15
IIUC, when the download is 99% complete, this can
yawano
2015/10/28 09:29:44
This value is used when we don't know total bytes
|
+ : total_bytes - download->GetReceivedBytes(); |
+ |
+ if (immediate) { |
+ file_system_->FreeDiskSpaceIfNeededFor( |
+ request_space, base::Bind(&IgnoreFreeDiskSpaceIfNeededForCallback)); |
+ |
+ return; |
+ } |
+ |
+ const bool post_task = pending_request_space_ == 0; |
+ pending_request_space_ = request_space; |
+ |
+ if (post_task) { |
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
+ FROM_HERE, base::Bind(&DownloadHandler::ExecuteFreeDiskSpaceIfNeededFor, |
+ weak_ptr_factory_.GetWeakPtr()), |
+ free_disk_space_delay_); |
+ } |
+} |
+ |
void DownloadHandler::OnDownloadCreated(DownloadManager* manager, |
DownloadItem* download) { |
+ FreeDiskSpaceIfNeeded(download, true /* immediate */); |
+ |
// Remove any persisted Drive DownloadItem. crbug.com/171384 |
if (IsPersistedDriveDownload(drive_tmp_download_path_, download)) { |
// Remove download later, since doing it here results in a crash. |
@@ -265,6 +309,8 @@ void DownloadHandler::OnDownloadUpdated( |
DownloadManager* manager, DownloadItem* download) { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ FreeDiskSpaceIfNeeded(download, false /* not immediate */); |
+ |
// Only accept downloads that have the Drive meta data associated with them. |
DriveUserData* data = GetDriveUserData(download); |
if (!drive_tmp_download_path_.IsParent(download->GetTargetFilePath()) || |
@@ -345,6 +391,14 @@ void DownloadHandler::SetCacheFilePath(void* manager_id, |
data->set_cache_file_path(*cache_file_path); |
} |
+void DownloadHandler::ExecuteFreeDiskSpaceIfNeededFor() { |
+ file_system_->FreeDiskSpaceIfNeededFor( |
+ pending_request_space_, |
+ base::Bind(&IgnoreFreeDiskSpaceIfNeededForCallback)); |
+ |
+ pending_request_space_ = 0; |
+} |
+ |
DownloadManager* DownloadHandler::GetDownloadManager(void* manager_id) { |
if (manager_id == notifier_->GetManager()) |
return notifier_->GetManager(); |