| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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_APPCACHE_APPCACHE_UPDATE_JOB_H_ |
| 6 #define WEBKIT_APPCACHE_APPCACHE_UPDATE_JOB_H_ |
| 7 |
| 8 #include <deque> |
| 9 #include <map> |
| 10 |
| 11 #include "base/ref_counted.h" |
| 12 #include "base/task.h" |
| 13 #include "googleurl/src/gurl.h" |
| 14 #include "net/url_request/url_request.h" |
| 15 #include "webkit/appcache/appcache.h" |
| 16 #include "webkit/appcache/appcache_interfaces.h" |
| 17 |
| 18 namespace appcache { |
| 19 |
| 20 struct UpdateJobInfo; |
| 21 |
| 22 // Application cache Update algorithm and state. |
| 23 class AppCacheUpdateJob : public URLRequest::Delegate { |
| 24 public: |
| 25 AppCacheUpdateJob(AppCacheService* service, AppCacheGroup* group); |
| 26 ~AppCacheUpdateJob(); |
| 27 |
| 28 // Triggers the update process or adds more info if this update is already |
| 29 // in progress. |
| 30 void StartUpdate(AppCacheHost* host, const GURL& new_master_resource); |
| 31 |
| 32 |
| 33 // TODO(jennb): add callback method for writing data to storage |
| 34 // TODO(jennb): add callback method for reading data from storage |
| 35 |
| 36 private: |
| 37 friend class ScopedRunnableMethodFactory<AppCacheUpdateJob>; |
| 38 friend class AppCacheUpdateJobTest; |
| 39 |
| 40 // Master entries have multiple hosts, for example, the same page is opened |
| 41 // in different tabs. |
| 42 // TODO(jennb): detect when hosts are deleted |
| 43 typedef std::vector<AppCacheHost*> PendingHosts; |
| 44 typedef std::map<GURL, PendingHosts> PendingMasters; |
| 45 typedef std::map<GURL, URLRequest*> PendingUrlFetches; |
| 46 typedef std::pair<GURL, bool> UrlToFetch; // flag TRUE if storage checked |
| 47 |
| 48 static const int kRerunDelayMs = 1000; |
| 49 |
| 50 enum UpdateType { |
| 51 UNKNOWN_TYPE, |
| 52 UPGRADE_ATTEMPT, |
| 53 CACHE_ATTEMPT, |
| 54 }; |
| 55 |
| 56 enum InternalUpdateState { |
| 57 FETCH_MANIFEST, |
| 58 NO_UPDATE, |
| 59 DOWNLOADING, |
| 60 REFETCH_MANIFEST, |
| 61 CACHE_FAILURE, |
| 62 CANCELLED, |
| 63 }; |
| 64 |
| 65 // Methods for URLRequest::Delegate. |
| 66 void OnResponseStarted(URLRequest* request); |
| 67 void OnReadCompleted(URLRequest* request, int bytes_read); |
| 68 void OnReceivedRedirect(URLRequest* request, |
| 69 const GURL& new_url, |
| 70 bool* defer_redirect); |
| 71 // TODO(jennb): any other delegate callbacks to handle? certificate? |
| 72 |
| 73 void FetchManifest(bool is_first_fetch); |
| 74 |
| 75 void OnResponseCompleted(URLRequest* request); |
| 76 |
| 77 void ReadResponseData(URLRequest* request); |
| 78 |
| 79 // Returns false if response data is processed asynchronously, in which |
| 80 // case a callback will be invoked when it is safe to continue reading |
| 81 // more response data from the request. |
| 82 bool ConsumeResponseData(URLRequest* request, |
| 83 UpdateJobInfo* info, |
| 84 int bytes_read); |
| 85 |
| 86 void HandleManifestFetchCompleted(URLRequest* request); |
| 87 void ContinueHandleManifestFetchCompleted(bool changed); |
| 88 void HandleUrlFetchCompleted(URLRequest* request); |
| 89 void HandleManifestRefetchCompleted(URLRequest* request); |
| 90 |
| 91 void NotifySingleHost(AppCacheHost* host, EventID event_id); |
| 92 void NotifyAllPendingMasterHosts(EventID event_id); |
| 93 void NotifyAllAssociatedHosts(EventID event_id); |
| 94 |
| 95 // Checks if manifest is byte for byte identical with the manifest |
| 96 // in the newest application cache. |
| 97 void CheckIfManifestChanged(); |
| 98 void ContinueCheckIfManifestChanged(const std::string& loaded_manifest); |
| 99 |
| 100 // TODO(jennb): delete when able to mock storage behavior |
| 101 void SimulateManifestChanged(bool changed) { |
| 102 simulate_manifest_changed_ = changed; |
| 103 } |
| 104 |
| 105 // Creates the list of files that may need to be fetched and initiates |
| 106 // fetches. Section 6.9.4 steps 12-17 |
| 107 void BuildUrlFileList(const Manifest& manifest); |
| 108 void AddUrlToFileList(const GURL& url, int type); |
| 109 void FetchUrls(); |
| 110 bool ShouldSkipUrlFetch(const AppCacheEntry& entry); |
| 111 |
| 112 // Asynchronously loads the entry from the newest complete cache if the |
| 113 // HTTP caching semantics allow. |
| 114 // Returns false if immediately obvious that data cannot be loaded from |
| 115 // newest complete cache. |
| 116 bool MaybeLoadFromNewestCache(const GURL& url, AppCacheEntry& entry); |
| 117 |
| 118 // TODO(jennb): delete me after storage code added |
| 119 void SimulateFailedLoadFromNewestCache(const GURL& url); |
| 120 |
| 121 // Copies the data from src entry to dest entry and adds the modified |
| 122 // entry to the inprogress cache. |
| 123 void CopyEntryToCache(const GURL& url, const AppCacheEntry& src, |
| 124 AppCacheEntry* dest); |
| 125 |
| 126 // Does nothing if update process is still waiting for pending master |
| 127 // entries or URL fetches to complete downloading. Otherwise, completes |
| 128 // the update process. |
| 129 // Returns true if update process is completed. |
| 130 bool MaybeCompleteUpdate(); |
| 131 |
| 132 // Runs the cache failure steps after all pending master entry downloads |
| 133 // have completed. |
| 134 void HandleCacheFailure(); |
| 135 |
| 136 // Schedules a rerun of the entire update with the same parameters as |
| 137 // this update job after a short delay. |
| 138 void ScheduleUpdateRetry(int delay_ms); |
| 139 |
| 140 void Cancel(); |
| 141 |
| 142 // Deletes this object after letting the stack unwind. |
| 143 void DeleteSoon(); |
| 144 |
| 145 // This factory will be used to schedule invocations of various methods. |
| 146 ScopedRunnableMethodFactory<AppCacheUpdateJob> method_factory_; |
| 147 |
| 148 GURL manifest_url_; // here for easier access |
| 149 AppCacheService* service_; |
| 150 scoped_refptr<AppCache> inprogress_cache_; |
| 151 scoped_refptr<AppCacheGroup> group_; |
| 152 |
| 153 UpdateType update_type_; |
| 154 InternalUpdateState internal_state_; |
| 155 |
| 156 PendingMasters pending_master_entries_; |
| 157 size_t master_entries_completed_; |
| 158 |
| 159 // URLs of files to fetch along with their flags. |
| 160 AppCache::EntryMap url_file_list_; |
| 161 size_t url_fetches_completed_; |
| 162 |
| 163 // Helper container to track which urls have not been fetched yet. URLs are |
| 164 // removed when the fetch is initiated. Flag indicates whether an attempt |
| 165 // to load the URL from storage has already been tried and failed. |
| 166 std::deque<UrlToFetch> urls_to_fetch_; |
| 167 |
| 168 // Keep track of pending URL requests so we can cancel them if necessary. |
| 169 URLRequest* manifest_url_request_; |
| 170 PendingUrlFetches pending_url_fetches_; |
| 171 |
| 172 // Temporary storage of manifest response data for parsing and comparison. |
| 173 std::string manifest_data_; |
| 174 std::string manifest_refetch_data_; |
| 175 |
| 176 // TODO(jennb): delete when able to mock storage behavior |
| 177 bool simulate_manifest_changed_; |
| 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(AppCacheUpdateJob); |
| 180 }; |
| 181 |
| 182 } // namespace appcache |
| 183 |
| 184 #endif // WEBKIT_APPCACHE_APPCACHE_UPDATE_JOB_H_ |
| OLD | NEW |