OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_ | |
6 #define WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_ | |
7 | |
8 #include <vector> | |
9 #include <string> | |
10 | |
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" | |
17 | |
18 namespace quota { | |
19 | |
20 // Mocks the pieces of QuotaManager's interface. | |
21 // | |
22 // For usage/quota tracking test: | |
23 // Usage and quota information can be updated by following private helper | |
24 // methods: SetQuota() and UpdateUsage(). | |
25 // | |
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 { | |
32 public: | |
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); | |
38 | |
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( | |
44 const GURL& origin, | |
45 quota::StorageType type, | |
46 const GetUsageAndQuotaCallback& callback) OVERRIDE; | |
47 | |
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( | |
52 StorageType type, | |
53 base::Time modified_since, | |
54 const GetOriginsCallback& callback) OVERRIDE; | |
55 | |
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, | |
63 StorageType type, | |
64 int quota_client_mask, | |
65 const StatusCallback& callback) OVERRIDE; | |
66 | |
67 // Helper method for updating internal quota info. | |
68 void SetQuota(const GURL& origin, StorageType type, int64 quota); | |
69 | |
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, | |
76 StorageType type, | |
77 int quota_client_mask, | |
78 base::Time modified); | |
79 | |
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, | |
85 StorageType type, | |
86 QuotaClient::ID quota_client) const; | |
87 | |
88 protected: | |
89 virtual ~MockQuotaManager(); | |
90 | |
91 private: | |
92 friend class MockQuotaManagerProxy; | |
93 | |
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. | |
97 struct OriginInfo { | |
98 OriginInfo(const GURL& origin, | |
99 StorageType type, | |
100 int quota_client_mask, | |
101 base::Time modified); | |
102 ~OriginInfo(); | |
103 | |
104 GURL origin; | |
105 StorageType type; | |
106 int quota_client_mask; | |
107 base::Time modified; | |
108 }; | |
109 | |
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. | |
114 struct StorageInfo { | |
115 StorageInfo(); | |
116 ~StorageInfo(); | |
117 int64 usage; | |
118 int64 quota; | |
119 }; | |
120 | |
121 typedef std::pair<GURL, StorageType> OriginAndType; | |
122 typedef std::map<OriginAndType, StorageInfo> UsageAndQuotaMap; | |
123 | |
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); | |
131 | |
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_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(MockQuotaManager); | |
138 }; | |
139 | |
140 // MockQuotaManagerProxy. | |
141 class MockQuotaManagerProxy : public QuotaManagerProxy { | |
142 public: | |
143 // It is ok to give NULL to |quota_manager|. | |
144 MockQuotaManagerProxy(MockQuotaManager* quota_manager, | |
145 base::SingleThreadTaskRunner* task_runner); | |
146 | |
147 virtual void RegisterClient(QuotaClient* client) OVERRIDE; | |
148 | |
149 void SimulateQuotaManagerDestroyed(); | |
150 | |
151 // We don't mock them. | |
152 virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} | |
153 virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} | |
154 | |
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, | |
160 const GURL& origin, | |
161 StorageType type) OVERRIDE; | |
162 | |
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, | |
168 const GURL& origin, | |
169 StorageType type, | |
170 int64 delta) OVERRIDE; | |
171 | |
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_; } | |
177 | |
178 protected: | |
179 virtual ~MockQuotaManagerProxy(); | |
180 | |
181 private: | |
182 MockQuotaManager* mock_manager() const { | |
183 return static_cast<MockQuotaManager*>(quota_manager()); | |
184 } | |
185 | |
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_; | |
191 | |
192 QuotaClient* registered_client_; | |
193 }; | |
194 | |
195 } // namespace quota | |
196 | |
197 #endif // WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_ | |
OLD | NEW |