1 // Copyright 2015 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_PENDING_NOTIFICATIONS_TRACKER_H_
6 #define CONTENT_CHILD_NOTIFICATIONS_PENDING_NOTIFICATIONS_TRACKER_H_
10 #include "base/id_map.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificationManager.h"
20 class SingleThreadTaskRunner
;
24 struct WebNotificationData
;
25 class WebNotificationDelegate
;
26 class WebSerializedOrigin
;
31 class NotificationImageLoader
;
32 class NotificationManager
;
34 // Type definition for the callback signature which is to be invoked when the
35 // resources associated with a notification have been fetched.
36 using NotificationResourcesFetchedCallback
=
37 base::Callback
<void(const SkBitmap
&)>;
39 // Tracks all aspects of all pending Web Notifications. Most notably, it's in
40 // charge of ensuring that all resource fetches associated with the notification
41 // are completed as expected. The data associated with the notifications is
42 // stored in this class, to maintain thread integrity of their members.
44 // The pending notification tracker is owned by the NotificationManager, and
45 // lives on the thread that manager has been associated with.
46 class PendingNotificationsTracker
{
48 explicit PendingNotificationsTracker(
49 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
);
50 ~PendingNotificationsTracker();
52 // Adds a page notification to the tracker. Resource fetches for the
53 // notification will be started on asynchronously the main thread.
54 void FetchPageNotificationResources(
55 const blink::WebNotificationData
& notification_data
,
56 blink::WebNotificationDelegate
* delegate
,
57 const NotificationResourcesFetchedCallback
& callback
);
59 // Adds a persistent notification to the tracker. Resource fetches for the
60 // notification will be started asynchronously on the main thread.
61 void FetchPersistentNotificationResources(
62 const blink::WebNotificationData
& notification_data
,
63 const NotificationResourcesFetchedCallback
& callback
);
65 // Cancels all pending and in-fligth fetches for the page notification
66 // identified by |delegate|. Returns if the notification was cancelled.
67 bool CancelPageNotificationFetches(blink::WebNotificationDelegate
* delegate
);
70 // To be called on the worker thread when the pending page notification
71 // identified by |notification_id| has finished fetching the icon.
72 void DidFetchPageNotification(blink::WebNotificationDelegate
* delegate
,
74 const SkBitmap
& icon
);
76 // To be called on the worker thread when the pending persistent notification
77 // identified by |notification_id| has finished fetching the icon.
78 void DidFetchPersistentNotification(int notification_id
,
79 const SkBitmap
& icon
);
81 // Common code for starting to fetch resources associated with any kind of
82 // notification. Will return the id of the pending notification as allocated
83 // in the |pending_notifications_| map.
84 int FetchNotificationResources(
85 const blink::WebNotificationData
& notification_data
,
86 const NotificationResourcesFetchedCallback
& callback
,
87 const scoped_refptr
<NotificationImageLoader
>& image_loader
);
89 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
91 struct PendingNotification
;
93 // List of the notifications whose resources are still being fetched.
94 IDMap
<PendingNotification
, IDMapOwnPointer
> pending_notifications_
;
96 // In order to be able to cancel pending page notifications by delegate, store
97 // a mapping of the delegate to the pending notification id as well.
98 std::map
<blink::WebNotificationDelegate
*, int> delegate_to_pending_id_map_
;
100 base::WeakPtrFactory
<PendingNotificationsTracker
> weak_factory_
;
102 DISALLOW_COPY_AND_ASSIGN(PendingNotificationsTracker
);
105 } // namespace content
107 #endif // CONTENT_CHILD_NOTIFICATIONS_PENDING_NOTIFICATIONS_TRACKER_H_