active_notification_tracker.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
4
5#include "content/renderer/active_notification_tracker.h"
6
7#include "base/memory/scoped_ptr.h"
8#include "base/message_loop.h"
9#include "third_party/WebKit/Source/WebKit/chromium/public/WebNotification.h"
10#include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPermissionCallback.h"
11
12using WebKit::WebNotification;
13using WebKit::WebNotificationPermissionCallback;
14
15namespace content {
16
17ActiveNotificationTracker::ActiveNotificationTracker() {}
18
19ActiveNotificationTracker::~ActiveNotificationTracker() {}
20
21bool ActiveNotificationTracker::GetId(
22    const WebNotification& notification, int& id) {
23  ReverseTable::iterator iter = reverse_notification_table_.find(notification);
24  if (iter == reverse_notification_table_.end())
25    return false;
26  id = iter->second;
27  return true;
28}
29
30bool ActiveNotificationTracker::GetNotification(
31    int id, WebNotification* notification) {
32  WebNotification* lookup = notification_table_.Lookup(id);
33  if (!lookup)
34    return false;
35
36  *notification = *lookup;
37  return true;
38}
39
40int ActiveNotificationTracker::RegisterNotification(
41    const WebKit::WebNotification& proxy) {
42  if (reverse_notification_table_.find(proxy)
43      != reverse_notification_table_.end()) {
44    return reverse_notification_table_[proxy];
45  } else {
46    WebNotification* notification = new WebNotification(proxy);
47    int id = notification_table_.Add(notification);
48    reverse_notification_table_[proxy] = id;
49    return id;
50  }
51}
52
53void 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());
58  if (notification.get())
59    reverse_notification_table_.erase(*notification);
60}
61
62void ActiveNotificationTracker::Clear() {
63  while (!reverse_notification_table_.empty()) {
64    ReverseTable::iterator iter = reverse_notification_table_.begin();
65    UnregisterNotification((*iter).second);
66  }
67}
68
69void ActiveNotificationTracker::DetachAll() {
70  ReverseTable::iterator iter;
71  for (iter = reverse_notification_table_.begin();
72       iter != reverse_notification_table_.end();
73       ++iter) {
74    WebNotification notification(iter->first);
75    notification.detachPresenter();
76  }
77}
78
79WebNotificationPermissionCallback* ActiveNotificationTracker::GetCallback(
80    int id) {
81  return callback_table_.Lookup(id);
82}
83
84int ActiveNotificationTracker::RegisterPermissionRequest(
85    WebNotificationPermissionCallback* callback) {
86  return callback_table_.Add(callback);
87}
88
89void ActiveNotificationTracker::OnPermissionRequestComplete(int id) {
90  callback_table_.Remove(id);
91}
92
93}  // namespace content
94