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
;
29 class PushMessagingBrowserTest
;
32 // The platform notification service is the profile-agnostic entry point through
33 // which Web Notifications can be controlled.
34 class PlatformNotificationServiceImpl
35 : public content::PlatformNotificationService
{
37 // Returns the active instance of the service in the browser process. Safe to
38 // be called from any thread.
39 static PlatformNotificationServiceImpl
* GetInstance();
41 // To be called when a persistent notification has been clicked on. The
42 // Service Worker associated with the registration will be started if
43 // needed, on which the event will be fired. Must be called on the UI thread.
44 void OnPersistentNotificationClick(
45 content::BrowserContext
* browser_context
,
46 int64_t persistent_notification_id
,
48 int action_index
) const;
50 // To be called when a persistent notification has been closed. The data
51 // associated with the notification has to be pruned from the database in this
52 // case, to make sure that it continues to be in sync. Must be called on the
54 void OnPersistentNotificationClose(
55 content::BrowserContext
* browser_context
,
56 int64_t persistent_notification_id
,
57 const GURL
& origin
) const;
59 // Returns the Notification UI Manager through which notifications can be
60 // displayed to the user. Can be overridden for testing.
61 NotificationUIManager
* GetNotificationUIManager() const;
63 // Open the Notification settings screen when clicking the right button.
64 // Returns |true| if the settings screen could be successfully opened.
65 bool OpenNotificationSettings(content::BrowserContext
* browser_context
);
67 // content::PlatformNotificationService implementation.
68 blink::WebNotificationPermission
CheckPermissionOnUIThread(
69 content::BrowserContext
* browser_context
,
71 int render_process_id
) override
;
72 blink::WebNotificationPermission
CheckPermissionOnIOThread(
73 content::ResourceContext
* resource_context
,
75 int render_process_id
) override
;
76 void DisplayNotification(
77 content::BrowserContext
* browser_context
,
80 const content::PlatformNotificationData
& notification_data
,
81 scoped_ptr
<content::DesktopNotificationDelegate
> delegate
,
82 base::Closure
* cancel_callback
) override
;
83 void DisplayPersistentNotification(
84 content::BrowserContext
* browser_context
,
85 int64_t persistent_notification_id
,
88 const content::PlatformNotificationData
& notification_data
) override
;
89 void ClosePersistentNotification(
90 content::BrowserContext
* browser_context
,
91 int64_t persistent_notification_id
) override
;
92 bool GetDisplayedPersistentNotifications(
93 content::BrowserContext
* browser_context
,
94 std::set
<std::string
>* displayed_notifications
) override
;
97 friend struct base::DefaultSingletonTraits
<PlatformNotificationServiceImpl
>;
98 friend class PlatformNotificationServiceBrowserTest
;
99 friend class PlatformNotificationServiceTest
;
100 friend class PushMessagingBrowserTest
;
101 FRIEND_TEST_ALL_PREFIXES(PlatformNotificationServiceTest
,
102 CreateNotificationFromData
);
103 FRIEND_TEST_ALL_PREFIXES(PlatformNotificationServiceTest
,
104 DisplayNameForContextMessage
);
106 PlatformNotificationServiceImpl();
107 ~PlatformNotificationServiceImpl() override
;
109 // Creates a new Web Notification-based Notification object.
110 // TODO(peter): |delegate| can be a scoped_refptr, but properly passing this
111 // through requires changing a whole lot of Notification constructor calls.
112 Notification
CreateNotificationFromData(
115 const SkBitmap
& icon
,
116 const content::PlatformNotificationData
& notification_data
,
117 NotificationDelegate
* delegate
) const;
119 // Overrides the Notification UI Manager to use to |manager|. Only to be
120 // used by tests. Tests are responsible for cleaning up after themselves.
121 void SetNotificationUIManagerForTesting(NotificationUIManager
* manager
);
123 // Returns a display name for an origin, to be used in the context message
124 base::string16
DisplayNameForContextMessage(Profile
* profile
,
125 const GURL
& origin
) const;
127 // Weak reference. Ownership maintains with the test.
128 NotificationUIManager
* notification_ui_manager_for_tests_
;
130 // Mapping between a persistent notification id and the id of the associated
131 // message_center::Notification object. Must only be used on the UI thread.
132 std::map
<int64_t, std::string
> persistent_notifications_
;
134 DISALLOW_COPY_AND_ASSIGN(PlatformNotificationServiceImpl
);
137 #endif // CHROME_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_SERVICE_IMPL_H_