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_STORAGE_CLIENT_H_ 6 #define WEBKIT_BROWSER_QUOTA_MOCK_STORAGE_CLIENT_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <utility> 12 13 #include "base/compiler_specific.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/time/time.h" 16 #include "url/gurl.h" 17 #include "webkit/browser/quota/quota_client.h" 18 19 namespace quota { 20 21 class QuotaManagerProxy; 22 23 struct MockOriginData { 24 const char* origin; 25 StorageType type; 26 int64 usage; 27 }; 28 29 // Mock storage class for testing. 30 class MockStorageClient : public QuotaClient { 31 public: 32 MockStorageClient(QuotaManagerProxy* quota_manager_proxy, 33 const MockOriginData* mock_data, 34 QuotaClient::ID id, 35 size_t mock_data_size); 36 virtual ~MockStorageClient(); 37 38 // To add or modify mock data in this client. 39 void AddOriginAndNotify( 40 const GURL& origin_url, StorageType type, int64 size); 41 void ModifyOriginAndNotify( 42 const GURL& origin_url, StorageType type, int64 delta); 43 void TouchAllOriginsAndNotify(); 44 45 void AddOriginToErrorSet(const GURL& origin_url, StorageType type); 46 47 base::Time IncrementMockTime(); 48 49 // QuotaClient methods. 50 virtual QuotaClient::ID id() const OVERRIDE; 51 virtual void OnQuotaManagerDestroyed() OVERRIDE; 52 virtual void GetOriginUsage(const GURL& origin_url, 53 StorageType type, 54 const GetUsageCallback& callback) OVERRIDE; 55 virtual void GetOriginsForType(StorageType type, 56 const GetOriginsCallback& callback) OVERRIDE; 57 virtual void GetOriginsForHost(StorageType type, const std::string& host, 58 const GetOriginsCallback& callback) OVERRIDE; 59 virtual void DeleteOriginData(const GURL& origin, 60 StorageType type, 61 const DeletionCallback& callback) OVERRIDE; 62 virtual bool DoesSupport(quota::StorageType type) const OVERRIDE; 63 64 private: 65 void RunGetOriginUsage(const GURL& origin_url, 66 StorageType type, 67 const GetUsageCallback& callback); 68 void RunGetOriginsForType(StorageType type, 69 const GetOriginsCallback& callback); 70 void RunGetOriginsForHost(StorageType type, 71 const std::string& host, 72 const GetOriginsCallback& callback); 73 void RunDeleteOriginData(const GURL& origin_url, 74 StorageType type, 75 const DeletionCallback& callback); 76 77 void Populate(const MockOriginData* mock_data, size_t mock_data_size); 78 79 scoped_refptr<QuotaManagerProxy> quota_manager_proxy_; 80 const ID id_; 81 82 typedef std::map<std::pair<GURL, StorageType>, int64> OriginDataMap; 83 OriginDataMap origin_data_; 84 typedef std::set<std::pair<GURL, StorageType> > ErrorOriginSet; 85 ErrorOriginSet error_origins_; 86 87 int mock_time_counter_; 88 89 base::WeakPtrFactory<MockStorageClient> weak_factory_; 90 91 DISALLOW_COPY_AND_ASSIGN(MockStorageClient); 92 }; 93 94 } // namespace quota 95 96 #endif // WEBKIT_BROWSER_QUOTA_MOCK_STORAGE_CLIENT_H_ 97