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
|
// 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 CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
#define CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
#include <map>
#include <set>
#include <utility>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "storage/browser/quota/quota_client.h"
#include "storage/browser/quota/quota_manager.h"
#include "storage/browser/quota/quota_task.h"
#include "storage/common/quota/quota_types.h"
#include "url/gurl.h"
using storage::GetOriginsCallback;
using storage::QuotaClient;
using storage::QuotaManager;
using storage::QuotaStatusCode;
using storage::SpecialStoragePolicy;
using storage::StatusCallback;
using storage::StorageType;
namespace content {
// Mocks the pieces of QuotaManager's interface.
//
// For usage/quota tracking test:
// Usage and quota information can be updated by following private helper
// methods: SetQuota() and UpdateUsage().
//
// For time-based deletion test:
// Origins can be added to the mock by calling AddOrigin, and that list of
// origins is then searched through in GetOriginsModifiedSince.
// Neither GetOriginsModifiedSince nor DeleteOriginData touches the actual
// origin data stored in the profile.
class MockQuotaManager : public QuotaManager {
public:
MockQuotaManager(
bool is_incognito,
const base::FilePath& profile_path,
const scoped_refptr<base::SingleThreadTaskRunner>& io_thread,
const scoped_refptr<base::SequencedTaskRunner>& db_thread,
const scoped_refptr<SpecialStoragePolicy>& special_storage_policy);
// Overrides QuotaManager's implementation. The internal usage data is
// updated when MockQuotaManagerProxy::NotifyStorageModified() is
// called. The internal quota value can be updated by calling
// a helper method MockQuotaManagerProxy::SetQuota().
void GetUsageAndQuota(const GURL& origin,
storage::StorageType type,
const GetUsageAndQuotaCallback& callback) override;
// Overrides QuotaManager's implementation with a canned implementation that
// allows clients to set up the origin database that should be queried. This
// method will only search through the origins added explicitly via AddOrigin.
void GetOriginsModifiedSince(StorageType type,
base::Time modified_since,
const GetOriginsCallback& callback) override;
// Removes an origin from the canned list of origins, but doesn't touch
// anything on disk. The caller must provide |quota_client_mask| which
// specifies the types of QuotaClients which should be removed from this
// origin as a bitmask built from QuotaClient::IDs. Setting the mask to
// QuotaClient::kAllClientsMask will remove all clients from the origin,
// regardless of type.
void DeleteOriginData(const GURL& origin,
StorageType type,
int quota_client_mask,
const StatusCallback& callback) override;
// Helper method for updating internal quota info.
void SetQuota(const GURL& origin, StorageType type, int64 quota);
// Helper methods for timed-deletion testing:
// Adds an origin to the canned list that will be searched through via
// GetOriginsModifiedSince. The caller must provide |quota_client_mask|
// which specifies the types of QuotaClients this canned origin contains
// as a bitmask built from QuotaClient::IDs.
bool AddOrigin(const GURL& origin,
StorageType type,
int quota_client_mask,
base::Time modified);
// Helper methods for timed-deletion testing:
// Checks an origin and type against the origins that have been added via
// AddOrigin and removed via DeleteOriginData. If the origin exists in the
// canned list with the proper StorageType and client, returns true.
bool OriginHasData(const GURL& origin,
StorageType type,
QuotaClient::ID quota_client) const;
protected:
~MockQuotaManager() override;
private:
friend class MockQuotaManagerProxy;
// Contains the essential bits of information about an origin that the
// MockQuotaManager needs to understand for time-based deletion:
// the origin itself, the StorageType and its modification time.
struct OriginInfo {
OriginInfo(const GURL& origin,
StorageType type,
int quota_client_mask,
base::Time modified);
~OriginInfo();
GURL origin;
StorageType type;
int quota_client_mask;
base::Time modified;
};
// Contains the essential information for each origin for usage/quota testing.
// (Ideally this should probably merged into the above struct, but for
// regular usage/quota testing we hardly need modified time but only
// want to keep usage and quota information, so this struct exists.
struct StorageInfo {
StorageInfo();
~StorageInfo();
int64 usage;
int64 quota;
};
typedef std::pair<GURL, StorageType> OriginAndType;
typedef std::map<OriginAndType, StorageInfo> UsageAndQuotaMap;
// This must be called via MockQuotaManagerProxy.
void UpdateUsage(const GURL& origin, StorageType type, int64 delta);
void DidGetModifiedSince(const GetOriginsCallback& callback,
std::set<GURL>* origins,
StorageType storage_type);
void DidDeleteOriginData(const StatusCallback& callback,
QuotaStatusCode status);
// The list of stored origins that have been added via AddOrigin.
std::vector<OriginInfo> origins_;
UsageAndQuotaMap usage_and_quota_map_;
base::WeakPtrFactory<MockQuotaManager> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(MockQuotaManager);
};
} // namespace content
#endif // CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
|