OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef WEBKIT_APPCACHE_MOCK_APPCACHE_STORAGE_H_ | 5 #ifndef WEBKIT_APPCACHE_MOCK_APPCACHE_STORAGE_H_ |
6 #define WEBKIT_APPCACHE_MOCK_APPCACHE_STORAGE_H_ | 6 #define WEBKIT_APPCACHE_MOCK_APPCACHE_STORAGE_H_ |
7 | 7 |
| 8 #include <deque> |
8 #include <map> | 9 #include <map> |
9 | 10 |
10 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
11 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 13 #include "base/task.h" |
12 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
13 #include "webkit/appcache/appcache.h" | 15 #include "webkit/appcache/appcache.h" |
14 #include "webkit/appcache/appcache_group.h" | 16 #include "webkit/appcache/appcache_group.h" |
15 #include "webkit/appcache/appcache_storage.h" | 17 #include "webkit/appcache/appcache_storage.h" |
16 | 18 |
17 namespace appcache { | 19 namespace appcache { |
18 | 20 |
19 // For use in unit tests. | 21 // For use in unit tests. |
20 // Note: This class is also being used to bootstrap our development efforts. | 22 // Note: This class is also being used to bootstrap our development efforts. |
21 // We can get layout tests up and running, and back fill with real storage | 23 // We can get layout tests up and running, and back fill with real storage |
22 // somewhat in parallel. | 24 // somewhat in parallel. |
23 class MockAppCacheStorage : public AppCacheStorage { | 25 class MockAppCacheStorage : public AppCacheStorage { |
24 public: | 26 public: |
25 explicit MockAppCacheStorage(AppCacheService* service); | 27 explicit MockAppCacheStorage(AppCacheService* service); |
| 28 ~MockAppCacheStorage(); |
| 29 |
26 virtual void LoadCache(int64 id, Delegate* delegate); | 30 virtual void LoadCache(int64 id, Delegate* delegate); |
27 virtual void LoadOrCreateGroup(const GURL& manifest_url, Delegate* delegate); | 31 virtual void LoadOrCreateGroup(const GURL& manifest_url, Delegate* delegate); |
28 virtual void StoreGroupAndNewestCache( | 32 virtual void StoreGroupAndNewestCache( |
29 AppCacheGroup* group, Delegate* delegate); | 33 AppCacheGroup* group, Delegate* delegate); |
30 virtual void FindResponseForMainRequest(const GURL& url, Delegate* delegate); | 34 virtual void FindResponseForMainRequest(const GURL& url, Delegate* delegate); |
31 virtual void MarkEntryAsForeign(const GURL& entry_url, int64 cache_id); | 35 virtual void MarkEntryAsForeign(const GURL& entry_url, int64 cache_id); |
32 virtual void MarkGroupAsObsolete(AppCacheGroup* group, Delegate* delegate); | 36 virtual void MakeGroupObsolete(AppCacheGroup* group, Delegate* delegate); |
33 virtual AppCacheResponseReader* CreateResponseReader( | 37 virtual AppCacheResponseReader* CreateResponseReader( |
34 const GURL& manifest_url, int64 response_id); | 38 const GURL& manifest_url, int64 response_id); |
35 virtual AppCacheResponseWriter* CreateResponseWriter(const GURL& origin); | 39 virtual AppCacheResponseWriter* CreateResponseWriter(const GURL& origin); |
36 virtual void DoomResponses( | 40 virtual void DoomResponses( |
37 const GURL& manifest_url, const std::vector<int64>& response_ids); | 41 const GURL& manifest_url, const std::vector<int64>& response_ids); |
38 | 42 |
39 private: | 43 private: |
40 typedef base::hash_map<int64, scoped_refptr<AppCache> > StoredCacheMap; | 44 typedef base::hash_map<int64, scoped_refptr<AppCache> > StoredCacheMap; |
41 typedef std::map<GURL, scoped_refptr<AppCacheGroup> > StoredGroupMap; | 45 typedef std::map<GURL, scoped_refptr<AppCacheGroup> > StoredGroupMap; |
| 46 typedef std::set<int64> DoomedResponseIds; |
| 47 |
| 48 void ProcessLoadCache( |
| 49 int64 id, scoped_refptr<DelegateReference> delegate_ref); |
| 50 void ProcessLoadOrCreateGroup( |
| 51 const GURL& manifest_url, scoped_refptr<DelegateReference> delegate_ref); |
| 52 void ProcessStoreGroupAndNewestCache( |
| 53 scoped_refptr<AppCacheGroup> group, scoped_refptr<AppCache> newest_cache, |
| 54 scoped_refptr<DelegateReference> delegate_ref); |
| 55 void ProcessMakeGroupObsolete( |
| 56 scoped_refptr<AppCacheGroup> group, |
| 57 scoped_refptr<DelegateReference> delegate_ref); |
| 58 void ProcessFindResponseForMainRequest( |
| 59 const GURL& url, scoped_refptr<DelegateReference> delegate_ref); |
| 60 |
| 61 void ScheduleTask(Task* task); |
| 62 void RunOnePendingTask(); |
42 | 63 |
43 void AddStoredCache(AppCache* cache); | 64 void AddStoredCache(AppCache* cache); |
44 void RemoveStoredCache(AppCache* cache); | 65 void RemoveStoredCache(AppCache* cache); |
45 AppCache* GetStoredCache(int64 id) { | 66 void RemoveStoredCaches(const AppCacheGroup::Caches& caches); |
46 StoredCacheMap::iterator it = stored_caches_.find(id); | 67 bool IsCacheStored(const AppCache* cache) { |
47 return (it != stored_caches_.end()) ? it->second : NULL; | 68 return stored_caches_.find(cache->cache_id()) != stored_caches_.end(); |
48 } | 69 } |
49 | 70 |
50 void AddStoredGroup(AppCacheGroup* group); | 71 void AddStoredGroup(AppCacheGroup* group); |
51 void RemoveStoredGroup(AppCacheGroup* group); | 72 void RemoveStoredGroup(AppCacheGroup* group); |
52 AppCacheGroup* GetStoredGroup(const GURL& manifest_url) { | 73 |
53 StoredGroupMap::iterator it = stored_groups_.find(manifest_url); | 74 // These helpers determine when certain operations should complete |
54 return (it != stored_groups_.end()) ? it->second : NULL; | 75 // asynchronously vs synchronously to faithfully mimic, or mock, |
55 } | 76 // the behavior of the real implemenation of the AppCacheStorage |
| 77 // interface. |
| 78 bool ShouldGroupLoadAppearAsync(const AppCacheGroup* group); |
| 79 bool ShouldCacheLoadAppearAsync(const AppCache* cache); |
56 | 80 |
57 // Lazily constructed in-memory disk cache. | 81 // Lazily constructed in-memory disk cache. |
58 disk_cache::Backend* disk_cache() { | 82 disk_cache::Backend* disk_cache() { |
59 if (!disk_cache_.get()) { | 83 if (!disk_cache_.get()) { |
60 const int kMaxCacheSize = 10 * 1024 * 1024; | 84 const int kMaxCacheSize = 10 * 1024 * 1024; |
61 disk_cache_.reset(disk_cache::CreateInMemoryCacheBackend(kMaxCacheSize)); | 85 disk_cache_.reset(disk_cache::CreateInMemoryCacheBackend(kMaxCacheSize)); |
62 } | 86 } |
63 return disk_cache_.get(); | 87 return disk_cache_.get(); |
64 } | 88 } |
65 | 89 |
66 StoredCacheMap stored_caches_; | 90 StoredCacheMap stored_caches_; |
67 StoredGroupMap stored_groups_; | 91 StoredGroupMap stored_groups_; |
| 92 DoomedResponseIds doomed_response_ids_; |
68 scoped_ptr<disk_cache::Backend> disk_cache_; | 93 scoped_ptr<disk_cache::Backend> disk_cache_; |
| 94 std::deque<Task*> pending_tasks_; |
| 95 ScopedRunnableMethodFactory<MockAppCacheStorage> method_factory_; |
| 96 |
| 97 FRIEND_TEST(MockAppCacheStorageTest, CreateGroup); |
| 98 FRIEND_TEST(MockAppCacheStorageTest, LoadCache_FarHit); |
| 99 FRIEND_TEST(MockAppCacheStorageTest, LoadGroupAndCache_FarHit); |
| 100 FRIEND_TEST(MockAppCacheStorageTest, MakeGroupObsolete); |
| 101 FRIEND_TEST(MockAppCacheStorageTest, StoreNewGroup); |
| 102 FRIEND_TEST(MockAppCacheStorageTest, StoreExistingGroup); |
| 103 DISALLOW_COPY_AND_ASSIGN(MockAppCacheStorage); |
69 }; | 104 }; |
70 | 105 |
71 } // namespace appcache | 106 } // namespace appcache |
72 | 107 |
73 #endif // WEBKIT_APPCACHE_MOCK_APPCACHE_STORAGE_H_ | 108 #endif // WEBKIT_APPCACHE_MOCK_APPCACHE_STORAGE_H_ |
OLD | NEW |