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"
24 class AppCacheStorage
;
25 class AppCacheUpdateJob
;
28 // Collection of application caches identified by the same manifest URL.
29 // A group exists as long as it is in use by a host or is being updated.
30 class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheGroup
31 : public base::RefCounted
<AppCacheGroup
> {
34 class WEBKIT_STORAGE_BROWSER_EXPORT UpdateObserver
{
36 // Called just after an appcache update has completed.
37 virtual void OnUpdateComplete(AppCacheGroup
* group
) = 0;
38 virtual ~UpdateObserver() {}
47 AppCacheGroup(AppCacheStorage
* storage
, const GURL
& manifest_url
,
50 // Adds/removes an update observer, the AppCacheGroup does not take
51 // ownership of the observer.
52 void AddUpdateObserver(UpdateObserver
* observer
);
53 void RemoveUpdateObserver(UpdateObserver
* observer
);
55 int64
group_id() const { return group_id_
; }
56 const GURL
& manifest_url() const { return manifest_url_
; }
57 const base::Time
& creation_time() const { return creation_time_
; }
58 void set_creation_time(const base::Time
& time
) { creation_time_
= time
; }
59 bool is_obsolete() const { return is_obsolete_
; }
60 void set_obsolete(bool value
) { is_obsolete_
= value
; }
62 bool is_being_deleted() const { return is_being_deleted_
; }
63 void set_being_deleted(bool value
) { is_being_deleted_
= value
; }
65 AppCache
* newest_complete_cache() const { return newest_complete_cache_
; }
67 void AddCache(AppCache
* complete_cache
);
68 void RemoveCache(AppCache
* cache
);
69 bool HasCache() const { return newest_complete_cache_
!= NULL
; }
71 void AddNewlyDeletableResponseIds(std::vector
<int64
>* response_ids
);
73 UpdateStatus
update_status() const { return update_status_
; }
75 // Starts an update via update() javascript API.
77 StartUpdateWithHost(NULL
);
80 // Starts an update for a doc loaded from an application cache.
81 void StartUpdateWithHost(AppCacheHost
* host
) {
82 StartUpdateWithNewMasterEntry(host
, GURL());
85 // Starts an update for a doc loaded using HTTP GET or equivalent with
86 // an <html> tag manifest attribute value that matches this group's
88 void StartUpdateWithNewMasterEntry(AppCacheHost
* host
,
89 const GURL
& new_master_resource
);
91 // Cancels an update if one is running.
97 friend class AppCacheUpdateJob
;
98 friend class AppCacheUpdateJobTest
;
99 friend class base::RefCounted
<AppCacheGroup
>;
100 friend class MockAppCacheStorage
; // for old_caches()
104 typedef std::vector
<AppCache
*> Caches
;
105 typedef std::map
<AppCacheHost
*, GURL
> QueuedUpdates
;
107 static const int kUpdateRestartDelayMs
= 1000;
109 AppCacheUpdateJob
* update_job() { return update_job_
; }
110 void SetUpdateStatus(UpdateStatus status
);
112 void NotifyContentBlocked();
114 const Caches
& old_caches() const { return old_caches_
; }
116 // Update cannot be processed at this time. Queue it for a later run.
117 void QueueUpdate(AppCacheHost
* host
, const GURL
& new_master_resource
);
118 void RunQueuedUpdates();
119 bool FindObserver(UpdateObserver
* find_me
,
120 const ObserverList
<UpdateObserver
>& observer_list
);
121 void ScheduleUpdateRestart(int delay_ms
);
122 void HostDestructionImminent(AppCacheHost
* host
);
124 const int64 group_id_
;
125 const GURL manifest_url_
;
126 base::Time creation_time_
;
127 UpdateStatus update_status_
;
129 bool is_being_deleted_
;
130 std::vector
<int64
> newly_deletable_response_ids_
;
132 // Old complete app caches.
135 // Newest cache in this group to be complete, aka relevant cache.
136 AppCache
* newest_complete_cache_
;
138 // Current update job for this group, if any.
139 AppCacheUpdateJob
* update_job_
;
141 // Central storage object.
142 AppCacheStorage
* storage_
;
144 // List of objects observing this group.
145 ObserverList
<UpdateObserver
> observers_
;
147 // Updates that have been queued for the next run.
148 QueuedUpdates queued_updates_
;
149 ObserverList
<UpdateObserver
> queued_observers_
;
150 base::CancelableClosure restart_update_task_
;
151 scoped_ptr
<HostObserver
> host_observer_
;
153 // True if we're in our destructor.
156 FRIEND_TEST_ALL_PREFIXES(AppCacheGroupTest
, StartUpdate
);
157 FRIEND_TEST_ALL_PREFIXES(AppCacheGroupTest
, CancelUpdate
);
158 FRIEND_TEST_ALL_PREFIXES(AppCacheGroupTest
, QueueUpdate
);
159 FRIEND_TEST_ALL_PREFIXES(AppCacheUpdateJobTest
, AlreadyChecking
);
160 FRIEND_TEST_ALL_PREFIXES(AppCacheUpdateJobTest
, AlreadyDownloading
);
162 DISALLOW_COPY_AND_ASSIGN(AppCacheGroup
);
165 } // namespace appcache
167 #endif // WEBKIT_BROWSER_APPCACHE_APPCACHE_GROUP_H_