"Fossies" - the Fresh Open Source Software Archive

Member "brave-core-1.31.87/vendor/bat-native-ads/src/bat/ads/internal/account/account.cc" (19 Oct 2021, 6661 Bytes) of package /linux/www/brave-core-1.31.87.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "account.cc": 1.30.89_vs_1.31.87.

    1 /* Copyright (c) 2020 The Brave Authors. All rights reserved.
    2  * This Source Code Form is subject to the terms of the Mozilla Public
    3  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
    4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
    5 
    6 #include "bat/ads/internal/account/account.h"
    7 
    8 #include "base/check.h"
    9 #include "bat/ads/ads_client.h"
   10 #include "bat/ads/internal/account/ad_rewards/ad_rewards.h"
   11 #include "bat/ads/internal/account/ad_rewards/ad_rewards_util.h"
   12 #include "bat/ads/internal/account/confirmations/confirmation_info.h"
   13 #include "bat/ads/internal/account/confirmations/confirmations.h"
   14 #include "bat/ads/internal/account/confirmations/confirmations_state.h"
   15 #include "bat/ads/internal/account/statement/statement.h"
   16 #include "bat/ads/internal/account/transactions/transactions.h"
   17 #include "bat/ads/internal/account/wallet/wallet.h"
   18 #include "bat/ads/internal/account/wallet/wallet_info.h"
   19 #include "bat/ads/internal/ads_client_helper.h"
   20 #include "bat/ads/internal/logging.h"
   21 #include "bat/ads/internal/privacy/tokens/token_generator_interface.h"
   22 #include "bat/ads/internal/privacy/unblinded_tokens/unblinded_tokens.h"
   23 #include "bat/ads/internal/tokens/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens.h"
   24 #include "bat/ads/internal/tokens/refill_unblinded_tokens/refill_unblinded_tokens.h"
   25 #include "bat/ads/statement_info.h"
   26 
   27 namespace ads {
   28 
   29 Account::Account(privacy::TokenGeneratorInterface* token_generator)
   30     : ad_rewards_(std::make_unique<AdRewards>()),
   31       confirmations_(
   32           std::make_unique<Confirmations>(token_generator, ad_rewards_.get())),
   33       redeem_unblinded_payment_tokens_(
   34           std::make_unique<RedeemUnblindedPaymentTokens>()),
   35       refill_unblinded_tokens_(
   36           std::make_unique<RefillUnblindedTokens>(token_generator)),
   37       statement_(std::make_unique<Statement>(ad_rewards_.get())),
   38       wallet_(std::make_unique<Wallet>()) {
   39   confirmations_->AddObserver(this);
   40 
   41   ad_rewards_->set_delegate(this);
   42   redeem_unblinded_payment_tokens_->set_delegate(this);
   43   refill_unblinded_tokens_->set_delegate(this);
   44 }
   45 
   46 Account::~Account() {
   47   confirmations_->RemoveObserver(this);
   48 }
   49 
   50 void Account::AddObserver(AccountObserver* observer) {
   51   DCHECK(observer);
   52   observers_.AddObserver(observer);
   53 }
   54 
   55 void Account::RemoveObserver(AccountObserver* observer) {
   56   DCHECK(observer);
   57   observers_.RemoveObserver(observer);
   58 }
   59 
   60 bool Account::SetWallet(const std::string& id, const std::string& seed) {
   61   const WalletInfo last_wallet = wallet_->Get();
   62 
   63   if (!wallet_->Set(id, seed)) {
   64     NotifyInvalidWallet();
   65     return false;
   66   }
   67 
   68   const WalletInfo wallet = wallet_->Get();
   69 
   70   if (last_wallet.IsValid() && last_wallet != wallet) {
   71     ad_rewards_->Reset();
   72 
   73     NotifyWalletDidChange(wallet);
   74   }
   75 
   76   NotifyWalletDidUpdate(wallet);
   77 
   78   return true;
   79 }
   80 
   81 WalletInfo Account::GetWallet() const {
   82   return wallet_->Get();
   83 }
   84 
   85 void Account::SetCatalogIssuers(const CatalogIssuersInfo& catalog_issuers) {
   86   confirmations_->SetCatalogIssuers(catalog_issuers);
   87   NotifyCatalogIssuersDidChange(catalog_issuers);
   88 }
   89 
   90 void Account::Deposit(const std::string& creative_instance_id,
   91                       const ConfirmationType& confirmation_type) {
   92   confirmations_->Confirm(creative_instance_id, confirmation_type);
   93 }
   94 
   95 StatementInfo Account::GetStatement(const int64_t from_timestamp,
   96                                     const int64_t to_timestamp) const {
   97   DCHECK(to_timestamp >= from_timestamp);
   98   return statement_->Get(from_timestamp, to_timestamp);
   99 }
  100 
  101 void Account::Reconcile() {
  102   if (!ShouldRewardUser()) {
  103     return;
  104   }
  105 
  106   const WalletInfo wallet = GetWallet();
  107   ad_rewards_->MaybeReconcile(wallet);
  108 }
  109 
  110 void Account::ProcessTransactions() {
  111   if (!ShouldRewardUser()) {
  112     return;
  113   }
  114 
  115   confirmations_->RetryAfterDelay();
  116 
  117   ProcessUnclearedTransactions();
  118 }
  119 
  120 void Account::TopUpUnblindedTokens() {
  121   if (!ShouldRewardUser()) {
  122     return;
  123   }
  124 
  125   const WalletInfo wallet = GetWallet();
  126   refill_unblinded_tokens_->MaybeRefill(wallet);
  127 }
  128 
  129 ///////////////////////////////////////////////////////////////////////////////
  130 
  131 void Account::ProcessUnclearedTransactions() {
  132   const WalletInfo wallet = GetWallet();
  133   redeem_unblinded_payment_tokens_->MaybeRedeemAfterDelay(wallet);
  134 }
  135 
  136 void Account::NotifyWalletDidUpdate(const WalletInfo& wallet) const {
  137   for (AccountObserver& observer : observers_) {
  138     observer.OnWalletDidUpdate(wallet);
  139   }
  140 }
  141 
  142 void Account::NotifyWalletDidChange(const WalletInfo& wallet) const {
  143   for (AccountObserver& observer : observers_) {
  144     observer.OnWalletDidChange(wallet);
  145   }
  146 }
  147 
  148 void Account::NotifyInvalidWallet() const {
  149   for (AccountObserver& observer : observers_) {
  150     observer.OnInvalidWallet();
  151   }
  152 }
  153 
  154 void Account::NotifyCatalogIssuersDidChange(
  155     const CatalogIssuersInfo& catalog_issuers) const {
  156   for (AccountObserver& observer : observers_) {
  157     observer.OnCatalogIssuersDidChange(catalog_issuers);
  158   }
  159 }
  160 
  161 void Account::NotifyStatementOfAccountsDidChange() const {
  162   for (AccountObserver& observer : observers_) {
  163     observer.OnStatementOfAccountsDidChange();
  164   }
  165 }
  166 
  167 void Account::OnDidConfirm(const double estimated_redemption_value,
  168                            const ConfirmationInfo& confirmation) {
  169   transactions::Add(estimated_redemption_value, confirmation);
  170   NotifyStatementOfAccountsDidChange();
  171 
  172   TopUpUnblindedTokens();
  173 }
  174 
  175 void Account::OnFailedToConfirm(const ConfirmationInfo& confirmation) {
  176   TopUpUnblindedTokens();
  177 
  178   confirmations_->RetryAfterDelay();
  179 }
  180 
  181 void Account::OnDidReconcileAdRewards() {
  182   NotifyStatementOfAccountsDidChange();
  183 }
  184 
  185 void Account::OnDidRedeemUnblindedPaymentTokens(
  186     const privacy::UnblindedTokenList unblinded_tokens) {
  187   BLOG(1, "Successfully redeemed unblinded payment tokens");
  188 
  189   const TransactionList transactions = transactions::GetUncleared();
  190   ad_rewards_->AppendUnreconciledTransactions(transactions);
  191 
  192   Reconcile();
  193 }
  194 
  195 void Account::OnFailedToRedeemUnblindedPaymentTokens() {
  196   BLOG(1, "Failed to redeem unblinded payment tokens");
  197 }
  198 
  199 void Account::OnDidRetryRedeemingUnblindedPaymentTokens() {
  200   BLOG(1, "Retry redeeming unblinded payment tokens");
  201 }
  202 
  203 void Account::OnDidRefillUnblindedTokens() {
  204   BLOG(1, "Successfully refilled unblinded tokens");
  205 
  206   AdsClientHelper::Get()->ClearScheduledCaptcha();
  207 }
  208 
  209 void Account::OnCaptchaRequiredToRefillUnblindedTokens(
  210     const std::string& captcha_id) {
  211   BLOG(1, "Captcha required to refill unblinded tokens");
  212 
  213   const WalletInfo wallet = GetWallet();
  214   AdsClientHelper::Get()->ShowScheduledCaptchaNotification(wallet.id,
  215                                                            captcha_id);
  216 }
  217 
  218 }  // namespace ads