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/message_loop.h"
9#include "third_party/WebKit/public/web/WebNotification.h"
10
11using blink::WebNotification;
12
13namespace content {
14
15ActiveNotificationTracker::ActiveNotificationTracker() {}
16
17ActiveNotificationTracker::~ActiveNotificationTracker() {}
18
19bool ActiveNotificationTracker::GetId(
20    const WebNotification& notification, int& id) {
21  ReverseTable::iterator iter = reverse_notification_table_.find(notification);
22  if (iter == reverse_notification_table_.end())
23    return false;
24  id = iter->second;
25  return true;
26}
27
28bool ActiveNotificationTracker::GetNotification(
29    int id, WebNotification* notification) {
30  WebNotification* lookup = notification_table_.Lookup(id);
31  if (!lookup)
32    return false;
33
34  *notification = *lookup;
35  return true;
36}
37
38int ActiveNotificationTracker::RegisterNotification(
39    const blink::WebNotification& proxy) {
40  if (reverse_notification_table_.find(proxy)
41      != reverse_notification_table_.end()) {
42    return reverse_notification_table_[proxy];
43  } else {
44    WebNotification* notification = new WebNotification(proxy);
45    int id = notification_table_.Add(notification);
46    reverse_notification_table_[proxy] = id;
47    return id;
48  }
49}
50
51void ActiveNotificationTracker::UnregisterNotification(int id) {
52  // We want to free the notification after removing it from the table.
53  scoped_ptr<WebNotification> notification(notification_table_.Lookup(id));
54  notification_table_.Remove(id);
55  DCHECK(notification.get());
56  if (notification)
57    reverse_notification_table_.erase(*notification);
58}
59
60void ActiveNotificationTracker::Clear() {
61  while (!reverse_notification_table_.empty()) {
62    ReverseTable::iterator iter = reverse_notification_table_.begin();
63    UnregisterNotification((*iter).second);
64  }
65}
66
67}  // namespace content
68