Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1930)

Side by Side Diff: webkit/quota/mock_quota_manager.h

Issue 10915202: Cleanup: merge MockQuotaManager in multiple places (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_ 5 #ifndef WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_
6 #define WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_ 6 #define WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_
7 7
8 #include <vector> 8 #include <vector>
9 #include <string> 9 #include <string>
10 10
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "googleurl/src/gurl.h" 12 #include "googleurl/src/gurl.h"
13 #include "webkit/quota/quota_client.h" 13 #include "webkit/quota/quota_client.h"
14 #include "webkit/quota/quota_manager.h" 14 #include "webkit/quota/quota_manager.h"
15 #include "webkit/quota/quota_task.h" 15 #include "webkit/quota/quota_task.h"
16 #include "webkit/quota/quota_types.h" 16 #include "webkit/quota/quota_types.h"
17 17
18 namespace quota { 18 namespace quota {
19 19
20 // Mocks the pieces of QuotaManager's interface that are used for time-based 20 // Mocks the pieces of QuotaManager's interface.
21 // deletion of a profile's browsing data. Origins can be added to the mock by 21 //
22 // calling AddOrigin, and that list of origins is then searched through in 22 // For usage/quota tracking test:
23 // GetOriginsModifiedSince. Neither GetOriginsModifiedSince nor DeleteOriginData 23 // Usage and quota information can be updated by following private helper
24 // touches the actual origin data stored in the profile. 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.
25 class MockQuotaManager : public QuotaManager { 31 class MockQuotaManager : public QuotaManager {
26 public: 32 public:
27 // Contains the essential bits of information about an origin that the
28 // MockQuotaManager needs to understand: the origin itself, the StorageType,
29 // and its modification time.
30 struct OriginInfo {
31 OriginInfo(const GURL& origin,
32 StorageType type,
33 int quota_client_mask,
34 base::Time modified);
35 ~OriginInfo();
36
37 GURL origin;
38 StorageType type;
39 int quota_client_mask;
40 base::Time modified;
41 };
42
43 MockQuotaManager(bool is_incognito, 33 MockQuotaManager(bool is_incognito,
44 const FilePath& profile_path, 34 const FilePath& profile_path,
45 base::SingleThreadTaskRunner* io_thread, 35 base::SingleThreadTaskRunner* io_thread,
46 base::SequencedTaskRunner* db_thread, 36 base::SequencedTaskRunner* db_thread,
47 SpecialStoragePolicy* special_storage_policy); 37 SpecialStoragePolicy* special_storage_policy);
48 38
49 // Adds an origin to the canned list that will be searched through via 39 // Overrides QuotaManager's implementation. The internal usage data is
50 // GetOriginsModifiedSince. The caller must provide |quota_client_mask| 40 // updated when MockQuotaManagerProxy::NotifyStorageModified() is
51 // which specifies the types of QuotaClients this canned origin contains 41 // called. The internal quota value can be updated by calling
52 // as a bitmask built from QuotaClient::IDs. 42 // a helper method MockQuotaManagerProxy::SetQuota().
53 bool AddOrigin(const GURL& origin, 43 virtual void GetUsageAndQuota(
54 StorageType type, 44 const GURL& origin,
55 int quota_client_mask, 45 quota::StorageType type,
56 base::Time modified); 46 const GetUsageAndQuotaCallback& callback) OVERRIDE;
57
58 // Checks an origin and type against the origins that have been added via
59 // AddOrigin and removed via DeleteOriginData. If the origin exists in the
60 // canned list with the proper StorageType and client, returns true.
61 bool OriginHasData(const GURL& origin,
62 StorageType type,
63 QuotaClient::ID quota_client) const;
64 47
65 // Overrides QuotaManager's implementation with a canned implementation that 48 // Overrides QuotaManager's implementation with a canned implementation that
66 // allows clients to set up the origin database that should be queried. This 49 // allows clients to set up the origin database that should be queried. This
67 // method will only search through the origins added explicitly via AddOrigin. 50 // method will only search through the origins added explicitly via AddOrigin.
68 virtual void GetOriginsModifiedSince( 51 virtual void GetOriginsModifiedSince(
69 StorageType type, 52 StorageType type,
70 base::Time modified_since, 53 base::Time modified_since,
71 const GetOriginsCallback& callback) OVERRIDE; 54 const GetOriginsCallback& callback) OVERRIDE;
72 55
73 // Removes an origin from the canned list of origins, but doesn't touch 56 // Removes an origin from the canned list of origins, but doesn't touch
74 // anything on disk. The caller must provide |quota_client_mask| which 57 // anything on disk. The caller must provide |quota_client_mask| which
75 // specifies the types of QuotaClients which should be removed from this 58 // specifies the types of QuotaClients which should be removed from this
76 // origin as a bitmask built from QuotaClient::IDs. Setting the mask to 59 // origin as a bitmask built from QuotaClient::IDs. Setting the mask to
77 // QuotaClient::kAllClientsMask will remove all clients from the origin, 60 // QuotaClient::kAllClientsMask will remove all clients from the origin,
78 // regardless of type. 61 // regardless of type.
79 virtual void DeleteOriginData(const GURL& origin, 62 virtual void DeleteOriginData(const GURL& origin,
80 StorageType type, 63 StorageType type,
81 int quota_client_mask, 64 int quota_client_mask,
82 const StatusCallback& callback) OVERRIDE; 65 const StatusCallback& callback) OVERRIDE;
83 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
84 protected: 88 protected:
85 virtual ~MockQuotaManager(); 89 virtual ~MockQuotaManager();
86 90
87 private: 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
88 class GetModifiedSinceTask; 124 class GetModifiedSinceTask;
89 class DeleteOriginDataTask; 125 class DeleteOriginDataTask;
90 126
127 // This must be called via MockQuotaManagerProxy.
128 void UpdateUsage(const GURL& origin, StorageType type, int64 delta);
129
91 // The list of stored origins that have been added via AddOrigin. 130 // The list of stored origins that have been added via AddOrigin.
92 std::vector<OriginInfo> origins_; 131 std::vector<OriginInfo> origins_;
93 132
133 UsageAndQuotaMap usage_and_quota_map_;
134
94 DISALLOW_COPY_AND_ASSIGN(MockQuotaManager); 135 DISALLOW_COPY_AND_ASSIGN(MockQuotaManager);
95 }; 136 };
96 137
138 // MockQuotaManagerProxy.
139 class MockQuotaManagerProxy : public QuotaManagerProxy {
140 public:
141 // It is ok to give NULL to |quota_manager|.
142 MockQuotaManagerProxy(MockQuotaManager* quota_manager,
143 base::SingleThreadTaskRunner* task_runner);
144
145 virtual void RegisterClient(QuotaClient* client) OVERRIDE;
146
147 void SimulateQuotaManagerDestroyed();
148
149 // We don't mock them.
150 virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {}
151 virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {}
152
153 // Validates the |client_id| and updates the internal access count
154 // which can be accessed via notify_storage_accessed_count().
155 // The also records the |origin| and |type| in last_notified_origin_ and
156 // last_notified_type_.
157 virtual void NotifyStorageAccessed(QuotaClient::ID client_id,
158 const GURL& origin,
159 StorageType type) OVERRIDE;
160
161 // Records the |origin|, |type| and |delta| as last_notified_origin_,
162 // last_notified_type_ and last_notified_delta_ respecitvely.
163 // If non-null MockQuotaManager is given to the constructor this also
164 // updates the manager's internal usage information.
165 virtual void NotifyStorageModified(QuotaClient::ID client_id,
166 const GURL& origin,
167 StorageType type,
168 int64 delta) OVERRIDE;
169
170 int notify_storage_accessed_count() const { return storage_accessed_count_; }
171 int notify_storage_modified_count() const { return storage_modified_count_; }
172 GURL last_notified_origin() const { return last_notified_origin_; }
173 StorageType last_notified_type() const { return last_notified_type_; }
174 int64 last_notified_delta() const { return last_notified_delta_; }
175
176 protected:
177 virtual ~MockQuotaManagerProxy();
178
179 private:
180 MockQuotaManager* mock_manager() const {
181 return static_cast<MockQuotaManager*>(quota_manager());
182 }
183
184 int storage_accessed_count_;
185 int storage_modified_count_;
186 GURL last_notified_origin_;
187 StorageType last_notified_type_;
188 int64 last_notified_delta_;
189
190 QuotaClient* registered_client_;
191 };
192
97 } // namespace quota 193 } // namespace quota
98 194
99 #endif // WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_ 195 #endif // WEBKIT_QUOTA_MOCK_QUOTA_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698