Skip to content

Commit

Permalink
Background download: Use weak pointer for DownloadServiceImpl.
Browse files Browse the repository at this point in the history
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/1144464
Reviewed-by: Min Qin <qinmin@chromium.org>
Commit-Queue: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576754}
  • Loading branch information
Xing Liu authored and Commit Bot committed Jul 20, 2018
1 parent 271eaf5 commit 62da7b8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ DownloadServiceImpl::DownloadServiceImpl(std::unique_ptr<Configuration> config,
logger_(std::move(logger)),
controller_(std::move(controller)),
service_config_(config_.get()),
startup_completed_(false) {
controller_->Initialize(base::Bind(
&DownloadServiceImpl::OnControllerInitialized, base::Unretained(this)));
startup_completed_(false),
weak_ptr_factory_(this) {
controller_->Initialize(
base::BindRepeating(&DownloadServiceImpl::OnControllerInitialized,
weak_ptr_factory_.GetWeakPtr()));
}

DownloadServiceImpl::~DownloadServiceImpl() = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "base/containers/circular_deque.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/service_config_impl.h"
#include "components/download/public/background_service/download_service.h"
Expand Down Expand Up @@ -60,6 +61,8 @@ class DownloadServiceImpl : public DownloadService {
std::map<DownloadTaskType, base::OnceClosure> pending_tasks_;
bool startup_completed_;

base::WeakPtrFactory<DownloadServiceImpl> weak_ptr_factory_;

DISALLOW_COPY_AND_ASSIGN(DownloadServiceImpl);
};

Expand Down

0 comments on commit 62da7b8

Please sign in to comment.