Chromium Code Reviews

Side by Side Diff: chrome/browser/notifications/notification_ui_manager.cc

Issue 5319011: Listen for APP_TERMINATING in notification ui; close windows earlier in the p... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
« no previous file with comments | « chrome/browser/notifications/notification_ui_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/notifications/notification_ui_manager.h" 5 #include "chrome/browser/notifications/notification_ui_manager.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/scoped_ptr.h" 8 #include "base/scoped_ptr.h"
9 #include "base/stl_util-inl.h" 9 #include "base/stl_util-inl.h"
10 #include "chrome/browser/notifications/balloon_collection.h" 10 #include "chrome/browser/notifications/balloon_collection.h"
11 #include "chrome/browser/notifications/notification.h" 11 #include "chrome/browser/notifications/notification.h"
12 #include "chrome/browser/renderer_host/site_instance.h" 12 #include "chrome/browser/renderer_host/site_instance.h"
13 #include "chrome/common/notification_service.h"
14 #include "chrome/common/notification_type.h"
13 15
14 // A class which represents a notification waiting to be shown. 16 // A class which represents a notification waiting to be shown.
15 class QueuedNotification { 17 class QueuedNotification {
16 public: 18 public:
17 QueuedNotification(const Notification& notification, Profile* profile) 19 QueuedNotification(const Notification& notification, Profile* profile)
18 : notification_(notification), 20 : notification_(notification),
19 profile_(profile) { 21 profile_(profile) {
20 } 22 }
21 23
22 const Notification& notification() const { return notification_; } 24 const Notification& notification() const { return notification_; }
23 Profile* profile() const { return profile_; } 25 Profile* profile() const { return profile_; }
24 26
25 void Replace(const Notification& new_notification) { 27 void Replace(const Notification& new_notification) {
26 notification_ = new_notification; 28 notification_ = new_notification;
27 } 29 }
28 30
29 private: 31 private:
30 // The notification to be shown. 32 // The notification to be shown.
31 Notification notification_; 33 Notification notification_;
32 34
33 // Non owned pointer to the user's profile. 35 // Non owned pointer to the user's profile.
34 Profile* profile_; 36 Profile* profile_;
35 37
36 DISALLOW_COPY_AND_ASSIGN(QueuedNotification); 38 DISALLOW_COPY_AND_ASSIGN(QueuedNotification);
37 }; 39 };
38 40
39 NotificationUIManager::NotificationUIManager() 41 NotificationUIManager::NotificationUIManager()
40 : balloon_collection_(NULL) { 42 : balloon_collection_(NULL) {
43 registrar_.Add(this, NotificationType::APP_TERMINATING,
44 NotificationService::AllSources());
41 } 45 }
42 46
43 NotificationUIManager::~NotificationUIManager() { 47 NotificationUIManager::~NotificationUIManager() {
44 STLDeleteElements(&show_queue_); 48 STLDeleteElements(&show_queue_);
45 } 49 }
46 50
47 // static 51 // static
48 NotificationUIManager* NotificationUIManager::Create() { 52 NotificationUIManager* NotificationUIManager::Create() {
49 BalloonCollection* balloons = BalloonCollection::Create(); 53 BalloonCollection* balloons = BalloonCollection::Create();
50 NotificationUIManager* instance = new NotificationUIManager(); 54 NotificationUIManager* instance = new NotificationUIManager();
(...skipping 28 matching lines...)
79 return balloon_collection_->RemoveById(id); 83 return balloon_collection_->RemoveById(id);
80 } 84 }
81 85
82 bool NotificationUIManager::CancelAllBySourceOrigin(const GURL& source) { 86 bool NotificationUIManager::CancelAllBySourceOrigin(const GURL& source) {
83 // Same pattern as CancelById, but more complicated than the above 87 // Same pattern as CancelById, but more complicated than the above
84 // because there may be multiple notifications from the same source. 88 // because there may be multiple notifications from the same source.
85 bool removed = false; 89 bool removed = false;
86 NotificationDeque::iterator iter; 90 NotificationDeque::iterator iter;
87 for (iter = show_queue_.begin(); iter != show_queue_.end(); ++iter) { 91 for (iter = show_queue_.begin(); iter != show_queue_.end(); ++iter) {
88 if ((*iter)->notification().origin_url() == source) { 92 if ((*iter)->notification().origin_url() == source) {
89 iter = show_queue_.erase(iter); 93 iter = show_queue_.erase(iter);
Andrew T Wilson (Slow) 2010/12/01 01:55:56 You are tracking this separately, but just wanted
90 removed = true; 94 removed = true;
91 } else { 95 } else {
92 ++iter; 96 ++iter;
93 } 97 }
94 } 98 }
95 99
96 return balloon_collection_->RemoveBySourceOrigin(source) || removed; 100 return balloon_collection_->RemoveBySourceOrigin(source) || removed;
97 } 101 }
98 102
103 void NotificationUIManager::CancelAll() {
104 STLDeleteElements(&show_queue_);
105 balloon_collection_->RemoveAll();
106 }
107
99 void NotificationUIManager::CheckAndShowNotifications() { 108 void NotificationUIManager::CheckAndShowNotifications() {
100 // TODO(johnnyg): http://crbug.com/25061 - Check for user idle/presentation. 109 // TODO(johnnyg): http://crbug.com/25061 - Check for user idle/presentation.
101 ShowNotifications(); 110 ShowNotifications();
102 } 111 }
103 112
104 void NotificationUIManager::ShowNotifications() { 113 void NotificationUIManager::ShowNotifications() {
105 while (!show_queue_.empty() && balloon_collection_->HasSpace()) { 114 while (!show_queue_.empty() && balloon_collection_->HasSpace()) {
106 scoped_ptr<QueuedNotification> queued_notification(show_queue_.front()); 115 scoped_ptr<QueuedNotification> queued_notification(show_queue_.front());
107 show_queue_.pop_front(); 116 show_queue_.pop_front();
108 balloon_collection_->Add(queued_notification->notification(), 117 balloon_collection_->Add(queued_notification->notification(),
(...skipping 31 matching lines...)
140 ++balloon_iter) { 149 ++balloon_iter) {
141 if (origin == (*balloon_iter)->notification().origin_url() && 150 if (origin == (*balloon_iter)->notification().origin_url() &&
142 replace_id == (*balloon_iter)->notification().replace_id()) { 151 replace_id == (*balloon_iter)->notification().replace_id()) {
143 (*balloon_iter)->Update(notification); 152 (*balloon_iter)->Update(notification);
144 return true; 153 return true;
145 } 154 }
146 } 155 }
147 156
148 return false; 157 return false;
149 } 158 }
159
160 void NotificationUIManager::Observe(NotificationType type,
161 const NotificationSource& source,
162 const NotificationDetails& details) {
163 if (type == NotificationType::APP_TERMINATING)
164 CancelAll();
Andrew T Wilson (Slow) 2010/12/01 01:55:56 Could also be CHECK(type == NotificationType::APP_
165 else
166 NOTREACHED();
167 }
OLDNEW
« no previous file with comments | « chrome/browser/notifications/notification_ui_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine