Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / child / notifications / pending_notifications_tracker.h
blob53aa32104be2b92c751a5bffc90d012e36ede7ee
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_
8 #include <map>
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"
17 class SkBitmap;
19 namespace base {
20 class SingleThreadTaskRunner;
23 namespace blink {
24 struct WebNotificationData;
25 class WebNotificationDelegate;
28 namespace content {
30 class NotificationImageLoader;
31 class NotificationManager;
33 // Type definition for the callback signature which is to be invoked when the
34 // resources associated with a notification have been fetched.
35 using NotificationResourcesFetchedCallback =
36 base::Callback<void(const SkBitmap&)>;
38 // Tracks all aspects of all pending Web Notifications. Most notably, it's in
39 // charge of ensuring that all resource fetches associated with the notification
40 // are completed as expected. The data associated with the notifications is
41 // stored in this class, to maintain thread integrity of their members.
43 // The pending notification tracker is owned by the NotificationManager, and
44 // lives on the thread that manager has been associated with.
45 class PendingNotificationsTracker {
46 public:
47 explicit PendingNotificationsTracker(
48 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner);
49 ~PendingNotificationsTracker();
51 // Adds a page notification to the tracker. Resource fetches for the
52 // notification will be started on asynchronously the main thread.
53 void FetchPageNotificationResources(
54 const blink::WebNotificationData& notification_data,
55 blink::WebNotificationDelegate* delegate,
56 const NotificationResourcesFetchedCallback& callback);
58 // Adds a persistent notification to the tracker. Resource fetches for the
59 // notification will be started asynchronously on the main thread.
60 void FetchPersistentNotificationResources(
61 const blink::WebNotificationData& notification_data,
62 const NotificationResourcesFetchedCallback& callback);
64 // Cancels all pending and in-fligth fetches for the page notification
65 // identified by |delegate|. Returns if the notification was cancelled.
66 bool CancelPageNotificationFetches(blink::WebNotificationDelegate* delegate);
68 private:
69 // To be called on the worker thread when the pending page notification
70 // identified by |notification_id| has finished fetching the icon.
71 void DidFetchPageNotification(blink::WebNotificationDelegate* delegate,
72 int notification_id,
73 const SkBitmap& icon);
75 // To be called on the worker thread when the pending persistent notification
76 // identified by |notification_id| has finished fetching the icon.
77 void DidFetchPersistentNotification(int notification_id,
78 const SkBitmap& icon);
80 // Common code for starting to fetch resources associated with any kind of
81 // notification. Will return the id of the pending notification as allocated
82 // in the |pending_notifications_| map.
83 int FetchNotificationResources(
84 const blink::WebNotificationData& notification_data,
85 const NotificationResourcesFetchedCallback& callback,
86 const scoped_refptr<NotificationImageLoader>& image_loader);
88 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
90 struct PendingNotification;
92 // List of the notifications whose resources are still being fetched.
93 IDMap<PendingNotification, IDMapOwnPointer> pending_notifications_;
95 // In order to be able to cancel pending page notifications by delegate, store
96 // a mapping of the delegate to the pending notification id as well.
97 std::map<blink::WebNotificationDelegate*, int> delegate_to_pending_id_map_;
99 base::WeakPtrFactory<PendingNotificationsTracker> weak_factory_;
101 DISALLOW_COPY_AND_ASSIGN(PendingNotificationsTracker);
104 } // namespace content
106 #endif // CONTENT_CHILD_NOTIFICATIONS_PENDING_NOTIFICATIONS_TRACKER_H_