Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2064)

Unified Diff: chrome/browser/extensions/api/notifications/extension_notification_display_helper.cc

Issue 2703213004: Migrate extension notifications to the new NotificationDisplayService (Closed)
Patch Set: Migrate extension notifications to the new NotificationDisplayService Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/notifications/extension_notification_display_helper.cc
diff --git a/chrome/browser/extensions/api/notifications/extension_notification_display_helper.cc b/chrome/browser/extensions/api/notifications/extension_notification_display_helper.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cfbdfc490ee940bd72e41aad80f3cb9c1b521a2b
--- /dev/null
+++ b/chrome/browser/extensions/api/notifications/extension_notification_display_helper.cc
@@ -0,0 +1,95 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/api/notifications/extension_notification_display_helper.h"
+
+#include <algorithm>
+
+#include "base/memory/ptr_util.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/notifications/notification.h"
+#include "chrome/browser/notifications/notification_display_service.h"
+#include "chrome/browser/notifications/notification_display_service_factory.h"
+#include "url/gurl.h"
+
+namespace extensions {
+
+ExtensionNotificationDisplayHelper::ExtensionNotificationDisplayHelper(
+ Profile* profile)
+ : profile_(profile) {}
+
+ExtensionNotificationDisplayHelper::~ExtensionNotificationDisplayHelper() {}
+
+void ExtensionNotificationDisplayHelper::Display(
+ const Notification& notification) {
+ // Remove the previous version of this notification if the |notification| is
+ // updating another notification.
+ EraseDataForNotificationId(notification.delegate_id());
+
+ notifications_.push_back(base::MakeUnique<Notification>(notification));
+
+ GetDisplayService()->Display(NotificationCommon::EXTENSION,
+ notification.delegate_id(), notification);
+}
+
+Notification* ExtensionNotificationDisplayHelper::GetByNotificationId(
+ const std::string& notification_id) {
+ for (const auto& notification : notifications_) {
+ if (notification->delegate_id() == notification_id)
+ return notification.get();
+ }
+
+ return nullptr;
+}
+
+std::set<std::string>
+ExtensionNotificationDisplayHelper::GetNotificationIdsForExtension(
+ const GURL& extension_origin) const {
+ std::set<std::string> notification_ids;
+ for (const auto& notification : notifications_) {
+ if (notification->origin_url() == extension_origin)
+ notification_ids.insert(notification->delegate_id());
+ }
+
+ return notification_ids;
+}
+
+bool ExtensionNotificationDisplayHelper::EraseDataForNotificationId(
+ const std::string& notification_id) {
+ auto iter = std::find_if(
+ notifications_.begin(), notifications_.end(),
+ [notification_id](const std::unique_ptr<Notification>& notification) {
+ return notification->delegate_id() == notification_id;
+ });
+
+ if (iter == notifications_.end())
+ return false;
+
+ notifications_.erase(iter);
+ return true;
+}
+
+bool ExtensionNotificationDisplayHelper::Close(
+ const std::string& notification_id) {
+ if (!EraseDataForNotificationId(notification_id))
+ return false;
+
+ GetDisplayService()->Close(NotificationCommon::EXTENSION, notification_id);
+ return true;
+}
+
+void ExtensionNotificationDisplayHelper::Shutdown() {
+ // Do not call GetDisplayService()->Close() here. The Shutdown method is
+ // called upon profile destruction and closing a notification requires a
+ // profile, as it may dispatch an event to the extension.
+
+ notifications_.clear();
+}
+
+NotificationDisplayService*
+ExtensionNotificationDisplayHelper::GetDisplayService() {
+ return NotificationDisplayServiceFactory::GetForProfile(profile_);
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698