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 appcache {
     21 
     22 class AppCache;
     23 class AppCacheHost;
     24 class AppCacheStorage;
     25 class AppCacheUpdateJob;
     26 class HostObserver;
     27 
     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> {
     32  public:
     33 
     34   class WEBKIT_STORAGE_BROWSER_EXPORT UpdateObserver {
     35     public:
     36       // Called just after an appcache update has completed.
     37       virtual void OnUpdateComplete(AppCacheGroup* group) = 0;
     38       virtual ~UpdateObserver() {}
     39   };
     40 
     41   enum UpdateStatus {
     42     IDLE,
     43     CHECKING,
     44     DOWNLOADING,
     45   };
     46 
     47   AppCacheGroup(AppCacheStorage* storage, const GURL& manifest_url,
     48                 int64 group_id);
     49 
     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);
     54 
     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; }
     61 
     62   bool is_being_deleted() const { return is_being_deleted_; }
     63   void set_being_deleted(bool value) { is_being_deleted_ = value; }
     64 
     65   AppCache* newest_complete_cache() const { return newest_complete_cache_; }
     66 
     67   void AddCache(AppCache* complete_cache);
     68   void RemoveCache(AppCache* cache);
     69   bool HasCache() const { return newest_complete_cache_ != NULL; }
     70 
     71   void AddNewlyDeletableResponseIds(std::vector<int64>* response_ids);
     72 
     73   UpdateStatus update_status() const { return update_status_; }
     74 
     75   // Starts an update via update() javascript API.
     76   void StartUpdate() {
     77     StartUpdateWithHost(NULL);
     78   }
     79 
     80   // Starts an update for a doc loaded from an application cache.
     81   void StartUpdateWithHost(AppCacheHost* host)  {
     82     StartUpdateWithNewMasterEntry(host, GURL());
     83   }
     84 
     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
     87   // manifest url.
     88   void StartUpdateWithNewMasterEntry(AppCacheHost* host,
     89                                      const GURL& new_master_resource);
     90 
     91   // Cancels an update if one is running.
     92   void CancelUpdate();
     93 
     94  private:
     95   class HostObserver;
     96 
     97   friend class AppCacheUpdateJob;
     98   friend class AppCacheUpdateJobTest;
     99   friend class base::RefCounted<AppCacheGroup>;
    100   friend class MockAppCacheStorage;  // for old_caches()
    101 
    102   ~AppCacheGroup();
    103 
    104   typedef std::vector<AppCache*> Caches;
    105   typedef std::map<AppCacheHost*, GURL> QueuedUpdates;
    106 
    107   static const int kUpdateRestartDelayMs = 1000;
    108 
    109   AppCacheUpdateJob* update_job() { return update_job_; }
    110   void SetUpdateStatus(UpdateStatus status);
    111 
    112   void NotifyContentBlocked();
    113 
    114   const Caches& old_caches() const { return old_caches_; }
    115 
    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);
    123 
    124   const int64 group_id_;
    125   const GURL manifest_url_;
    126   base::Time creation_time_;
    127   UpdateStatus update_status_;
    128   bool is_obsolete_;
    129   bool is_being_deleted_;
    130   std::vector<int64> newly_deletable_response_ids_;
    131 
    132   // Old complete app caches.
    133   Caches old_caches_;
    134 
    135   // Newest cache in this group to be complete, aka relevant cache.
    136   AppCache* newest_complete_cache_;
    137 
    138   // Current update job for this group, if any.
    139   AppCacheUpdateJob* update_job_;
    140 
    141   // Central storage object.
    142   AppCacheStorage* storage_;
    143 
    144   // List of objects observing this group.
    145   ObserverList<UpdateObserver> observers_;
    146 
    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_;
    152 
    153   // True if we're in our destructor.
    154   bool is_in_dtor_;
    155 
    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);
    161 
    162   DISALLOW_COPY_AND_ASSIGN(AppCacheGroup);
    163 };
    164 
    165 }  // namespace appcache
    166 
    167 #endif  // WEBKIT_BROWSER_APPCACHE_APPCACHE_GROUP_H_
    168