Commit 61774b21 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Update Privet notification to use NotificationDisplayService.

This one is a little more complicated because "AddOrUpdate" is not
sufficient --- in some cases an existing notification should be updated
but a new one should not be added.

Bug: 783018
Change-Id: I7517b7125dd6e06d7f00fcfd4a678b876c96054a
Reviewed-on: https://chromium-review.googlesource.com/772352
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517272}
parent 977ebe42
...@@ -13,11 +13,12 @@ ...@@ -13,11 +13,12 @@
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/rand_util.h" #include "base/rand_util.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/local_discovery/service_discovery_shared_client.h" #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
#include "chrome/browser/notifications/notification_ui_manager.h" #include "chrome/browser/notifications/notification_common.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/printing/cloud_print/privet_device_lister_impl.h" #include "chrome/browser/printing/cloud_print/privet_device_lister_impl.h"
#include "chrome/browser/printing/cloud_print/privet_http_asynchronous_factory.h" #include "chrome/browser/printing/cloud_print/privet_http_asynchronous_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
...@@ -241,6 +242,27 @@ void PrivetNotificationService::PrivetNotify(int devices_active, ...@@ -241,6 +242,27 @@ void PrivetNotificationService::PrivetNotify(int devices_active,
bool added) { bool added) {
DCHECK_GT(devices_active, 0); DCHECK_GT(devices_active, 0);
NotificationDisplayService::GetForProfile(
Profile::FromBrowserContext(profile_))
->GetDisplayed(base::Bind(&PrivetNotificationService::AddNotification,
AsWeakPtr(), devices_active, added));
}
void PrivetNotificationService::AddNotification(
int devices_active,
bool device_added,
std::unique_ptr<std::set<std::string>> displayed_notifications,
bool supports_synchronization) {
// If the UI is already open or a device was removed, we'll update the
// existing notification but not add a new one.
const bool notification_exists =
base::ContainsKey(*displayed_notifications, kPrivetNotificationID);
const bool add_new_notification =
device_added &&
!local_discovery::LocalDiscoveryUIHandler::GetHasVisible();
if (!notification_exists && !add_new_notification)
return;
message_center::RichNotificationData rich_notification_data; message_center::RichNotificationData rich_notification_data;
rich_notification_data.buttons.push_back( rich_notification_data.buttons.push_back(
message_center::ButtonInfo(l10n_util::GetStringUTF16( message_center::ButtonInfo(l10n_util::GetStringUTF16(
...@@ -267,21 +289,19 @@ void PrivetNotificationService::PrivetNotify(int devices_active, ...@@ -267,21 +289,19 @@ void PrivetNotificationService::PrivetNotify(int devices_active,
kPrivetNotificationID), kPrivetNotificationID),
rich_notification_data, CreateNotificationDelegate(profile)); rich_notification_data, CreateNotificationDelegate(profile));
auto* notification_ui_manager = g_browser_process->notification_ui_manager(); if (add_new_notification)
bool updated = notification_ui_manager->Update(notification, profile);
if (!updated && added &&
!local_discovery::LocalDiscoveryUIHandler::GetHasVisible()) {
ReportPrivetUmaEvent(PRIVET_NOTIFICATION_SHOWN); ReportPrivetUmaEvent(PRIVET_NOTIFICATION_SHOWN);
notification_ui_manager->Add(notification, profile);
} NotificationDisplayService::GetForProfile(
Profile::FromBrowserContext(profile_))
->Display(NotificationCommon::TRANSIENT, notification);
} }
void PrivetNotificationService::PrivetRemoveNotification() { void PrivetNotificationService::PrivetRemoveNotification() {
ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CANCELED); ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CANCELED);
Profile* profile_object = Profile::FromBrowserContext(profile_); NotificationDisplayService::GetForProfile(
g_browser_process->notification_ui_manager()->CancelById( Profile::FromBrowserContext(profile_))
kPrivetNotificationID, ->Close(NotificationCommon::TRANSIENT, kPrivetNotificationID);
NotificationUIManager::GetProfileID(profile_object));
} }
void PrivetNotificationService::Start() { void PrivetNotificationService::Start() {
...@@ -387,8 +407,8 @@ void PrivetNotificationDelegate::DisableNotifications() { ...@@ -387,8 +407,8 @@ void PrivetNotificationDelegate::DisableNotifications() {
} }
void PrivetNotificationDelegate::CloseNotification() { void PrivetNotificationDelegate::CloseNotification() {
g_browser_process->notification_ui_manager()->CancelById( NotificationDisplayService::GetForProfile(profile_)->Close(
kPrivetNotificationID, NotificationUIManager::GetProfileID(profile_)); NotificationCommon::TRANSIENT, kPrivetNotificationID);
} }
} // namespace cloud_print } // namespace cloud_print
...@@ -121,6 +121,12 @@ class PrivetNotificationService ...@@ -121,6 +121,12 @@ class PrivetNotificationService
void OnNotificationsEnabledChanged(); void OnNotificationsEnabledChanged();
void StartLister(); void StartLister();
void AddNotification(
int devices_active,
bool device_added,
std::unique_ptr<std::set<std::string>> displayed_notifications,
bool supports_synchronization);
// Virtual for testing. The returned delegate is refcounted. // Virtual for testing. The returned delegate is refcounted.
virtual PrivetNotificationDelegate* CreateNotificationDelegate( virtual PrivetNotificationDelegate* CreateNotificationDelegate(
Profile* profile); Profile* profile);
......
...@@ -290,6 +290,8 @@ class PrivetNotificationsNotificationTest : public testing::Test { ...@@ -290,6 +290,8 @@ class PrivetNotificationsNotificationTest : public testing::Test {
TEST_F(PrivetNotificationsNotificationTest, AddToCloudPrint) { TEST_F(PrivetNotificationsNotificationTest, AddToCloudPrint) {
TestPrivetNotificationService service(profile()); TestPrivetNotificationService service(profile());
service.PrivetNotify(1 /* devices_active */, true /* added */); service.PrivetNotify(1 /* devices_active */, true /* added */);
// The notification is added asynchronously.
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1U, ui_manager()->GetNotificationCount()); ASSERT_EQ(1U, ui_manager()->GetNotificationCount());
const auto& notification = ui_manager()->GetNotificationAt(0); const auto& notification = ui_manager()->GetNotificationAt(0);
...@@ -304,6 +306,8 @@ TEST_F(PrivetNotificationsNotificationTest, AddToCloudPrint) { ...@@ -304,6 +306,8 @@ TEST_F(PrivetNotificationsNotificationTest, AddToCloudPrint) {
TEST_F(PrivetNotificationsNotificationTest, DontShowAgain) { TEST_F(PrivetNotificationsNotificationTest, DontShowAgain) {
TestPrivetNotificationService service(profile()); TestPrivetNotificationService service(profile());
service.PrivetNotify(1 /* devices_active */, true /* added */); service.PrivetNotify(1 /* devices_active */, true /* added */);
// The notification is added asynchronously.
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1U, ui_manager()->GetNotificationCount()); ASSERT_EQ(1U, ui_manager()->GetNotificationCount());
const auto& notification = ui_manager()->GetNotificationAt(0); const auto& notification = ui_manager()->GetNotificationAt(0);
......
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