| 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.h" | 10 #include "base/message_loop.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 // The minimum period between polling for alarms to run. | 34 // The minimum period between polling for alarms to run. |
| 35 const base::TimeDelta kDefaultMinPollPeriod = base::TimeDelta::FromDays(1); | 35 const base::TimeDelta kDefaultMinPollPeriod = base::TimeDelta::FromDays(1); |
| 36 | 36 |
| 37 class DefaultAlarmDelegate : public AlarmManager::Delegate { | 37 class DefaultAlarmDelegate : public AlarmManager::Delegate { |
| 38 public: | 38 public: |
| 39 explicit DefaultAlarmDelegate(Profile* profile) : profile_(profile) {} | 39 explicit DefaultAlarmDelegate(Profile* profile) : profile_(profile) {} |
| 40 virtual ~DefaultAlarmDelegate() {} | 40 virtual ~DefaultAlarmDelegate() {} |
| 41 | 41 |
| 42 virtual void OnAlarm(const std::string& extension_id, | 42 virtual void OnAlarm(const std::string& extension_id, |
| 43 const Alarm& alarm) OVERRIDE { | 43 const Alarm& alarm) OVERRIDE { |
| 44 scoped_ptr<ListValue> args(new ListValue()); | 44 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 45 args->Append(alarm.js_alarm->ToValue().release()); | 45 args->Append(alarm.js_alarm->ToValue().release()); |
| 46 scoped_ptr<Event> event(new Event(kOnAlarmEvent, args.Pass())); | 46 scoped_ptr<Event> event(new Event(kOnAlarmEvent, args.Pass())); |
| 47 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 47 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
| 48 extension_id, event.Pass()); | 48 extension_id, event.Pass()); |
| 49 } | 49 } |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 Profile* profile_; | 52 Profile* profile_; |
| 53 }; | 53 }; |
| 54 | 54 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 68 const base::Value* time_value = NULL; | 68 const base::Value* time_value = NULL; |
| 69 if (alarm_dict->Get(kAlarmGranularity, &time_value)) | 69 if (alarm_dict->Get(kAlarmGranularity, &time_value)) |
| 70 base::GetValueAsTimeDelta(*time_value, &alarm.granularity); | 70 base::GetValueAsTimeDelta(*time_value, &alarm.granularity); |
| 71 alarms.push_back(alarm); | 71 alarms.push_back(alarm); |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 return alarms; | 74 return alarms; |
| 75 } | 75 } |
| 76 | 76 |
| 77 scoped_ptr<base::ListValue> AlarmsToValue(const std::vector<Alarm>& alarms) { | 77 scoped_ptr<base::ListValue> AlarmsToValue(const std::vector<Alarm>& alarms) { |
| 78 scoped_ptr<base::ListValue> list(new ListValue()); | 78 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 79 for (size_t i = 0; i < alarms.size(); ++i) { | 79 for (size_t i = 0; i < alarms.size(); ++i) { |
| 80 scoped_ptr<base::DictionaryValue> alarm = | 80 scoped_ptr<base::DictionaryValue> alarm = |
| 81 alarms[i].js_alarm->ToValue().Pass(); | 81 alarms[i].js_alarm->ToValue().Pass(); |
| 82 alarm->Set(kAlarmGranularity, | 82 alarm->Set(kAlarmGranularity, |
| 83 base::CreateTimeDeltaValue(alarms[i].granularity)); | 83 base::CreateTimeDeltaValue(alarms[i].granularity)); |
| 84 list->Append(alarm.release()); | 84 list->Append(alarm.release()); |
| 85 } | 85 } |
| 86 return list.Pass(); | 86 return list.Pass(); |
| 87 } | 87 } |
| 88 | 88 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 alarms_[extension_id].push_back(alarm); | 227 alarms_[extension_id].push_back(alarm); |
| 228 | 228 |
| 229 ScheduleNextPoll(); | 229 ScheduleNextPoll(); |
| 230 } | 230 } |
| 231 | 231 |
| 232 void AlarmManager::WriteToStorage(const std::string& extension_id) { | 232 void AlarmManager::WriteToStorage(const std::string& extension_id) { |
| 233 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); | 233 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); |
| 234 if (!storage) | 234 if (!storage) |
| 235 return; | 235 return; |
| 236 | 236 |
| 237 scoped_ptr<Value> alarms; | 237 scoped_ptr<base::Value> alarms; |
| 238 AlarmMap::iterator list = alarms_.find(extension_id); | 238 AlarmMap::iterator list = alarms_.find(extension_id); |
| 239 if (list != alarms_.end()) | 239 if (list != alarms_.end()) |
| 240 alarms.reset(AlarmsToValue(list->second).release()); | 240 alarms.reset(AlarmsToValue(list->second).release()); |
| 241 else | 241 else |
| 242 alarms.reset(AlarmsToValue(std::vector<Alarm>()).release()); | 242 alarms.reset(AlarmsToValue(std::vector<Alarm>()).release()); |
| 243 storage->SetExtensionValue(extension_id, kRegisteredAlarms, alarms.Pass()); | 243 storage->SetExtensionValue(extension_id, kRegisteredAlarms, alarms.Pass()); |
| 244 } | 244 } |
| 245 | 245 |
| 246 void AlarmManager::ReadFromStorage(const std::string& extension_id, | 246 void AlarmManager::ReadFromStorage(const std::string& extension_id, |
| 247 scoped_ptr<base::Value> value) { | 247 scoped_ptr<base::Value> value) { |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 if (create_info.period_in_minutes.get()) { | 387 if (create_info.period_in_minutes.get()) { |
| 388 js_alarm->period_in_minutes.reset( | 388 js_alarm->period_in_minutes.reset( |
| 389 new double(*create_info.period_in_minutes)); | 389 new double(*create_info.period_in_minutes)); |
| 390 } | 390 } |
| 391 } | 391 } |
| 392 | 392 |
| 393 Alarm::~Alarm() { | 393 Alarm::~Alarm() { |
| 394 } | 394 } |
| 395 | 395 |
| 396 } // namespace extensions | 396 } // namespace extensions |
| OLD | NEW |