Commit 4d7a4631 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Update Privet notification to use NotificationDisplayService.

This re-lands 61774b21. Now that
NativeNotificationDisplay passes off to the MessageCenter for platform
bridges that don't yet handle TRANSIENT, there will be no net change
for those platforms.

Bug: 783018
Change-Id: Iec09815faf6e71cc3622edddad299c357af4ee67
Reviewed-on: https://chromium-review.googlesource.com/807400Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521733}
parent c42a6705
......@@ -13,11 +13,12 @@
#include "base/metrics/histogram_macros.h"
#include "base/rand_util.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.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/notifications/notification_ui_manager.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_handler.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/profiles/profile.h"
......@@ -241,6 +242,27 @@ void PrivetNotificationService::PrivetNotify(int devices_active,
bool added) {
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;
rich_notification_data.buttons.push_back(
message_center::ButtonInfo(l10n_util::GetStringUTF16(
......@@ -267,21 +289,19 @@ void PrivetNotificationService::PrivetNotify(int devices_active,
kPrivetNotificationID),
rich_notification_data, CreateNotificationDelegate(profile));
auto* notification_ui_manager = g_browser_process->notification_ui_manager();
bool updated = notification_ui_manager->Update(notification, profile);
if (!updated && added &&
!local_discovery::LocalDiscoveryUIHandler::GetHasVisible()) {
if (add_new_notification)
ReportPrivetUmaEvent(PRIVET_NOTIFICATION_SHOWN);
notification_ui_manager->Add(notification, profile);
}
NotificationDisplayService::GetForProfile(
Profile::FromBrowserContext(profile_))
->Display(NotificationHandler::Type::TRANSIENT, notification);
}
void PrivetNotificationService::PrivetRemoveNotification() {
ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CANCELED);
Profile* profile_object = Profile::FromBrowserContext(profile_);
g_browser_process->notification_ui_manager()->CancelById(
kPrivetNotificationID,
NotificationUIManager::GetProfileID(profile_object));
NotificationDisplayService::GetForProfile(
Profile::FromBrowserContext(profile_))
->Close(NotificationHandler::Type::TRANSIENT, kPrivetNotificationID);
}
void PrivetNotificationService::Start() {
......@@ -386,8 +406,8 @@ void PrivetNotificationDelegate::DisableNotifications() {
}
void PrivetNotificationDelegate::CloseNotification() {
g_browser_process->notification_ui_manager()->CancelById(
kPrivetNotificationID, NotificationUIManager::GetProfileID(profile_));
NotificationDisplayService::GetForProfile(profile_)->Close(
NotificationHandler::Type::TRANSIENT, kPrivetNotificationID);
}
} // namespace cloud_print
......@@ -121,6 +121,12 @@ class PrivetNotificationService
void OnNotificationsEnabledChanged();
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 PrivetNotificationDelegate* CreateNotificationDelegate(
Profile* profile);
......
......@@ -290,6 +290,8 @@ class PrivetNotificationsNotificationTest : public testing::Test {
TEST_F(PrivetNotificationsNotificationTest, AddToCloudPrint) {
TestPrivetNotificationService service(profile());
service.PrivetNotify(1 /* devices_active */, true /* added */);
// The notification is added asynchronously.
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1U, ui_manager()->GetNotificationCount());
const auto& notification = ui_manager()->GetNotificationAt(0);
......@@ -304,6 +306,8 @@ TEST_F(PrivetNotificationsNotificationTest, AddToCloudPrint) {
TEST_F(PrivetNotificationsNotificationTest, DontShowAgain) {
TestPrivetNotificationService service(profile());
service.PrivetNotify(1 /* devices_active */, true /* added */);
// The notification is added asynchronously.
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1U, ui_manager()->GetNotificationCount());
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