| 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/alarms/alarm_manager.h" | 5 #include "chrome/browser/extensions/api/alarms/alarm_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/lazy_instance.h" |
| 9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 10 #include "base/time.h" | 11 #include "base/time.h" |
| 11 #include "base/time/clock.h" | 12 #include "base/time/clock.h" |
| 13 #include "base/time/default_clock.h" |
| 12 #include "base/value_conversions.h" | 14 #include "base/value_conversions.h" |
| 13 #include "base/values.h" | 15 #include "base/values.h" |
| 14 #include "chrome/browser/extensions/event_router.h" | 16 #include "chrome/browser/extensions/event_router.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/extension_system.h" | 18 #include "chrome/browser/extensions/extension_system.h" |
| 17 #include "chrome/browser/extensions/state_store.h" | 19 #include "chrome/browser/extensions/state_store.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
| 20 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 21 | 23 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 list->Append(alarm.release()); | 84 list->Append(alarm.release()); |
| 83 } | 85 } |
| 84 return list.Pass(); | 86 return list.Pass(); |
| 85 } | 87 } |
| 86 | 88 |
| 87 | 89 |
| 88 } // namespace | 90 } // namespace |
| 89 | 91 |
| 90 // AlarmManager | 92 // AlarmManager |
| 91 | 93 |
| 92 AlarmManager::AlarmManager(Profile* profile, base::Clock* clock) | 94 AlarmManager::AlarmManager(Profile* profile) |
| 93 : profile_(profile), | 95 : profile_(profile), |
| 94 clock_(clock), | 96 clock_(new base::DefaultClock()), |
| 95 delegate_(new DefaultAlarmDelegate(profile)) { | 97 delegate_(new DefaultAlarmDelegate(profile)) { |
| 96 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 98 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 97 content::Source<Profile>(profile_)); | 99 content::Source<Profile>(profile_)); |
| 98 | 100 |
| 99 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); | 101 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); |
| 100 if (storage) | 102 if (storage) |
| 101 storage->RegisterKey(kRegisteredAlarms); | 103 storage->RegisterKey(kRegisteredAlarms); |
| 102 } | 104 } |
| 103 | 105 |
| 104 AlarmManager::~AlarmManager() { | 106 AlarmManager::~AlarmManager() { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 161 |
| 160 // Note: I'm using indices rather than iterators here because | 162 // Note: I'm using indices rather than iterators here because |
| 161 // RemoveAlarmIterator will delete the list when it becomes empty. | 163 // RemoveAlarmIterator will delete the list when it becomes empty. |
| 162 for (size_t i = 0, size = list->second.size(); i < size; ++i) | 164 for (size_t i = 0, size = list->second.size(); i < size; ++i) |
| 163 RemoveAlarmIterator(AlarmIterator(list, list->second.begin())); | 165 RemoveAlarmIterator(AlarmIterator(list, list->second.begin())); |
| 164 | 166 |
| 165 CHECK(alarms_.find(extension_id) == alarms_.end()); | 167 CHECK(alarms_.find(extension_id) == alarms_.end()); |
| 166 WriteToStorage(extension_id); | 168 WriteToStorage(extension_id); |
| 167 } | 169 } |
| 168 | 170 |
| 171 void AlarmManager::SetClockForTesting(base::Clock* clock) { |
| 172 clock_.reset(clock); |
| 173 } |
| 174 |
| 175 static base::LazyInstance<ProfileKeyedAPIFactory<AlarmManager> > |
| 176 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 177 |
| 178 // static |
| 179 ProfileKeyedAPIFactory<AlarmManager>* AlarmManager::GetFactoryInstance() { |
| 180 return &g_factory.Get(); |
| 181 } |
| 182 |
| 183 // static |
| 184 AlarmManager* AlarmManager::Get(Profile* profile) { |
| 185 return ProfileKeyedAPIFactory<AlarmManager>::GetForProfile(profile); |
| 186 } |
| 187 |
| 169 void AlarmManager::RemoveAlarmIterator(const AlarmIterator& iter) { | 188 void AlarmManager::RemoveAlarmIterator(const AlarmIterator& iter) { |
| 170 AlarmList& list = iter.first->second; | 189 AlarmList& list = iter.first->second; |
| 171 list.erase(iter.second); | 190 list.erase(iter.second); |
| 172 if (list.empty()) | 191 if (list.empty()) |
| 173 alarms_.erase(iter.first); | 192 alarms_.erase(iter.first); |
| 174 | 193 |
| 175 // Cancel the timer if there are no more alarms. | 194 // Cancel the timer if there are no more alarms. |
| 176 // We don't need to reschedule the poll otherwise, because in | 195 // We don't need to reschedule the poll otherwise, because in |
| 177 // the worst case we would just poll one extra time. | 196 // the worst case we would just poll one extra time. |
| 178 if (alarms_.empty()) | 197 if (alarms_.empty()) |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 if (create_info.period_in_minutes.get()) { | 387 if (create_info.period_in_minutes.get()) { |
| 369 js_alarm->period_in_minutes.reset( | 388 js_alarm->period_in_minutes.reset( |
| 370 new double(*create_info.period_in_minutes)); | 389 new double(*create_info.period_in_minutes)); |
| 371 } | 390 } |
| 372 } | 391 } |
| 373 | 392 |
| 374 Alarm::~Alarm() { | 393 Alarm::~Alarm() { |
| 375 } | 394 } |
| 376 | 395 |
| 377 } // namespace extensions | 396 } // namespace extensions |
| OLD | NEW |