-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added WalletNotificationService to show notifications about transactions
- Loading branch information
1 parent
71d9351
commit d15297e
Showing
12 changed files
with
441 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. | ||
|
||
brave_browser_brave_wallet_sources = [] | ||
|
||
if (!is_android) { | ||
brave_browser_brave_wallet_sources += [ | ||
"//brave/browser/brave_wallet/notifications/wallet_notification_helper_impl.cc", | ||
"//brave/browser/brave_wallet/notifications/wallet_notification_service.cc", | ||
"//brave/browser/brave_wallet/notifications/wallet_notification_service.h", | ||
"//brave/browser/brave_wallet/notifications/wallet_notification_service_factory.cc", | ||
"//brave/browser/brave_wallet/notifications/wallet_notification_service_factory.h", | ||
] | ||
} |
21 changes: 21 additions & 0 deletions
21
browser/brave_wallet/notifications/wallet_notification_helper_impl.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/brave_wallet/wallet_notification_helper.h" | ||
|
||
#include "brave/browser/brave_wallet/notifications/wallet_notification_service_factory.h" | ||
#include "brave/components/brave_wallet/browser/tx_service.h" | ||
|
||
namespace brave_wallet { | ||
|
||
void RegisterWalletNotificationService(content::BrowserContext* context, | ||
TxService* tx_service) { | ||
auto* notification_service = | ||
WalletNotificationServiceFactory::GetInstance()->GetServiceForContext( | ||
context); | ||
tx_service->AddObserver(notification_service->GetReceiver()); | ||
} | ||
|
||
} // namespace brave_wallet |
106 changes: 106 additions & 0 deletions
106
browser/brave_wallet/notifications/wallet_notification_service.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/brave_wallet/notifications/wallet_notification_service.h" | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "base/strings/utf_string_conversions.h" | ||
#include "brave/components/brave_wallet/browser/tx_service.h" | ||
#include "chrome/browser/notifications/notification_display_service.h" | ||
#include "chrome/browser/notifications/notification_display_service_factory.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "components/grit/brave_components_strings.h" | ||
#include "ui/base/l10n/l10n_util.h" | ||
#include "ui/base/models/image_model.h" | ||
#include "ui/message_center/public/cpp/notification.h" | ||
#include "ui/message_center/public/cpp/notification_types.h" | ||
#include "ui/message_center/public/cpp/notifier_id.h" | ||
|
||
namespace { | ||
int GetStatusTitle(brave_wallet::mojom::TransactionStatus status) { | ||
switch (status) { | ||
case brave_wallet::mojom::TransactionStatus::Confirmed: | ||
return IDS_WALLET_TRANSACTION_STATUS_UPDATE_MESSAGE_TITLE_CONFIRMED; | ||
case brave_wallet::mojom::TransactionStatus::Error: | ||
return IDS_WALLET_TRANSACTION_STATUS_UPDATE_MESSAGE_TITLE_ERROR; | ||
case brave_wallet::mojom::TransactionStatus::Dropped: | ||
return IDS_WALLET_TRANSACTION_STATUS_UPDATE_MESSAGE_TITLE_DROPPED; | ||
default: | ||
break; | ||
} | ||
VLOG(1) << "No title for " << int(status) << " transaction status"; | ||
return -1; | ||
} | ||
|
||
std::unique_ptr<message_center::Notification> CreateMessageCenterNotification( | ||
const std::u16string& title, | ||
const std::u16string& body, | ||
const std::string& uuid, | ||
const GURL& link) { | ||
message_center::RichNotificationData notification_data; | ||
// hack to prevent origin from showing in the notification | ||
notification_data.context_message = u" "; | ||
auto notification = std::make_unique<message_center::Notification>( | ||
message_center::NOTIFICATION_TYPE_SIMPLE, uuid, title, body, | ||
ui::ImageModel(), std::u16string(), link, | ||
message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT, | ||
"service.wallet"), | ||
notification_data, nullptr); | ||
|
||
return notification; | ||
} | ||
|
||
void PushNotification(content::BrowserContext* context, | ||
const std::string& uuid, | ||
const std::string& from, | ||
const std::u16string& title, | ||
const std::u16string& body) { | ||
auto notification = CreateMessageCenterNotification( | ||
title, body, uuid, | ||
GURL("brave://wallet/crypto/accounts/" + from + "#" + uuid)); | ||
auto* profile = Profile::FromBrowserContext(context); | ||
NotificationDisplayServiceFactory::GetForProfile(profile)->Display( | ||
NotificationHandler::Type::SEND_TAB_TO_SELF, *notification, nullptr); | ||
} | ||
|
||
} // namespace | ||
|
||
namespace brave_wallet { | ||
|
||
WalletNotificationService::WalletNotificationService( | ||
content::BrowserContext* context) | ||
: context_(context) {} | ||
|
||
WalletNotificationService::~WalletNotificationService() {} | ||
|
||
bool WalletNotificationService::ShouldDisplayUserNotification( | ||
mojom::TransactionStatus status) { | ||
return (status == mojom::TransactionStatus::Confirmed || | ||
status == mojom::TransactionStatus::Error || | ||
status == mojom::TransactionStatus::Dropped); | ||
} | ||
|
||
void WalletNotificationService::DisplayUserNotification( | ||
mojom::TransactionStatus status, | ||
const std::string& address, | ||
const std::string& tx_id) { | ||
PushNotification(context_, tx_id, address, | ||
l10n_util::GetStringUTF16(GetStatusTitle(status)), | ||
l10n_util::GetStringFUTF16( | ||
IDS_WALLET_TRANSACTION_STATUS_UPDATE_MESSAGE_TEXT, | ||
base::UTF8ToUTF16(address))); | ||
} | ||
|
||
void WalletNotificationService::OnTransactionStatusChanged( | ||
mojom::TransactionInfoPtr tx_info) { | ||
if (ShouldDisplayUserNotification(tx_info->tx_status)) { | ||
DisplayUserNotification(tx_info->tx_status, tx_info->from_address, | ||
tx_info->id); | ||
} | ||
} | ||
|
||
} // namespace brave_wallet |
55 changes: 55 additions & 0 deletions
55
browser/brave_wallet/notifications/wallet_notification_service.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_BROWSER_BRAVE_WALLET_NOTIFICATIONS_WALLET_NOTIFICATION_SERVICE_H_ | ||
#define BRAVE_BROWSER_BRAVE_WALLET_NOTIFICATIONS_WALLET_NOTIFICATION_SERVICE_H_ | ||
|
||
#include <string> | ||
|
||
#include "base/memory/raw_ptr.h" | ||
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h" | ||
#include "components/keyed_service/core/keyed_service.h" | ||
#include "mojo/public/cpp/bindings/pending_remote.h" | ||
#include "mojo/public/cpp/bindings/receiver.h" | ||
|
||
namespace content { | ||
class BrowserContext; | ||
} // namespace content | ||
|
||
namespace brave_wallet { | ||
|
||
class WalletNotificationService : public KeyedService, | ||
public mojom::TxServiceObserver { | ||
public: | ||
explicit WalletNotificationService(content::BrowserContext* context); | ||
~WalletNotificationService() override; | ||
WalletNotificationService(const WalletNotificationService&) = delete; | ||
WalletNotificationService operator=(const WalletNotificationService&) = | ||
delete; | ||
|
||
// mojom::TxServiceObserver | ||
void OnNewUnapprovedTx(mojom::TransactionInfoPtr tx_info) override {} | ||
void OnUnapprovedTxUpdated(mojom::TransactionInfoPtr tx_info) override {} | ||
void OnTransactionStatusChanged(mojom::TransactionInfoPtr tx_info) override; | ||
|
||
mojo::PendingRemote<mojom::TxServiceObserver> GetReceiver() { | ||
return tx_observer_receiver_.BindNewPipeAndPassRemote(); | ||
} | ||
|
||
private: | ||
friend class WalletNotificationServiceUnitTest; | ||
|
||
bool ShouldDisplayUserNotification(mojom::TransactionStatus status); | ||
void DisplayUserNotification(mojom::TransactionStatus status, | ||
const std::string& address, | ||
const std::string& tx_id); | ||
|
||
raw_ptr<content::BrowserContext> context_; | ||
mojo::Receiver<mojom::TxServiceObserver> tx_observer_receiver_{this}; | ||
}; | ||
|
||
} // namespace brave_wallet | ||
|
||
#endif // BRAVE_BROWSER_BRAVE_WALLET_NOTIFICATIONS_WALLET_NOTIFICATION_SERVICE_H_ |
50 changes: 50 additions & 0 deletions
50
browser/brave_wallet/notifications/wallet_notification_service_factory.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/brave_wallet/notifications/wallet_notification_service_factory.h" | ||
|
||
#include <memory> | ||
|
||
#include "brave/browser/brave_wallet/brave_wallet_context_utils.h" | ||
#include "brave/browser/brave_wallet/notifications/wallet_notification_service.h" | ||
#include "brave/browser/brave_wallet/tx_service_factory.h" | ||
#include "chrome/browser/notifications/notification_display_service_factory.h" | ||
#include "components/keyed_service/content/browser_context_dependency_manager.h" | ||
|
||
namespace brave_wallet { | ||
|
||
// static | ||
WalletNotificationServiceFactory* | ||
WalletNotificationServiceFactory::GetInstance() { | ||
return base::Singleton<WalletNotificationServiceFactory>::get(); | ||
} | ||
|
||
WalletNotificationServiceFactory::WalletNotificationServiceFactory() | ||
: BrowserContextKeyedServiceFactory( | ||
"WalletNotificationService", | ||
BrowserContextDependencyManager::GetInstance()) { | ||
DependsOn(NotificationDisplayServiceFactory::GetInstance()); | ||
DependsOn(brave_wallet::TxServiceFactory::GetInstance()); | ||
} | ||
|
||
WalletNotificationServiceFactory::~WalletNotificationServiceFactory() {} | ||
|
||
KeyedService* WalletNotificationServiceFactory::BuildServiceInstanceFor( | ||
content::BrowserContext* context) const { | ||
return new WalletNotificationService(context); | ||
} | ||
|
||
// static | ||
WalletNotificationService* | ||
WalletNotificationServiceFactory::GetServiceForContext( | ||
content::BrowserContext* context) { | ||
if (!IsAllowedForContext(context)) { | ||
return nullptr; | ||
} | ||
return static_cast<WalletNotificationService*>( | ||
GetInstance()->GetServiceForBrowserContext(context, true)); | ||
} | ||
|
||
} // namespace brave_wallet |
42 changes: 42 additions & 0 deletions
42
browser/brave_wallet/notifications/wallet_notification_service_factory.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_BROWSER_BRAVE_WALLET_NOTIFICATIONS_WALLET_NOTIFICATION_SERVICE_FACTORY_H_ | ||
#define BRAVE_BROWSER_BRAVE_WALLET_NOTIFICATIONS_WALLET_NOTIFICATION_SERVICE_FACTORY_H_ | ||
|
||
#include "base/memory/singleton.h" | ||
#include "brave/browser/brave_wallet/notifications/wallet_notification_service.h" | ||
#include "components/keyed_service/content/browser_context_keyed_service_factory.h" | ||
|
||
namespace brave_wallet { | ||
|
||
// Singleton that owns all WalletNotificationService and associates them with | ||
// BrowserContext. | ||
class WalletNotificationServiceFactory | ||
: public BrowserContextKeyedServiceFactory { | ||
public: | ||
WalletNotificationServiceFactory(const WalletNotificationServiceFactory&) = | ||
delete; | ||
WalletNotificationServiceFactory& operator=( | ||
const WalletNotificationServiceFactory&) = delete; | ||
|
||
static WalletNotificationServiceFactory* GetInstance(); | ||
static WalletNotificationService* GetServiceForContext( | ||
content::BrowserContext* context); | ||
|
||
private: | ||
friend struct base::DefaultSingletonTraits<WalletNotificationServiceFactory>; | ||
|
||
WalletNotificationServiceFactory(); | ||
~WalletNotificationServiceFactory() override; | ||
|
||
// BrowserContextKeyedServiceFactory: | ||
KeyedService* BuildServiceInstanceFor( | ||
content::BrowserContext* context) const override; | ||
}; | ||
|
||
} // namespace brave_wallet | ||
|
||
#endif // BRAVE_BROWSER_BRAVE_WALLET_NOTIFICATIONS_WALLET_NOTIFICATION_SERVICE_FACTORY_H_ |
Oops, something went wrong.