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_GROUP_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_GROUP_H_
12 #include "base/cancelable_callback.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/time/time.h"
18 #include "content/common/content_export.h"
22 FORWARD_DECLARE_TEST(AppCacheGroupTest
, StartUpdate
);
23 FORWARD_DECLARE_TEST(AppCacheGroupTest
, CancelUpdate
);
24 FORWARD_DECLARE_TEST(AppCacheGroupTest
, QueueUpdate
);
25 FORWARD_DECLARE_TEST(AppCacheUpdateJobTest
, AlreadyChecking
);
26 FORWARD_DECLARE_TEST(AppCacheUpdateJobTest
, AlreadyDownloading
);
29 class AppCacheStorage
;
30 class AppCacheUpdateJob
;
31 class AppCacheUpdateJobTest
;
33 class MockAppCacheStorage
;
35 // Collection of application caches identified by the same manifest URL.
36 // A group exists as long as it is in use by a host or is being updated.
37 class CONTENT_EXPORT AppCacheGroup
38 : public base::RefCounted
<AppCacheGroup
> {
41 class CONTENT_EXPORT UpdateObserver
{
43 // Called just after an appcache update has completed.
44 virtual void OnUpdateComplete(AppCacheGroup
* group
) = 0;
45 virtual ~UpdateObserver() {}
48 enum UpdateAppCacheStatus
{
54 AppCacheGroup(AppCacheStorage
* storage
, const GURL
& manifest_url
,
57 // Adds/removes an update observer, the AppCacheGroup does not take
58 // ownership of the observer.
59 void AddUpdateObserver(UpdateObserver
* observer
);
60 void RemoveUpdateObserver(UpdateObserver
* observer
);
62 int64
group_id() const { return group_id_
; }
63 const GURL
& manifest_url() const { return manifest_url_
; }
64 base::Time
creation_time() const { return creation_time_
; }
65 void set_creation_time(base::Time time
) { creation_time_
= time
; }
66 bool is_obsolete() const { return is_obsolete_
; }
67 void set_obsolete(bool value
) { is_obsolete_
= value
; }
68 bool is_being_deleted() const { return is_being_deleted_
; }
69 void set_being_deleted(bool value
) { is_being_deleted_
= value
; }
70 base::Time
last_full_update_check_time() const {
71 return last_full_update_check_time_
;
73 void set_last_full_update_check_time(base::Time time
) {
74 last_full_update_check_time_
= time
;
76 base::Time
first_evictable_error_time() const {
77 return first_evictable_error_time_
;
79 void set_first_evictable_error_time(base::Time time
) {
80 first_evictable_error_time_
= time
;
83 AppCache
* newest_complete_cache() const { return newest_complete_cache_
; }
85 void AddCache(AppCache
* complete_cache
);
86 void RemoveCache(AppCache
* cache
);
87 bool HasCache() const { return newest_complete_cache_
!= NULL
; }
89 void AddNewlyDeletableResponseIds(std::vector
<int64
>* response_ids
);
91 UpdateAppCacheStatus
update_status() const { return update_status_
; }
93 // Starts an update via update() javascript API.
95 StartUpdateWithHost(NULL
);
98 // Starts an update for a doc loaded from an application cache.
99 void StartUpdateWithHost(AppCacheHost
* host
) {
100 StartUpdateWithNewMasterEntry(host
, GURL());
103 // Starts an update for a doc loaded using HTTP GET or equivalent with
104 // an <html> tag manifest attribute value that matches this group's
106 void StartUpdateWithNewMasterEntry(AppCacheHost
* host
,
107 const GURL
& new_master_resource
);
109 // Cancels an update if one is running.
115 friend class base::RefCounted
<AppCacheGroup
>;
116 friend class content::AppCacheUpdateJobTest
;
117 friend class content::MockAppCacheStorage
; // for old_caches()
118 friend class AppCacheUpdateJob
;
122 typedef std::vector
<AppCache
*> Caches
;
123 typedef std::map
<UpdateObserver
*, std::pair
<AppCacheHost
*, GURL
>>
126 static const int kUpdateRestartDelayMs
= 1000;
128 AppCacheUpdateJob
* update_job() { return update_job_
; }
129 void SetUpdateAppCacheStatus(UpdateAppCacheStatus status
);
131 void NotifyContentBlocked();
133 const Caches
& old_caches() const { return old_caches_
; }
135 // Update cannot be processed at this time. Queue it for a later run.
136 void QueueUpdate(AppCacheHost
* host
, const GURL
& new_master_resource
);
137 void RunQueuedUpdates();
138 static bool FindObserver(
139 const UpdateObserver
* find_me
,
140 const base::ObserverList
<UpdateObserver
>& observer_list
);
141 void ScheduleUpdateRestart(int delay_ms
);
142 void HostDestructionImminent(AppCacheHost
* host
);
144 const int64 group_id_
;
145 const GURL manifest_url_
;
146 base::Time creation_time_
;
147 UpdateAppCacheStatus update_status_
;
149 bool is_being_deleted_
;
150 std::vector
<int64
> newly_deletable_response_ids_
;
152 // Most update checks respect the cache control headers of the manifest
153 // resource, but we bypass the http cache for a "full" update check after 24
154 // hours since the last full check was successfully performed.
155 base::Time last_full_update_check_time_
;
157 // Groups that fail to update for a sufficiently long time are evicted. This
158 // value is reset after a successful update or update check.
159 base::Time first_evictable_error_time_
;
161 // Old complete app caches.
164 // Newest cache in this group to be complete, aka relevant cache.
165 AppCache
* newest_complete_cache_
;
167 // Current update job for this group, if any.
168 AppCacheUpdateJob
* update_job_
;
170 // Central storage object.
171 AppCacheStorage
* storage_
;
173 // List of objects observing this group.
174 base::ObserverList
<UpdateObserver
> observers_
;
176 // Updates that have been queued for the next run.
177 QueuedUpdates queued_updates_
;
178 base::ObserverList
<UpdateObserver
> queued_observers_
;
179 base::CancelableClosure restart_update_task_
;
180 scoped_ptr
<HostObserver
> host_observer_
;
182 // True if we're in our destructor.
185 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, StartUpdate
);
186 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, CancelUpdate
);
187 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, QueueUpdate
);
188 FRIEND_TEST_ALL_PREFIXES(content::AppCacheUpdateJobTest
, AlreadyChecking
);
189 FRIEND_TEST_ALL_PREFIXES(content::AppCacheUpdateJobTest
, AlreadyDownloading
);
191 DISALLOW_COPY_AND_ASSIGN(AppCacheGroup
);
194 } // namespace content
196 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_GROUP_H_