Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Side by Side Diff: chrome/browser/extensions/api/alarms/alarm_manager.cc

Issue 12314056: Make chrome/browser/extensions use base::Clock instead of MockTimeProvider (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add include Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "base/time/clock.h"
11 #include "base/value_conversions.h" 12 #include "base/value_conversions.h"
12 #include "base/values.h" 13 #include "base/values.h"
13 #include "chrome/browser/extensions/event_router.h" 14 #include "chrome/browser/extensions/event_router.h"
14 #include "chrome/browser/extensions/extension_service.h" 15 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_system.h" 16 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/state_store.h" 17 #include "chrome/browser/extensions/state_store.h"
17 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/chrome_notification_types.h" 19 #include "chrome/common/chrome_notification_types.h"
19 #include "content/public/browser/notification_service.h" 20 #include "content/public/browser/notification_service.h"
20 21
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 list->Append(alarm.release()); 82 list->Append(alarm.release());
82 } 83 }
83 return list.Pass(); 84 return list.Pass();
84 } 85 }
85 86
86 87
87 } // namespace 88 } // namespace
88 89
89 // AlarmManager 90 // AlarmManager
90 91
91 AlarmManager::AlarmManager(Profile* profile, TimeProvider now) 92 AlarmManager::AlarmManager(Profile* profile, base::Clock* clock)
92 : profile_(profile), 93 : profile_(profile),
93 now_(now), 94 clock_(clock),
94 delegate_(new DefaultAlarmDelegate(profile)), 95 delegate_(new DefaultAlarmDelegate(profile)) {
95 last_poll_time_(base::Time()) {
96 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, 96 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
97 content::Source<Profile>(profile_)); 97 content::Source<Profile>(profile_));
98 98
99 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); 99 StateStore* storage = ExtensionSystem::Get(profile_)->state_store();
100 if (storage) 100 if (storage)
101 storage->RegisterKey(kRegisteredAlarms); 101 storage->RegisterKey(kRegisteredAlarms);
102 } 102 }
103 103
104 AlarmManager::~AlarmManager() { 104 AlarmManager::~AlarmManager() {
105 } 105 }
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 // If the next alarm is more than min_granularity in the future, wait for it. 269 // If the next alarm is more than min_granularity in the future, wait for it.
270 // Otherwise, only poll as often as min_granularity. 270 // Otherwise, only poll as often as min_granularity.
271 // As a special case, if we've never checked for an alarm before 271 // As a special case, if we've never checked for an alarm before
272 // (e.g. during startup), let alarms fire asap. 272 // (e.g. during startup), let alarms fire asap.
273 if (last_poll_time_.is_null() || next_poll < soonest_alarm_time) 273 if (last_poll_time_.is_null() || next_poll < soonest_alarm_time)
274 next_poll = soonest_alarm_time; 274 next_poll = soonest_alarm_time;
275 275
276 // Schedule the poll. 276 // Schedule the poll.
277 next_poll_time_ = next_poll; 277 next_poll_time_ = next_poll;
278 base::TimeDelta delay = std::max(base::TimeDelta::FromSeconds(0), 278 base::TimeDelta delay = std::max(base::TimeDelta::FromSeconds(0),
279 next_poll - now_()); 279 next_poll - clock_->Now());
280 timer_.Start(FROM_HERE, 280 timer_.Start(FROM_HERE,
281 delay, 281 delay,
282 this, 282 this,
283 &AlarmManager::PollAlarms); 283 &AlarmManager::PollAlarms);
284 } 284 }
285 285
286 void AlarmManager::PollAlarms() { 286 void AlarmManager::PollAlarms() {
287 last_poll_time_ = now_(); 287 last_poll_time_ = clock_->Now();
288 288
289 // Run any alarms scheduled in the past. OnAlarm uses vector::erase to remove 289 // Run any alarms scheduled in the past. OnAlarm uses vector::erase to remove
290 // elements from the AlarmList, and map::erase to remove AlarmLists from the 290 // elements from the AlarmList, and map::erase to remove AlarmLists from the
291 // AlarmMap. 291 // AlarmMap.
292 for (AlarmMap::iterator m_it = alarms_.begin(), m_end = alarms_.end(); 292 for (AlarmMap::iterator m_it = alarms_.begin(), m_end = alarms_.end();
293 m_it != m_end;) { 293 m_it != m_end;) {
294 AlarmMap::iterator cur_extension = m_it++; 294 AlarmMap::iterator cur_extension = m_it++;
295 295
296 // Iterate (a) backwards so that removing elements doesn't affect 296 // Iterate (a) backwards so that removing elements doesn't affect
297 // upcoming iterations, and (b) with indices so that if the last 297 // upcoming iterations, and (b) with indices so that if the last
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 333
334 // AlarmManager::Alarm 334 // AlarmManager::Alarm
335 335
336 Alarm::Alarm() 336 Alarm::Alarm()
337 : js_alarm(new api::alarms::Alarm()) { 337 : js_alarm(new api::alarms::Alarm()) {
338 } 338 }
339 339
340 Alarm::Alarm(const std::string& name, 340 Alarm::Alarm(const std::string& name,
341 const api::alarms::AlarmCreateInfo& create_info, 341 const api::alarms::AlarmCreateInfo& create_info,
342 base::TimeDelta min_granularity, 342 base::TimeDelta min_granularity,
343 TimeProvider now) 343 base::Time now)
344 : js_alarm(new api::alarms::Alarm()) { 344 : js_alarm(new api::alarms::Alarm()) {
345 js_alarm->name = name; 345 js_alarm->name = name;
346 346
347 if (create_info.when.get()) { 347 if (create_info.when.get()) {
348 // Absolute scheduling. 348 // Absolute scheduling.
349 js_alarm->scheduled_time = *create_info.when; 349 js_alarm->scheduled_time = *create_info.when;
350 granularity = base::Time::FromJsTime(js_alarm->scheduled_time) - now(); 350 granularity = base::Time::FromJsTime(js_alarm->scheduled_time) - now;
351 } else { 351 } else {
352 // Relative scheduling. 352 // Relative scheduling.
353 double* delay_in_minutes = create_info.delay_in_minutes.get(); 353 double* delay_in_minutes = create_info.delay_in_minutes.get();
354 if (delay_in_minutes == NULL) 354 if (delay_in_minutes == NULL)
355 delay_in_minutes = create_info.period_in_minutes.get(); 355 delay_in_minutes = create_info.period_in_minutes.get();
356 CHECK(delay_in_minutes != NULL) 356 CHECK(delay_in_minutes != NULL)
357 << "ValidateAlarmCreateInfo in alarms_api.cc should have " 357 << "ValidateAlarmCreateInfo in alarms_api.cc should have "
358 << "prevented this call."; 358 << "prevented this call.";
359 base::TimeDelta delay = TimeDeltaFromDelay(*delay_in_minutes); 359 base::TimeDelta delay = TimeDeltaFromDelay(*delay_in_minutes);
360 js_alarm->scheduled_time = (now() + delay).ToJsTime(); 360 js_alarm->scheduled_time = (now + delay).ToJsTime();
361 granularity = delay; 361 granularity = delay;
362 } 362 }
363 363
364 if (granularity < min_granularity) 364 if (granularity < min_granularity)
365 granularity = min_granularity; 365 granularity = min_granularity;
366 366
367 // Check for repetition. 367 // Check for repetition.
368 if (create_info.period_in_minutes.get()) { 368 if (create_info.period_in_minutes.get()) {
369 js_alarm->period_in_minutes.reset( 369 js_alarm->period_in_minutes.reset(
370 new double(*create_info.period_in_minutes)); 370 new double(*create_info.period_in_minutes));
371 } 371 }
372 } 372 }
373 373
374 Alarm::~Alarm() { 374 Alarm::~Alarm() {
375 } 375 }
376 376
377 } // namespace extensions 377 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/alarms/alarm_manager.h ('k') | chrome/browser/extensions/api/alarms/alarms_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698