Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / quota / mock_quota_manager.h
bloba6bba10e985c4a474d0b3c98e978b0b66e8309be
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
6 #define CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
8 #include <map>
9 #include <set>
10 #include <utility>
11 #include <vector>
13 #include "base/memory/scoped_ptr.h"
14 #include "storage/browser/quota/quota_client.h"
15 #include "storage/browser/quota/quota_manager.h"
16 #include "storage/browser/quota/quota_task.h"
17 #include "storage/common/quota/quota_types.h"
18 #include "url/gurl.h"
20 using storage::GetOriginsCallback;
21 using storage::QuotaClient;
22 using storage::QuotaManager;
23 using storage::QuotaStatusCode;
24 using storage::SpecialStoragePolicy;
25 using storage::StatusCallback;
26 using storage::StorageType;
28 namespace content {
30 // Mocks the pieces of QuotaManager's interface.
32 // For usage/quota tracking test:
33 // Usage and quota information can be updated by following private helper
34 // methods: SetQuota() and UpdateUsage().
36 // For time-based deletion test:
37 // Origins can be added to the mock by calling AddOrigin, and that list of
38 // origins is then searched through in GetOriginsModifiedSince.
39 // Neither GetOriginsModifiedSince nor DeleteOriginData touches the actual
40 // origin data stored in the profile.
41 class MockQuotaManager : public QuotaManager {
42 public:
43 MockQuotaManager(
44 bool is_incognito,
45 const base::FilePath& profile_path,
46 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread,
47 const scoped_refptr<base::SequencedTaskRunner>& db_thread,
48 const scoped_refptr<SpecialStoragePolicy>& special_storage_policy);
50 // Overrides QuotaManager's implementation. The internal usage data is
51 // updated when MockQuotaManagerProxy::NotifyStorageModified() is
52 // called. The internal quota value can be updated by calling
53 // a helper method MockQuotaManagerProxy::SetQuota().
54 void GetUsageAndQuota(const GURL& origin,
55 storage::StorageType type,
56 const GetUsageAndQuotaCallback& callback) override;
58 // Overrides QuotaManager's implementation with a canned implementation that
59 // allows clients to set up the origin database that should be queried. This
60 // method will only search through the origins added explicitly via AddOrigin.
61 void GetOriginsModifiedSince(StorageType type,
62 base::Time modified_since,
63 const GetOriginsCallback& callback) override;
65 // Removes an origin from the canned list of origins, but doesn't touch
66 // anything on disk. The caller must provide |quota_client_mask| which
67 // specifies the types of QuotaClients which should be removed from this
68 // origin as a bitmask built from QuotaClient::IDs. Setting the mask to
69 // QuotaClient::kAllClientsMask will remove all clients from the origin,
70 // regardless of type.
71 void DeleteOriginData(const GURL& origin,
72 StorageType type,
73 int quota_client_mask,
74 const StatusCallback& callback) override;
76 // Helper method for updating internal quota info.
77 void SetQuota(const GURL& origin, StorageType type, int64 quota);
79 // Helper methods for timed-deletion testing:
80 // Adds an origin to the canned list that will be searched through via
81 // GetOriginsModifiedSince. The caller must provide |quota_client_mask|
82 // which specifies the types of QuotaClients this canned origin contains
83 // as a bitmask built from QuotaClient::IDs.
84 bool AddOrigin(const GURL& origin,
85 StorageType type,
86 int quota_client_mask,
87 base::Time modified);
89 // Helper methods for timed-deletion testing:
90 // Checks an origin and type against the origins that have been added via
91 // AddOrigin and removed via DeleteOriginData. If the origin exists in the
92 // canned list with the proper StorageType and client, returns true.
93 bool OriginHasData(const GURL& origin,
94 StorageType type,
95 QuotaClient::ID quota_client) const;
97 protected:
98 ~MockQuotaManager() override;
100 private:
101 friend class MockQuotaManagerProxy;
103 // Contains the essential bits of information about an origin that the
104 // MockQuotaManager needs to understand for time-based deletion:
105 // the origin itself, the StorageType and its modification time.
106 struct OriginInfo {
107 OriginInfo(const GURL& origin,
108 StorageType type,
109 int quota_client_mask,
110 base::Time modified);
111 ~OriginInfo();
113 GURL origin;
114 StorageType type;
115 int quota_client_mask;
116 base::Time modified;
119 // Contains the essential information for each origin for usage/quota testing.
120 // (Ideally this should probably merged into the above struct, but for
121 // regular usage/quota testing we hardly need modified time but only
122 // want to keep usage and quota information, so this struct exists.
123 struct StorageInfo {
124 StorageInfo();
125 ~StorageInfo();
126 int64 usage;
127 int64 quota;
130 typedef std::pair<GURL, StorageType> OriginAndType;
131 typedef std::map<OriginAndType, StorageInfo> UsageAndQuotaMap;
133 // This must be called via MockQuotaManagerProxy.
134 void UpdateUsage(const GURL& origin, StorageType type, int64 delta);
135 void DidGetModifiedSince(const GetOriginsCallback& callback,
136 std::set<GURL>* origins,
137 StorageType storage_type);
138 void DidDeleteOriginData(const StatusCallback& callback,
139 QuotaStatusCode status);
141 // The list of stored origins that have been added via AddOrigin.
142 std::vector<OriginInfo> origins_;
143 UsageAndQuotaMap usage_and_quota_map_;
144 base::WeakPtrFactory<MockQuotaManager> weak_factory_;
146 DISALLOW_COPY_AND_ASSIGN(MockQuotaManager);
149 } // namespace content
151 #endif // CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_