| 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/lazy_instance.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/time/clock.h" | 11 #include "base/time/clock.h" |
| 12 #include "base/time/default_clock.h" | 12 #include "base/time/default_clock.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "base/value_conversions.h" | 14 #include "base/value_conversions.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/extensions/state_store.h" | 18 #include "chrome/browser/extensions/state_store.h" |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 20 #include "chrome/common/extensions/api/alarms.h" | 19 #include "chrome/common/extensions/api/alarms.h" |
| 21 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 22 #include "extensions/browser/event_router.h" | 21 #include "extensions/browser/event_router.h" |
| 22 #include "extensions/browser/extension_registry.h" |
| 23 #include "extensions/browser/extension_system.h" | 23 #include "extensions/browser/extension_system.h" |
| 24 | 24 |
| 25 namespace extensions { | 25 namespace extensions { |
| 26 | 26 |
| 27 namespace alarms = api::alarms; | 27 namespace alarms = api::alarms; |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // A list of alarms that this extension has set. | 31 // A list of alarms that this extension has set. |
| 32 const char kRegisteredAlarms[] = "alarms"; | 32 const char kRegisteredAlarms[] = "alarms"; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 } | 90 } |
| 91 return list.Pass(); | 91 return list.Pass(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 | 94 |
| 95 } // namespace | 95 } // namespace |
| 96 | 96 |
| 97 // AlarmManager | 97 // AlarmManager |
| 98 | 98 |
| 99 AlarmManager::AlarmManager(content::BrowserContext* context) | 99 AlarmManager::AlarmManager(content::BrowserContext* context) |
| 100 : profile_(Profile::FromBrowserContext(context)), | 100 : browser_context_(context), |
| 101 clock_(new base::DefaultClock()), | 101 clock_(new base::DefaultClock()), |
| 102 delegate_(new DefaultAlarmDelegate(context)) { | 102 delegate_(new DefaultAlarmDelegate(context)), |
| 103 extension_registry_observer_(this) { |
| 104 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 103 registrar_.Add(this, | 105 registrar_.Add(this, |
| 104 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 106 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 105 content::Source<Profile>(profile_)); | 107 content::Source<content::BrowserContext>(browser_context_)); |
| 106 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | |
| 107 content::Source<Profile>(profile_)); | |
| 108 | 108 |
| 109 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); | 109 StateStore* storage = ExtensionSystem::Get(browser_context_)->state_store(); |
| 110 if (storage) | 110 if (storage) |
| 111 storage->RegisterKey(kRegisteredAlarms); | 111 storage->RegisterKey(kRegisteredAlarms); |
| 112 } | 112 } |
| 113 | 113 |
| 114 AlarmManager::~AlarmManager() { | 114 AlarmManager::~AlarmManager() { |
| 115 } | 115 } |
| 116 | 116 |
| 117 void AlarmManager::AddAlarm(const std::string& extension_id, | 117 void AlarmManager::AddAlarm(const std::string& extension_id, |
| 118 const Alarm& alarm, | 118 const Alarm& alarm, |
| 119 const AddAlarmCallback& callback) { | 119 const AddAlarmCallback& callback) { |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 static base::LazyInstance<BrowserContextKeyedAPIFactory<AlarmManager> > | 219 static base::LazyInstance<BrowserContextKeyedAPIFactory<AlarmManager> > |
| 220 g_factory = LAZY_INSTANCE_INITIALIZER; | 220 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 221 | 221 |
| 222 // static | 222 // static |
| 223 BrowserContextKeyedAPIFactory<AlarmManager>* | 223 BrowserContextKeyedAPIFactory<AlarmManager>* |
| 224 AlarmManager::GetFactoryInstance() { | 224 AlarmManager::GetFactoryInstance() { |
| 225 return g_factory.Pointer(); | 225 return g_factory.Pointer(); |
| 226 } | 226 } |
| 227 | 227 |
| 228 // static | 228 // static |
| 229 AlarmManager* AlarmManager::Get(Profile* profile) { | 229 AlarmManager* AlarmManager::Get(content::BrowserContext* browser_context) { |
| 230 return BrowserContextKeyedAPIFactory<AlarmManager>::Get(profile); | 230 return BrowserContextKeyedAPIFactory<AlarmManager>::Get(browser_context); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void AlarmManager::RemoveAlarmIterator(const AlarmIterator& iter) { | 233 void AlarmManager::RemoveAlarmIterator(const AlarmIterator& iter) { |
| 234 AlarmList& list = iter.first->second; | 234 AlarmList& list = iter.first->second; |
| 235 list.erase(iter.second); | 235 list.erase(iter.second); |
| 236 if (list.empty()) | 236 if (list.empty()) |
| 237 alarms_.erase(iter.first); | 237 alarms_.erase(iter.first); |
| 238 | 238 |
| 239 // Cancel the timer if there are no more alarms. | 239 // Cancel the timer if there are no more alarms. |
| 240 // We don't need to reschedule the poll otherwise, because in | 240 // We don't need to reschedule the poll otherwise, because in |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 RemoveAlarmIterator(old_alarm); | 283 RemoveAlarmIterator(old_alarm); |
| 284 | 284 |
| 285 alarms_[extension_id].push_back(alarm); | 285 alarms_[extension_id].push_back(alarm); |
| 286 base::Time alarm_time = | 286 base::Time alarm_time = |
| 287 base::Time::FromJsTime(alarm.js_alarm->scheduled_time); | 287 base::Time::FromJsTime(alarm.js_alarm->scheduled_time); |
| 288 if (next_poll_time_.is_null() || alarm_time < next_poll_time_) | 288 if (next_poll_time_.is_null() || alarm_time < next_poll_time_) |
| 289 SetNextPollTime(alarm_time); | 289 SetNextPollTime(alarm_time); |
| 290 } | 290 } |
| 291 | 291 |
| 292 void AlarmManager::WriteToStorage(const std::string& extension_id) { | 292 void AlarmManager::WriteToStorage(const std::string& extension_id) { |
| 293 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); | 293 StateStore* storage = ExtensionSystem::Get(browser_context_)->state_store(); |
| 294 if (!storage) | 294 if (!storage) |
| 295 return; | 295 return; |
| 296 | 296 |
| 297 scoped_ptr<base::Value> alarms; | 297 scoped_ptr<base::Value> alarms; |
| 298 AlarmMap::iterator list = alarms_.find(extension_id); | 298 AlarmMap::iterator list = alarms_.find(extension_id); |
| 299 if (list != alarms_.end()) | 299 if (list != alarms_.end()) |
| 300 alarms.reset(AlarmsToValue(list->second).release()); | 300 alarms.reset(AlarmsToValue(list->second).release()); |
| 301 else | 301 else |
| 302 alarms.reset(AlarmsToValue(std::vector<Alarm>()).release()); | 302 alarms.reset(AlarmsToValue(std::vector<Alarm>()).release()); |
| 303 storage->SetExtensionValue(extension_id, kRegisteredAlarms, alarms.Pass()); | 303 storage->SetExtensionValue(extension_id, kRegisteredAlarms, alarms.Pass()); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 void AlarmManager::RunWhenReady( | 403 void AlarmManager::RunWhenReady( |
| 404 const std::string& extension_id, const ReadyAction& action) { | 404 const std::string& extension_id, const ReadyAction& action) { |
| 405 ReadyMap::iterator it = ready_actions_.find(extension_id); | 405 ReadyMap::iterator it = ready_actions_.find(extension_id); |
| 406 | 406 |
| 407 if (it == ready_actions_.end()) | 407 if (it == ready_actions_.end()) |
| 408 action.Run(extension_id); | 408 action.Run(extension_id); |
| 409 else | 409 else |
| 410 it->second.push(action); | 410 it->second.push(action); |
| 411 } | 411 } |
| 412 | 412 |
| 413 void AlarmManager::OnExtensionLoaded(content::BrowserContext* browser_context, |
| 414 const Extension* extension) { |
| 415 StateStore* storage = ExtensionSystem::Get(browser_context_)->state_store(); |
| 416 if (storage) { |
| 417 ready_actions_.insert(ReadyMap::value_type(extension->id(), ReadyQueue())); |
| 418 storage->GetExtensionValue( |
| 419 extension->id(), |
| 420 kRegisteredAlarms, |
| 421 base::Bind( |
| 422 &AlarmManager::ReadFromStorage, AsWeakPtr(), extension->id())); |
| 423 } |
| 424 } |
| 425 |
| 413 void AlarmManager::Observe( | 426 void AlarmManager::Observe( |
| 414 int type, | 427 int type, |
| 415 const content::NotificationSource& source, | 428 const content::NotificationSource& source, |
| 416 const content::NotificationDetails& details) { | 429 const content::NotificationDetails& details) { |
| 417 switch (type) { | 430 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNINSTALLED); |
| 418 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { | 431 const Extension* extension = content::Details<const Extension>(details).ptr(); |
| 419 const Extension* extension = | 432 RemoveAllAlarms(extension->id(), base::Bind(RemoveAllOnUninstallCallback)); |
| 420 content::Details<const Extension>(details).ptr(); | |
| 421 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); | |
| 422 if (storage) { | |
| 423 ready_actions_.insert( | |
| 424 ReadyMap::value_type(extension->id(), ReadyQueue())); | |
| 425 storage->GetExtensionValue(extension->id(), kRegisteredAlarms, | |
| 426 base::Bind(&AlarmManager::ReadFromStorage, | |
| 427 AsWeakPtr(), extension->id())); | |
| 428 } | |
| 429 break; | |
| 430 } | |
| 431 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | |
| 432 const Extension* extension = | |
| 433 content::Details<const Extension>(details).ptr(); | |
| 434 RemoveAllAlarms( | |
| 435 extension->id(), base::Bind(RemoveAllOnUninstallCallback)); | |
| 436 break; | |
| 437 } | |
| 438 default: | |
| 439 NOTREACHED(); | |
| 440 break; | |
| 441 } | |
| 442 } | 433 } |
| 443 | 434 |
| 444 // AlarmManager::Alarm | 435 // AlarmManager::Alarm |
| 445 | 436 |
| 446 Alarm::Alarm() | 437 Alarm::Alarm() |
| 447 : js_alarm(new api::alarms::Alarm()) { | 438 : js_alarm(new api::alarms::Alarm()) { |
| 448 } | 439 } |
| 449 | 440 |
| 450 Alarm::Alarm(const std::string& name, | 441 Alarm::Alarm(const std::string& name, |
| 451 const api::alarms::AlarmCreateInfo& create_info, | 442 const api::alarms::AlarmCreateInfo& create_info, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 479 if (create_info.period_in_minutes.get()) { | 470 if (create_info.period_in_minutes.get()) { |
| 480 js_alarm->period_in_minutes.reset( | 471 js_alarm->period_in_minutes.reset( |
| 481 new double(*create_info.period_in_minutes)); | 472 new double(*create_info.period_in_minutes)); |
| 482 } | 473 } |
| 483 } | 474 } |
| 484 | 475 |
| 485 Alarm::~Alarm() { | 476 Alarm::~Alarm() { |
| 486 } | 477 } |
| 487 | 478 |
| 488 } // namespace extensions | 479 } // namespace extensions |
| OLD | NEW |