Chromium Code Reviews| Index: chrome/browser/budget_service/budget_database.cc |
| diff --git a/chrome/browser/budget_service/budget_database.cc b/chrome/browser/budget_service/budget_database.cc |
| index a1ee1b13fb1bd03075f7a1647e340756a4bc2dd4..e9954d88dcc8f3565203f316703b1b57453257e2 100644 |
| --- a/chrome/browser/budget_service/budget_database.cc |
| +++ b/chrome/browser/budget_service/budget_database.cc |
| @@ -66,7 +66,7 @@ void BudgetDatabase::GetBudgetDetails(const GURL& origin, |
| void BudgetDatabase::SpendBudget(const GURL& origin, |
| double amount, |
| - const StoreBudgetCallback& callback) { |
| + const SpendBudgetCallback& callback) { |
| SyncCache(origin, base::Bind(&BudgetDatabase::SpendBudgetAfterSync, |
| weak_ptr_factory_.GetWeakPtr(), origin, amount, |
| callback)); |
| @@ -98,10 +98,10 @@ double BudgetDatabase::GetBudget(const GURL& origin) const { |
| void BudgetDatabase::AddToCache( |
| const GURL& origin, |
| - const AddToCacheCallback& callback, |
| + const CacheCallback& callback, |
| bool success, |
| std::unique_ptr<budget_service::Budget> budget_proto) { |
| - // If the database read failed, there's nothing to add to the cache. |
| + // If the database read failed, or there's nothing to add to the cache. |
|
Peter Beverloo
2016/09/08 17:16:06
nit: This sentence feels incomplete now?
harkness
2016/09/13 13:41:28
Done.
|
| if (!success || !budget_proto) { |
| callback.Run(success); |
| return; |
| @@ -172,10 +172,11 @@ void BudgetDatabase::GetBudgetAfterSync(const GURL& origin, |
| void BudgetDatabase::SpendBudgetAfterSync(const GURL& origin, |
| double amount, |
| - const StoreBudgetCallback& callback, |
| + const SpendBudgetCallback& callback, |
| bool success) { |
| if (!success) { |
| - callback.Run(false /* success */); |
| + callback.Run(blink::mojom::BudgetServiceErrorType::DATABASE_ERROR, |
| + false /* success */); |
| return; |
| } |
| @@ -191,7 +192,8 @@ void BudgetDatabase::SpendBudgetAfterSync(const GURL& origin, |
| if (total < amount) { |
| UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForNoBudgetOrigin", score); |
| - callback.Run(false /* success */); |
| + callback.Run(blink::mojom::BudgetServiceErrorType::NONE, |
| + false /* success */); |
| return; |
| } else if (total < amount * 2) { |
| UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForLowBudgetOrigin", score); |
| @@ -213,12 +215,23 @@ void BudgetDatabase::SpendBudgetAfterSync(const GURL& origin, |
| DCHECK_EQ(0, bill); |
| // Now that the cache is updated, write the data to the database. |
| - // TODO(harkness): Consider adding a second parameter to the callback so the |
| - // caller can distinguish between not enough budget and a failed database |
| - // write. |
| + WriteCachedValuesToDatabase( |
| + origin, base::Bind(&BudgetDatabase::SpendBudgetAfterWrite, |
| + weak_ptr_factory_.GetWeakPtr(), callback)); |
| +} |
| + |
| +// This converts the bool value which is returned from the database to a Mojo |
| +// error type. |
| +void BudgetDatabase::SpendBudgetAfterWrite(const SpendBudgetCallback& callback, |
| + bool write_successful) { |
| // TODO(harkness): If the database write fails, the cache will be out of sync |
| // with the database. Consider ways to mitigate this. |
| - WriteCachedValuesToDatabase(origin, callback); |
| + if (!write_successful) { |
| + callback.Run(blink::mojom::BudgetServiceErrorType::DATABASE_ERROR, |
| + false /* success */); |
| + return; |
| + } |
| + callback.Run(blink::mojom::BudgetServiceErrorType::NONE, true /* success */); |
| } |
| void BudgetDatabase::WriteCachedValuesToDatabase( |
| @@ -256,12 +269,12 @@ void BudgetDatabase::WriteCachedValuesToDatabase( |
| } |
| void BudgetDatabase::SyncCache(const GURL& origin, |
| - const SyncCacheCallback& callback) { |
| + const CacheCallback& callback) { |
| DCHECK_EQ(origin, origin.GetOrigin()); |
| // If the origin isn't already cached, add it to the cache. |
| if (!IsCached(origin)) { |
| - AddToCacheCallback add_callback = |
| + CacheCallback add_callback = |
| base::Bind(&BudgetDatabase::SyncLoadedCache, |
| weak_ptr_factory_.GetWeakPtr(), origin, callback); |
| db_->GetEntry(origin.spec(), base::Bind(&BudgetDatabase::AddToCache, |
| @@ -273,7 +286,7 @@ void BudgetDatabase::SyncCache(const GURL& origin, |
| } |
| void BudgetDatabase::SyncLoadedCache(const GURL& origin, |
| - const SyncCacheCallback& callback, |
| + const CacheCallback& callback, |
| bool success) { |
| if (!success) { |
| callback.Run(false /* success */); |