1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_SERVICE_IMPL_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_SERVICE_IMPL_H_
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/singleton.h"
15 #include "base/strings/string16.h"
16 #include "chrome/browser/notifications/notification.h"
17 #include "content/public/browser/platform_notification_service.h"
18 #include "content/public/common/persistent_notification_status.h"
20 class NotificationDelegate
;
21 class NotificationUIManager
;
25 class PushMessagingBrowserTest
;
28 // The platform notification service is the profile-agnostic entry point through
29 // which Web Notifications can be controlled.
30 class PlatformNotificationServiceImpl
31 : public content::PlatformNotificationService
{
33 // Returns the active instance of the service in the browser process. Safe to
34 // be called from any thread.
35 static PlatformNotificationServiceImpl
* GetInstance();
37 // To be called when a persistent notification has been clicked on. The
38 // Service Worker associated with the registration will be started if
39 // needed, on which the event will be fired. Must be called on the UI thread.
40 void OnPersistentNotificationClick(
41 content::BrowserContext
* browser_context
,
42 int64_t persistent_notification_id
,
44 int action_index
) const;
46 // To be called when a persistent notification has been closed. The data
47 // associated with the notification has to be pruned from the database in this
48 // case, to make sure that it continues to be in sync. Must be called on the
50 void OnPersistentNotificationClose(
51 content::BrowserContext
* browser_context
,
52 int64_t persistent_notification_id
,
53 const GURL
& origin
) const;
55 // Returns the Notification UI Manager through which notifications can be
56 // displayed to the user. Can be overridden for testing.
57 NotificationUIManager
* GetNotificationUIManager() const;
59 // content::PlatformNotificationService implementation.
60 blink::WebNotificationPermission
CheckPermissionOnUIThread(
61 content::BrowserContext
* browser_context
,
63 int render_process_id
) override
;
64 blink::WebNotificationPermission
CheckPermissionOnIOThread(
65 content::ResourceContext
* resource_context
,
67 int render_process_id
) override
;
68 void DisplayNotification(
69 content::BrowserContext
* browser_context
,
72 const content::PlatformNotificationData
& notification_data
,
73 scoped_ptr
<content::DesktopNotificationDelegate
> delegate
,
74 base::Closure
* cancel_callback
) override
;
75 void DisplayPersistentNotification(
76 content::BrowserContext
* browser_context
,
77 int64_t persistent_notification_id
,
80 const content::PlatformNotificationData
& notification_data
) override
;
81 void ClosePersistentNotification(
82 content::BrowserContext
* browser_context
,
83 int64_t persistent_notification_id
) override
;
84 bool GetDisplayedPersistentNotifications(
85 content::BrowserContext
* browser_context
,
86 std::set
<std::string
>* displayed_notifications
) override
;
89 friend struct DefaultSingletonTraits
<PlatformNotificationServiceImpl
>;
90 friend class PlatformNotificationServiceBrowserTest
;
91 friend class PlatformNotificationServiceTest
;
92 friend class PushMessagingBrowserTest
;
93 FRIEND_TEST_ALL_PREFIXES(PlatformNotificationServiceTest
,
94 CreateNotificationFromData
);
95 FRIEND_TEST_ALL_PREFIXES(PlatformNotificationServiceTest
,
96 DisplayNameForContextMessage
);
98 PlatformNotificationServiceImpl();
99 ~PlatformNotificationServiceImpl() override
;
101 // Creates a new Web Notification-based Notification object.
102 // TODO(peter): |delegate| can be a scoped_refptr, but properly passing this
103 // through requires changing a whole lot of Notification constructor calls.
104 Notification
CreateNotificationFromData(
107 const SkBitmap
& icon
,
108 const content::PlatformNotificationData
& notification_data
,
109 NotificationDelegate
* delegate
) const;
111 // Overrides the Notification UI Manager to use to |manager|. Only to be
112 // used by tests. Tests are responsible for cleaning up after themselves.
113 void SetNotificationUIManagerForTesting(NotificationUIManager
* manager
);
115 // Returns a display name for an origin, to be used in the context message
116 base::string16
DisplayNameForContextMessage(Profile
* profile
,
117 const GURL
& origin
) const;
119 // Weak reference. Ownership maintains with the test.
120 NotificationUIManager
* notification_ui_manager_for_tests_
;
122 // Mapping between a persistent notification id and the id of the associated
123 // message_center::Notification object. Must only be used on the UI thread.
124 std::map
<int64_t, std::string
> persistent_notifications_
;
126 DISALLOW_COPY_AND_ASSIGN(PlatformNotificationServiceImpl
);
129 #endif // CHROME_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_SERVICE_IMPL_H_