OLD | NEW |
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/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/fullscreen.h" |
| 12 #include "chrome/browser/idle.h" |
11 #include "chrome/browser/notifications/balloon_collection.h" | 13 #include "chrome/browser/notifications/balloon_collection.h" |
12 #include "chrome/browser/notifications/notification.h" | 14 #include "chrome/browser/notifications/notification.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/renderer_host/site_instance.h" | 16 #include "chrome/browser/renderer_host/site_instance.h" |
15 #include "chrome/common/notification_service.h" | 17 #include "chrome/common/notification_service.h" |
16 #include "chrome/common/notification_type.h" | 18 #include "chrome/common/notification_type.h" |
17 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
18 | 20 |
| 21 namespace { |
| 22 const int kUserStatePollingIntervalSeconds = 1; |
| 23 } |
| 24 |
19 // A class which represents a notification waiting to be shown. | 25 // A class which represents a notification waiting to be shown. |
20 class QueuedNotification { | 26 class QueuedNotification { |
21 public: | 27 public: |
22 QueuedNotification(const Notification& notification, Profile* profile) | 28 QueuedNotification(const Notification& notification, Profile* profile) |
23 : notification_(notification), | 29 : notification_(notification), |
24 profile_(profile) { | 30 profile_(profile) { |
25 } | 31 } |
26 | 32 |
27 const Notification& notification() const { return notification_; } | 33 const Notification& notification() const { return notification_; } |
28 Profile* profile() const { return profile_; } | 34 Profile* profile() const { return profile_; } |
29 | 35 |
30 void Replace(const Notification& new_notification) { | 36 void Replace(const Notification& new_notification) { |
31 notification_ = new_notification; | 37 notification_ = new_notification; |
32 } | 38 } |
33 | 39 |
34 private: | 40 private: |
35 // The notification to be shown. | 41 // The notification to be shown. |
36 Notification notification_; | 42 Notification notification_; |
37 | 43 |
38 // Non owned pointer to the user's profile. | 44 // Non owned pointer to the user's profile. |
39 Profile* profile_; | 45 Profile* profile_; |
40 | 46 |
41 DISALLOW_COPY_AND_ASSIGN(QueuedNotification); | 47 DISALLOW_COPY_AND_ASSIGN(QueuedNotification); |
42 }; | 48 }; |
43 | 49 |
44 NotificationUIManager::NotificationUIManager(PrefService* local_state) | 50 NotificationUIManager::NotificationUIManager(PrefService* local_state) |
45 : balloon_collection_(NULL) { | 51 : balloon_collection_(NULL), |
| 52 is_user_active_(true) { |
46 registrar_.Add(this, NotificationType::APP_TERMINATING, | 53 registrar_.Add(this, NotificationType::APP_TERMINATING, |
47 NotificationService::AllSources()); | 54 NotificationService::AllSources()); |
48 position_pref_.Init(prefs::kDesktopNotificationPosition, local_state, this); | 55 position_pref_.Init(prefs::kDesktopNotificationPosition, local_state, this); |
| 56 #if defined(OS_MACOSX) |
| 57 InitFullScreenMonitor(); |
| 58 #endif |
49 } | 59 } |
50 | 60 |
51 NotificationUIManager::~NotificationUIManager() { | 61 NotificationUIManager::~NotificationUIManager() { |
52 STLDeleteElements(&show_queue_); | 62 STLDeleteElements(&show_queue_); |
| 63 #if defined(OS_MACOSX) |
| 64 StopFullScreenMonitor(); |
| 65 #endif |
53 } | 66 } |
54 | 67 |
55 // static | 68 // static |
56 NotificationUIManager* NotificationUIManager::Create(PrefService* local_state) { | 69 NotificationUIManager* NotificationUIManager::Create(PrefService* local_state) { |
57 BalloonCollection* balloons = BalloonCollection::Create(); | 70 BalloonCollection* balloons = BalloonCollection::Create(); |
58 NotificationUIManager* instance = new NotificationUIManager(local_state); | 71 NotificationUIManager* instance = new NotificationUIManager(local_state); |
59 instance->Initialize(balloons); | 72 instance->Initialize(balloons); |
60 balloons->set_space_change_listener(instance); | 73 balloons->set_space_change_listener(instance); |
61 return instance; | 74 return instance; |
62 } | 75 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 132 |
120 return balloon_collection_->RemoveBySourceOrigin(source) || removed; | 133 return balloon_collection_->RemoveBySourceOrigin(source) || removed; |
121 } | 134 } |
122 | 135 |
123 void NotificationUIManager::CancelAll() { | 136 void NotificationUIManager::CancelAll() { |
124 STLDeleteElements(&show_queue_); | 137 STLDeleteElements(&show_queue_); |
125 balloon_collection_->RemoveAll(); | 138 balloon_collection_->RemoveAll(); |
126 } | 139 } |
127 | 140 |
128 void NotificationUIManager::CheckAndShowNotifications() { | 141 void NotificationUIManager::CheckAndShowNotifications() { |
129 // TODO(johnnyg): http://crbug.com/25061 - Check for user idle/presentation. | 142 CheckUserState(); |
130 ShowNotifications(); | 143 if (is_user_active_) |
| 144 ShowNotifications(); |
| 145 } |
| 146 |
| 147 void NotificationUIManager::CheckUserState() { |
| 148 bool is_user_active_previously = is_user_active_; |
| 149 is_user_active_ = CalculateIdleState(0) != IDLE_STATE_LOCKED && |
| 150 !IsFullScreenMode(); |
| 151 if (is_user_active_ == is_user_active_previously) |
| 152 return; |
| 153 |
| 154 if (is_user_active_) { |
| 155 user_state_check_timer_.Stop(); |
| 156 // We need to show any postponed nofications when the user becomes active |
| 157 // again. |
| 158 ShowNotifications(); |
| 159 } else if (!user_state_check_timer_.IsRunning()) { |
| 160 // Start a timer to detect the moment at which the user becomes active. |
| 161 user_state_check_timer_.Start( |
| 162 base::TimeDelta::FromSeconds(kUserStatePollingIntervalSeconds), this, |
| 163 &NotificationUIManager::CheckUserState); |
| 164 } |
131 } | 165 } |
132 | 166 |
133 void NotificationUIManager::ShowNotifications() { | 167 void NotificationUIManager::ShowNotifications() { |
134 while (!show_queue_.empty() && balloon_collection_->HasSpace()) { | 168 while (!show_queue_.empty() && balloon_collection_->HasSpace()) { |
135 scoped_ptr<QueuedNotification> queued_notification(show_queue_.front()); | 169 scoped_ptr<QueuedNotification> queued_notification(show_queue_.front()); |
136 show_queue_.pop_front(); | 170 show_queue_.pop_front(); |
137 balloon_collection_->Add(queued_notification->notification(), | 171 balloon_collection_->Add(queued_notification->notification(), |
138 queued_notification->profile()); | 172 queued_notification->profile()); |
139 } | 173 } |
140 } | 174 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 } else if (type == NotificationType::PREF_CHANGED) { | 234 } else if (type == NotificationType::PREF_CHANGED) { |
201 std::string* name = Details<std::string>(details).ptr(); | 235 std::string* name = Details<std::string>(details).ptr(); |
202 if (*name == prefs::kDesktopNotificationPosition) | 236 if (*name == prefs::kDesktopNotificationPosition) |
203 balloon_collection_->SetPositionPreference( | 237 balloon_collection_->SetPositionPreference( |
204 static_cast<BalloonCollection::PositionPreference>( | 238 static_cast<BalloonCollection::PositionPreference>( |
205 position_pref_.GetValue())); | 239 position_pref_.GetValue())); |
206 } else { | 240 } else { |
207 NOTREACHED(); | 241 NOTREACHED(); |
208 } | 242 } |
209 } | 243 } |
OLD | NEW |