summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/alarms/alarm_manager.h
blob: 6930998c2075e505d2cef7adedae5715811e8755 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__
#define CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__
#pragma once

#include <string>
#include <map>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "base/timer.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/common/extensions/api/alarms.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"

class Profile;

namespace extensions {

class ExtensionAlarmsSchedulingTest;

// Manages the currently pending alarms for every extension in a profile.
// There is one manager per virtual Profile.
class AlarmManager
    : public content::NotificationObserver,
      public base::SupportsWeakPtr<AlarmManager> {
 public:
  typedef extensions::api::alarms::Alarm Alarm;
  typedef std::vector<linked_ptr<Alarm> > AlarmList;

  class Delegate {
   public:
    virtual ~Delegate() {}
    // Called when an alarm fires.
    virtual void OnAlarm(const std::string& extension_id,
                         const Alarm& alarm) = 0;
  };

  explicit AlarmManager(Profile* profile);
  virtual ~AlarmManager();

  // Override the default delegate. Callee assumes onwership. Used for testing.
  void set_delegate(Delegate* delegate) { delegate_.reset(delegate); }

  // Adds |alarm| for the given extension, and starts the timer.
  void AddAlarm(const std::string& extension_id,
                const linked_ptr<Alarm>& alarm);

  // Returns the alarm with the given name, or NULL if none exists.
  const Alarm* GetAlarm(const std::string& extension_id,
                        const std::string& name);

  // Returns the list of pending alarms for the given extension, or NULL
  // if none exist.
  const AlarmList* GetAllAlarms(const std::string& extension_id);

  // Cancels and removes the alarm with the given name.
  bool RemoveAlarm(const std::string& extension_id,
                   const std::string& name);

  // Cancels and removes all alarms for the given extension.
  void RemoveAllAlarms(const std::string& extension_id);

 private:
  FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, CreateRepeating);
  FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, Clear);
  friend class ExtensionAlarmsSchedulingTest;
  FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling);
  FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning);

  typedef std::string ExtensionId;
  typedef std::map<ExtensionId, AlarmList> AlarmMap;

  // Iterator used to identify a particular alarm within the Map/List pair.
  // "Not found" is represented by <alarms_.end(), invalid_iterator>.
  typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator;

  struct AlarmRuntimeInfo {
    std::string extension_id;
    base::Time time;
  };
  typedef std::map<const Alarm*, AlarmRuntimeInfo> AlarmRuntimeInfoMap;

  // Helper to return the iterators within the AlarmMap and AlarmList for the
  // matching alarm, or an iterator to the end of the AlarmMap if none were
  // found.
  AlarmIterator GetAlarmIterator(const std::string& extension_id,
                                 const std::string& name);

  // Helper to cancel and remove the alarm at the given iterator. The iterator
  // must be valid.
  void RemoveAlarmIterator(const AlarmIterator& iter);

  // Callback for when an alarm fires.
  void OnAlarm(const std::string& extension_id, const std::string& name);

  // Internal helper to add an alarm and start the timer with the given delay.
  void AddAlarmImpl(const std::string& extension_id,
                    const linked_ptr<Alarm>& alarm,
                    base::TimeDelta time_delay);

  // Syncs our alarm data for the given extension to/from the state storage.
  void WriteToStorage(const std::string& extension_id);
  void ReadFromStorage(const std::string& extension_id,
                       scoped_ptr<base::Value> value);

  // Schedules the next poll of alarms for when the next soonest alarm runs,
  // but do not more often than min_period.
  void ScheduleNextPoll(base::TimeDelta min_period);

  // Polls the alarms, running any that have elapsed. After running them and
  // rescheduling repeating alarms, schedule the next poll.
  void PollAlarms();

  // NotificationObserver:
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  Profile* profile_;
  content::NotificationRegistrar registrar_;
  scoped_ptr<Delegate> delegate_;

  // The timer for this alarm manager.
  base::OneShotTimer<AlarmManager> timer_;

  // A map of our pending alarms, per extension.
  AlarmMap alarms_;

  // A map of the next scheduled times associated with each alarm.
  AlarmRuntimeInfoMap scheduled_times_;

  // The previous and next time that alarms were and will be run.
  base::Time last_poll_time_;
  base::Time next_poll_time_;
};

} //  namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__