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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
// 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.
#include "webkit/quota/usage_tracker.h"
#include <algorithm>
#include <deque>
#include <set>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/message_loop_proxy.h"
#include "base/stl_util.h"
#include "net/base/net_util.h"
namespace quota {
namespace {
bool SortByHost(const GURL& lhs, const GURL& rhs) {
return net::GetHostOrSpecFromURL(lhs) > net::GetHostOrSpecFromURL(rhs);
}
}
// A task class for getting the total amount of data used for a collection of
// origins. This class is self-destructed.
class ClientUsageTracker::GatherUsageTaskBase : public QuotaTask {
public:
GatherUsageTaskBase(
UsageTracker* tracker,
QuotaClient* client)
: QuotaTask(tracker),
client_(client),
tracker_(tracker),
weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
DCHECK(tracker_);
DCHECK(client_);
client_tracker_ = base::AsWeakPtr(
tracker_->GetClientTracker(client_->id()));
DCHECK(client_tracker_.get());
}
virtual ~GatherUsageTaskBase() {}
// Get total usage for the given |origins|.
void GetUsageForOrigins(const std::set<GURL>& origins, StorageType type) {
DCHECK(original_task_runner()->BelongsToCurrentThread());
if (!client_tracker()) {
DeleteSoon();
return;
}
// We do not get usage for origins for which we have valid usage cache.
std::vector<GURL> origins_to_gather;
std::set<GURL> cached_origins;
client_tracker()->GetCachedOrigins(&cached_origins);
std::set<GURL> already_added;
for (std::set<GURL>::const_iterator iter = origins.begin();
iter != origins.end(); ++iter) {
if (cached_origins.find(*iter) == cached_origins.end() &&
already_added.insert(*iter).second) {
origins_to_gather.push_back(*iter);
}
}
if (origins_to_gather.empty()) {
CallCompleted();
DeleteSoon();
return;
}
// Sort them so we can detect when we've gathered all info for a particular
// host in DidGetUsage.
std::sort(origins_to_gather.begin(), origins_to_gather.end(), SortByHost);
// First, fully populate the pending queue because GetOriginUsage may call
// the completion callback immediately.
for (std::vector<GURL>::const_iterator iter = origins_to_gather.begin();
iter != origins_to_gather.end(); iter++)
pending_origins_.push_back(*iter);
for (std::vector<GURL>::const_iterator iter = origins_to_gather.begin();
iter != origins_to_gather.end(); iter++)
client_->GetOriginUsage(
*iter,
tracker_->type(),
base::Bind(&GatherUsageTaskBase::DidGetUsage,
weak_factory_.GetWeakPtr()));
}
protected:
virtual void Aborted() OVERRIDE {
DeleteSoon();
}
UsageTracker* tracker() const { return tracker_; }
ClientUsageTracker* client_tracker() const { return client_tracker_.get(); }
private:
void DidGetUsage(int64 usage) {
if (!client_tracker()) {
DeleteSoon();
return;
}
DCHECK(original_task_runner()->BelongsToCurrentThread());
DCHECK(!pending_origins_.empty());
// Defend against confusing inputs from QuotaClients.
DCHECK_GE(usage, 0);
if (usage < 0)
usage = 0;
// This code assumes DidGetUsage callbacks are called in the same
// order as we dispatched GetOriginUsage calls.
const GURL& origin = pending_origins_.front();
std::string host = net::GetHostOrSpecFromURL(origin);
client_tracker_->AddCachedOrigin(origin, usage);
pending_origins_.pop_front();
if (pending_origins_.empty() ||
host != net::GetHostOrSpecFromURL(pending_origins_.front())) {
client_tracker_->AddCachedHost(host);
}
if (pending_origins_.empty()) {
// We're done.
CallCompleted();
DeleteSoon();
}
}
QuotaClient* client_;
UsageTracker* tracker_;
base::WeakPtr<ClientUsageTracker> client_tracker_;
std::deque<GURL> pending_origins_;
std::map<GURL, int64> origin_usage_map_;
base::WeakPtrFactory<GatherUsageTaskBase> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(GatherUsageTaskBase);
};
// A task class for getting the total amount of data used for a given storage
// type. This class is self-destructed.
class ClientUsageTracker::GatherGlobalUsageTask
: public GatherUsageTaskBase {
public:
GatherGlobalUsageTask(
UsageTracker* tracker,
QuotaClient* client)
: GatherUsageTaskBase(tracker, client),
client_(client),
weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
DCHECK(tracker);
DCHECK(client);
}
virtual ~GatherGlobalUsageTask() {}
protected:
virtual void Run() OVERRIDE {
client_->GetOriginsForType(tracker()->type(),
base::Bind(&GatherUsageTaskBase::GetUsageForOrigins,
weak_factory_.GetWeakPtr()));
}
virtual void Completed() OVERRIDE {
client_tracker()->GatherGlobalUsageComplete();
}
private:
QuotaClient* client_;
base::WeakPtrFactory<GatherUsageTaskBase> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(GatherGlobalUsageTask);
};
// A task class for getting the total amount of data used for a given host.
// This class is self-destructed.
class ClientUsageTracker::GatherHostUsageTask
: public GatherUsageTaskBase {
public:
GatherHostUsageTask(
UsageTracker* tracker,
QuotaClient* client,
const std::string& host)
: GatherUsageTaskBase(tracker, client),
client_(client),
host_(host),
weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
DCHECK(client_);
}
virtual ~GatherHostUsageTask() {}
protected:
virtual void Run() OVERRIDE {
client_->GetOriginsForHost(tracker()->type(), host_,
base::Bind(&GatherUsageTaskBase::GetUsageForOrigins,
weak_factory_.GetWeakPtr()));
}
virtual void Completed() OVERRIDE {
client_tracker()->GatherHostUsageComplete(host_);
}
private:
QuotaClient* client_;
std::string host_;
base::WeakPtrFactory<GatherUsageTaskBase> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(GatherHostUsageTask);
};
// UsageTracker ----------------------------------------------------------
UsageTracker::UsageTracker(const QuotaClientList& clients, StorageType type,
SpecialStoragePolicy* special_storage_policy)
: type_(type),
weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
for (QuotaClientList::const_iterator iter = clients.begin();
iter != clients.end();
++iter) {
client_tracker_map_.insert(std::make_pair(
(*iter)->id(),
new ClientUsageTracker(this, *iter, type, special_storage_policy)));
}
}
UsageTracker::~UsageTracker() {
STLDeleteValues(&client_tracker_map_);
}
ClientUsageTracker* UsageTracker::GetClientTracker(QuotaClient::ID client_id) {
ClientTrackerMap::iterator found = client_tracker_map_.find(client_id);
if (found != client_tracker_map_.end())
return found->second;
return NULL;
}
void UsageTracker::GetGlobalUsage(const GlobalUsageCallback& callback) {
if (client_tracker_map_.size() == 0) {
// No clients registered.
callback.Run(type_, 0, 0);
return;
}
if (global_usage_callbacks_.Add(callback)) {
// This is the first call. Asks each ClientUsageTracker to collect
// usage information.
global_usage_.pending_clients = client_tracker_map_.size();
global_usage_.usage = 0;
global_usage_.unlimited_usage = 0;
for (ClientTrackerMap::iterator iter = client_tracker_map_.begin();
iter != client_tracker_map_.end();
++iter) {
iter->second->GetGlobalUsage(
base::Bind(&UsageTracker::DidGetClientGlobalUsage,
weak_factory_.GetWeakPtr()));
}
}
}
void UsageTracker::GetHostUsage(
const std::string& host, const UsageCallback& callback) {
if (client_tracker_map_.size() == 0) {
// No clients registered.
callback.Run(0);
return;
}
if (host_usage_callbacks_.Add(host, callback)) {
// This is the first call for the given host.
DCHECK(outstanding_host_usage_.find(host) == outstanding_host_usage_.end());
outstanding_host_usage_[host].pending_clients = client_tracker_map_.size();
for (ClientTrackerMap::iterator iter = client_tracker_map_.begin();
iter != client_tracker_map_.end();
++iter) {
iter->second->GetHostUsage(host,
base::Bind(&UsageTracker::DidGetClientHostUsage,
weak_factory_.GetWeakPtr(), host, type_));
}
}
}
void UsageTracker::UpdateUsageCache(
QuotaClient::ID client_id, const GURL& origin, int64 delta) {
ClientUsageTracker* client_tracker = GetClientTracker(client_id);
DCHECK(client_tracker);
client_tracker->UpdateUsageCache(origin, delta);
}
void UsageTracker::GetCachedHostsUsage(
std::map<std::string, int64>* host_usage) const {
DCHECK(host_usage);
host_usage->clear();
for (ClientTrackerMap::const_iterator iter = client_tracker_map_.begin();
iter != client_tracker_map_.end(); ++iter) {
iter->second->GetCachedHostsUsage(host_usage);
}
}
void UsageTracker::GetCachedOrigins(std::set<GURL>* origins) const {
DCHECK(origins);
origins->clear();
for (ClientTrackerMap::const_iterator iter = client_tracker_map_.begin();
iter != client_tracker_map_.end(); ++iter) {
iter->second->GetCachedOrigins(origins);
}
}
void UsageTracker::DidGetClientGlobalUsage(StorageType type,
int64 usage,
int64 unlimited_usage) {
DCHECK_EQ(type, type_);
global_usage_.usage += usage;
global_usage_.unlimited_usage += unlimited_usage;
if (--global_usage_.pending_clients == 0) {
// Defend against confusing inputs from clients.
if (global_usage_.usage < 0)
global_usage_.usage = 0;
// TODO(michaeln): The unlimited number is not trustworthy, it
// can get out of whack when apps are installed or uninstalled.
if (global_usage_.unlimited_usage > global_usage_.usage)
global_usage_.unlimited_usage = global_usage_.usage;
else if (global_usage_.unlimited_usage < 0)
global_usage_.unlimited_usage = 0;
// All the clients have returned their usage data. Dispatches the
// pending callbacks.
global_usage_callbacks_.Run(type, global_usage_.usage,
global_usage_.unlimited_usage);
}
}
void UsageTracker::DidGetClientHostUsage(const std::string& host,
StorageType type,
int64 usage) {
DCHECK_EQ(type, type_);
TrackingInfo& info = outstanding_host_usage_[host];
info.usage += usage;
if (--info.pending_clients == 0) {
// Defend against confusing inputs from clients.
if (info.usage < 0)
info.usage = 0;
// All the clients have returned their usage data. Dispatches the
// pending callbacks.
host_usage_callbacks_.Run(host, info.usage);
outstanding_host_usage_.erase(host);
}
}
// ClientUsageTracker ----------------------------------------------------
ClientUsageTracker::ClientUsageTracker(
UsageTracker* tracker, QuotaClient* client, StorageType type,
SpecialStoragePolicy* special_storage_policy)
: tracker_(tracker),
client_(client),
type_(type),
global_usage_(0),
global_unlimited_usage_(0),
global_usage_retrieved_(false),
global_unlimited_usage_is_valid_(true),
global_usage_task_(NULL),
special_storage_policy_(special_storage_policy) {
DCHECK(tracker_);
DCHECK(client_);
if (special_storage_policy_)
special_storage_policy_->AddObserver(this);
}
ClientUsageTracker::~ClientUsageTracker() {
if (special_storage_policy_)
special_storage_policy_->RemoveObserver(this);
}
void ClientUsageTracker::GetGlobalUsage(const GlobalUsageCallback& callback) {
if (global_usage_retrieved_) {
callback.Run(type_, global_usage_, GetCachedGlobalUnlimitedUsage());
return;
}
DCHECK(!global_usage_callback_.HasCallbacks());
global_usage_callback_.Add(callback);
global_usage_task_ = new GatherGlobalUsageTask(tracker_, client_);
global_usage_task_->Start();
}
void ClientUsageTracker::GetHostUsage(
const std::string& host, const UsageCallback& callback) {
HostSet::const_iterator found = cached_hosts_.find(host);
if (found != cached_hosts_.end()) {
// TODO(kinuko): Drop host_usage_map_ cache periodically.
callback.Run(GetCachedHostUsage(host));
return;
}
if (!host_usage_callbacks_.Add(host, callback) || global_usage_task_)
return;
GatherHostUsageTask* task = new GatherHostUsageTask(tracker_, client_, host);
host_usage_tasks_[host] = task;
task->Start();
}
void ClientUsageTracker::UpdateUsageCache(
const GURL& origin, int64 delta) {
std::string host = net::GetHostOrSpecFromURL(origin);
if (cached_hosts_.find(host) != cached_hosts_.end()) {
cached_usage_[host][origin] += delta;
global_usage_ += delta;
if (global_unlimited_usage_is_valid_ && IsStorageUnlimited(origin))
global_unlimited_usage_ += delta;
DCHECK_GE(cached_usage_[host][origin], 0);
DCHECK_GE(global_usage_, 0);
return;
}
// We don't know about this host yet, so populate our cache for it.
GetHostUsage(host,
base::Bind(&ClientUsageTracker::NoopHostUsageCallback,
base::Unretained(this)));
}
void ClientUsageTracker::GetCachedHostsUsage(
std::map<std::string, int64>* host_usage) const {
DCHECK(host_usage);
for (HostUsageMap::const_iterator host_iter = cached_usage_.begin();
host_iter != cached_usage_.end(); host_iter++) {
host_usage->operator[](host_iter->first) +=
GetCachedHostUsage(host_iter->first);
}
}
void ClientUsageTracker::GetCachedOrigins(std::set<GURL>* origins) const {
DCHECK(origins);
for (HostUsageMap::const_iterator host_iter = cached_usage_.begin();
host_iter != cached_usage_.end(); host_iter++) {
const UsageMap& origin_map = host_iter->second;
for (UsageMap::const_iterator origin_iter = origin_map.begin();
origin_iter != origin_map.end(); origin_iter++) {
origins->insert(origin_iter->first);
}
}
}
void ClientUsageTracker::AddCachedOrigin(
const GURL& origin, int64 usage) {
std::string host = net::GetHostOrSpecFromURL(origin);
UsageMap::iterator iter = cached_usage_[host].
insert(UsageMap::value_type(origin, 0)).first;
int64 old_usage = iter->second;
iter->second = usage;
int64 delta = usage - old_usage;
if (delta) {
global_usage_ += delta;
if (global_unlimited_usage_is_valid_ && IsStorageUnlimited(origin))
global_unlimited_usage_ += delta;
}
DCHECK_GE(iter->second, 0);
DCHECK_GE(global_usage_, 0);
}
void ClientUsageTracker::AddCachedHost(const std::string& host) {
cached_hosts_.insert(host);
}
void ClientUsageTracker::GatherGlobalUsageComplete() {
DCHECK(global_usage_task_ != NULL);
global_usage_task_ = NULL;
// TODO(kinuko): Record when it has retrieved the global usage.
global_usage_retrieved_ = true;
DCHECK(global_usage_callback_.HasCallbacks());
global_usage_callback_.Run(type_, global_usage_,
GetCachedGlobalUnlimitedUsage());
for (HostUsageCallbackMap::iterator iter = host_usage_callbacks_.Begin();
iter != host_usage_callbacks_.End(); ++iter) {
iter->second.Run(GetCachedHostUsage(iter->first));
}
host_usage_callbacks_.Clear();
}
void ClientUsageTracker::GatherHostUsageComplete(const std::string& host) {
DCHECK(host_usage_tasks_.find(host) != host_usage_tasks_.end());
host_usage_tasks_.erase(host);
host_usage_callbacks_.Run(host, GetCachedHostUsage(host));
}
int64 ClientUsageTracker::GetCachedHostUsage(const std::string& host) const {
HostUsageMap::const_iterator found = cached_usage_.find(host);
if (found == cached_usage_.end())
return 0;
int64 usage = 0;
const UsageMap& map = found->second;
for (UsageMap::const_iterator iter = map.begin();
iter != map.end(); ++iter) {
usage += iter->second;
}
return usage;
}
int64 ClientUsageTracker::GetCachedGlobalUnlimitedUsage() {
if (!global_unlimited_usage_is_valid_) {
global_unlimited_usage_ = 0;
for (HostUsageMap::const_iterator host_iter = cached_usage_.begin();
host_iter != cached_usage_.end(); host_iter++) {
const UsageMap& origin_map = host_iter->second;
for (UsageMap::const_iterator origin_iter = origin_map.begin();
origin_iter != origin_map.end(); origin_iter++) {
if (IsStorageUnlimited(origin_iter->first))
global_unlimited_usage_ += origin_iter->second;
}
}
global_unlimited_usage_is_valid_ = true;
}
return global_unlimited_usage_;
}
void ClientUsageTracker::OnSpecialStoragePolicyChanged() {
DCHECK(CalledOnValidThread());
global_unlimited_usage_is_valid_ = false;
}
void ClientUsageTracker::NoopHostUsageCallback(int64 usage) {}
bool ClientUsageTracker::IsStorageUnlimited(const GURL& origin) const {
if (type_ == kStorageTypeSyncable)
return false;
return special_storage_policy_.get() &&
special_storage_policy_->IsStorageUnlimited(origin);
}
} // namespace quota
|