• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_GROUP_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_GROUP_H_
7 
8 #include <map>
9 #include <vector>
10 
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"
18 #include "url/gurl.h"
19 
20 namespace content {
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 AppCache;
27 class AppCacheHost;
28 class AppCacheStorage;
29 class AppCacheUpdateJob;
30 class AppCacheUpdateJobTest;
31 class HostObserver;
32 class MockAppCacheStorage;
33 
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> {
38  public:
39 
40   class CONTENT_EXPORT UpdateObserver {
41     public:
42       // Called just after an appcache update has completed.
43       virtual void OnUpdateComplete(AppCacheGroup* group) = 0;
~UpdateObserver()44       virtual ~UpdateObserver() {}
45   };
46 
47   enum UpdateAppCacheStatus {
48     IDLE,
49     CHECKING,
50     DOWNLOADING,
51   };
52 
53   AppCacheGroup(AppCacheStorage* storage, const GURL& manifest_url,
54                 int64 group_id);
55 
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);
60 
group_id()61   int64 group_id() const { return group_id_; }
manifest_url()62   const GURL& manifest_url() const { return manifest_url_; }
creation_time()63   const base::Time& creation_time() const { return creation_time_; }
set_creation_time(const base::Time & time)64   void set_creation_time(const base::Time& time) { creation_time_ = time; }
is_obsolete()65   bool is_obsolete() const { return is_obsolete_; }
set_obsolete(bool value)66   void set_obsolete(bool value) { is_obsolete_ = value; }
67 
is_being_deleted()68   bool is_being_deleted() const { return is_being_deleted_; }
set_being_deleted(bool value)69   void set_being_deleted(bool value) { is_being_deleted_ = value; }
70 
newest_complete_cache()71   AppCache* newest_complete_cache() const { return newest_complete_cache_; }
72 
73   void AddCache(AppCache* complete_cache);
74   void RemoveCache(AppCache* cache);
HasCache()75   bool HasCache() const { return newest_complete_cache_ != NULL; }
76 
77   void AddNewlyDeletableResponseIds(std::vector<int64>* response_ids);
78 
update_status()79   UpdateAppCacheStatus update_status() const { return update_status_; }
80 
81   // Starts an update via update() javascript API.
StartUpdate()82   void StartUpdate() {
83     StartUpdateWithHost(NULL);
84   }
85 
86   // Starts an update for a doc loaded from an application cache.
StartUpdateWithHost(AppCacheHost * host)87   void StartUpdateWithHost(AppCacheHost* host)  {
88     StartUpdateWithNewMasterEntry(host, GURL());
89   }
90 
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
93   // manifest url.
94   void StartUpdateWithNewMasterEntry(AppCacheHost* host,
95                                      const GURL& new_master_resource);
96 
97   // Cancels an update if one is running.
98   void CancelUpdate();
99 
100  private:
101   class HostObserver;
102 
103   friend class base::RefCounted<AppCacheGroup>;
104   friend class content::AppCacheUpdateJobTest;
105   friend class content::MockAppCacheStorage;  // for old_caches()
106   friend class AppCacheUpdateJob;
107 
108   ~AppCacheGroup();
109 
110   typedef std::vector<AppCache*> Caches;
111   typedef std::map<AppCacheHost*, GURL> QueuedUpdates;
112 
113   static const int kUpdateRestartDelayMs = 1000;
114 
update_job()115   AppCacheUpdateJob* update_job() { return update_job_; }
116   void SetUpdateAppCacheStatus(UpdateAppCacheStatus status);
117 
118   void NotifyContentBlocked();
119 
old_caches()120   const Caches& old_caches() const { return old_caches_; }
121 
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);
129 
130   const int64 group_id_;
131   const GURL manifest_url_;
132   base::Time creation_time_;
133   UpdateAppCacheStatus update_status_;
134   bool is_obsolete_;
135   bool is_being_deleted_;
136   std::vector<int64> newly_deletable_response_ids_;
137 
138   // Old complete app caches.
139   Caches old_caches_;
140 
141   // Newest cache in this group to be complete, aka relevant cache.
142   AppCache* newest_complete_cache_;
143 
144   // Current update job for this group, if any.
145   AppCacheUpdateJob* update_job_;
146 
147   // Central storage object.
148   AppCacheStorage* storage_;
149 
150   // List of objects observing this group.
151   ObserverList<UpdateObserver> observers_;
152 
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_;
158 
159   // True if we're in our destructor.
160   bool is_in_dtor_;
161 
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);
167 
168   DISALLOW_COPY_AND_ASSIGN(AppCacheGroup);
169 };
170 
171 }  // namespace content
172 
173 #endif  // CONTENT_BROWSER_APPCACHE_APPCACHE_GROUP_H_
174