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"
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"
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
;
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
{
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 virtual void GetUsageAndQuota(
56 storage::StorageType type
,
57 const GetUsageAndQuotaCallback
& callback
) OVERRIDE
;
59 // Overrides QuotaManager's implementation with a canned implementation that
60 // allows clients to set up the origin database that should be queried. This
61 // method will only search through the origins added explicitly via AddOrigin.
62 virtual void GetOriginsModifiedSince(
64 base::Time modified_since
,
65 const GetOriginsCallback
& callback
) OVERRIDE
;
67 // Removes an origin from the canned list of origins, but doesn't touch
68 // anything on disk. The caller must provide |quota_client_mask| which
69 // specifies the types of QuotaClients which should be removed from this
70 // origin as a bitmask built from QuotaClient::IDs. Setting the mask to
71 // QuotaClient::kAllClientsMask will remove all clients from the origin,
72 // regardless of type.
73 virtual void DeleteOriginData(const GURL
& origin
,
75 int quota_client_mask
,
76 const StatusCallback
& callback
) OVERRIDE
;
78 // Helper method for updating internal quota info.
79 void SetQuota(const GURL
& origin
, StorageType type
, int64 quota
);
81 // Helper methods for timed-deletion testing:
82 // Adds an origin to the canned list that will be searched through via
83 // GetOriginsModifiedSince. The caller must provide |quota_client_mask|
84 // which specifies the types of QuotaClients this canned origin contains
85 // as a bitmask built from QuotaClient::IDs.
86 bool AddOrigin(const GURL
& origin
,
88 int quota_client_mask
,
91 // Helper methods for timed-deletion testing:
92 // Checks an origin and type against the origins that have been added via
93 // AddOrigin and removed via DeleteOriginData. If the origin exists in the
94 // canned list with the proper StorageType and client, returns true.
95 bool OriginHasData(const GURL
& origin
,
97 QuotaClient::ID quota_client
) const;
100 virtual ~MockQuotaManager();
103 friend class MockQuotaManagerProxy
;
105 // Contains the essential bits of information about an origin that the
106 // MockQuotaManager needs to understand for time-based deletion:
107 // the origin itself, the StorageType and its modification time.
109 OriginInfo(const GURL
& origin
,
111 int quota_client_mask
,
112 base::Time modified
);
117 int quota_client_mask
;
121 // Contains the essential information for each origin for usage/quota testing.
122 // (Ideally this should probably merged into the above struct, but for
123 // regular usage/quota testing we hardly need modified time but only
124 // want to keep usage and quota information, so this struct exists.
132 typedef std::pair
<GURL
, StorageType
> OriginAndType
;
133 typedef std::map
<OriginAndType
, StorageInfo
> UsageAndQuotaMap
;
135 // This must be called via MockQuotaManagerProxy.
136 void UpdateUsage(const GURL
& origin
, StorageType type
, int64 delta
);
137 void DidGetModifiedSince(const GetOriginsCallback
& callback
,
138 std::set
<GURL
>* origins
,
139 StorageType storage_type
);
140 void DidDeleteOriginData(const StatusCallback
& callback
,
141 QuotaStatusCode status
);
143 // The list of stored origins that have been added via AddOrigin.
144 std::vector
<OriginInfo
> origins_
;
145 UsageAndQuotaMap usage_and_quota_map_
;
146 base::WeakPtrFactory
<MockQuotaManager
> weak_factory_
;
148 DISALLOW_COPY_AND_ASSIGN(MockQuotaManager
);
151 } // namespace content
153 #endif // CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_