Skip to content

Commit

Permalink
Merge pull request #675 from /issues/1614
Browse files Browse the repository at this point in the history
make RunIOTask safer when returning back from the task runner thread
  • Loading branch information
NejcZdovc authored Oct 18, 2018
2 parents cc06d17 + 6226242 commit 538ea57
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ deps = {
"vendor/boto": "https://github.com/boto/boto@f7574aa6cc2c819430c1f05e9a1a1a666ef8169b",
"vendor/python-patch": "https://github.com/svn2github/python-patch@a336a458016ced89aba90dfc3f4c8222ae3b1403",
"vendor/sparkle": "https://github.com/brave/Sparkle.git@c0759cce415d7c0feae45005c8a013b1898711f0",
"vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@8a45775a4df42276013d2f8aa3c69ad9f283ff15",
"vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@9276e6390242b63e50e2462a6186f8a6e8daa917",
"vendor/bat-native-rapidjson": "https://github.com/brave-intl/bat-native-rapidjson.git@86aafe2ef89835ae71c9ed7c2527e3bb3000930e",
"vendor/bip39wally-core-native": "https://github.com/brave-intl/bip39wally-core-native.git@9b119931c702d55be994117eb505d56310720b1d",
"vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@adeff3254bb90ccdc9699040d5a4e1cd6b8393b7",
Expand Down
24 changes: 17 additions & 7 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -851,17 +851,27 @@ void RewardsServiceImpl::OnURLFetchComplete(
callback.Run(response_code, body, headers);
}

void RunIOTaskCallback(
base::WeakPtr<RewardsServiceImpl> rewards_service,
std::function<void(void)> callback) {
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
base::BindOnce(&RewardsServiceImpl::OnIOTaskComplete,
rewards_service,
callback));
}

void RewardsServiceImpl::OnIOTaskComplete(std::function<void(void)> callback) {
callback();
}

void RewardsServiceImpl::RunIOTask(
std::unique_ptr<ledger::LedgerTaskRunner> task) {
file_task_runner_->PostTask(FROM_HERE,
base::BindOnce(&ledger::LedgerTaskRunner::Run, std::move(task)));
}
ledger::LedgerTaskRunner::CallerThreadCallback callback =
std::bind(&RunIOTaskCallback, AsWeakPtr(), _1);

void RewardsServiceImpl::RunTask(
std::unique_ptr<ledger::LedgerTaskRunner> task) {
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
file_task_runner_->PostTask(FROM_HERE,
base::BindOnce(&ledger::LedgerTaskRunner::Run,
std::move(task)));
std::move(task), std::move(callback)));
}

void RewardsServiceImpl::TriggerOnWalletInitialized(int error_code) {
Expand Down
7 changes: 6 additions & 1 deletion components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_IMPL_
#define BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_IMPL_

#include <functional>
#include <map>
#include <memory>
#include <string>
Expand Down Expand Up @@ -111,6 +112,9 @@ class RewardsServiceImpl : public RewardsService,
void UpdateTipsList() override;

private:
friend void RunIOTaskCallback(
base::WeakPtr<RewardsServiceImpl>,
std::function<void(void)>);
typedef base::Callback<void(int, const std::string&, const std::map<std::string, std::string>& headers)> FetchCallback;

const extensions::OneShotEvent& ready() const { return ready_; }
Expand Down Expand Up @@ -212,7 +216,6 @@ class RewardsServiceImpl : public RewardsService,
ledger::LedgerCallbackHandler* handler) override;

void RunIOTask(std::unique_ptr<ledger::LedgerTaskRunner> task) override;
void RunTask(std::unique_ptr<ledger::LedgerTaskRunner> task) override;
void SetRewardsMainEnabled(bool enabled) const override;
void SetPublisherMinVisitTime(uint64_t duration_in_seconds) const override;
void SetPublisherMinVisits(unsigned int visits) const override;
Expand All @@ -239,6 +242,8 @@ class RewardsServiceImpl : public RewardsService,
const ledger::PUBLISHER_CATEGORY category) override;
void GetRecurringDonations(ledger::RecurringDonationCallback callback) override;

void OnIOTaskComplete(std::function<void(void)> callback);

// URLFetcherDelegate impl
void OnURLFetchComplete(const net::URLFetcher* source) override;

Expand Down

0 comments on commit 538ea57

Please sign in to comment.