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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// Copyright 2014 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 STORAGE_BROWSER_QUOTA_STORAGE_MONITOR_H_
#define STORAGE_BROWSER_QUOTA_STORAGE_MONITOR_H_
#include <map>
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "storage/browser/quota/storage_observer.h"
namespace content {
class StorageMonitorTestBase;
}
namespace storage {
class QuotaManager;
// This class dispatches storage events to observers of a common
// StorageObserver::Filter.
class STORAGE_EXPORT_PRIVATE StorageObserverList {
public:
StorageObserverList();
virtual ~StorageObserverList();
// Adds/removes an observer.
void AddObserver(StorageObserver* observer,
const StorageObserver::MonitorParams& params);
void RemoveObserver(StorageObserver* observer);
// Returns the number of observers.
int ObserverCount() const;
// Forwards a storage change to observers. The event may be dispatched
// immediately to an observer or after a delay, depending on the desired event
// rate of the observer.
void OnStorageChange(const StorageObserver::Event& event);
// Dispatch an event to observers that require it.
void MaybeDispatchEvent(const StorageObserver::Event& event);
// Ensure the specified observer receives the next dispatched event.
void ScheduleUpdateForObserver(StorageObserver* observer);
private:
struct STORAGE_EXPORT_PRIVATE ObserverState {
GURL origin;
base::TimeTicks last_notification_time;
base::TimeDelta rate;
bool requires_update;
ObserverState();
};
typedef std::map<StorageObserver*, ObserverState> StorageObserverStateMap;
void DispatchPendingEvent();
StorageObserverStateMap observers_;
base::OneShotTimer notification_timer_;
StorageObserver::Event pending_event_;
friend class content::StorageMonitorTestBase;
DISALLOW_COPY_AND_ASSIGN(StorageObserverList);
};
// Manages the storage observers of a common host. Caches the usage and quota of
// the host to avoid accumulating for every change.
class STORAGE_EXPORT_PRIVATE HostStorageObservers {
public:
explicit HostStorageObservers(QuotaManager* quota_manager);
virtual ~HostStorageObservers();
bool is_initialized() const { return initialized_; }
// Adds/removes an observer.
void AddObserver(
StorageObserver* observer,
const StorageObserver::MonitorParams& params);
void RemoveObserver(StorageObserver* observer);
bool ContainsObservers() const;
// Handles a usage change.
void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta);
private:
void StartInitialization(const StorageObserver::Filter& filter);
void GotHostUsageAndQuota(const StorageObserver::Filter& filter,
QuotaStatusCode status,
int64 usage,
int64 quota);
void DispatchEvent(const StorageObserver::Filter& filter, bool is_update);
QuotaManager* quota_manager_;
StorageObserverList observers_;
// Flags used during initialization of the cached properties.
bool initialized_;
bool initializing_;
bool event_occurred_before_init_;
int64 usage_deltas_during_init_;
// Cached accumulated usage and quota for the host.
int64 cached_usage_;
int64 cached_quota_;
base::WeakPtrFactory<HostStorageObservers> weak_factory_;
friend class content::StorageMonitorTestBase;
DISALLOW_COPY_AND_ASSIGN(HostStorageObservers);
};
// Manages the observers of a common storage type.
class STORAGE_EXPORT_PRIVATE StorageTypeObservers {
public:
explicit StorageTypeObservers(QuotaManager* quota_manager);
virtual ~StorageTypeObservers();
// Adds and removes an observer.
void AddObserver(StorageObserver* observer,
const StorageObserver::MonitorParams& params);
void RemoveObserver(StorageObserver* observer);
void RemoveObserverForFilter(StorageObserver* observer,
const StorageObserver::Filter& filter);
// Returns the observers of a specific host.
const HostStorageObservers* GetHostObservers(const std::string& host) const;
// Handles a usage change.
void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta);
private:
typedef std::map<std::string, HostStorageObservers*> HostObserversMap;
QuotaManager* quota_manager_;
HostObserversMap host_observers_map_;
DISALLOW_COPY_AND_ASSIGN(StorageTypeObservers);
};
// Storage monitor manages observers and dispatches storage events to them.
class STORAGE_EXPORT_PRIVATE StorageMonitor {
public:
explicit StorageMonitor(QuotaManager* quota_manager);
virtual ~StorageMonitor();
// Adds and removes an observer.
void AddObserver(StorageObserver* observer,
const StorageObserver::MonitorParams& params);
void RemoveObserver(StorageObserver* observer);
void RemoveObserverForFilter(StorageObserver* observer,
const StorageObserver::Filter& filter);
// Returns the observers of a specific storage type.
const StorageTypeObservers* GetStorageTypeObservers(
StorageType storage_type) const;
// Handles a usage change.
void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta);
private:
typedef std::map<StorageType, StorageTypeObservers*> StorageTypeObserversMap;
QuotaManager* quota_manager_;
StorageTypeObserversMap storage_type_observers_map_;
DISALLOW_COPY_AND_ASSIGN(StorageMonitor);
};
} // namespace storage
#endif // STORAGE_BROWSER_QUOTA_STORAGE_MONITOR_H_
|