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" | |
11 #include "chrome/browser/extensions/api/idle/idle_api_constants.h" | 10 #include "chrome/browser/extensions/api/idle/idle_api_constants.h" |
12 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/common/extensions/api/idle.h" | 12 #include "chrome/common/extensions/api/idle.h" |
14 #include "chrome/common/extensions/extension_constants.h" | 13 #include "chrome/common/extensions/extension_constants.h" |
15 #include "content/public/browser/notification_details.h" | |
16 #include "content/public/browser/notification_source.h" | |
17 #include "extensions/browser/event_router.h" | 14 #include "extensions/browser/event_router.h" |
| 15 #include "extensions/browser/extension_registry.h" |
18 #include "extensions/common/extension.h" | 16 #include "extensions/common/extension.h" |
19 | 17 |
20 namespace keys = extensions::idle_api_constants; | 18 namespace keys = extensions::idle_api_constants; |
21 namespace idle = extensions::api::idle; | 19 namespace idle = extensions::api::idle; |
22 | 20 |
23 namespace extensions { | 21 namespace extensions { |
24 | 22 |
25 namespace { | 23 namespace { |
26 | 24 |
27 const int kDefaultIdleThreshold = 60; | 25 const int kDefaultIdleThreshold = 60; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 : last_state(initial_state), | 116 : last_state(initial_state), |
119 listeners(0), | 117 listeners(0), |
120 threshold(kDefaultIdleThreshold) { | 118 threshold(kDefaultIdleThreshold) { |
121 } | 119 } |
122 | 120 |
123 IdleManager::IdleManager(Profile* profile) | 121 IdleManager::IdleManager(Profile* profile) |
124 : profile_(profile), | 122 : profile_(profile), |
125 last_state_(IDLE_STATE_ACTIVE), | 123 last_state_(IDLE_STATE_ACTIVE), |
126 weak_factory_(this), | 124 weak_factory_(this), |
127 idle_time_provider_(new DefaultIdleProvider()), | 125 idle_time_provider_(new DefaultIdleProvider()), |
128 event_delegate_(new DefaultEventDelegate(profile)) { | 126 event_delegate_(new DefaultEventDelegate(profile)), |
| 127 extension_registry_observer_(this) { |
129 } | 128 } |
130 | 129 |
131 IdleManager::~IdleManager() { | 130 IdleManager::~IdleManager() { |
132 } | 131 } |
133 | 132 |
134 void IdleManager::Init() { | 133 void IdleManager::Init() { |
135 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 134 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
136 content::Source<Profile>(profile_->GetOriginalProfile())); | |
137 event_delegate_->RegisterObserver(this); | 135 event_delegate_->RegisterObserver(this); |
138 } | 136 } |
139 | 137 |
140 void IdleManager::Shutdown() { | 138 void IdleManager::Shutdown() { |
141 DCHECK(thread_checker_.CalledOnValidThread()); | 139 DCHECK(thread_checker_.CalledOnValidThread()); |
142 event_delegate_->UnregisterObserver(this); | 140 event_delegate_->UnregisterObserver(this); |
143 } | 141 } |
144 | 142 |
145 void IdleManager::Observe(int type, | 143 void IdleManager::OnExtensionUnloaded(content::BrowserContext* browser_context, |
146 const content::NotificationSource& source, | 144 const Extension* extension, |
147 const content::NotificationDetails& details) { | 145 UnloadedExtensionInfo::Reason reason) { |
148 DCHECK(thread_checker_.CalledOnValidThread()); | 146 DCHECK(thread_checker_.CalledOnValidThread()); |
149 | 147 monitors_.erase(extension->id()); |
150 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) { | |
151 const Extension* extension = | |
152 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | |
153 monitors_.erase(extension->id()); | |
154 } else { | |
155 NOTREACHED(); | |
156 } | |
157 } | 148 } |
158 | 149 |
159 void IdleManager::OnListenerAdded(const EventListenerInfo& details) { | 150 void IdleManager::OnListenerAdded(const EventListenerInfo& details) { |
160 DCHECK(thread_checker_.CalledOnValidThread()); | 151 DCHECK(thread_checker_.CalledOnValidThread()); |
161 | 152 |
162 ++GetMonitor(details.extension_id)->listeners; | 153 ++GetMonitor(details.extension_id)->listeners; |
163 StartPolling(); | 154 StartPolling(); |
164 } | 155 } |
165 | 156 |
166 void IdleManager::OnListenerRemoved(const EventListenerInfo& details) { | 157 void IdleManager::OnListenerRemoved(const EventListenerInfo& details) { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 event_delegate_->OnStateChanged(it->first, new_state); | 261 event_delegate_->OnStateChanged(it->first, new_state); |
271 monitor.last_state = new_state; | 262 monitor.last_state = new_state; |
272 listener_count += monitor.listeners; | 263 listener_count += monitor.listeners; |
273 } | 264 } |
274 | 265 |
275 if (listener_count == 0) | 266 if (listener_count == 0) |
276 StopPolling(); | 267 StopPolling(); |
277 } | 268 } |
278 | 269 |
279 } // namespace extensions | 270 } // namespace extensions |
OLD | NEW |