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