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 AppCacheResponseReader
* CreateResponseReader(const GURL
& manifest_url
,
65 int64 response_id
) override
;
66 AppCacheResponseWriter
* CreateResponseWriter(const GURL
& manifest_url
,
67 int64 group_id
) override
;
68 AppCacheResponseMetadataWriter
* CreateResponseMetadataWriter(
70 int64 response_id
) override
;
71 void DoomResponses(const GURL
& manifest_url
,
72 const std::vector
<int64
>& response_ids
) override
;
73 void DeleteResponses(const GURL
& manifest_url
,
74 const std::vector
<int64
>& response_ids
) override
;
77 // The AppCacheStorageImpl class methods and datamembers may only be
78 // accessed on the IO thread. This class manufactures seperate DatabaseTasks
79 // which access the DB on a seperate background thread.
82 class DisableDatabaseTask
;
84 class StoreOrLoadTask
;
87 class StoreGroupAndCacheTask
;
88 class FindMainResponseTask
;
89 class MarkEntryAsForeignTask
;
90 class MakeGroupObsoleteTask
;
91 class GetDeletableResponseIdsTask
;
92 class InsertDeletableResponseIdsTask
;
93 class DeleteDeletableResponseIdsTask
;
94 class LazyUpdateLastAccessTimeTask
;
95 class CommitLastAccessTimesTask
;
97 typedef std::deque
<DatabaseTask
*> DatabaseTaskQueue
;
98 typedef std::map
<int64
, CacheLoadTask
*> PendingCacheLoads
;
99 typedef std::map
<GURL
, GroupLoadTask
*> PendingGroupLoads
;
100 typedef std::deque
<std::pair
<GURL
, int64
> > PendingForeignMarkings
;
101 typedef std::set
<StoreGroupAndCacheTask
*> PendingQuotaQueries
;
103 bool IsInitTaskComplete() {
104 return last_cache_id_
!= AppCacheStorage::kUnitializedId
;
107 CacheLoadTask
* GetPendingCacheLoadTask(int64 cache_id
);
108 GroupLoadTask
* GetPendingGroupLoadTask(const GURL
& manifest_url
);
109 void GetPendingForeignMarkingsForCache(
110 int64 cache_id
, std::vector
<GURL
>* urls
);
112 void ScheduleSimpleTask(const base::Closure
& task
);
113 void RunOnePendingSimpleTask();
115 void DelayedStartDeletingUnusedResponses();
116 void StartDeletingResponses(const std::vector
<int64
>& response_ids
);
117 void ScheduleDeleteOneResponse();
118 void DeleteOneResponse();
119 void OnDeletedOneResponse(int rv
);
120 void OnDiskCacheInitialized(int rv
);
121 void DeleteAndStartOver();
122 void DeleteAndStartOverPart2();
123 void CallScheduleReinitialize();
124 void LazilyCommitLastAccessTimes();
125 void OnLazyCommitTimer();
127 // Sometimes we can respond without having to query the database.
128 bool FindResponseForMainRequestInGroup(
129 AppCacheGroup
* group
, const GURL
& url
, Delegate
* delegate
);
130 void DeliverShortCircuitedFindMainResponse(
132 const AppCacheEntry
& found_entry
,
133 scoped_refptr
<AppCacheGroup
> group
,
134 scoped_refptr
<AppCache
> newest_cache
,
135 scoped_refptr
<DelegateReference
> delegate_ref
);
137 void CallOnMainResponseFound(
138 DelegateReferenceVector
* delegates
,
139 const GURL
& url
, const AppCacheEntry
& entry
,
140 const GURL
& namespace_entry_url
, const AppCacheEntry
& fallback_entry
,
141 int64 cache_id
, int64 group_id
, const GURL
& manifest_url
);
143 CONTENT_EXPORT AppCacheDiskCache
* disk_cache();
145 // The directory in which we place files in the file system.
146 base::FilePath cache_directory_
;
149 // This class operates primarily on the IO thread, but schedules
150 // its DatabaseTasks on the db thread. Separately, the disk_cache uses
152 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread_
;
153 scoped_refptr
<base::SingleThreadTaskRunner
> cache_thread_
;
155 // Structures to keep track of DatabaseTasks that are in-flight.
156 DatabaseTaskQueue scheduled_database_tasks_
;
157 PendingCacheLoads pending_cache_loads_
;
158 PendingGroupLoads pending_group_loads_
;
159 PendingForeignMarkings pending_foreign_markings_
;
160 PendingQuotaQueries pending_quota_queries_
;
162 // Structures to keep track of lazy response deletion.
163 std::deque
<int64
> deletable_response_ids_
;
164 std::vector
<int64
> deleted_response_ids_
;
165 bool is_response_deletion_scheduled_
;
166 bool did_start_deleting_responses_
;
167 int64 last_deletable_response_rowid_
;
169 // Created on the IO thread, but only used on the DB thread.
170 AppCacheDatabase
* database_
;
172 // Set if we discover a fatal error like a corrupt SQL database or
173 // disk cache and cannot continue.
176 scoped_ptr
<AppCacheDiskCache
> disk_cache_
;
177 base::OneShotTimer
<AppCacheStorageImpl
> lazy_commit_timer_
;
179 // Used to short-circuit certain operations without having to schedule
180 // any tasks on the background database thread.
181 std::deque
<base::Closure
> pending_simple_tasks_
;
182 base::WeakPtrFactory
<AppCacheStorageImpl
> weak_factory_
;
184 friend class content::AppCacheStorageImplTest
;
185 friend class content::ChromeAppCacheServiceTest
;
188 } // namespace content
190 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_STORAGE_IMPL_H_