| 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 "extensions/browser/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/extensions/extension_service.h" | |
| 17 #include "chrome/common/extensions/api/alarms.h" | |
| 18 #include "extensions/browser/event_router.h" | 16 #include "extensions/browser/event_router.h" |
| 19 #include "extensions/browser/extension_registry.h" | 17 #include "extensions/browser/extension_registry.h" |
| 20 #include "extensions/browser/extension_system.h" | 18 #include "extensions/browser/extension_system.h" |
| 21 #include "extensions/browser/state_store.h" | 19 #include "extensions/browser/state_store.h" |
| 20 #include "extensions/common/api/alarms.h" |
| 22 | 21 |
| 23 namespace extensions { | 22 namespace extensions { |
| 24 | 23 |
| 25 namespace alarms = api::alarms; | 24 namespace alarms = core_api::alarms; |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 // A list of alarms that this extension has set. | 28 // A list of alarms that this extension has set. |
| 30 const char kRegisteredAlarms[] = "alarms"; | 29 const char kRegisteredAlarms[] = "alarms"; |
| 31 const char kAlarmGranularity[] = "granularity"; | 30 const char kAlarmGranularity[] = "granularity"; |
| 32 | 31 |
| 33 // The minimum period between polling for alarms to run. | 32 // The minimum period between polling for alarms to run. |
| 34 const base::TimeDelta kDefaultMinPollPeriod() { | 33 const base::TimeDelta kDefaultMinPollPeriod() { |
| 35 return base::TimeDelta::FromDays(1); | 34 return base::TimeDelta::FromDays(1); |
| 36 } | 35 } |
| 37 | 36 |
| 38 class DefaultAlarmDelegate : public AlarmManager::Delegate { | 37 class DefaultAlarmDelegate : public AlarmManager::Delegate { |
| 39 public: | 38 public: |
| 40 explicit DefaultAlarmDelegate(content::BrowserContext* context) | 39 explicit DefaultAlarmDelegate(content::BrowserContext* context) |
| 41 : browser_context_(context) {} | 40 : browser_context_(context) {} |
| 42 ~DefaultAlarmDelegate() override {} | 41 ~DefaultAlarmDelegate() override {} |
| 43 | 42 |
| 44 void OnAlarm(const std::string& extension_id, const Alarm& alarm) override { | 43 void OnAlarm(const std::string& extension_id, const Alarm& alarm) override { |
| 45 scoped_ptr<base::ListValue> args(new base::ListValue()); | 44 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 46 args->Append(alarm.js_alarm->ToValue().release()); | 45 args->Append(alarm.js_alarm->ToValue().release()); |
| 47 scoped_ptr<Event> event(new Event(alarms::OnAlarm::kEventName, | 46 scoped_ptr<Event> event( |
| 48 args.Pass())); | 47 new Event(alarms::OnAlarm::kEventName, args.Pass())); |
| 49 EventRouter::Get(browser_context_) | 48 EventRouter::Get(browser_context_) |
| 50 ->DispatchEventToExtension(extension_id, event.Pass()); | 49 ->DispatchEventToExtension(extension_id, event.Pass()); |
| 51 } | 50 } |
| 52 | 51 |
| 53 private: | 52 private: |
| 54 content::BrowserContext* browser_context_; | 53 content::BrowserContext* browser_context_; |
| 55 }; | 54 }; |
| 56 | 55 |
| 57 // Creates a TimeDelta from a delay as specified in the API. | 56 // Creates a TimeDelta from a delay as specified in the API. |
| 58 base::TimeDelta TimeDeltaFromDelay(double delay_in_minutes) { | 57 base::TimeDelta TimeDeltaFromDelay(double delay_in_minutes) { |
| 59 return base::TimeDelta::FromMicroseconds( | 58 return base::TimeDelta::FromMicroseconds(delay_in_minutes * |
| 60 delay_in_minutes * base::Time::kMicrosecondsPerMinute); | 59 base::Time::kMicrosecondsPerMinute); |
| 61 } | 60 } |
| 62 | 61 |
| 63 std::vector<Alarm> AlarmsFromValue(const base::ListValue* list) { | 62 std::vector<Alarm> AlarmsFromValue(const base::ListValue* list) { |
| 64 std::vector<Alarm> alarms; | 63 std::vector<Alarm> alarms; |
| 65 for (size_t i = 0; i < list->GetSize(); ++i) { | 64 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 66 const base::DictionaryValue* alarm_dict = NULL; | 65 const base::DictionaryValue* alarm_dict = NULL; |
| 67 Alarm alarm; | 66 Alarm alarm; |
| 68 if (list->GetDictionary(i, &alarm_dict) && | 67 if (list->GetDictionary(i, &alarm_dict) && |
| 69 api::alarms::Alarm::Populate(*alarm_dict, alarm.js_alarm.get())) { | 68 alarms::Alarm::Populate(*alarm_dict, alarm.js_alarm.get())) { |
| 70 const base::Value* time_value = NULL; | 69 const base::Value* time_value = NULL; |
| 71 if (alarm_dict->Get(kAlarmGranularity, &time_value)) | 70 if (alarm_dict->Get(kAlarmGranularity, &time_value)) |
| 72 base::GetValueAsTimeDelta(*time_value, &alarm.granularity); | 71 base::GetValueAsTimeDelta(*time_value, &alarm.granularity); |
| 73 alarms.push_back(alarm); | 72 alarms.push_back(alarm); |
| 74 } | 73 } |
| 75 } | 74 } |
| 76 return alarms; | 75 return alarms; |
| 77 } | 76 } |
| 78 | 77 |
| 79 scoped_ptr<base::ListValue> AlarmsToValue(const std::vector<Alarm>& alarms) { | 78 scoped_ptr<base::ListValue> AlarmsToValue(const std::vector<Alarm>& alarms) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 103 if (storage) | 102 if (storage) |
| 104 storage->RegisterKey(kRegisteredAlarms); | 103 storage->RegisterKey(kRegisteredAlarms); |
| 105 } | 104 } |
| 106 | 105 |
| 107 AlarmManager::~AlarmManager() { | 106 AlarmManager::~AlarmManager() { |
| 108 } | 107 } |
| 109 | 108 |
| 110 void AlarmManager::AddAlarm(const std::string& extension_id, | 109 void AlarmManager::AddAlarm(const std::string& extension_id, |
| 111 const Alarm& alarm, | 110 const Alarm& alarm, |
| 112 const AddAlarmCallback& callback) { | 111 const AddAlarmCallback& callback) { |
| 113 RunWhenReady(extension_id, base::Bind( | 112 RunWhenReady(extension_id, base::Bind(&AlarmManager::AddAlarmWhenReady, |
| 114 &AlarmManager::AddAlarmWhenReady, AsWeakPtr(), alarm, callback)); | 113 AsWeakPtr(), alarm, callback)); |
| 115 } | 114 } |
| 116 | 115 |
| 117 void AlarmManager::GetAlarm(const std::string& extension_id, | 116 void AlarmManager::GetAlarm(const std::string& extension_id, |
| 118 const std::string& name, | 117 const std::string& name, |
| 119 const GetAlarmCallback& callback) { | 118 const GetAlarmCallback& callback) { |
| 120 RunWhenReady(extension_id, base::Bind( | 119 RunWhenReady(extension_id, base::Bind(&AlarmManager::GetAlarmWhenReady, |
| 121 &AlarmManager::GetAlarmWhenReady, AsWeakPtr(), name, callback)); | 120 AsWeakPtr(), name, callback)); |
| 122 } | 121 } |
| 123 | 122 |
| 124 void AlarmManager::GetAllAlarms( | 123 void AlarmManager::GetAllAlarms(const std::string& extension_id, |
| 125 const std::string& extension_id, const GetAllAlarmsCallback& callback) { | 124 const GetAllAlarmsCallback& callback) { |
| 126 RunWhenReady(extension_id, base::Bind( | 125 RunWhenReady(extension_id, base::Bind(&AlarmManager::GetAllAlarmsWhenReady, |
| 127 &AlarmManager::GetAllAlarmsWhenReady, AsWeakPtr(), callback)); | 126 AsWeakPtr(), callback)); |
| 128 } | 127 } |
| 129 | 128 |
| 130 void AlarmManager::RemoveAlarm(const std::string& extension_id, | 129 void AlarmManager::RemoveAlarm(const std::string& extension_id, |
| 131 const std::string& name, | 130 const std::string& name, |
| 132 const RemoveAlarmCallback& callback) { | 131 const RemoveAlarmCallback& callback) { |
| 133 RunWhenReady(extension_id, base::Bind( | 132 RunWhenReady(extension_id, base::Bind(&AlarmManager::RemoveAlarmWhenReady, |
| 134 &AlarmManager::RemoveAlarmWhenReady, AsWeakPtr(), name, callback)); | 133 AsWeakPtr(), name, callback)); |
| 135 } | 134 } |
| 136 | 135 |
| 137 void AlarmManager::RemoveAllAlarms(const std::string& extension_id, | 136 void AlarmManager::RemoveAllAlarms(const std::string& extension_id, |
| 138 const RemoveAllAlarmsCallback& callback) { | 137 const RemoveAllAlarmsCallback& callback) { |
| 139 RunWhenReady(extension_id, base::Bind( | 138 RunWhenReady(extension_id, base::Bind(&AlarmManager::RemoveAllAlarmsWhenReady, |
| 140 &AlarmManager::RemoveAllAlarmsWhenReady, AsWeakPtr(), callback)); | 139 AsWeakPtr(), callback)); |
| 141 } | 140 } |
| 142 | 141 |
| 143 void AlarmManager::AddAlarmWhenReady(const Alarm& alarm, | 142 void AlarmManager::AddAlarmWhenReady(const Alarm& alarm, |
| 144 const AddAlarmCallback& callback, | 143 const AddAlarmCallback& callback, |
| 145 const std::string& extension_id) { | 144 const std::string& extension_id) { |
| 146 AddAlarmImpl(extension_id, alarm); | 145 AddAlarmImpl(extension_id, alarm); |
| 147 WriteToStorage(extension_id); | 146 WriteToStorage(extension_id); |
| 148 callback.Run(); | 147 callback.Run(); |
| 149 } | 148 } |
| 150 | 149 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 169 callback.Run(false); | 168 callback.Run(false); |
| 170 return; | 169 return; |
| 171 } | 170 } |
| 172 | 171 |
| 173 RemoveAlarmIterator(it); | 172 RemoveAlarmIterator(it); |
| 174 WriteToStorage(extension_id); | 173 WriteToStorage(extension_id); |
| 175 callback.Run(true); | 174 callback.Run(true); |
| 176 } | 175 } |
| 177 | 176 |
| 178 void AlarmManager::RemoveAllAlarmsWhenReady( | 177 void AlarmManager::RemoveAllAlarmsWhenReady( |
| 179 const RemoveAllAlarmsCallback& callback, const std::string& extension_id) { | 178 const RemoveAllAlarmsCallback& callback, |
| 179 const std::string& extension_id) { |
| 180 AlarmMap::iterator list = alarms_.find(extension_id); | 180 AlarmMap::iterator list = alarms_.find(extension_id); |
| 181 if (list != alarms_.end()) { | 181 if (list != alarms_.end()) { |
| 182 // Note: I'm using indices rather than iterators here because | 182 // Note: I'm using indices rather than iterators here because |
| 183 // RemoveAlarmIterator will delete the list when it becomes empty. | 183 // RemoveAlarmIterator will delete the list when it becomes empty. |
| 184 for (size_t i = 0, size = list->second.size(); i < size; ++i) | 184 for (size_t i = 0, size = list->second.size(); i < size; ++i) |
| 185 RemoveAlarmIterator(AlarmIterator(list, list->second.begin())); | 185 RemoveAlarmIterator(AlarmIterator(list, list->second.begin())); |
| 186 | 186 |
| 187 CHECK(alarms_.find(extension_id) == alarms_.end()); | 187 CHECK(alarms_.find(extension_id) == alarms_.end()); |
| 188 WriteToStorage(extension_id); | 188 WriteToStorage(extension_id); |
| 189 } | 189 } |
| 190 callback.Run(); | 190 callback.Run(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 AlarmManager::AlarmIterator AlarmManager::GetAlarmIterator( | 193 AlarmManager::AlarmIterator AlarmManager::GetAlarmIterator( |
| 194 const std::string& extension_id, const std::string& name) { | 194 const std::string& extension_id, |
| 195 const std::string& name) { |
| 195 AlarmMap::iterator list = alarms_.find(extension_id); | 196 AlarmMap::iterator list = alarms_.find(extension_id); |
| 196 if (list == alarms_.end()) | 197 if (list == alarms_.end()) |
| 197 return make_pair(alarms_.end(), AlarmList::iterator()); | 198 return make_pair(alarms_.end(), AlarmList::iterator()); |
| 198 | 199 |
| 199 for (AlarmList::iterator it = list->second.begin(); | 200 for (AlarmList::iterator it = list->second.begin(); it != list->second.end(); |
| 200 it != list->second.end(); ++it) { | 201 ++it) { |
| 201 if (it->js_alarm->name == name) | 202 if (it->js_alarm->name == name) |
| 202 return make_pair(list, it); | 203 return make_pair(list, it); |
| 203 } | 204 } |
| 204 | 205 |
| 205 return make_pair(alarms_.end(), AlarmList::iterator()); | 206 return make_pair(alarms_.end(), AlarmList::iterator()); |
| 206 } | 207 } |
| 207 | 208 |
| 208 void AlarmManager::SetClockForTesting(base::Clock* clock) { | 209 void AlarmManager::SetClockForTesting(base::Clock* clock) { |
| 209 clock_.reset(clock); | 210 clock_.reset(clock); |
| 210 } | 211 } |
| 211 | 212 |
| 212 static base::LazyInstance<BrowserContextKeyedAPIFactory<AlarmManager> > | 213 static base::LazyInstance<BrowserContextKeyedAPIFactory<AlarmManager>> |
| 213 g_factory = LAZY_INSTANCE_INITIALIZER; | 214 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 214 | 215 |
| 215 // static | 216 // static |
| 216 BrowserContextKeyedAPIFactory<AlarmManager>* | 217 BrowserContextKeyedAPIFactory<AlarmManager>* |
| 217 AlarmManager::GetFactoryInstance() { | 218 AlarmManager::GetFactoryInstance() { |
| 218 return g_factory.Pointer(); | 219 return g_factory.Pointer(); |
| 219 } | 220 } |
| 220 | 221 |
| 221 // static | 222 // static |
| 222 AlarmManager* AlarmManager::Get(content::BrowserContext* browser_context) { | 223 AlarmManager* AlarmManager::Get(content::BrowserContext* browser_context) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 238 } | 239 } |
| 239 } | 240 } |
| 240 | 241 |
| 241 void AlarmManager::OnAlarm(AlarmIterator it) { | 242 void AlarmManager::OnAlarm(AlarmIterator it) { |
| 242 CHECK(it.first != alarms_.end()); | 243 CHECK(it.first != alarms_.end()); |
| 243 Alarm& alarm = *it.second; | 244 Alarm& alarm = *it.second; |
| 244 std::string extension_id_copy(it.first->first); | 245 std::string extension_id_copy(it.first->first); |
| 245 delegate_->OnAlarm(extension_id_copy, alarm); | 246 delegate_->OnAlarm(extension_id_copy, alarm); |
| 246 | 247 |
| 247 // Update our scheduled time for the next alarm. | 248 // Update our scheduled time for the next alarm. |
| 248 if (double* period_in_minutes = | 249 if (double* period_in_minutes = alarm.js_alarm->period_in_minutes.get()) { |
| 249 alarm.js_alarm->period_in_minutes.get()) { | |
| 250 // Get the timer's delay in JS time (i.e., convert it from minutes to | 250 // Get the timer's delay in JS time (i.e., convert it from minutes to |
| 251 // milliseconds). | 251 // milliseconds). |
| 252 double period_in_js_time = | 252 double period_in_js_time = *period_in_minutes * |
| 253 *period_in_minutes * base::Time::kMicrosecondsPerMinute / | 253 base::Time::kMicrosecondsPerMinute / |
| 254 base::Time::kMicrosecondsPerMillisecond; | 254 base::Time::kMicrosecondsPerMillisecond; |
| 255 // Find out how many periods have transpired since the alarm last went off | 255 // Find out how many periods have transpired since the alarm last went off |
| 256 // (it's possible that we missed some). | 256 // (it's possible that we missed some). |
| 257 int transpired_periods = | 257 int transpired_periods = |
| 258 (last_poll_time_.ToJsTime() - alarm.js_alarm->scheduled_time) / | 258 (last_poll_time_.ToJsTime() - alarm.js_alarm->scheduled_time) / |
| 259 period_in_js_time; | 259 period_in_js_time; |
| 260 // Schedule the alarm for the next period that is in-line with the original | 260 // Schedule the alarm for the next period that is in-line with the original |
| 261 // scheduling. | 261 // scheduling. |
| 262 alarm.js_alarm->scheduled_time += | 262 alarm.js_alarm->scheduled_time += |
| 263 period_in_js_time * (transpired_periods + 1); | 263 period_in_js_time * (transpired_periods + 1); |
| 264 } else { | 264 } else { |
| 265 RemoveAlarmIterator(it); | 265 RemoveAlarmIterator(it); |
| 266 } | 266 } |
| 267 WriteToStorage(extension_id_copy); | 267 WriteToStorage(extension_id_copy); |
| 268 } | 268 } |
| 269 | 269 |
| 270 void AlarmManager::AddAlarmImpl(const std::string& extension_id, | 270 void AlarmManager::AddAlarmImpl(const std::string& extension_id, |
| 271 const Alarm& alarm) { | 271 const Alarm& alarm) { |
| 272 // Override any old alarm with the same name. | 272 // Override any old alarm with the same name. |
| 273 AlarmIterator old_alarm = GetAlarmIterator(extension_id, | 273 AlarmIterator old_alarm = |
| 274 alarm.js_alarm->name); | 274 GetAlarmIterator(extension_id, alarm.js_alarm->name); |
| 275 if (old_alarm.first != alarms_.end()) | 275 if (old_alarm.first != alarms_.end()) |
| 276 RemoveAlarmIterator(old_alarm); | 276 RemoveAlarmIterator(old_alarm); |
| 277 | 277 |
| 278 alarms_[extension_id].push_back(alarm); | 278 alarms_[extension_id].push_back(alarm); |
| 279 base::Time alarm_time = | 279 base::Time alarm_time = |
| 280 base::Time::FromJsTime(alarm.js_alarm->scheduled_time); | 280 base::Time::FromJsTime(alarm.js_alarm->scheduled_time); |
| 281 if (next_poll_time_.is_null() || alarm_time < next_poll_time_) | 281 if (next_poll_time_.is_null() || alarm_time < next_poll_time_) |
| 282 SetNextPollTime(alarm_time); | 282 SetNextPollTime(alarm_time); |
| 283 } | 283 } |
| 284 | 284 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 310 extension_ready_queue.front().Run(extension_id); | 310 extension_ready_queue.front().Run(extension_id); |
| 311 extension_ready_queue.pop(); | 311 extension_ready_queue.pop(); |
| 312 } | 312 } |
| 313 ready_actions_.erase(extension_id); | 313 ready_actions_.erase(extension_id); |
| 314 } | 314 } |
| 315 | 315 |
| 316 void AlarmManager::SetNextPollTime(const base::Time& time) { | 316 void AlarmManager::SetNextPollTime(const base::Time& time) { |
| 317 next_poll_time_ = time; | 317 next_poll_time_ = time; |
| 318 timer_.Start(FROM_HERE, | 318 timer_.Start(FROM_HERE, |
| 319 std::max(base::TimeDelta::FromSeconds(0), time - clock_->Now()), | 319 std::max(base::TimeDelta::FromSeconds(0), time - clock_->Now()), |
| 320 this, | 320 this, &AlarmManager::PollAlarms); |
| 321 &AlarmManager::PollAlarms); | |
| 322 } | 321 } |
| 323 | 322 |
| 324 void AlarmManager::ScheduleNextPoll() { | 323 void AlarmManager::ScheduleNextPoll() { |
| 325 // If there are no alarms, stop the timer. | 324 // If there are no alarms, stop the timer. |
| 326 if (alarms_.empty()) { | 325 if (alarms_.empty()) { |
| 327 timer_.Stop(); | 326 timer_.Stop(); |
| 328 next_poll_time_ = base::Time(); | 327 next_poll_time_ = base::Time(); |
| 329 return; | 328 return; |
| 330 } | 329 } |
| 331 | 330 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 if (base::Time::FromJsTime(cur_alarm->js_alarm->scheduled_time) <= | 383 if (base::Time::FromJsTime(cur_alarm->js_alarm->scheduled_time) <= |
| 385 last_poll_time_) { | 384 last_poll_time_) { |
| 386 OnAlarm(make_pair(cur_extension, cur_alarm)); | 385 OnAlarm(make_pair(cur_extension, cur_alarm)); |
| 387 } | 386 } |
| 388 } | 387 } |
| 389 } | 388 } |
| 390 | 389 |
| 391 ScheduleNextPoll(); | 390 ScheduleNextPoll(); |
| 392 } | 391 } |
| 393 | 392 |
| 394 static void RemoveAllOnUninstallCallback() {} | 393 static void RemoveAllOnUninstallCallback() { |
| 394 } |
| 395 | 395 |
| 396 void AlarmManager::RunWhenReady( | 396 void AlarmManager::RunWhenReady(const std::string& extension_id, |
| 397 const std::string& extension_id, const ReadyAction& action) { | 397 const ReadyAction& action) { |
| 398 ReadyMap::iterator it = ready_actions_.find(extension_id); | 398 ReadyMap::iterator it = ready_actions_.find(extension_id); |
| 399 | 399 |
| 400 if (it == ready_actions_.end()) | 400 if (it == ready_actions_.end()) |
| 401 action.Run(extension_id); | 401 action.Run(extension_id); |
| 402 else | 402 else |
| 403 it->second.push(action); | 403 it->second.push(action); |
| 404 } | 404 } |
| 405 | 405 |
| 406 void AlarmManager::OnExtensionLoaded(content::BrowserContext* browser_context, | 406 void AlarmManager::OnExtensionLoaded(content::BrowserContext* browser_context, |
| 407 const Extension* extension) { | 407 const Extension* extension) { |
| 408 StateStore* storage = ExtensionSystem::Get(browser_context_)->state_store(); | 408 StateStore* storage = ExtensionSystem::Get(browser_context_)->state_store(); |
| 409 if (storage) { | 409 if (storage) { |
| 410 ready_actions_.insert(ReadyMap::value_type(extension->id(), ReadyQueue())); | 410 ready_actions_.insert(ReadyMap::value_type(extension->id(), ReadyQueue())); |
| 411 storage->GetExtensionValue( | 411 storage->GetExtensionValue(extension->id(), kRegisteredAlarms, |
| 412 extension->id(), | 412 base::Bind(&AlarmManager::ReadFromStorage, |
| 413 kRegisteredAlarms, | 413 AsWeakPtr(), extension->id())); |
| 414 base::Bind( | |
| 415 &AlarmManager::ReadFromStorage, AsWeakPtr(), extension->id())); | |
| 416 } | 414 } |
| 417 } | 415 } |
| 418 | 416 |
| 419 void AlarmManager::OnExtensionUninstalled( | 417 void AlarmManager::OnExtensionUninstalled( |
| 420 content::BrowserContext* browser_context, | 418 content::BrowserContext* browser_context, |
| 421 const Extension* extension, | 419 const Extension* extension, |
| 422 extensions::UninstallReason reason) { | 420 extensions::UninstallReason reason) { |
| 423 RemoveAllAlarms(extension->id(), base::Bind(RemoveAllOnUninstallCallback)); | 421 RemoveAllAlarms(extension->id(), base::Bind(RemoveAllOnUninstallCallback)); |
| 424 } | 422 } |
| 425 | 423 |
| 426 // AlarmManager::Alarm | 424 // AlarmManager::Alarm |
| 427 | 425 |
| 428 Alarm::Alarm() | 426 Alarm::Alarm() : js_alarm(new alarms::Alarm()) { |
| 429 : js_alarm(new api::alarms::Alarm()) { | |
| 430 } | 427 } |
| 431 | 428 |
| 432 Alarm::Alarm(const std::string& name, | 429 Alarm::Alarm(const std::string& name, |
| 433 const api::alarms::AlarmCreateInfo& create_info, | 430 const alarms::AlarmCreateInfo& create_info, |
| 434 base::TimeDelta min_granularity, | 431 base::TimeDelta min_granularity, |
| 435 base::Time now) | 432 base::Time now) |
| 436 : js_alarm(new api::alarms::Alarm()) { | 433 : js_alarm(new alarms::Alarm()) { |
| 437 js_alarm->name = name; | 434 js_alarm->name = name; |
| 438 minimum_granularity = min_granularity; | 435 minimum_granularity = min_granularity; |
| 439 | 436 |
| 440 if (create_info.when.get()) { | 437 if (create_info.when.get()) { |
| 441 // Absolute scheduling. | 438 // Absolute scheduling. |
| 442 js_alarm->scheduled_time = *create_info.when; | 439 js_alarm->scheduled_time = *create_info.when; |
| 443 granularity = base::Time::FromJsTime(js_alarm->scheduled_time) - now; | 440 granularity = base::Time::FromJsTime(js_alarm->scheduled_time) - now; |
| 444 } else { | 441 } else { |
| 445 // Relative scheduling. | 442 // Relative scheduling. |
| 446 double* delay_in_minutes = create_info.delay_in_minutes.get(); | 443 double* delay_in_minutes = create_info.delay_in_minutes.get(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 461 if (create_info.period_in_minutes.get()) { | 458 if (create_info.period_in_minutes.get()) { |
| 462 js_alarm->period_in_minutes.reset( | 459 js_alarm->period_in_minutes.reset( |
| 463 new double(*create_info.period_in_minutes)); | 460 new double(*create_info.period_in_minutes)); |
| 464 } | 461 } |
| 465 } | 462 } |
| 466 | 463 |
| 467 Alarm::~Alarm() { | 464 Alarm::~Alarm() { |
| 468 } | 465 } |
| 469 | 466 |
| 470 } // namespace extensions | 467 } // namespace extensions |
| OLD | NEW |