• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
6 #define CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
7 
8 #include <map>
9 #include <set>
10 #include <utility>
11 #include <vector>
12 
13 #include "base/memory/scoped_ptr.h"
14 #include "url/gurl.h"
15 #include "webkit/browser/quota/quota_client.h"
16 #include "webkit/browser/quota/quota_manager.h"
17 #include "webkit/browser/quota/quota_task.h"
18 #include "webkit/common/quota/quota_types.h"
19 
20 using quota::GetOriginsCallback;
21 using quota::QuotaClient;
22 using quota::QuotaManager;
23 using quota::QuotaStatusCode;
24 using quota::SpecialStoragePolicy;
25 using quota::StatusCallback;
26 using quota::StorageType;
27 
28 namespace content {
29 
30 // Mocks the pieces of QuotaManager's interface.
31 //
32 // For usage/quota tracking test:
33 // Usage and quota information can be updated by following private helper
34 // methods: SetQuota() and UpdateUsage().
35 //
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 {
42  public:
43   MockQuotaManager(bool is_incognito,
44                    const base::FilePath& profile_path,
45                    base::SingleThreadTaskRunner* io_thread,
46                    base::SequencedTaskRunner* db_thread,
47                    SpecialStoragePolicy* special_storage_policy);
48 
49   // Overrides QuotaManager's implementation. The internal usage data is
50   // updated when MockQuotaManagerProxy::NotifyStorageModified() is
51   // called.  The internal quota value can be updated by calling
52   // a helper method MockQuotaManagerProxy::SetQuota().
53   virtual void GetUsageAndQuota(
54       const GURL& origin,
55       quota::StorageType type,
56       const GetUsageAndQuotaCallback& callback) OVERRIDE;
57 
58   // Overrides QuotaManager's implementation with a canned implementation that
59   // allows clients to set up the origin database that should be queried. This
60   // method will only search through the origins added explicitly via AddOrigin.
61   virtual void GetOriginsModifiedSince(
62       StorageType type,
63       base::Time modified_since,
64       const GetOriginsCallback& callback) OVERRIDE;
65 
66   // Removes an origin from the canned list of origins, but doesn't touch
67   // anything on disk. The caller must provide |quota_client_mask| which
68   // specifies the types of QuotaClients which should be removed from this
69   // origin as a bitmask built from QuotaClient::IDs. Setting the mask to
70   // QuotaClient::kAllClientsMask will remove all clients from the origin,
71   // regardless of type.
72   virtual void DeleteOriginData(const GURL& origin,
73                                 StorageType type,
74                                 int quota_client_mask,
75                                 const StatusCallback& callback) OVERRIDE;
76 
77   // Helper method for updating internal quota info.
78   void SetQuota(const GURL& origin, StorageType type, int64 quota);
79 
80   // Helper methods for timed-deletion testing:
81   // Adds an origin to the canned list that will be searched through via
82   // GetOriginsModifiedSince. The caller must provide |quota_client_mask|
83   // which specifies the types of QuotaClients this canned origin contains
84   // as a bitmask built from QuotaClient::IDs.
85   bool AddOrigin(const GURL& origin,
86                  StorageType type,
87                  int quota_client_mask,
88                  base::Time modified);
89 
90   // Helper methods for timed-deletion testing:
91   // Checks an origin and type against the origins that have been added via
92   // AddOrigin and removed via DeleteOriginData. If the origin exists in the
93   // canned list with the proper StorageType and client, returns true.
94   bool OriginHasData(const GURL& origin,
95                      StorageType type,
96                      QuotaClient::ID quota_client) const;
97 
98  protected:
99   virtual ~MockQuotaManager();
100 
101  private:
102   friend class MockQuotaManagerProxy;
103 
104   // Contains the essential bits of information about an origin that the
105   // MockQuotaManager needs to understand for time-based deletion:
106   // the origin itself, the StorageType and its modification time.
107   struct OriginInfo {
108     OriginInfo(const GURL& origin,
109                StorageType type,
110                int quota_client_mask,
111                base::Time modified);
112     ~OriginInfo();
113 
114     GURL origin;
115     StorageType type;
116     int quota_client_mask;
117     base::Time modified;
118   };
119 
120   // Contains the essential information for each origin for usage/quota testing.
121   // (Ideally this should probably merged into the above struct, but for
122   // regular usage/quota testing we hardly need modified time but only
123   // want to keep usage and quota information, so this struct exists.
124   struct StorageInfo {
125     StorageInfo();
126     ~StorageInfo();
127     int64 usage;
128     int64 quota;
129   };
130 
131   typedef std::pair<GURL, StorageType> OriginAndType;
132   typedef std::map<OriginAndType, StorageInfo> UsageAndQuotaMap;
133 
134   // This must be called via MockQuotaManagerProxy.
135   void UpdateUsage(const GURL& origin, StorageType type, int64 delta);
136   void DidGetModifiedSince(const GetOriginsCallback& callback,
137                            std::set<GURL>* origins,
138                            StorageType storage_type);
139   void DidDeleteOriginData(const StatusCallback& callback,
140                            QuotaStatusCode status);
141 
142   // The list of stored origins that have been added via AddOrigin.
143   std::vector<OriginInfo> origins_;
144   UsageAndQuotaMap usage_and_quota_map_;
145   base::WeakPtrFactory<MockQuotaManager> weak_factory_;
146 
147   DISALLOW_COPY_AND_ASSIGN(MockQuotaManager);
148 };
149 
150 }  // namespace content
151 
152 #endif  // CONTENT_BROWSER_QUOTA_MOCK_QUOTA_MANAGER_H_
153