OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 "chrome/browser/notifications/notification_ui_manager.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/scoped_ptr.h" |
| 9 #include "base/stl_util-inl.h" |
| 10 #include "chrome/browser/notifications/balloon_collection.h" |
| 11 #include "chrome/browser/notifications/notification.h" |
| 12 #include "chrome/browser/renderer_host/site_instance.h" |
| 13 |
| 14 // A class which represents a notification waiting to be shown. |
| 15 class QueuedNotification { |
| 16 public: |
| 17 QueuedNotification(const Notification& notification, Profile* profile) |
| 18 : notification_(notification), |
| 19 profile_(profile) { |
| 20 } |
| 21 |
| 22 const Notification& notification() const { return notification_; } |
| 23 Profile* profile() const { return profile_; } |
| 24 |
| 25 private: |
| 26 // The notification to be shown. |
| 27 Notification notification_; |
| 28 |
| 29 // Non owned pointer to the user's profile. |
| 30 Profile* profile_; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(QueuedNotification); |
| 33 }; |
| 34 |
| 35 NotificationUIManager::NotificationUIManager() |
| 36 : balloon_collection_(NULL) { |
| 37 } |
| 38 |
| 39 NotificationUIManager::~NotificationUIManager() { |
| 40 STLDeleteElements(&show_queue_); |
| 41 } |
| 42 |
| 43 // static |
| 44 NotificationUIManager* NotificationUIManager::Create() { |
| 45 BalloonCollectionImpl* balloons = new BalloonCollectionImpl(); |
| 46 NotificationUIManager* instance = new NotificationUIManager(); |
| 47 instance->Initialize(balloons); |
| 48 balloons->set_space_change_listener(instance); |
| 49 return instance; |
| 50 } |
| 51 |
| 52 void NotificationUIManager::Add(const Notification& notification, |
| 53 Profile* profile) { |
| 54 LOG(INFO) << "Added notification. URL: " |
| 55 << notification.content_url().spec().c_str(); |
| 56 show_queue_.push_back( |
| 57 new QueuedNotification(notification, profile)); |
| 58 CheckAndShowNotifications(); |
| 59 } |
| 60 |
| 61 void NotificationUIManager::CheckAndShowNotifications() { |
| 62 // TODO(johnnyg): http://crbug.com/25061 - Check for user idle/presentation. |
| 63 ShowNotifications(); |
| 64 } |
| 65 |
| 66 void NotificationUIManager::ShowNotifications() { |
| 67 while (!show_queue_.empty() && balloon_collection_->HasSpace()) { |
| 68 scoped_ptr<QueuedNotification> queued_notification(show_queue_.front()); |
| 69 show_queue_.pop_front(); |
| 70 balloon_collection_->Add(queued_notification->notification(), |
| 71 queued_notification->profile()); |
| 72 } |
| 73 } |
| 74 |
| 75 void NotificationUIManager::OnBalloonSpaceChanged() { |
| 76 CheckAndShowNotifications(); |
| 77 } |
OLD | NEW |