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_GROUP_H_
6 #define WEBKIT_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"
18 #include "webkit/browser/webkit_storage_browser_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
);
26 class AppCacheUpdateJobTest
;
27 class MockAppCacheStorage
;
34 class AppCacheStorage
;
35 class AppCacheUpdateJob
;
38 // Collection of application caches identified by the same manifest URL.
39 // A group exists as long as it is in use by a host or is being updated.
40 class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheGroup
41 : public base::RefCounted
<AppCacheGroup
> {
44 class WEBKIT_STORAGE_BROWSER_EXPORT UpdateObserver
{
46 // Called just after an appcache update has completed.
47 virtual void OnUpdateComplete(AppCacheGroup
* group
) = 0;
48 virtual ~UpdateObserver() {}
51 enum UpdateAppCacheStatus
{
57 AppCacheGroup(AppCacheStorage
* storage
, const GURL
& manifest_url
,
60 // Adds/removes an update observer, the AppCacheGroup does not take
61 // ownership of the observer.
62 void AddUpdateObserver(UpdateObserver
* observer
);
63 void RemoveUpdateObserver(UpdateObserver
* observer
);
65 int64
group_id() const { return group_id_
; }
66 const GURL
& manifest_url() const { return manifest_url_
; }
67 const base::Time
& creation_time() const { return creation_time_
; }
68 void set_creation_time(const base::Time
& time
) { creation_time_
= time
; }
69 bool is_obsolete() const { return is_obsolete_
; }
70 void set_obsolete(bool value
) { is_obsolete_
= value
; }
72 bool is_being_deleted() const { return is_being_deleted_
; }
73 void set_being_deleted(bool value
) { is_being_deleted_
= value
; }
75 AppCache
* newest_complete_cache() const { return newest_complete_cache_
; }
77 void AddCache(AppCache
* complete_cache
);
78 void RemoveCache(AppCache
* cache
);
79 bool HasCache() const { return newest_complete_cache_
!= NULL
; }
81 void AddNewlyDeletableResponseIds(std::vector
<int64
>* response_ids
);
83 UpdateAppCacheStatus
update_status() const { return update_status_
; }
85 // Starts an update via update() javascript API.
87 StartUpdateWithHost(NULL
);
90 // Starts an update for a doc loaded from an application cache.
91 void StartUpdateWithHost(AppCacheHost
* host
) {
92 StartUpdateWithNewMasterEntry(host
, GURL());
95 // Starts an update for a doc loaded using HTTP GET or equivalent with
96 // an <html> tag manifest attribute value that matches this group's
98 void StartUpdateWithNewMasterEntry(AppCacheHost
* host
,
99 const GURL
& new_master_resource
);
101 // Cancels an update if one is running.
107 friend class base::RefCounted
<AppCacheGroup
>;
108 friend class content::AppCacheUpdateJobTest
;
109 friend class content::MockAppCacheStorage
; // for old_caches()
110 friend class AppCacheUpdateJob
;
114 typedef std::vector
<AppCache
*> Caches
;
115 typedef std::map
<AppCacheHost
*, GURL
> QueuedUpdates
;
117 static const int kUpdateRestartDelayMs
= 1000;
119 AppCacheUpdateJob
* update_job() { return update_job_
; }
120 void SetUpdateAppCacheStatus(UpdateAppCacheStatus status
);
122 void NotifyContentBlocked();
124 const Caches
& old_caches() const { return old_caches_
; }
126 // Update cannot be processed at this time. Queue it for a later run.
127 void QueueUpdate(AppCacheHost
* host
, const GURL
& new_master_resource
);
128 void RunQueuedUpdates();
129 bool FindObserver(UpdateObserver
* find_me
,
130 const ObserverList
<UpdateObserver
>& observer_list
);
131 void ScheduleUpdateRestart(int delay_ms
);
132 void HostDestructionImminent(AppCacheHost
* host
);
134 const int64 group_id_
;
135 const GURL manifest_url_
;
136 base::Time creation_time_
;
137 UpdateAppCacheStatus update_status_
;
139 bool is_being_deleted_
;
140 std::vector
<int64
> newly_deletable_response_ids_
;
142 // Old complete app caches.
145 // Newest cache in this group to be complete, aka relevant cache.
146 AppCache
* newest_complete_cache_
;
148 // Current update job for this group, if any.
149 AppCacheUpdateJob
* update_job_
;
151 // Central storage object.
152 AppCacheStorage
* storage_
;
154 // List of objects observing this group.
155 ObserverList
<UpdateObserver
> observers_
;
157 // Updates that have been queued for the next run.
158 QueuedUpdates queued_updates_
;
159 ObserverList
<UpdateObserver
> queued_observers_
;
160 base::CancelableClosure restart_update_task_
;
161 scoped_ptr
<HostObserver
> host_observer_
;
163 // True if we're in our destructor.
166 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, StartUpdate
);
167 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, CancelUpdate
);
168 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, QueueUpdate
);
169 FRIEND_TEST_ALL_PREFIXES(content::AppCacheUpdateJobTest
, AlreadyChecking
);
170 FRIEND_TEST_ALL_PREFIXES(content::AppCacheUpdateJobTest
, AlreadyDownloading
);
172 DISALLOW_COPY_AND_ASSIGN(AppCacheGroup
);
175 } // namespace appcache
177 #endif // WEBKIT_BROWSER_APPCACHE_APPCACHE_GROUP_H_