Commit 62da7b8c authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Background download: Use weak pointer for DownloadServiceImpl.

Uses weak pointer in DownloadServiceImpl to fix a potential memory
corruption in |pending_tasks_|.

Bug: 860771
Change-Id: Ib84f4c27aa4d2f1e7560434d41175ecba0a622e5
Reviewed-on: https://chromium-review.googlesource.com/1144464Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Commit-Queue: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576754}
parent 271eaf50
...@@ -22,9 +22,11 @@ DownloadServiceImpl::DownloadServiceImpl(std::unique_ptr<Configuration> config, ...@@ -22,9 +22,11 @@ DownloadServiceImpl::DownloadServiceImpl(std::unique_ptr<Configuration> config,
logger_(std::move(logger)), logger_(std::move(logger)),
controller_(std::move(controller)), controller_(std::move(controller)),
service_config_(config_.get()), service_config_(config_.get()),
startup_completed_(false) { startup_completed_(false),
controller_->Initialize(base::Bind( weak_ptr_factory_(this) {
&DownloadServiceImpl::OnControllerInitialized, base::Unretained(this))); controller_->Initialize(
base::BindRepeating(&DownloadServiceImpl::OnControllerInitialized,
weak_ptr_factory_.GetWeakPtr()));
} }
DownloadServiceImpl::~DownloadServiceImpl() = default; DownloadServiceImpl::~DownloadServiceImpl() = default;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/containers/circular_deque.h" #include "base/containers/circular_deque.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/download/internal/background_service/config.h" #include "components/download/internal/background_service/config.h"
#include "components/download/internal/background_service/service_config_impl.h" #include "components/download/internal/background_service/service_config_impl.h"
#include "components/download/public/background_service/download_service.h" #include "components/download/public/background_service/download_service.h"
...@@ -60,6 +61,8 @@ class DownloadServiceImpl : public DownloadService { ...@@ -60,6 +61,8 @@ class DownloadServiceImpl : public DownloadService {
std::map<DownloadTaskType, base::OnceClosure> pending_tasks_; std::map<DownloadTaskType, base::OnceClosure> pending_tasks_;
bool startup_completed_; bool startup_completed_;
base::WeakPtrFactory<DownloadServiceImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(DownloadServiceImpl); DISALLOW_COPY_AND_ASSIGN(DownloadServiceImpl);
}; };
......
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