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 CONTENT_BROWSER_APPCACHE_APPCACHE_STORAGE_IMPL_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_STORAGE_IMPL_H_
14 #include "base/callback.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/timer/timer.h"
19 #include "content/browser/appcache/appcache_database.h"
20 #include "content/browser/appcache/appcache_disk_cache.h"
21 #include "content/browser/appcache/appcache_storage.h"
22 #include "content/common/content_export.h"
25 class SingleThreadTaskRunner
;
29 class AppCacheStorageImplTest
;
30 class ChromeAppCacheServiceTest
;
32 class AppCacheStorageImpl
: public AppCacheStorage
{
34 explicit AppCacheStorageImpl(AppCacheServiceImpl
* service
);
35 ~AppCacheStorageImpl() override
;
38 const base::FilePath
& cache_directory
,
39 const scoped_refptr
<base::SingleThreadTaskRunner
>& db_thread
,
40 const scoped_refptr
<base::SingleThreadTaskRunner
>& cache_thread
);
42 bool is_disabled() const { return is_disabled_
; }
44 // AppCacheStorage methods, see the base class for doc comments.
45 void GetAllInfo(Delegate
* delegate
) override
;
46 void LoadCache(int64 id
, Delegate
* delegate
) override
;
47 void LoadOrCreateGroup(const GURL
& manifest_url
, Delegate
* delegate
) override
;
48 void StoreGroupAndNewestCache(AppCacheGroup
* group
,
49 AppCache
* newest_cache
,
50 Delegate
* delegate
) override
;
51 void FindResponseForMainRequest(const GURL
& url
,
52 const GURL
& preferred_manifest_url
,
53 Delegate
* delegate
) override
;
54 void FindResponseForSubRequest(AppCache
* cache
,
56 AppCacheEntry
* found_entry
,
57 AppCacheEntry
* found_fallback_entry
,
58 bool* found_network_namespace
) override
;
59 void MarkEntryAsForeign(const GURL
& entry_url
, int64 cache_id
) override
;
60 void MakeGroupObsolete(AppCacheGroup
* group
,
62 int response_code
) override
;
63 void StoreEvictionTimes(AppCacheGroup
* group
) override
;
64 AppCacheResponseReader
* CreateResponseReader(const GURL
& manifest_url
,
66 int64 response_id
) override
;
67 AppCacheResponseWriter
* CreateResponseWriter(const GURL
& manifest_url
,
68 int64 group_id
) override
;
69 AppCacheResponseMetadataWriter
* CreateResponseMetadataWriter(
71 int64 response_id
) override
;
72 void DoomResponses(const GURL
& manifest_url
,
73 const std::vector
<int64
>& response_ids
) override
;
74 void DeleteResponses(const GURL
& manifest_url
,
75 const std::vector
<int64
>& response_ids
) override
;
78 // The AppCacheStorageImpl class methods and datamembers may only be
79 // accessed on the IO thread. This class manufactures seperate DatabaseTasks
80 // which access the DB on a seperate background thread.
83 class DisableDatabaseTask
;
85 class StoreOrLoadTask
;
88 class StoreGroupAndCacheTask
;
89 class FindMainResponseTask
;
90 class MarkEntryAsForeignTask
;
91 class MakeGroupObsoleteTask
;
92 class GetDeletableResponseIdsTask
;
93 class InsertDeletableResponseIdsTask
;
94 class DeleteDeletableResponseIdsTask
;
95 class LazyUpdateLastAccessTimeTask
;
96 class CommitLastAccessTimesTask
;
97 class UpdateEvictionTimesTask
;
99 typedef std::deque
<DatabaseTask
*> DatabaseTaskQueue
;
100 typedef std::map
<int64
, CacheLoadTask
*> PendingCacheLoads
;
101 typedef std::map
<GURL
, GroupLoadTask
*> PendingGroupLoads
;
102 typedef std::deque
<std::pair
<GURL
, int64
> > PendingForeignMarkings
;
103 typedef std::set
<StoreGroupAndCacheTask
*> PendingQuotaQueries
;
105 bool IsInitTaskComplete() {
106 return last_cache_id_
!= AppCacheStorage::kUnitializedId
;
109 CacheLoadTask
* GetPendingCacheLoadTask(int64 cache_id
);
110 GroupLoadTask
* GetPendingGroupLoadTask(const GURL
& manifest_url
);
111 void GetPendingForeignMarkingsForCache(
112 int64 cache_id
, std::vector
<GURL
>* urls
);
114 void ScheduleSimpleTask(const base::Closure
& task
);
115 void RunOnePendingSimpleTask();
117 void DelayedStartDeletingUnusedResponses();
118 void StartDeletingResponses(const std::vector
<int64
>& response_ids
);
119 void ScheduleDeleteOneResponse();
120 void DeleteOneResponse();
121 void OnDeletedOneResponse(int rv
);
122 void OnDiskCacheInitialized(int rv
);
123 void DeleteAndStartOver();
124 void DeleteAndStartOverPart2();
125 void CallScheduleReinitialize();
126 void LazilyCommitLastAccessTimes();
127 void OnLazyCommitTimer();
129 // Sometimes we can respond without having to query the database.
130 bool FindResponseForMainRequestInGroup(
131 AppCacheGroup
* group
, const GURL
& url
, Delegate
* delegate
);
132 void DeliverShortCircuitedFindMainResponse(
134 const AppCacheEntry
& found_entry
,
135 scoped_refptr
<AppCacheGroup
> group
,
136 scoped_refptr
<AppCache
> newest_cache
,
137 scoped_refptr
<DelegateReference
> delegate_ref
);
139 void CallOnMainResponseFound(
140 DelegateReferenceVector
* delegates
,
141 const GURL
& url
, const AppCacheEntry
& entry
,
142 const GURL
& namespace_entry_url
, const AppCacheEntry
& fallback_entry
,
143 int64 cache_id
, int64 group_id
, const GURL
& manifest_url
);
145 CONTENT_EXPORT AppCacheDiskCache
* disk_cache();
147 // The directory in which we place files in the file system.
148 base::FilePath cache_directory_
;
151 // This class operates primarily on the IO thread, but schedules
152 // its DatabaseTasks on the db thread. Separately, the disk_cache uses
154 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread_
;
155 scoped_refptr
<base::SingleThreadTaskRunner
> cache_thread_
;
157 // Structures to keep track of DatabaseTasks that are in-flight.
158 DatabaseTaskQueue scheduled_database_tasks_
;
159 PendingCacheLoads pending_cache_loads_
;
160 PendingGroupLoads pending_group_loads_
;
161 PendingForeignMarkings pending_foreign_markings_
;
162 PendingQuotaQueries pending_quota_queries_
;
164 // Structures to keep track of lazy response deletion.
165 std::deque
<int64
> deletable_response_ids_
;
166 std::vector
<int64
> deleted_response_ids_
;
167 bool is_response_deletion_scheduled_
;
168 bool did_start_deleting_responses_
;
169 int64 last_deletable_response_rowid_
;
171 // Created on the IO thread, but only used on the DB thread.
172 AppCacheDatabase
* database_
;
174 // Set if we discover a fatal error like a corrupt SQL database or
175 // disk cache and cannot continue.
178 scoped_ptr
<AppCacheDiskCache
> disk_cache_
;
179 base::OneShotTimer
<AppCacheStorageImpl
> lazy_commit_timer_
;
181 // Used to short-circuit certain operations without having to schedule
182 // any tasks on the background database thread.
183 std::deque
<base::Closure
> pending_simple_tasks_
;
184 base::WeakPtrFactory
<AppCacheStorageImpl
> weak_factory_
;
186 friend class content::AppCacheStorageImplTest
;
187 friend class content::ChromeAppCacheServiceTest
;
190 } // namespace content
192 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_STORAGE_IMPL_H_