1 // Copyright (c) 2012 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_QUOTA_MOCK_QUOTA_MANAGER_H_
6 #define WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "googleurl/src/gurl.h"
13 #include "webkit/quota/quota_client.h"
14 #include "webkit/quota/quota_manager.h"
15 #include "webkit/quota/quota_task.h"
16 #include "webkit/quota/quota_types.h"
20 // Mocks the pieces of QuotaManager's interface.
22 // For usage/quota tracking test:
23 // Usage and quota information can be updated by following private helper
24 // methods: SetQuota() and UpdateUsage().
26 // For time-based deletion test:
27 // Origins can be added to the mock by calling AddOrigin, and that list of
28 // origins is then searched through in GetOriginsModifiedSince.
29 // Neither GetOriginsModifiedSince nor DeleteOriginData touches the actual
30 // origin data stored in the profile.
31 class MockQuotaManager
: public QuotaManager
{
33 MockQuotaManager(bool is_incognito
,
34 const base::FilePath
& profile_path
,
35 base::SingleThreadTaskRunner
* io_thread
,
36 base::SequencedTaskRunner
* db_thread
,
37 SpecialStoragePolicy
* special_storage_policy
);
39 // Overrides QuotaManager's implementation. The internal usage data is
40 // updated when MockQuotaManagerProxy::NotifyStorageModified() is
41 // called. The internal quota value can be updated by calling
42 // a helper method MockQuotaManagerProxy::SetQuota().
43 virtual void GetUsageAndQuota(
45 quota::StorageType type
,
46 const GetUsageAndQuotaCallback
& callback
) OVERRIDE
;
48 // Overrides QuotaManager's implementation with a canned implementation that
49 // allows clients to set up the origin database that should be queried. This
50 // method will only search through the origins added explicitly via AddOrigin.
51 virtual void GetOriginsModifiedSince(
53 base::Time modified_since
,
54 const GetOriginsCallback
& callback
) OVERRIDE
;
56 // Removes an origin from the canned list of origins, but doesn't touch
57 // anything on disk. The caller must provide |quota_client_mask| which
58 // specifies the types of QuotaClients which should be removed from this
59 // origin as a bitmask built from QuotaClient::IDs. Setting the mask to
60 // QuotaClient::kAllClientsMask will remove all clients from the origin,
61 // regardless of type.
62 virtual void DeleteOriginData(const GURL
& origin
,
64 int quota_client_mask
,
65 const StatusCallback
& callback
) OVERRIDE
;
67 // Helper method for updating internal quota info.
68 void SetQuota(const GURL
& origin
, StorageType type
, int64 quota
);
70 // Helper methods for timed-deletion testing:
71 // Adds an origin to the canned list that will be searched through via
72 // GetOriginsModifiedSince. The caller must provide |quota_client_mask|
73 // which specifies the types of QuotaClients this canned origin contains
74 // as a bitmask built from QuotaClient::IDs.
75 bool AddOrigin(const GURL
& origin
,
77 int quota_client_mask
,
80 // Helper methods for timed-deletion testing:
81 // Checks an origin and type against the origins that have been added via
82 // AddOrigin and removed via DeleteOriginData. If the origin exists in the
83 // canned list with the proper StorageType and client, returns true.
84 bool OriginHasData(const GURL
& origin
,
86 QuotaClient::ID quota_client
) const;
89 virtual ~MockQuotaManager();
92 friend class MockQuotaManagerProxy
;
94 // Contains the essential bits of information about an origin that the
95 // MockQuotaManager needs to understand for time-based deletion:
96 // the origin itself, the StorageType and its modification time.
98 OriginInfo(const GURL
& origin
,
100 int quota_client_mask
,
101 base::Time modified
);
106 int quota_client_mask
;
110 // Contains the essential information for each origin for usage/quota testing.
111 // (Ideally this should probably merged into the above struct, but for
112 // regular usage/quota testing we hardly need modified time but only
113 // want to keep usage and quota information, so this struct exists.
121 typedef std::pair
<GURL
, StorageType
> OriginAndType
;
122 typedef std::map
<OriginAndType
, StorageInfo
> UsageAndQuotaMap
;
124 // This must be called via MockQuotaManagerProxy.
125 void UpdateUsage(const GURL
& origin
, StorageType type
, int64 delta
);
126 void DidGetModifiedSince(const GetOriginsCallback
& callback
,
127 std::set
<GURL
>* origins
,
128 StorageType storage_type
);
129 void DidDeleteOriginData(const StatusCallback
& callback
,
130 QuotaStatusCode status
);
132 // The list of stored origins that have been added via AddOrigin.
133 std::vector
<OriginInfo
> origins_
;
134 base::WeakPtrFactory
<MockQuotaManager
> weak_factory_
;
135 UsageAndQuotaMap usage_and_quota_map_
;
137 DISALLOW_COPY_AND_ASSIGN(MockQuotaManager
);
140 // MockQuotaManagerProxy.
141 class MockQuotaManagerProxy
: public QuotaManagerProxy
{
143 // It is ok to give NULL to |quota_manager|.
144 MockQuotaManagerProxy(MockQuotaManager
* quota_manager
,
145 base::SingleThreadTaskRunner
* task_runner
);
147 virtual void RegisterClient(QuotaClient
* client
) OVERRIDE
;
149 void SimulateQuotaManagerDestroyed();
151 // We don't mock them.
152 virtual void NotifyOriginInUse(const GURL
& origin
) OVERRIDE
{}
153 virtual void NotifyOriginNoLongerInUse(const GURL
& origin
) OVERRIDE
{}
155 // Validates the |client_id| and updates the internal access count
156 // which can be accessed via notify_storage_accessed_count().
157 // The also records the |origin| and |type| in last_notified_origin_ and
158 // last_notified_type_.
159 virtual void NotifyStorageAccessed(QuotaClient::ID client_id
,
161 StorageType type
) OVERRIDE
;
163 // Records the |origin|, |type| and |delta| as last_notified_origin_,
164 // last_notified_type_ and last_notified_delta_ respecitvely.
165 // If non-null MockQuotaManager is given to the constructor this also
166 // updates the manager's internal usage information.
167 virtual void NotifyStorageModified(QuotaClient::ID client_id
,
170 int64 delta
) OVERRIDE
;
172 int notify_storage_accessed_count() const { return storage_accessed_count_
; }
173 int notify_storage_modified_count() const { return storage_modified_count_
; }
174 GURL
last_notified_origin() const { return last_notified_origin_
; }
175 StorageType
last_notified_type() const { return last_notified_type_
; }
176 int64
last_notified_delta() const { return last_notified_delta_
; }
179 virtual ~MockQuotaManagerProxy();
182 MockQuotaManager
* mock_manager() const {
183 return static_cast<MockQuotaManager
*>(quota_manager());
186 int storage_accessed_count_
;
187 int storage_modified_count_
;
188 GURL last_notified_origin_
;
189 StorageType last_notified_type_
;
190 int64 last_notified_delta_
;
192 QuotaClient
* registered_client_
;
197 #endif // WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_