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_
11 #include "base/cancelable_callback.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/time/time.h"
17 #include "content/common/content_export.h"
21 FORWARD_DECLARE_TEST(AppCacheGroupTest
, StartUpdate
);
22 FORWARD_DECLARE_TEST(AppCacheGroupTest
, CancelUpdate
);
23 FORWARD_DECLARE_TEST(AppCacheGroupTest
, QueueUpdate
);
24 FORWARD_DECLARE_TEST(AppCacheUpdateJobTest
, AlreadyChecking
);
25 FORWARD_DECLARE_TEST(AppCacheUpdateJobTest
, AlreadyDownloading
);
28 class AppCacheStorage
;
29 class AppCacheUpdateJob
;
30 class AppCacheUpdateJobTest
;
32 class MockAppCacheStorage
;
34 // Collection of application caches identified by the same manifest URL.
35 // A group exists as long as it is in use by a host or is being updated.
36 class CONTENT_EXPORT AppCacheGroup
37 : public base::RefCounted
<AppCacheGroup
> {
40 class CONTENT_EXPORT UpdateObserver
{
42 // Called just after an appcache update has completed.
43 virtual void OnUpdateComplete(AppCacheGroup
* group
) = 0;
44 virtual ~UpdateObserver() {}
47 enum UpdateAppCacheStatus
{
53 AppCacheGroup(AppCacheStorage
* storage
, const GURL
& manifest_url
,
56 // Adds/removes an update observer, the AppCacheGroup does not take
57 // ownership of the observer.
58 void AddUpdateObserver(UpdateObserver
* observer
);
59 void RemoveUpdateObserver(UpdateObserver
* observer
);
61 int64
group_id() const { return group_id_
; }
62 const GURL
& manifest_url() const { return manifest_url_
; }
63 base::Time
creation_time() const { return creation_time_
; }
64 void set_creation_time(base::Time time
) { creation_time_
= time
; }
65 bool is_obsolete() const { return is_obsolete_
; }
66 void set_obsolete(bool value
) { is_obsolete_
= value
; }
67 bool is_being_deleted() const { return is_being_deleted_
; }
68 void set_being_deleted(bool value
) { is_being_deleted_
= value
; }
69 base::Time
last_full_update_check_time() const {
70 return last_full_update_check_time_
;
72 void set_last_full_update_check_time(base::Time time
) {
73 last_full_update_check_time_
= time
;
75 base::Time
first_evictable_error_time() const {
76 return first_evictable_error_time_
;
78 void set_first_evictable_error_time(base::Time time
) {
79 first_evictable_error_time_
= time
;
82 AppCache
* newest_complete_cache() const { return newest_complete_cache_
; }
84 void AddCache(AppCache
* complete_cache
);
85 void RemoveCache(AppCache
* cache
);
86 bool HasCache() const { return newest_complete_cache_
!= NULL
; }
88 void AddNewlyDeletableResponseIds(std::vector
<int64
>* response_ids
);
90 UpdateAppCacheStatus
update_status() const { return update_status_
; }
92 // Starts an update via update() javascript API.
94 StartUpdateWithHost(NULL
);
97 // Starts an update for a doc loaded from an application cache.
98 void StartUpdateWithHost(AppCacheHost
* host
) {
99 StartUpdateWithNewMasterEntry(host
, GURL());
102 // Starts an update for a doc loaded using HTTP GET or equivalent with
103 // an <html> tag manifest attribute value that matches this group's
105 void StartUpdateWithNewMasterEntry(AppCacheHost
* host
,
106 const GURL
& new_master_resource
);
108 // Cancels an update if one is running.
114 friend class base::RefCounted
<AppCacheGroup
>;
115 friend class content::AppCacheUpdateJobTest
;
116 friend class content::MockAppCacheStorage
; // for old_caches()
117 friend class AppCacheUpdateJob
;
121 typedef std::vector
<AppCache
*> Caches
;
122 typedef std::map
<AppCacheHost
*, GURL
> QueuedUpdates
;
124 static const int kUpdateRestartDelayMs
= 1000;
126 AppCacheUpdateJob
* update_job() { return update_job_
; }
127 void SetUpdateAppCacheStatus(UpdateAppCacheStatus status
);
129 void NotifyContentBlocked();
131 const Caches
& old_caches() const { return old_caches_
; }
133 // Update cannot be processed at this time. Queue it for a later run.
134 void QueueUpdate(AppCacheHost
* host
, const GURL
& new_master_resource
);
135 void RunQueuedUpdates();
136 static bool FindObserver(
137 const UpdateObserver
* find_me
,
138 const base::ObserverList
<UpdateObserver
>& observer_list
);
139 void ScheduleUpdateRestart(int delay_ms
);
140 void HostDestructionImminent(AppCacheHost
* host
);
142 const int64 group_id_
;
143 const GURL manifest_url_
;
144 base::Time creation_time_
;
145 UpdateAppCacheStatus update_status_
;
147 bool is_being_deleted_
;
148 std::vector
<int64
> newly_deletable_response_ids_
;
150 // Most update checks respect the cache control headers of the manifest
151 // resource, but we bypass the http cache for a "full" update check after 24
152 // hours since the last full check was successfully performed.
153 base::Time last_full_update_check_time_
;
155 // Groups that fail to update for a sufficiently long time are evicted. This
156 // value is reset after a successful update or update check.
157 base::Time first_evictable_error_time_
;
159 // Old complete app caches.
162 // Newest cache in this group to be complete, aka relevant cache.
163 AppCache
* newest_complete_cache_
;
165 // Current update job for this group, if any.
166 AppCacheUpdateJob
* update_job_
;
168 // Central storage object.
169 AppCacheStorage
* storage_
;
171 // List of objects observing this group.
172 base::ObserverList
<UpdateObserver
> observers_
;
174 // Updates that have been queued for the next run.
175 QueuedUpdates queued_updates_
;
176 base::ObserverList
<UpdateObserver
> queued_observers_
;
177 base::CancelableClosure restart_update_task_
;
178 scoped_ptr
<HostObserver
> host_observer_
;
180 // True if we're in our destructor.
183 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, StartUpdate
);
184 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, CancelUpdate
);
185 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, QueueUpdate
);
186 FRIEND_TEST_ALL_PREFIXES(content::AppCacheUpdateJobTest
, AlreadyChecking
);
187 FRIEND_TEST_ALL_PREFIXES(content::AppCacheUpdateJobTest
, AlreadyDownloading
);
189 DISALLOW_COPY_AND_ASSIGN(AppCacheGroup
);
192 } // namespace content
194 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_GROUP_H_