Commit da33eb36 authored by Min Qin's avatar Min Qin Committed by Commit Bot

Use OnceClosure for InProgressCache::Initialize().

There is no need to use a RepeatingClosure.

Change-Id: Iac2a26754cb93fff720085ec955ef455dc2e8e2b
Reviewed-on: https://chromium-review.googlesource.com/1038363Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Commit-Queue: Min Qin <qinmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#555474}
parent 48298342
......@@ -23,7 +23,7 @@ class InProgressCache {
virtual ~InProgressCache() = default;
// Initializes the cache.
virtual void Initialize(const base::RepeatingClosure& callback) = 0;
virtual void Initialize(base::OnceClosure callback) = 0;
// Adds or updates an existing entry.
virtual void AddOrReplaceEntry(const DownloadEntry& entry) = 0;
......
......@@ -136,26 +136,26 @@ InProgressCacheImpl::InProgressCacheImpl(
InProgressCacheImpl::~InProgressCacheImpl() = default;
void InProgressCacheImpl::Initialize(const base::RepeatingClosure& callback) {
void InProgressCacheImpl::Initialize(base::OnceClosure callback) {
// If it's already initialized, just run the callback.
if (initialization_status_ == CACHE_INITIALIZED) {
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
std::move(callback));
return;
}
pending_actions_.push_back(callback);
// If uninitialized, initialize |entries_| by reading from file.
if (initialization_status_ == CACHE_UNINITIALIZED) {
base::PostTaskAndReplyWithResult(
task_runner_.get(), FROM_HERE,
base::BindOnce(&ReadEntriesFromFile, file_path_),
base::BindOnce(&InProgressCacheImpl::OnInitialized,
weak_ptr_factory_.GetWeakPtr()));
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
}
void InProgressCacheImpl::OnInitialized(const std::vector<char>& entries) {
void InProgressCacheImpl::OnInitialized(base::OnceClosure callback,
const std::vector<char>& entries) {
if (!entries.empty()) {
if (!entries_.ParseFromArray(entries.data(), entries.size())) {
// TODO(crbug.com/778425): Get UMA for errors.
......@@ -167,11 +167,7 @@ void InProgressCacheImpl::OnInitialized(const std::vector<char>& entries) {
initialization_status_ = CACHE_INITIALIZED;
while (!pending_actions_.empty()) {
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
pending_actions_.front());
pending_actions_.pop_front();
}
std::move(callback).Run();
}
void InProgressCacheImpl::AddOrReplaceEntry(const DownloadEntry& entry) {
......
......@@ -7,7 +7,6 @@
#include <string>
#include "base/containers/circular_deque.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
......@@ -30,7 +29,7 @@ class InProgressCacheImpl : public InProgressCache {
~InProgressCacheImpl() override;
// InProgressCache implementation.
void Initialize(const base::RepeatingClosure& callback) override;
void Initialize(base::OnceClosure callback) override;
void AddOrReplaceEntry(const DownloadEntry& entry) override;
base::Optional<DownloadEntry> RetrieveEntry(const std::string& guid) override;
void RemoveEntry(const std::string& guid) override;
......@@ -44,12 +43,12 @@ class InProgressCacheImpl : public InProgressCache {
};
// Steps to execute after initialization is complete.
void OnInitialized(const std::vector<char>& entries);
void OnInitialized(base::OnceClosure callback,
const std::vector<char>& entries);
metadata_pb::DownloadEntries entries_;
base::FilePath file_path_;
InitializationStatus initialization_status_;
base::circular_deque<base::RepeatingClosure> pending_actions_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::WeakPtrFactory<InProgressCacheImpl> weak_ptr_factory_;
......
......@@ -238,7 +238,7 @@ void InProgressDownloadManager::InterceptDownloadFromNavigation(
void InProgressDownloadManager::Initialize(
const base::FilePath& metadata_cache_dir,
const base::RepeatingClosure& callback) {
base::OnceClosure callback) {
download_metadata_cache_ = std::make_unique<InProgressCacheImpl>(
metadata_cache_dir.empty() ? base::FilePath() :
metadata_cache_dir.Append(kDownloadMetadataStoreFilename),
......@@ -246,7 +246,7 @@ void InProgressDownloadManager::Initialize(
{base::MayBlock(), base::TaskPriority::BACKGROUND,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}));
download_metadata_cache_->Initialize(callback);
download_metadata_cache_->Initialize(std::move(callback));
}
void InProgressDownloadManager::ShutDown() {
......
......@@ -98,9 +98,8 @@ class COMPONENTS_DOWNLOAD_EXPORT InProgressDownloadManager
network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints,
scoped_refptr<DownloadURLLoaderFactoryGetter> url_loader_factory_getter);
// TODO(qinmin): change the |callback| to be an OnceClosure.
void Initialize(const base::FilePath& metadata_cache_dir,
const base::RepeatingClosure& callback);
base::OnceClosure callback);
void StartDownload(
std::unique_ptr<DownloadCreateInfo> info,
......
......@@ -291,9 +291,9 @@ DownloadManagerImpl::DownloadManagerImpl(BrowserContext* browser_context)
this, base::BindRepeating(&IsOriginSecure));
in_progress_manager_->Initialize(
IsOffTheRecord() ? base::FilePath() : browser_context_->GetPath(),
base::BindRepeating(
&DownloadManagerImpl::PostInitialization, weak_factory_.GetWeakPtr(),
DOWNLOAD_INITIALIZATION_DEPENDENCY_IN_PROGRESS_CACHE));
base::BindOnce(&DownloadManagerImpl::PostInitialization,
weak_factory_.GetWeakPtr(),
DOWNLOAD_INITIALIZATION_DEPENDENCY_IN_PROGRESS_CACHE));
}
DownloadManagerImpl::~DownloadManagerImpl() {
......
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