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 const base::Time
& creation_time() const { return creation_time_
; }
64 void set_creation_time(const base::Time
& time
) { creation_time_
= time
; }
65 bool is_obsolete() const { return is_obsolete_
; }
66 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
; }
71 AppCache
* newest_complete_cache() const { return newest_complete_cache_
; }
73 void AddCache(AppCache
* complete_cache
);
74 void RemoveCache(AppCache
* cache
);
75 bool HasCache() const { return newest_complete_cache_
!= NULL
; }
77 void AddNewlyDeletableResponseIds(std::vector
<int64
>* response_ids
);
79 UpdateAppCacheStatus
update_status() const { return update_status_
; }
81 // Starts an update via update() javascript API.
83 StartUpdateWithHost(NULL
);
86 // Starts an update for a doc loaded from an application cache.
87 void StartUpdateWithHost(AppCacheHost
* host
) {
88 StartUpdateWithNewMasterEntry(host
, GURL());
91 // Starts an update for a doc loaded using HTTP GET or equivalent with
92 // an <html> tag manifest attribute value that matches this group's
94 void StartUpdateWithNewMasterEntry(AppCacheHost
* host
,
95 const GURL
& new_master_resource
);
97 // Cancels an update if one is running.
103 friend class base::RefCounted
<AppCacheGroup
>;
104 friend class content::AppCacheUpdateJobTest
;
105 friend class content::MockAppCacheStorage
; // for old_caches()
106 friend class AppCacheUpdateJob
;
110 typedef std::vector
<AppCache
*> Caches
;
111 typedef std::map
<AppCacheHost
*, GURL
> QueuedUpdates
;
113 static const int kUpdateRestartDelayMs
= 1000;
115 AppCacheUpdateJob
* update_job() { return update_job_
; }
116 void SetUpdateAppCacheStatus(UpdateAppCacheStatus status
);
118 void NotifyContentBlocked();
120 const Caches
& old_caches() const { return old_caches_
; }
122 // Update cannot be processed at this time. Queue it for a later run.
123 void QueueUpdate(AppCacheHost
* host
, const GURL
& new_master_resource
);
124 void RunQueuedUpdates();
125 bool FindObserver(UpdateObserver
* find_me
,
126 const ObserverList
<UpdateObserver
>& observer_list
);
127 void ScheduleUpdateRestart(int delay_ms
);
128 void HostDestructionImminent(AppCacheHost
* host
);
130 const int64 group_id_
;
131 const GURL manifest_url_
;
132 base::Time creation_time_
;
133 UpdateAppCacheStatus update_status_
;
135 bool is_being_deleted_
;
136 std::vector
<int64
> newly_deletable_response_ids_
;
138 // Old complete app caches.
141 // Newest cache in this group to be complete, aka relevant cache.
142 AppCache
* newest_complete_cache_
;
144 // Current update job for this group, if any.
145 AppCacheUpdateJob
* update_job_
;
147 // Central storage object.
148 AppCacheStorage
* storage_
;
150 // List of objects observing this group.
151 ObserverList
<UpdateObserver
> observers_
;
153 // Updates that have been queued for the next run.
154 QueuedUpdates queued_updates_
;
155 ObserverList
<UpdateObserver
> queued_observers_
;
156 base::CancelableClosure restart_update_task_
;
157 scoped_ptr
<HostObserver
> host_observer_
;
159 // True if we're in our destructor.
162 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, StartUpdate
);
163 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, CancelUpdate
);
164 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, QueueUpdate
);
165 FRIEND_TEST_ALL_PREFIXES(content::AppCacheUpdateJobTest
, AlreadyChecking
);
166 FRIEND_TEST_ALL_PREFIXES(content::AppCacheUpdateJobTest
, AlreadyDownloading
);
168 DISALLOW_COPY_AND_ASSIGN(AppCacheGroup
);
171 } // namespace content
173 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_GROUP_H_