Commit 9b365a74 authored by Owen Min's avatar Owen Min Committed by Commit Bot

Add callback for ExtensionRequestNotification

The callback will be invoked when the notification is closed. It also
returns whether the notification is closed by user action or not.

Bug: 1006899
Change-Id: I339fc43e32c1ab101957867a9d871b479fa9d060
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2003339Reviewed-by: default avatarJulian Pastarmov <pastarmovj@chromium.org>
Commit-Queue: Owen Min <zmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#732857}
parent 8078179c
......@@ -58,12 +58,14 @@ ExtensionRequestNotification::ExtensionRequestNotification(
ExtensionRequestNotification::~ExtensionRequestNotification() = default;
void ExtensionRequestNotification::Show() {
void ExtensionRequestNotification::Show(NotificationCloseCallback callback) {
if (extension_ids_.empty()) {
NOTREACHED();
return;
}
callback_ = std::move(callback);
const base::string16 title = l10n_util::GetPluralStringFUTF16(
kNotificationTitles[notify_type_], extension_ids_.size());
const base::string16 body = l10n_util::GetPluralStringFUTF16(
......@@ -80,22 +82,23 @@ void ExtensionRequestNotification::Show() {
message_center::NotifierId(message_center::NotifierType::APPLICATION,
kExtensionRequestNotifierId),
message_center::RichNotificationData(),
new message_center::HandleNotificationClickDelegate(base::BindRepeating(
&ExtensionRequestNotification::OnClick, weak_factory_.GetWeakPtr())));
base::MakeRefCounted<message_center::ThunkNotificationDelegate>(
weak_factory_.GetWeakPtr()));
notification_->set_never_timeout(true);
NotificationDisplayService::GetForProfile(profile_)->Display(
NotificationHandler::Type::TRANSIENT, *notification_, nullptr);
}
void ExtensionRequestNotification::Close() {
void ExtensionRequestNotification::CloseNotification() {
NotificationDisplayService::GetForProfile(profile_)->Close(
NotificationHandler::Type::TRANSIENT, kNotificationIds[notify_type_]);
notification_.reset();
}
void ExtensionRequestNotification::OnClick(
const base::Optional<int> button_index) {
void ExtensionRequestNotification::Click(
const base::Optional<int>& button_index,
const base::Optional<base::string16>& reply) {
for (const std::string& extension_id : extension_ids_) {
NavigateParams params(profile_, GURL(kChromeWebstoreUrl + extension_id),
ui::PAGE_TRANSITION_LINK);
......@@ -103,7 +106,15 @@ void ExtensionRequestNotification::OnClick(
params.window_action = NavigateParams::SHOW_WINDOW;
Navigate(&params);
}
Close();
if (callback_)
std::move(callback_).Run(true);
CloseNotification();
}
void ExtensionRequestNotification::Close(bool by_user) {
if (callback_) {
std::move(callback_).Run(by_user);
}
}
} // namespace enterprise_reporting
......@@ -15,9 +15,12 @@ class Profile;
namespace enterprise_reporting {
class ExtensionRequestNotification {
class ExtensionRequestNotification
: public message_center::NotificationObserver {
public:
using ExtensionIds = std::vector<std::string>;
// Callback when the notification is closed.
using NotificationCloseCallback = base::OnceCallback<void(bool by_user)>;
enum NotifyType { kApproved = 0, kRejected = 1, kForceInstalled = 2 };
ExtensionRequestNotification();
......@@ -29,17 +32,21 @@ class ExtensionRequestNotification {
delete;
virtual ~ExtensionRequestNotification();
void Show();
void Close();
void Show(NotificationCloseCallback callback);
void CloseNotification();
private:
void OnClick(const base::Optional<int> button_index);
// message_center::NotificationObserver
void Click(const base::Optional<int>& button_index,
const base::Optional<base::string16>& reply) override;
void Close(bool by_user) override;
std::unique_ptr<message_center::Notification> notification_;
Profile* profile_;
const NotifyType notify_type_ = kApproved;
const ExtensionIds extension_ids_;
NotificationCloseCallback callback_;
base::WeakPtrFactory<ExtensionRequestNotification> weak_factory_{this};
};
......
......@@ -34,6 +34,10 @@ const char* const kNotificationTitleKeywords[] = {"approved", "rejected",
const char* const kNotificationBodyKeywords[] = {"to install", "to view",
"to view"};
void OnNotificationClosed(bool expected_by_user, bool by_user) {
EXPECT_EQ(expected_by_user, by_user);
}
class ExtensionRequestNotificationTest
: public BrowserWithTestWindowTest,
public testing::WithParamInterface<
......@@ -71,22 +75,24 @@ TEST_P(ExtensionRequestNotificationTest, NoExtension) {
ExtensionRequestNotification request_notification(
profile(), GetNotifyType(), ExtensionRequestNotification::ExtensionIds());
#if DCHECK_IS_ON()
EXPECT_DEATH_IF_SUPPORTED(request_notification.Show(), "");
EXPECT_DEATH_IF_SUPPORTED(
request_notification.Show(base::BindOnce(&OnNotificationClosed, false)),
"");
#else
request_notification.Show();
request_notification.Show(base::BindOnce(&OnNotificationClosed, false));
#endif
task_environment()->RunUntilIdle();
EXPECT_FALSE(GetNotification().has_value());
}
TEST_P(ExtensionRequestNotificationTest, HasExtension) {
TEST_P(ExtensionRequestNotificationTest, HasExtensionAndClickedByUser) {
ExtensionRequestNotification request_notification(
profile(), GetNotifyType(),
ExtensionRequestNotification::ExtensionIds({kFakeExtensionId}));
base::RunLoop show_run_loop;
display_service_tester_->SetNotificationAddedClosure(
show_run_loop.QuitClosure());
request_notification.Show();
request_notification.Show(base::BindOnce(&OnNotificationClosed, true));
show_run_loop.Run();
base::Optional<message_center::Notification> notification = GetNotification();
......@@ -112,4 +118,26 @@ TEST_P(ExtensionRequestNotificationTest, HasExtension) {
browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
}
TEST_P(ExtensionRequestNotificationTest, HasExtensionAndClosedByBrowser) {
ExtensionRequestNotification request_notification(
profile(), GetNotifyType(),
ExtensionRequestNotification::ExtensionIds({kFakeExtensionId}));
base::RunLoop show_run_loop;
display_service_tester_->SetNotificationAddedClosure(
show_run_loop.QuitClosure());
request_notification.Show(base::BindOnce(&OnNotificationClosed, false));
show_run_loop.Run();
base::Optional<message_center::Notification> notification = GetNotification();
ASSERT_TRUE(notification.has_value());
base::RunLoop close_run_loop;
display_service_tester_->SetNotificationClosedClosure(
close_run_loop.QuitClosure());
request_notification.CloseNotification();
close_run_loop.Run();
EXPECT_FALSE(GetNotification().has_value());
}
} // namespace enterprise_reporting
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment