Skip to content

Commit

Permalink
Merge pull request #6000 from brave/new-persona
Browse files Browse the repository at this point in the history
Implements new persona api
  • Loading branch information
NejcZdovc authored Jul 28, 2020
2 parents 7e14b75 + e35df1b commit 673590f
Show file tree
Hide file tree
Showing 111 changed files with 1,129 additions and 2,760 deletions.
3 changes: 1 addition & 2 deletions browser/brave_rewards/android/brave_rewards_native_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,7 @@ void BraveRewardsNativeWorker::RecoverWallet(

void BraveRewardsNativeWorker::OnRecoverWallet(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
double balance) {
const int32_t result) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_BraveRewardsNativeWorker_OnRecoverWallet(
env, weak_java_brave_rewards_native_worker_.get(env), result);
Expand Down
6 changes: 3 additions & 3 deletions browser/brave_rewards/android/brave_rewards_native_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ class BraveRewardsNativeWorker : public brave_rewards::RewardsServiceObserver,
const std::string& wallet_type, const std::string& action,
const std::map<std::string, std::string>& args);

void OnRecoverWallet(brave_rewards::RewardsService* rewards_service,
unsigned int result,
double balance) override;
void OnRecoverWallet(
brave_rewards::RewardsService* rewards_service,
const int32_t result) override;

private:
std::string StdStrStrMapToJsonString(
Expand Down
25 changes: 11 additions & 14 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ class RewardsDOMHandler : public WebUIMessageHandler,
brave_rewards::RewardsService* rewards_service,
const uint32_t result,
brave_rewards::Promotion promotion) override;
void OnRecoverWallet(brave_rewards::RewardsService* rewards_service,
unsigned int result,
double balance) override;
void OnRecoverWallet(
brave_rewards::RewardsService* rewards_service,
const int32_t result) override;
void OnExcludedSitesChanged(brave_rewards::RewardsService* rewards_service,
std::string publisher_id,
bool excluded) override;
Expand Down Expand Up @@ -754,23 +754,20 @@ void RewardsDOMHandler::GetWalletPassphrase(const base::ListValue* args) {
void RewardsDOMHandler::RecoverWallet(const base::ListValue *args) {
CHECK_EQ(1U, args->GetSize());
if (rewards_service_) {
const std::string passPhrase = args->GetList()[0].GetString();
rewards_service_->RecoverWallet(passPhrase);
const std::string pass_phrase = args->GetList()[0].GetString();
rewards_service_->RecoverWallet(pass_phrase);
}
}

void RewardsDOMHandler::OnRecoverWallet(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
double balance) {
if (web_ui()->CanCallJavascript()) {
base::DictionaryValue recover;
recover.SetInteger("result", result);
recover.SetDouble("balance", balance);

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.recoverWalletData", recover);
const int32_t result) {
if (!web_ui()->CanCallJavascript()) {
return;
}

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.recoverWalletData", base::Value(result));
}

void RewardsDOMHandler::OnGetReconcileStamp(uint64_t reconcile_stamp) {
Expand Down
1 change: 1 addition & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "walletRecoverySuccess", IDS_BRAVE_REWARDS_LOCAL_WALLET_RECOVERY_SUCCESS }, // NOLINT
{ "walletRestored", IDS_BRAVE_REWARDS_LOCAL_WALLET_RESTORED },
{ "walletRecoveryFail", IDS_BRAVE_REWARDS_LOCAL_WALLET_RECOVERY_FAIL }, // NOLINT
{ "walletRecoveryOutdated", IDS_BRAVE_REWARDS_LOCAL_WALLET_RECOVERY_OUTDATED }, // NOLINT
{ "almostThere", IDS_BRAVE_REWARDS_LOCAL_ALMOST_THERE },
{ "notQuite", IDS_BRAVE_REWARDS_LOCAL_NOT_QUITE },
{ "proveHuman", IDS_BRAVE_REWARDS_LOCAL_PROVE_HUMAN },
Expand Down
50 changes: 13 additions & 37 deletions components/brave_ads/browser/ads_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,6 @@ std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
return std::move(http_response);
}

bool URLMatches(const std::string& url,
const std::string& path,
const std::string& prefix,
const ServerTypes& server) {
const std::string target_url =
braveledger_request_util::BuildUrl(path, prefix, server);
return (url.find(target_url) == 0);
}

} // namespace

class BraveAdsBrowserTest
Expand Down Expand Up @@ -180,37 +171,23 @@ class BraveAdsBrowserTest
base::FilePath path;
GetTestDataDir(&path);
ASSERT_TRUE(
base::ReadFileToString(path.AppendASCII("register_persona_resp.json"),
&registrarVK_));
ASSERT_TRUE(
base::ReadFileToString(path.AppendASCII("verify_persona_resp.json"),
&verification_));
base::ReadFileToString(path.AppendASCII("wallet_resp.json"),
&wallet_));
ASSERT_TRUE(
base::ReadFileToString(path.AppendASCII("parameters_resp.json"),
&parameters_));
}

void GetTestResponse(const std::string& url,
int32_t method,
int* response_status_code,
std::string* response,
std::map<std::string, std::string>* headers) {
std::vector<std::string> tmp = base::SplitString(
url,
"/",
base::TRIM_WHITESPACE,
base::SPLIT_WANT_ALL);
const std::string persona_url =
braveledger_request_util::BuildUrl(REGISTER_PERSONA, PREFIX_V2);
if (url.find(persona_url) == 0 && tmp.size() == 6) {
*response = registrarVK_;
} else if (URLMatches(url, REGISTER_PERSONA, PREFIX_V2,
ServerTypes::LEDGER) &&
tmp.size() == 7) {
*response = verification_;
} else if (URLMatches(url, "/wallet/", PREFIX_V2,
ServerTypes::BALANCE)) {
*response = parameters_;
void GetTestResponse(
const std::string& url,
int32_t method,
int* response_status_code,
std::string* response,
std::map<std::string, std::string>* headers) {
if (url.find("/v3/wallet/brave") != std::string::npos) {
*response = wallet_;
*response_status_code = net::HTTP_CREATED;
return;
}
}

Expand Down Expand Up @@ -520,8 +497,7 @@ class BraveAdsBrowserTest
std::unique_ptr<base::RunLoop> brave_ads_have_arrived_notification_run_loop_;
bool brave_ads_have_arrived_notification_was_already_shown_ = false;

std::string registrarVK_;
std::string verification_;
std::string wallet_;
std::string parameters_;
};

Expand Down
1 change: 0 additions & 1 deletion components/brave_rewards/browser/rewards_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterDoublePref(prefs::kStateAutoContributeAmount, 0.0);
registry->RegisterUint64Pref(prefs::kStateNextReconcileStamp, 0ull);
registry->RegisterUint64Pref(prefs::kStateCreationStamp, 0ull);
registry->RegisterStringPref(prefs::kStateAnonymousCardId, "");
registry->RegisterStringPref(prefs::kStateRecoverySeed, "");
registry->RegisterStringPref(prefs::kStatePaymentId, "");
registry->RegisterBooleanPref(prefs::kStateInlineTipRedditEnabled, false);
Expand Down
40 changes: 6 additions & 34 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -927,17 +927,6 @@ void RewardsServiceImpl::GetAutoContributeProperties(
callback));
}

void RewardsServiceImpl::OnRecoverWallet(
ledger::Result result,
double balance) {
for (auto& observer : observers_) {
observer.OnRecoverWallet(
this,
static_cast<int>(result),
balance);
}
}

void RewardsServiceImpl::OnReconcileComplete(
const ledger::Result result,
ledger::ContributionInfoPtr contribution) {
Expand Down Expand Up @@ -1014,22 +1003,6 @@ void RewardsServiceImpl::OnPublisherStateLoaded(
data);
}

void RewardsServiceImpl::LoadNicewareList(
ledger::GetNicewareListCallback callback) {
if (!Connected()) {
return;
}

std::string data = ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_BRAVE_REWARDS_NICEWARE_LIST).as_string();

if (data.empty()) {
BLOG(0, "Failed to read in niceware list");
}
callback(data.empty() ? ledger::Result::LEDGER_ERROR
: ledger::Result::LEDGER_OK, data);
}

void RewardsServiceImpl::LoadURL(
const std::string& url,
const std::vector<std::string>& headers,
Expand Down Expand Up @@ -1403,6 +1376,12 @@ void RewardsServiceImpl::RecoverWallet(const std::string& passPhrase) {
AsWeakPtr()));
}

void RewardsServiceImpl::OnRecoverWallet(const ledger::Result result) {
for (auto& observer : observers_) {
observer.OnRecoverWallet(this, static_cast<int>(result));
}
}

void RewardsServiceImpl::AttestPromotion(
const std::string& promotion_id,
const std::string& solution,
Expand Down Expand Up @@ -2708,7 +2687,6 @@ void RewardsServiceImpl::HandleFlags(const std::string& options) {
uphold->status = ledger::WalletStatus::VERIFIED;
uphold->one_time_string = "";
uphold->user_name = "Brave Test";
uphold->transferred = true;
SaveExternalWallet(ledger::kWalletUphold, std::move(uphold));
continue;
}
Expand Down Expand Up @@ -3150,7 +3128,6 @@ void RewardsServiceImpl::SaveExternalWallet(const std::string& wallet_type,
new_wallet.SetIntKey("status", static_cast<int>(wallet->status));
new_wallet.SetStringKey("one_time_string", wallet->one_time_string);
new_wallet.SetStringKey("user_name", wallet->user_name);
new_wallet.SetBoolKey("transferred", wallet->transferred);
new_wallet.SetStringKey("verify_url", wallet->verify_url);
new_wallet.SetStringKey("add_url", wallet->add_url);
new_wallet.SetStringKey("withdraw_url", wallet->withdraw_url);
Expand Down Expand Up @@ -3200,11 +3177,6 @@ RewardsServiceImpl::GetExternalWallets() {
wallet->user_name = *user_name;
}

auto transferred = it.second.FindBoolKey("transferred");
if (transferred) {
wallet->transferred = *transferred;
}

auto* verify_url = it.second.FindStringKey("verify_url");
if (verify_url) {
wallet->verify_url = *verify_url;
Expand Down
6 changes: 2 additions & 4 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ class RewardsServiceImpl : public RewardsService,
ledger::Result result);
void OnLedgerStateLoaded(ledger::OnLoadCallback callback,
std::pair<std::string, base::Value> data);
void LoadNicewareList(ledger::GetNicewareListCallback callback) override;
void OnPublisherStateLoaded(ledger::OnLoadCallback callback,
const std::string& data);
void OnGetRewardsParameters(
Expand Down Expand Up @@ -474,10 +473,9 @@ class RewardsServiceImpl : public RewardsService,
GetAnonWalletStatusCallback callback,
const ledger::Result result);

void OnRecoverWallet(const ledger::Result result);

// ledger::LedgerClient
void OnRecoverWallet(
ledger::Result result,
double balance);
void OnReconcileComplete(
const ledger::Result result,
ledger::ContributionInfoPtr contribution) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class MockRewardsServiceObserver : public RewardsServiceObserver {
MOCK_METHOD3(OnFetchPromotions, void(RewardsService*,
const uint32_t,
const std::vector<brave_rewards::Promotion>& list));
MOCK_METHOD3(OnRecoverWallet, void(RewardsService*,
unsigned int,
double));
MOCK_METHOD2(OnRecoverWallet, void(RewardsService*, const int32_t));
MOCK_METHOD3(OnPromotionFinished, void(RewardsService*,
const uint32_t,
brave_rewards::Promotion));
Expand Down
3 changes: 1 addition & 2 deletions components/brave_rewards/browser/rewards_service_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class RewardsServiceObserver : public base::CheckedObserver {
const std::vector<Promotion>& list) {}
virtual void OnRecoverWallet(
RewardsService* rewards_service,
unsigned int result,
double balance) {}
const int32_t result) {}
virtual void OnPromotionFinished(
RewardsService* rewards_service,
const uint32_t result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ void RewardsBrowserTestContribution::SetUpUpholdWallet(
wallet->status = status;
wallet->one_time_string = "";
wallet->user_name = "Brave Test";
wallet->transferred = true;
rewards_service_->SaveExternalWallet("uphold", std::move(wallet));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,8 @@ void RewardsBrowserTestResponse::LoadMocks() {
base::FilePath path;
rewards_browsertest_util::GetTestDataDir(&path);
ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("register_persona_resp.json"),
&registrar_vk_));

ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("verify_persona_resp.json"),
&verification_));
path.AppendASCII("wallet_resp.json"),
&wallet_));

ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("promotions_resp.json"),
Expand Down Expand Up @@ -202,6 +198,10 @@ void RewardsBrowserTestResponse::LoadMocks() {
path.AppendASCII("uphold_commit_resp.json"),
&uphold_commit_resp_));

ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("uphold_addresses_resp.json"),
&uphold_addresses_resp_));

std::vector<std::string> publisher_keys {
"bumpsmack.com",
"duckduckgo.com",
Expand All @@ -228,32 +228,17 @@ void RewardsBrowserTestResponse::Get(
requests_.emplace_back(url, method);
DCHECK(response_status_code && response);

std::vector<std::string> tmp = base::SplitString(
url,
"/",
base::TRIM_WHITESPACE,
base::SPLIT_WANT_ALL);
const std::string persona_url =
braveledger_request_util::BuildUrl(REGISTER_PERSONA, PREFIX_V2);
if (url.find(persona_url) == 0 && tmp.size() == 6) {
*response = registrar_vk_;
return;
}

if (URLMatches(
url,
REGISTER_PERSONA,
PREFIX_V2,
ServerTypes::LEDGER) && tmp.size() == 7) {
*response = verification_;
if (url.find("/v3/wallet/brave") != std::string::npos) {
*response = wallet_;
*response_status_code = net::HTTP_CREATED;
return;
}

if (URLMatches(
url,
"/wallet/",
PREFIX_V2,
ServerTypes::BALANCE)) {
"/wallet/uphold",
PREFIX_V3,
ServerTypes::kPromotion)) {
if (user_funds_balance_) {
*response = user_funds_balance_resp_;
} else {
Expand Down Expand Up @@ -354,7 +339,12 @@ void RewardsBrowserTestResponse::Get(
url,
"commit",
base::CompareCase::INSENSITIVE_ASCII)) {
*response = uphold_commit_resp_;
*response = uphold_commit_resp_;
} else if (base::EndsWith(
url,
"addresses",
base::CompareCase::INSENSITIVE_ASCII)) {
*response = uphold_addresses_resp_;
} else {
*response = rewards_browsertest_util::GetUpholdCard(
external_balance_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ class RewardsBrowserTestResponse {
void SetUserFundsBalance(const bool user_funds);

private:
std::string registrar_vk_;
std::string verification_;
std::string wallet_;
std::string promotions_;
std::string promotion_empty_key_;
std::string promotion_claim_;
Expand All @@ -68,6 +67,7 @@ class RewardsBrowserTestResponse {
std::string uphold_auth_resp_;
std::string uphold_transactions_resp_;
std::string uphold_commit_resp_;
std::string uphold_addresses_resp_;
std::string user_funds_balance_resp_;

std::vector<Request> requests_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,6 @@ IN_PROC_BROWSER_TEST_F(RewardsStateBrowserTest, State_2) {
EXPECT_EQ(
profile_->GetPrefs()->GetString("brave.rewards.wallet.seed"),
"PgFfhazUJuf8dX+8ckTjrtK1KMLyrfXmKJFDiS1Ad3I=");
EXPECT_EQ(
profile_->GetPrefs()->GetString("brave.rewards.wallet.anonymous_card_id"),
"cf5b388c-eea2-4c98-bec2-f8daf39881a4");
EXPECT_EQ(
profile_->GetPrefs()->GetUint64("brave.rewards.creation_stamp"),
1590484778ul);
Expand Down
Loading

0 comments on commit 673590f

Please sign in to comment.