| 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 owns_clock_(true), |
| 95 delegate_(new DefaultAlarmDelegate(profile)) { | 97 delegate_(new DefaultAlarmDelegate(profile)) { |
| 98 clock_ = new base::DefaultClock(); |
| 99 |
| 96 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 100 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 97 content::Source<Profile>(profile_)); | 101 content::Source<Profile>(profile_)); |
| 98 | 102 |
| 99 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); | 103 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); |
| 100 if (storage) | 104 if (storage) |
| 101 storage->RegisterKey(kRegisteredAlarms); | 105 storage->RegisterKey(kRegisteredAlarms); |
| 102 } | 106 } |
| 103 | 107 |
| 104 AlarmManager::~AlarmManager() { | 108 AlarmManager::~AlarmManager() { |
| 109 if (owns_clock_) |
| 110 delete clock_; |
| 105 } | 111 } |
| 106 | 112 |
| 107 void AlarmManager::AddAlarm(const std::string& extension_id, | 113 void AlarmManager::AddAlarm(const std::string& extension_id, |
| 108 const Alarm& alarm) { | 114 const Alarm& alarm) { |
| 109 AddAlarmImpl(extension_id, alarm); | 115 AddAlarmImpl(extension_id, alarm); |
| 110 WriteToStorage(extension_id); | 116 WriteToStorage(extension_id); |
| 111 } | 117 } |
| 112 | 118 |
| 113 const Alarm* AlarmManager::GetAlarm( | 119 const Alarm* AlarmManager::GetAlarm( |
| 114 const std::string& extension_id, const std::string& name) { | 120 const std::string& extension_id, const std::string& name) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 165 |
| 160 // Note: I'm using indices rather than iterators here because | 166 // Note: I'm using indices rather than iterators here because |
| 161 // RemoveAlarmIterator will delete the list when it becomes empty. | 167 // RemoveAlarmIterator will delete the list when it becomes empty. |
| 162 for (size_t i = 0, size = list->second.size(); i < size; ++i) | 168 for (size_t i = 0, size = list->second.size(); i < size; ++i) |
| 163 RemoveAlarmIterator(AlarmIterator(list, list->second.begin())); | 169 RemoveAlarmIterator(AlarmIterator(list, list->second.begin())); |
| 164 | 170 |
| 165 CHECK(alarms_.find(extension_id) == alarms_.end()); | 171 CHECK(alarms_.find(extension_id) == alarms_.end()); |
| 166 WriteToStorage(extension_id); | 172 WriteToStorage(extension_id); |
| 167 } | 173 } |
| 168 | 174 |
| 175 void AlarmManager::SetClockForTesting(base::Clock* clock) { |
| 176 if (owns_clock_) |
| 177 delete clock_; |
| 178 owns_clock_ = false; |
| 179 clock_ = clock; |
| 180 } |
| 181 |
| 182 static base::LazyInstance<ProfileKeyedAPIFactory<AlarmManager> > |
| 183 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 184 |
| 185 // static |
| 186 ProfileKeyedAPIFactory<AlarmManager>* AlarmManager::GetFactoryInstance() { |
| 187 return &g_factory.Get(); |
| 188 } |
| 189 |
| 190 // static |
| 191 AlarmManager* AlarmManager::Get(Profile* profile) { |
| 192 return ProfileKeyedAPIFactory<AlarmManager>::GetForProfile(profile); |
| 193 } |
| 194 |
| 169 void AlarmManager::RemoveAlarmIterator(const AlarmIterator& iter) { | 195 void AlarmManager::RemoveAlarmIterator(const AlarmIterator& iter) { |
| 170 AlarmList& list = iter.first->second; | 196 AlarmList& list = iter.first->second; |
| 171 list.erase(iter.second); | 197 list.erase(iter.second); |
| 172 if (list.empty()) | 198 if (list.empty()) |
| 173 alarms_.erase(iter.first); | 199 alarms_.erase(iter.first); |
| 174 | 200 |
| 175 // Cancel the timer if there are no more alarms. | 201 // Cancel the timer if there are no more alarms. |
| 176 // We don't need to reschedule the poll otherwise, because in | 202 // We don't need to reschedule the poll otherwise, because in |
| 177 // the worst case we would just poll one extra time. | 203 // the worst case we would just poll one extra time. |
| 178 if (alarms_.empty()) | 204 if (alarms_.empty()) |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 if (create_info.period_in_minutes.get()) { | 394 if (create_info.period_in_minutes.get()) { |
| 369 js_alarm->period_in_minutes.reset( | 395 js_alarm->period_in_minutes.reset( |
| 370 new double(*create_info.period_in_minutes)); | 396 new double(*create_info.period_in_minutes)); |
| 371 } | 397 } |
| 372 } | 398 } |
| 373 | 399 |
| 374 Alarm::~Alarm() { | 400 Alarm::~Alarm() { |
| 375 } | 401 } |
| 376 | 402 |
| 377 } // namespace extensions | 403 } // namespace extensions |
| OLD | NEW |