1 // Copyright (c) 2011 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 #include "content/renderer/active_notification_tracker.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "third_party/WebKit/public/web/WebNotification.h"
10 #include "third_party/WebKit/public/web/WebNotificationPermissionCallback.h"
12 using blink::WebNotification
;
13 using blink::WebNotificationPermissionCallback
;
17 ActiveNotificationTracker::ActiveNotificationTracker() {}
19 ActiveNotificationTracker::~ActiveNotificationTracker() {}
21 bool ActiveNotificationTracker::GetId(
22 const WebNotification
& notification
, int& id
) {
23 ReverseTable::iterator iter
= reverse_notification_table_
.find(notification
);
24 if (iter
== reverse_notification_table_
.end())
30 bool ActiveNotificationTracker::GetNotification(
31 int id
, WebNotification
* notification
) {
32 WebNotification
* lookup
= notification_table_
.Lookup(id
);
36 *notification
= *lookup
;
40 int ActiveNotificationTracker::RegisterNotification(
41 const blink::WebNotification
& proxy
) {
42 if (reverse_notification_table_
.find(proxy
)
43 != reverse_notification_table_
.end()) {
44 return reverse_notification_table_
[proxy
];
46 WebNotification
* notification
= new WebNotification(proxy
);
47 int id
= notification_table_
.Add(notification
);
48 reverse_notification_table_
[proxy
] = id
;
53 void ActiveNotificationTracker::UnregisterNotification(int id
) {
54 // We want to free the notification after removing it from the table.
55 scoped_ptr
<WebNotification
> notification(notification_table_
.Lookup(id
));
56 notification_table_
.Remove(id
);
57 DCHECK(notification
.get());
59 reverse_notification_table_
.erase(*notification
);
62 void ActiveNotificationTracker::Clear() {
63 while (!reverse_notification_table_
.empty()) {
64 ReverseTable::iterator iter
= reverse_notification_table_
.begin();
65 UnregisterNotification((*iter
).second
);
69 WebNotificationPermissionCallback
* ActiveNotificationTracker::GetCallback(
71 return callback_table_
.Lookup(id
);
74 int ActiveNotificationTracker::RegisterPermissionRequest(
75 WebNotificationPermissionCallback
* callback
) {
76 return callback_table_
.Add(callback
);
79 void ActiveNotificationTracker::OnPermissionRequestComplete(int id
) {
80 callback_table_
.Remove(id
);
83 } // namespace content