OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/idle/idle_manager.h" | 5 #include "chrome/browser/extensions/api/idle/idle_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 } | 164 } |
165 | 165 |
166 void IdleManager::OnListenerRemoved(const EventListenerInfo& details) { | 166 void IdleManager::OnListenerRemoved(const EventListenerInfo& details) { |
167 DCHECK(thread_checker_.CalledOnValidThread()); | 167 DCHECK(thread_checker_.CalledOnValidThread()); |
168 | 168 |
169 // During unload the monitor could already have been deleted. No need to do | 169 // During unload the monitor could already have been deleted. No need to do |
170 // anything in that case. | 170 // anything in that case. |
171 MonitorMap::iterator it = monitors_.find(details.extension_id); | 171 MonitorMap::iterator it = monitors_.find(details.extension_id); |
172 if (it != monitors_.end()) { | 172 if (it != monitors_.end()) { |
173 DCHECK_GT(it->second.listeners, 0); | 173 DCHECK_GT(it->second.listeners, 0); |
| 174 // Note: Deliberately leave the listener count as 0 rather than erase()ing |
| 175 // this record so that the threshold doesn't get reset when all listeners |
| 176 // are removed. |
174 --it->second.listeners; | 177 --it->second.listeners; |
175 } | 178 } |
176 } | 179 } |
177 | 180 |
178 void IdleManager::QueryState(int threshold, QueryStateCallback notify) { | 181 void IdleManager::QueryState(int threshold, QueryStateCallback notify) { |
179 DCHECK(thread_checker_.CalledOnValidThread()); | 182 DCHECK(thread_checker_.CalledOnValidThread()); |
180 idle_time_provider_->CalculateIdleState(threshold, notify); | 183 idle_time_provider_->CalculateIdleState(threshold, notify); |
181 } | 184 } |
182 | 185 |
183 void IdleManager::SetThreshold(const std::string& extension_id, | 186 void IdleManager::SetThreshold(const std::string& extension_id, |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 bool locked = idle_time_provider_->CheckIdleStateIsLocked(); | 255 bool locked = idle_time_provider_->CheckIdleStateIsLocked(); |
253 int listener_count = 0; | 256 int listener_count = 0; |
254 | 257 |
255 // Remember this state for initializing new event listeners. | 258 // Remember this state for initializing new event listeners. |
256 last_state_ = IdleTimeToIdleState(locked, | 259 last_state_ = IdleTimeToIdleState(locked, |
257 idle_time, | 260 idle_time, |
258 kDefaultIdleThreshold); | 261 kDefaultIdleThreshold); |
259 | 262 |
260 for (MonitorMap::iterator it = monitors_.begin(); | 263 for (MonitorMap::iterator it = monitors_.begin(); |
261 it != monitors_.end(); ++it) { | 264 it != monitors_.end(); ++it) { |
262 if (it->second.listeners < 1) | 265 IdleMonitor& monitor = it->second; |
263 continue; | 266 IdleState new_state = |
264 | 267 IdleTimeToIdleState(locked, idle_time, monitor.threshold); |
265 ++listener_count; | 268 // TODO(kalman): Use EventRouter::HasListeners for these sorts of checks. |
266 | 269 if (monitor.listeners > 0 && monitor.last_state != new_state) |
267 IdleState new_state = IdleTimeToIdleState(locked, | |
268 idle_time, | |
269 it->second.threshold); | |
270 | |
271 if (new_state != it->second.last_state) { | |
272 it->second.last_state = new_state; | |
273 event_delegate_->OnStateChanged(it->first, new_state); | 270 event_delegate_->OnStateChanged(it->first, new_state); |
274 } | 271 monitor.last_state = new_state; |
| 272 listener_count += monitor.listeners; |
275 } | 273 } |
276 | 274 |
277 if (listener_count == 0) { | 275 if (listener_count == 0) |
278 StopPolling(); | 276 StopPolling(); |
279 } | |
280 } | 277 } |
281 | 278 |
282 } // namespace extensions | 279 } // namespace extensions |
OLD | NEW |