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 CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_MANAGER_H_
6 #define CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_MANAGER_H_
12 #include "base/id_map.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/single_thread_task_runner.h"
15 #include "content/child/notifications/notification_dispatcher.h"
16 #include "content/child/notifications/pending_notifications_tracker.h"
17 #include "content/child/worker_task_runner.h"
18 #include "content/common/platform_notification_messages.h"
19 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificationManager.h"
25 struct PlatformNotificationData
;
26 class ThreadSafeSender
;
28 class NotificationManager
: public blink::WebNotificationManager
,
29 public WorkerTaskRunner::Observer
{
31 ~NotificationManager() override
;
33 // |thread_safe_sender| and |notification_dispatcher| are used if
34 // calling this leads to construction.
35 static NotificationManager
* ThreadSpecificInstance(
36 ThreadSafeSender
* thread_safe_sender
,
37 base::SingleThreadTaskRunner
* main_thread_task_runner
,
38 NotificationDispatcher
* notification_dispatcher
);
40 // WorkerTaskRunner::Observer implementation.
41 void OnWorkerRunLoopStopped() override
;
43 // blink::WebNotificationManager implementation.
44 virtual void show(const blink::WebSerializedOrigin
& origin
,
45 const blink::WebNotificationData
& notification_data
,
46 blink::WebNotificationDelegate
* delegate
);
47 virtual void showPersistent(
48 const blink::WebSerializedOrigin
& origin
,
49 const blink::WebNotificationData
& notification_data
,
50 blink::WebServiceWorkerRegistration
* service_worker_registration
,
51 blink::WebNotificationShowCallbacks
* callbacks
);
52 virtual void getNotifications(
53 const blink::WebString
& filter_tag
,
54 blink::WebServiceWorkerRegistration
* service_worker_registration
,
55 blink::WebNotificationGetCallbacks
* callbacks
);
56 virtual void close(blink::WebNotificationDelegate
* delegate
);
57 virtual void closePersistent(
58 const blink::WebSerializedOrigin
& origin
,
59 int64_t persistent_notification_id
);
60 virtual void notifyDelegateDestroyed(
61 blink::WebNotificationDelegate
* delegate
);
62 virtual blink::WebNotificationPermission
checkPermission(
63 const blink::WebSerializedOrigin
& origin
);
65 // Called by the NotificationDispatcher.
66 bool OnMessageReceived(const IPC::Message
& message
);
70 ThreadSafeSender
* thread_safe_sender
,
71 base::SingleThreadTaskRunner
* main_thread_task_runner
,
72 NotificationDispatcher
* notification_dispatcher
);
74 // IPC message handlers.
75 void OnDidShow(int notification_id
);
76 void OnDidShowPersistent(int request_id
, bool success
);
77 void OnDidClose(int notification_id
);
78 void OnDidClick(int notification_id
);
79 void OnDidGetNotifications(
81 const std::vector
<PersistentNotificationInfo
>& notification_infos
);
83 // To be called when a page notification is ready to be displayed. Will
84 // inform the browser process about all available data. The |delegate|,
85 // owned by Blink, will be used to feed back events associated with the
86 // notification to the JavaScript object.
87 void DisplayPageNotification(
88 const blink::WebSerializedOrigin
& origin
,
89 const blink::WebNotificationData
& notification_data
,
90 blink::WebNotificationDelegate
* delegate
,
91 const SkBitmap
& icon
);
93 // To be called when a persistent notification is ready to be displayed. Will
94 // inform the browser process about all available data. The |callbacks| will
95 // be used to inform the Promise pending in Blink that the notification has
96 // been send to the browser process to be displayed.
97 void DisplayPersistentNotification(
98 const blink::WebSerializedOrigin
& origin
,
99 const blink::WebNotificationData
& notification_data
,
100 int64 service_worker_registration_id
,
101 scoped_ptr
<blink::WebNotificationShowCallbacks
> callbacks
,
102 const SkBitmap
& icon
);
104 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
105 scoped_refptr
<NotificationDispatcher
> notification_dispatcher_
;
107 // Tracker which stores all pending Notifications, both page and persistent
108 // ones, until all their associated resources have been fetched.
109 PendingNotificationsTracker pending_notifications_
;
111 // Tracks pending requests for getting a list of notifications.
112 IDMap
<blink::WebNotificationGetCallbacks
, IDMapOwnPointer
>
113 pending_get_notification_requests_
;
115 // Tracks pending requests for displaying persistent notifications.
116 IDMap
<blink::WebNotificationShowCallbacks
, IDMapOwnPointer
>
117 pending_show_notification_requests_
;
119 // Map to store the delegate associated with a notification request Id.
120 std::map
<int, blink::WebNotificationDelegate
*> active_page_notifications_
;
122 DISALLOW_COPY_AND_ASSIGN(NotificationManager
);
125 } // namespace content
127 #endif // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_MANAGER_H_