• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
6 #define WEBKIT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/memory/scoped_ptr.h"
12 #include "url/gurl.h"
13 #include "webkit/browser/quota/quota_client.h"
14 #include "webkit/browser/quota/quota_manager.h"
15 #include "webkit/browser/quota/quota_task.h"
16 #include "webkit/common/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   UsageAndQuotaMap usage_and_quota_map_;
135   base::WeakPtrFactory<MockQuotaManager> weak_factory_;
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.
NotifyOriginInUse(const GURL & origin)152   virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {}
NotifyOriginNoLongerInUse(const GURL & origin)153   virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {}
SetUsageCacheEnabled(QuotaClient::ID client_id,const GURL & origin,StorageType type,bool enabled)154   virtual void SetUsageCacheEnabled(QuotaClient::ID client_id,
155                                     const GURL& origin,
156                                     StorageType type,
157                                     bool enabled) OVERRIDE {}
GetUsageAndQuota(base::SequencedTaskRunner * original_task_runner,const GURL & origin,StorageType type,const QuotaManager::GetUsageAndQuotaCallback & callback)158   virtual void GetUsageAndQuota(
159       base::SequencedTaskRunner* original_task_runner,
160       const GURL& origin,
161       StorageType type,
162       const QuotaManager::GetUsageAndQuotaCallback& callback) OVERRIDE {}
163 
164   // Validates the |client_id| and updates the internal access count
165   // which can be accessed via notify_storage_accessed_count().
166   // The also records the |origin| and |type| in last_notified_origin_ and
167   // last_notified_type_.
168   virtual void NotifyStorageAccessed(QuotaClient::ID client_id,
169                                      const GURL& origin,
170                                      StorageType type) OVERRIDE;
171 
172   // Records the |origin|, |type| and |delta| as last_notified_origin_,
173   // last_notified_type_ and last_notified_delta_ respecitvely.
174   // If non-null MockQuotaManager is given to the constructor this also
175   // updates the manager's internal usage information.
176   virtual void NotifyStorageModified(QuotaClient::ID client_id,
177                                      const GURL& origin,
178                                      StorageType type,
179                                      int64 delta) OVERRIDE;
180 
notify_storage_accessed_count()181   int notify_storage_accessed_count() const { return storage_accessed_count_; }
notify_storage_modified_count()182   int notify_storage_modified_count() const { return storage_modified_count_; }
last_notified_origin()183   GURL last_notified_origin() const { return last_notified_origin_; }
last_notified_type()184   StorageType last_notified_type() const { return last_notified_type_; }
last_notified_delta()185   int64 last_notified_delta() const { return last_notified_delta_; }
186 
187  protected:
188   virtual ~MockQuotaManagerProxy();
189 
190  private:
mock_manager()191   MockQuotaManager* mock_manager() const {
192     return static_cast<MockQuotaManager*>(quota_manager());
193   }
194 
195   int storage_accessed_count_;
196   int storage_modified_count_;
197   GURL last_notified_origin_;
198   StorageType last_notified_type_;
199   int64 last_notified_delta_;
200 
201   QuotaClient* registered_client_;
202 };
203 
204 }  // namespace quota
205 
206 #endif  // WEBKIT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
207