1 // Copyright 2013 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 WEBKIT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
6 #define WEBKIT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
13 #include "base/memory/scoped_ptr.h"
15 #include "webkit/browser/quota/quota_client.h"
16 #include "webkit/browser/quota/quota_manager.h"
17 #include "webkit/browser/quota/quota_task.h"
18 #include "webkit/common/quota/quota_types.h"
22 // Mocks the pieces of QuotaManager's interface.
24 // For usage/quota tracking test:
25 // Usage and quota information can be updated by following private helper
26 // methods: SetQuota() and UpdateUsage().
28 // For time-based deletion test:
29 // Origins can be added to the mock by calling AddOrigin, and that list of
30 // origins is then searched through in GetOriginsModifiedSince.
31 // Neither GetOriginsModifiedSince nor DeleteOriginData touches the actual
32 // origin data stored in the profile.
33 class MockQuotaManager : public QuotaManager {
35 MockQuotaManager(bool is_incognito,
36 const base::FilePath& profile_path,
37 base::SingleThreadTaskRunner* io_thread,
38 base::SequencedTaskRunner* db_thread,
39 SpecialStoragePolicy* special_storage_policy);
41 // Overrides QuotaManager's implementation. The internal usage data is
42 // updated when MockQuotaManagerProxy::NotifyStorageModified() is
43 // called. The internal quota value can be updated by calling
44 // a helper method MockQuotaManagerProxy::SetQuota().
45 virtual void GetUsageAndQuota(
47 quota::StorageType type,
48 const GetUsageAndQuotaCallback& callback) OVERRIDE;
50 // Overrides QuotaManager's implementation with a canned implementation that
51 // allows clients to set up the origin database that should be queried. This
52 // method will only search through the origins added explicitly via AddOrigin.
53 virtual void GetOriginsModifiedSince(
55 base::Time modified_since,
56 const GetOriginsCallback& callback) OVERRIDE;
58 // Removes an origin from the canned list of origins, but doesn't touch
59 // anything on disk. The caller must provide |quota_client_mask| which
60 // specifies the types of QuotaClients which should be removed from this
61 // origin as a bitmask built from QuotaClient::IDs. Setting the mask to
62 // QuotaClient::kAllClientsMask will remove all clients from the origin,
63 // regardless of type.
64 virtual void DeleteOriginData(const GURL& origin,
66 int quota_client_mask,
67 const StatusCallback& callback) OVERRIDE;
69 // Helper method for updating internal quota info.
70 void SetQuota(const GURL& origin, StorageType type, int64 quota);
72 // Helper methods for timed-deletion testing:
73 // Adds an origin to the canned list that will be searched through via
74 // GetOriginsModifiedSince. The caller must provide |quota_client_mask|
75 // which specifies the types of QuotaClients this canned origin contains
76 // as a bitmask built from QuotaClient::IDs.
77 bool AddOrigin(const GURL& origin,
79 int quota_client_mask,
82 // Helper methods for timed-deletion testing:
83 // Checks an origin and type against the origins that have been added via
84 // AddOrigin and removed via DeleteOriginData. If the origin exists in the
85 // canned list with the proper StorageType and client, returns true.
86 bool OriginHasData(const GURL& origin,
88 QuotaClient::ID quota_client) const;
91 virtual ~MockQuotaManager();
94 friend class MockQuotaManagerProxy;
96 // Contains the essential bits of information about an origin that the
97 // MockQuotaManager needs to understand for time-based deletion:
98 // the origin itself, the StorageType and its modification time.
100 OriginInfo(const GURL& origin,
102 int quota_client_mask,
103 base::Time modified);
108 int quota_client_mask;
112 // Contains the essential information for each origin for usage/quota testing.
113 // (Ideally this should probably merged into the above struct, but for
114 // regular usage/quota testing we hardly need modified time but only
115 // want to keep usage and quota information, so this struct exists.
123 typedef std::pair<GURL, StorageType> OriginAndType;
124 typedef std::map<OriginAndType, StorageInfo> UsageAndQuotaMap;
126 // This must be called via MockQuotaManagerProxy.
127 void UpdateUsage(const GURL& origin, StorageType type, int64 delta);
128 void DidGetModifiedSince(const GetOriginsCallback& callback,
129 std::set<GURL>* origins,
130 StorageType storage_type);
131 void DidDeleteOriginData(const StatusCallback& callback,
132 QuotaStatusCode status);
134 // The list of stored origins that have been added via AddOrigin.
135 std::vector<OriginInfo> origins_;
136 UsageAndQuotaMap usage_and_quota_map_;
137 base::WeakPtrFactory<MockQuotaManager> weak_factory_;
139 DISALLOW_COPY_AND_ASSIGN(MockQuotaManager);
144 #endif // WEBKIT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_