summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications/notification_ui_manager.cc
blob: 0330570d2e336f02a0c506e33e41160c837d04ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/notifications/notification_ui_manager.h"

#include "base/logging.h"
#include "base/scoped_ptr.h"
#include "base/stl_util-inl.h"
#include "chrome/browser/notifications/balloon_collection.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/renderer_host/site_instance.h"

// A class which represents a notification waiting to be shown.
class QueuedNotification {
 public:
  QueuedNotification(const Notification& notification, Profile* profile)
      : notification_(notification),
        profile_(profile) {
  }

  const Notification& notification() const { return notification_; }
  Profile* profile() const { return profile_; }

 private:
  // The notification to be shown.
  Notification notification_;

  // Non owned pointer to the user's profile.
  Profile* profile_;

  DISALLOW_COPY_AND_ASSIGN(QueuedNotification);
};

NotificationUIManager::NotificationUIManager()
    : balloon_collection_(NULL) {
}

NotificationUIManager::~NotificationUIManager() {
  STLDeleteElements(&show_queue_);
}

// static
NotificationUIManager* NotificationUIManager::Create() {
  BalloonCollectionImpl* balloons = new BalloonCollectionImpl();
  NotificationUIManager* instance = new NotificationUIManager();
  instance->Initialize(balloons);
  balloons->set_space_change_listener(instance);
  return instance;
}

void NotificationUIManager::Add(const Notification& notification,
                                Profile* profile) {
  LOG(INFO) << "Added notification. URL: "
            << notification.content_url().spec().c_str();
  show_queue_.push_back(
      new QueuedNotification(notification, profile));
  CheckAndShowNotifications();
}

bool NotificationUIManager::Cancel(const Notification& notification) {
  // First look through the notifications that haven't been shown.  If not
  // found there, call to the active balloon collection to tear it down.
  NotificationDeque::iterator iter;
  for (iter = show_queue_.begin(); iter != show_queue_.end(); ++iter) {
    if (notification.IsSame((*iter)->notification())) {
      show_queue_.erase(iter);
      return true;
    }
  }
  return balloon_collection_->Remove(notification);
}

void NotificationUIManager::CheckAndShowNotifications() {
  // TODO(johnnyg): http://crbug.com/25061 - Check for user idle/presentation.
  ShowNotifications();
}

void NotificationUIManager::ShowNotifications() {
  while (!show_queue_.empty() && balloon_collection_->HasSpace()) {
    scoped_ptr<QueuedNotification> queued_notification(show_queue_.front());
    show_queue_.pop_front();
    balloon_collection_->Add(queued_notification->notification(),
                             queued_notification->profile());
  }
}

void NotificationUIManager::OnBalloonSpaceChanged() {
  CheckAndShowNotifications();
}