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_
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"
20 using quota::GetOriginsCallback
;
21 using quota::QuotaClient
;
22 using quota::QuotaManager
;
23 using quota::QuotaStatusCode
;
24 using quota::SpecialStoragePolicy
;
25 using quota::StatusCallback
;
26 using quota::StorageType
;
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
{
43 MockQuotaManager(bool is_incognito
,
44 const base::FilePath
& profile_path
,
45 base::SingleThreadTaskRunner
* io_thread
,
46 base::SequencedTaskRunner
* db_thread
,
47 SpecialStoragePolicy
* special_storage_policy
);
49 // Overrides QuotaManager's implementation. The internal usage data is
50 // updated when MockQuotaManagerProxy::NotifyStorageModified() is
51 // called. The internal quota value can be updated by calling
52 // a helper method MockQuotaManagerProxy::SetQuota().
53 virtual void GetUsageAndQuota(
55 quota::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 virtual void GetOriginsModifiedSince(
63 base::Time modified_since
,
64 const GetOriginsCallback
& callback
) OVERRIDE
;
66 // Removes an origin from the canned list of origins, but doesn't touch
67 // anything on disk. The caller must provide |quota_client_mask| which
68 // specifies the types of QuotaClients which should be removed from this
69 // origin as a bitmask built from QuotaClient::IDs. Setting the mask to
70 // QuotaClient::kAllClientsMask will remove all clients from the origin,
71 // regardless of type.
72 virtual void DeleteOriginData(const GURL
& origin
,
74 int quota_client_mask
,
75 const StatusCallback
& callback
) OVERRIDE
;
77 // Helper method for updating internal quota info.
78 void SetQuota(const GURL
& origin
, StorageType type
, int64 quota
);
80 // Helper methods for timed-deletion testing:
81 // Adds an origin to the canned list that will be searched through via
82 // GetOriginsModifiedSince. The caller must provide |quota_client_mask|
83 // which specifies the types of QuotaClients this canned origin contains
84 // as a bitmask built from QuotaClient::IDs.
85 bool AddOrigin(const GURL
& origin
,
87 int quota_client_mask
,
90 // Helper methods for timed-deletion testing:
91 // Checks an origin and type against the origins that have been added via
92 // AddOrigin and removed via DeleteOriginData. If the origin exists in the
93 // canned list with the proper StorageType and client, returns true.
94 bool OriginHasData(const GURL
& origin
,
96 QuotaClient::ID quota_client
) const;
99 virtual ~MockQuotaManager();
102 friend class MockQuotaManagerProxy
;
104 // Contains the essential bits of information about an origin that the
105 // MockQuotaManager needs to understand for time-based deletion:
106 // the origin itself, the StorageType and its modification time.
108 OriginInfo(const GURL
& origin
,
110 int quota_client_mask
,
111 base::Time modified
);
116 int quota_client_mask
;
120 // Contains the essential information for each origin for usage/quota testing.
121 // (Ideally this should probably merged into the above struct, but for
122 // regular usage/quota testing we hardly need modified time but only
123 // want to keep usage and quota information, so this struct exists.
131 typedef std::pair
<GURL
, StorageType
> OriginAndType
;
132 typedef std::map
<OriginAndType
, StorageInfo
> UsageAndQuotaMap
;
134 // This must be called via MockQuotaManagerProxy.
135 void UpdateUsage(const GURL
& origin
, StorageType type
, int64 delta
);
136 void DidGetModifiedSince(const GetOriginsCallback
& callback
,
137 std::set
<GURL
>* origins
,
138 StorageType storage_type
);
139 void DidDeleteOriginData(const StatusCallback
& callback
,
140 QuotaStatusCode status
);
142 // The list of stored origins that have been added via AddOrigin.
143 std::vector
<OriginInfo
> origins_
;
144 UsageAndQuotaMap usage_and_quota_map_
;
145 base::WeakPtrFactory
<MockQuotaManager
> weak_factory_
;
147 DISALLOW_COPY_AND_ASSIGN(MockQuotaManager
);
150 } // namespace content
152 #endif // CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_