15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/renderer/active_notification_tracker.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebNotification.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using blink::WebNotification;
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ActiveNotificationTracker::ActiveNotificationTracker() {}
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ActiveNotificationTracker::~ActiveNotificationTracker() {}
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ActiveNotificationTracker::GetId(
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const WebNotification& notification, int& id) {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ReverseTable::iterator iter = reverse_notification_table_.find(notification);
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (iter == reverse_notification_table_.end())
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  id = iter->second;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ActiveNotificationTracker::GetNotification(
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int id, WebNotification* notification) {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebNotification* lookup = notification_table_.Lookup(id);
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!lookup)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *notification = *lookup;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int ActiveNotificationTracker::RegisterNotification(
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const blink::WebNotification& proxy) {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (reverse_notification_table_.find(proxy)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      != reverse_notification_table_.end()) {
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return reverse_notification_table_[proxy];
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebNotification* notification = new WebNotification(proxy);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int id = notification_table_.Add(notification);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    reverse_notification_table_[proxy] = id;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return id;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ActiveNotificationTracker::UnregisterNotification(int id) {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We want to free the notification after removing it from the table.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<WebNotification> notification(notification_table_.Lookup(id));
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  notification_table_.Remove(id);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(notification.get());
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (notification)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    reverse_notification_table_.erase(*notification);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ActiveNotificationTracker::Clear() {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (!reverse_notification_table_.empty()) {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ReverseTable::iterator iter = reverse_notification_table_.begin();
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UnregisterNotification((*iter).second);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
68