1 // Copyright (c) 2011 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.
5 #ifndef WEBKIT_BROWSER_APPCACHE_APPCACHE_STORAGE_IMPL_H_
6 #define WEBKIT_BROWSER_APPCACHE_APPCACHE_STORAGE_IMPL_H_
14 #include "base/callback.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/message_loop/message_loop_proxy.h"
18 #include "webkit/browser/appcache/appcache_database.h"
19 #include "webkit/browser/appcache/appcache_disk_cache.h"
20 #include "webkit/browser/appcache/appcache_storage.h"
21 #include "webkit/browser/webkit_storage_browser_export.h"
24 class ChromeAppCacheServiceTest;
29 class AppCacheStorageImpl : public AppCacheStorage {
31 explicit AppCacheStorageImpl(AppCacheService* service);
32 virtual ~AppCacheStorageImpl();
34 void Initialize(const base::FilePath& cache_directory,
35 base::MessageLoopProxy* db_thread,
36 base::MessageLoopProxy* cache_thread);
38 bool is_disabled() const { return is_disabled_; }
40 // AppCacheStorage methods, see the base class for doc comments.
41 virtual void GetAllInfo(Delegate* delegate) OVERRIDE;
42 virtual void LoadCache(int64 id, Delegate* delegate) OVERRIDE;
43 virtual void LoadOrCreateGroup(const GURL& manifest_url,
44 Delegate* delegate) OVERRIDE;
45 virtual void StoreGroupAndNewestCache(AppCacheGroup* group,
46 AppCache* newest_cache,
47 Delegate* delegate) OVERRIDE;
48 virtual void FindResponseForMainRequest(const GURL& url,
49 const GURL& preferred_manifest_url,
50 Delegate* delegate) OVERRIDE;
51 virtual void FindResponseForSubRequest(
52 AppCache* cache, const GURL& url,
53 AppCacheEntry* found_entry, AppCacheEntry* found_fallback_entry,
54 bool* found_network_namespace) OVERRIDE;
55 virtual void MarkEntryAsForeign(const GURL& entry_url,
56 int64 cache_id) OVERRIDE;
57 virtual void MakeGroupObsolete(AppCacheGroup* group,
58 Delegate* delegate) OVERRIDE;
59 virtual AppCacheResponseReader* CreateResponseReader(
60 const GURL& manifest_url, int64 group_id, int64 response_id) OVERRIDE;
61 virtual AppCacheResponseWriter* CreateResponseWriter(
62 const GURL& manifest_url, int64 group_id) OVERRIDE;
63 virtual void DoomResponses(const GURL& manifest_url,
64 const std::vector<int64>& response_ids) OVERRIDE;
65 virtual void DeleteResponses(const GURL& manifest_url,
66 const std::vector<int64>& response_ids) OVERRIDE;
67 virtual void PurgeMemory() OVERRIDE;
70 // The AppCacheStorageImpl class methods and datamembers may only be
71 // accessed on the IO thread. This class manufactures seperate DatabaseTasks
72 // which access the DB on a seperate background thread.
75 class CloseConnectionTask;
76 class DisableDatabaseTask;
78 class StoreOrLoadTask;
81 class StoreGroupAndCacheTask;
82 class FindMainResponseTask;
83 class MarkEntryAsForeignTask;
84 class MakeGroupObsoleteTask;
85 class GetDeletableResponseIdsTask;
86 class InsertDeletableResponseIdsTask;
87 class DeleteDeletableResponseIdsTask;
88 class UpdateGroupLastAccessTimeTask;
90 typedef std::deque<DatabaseTask*> DatabaseTaskQueue;
91 typedef std::map<int64, CacheLoadTask*> PendingCacheLoads;
92 typedef std::map<GURL, GroupLoadTask*> PendingGroupLoads;
93 typedef std::deque<std::pair<GURL, int64> > PendingForeignMarkings;
94 typedef std::set<StoreGroupAndCacheTask*> PendingQuotaQueries;
96 bool IsInitTaskComplete() {
97 return last_cache_id_ != AppCacheStorage::kUnitializedId;
100 CacheLoadTask* GetPendingCacheLoadTask(int64 cache_id);
101 GroupLoadTask* GetPendingGroupLoadTask(const GURL& manifest_url);
102 void GetPendingForeignMarkingsForCache(
103 int64 cache_id, std::vector<GURL>* urls);
105 void ScheduleSimpleTask(const base::Closure& task);
106 void RunOnePendingSimpleTask();
108 void DelayedStartDeletingUnusedResponses();
109 void StartDeletingResponses(const std::vector<int64>& response_ids);
110 void ScheduleDeleteOneResponse();
111 void DeleteOneResponse();
113 void OnDeletedOneResponse(int rv);
114 void OnDiskCacheInitialized(int rv);
115 void DeleteAndStartOver();
116 void DeleteAndStartOverPart2();
117 void CallScheduleReinitialize();
119 // Sometimes we can respond without having to query the database.
120 bool FindResponseForMainRequestInGroup(
121 AppCacheGroup* group, const GURL& url, Delegate* delegate);
122 void DeliverShortCircuitedFindMainResponse(
124 const AppCacheEntry& found_entry,
125 scoped_refptr<AppCacheGroup> group,
126 scoped_refptr<AppCache> newest_cache,
127 scoped_refptr<DelegateReference> delegate_ref);
129 void CallOnMainResponseFound(
130 DelegateReferenceVector* delegates,
131 const GURL& url, const AppCacheEntry& entry,
132 const GURL& namespace_entry_url, const AppCacheEntry& fallback_entry,
133 int64 cache_id, int64 group_id, const GURL& manifest_url);
135 WEBKIT_STORAGE_BROWSER_EXPORT AppCacheDiskCache* disk_cache();
137 // The directory in which we place files in the file system.
138 base::FilePath cache_directory_;
141 // This class operates primarily on the IO thread, but schedules
142 // its DatabaseTasks on the db thread. Separately, the disk_cache uses
144 scoped_refptr<base::MessageLoopProxy> db_thread_;
145 scoped_refptr<base::MessageLoopProxy> cache_thread_;
147 // Structures to keep track of DatabaseTasks that are in-flight.
148 DatabaseTaskQueue scheduled_database_tasks_;
149 PendingCacheLoads pending_cache_loads_;
150 PendingGroupLoads pending_group_loads_;
151 PendingForeignMarkings pending_foreign_markings_;
152 PendingQuotaQueries pending_quota_queries_;
154 // Structures to keep track of lazy response deletion.
155 std::deque<int64> deletable_response_ids_;
156 std::vector<int64> deleted_response_ids_;
157 bool is_response_deletion_scheduled_;
158 bool did_start_deleting_responses_;
159 int64 last_deletable_response_rowid_;
161 // Created on the IO thread, but only used on the DB thread.
162 AppCacheDatabase* database_;
164 // Set if we discover a fatal error like a corrupt SQL database or
165 // disk cache and cannot continue.
168 scoped_ptr<AppCacheDiskCache> disk_cache_;
170 // Used to short-circuit certain operations without having to schedule
171 // any tasks on the background database thread.
172 std::deque<base::Closure> pending_simple_tasks_;
173 base::WeakPtrFactory<AppCacheStorageImpl> weak_factory_;
175 friend class AppCacheStorageImplTest;
176 friend class content::ChromeAppCacheServiceTest;
179 } // namespace appcache
181 #endif // WEBKIT_BROWSER_APPCACHE_APPCACHE_STORAGE_IMPL_H_