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
,
43 const GURL
& origin
) const;
45 // To be called when a persistent notification has been closed. The data
46 // associated with the notification has to be pruned from the database in this
47 // case, to make sure that it continues to be in sync. Must be called on the
49 void OnPersistentNotificationClose(
50 content::BrowserContext
* browser_context
,
51 int64_t persistent_notification_id
,
52 const GURL
& origin
) const;
54 // Returns the Notification UI Manager through which notifications can be
55 // displayed to the user. Can be overridden for testing.
56 NotificationUIManager
* GetNotificationUIManager() const;
58 // content::PlatformNotificationService implementation.
59 blink::WebNotificationPermission
CheckPermissionOnUIThread(
60 content::BrowserContext
* browser_context
,
62 int render_process_id
) override
;
63 blink::WebNotificationPermission
CheckPermissionOnIOThread(
64 content::ResourceContext
* resource_context
,
66 int render_process_id
) override
;
67 void DisplayNotification(
68 content::BrowserContext
* browser_context
,
71 const content::PlatformNotificationData
& notification_data
,
72 scoped_ptr
<content::DesktopNotificationDelegate
> delegate
,
73 base::Closure
* cancel_callback
) override
;
74 void DisplayPersistentNotification(
75 content::BrowserContext
* browser_context
,
76 int64_t persistent_notification_id
,
79 const content::PlatformNotificationData
& notification_data
) override
;
80 void ClosePersistentNotification(
81 content::BrowserContext
* browser_context
,
82 int64_t persistent_notification_id
) override
;
83 bool GetDisplayedPersistentNotifications(
84 content::BrowserContext
* browser_context
,
85 std::set
<std::string
>* displayed_notifications
) override
;
88 friend struct DefaultSingletonTraits
<PlatformNotificationServiceImpl
>;
89 friend class PlatformNotificationServiceBrowserTest
;
90 friend class PlatformNotificationServiceTest
;
91 friend class PushMessagingBrowserTest
;
92 FRIEND_TEST_ALL_PREFIXES(
93 PlatformNotificationServiceTest
, DisplayNameForOrigin
);
94 FRIEND_TEST_ALL_PREFIXES(PlatformNotificationServiceTest
,
95 TestWebOriginDisplayName
);
97 PlatformNotificationServiceImpl();
98 ~PlatformNotificationServiceImpl() override
;
100 // Creates a new Web Notification-based Notification object.
101 // TODO(peter): |delegate| can be a scoped_refptr, but properly passing this
102 // through requires changing a whole lot of Notification constructor calls.
103 Notification
CreateNotificationFromData(
106 const SkBitmap
& icon
,
107 const content::PlatformNotificationData
& notification_data
,
108 NotificationDelegate
* delegate
) const;
110 // Overrides the Notification UI Manager to use to |manager|. Only to be
111 // used by tests. Tests are responsible for cleaning up after themselves.
112 void SetNotificationUIManagerForTesting(NotificationUIManager
* manager
);
114 // Returns a display name for an origin, to be used in permission infobar or
115 // on the frame of the notification toast. Different from the origin itself
116 // when dealing with extensions.
117 base::string16
DisplayNameForOrigin(Profile
* profile
,
118 const GURL
& origin
) const;
120 // Translates a URL into a slightly more readable version that may omit
121 // the port and scheme for common cases.
122 // TODO(dewittj): Remove this when the proper function is implemented in a
123 // chrome/browser/ui library function. See crbug.com/402698.
124 static base::string16
WebOriginDisplayName(const GURL
& origin
,
125 const std::string
& languages
);
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_