Home | History | Annotate | Download | only in appcache
      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 WEBKIT_BROWSER_APPCACHE_APPCACHE_GROUP_H_
      6 #define WEBKIT_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 "url/gurl.h"
     18 #include "webkit/browser/webkit_storage_browser_export.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 AppCacheUpdateJobTest;
     27 class MockAppCacheStorage;
     28 }
     29 
     30 namespace appcache {
     31 
     32 class AppCache;
     33 class AppCacheHost;
     34 class AppCacheStorage;
     35 class AppCacheUpdateJob;
     36 class HostObserver;
     37 
     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> {
     42  public:
     43 
     44   class WEBKIT_STORAGE_BROWSER_EXPORT UpdateObserver {
     45     public:
     46       // Called just after an appcache update has completed.
     47       virtual void OnUpdateComplete(AppCacheGroup* group) = 0;
     48       virtual ~UpdateObserver() {}
     49   };
     50 
     51   enum UpdateAppCacheStatus {
     52     IDLE,
     53     CHECKING,
     54     DOWNLOADING,
     55   };
     56 
     57   AppCacheGroup(AppCacheStorage* storage, const GURL& manifest_url,
     58                 int64 group_id);
     59 
     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);
     64 
     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; }
     71 
     72   bool is_being_deleted() const { return is_being_deleted_; }
     73   void set_being_deleted(bool value) { is_being_deleted_ = value; }
     74 
     75   AppCache* newest_complete_cache() const { return newest_complete_cache_; }
     76 
     77   void AddCache(AppCache* complete_cache);
     78   void RemoveCache(AppCache* cache);
     79   bool HasCache() const { return newest_complete_cache_ != NULL; }
     80 
     81   void AddNewlyDeletableResponseIds(std::vector<int64>* response_ids);
     82 
     83   UpdateAppCacheStatus update_status() const { return update_status_; }
     84 
     85   // Starts an update via update() javascript API.
     86   void StartUpdate() {
     87     StartUpdateWithHost(NULL);
     88   }
     89 
     90   // Starts an update for a doc loaded from an application cache.
     91   void StartUpdateWithHost(AppCacheHost* host)  {
     92     StartUpdateWithNewMasterEntry(host, GURL());
     93   }
     94 
     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
     97   // manifest url.
     98   void StartUpdateWithNewMasterEntry(AppCacheHost* host,
     99                                      const GURL& new_master_resource);
    100 
    101   // Cancels an update if one is running.
    102   void CancelUpdate();
    103 
    104  private:
    105   class HostObserver;
    106 
    107   friend class base::RefCounted<AppCacheGroup>;
    108   friend class content::AppCacheUpdateJobTest;
    109   friend class content::MockAppCacheStorage;  // for old_caches()
    110   friend class AppCacheUpdateJob;
    111 
    112   ~AppCacheGroup();
    113 
    114   typedef std::vector<AppCache*> Caches;
    115   typedef std::map<AppCacheHost*, GURL> QueuedUpdates;
    116 
    117   static const int kUpdateRestartDelayMs = 1000;
    118 
    119   AppCacheUpdateJob* update_job() { return update_job_; }
    120   void SetUpdateAppCacheStatus(UpdateAppCacheStatus status);
    121 
    122   void NotifyContentBlocked();
    123 
    124   const Caches& old_caches() const { return old_caches_; }
    125 
    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);
    133 
    134   const int64 group_id_;
    135   const GURL manifest_url_;
    136   base::Time creation_time_;
    137   UpdateAppCacheStatus update_status_;
    138   bool is_obsolete_;
    139   bool is_being_deleted_;
    140   std::vector<int64> newly_deletable_response_ids_;
    141 
    142   // Old complete app caches.
    143   Caches old_caches_;
    144 
    145   // Newest cache in this group to be complete, aka relevant cache.
    146   AppCache* newest_complete_cache_;
    147 
    148   // Current update job for this group, if any.
    149   AppCacheUpdateJob* update_job_;
    150 
    151   // Central storage object.
    152   AppCacheStorage* storage_;
    153 
    154   // List of objects observing this group.
    155   ObserverList<UpdateObserver> observers_;
    156 
    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_;
    162 
    163   // True if we're in our destructor.
    164   bool is_in_dtor_;
    165 
    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);
    171 
    172   DISALLOW_COPY_AND_ASSIGN(AppCacheGroup);
    173 };
    174 
    175 }  // namespace appcache
    176 
    177 #endif  // WEBKIT_BROWSER_APPCACHE_APPCACHE_GROUP_H_
    178