Commit a2631f83 authored by thestig@chromium.org's avatar thestig@chromium.org

Cleanup: Use PostTaskAndReplyWithResults instead of passing callbacks around in downloads code.

Review URL: https://codereview.chromium.org/324883006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@276613 0039d316-1c4b-4281-b951-d872f2087c98
parent f51a712d
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/task_runner.h" #include "base/task_runner.h"
#include "base/task_runner_util.h"
#include "base/threading/sequenced_worker_pool.h" #include "base/threading/sequenced_worker_pool.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
...@@ -177,13 +178,10 @@ void CheckDownloadUrlDone( ...@@ -177,13 +178,10 @@ void CheckDownloadUrlDone(
#endif // FULL_SAFE_BROWSING #endif // FULL_SAFE_BROWSING
// Called on the blocking pool to determine the MIME type for |path|. // Called on the blocking pool to determine the MIME type for |path|.
void GetMimeTypeAndReplyOnUIThread( std::string GetMimeType(const base::FilePath& path) {
const base::FilePath& path,
const base::Callback<void(const std::string&)>& callback) {
std::string mime_type; std::string mime_type;
net::GetMimeTypeFromFile(path, &mime_type); net::GetMimeTypeFromFile(path, &mime_type);
BrowserThread::PostTask( return mime_type;
BrowserThread::UI, FROM_HERE, base::Bind(callback, mime_type));
} }
bool IsOpenInBrowserPreferreredForFile(const base::FilePath& path) { bool IsOpenInBrowserPreferreredForFile(const base::FilePath& path) {
...@@ -636,9 +634,11 @@ void ChromeDownloadManagerDelegate::CheckDownloadUrl( ...@@ -636,9 +634,11 @@ void ChromeDownloadManagerDelegate::CheckDownloadUrl(
void ChromeDownloadManagerDelegate::GetFileMimeType( void ChromeDownloadManagerDelegate::GetFileMimeType(
const base::FilePath& path, const base::FilePath& path,
const GetFileMimeTypeCallback& callback) { const GetFileMimeTypeCallback& callback) {
BrowserThread::PostBlockingPoolTask( DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
FROM_HERE, base::PostTaskAndReplyWithResult(BrowserThread::GetBlockingPool(),
base::Bind(&GetMimeTypeAndReplyOnUIThread, path, callback)); FROM_HERE,
base::Bind(&GetMimeType, path),
callback);
} }
#if defined(FULL_SAFE_BROWSING) #if defined(FULL_SAFE_BROWSING)
......
...@@ -147,15 +147,14 @@ bool TruncateFileName(base::FilePath* path, size_t limit) { ...@@ -147,15 +147,14 @@ bool TruncateFileName(base::FilePath* path, size_t limit) {
// writeable. // writeable.
// - Truncates the suggested name if it exceeds the filesystem's limit. // - Truncates the suggested name if it exceeds the filesystem's limit.
// - Uniquifies |suggested_path| if |should_uniquify_path| is true. // - Uniquifies |suggested_path| if |should_uniquify_path| is true.
// - Schedules |callback| on the UI thread with the reserved path and a flag // - Returns true if |reserved_path| has been successfully verified.
// indicating whether the returned path has been successfully verified. bool CreateReservation(
void CreateReservation(
ReservationKey key, ReservationKey key,
const base::FilePath& suggested_path, const base::FilePath& suggested_path,
const base::FilePath& default_download_path, const base::FilePath& default_download_path,
bool create_directory, bool create_directory,
DownloadPathReservationTracker::FilenameConflictAction conflict_action, DownloadPathReservationTracker::FilenameConflictAction conflict_action,
const DownloadPathReservationTracker::ReservedPathCallback& callback) { base::FilePath* reserved_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
DCHECK(suggested_path.IsAbsolute()); DCHECK(suggested_path.IsAbsolute());
...@@ -243,8 +242,8 @@ void CreateReservation( ...@@ -243,8 +242,8 @@ void CreateReservation(
reservations[key] = target_path; reservations[key] = target_path;
bool verified = (is_path_writeable && !has_conflicts && !name_too_long); bool verified = (is_path_writeable && !has_conflicts && !name_too_long);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, *reserved_path = target_path;
base::Bind(callback, target_path, verified)); return verified;
} }
// Called on the FILE thread to update the path of the reservation associated // Called on the FILE thread to update the path of the reservation associated
...@@ -277,6 +276,14 @@ void RevokeReservation(ReservationKey key) { ...@@ -277,6 +276,14 @@ void RevokeReservation(ReservationKey key) {
} }
} }
void RunGetReservedPathCallback(
const DownloadPathReservationTracker::ReservedPathCallback& callback,
const base::FilePath* reserved_path,
bool verified) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
callback.Run(*reserved_path, verified);
}
DownloadItemObserver::DownloadItemObserver(DownloadItem* download_item) DownloadItemObserver::DownloadItemObserver(DownloadItem* download_item)
: download_item_(download_item), : download_item_(download_item),
last_target_path_(download_item->GetTargetFilePath()) { last_target_path_(download_item->GetTargetFilePath()) {
...@@ -349,14 +356,20 @@ void DownloadPathReservationTracker::GetReservedPath( ...@@ -349,14 +356,20 @@ void DownloadPathReservationTracker::GetReservedPath(
new DownloadItemObserver(download_item); new DownloadItemObserver(download_item);
// DownloadItemObserver deletes itself. // DownloadItemObserver deletes itself.
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( base::FilePath* reserved_path = new base::FilePath;
&CreateReservation, BrowserThread::PostTaskAndReplyWithResult(
download_item, BrowserThread::FILE,
target_path, FROM_HERE,
default_path, base::Bind(&CreateReservation,
create_directory, download_item,
conflict_action, target_path,
callback)); default_path,
create_directory,
conflict_action,
reserved_path),
base::Bind(&RunGetReservedPathCallback,
callback,
base::Owned(reserved_path)));
} }
// static // static
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment