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