Skip to content

Commit

Permalink
Review fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cypt4 committed Jan 24, 2023
1 parent 0a34eed commit 411a98b
Show file tree
Hide file tree
Showing 34 changed files with 420 additions and 303 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public GetNftErc721MetadataContext(Runnable responseCompleteCallback) {
}

@Override
public void call(String erc721Metadata, Integer errorCode, String errorMessage) {
public void call(String tokenUrl, String erc721Metadata, Integer errorCode, String errorMessage) {
this.tokenMetadata = erc721Metadata;
this.errorCode = errorCode;
this.errorMessage = errorMessage;
Expand Down
2 changes: 1 addition & 1 deletion browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2022 The Brave Authors. All rights reserved.
# Copyright (c) 2019 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.
Expand Down
5 changes: 2 additions & 3 deletions browser/brave_wallet/brave_wallet_auto_pin_service_factory.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
Expand All @@ -9,10 +9,9 @@
#include <utility>

#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"

#include "brave/browser/brave_wallet/brave_wallet_pin_service_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
// TODO(cypt4) : Refactor brave/browser into separate component (#27486)
// TODO(cypt4) : Refactor brave/browser/ipfs into separate component (#27486)
#include "brave/browser/ipfs/ipfs_service_factory.h" // nogncheck

#include "brave/components/brave_wallet/browser/brave_wallet_pin_service.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
Expand Down
9 changes: 7 additions & 2 deletions browser/brave_wallet/brave_wallet_pin_service_factory.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -34,7 +34,12 @@ BraveWalletPinServiceFactory::GetForContext(content::BrowserContext* context) {
return mojo::PendingRemote<mojom::WalletPinService>();
}

return GetServiceForContext(context)->MakeRemote();
auto* service = GetServiceForContext(context);
if (!service) {
return mojo::PendingRemote<mojom::WalletPinService>();
}

return service->MakeRemote();
}

// static
Expand Down
2 changes: 1 addition & 1 deletion browser/brave_wallet/brave_wallet_pin_service_factory.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
Expand Down
5 changes: 4 additions & 1 deletion browser/ipfs/ipfs_local_pin_service_factory.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
Expand All @@ -25,6 +25,9 @@ IpfsLocalPinServiceFactory* IpfsLocalPinServiceFactory::GetInstance() {
// static
IpfsLocalPinService* IpfsLocalPinServiceFactory::GetServiceForContext(
content::BrowserContext* context) {
if (!ipfs::IpfsServiceFactory::IsIpfsEnabled(context)) {
return nullptr;
}
return static_cast<IpfsLocalPinService*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
Expand Down
2 changes: 1 addition & 1 deletion browser/ipfs/ipfs_local_pin_service_factory.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023 The Brave Authors. All rights reserved.
# Copyright (c) 2019 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.
Expand Down
50 changes: 29 additions & 21 deletions components/brave_wallet/browser/brave_wallet_auto_pin_service.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
Expand All @@ -9,12 +9,13 @@

namespace brave_wallet {

IntentData::IntentData(const BlockchainTokenPtr& token,
Operation operation,
absl::optional<std::string> service)
BraveWalletAutoPinService::IntentData::IntentData(
const BlockchainTokenPtr& token,
Operation operation,
absl::optional<std::string> service)
: token(token.Clone()), operation(operation), service(std::move(service)) {}

IntentData::~IntentData() {}
BraveWalletAutoPinService::IntentData::~IntentData() = default;

BraveWalletAutoPinService::BraveWalletAutoPinService(
PrefService* prefs,
Expand All @@ -23,6 +24,7 @@ BraveWalletAutoPinService::BraveWalletAutoPinService(
: pref_service_(prefs),
brave_wallet_service_(brave_wallet_service),
brave_wallet_pin_service_(brave_wallet_pin_service) {
DCHECK(brave_wallet_service);
Restore();
brave_wallet_service->AddTokenObserver(
token_observer_.BindNewPipeAndPassRemote());
Expand All @@ -43,41 +45,44 @@ BraveWalletAutoPinService::MakeRemote() {
}

void BraveWalletAutoPinService::OnTokenAdded(BlockchainTokenPtr token) {
if (!token->is_nft) {
if (!BraveWalletPinService::IsTokenSupportedForPinning(token)) {
return;
}
if (!IsAutoPinEnabled()) {
return;
}
PostPinToken(std::move(token), base::OnceCallback<void(bool)>());
PostPinToken(std::move(token));
}

void BraveWalletAutoPinService::OnTokenRemoved(BlockchainTokenPtr token) {
if (!token->is_nft) {
if (!BraveWalletPinService::IsTokenSupportedForPinning(token)) {
return;
}
base::EraseIf(queue_, [&token](const std::unique_ptr<IntentData>& intent) {
return intent->token == token;
});
PostUnpinToken(std::move(token), base::OnceCallback<void(bool)>());
PostUnpinToken(std::move(token));
}

void BraveWalletAutoPinService::Restore() {
brave_wallet_service_->GetAllUserAssets(base::BindOnce(
&BraveWalletAutoPinService::OnTokenListResolved, base::Unretained(this)));
brave_wallet_service_->GetAllUserAssets(
base::BindOnce(&BraveWalletAutoPinService::OnTokenListResolved,
weak_ptr_factory_.GetWeakPtr()));
}

void BraveWalletAutoPinService::OnTokenListResolved(
std::vector<BlockchainTokenPtr> token_list) {
bool autopin_enabled = IsAutoPinEnabled();
// Resolves list of user tokens.
// Check whether they are pinned or not and posts corresponding tasks.
std::set<std::string> known_tokens =
brave_wallet_pin_service_->GetTokens(absl::nullopt);
for (const auto& token : token_list) {
if (!token->is_nft) {
if (!BraveWalletPinService::IsTokenSupportedForPinning(token)) {
continue;
}
auto current_token_path =
BraveWalletPinService::GetPath(absl::nullopt, token);
BraveWalletPinService::GetTokenPrefPath(absl::nullopt, token);
if (!current_token_path) {
continue;
}
Expand Down Expand Up @@ -113,6 +118,8 @@ void BraveWalletAutoPinService::OnTokenListResolved(
AddOrExecute(std::make_unique<IntentData>(token, Operation::kDelete,
absl::nullopt));
} else if (status->code == mojom::TokenPinStatusCode::STATUS_PINNED) {
// Pinned tokens should be verified for entirety time to time.
// We should check that related CIDs are still pinned.
auto t1 = status->validate_time;
if ((base::Time::Now() - t1) > base::Days(1) || t1 > base::Time::Now()) {
AddOrExecute(std::make_unique<IntentData>(token, Operation::kValidate,
Expand All @@ -121,8 +128,11 @@ void BraveWalletAutoPinService::OnTokenListResolved(
}
}

// Tokens that were previously pinned but not listed in the wallet should be
// unpinned.
for (const auto& t : known_tokens) {
mojom::BlockchainTokenPtr token = BraveWalletPinService::TokenFromPath(t);
mojom::BlockchainTokenPtr token =
BraveWalletPinService::TokenFromPrefPath(t);
if (token) {
AddOrExecute(std::make_unique<IntentData>(token, Operation::kDelete,
absl::nullopt));
Expand All @@ -132,15 +142,13 @@ void BraveWalletAutoPinService::OnTokenListResolved(
CheckQueue();
}

void BraveWalletAutoPinService::PostPinToken(BlockchainTokenPtr token,
PostPinTokenCallback callback) {
void BraveWalletAutoPinService::PostPinToken(BlockchainTokenPtr token) {
queue_.push_back(
std::make_unique<IntentData>(token, Operation::kAdd, absl::nullopt));
CheckQueue();
}

void BraveWalletAutoPinService::PostUnpinToken(BlockchainTokenPtr token,
PostPinTokenCallback callback) {
void BraveWalletAutoPinService::PostUnpinToken(BlockchainTokenPtr token) {
queue_.push_back(
std::make_unique<IntentData>(token, Operation::kDelete, absl::nullopt));
CheckQueue();
Expand All @@ -151,23 +159,23 @@ void BraveWalletAutoPinService::ValidateToken(
brave_wallet_pin_service_->Validate(
data->token->Clone(), data->service,
base::BindOnce(&BraveWalletAutoPinService::OnValidateTaskFinished,
base::Unretained(this)));
weak_ptr_factory_.GetWeakPtr()));
}

void BraveWalletAutoPinService::PinToken(
const std::unique_ptr<IntentData>& data) {
brave_wallet_pin_service_->AddPin(
data->token->Clone(), data->service,
base::BindOnce(&BraveWalletAutoPinService::OnTaskFinished,
base::Unretained(this)));
weak_ptr_factory_.GetWeakPtr()));
}

void BraveWalletAutoPinService::UnpinToken(
const std::unique_ptr<IntentData>& data) {
brave_wallet_pin_service_->RemovePin(
data->token->Clone(), data->service,
base::BindOnce(&BraveWalletAutoPinService::OnTaskFinished,
base::Unretained(this)));
weak_ptr_factory_.GetWeakPtr()));
}

void BraveWalletAutoPinService::AddOrExecute(std::unique_ptr<IntentData> data) {
Expand Down
37 changes: 18 additions & 19 deletions components/brave_wallet/browser/brave_wallet_auto_pin_service.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -30,19 +30,6 @@ using brave_wallet::mojom::BlockchainTokenPtr;

namespace brave_wallet {

enum Operation { kAdd = 0, kDelete = 1, kValidate = 2 };

struct IntentData {
BlockchainTokenPtr token;
Operation operation;
absl::optional<std::string> service;
size_t attempt = 0;
IntentData(const BlockchainTokenPtr& token,
Operation operation,
absl::optional<std::string> service);
~IntentData();
};

class BraveWalletAutoPinService
: public KeyedService,
public brave_wallet::mojom::WalletAutoPinService,
Expand All @@ -59,16 +46,28 @@ class BraveWalletAutoPinService
void SetAutoPinEnabled(bool enabled) override;
void IsAutoPinEnabled(IsAutoPinEnabledCallback callback) override;

void PostPinToken(BlockchainTokenPtr token,
PostPinTokenCallback callback) override;
void PostUnpinToken(BlockchainTokenPtr token,
PostUnpinTokenCallback callback) override;

// BraveWalletServiceTokenObserver
void OnTokenAdded(mojom::BlockchainTokenPtr token) override;
void OnTokenRemoved(mojom::BlockchainTokenPtr token) override;

private:
enum Operation { kAdd = 0, kDelete = 1, kValidate = 2 };

struct IntentData {
BlockchainTokenPtr token;
Operation operation;
absl::optional<std::string> service;
size_t attempt = 0;
IntentData(const BlockchainTokenPtr& token,
Operation operation,
absl::optional<std::string> service);
~IntentData();
};

void PostPinToken(BlockchainTokenPtr token);
void PostUnpinToken(BlockchainTokenPtr token);

// Iterates through user tokens and manages their pin statuses.
void Restore();
void OnTokenListResolved(std::vector<BlockchainTokenPtr>);

Expand Down
Loading

0 comments on commit 411a98b

Please sign in to comment.