| 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" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 const char kRegisteredAlarms[] = "alarms"; | 32 const char kRegisteredAlarms[] = "alarms"; |
| 33 const char kAlarmGranularity[] = "granularity"; | 33 const char kAlarmGranularity[] = "granularity"; |
| 34 | 34 |
| 35 // The minimum period between polling for alarms to run. | 35 // The minimum period between polling for alarms to run. |
| 36 const base::TimeDelta kDefaultMinPollPeriod() { | 36 const base::TimeDelta kDefaultMinPollPeriod() { |
| 37 return base::TimeDelta::FromDays(1); | 37 return base::TimeDelta::FromDays(1); |
| 38 } | 38 } |
| 39 | 39 |
| 40 class DefaultAlarmDelegate : public AlarmManager::Delegate { | 40 class DefaultAlarmDelegate : public AlarmManager::Delegate { |
| 41 public: | 41 public: |
| 42 explicit DefaultAlarmDelegate(Profile* profile) : profile_(profile) {} | 42 explicit DefaultAlarmDelegate(content::BrowserContext* context) |
| 43 : context_(context) {} |
| 43 virtual ~DefaultAlarmDelegate() {} | 44 virtual ~DefaultAlarmDelegate() {} |
| 44 | 45 |
| 45 virtual void OnAlarm(const std::string& extension_id, | 46 virtual void OnAlarm(const std::string& extension_id, |
| 46 const Alarm& alarm) OVERRIDE { | 47 const Alarm& alarm) OVERRIDE { |
| 47 scoped_ptr<base::ListValue> args(new base::ListValue()); | 48 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 48 args->Append(alarm.js_alarm->ToValue().release()); | 49 args->Append(alarm.js_alarm->ToValue().release()); |
| 49 scoped_ptr<Event> event(new Event(alarms::OnAlarm::kEventName, | 50 scoped_ptr<Event> event(new Event(alarms::OnAlarm::kEventName, |
| 50 args.Pass())); | 51 args.Pass())); |
| 51 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 52 ExtensionSystem::Get(context_)->event_router()->DispatchEventToExtension( |
| 52 extension_id, event.Pass()); | 53 extension_id, event.Pass()); |
| 53 } | 54 } |
| 54 | 55 |
| 55 private: | 56 private: |
| 56 Profile* profile_; | 57 content::BrowserContext* context_; |
| 57 }; | 58 }; |
| 58 | 59 |
| 59 // Creates a TimeDelta from a delay as specified in the API. | 60 // Creates a TimeDelta from a delay as specified in the API. |
| 60 base::TimeDelta TimeDeltaFromDelay(double delay_in_minutes) { | 61 base::TimeDelta TimeDeltaFromDelay(double delay_in_minutes) { |
| 61 return base::TimeDelta::FromMicroseconds( | 62 return base::TimeDelta::FromMicroseconds( |
| 62 delay_in_minutes * base::Time::kMicrosecondsPerMinute); | 63 delay_in_minutes * base::Time::kMicrosecondsPerMinute); |
| 63 } | 64 } |
| 64 | 65 |
| 65 std::vector<Alarm> AlarmsFromValue(const base::ListValue* list) { | 66 std::vector<Alarm> AlarmsFromValue(const base::ListValue* list) { |
| 66 std::vector<Alarm> alarms; | 67 std::vector<Alarm> alarms; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 88 list->Append(alarm.release()); | 89 list->Append(alarm.release()); |
| 89 } | 90 } |
| 90 return list.Pass(); | 91 return list.Pass(); |
| 91 } | 92 } |
| 92 | 93 |
| 93 | 94 |
| 94 } // namespace | 95 } // namespace |
| 95 | 96 |
| 96 // AlarmManager | 97 // AlarmManager |
| 97 | 98 |
| 98 AlarmManager::AlarmManager(Profile* profile) | 99 AlarmManager::AlarmManager(content::BrowserContext* context) |
| 99 : profile_(profile), | 100 : profile_(Profile::FromBrowserContext(context)), |
| 100 clock_(new base::DefaultClock()), | 101 clock_(new base::DefaultClock()), |
| 101 delegate_(new DefaultAlarmDelegate(profile)) { | 102 delegate_(new DefaultAlarmDelegate(context)) { |
| 102 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 103 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 103 content::Source<Profile>(profile_)); | 104 content::Source<Profile>(profile_)); |
| 104 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 105 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 105 content::Source<Profile>(profile_)); | 106 content::Source<Profile>(profile_)); |
| 106 | 107 |
| 107 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); | 108 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); |
| 108 if (storage) | 109 if (storage) |
| 109 storage->RegisterKey(kRegisteredAlarms); | 110 storage->RegisterKey(kRegisteredAlarms); |
| 110 } | 111 } |
| 111 | 112 |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 if (create_info.period_in_minutes.get()) { | 477 if (create_info.period_in_minutes.get()) { |
| 477 js_alarm->period_in_minutes.reset( | 478 js_alarm->period_in_minutes.reset( |
| 478 new double(*create_info.period_in_minutes)); | 479 new double(*create_info.period_in_minutes)); |
| 479 } | 480 } |
| 480 } | 481 } |
| 481 | 482 |
| 482 Alarm::~Alarm() { | 483 Alarm::~Alarm() { |
| 483 } | 484 } |
| 484 | 485 |
| 485 } // namespace extensions | 486 } // namespace extensions |
| OLD | NEW |