Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / appcache / appcache_update_job.h
blob4d2d896ea74485cbb2757e145de08748a547eca7
1 // Copyright (c) 2012 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 CONTENT_BROWSER_APPCACHE_APPCACHE_UPDATE_JOB_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_UPDATE_JOB_H_
8 #include <deque>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/time/time.h"
17 #include "content/browser/appcache/appcache.h"
18 #include "content/browser/appcache/appcache_host.h"
19 #include "content/browser/appcache/appcache_response.h"
20 #include "content/browser/appcache/appcache_service_impl.h"
21 #include "content/browser/appcache/appcache_storage.h"
22 #include "content/common/appcache_interfaces.h"
23 #include "content/common/content_export.h"
24 #include "net/base/completion_callback.h"
25 #include "net/http/http_response_headers.h"
26 #include "net/url_request/url_request.h"
27 #include "url/gurl.h"
29 namespace content {
30 FORWARD_DECLARE_TEST(AppCacheGroupTest, QueueUpdate);
31 class AppCacheGroupTest;
32 class AppCacheUpdateJobTest;
33 class HostNotifier;
35 // Application cache Update algorithm and state.
36 class CONTENT_EXPORT AppCacheUpdateJob
37 : public AppCacheStorage::Delegate,
38 public AppCacheHost::Observer,
39 public AppCacheServiceImpl::Observer {
40 public:
41 // Used for uma stats only for now, so new values are append only.
42 enum ResultType {
43 UPDATE_OK, DB_ERROR, DISKCACHE_ERROR, QUOTA_ERROR, REDIRECT_ERROR,
44 MANIFEST_ERROR, NETWORK_ERROR, SERVER_ERROR, CANCELLED_ERROR,
45 SECURITY_ERROR, NUM_UPDATE_JOB_RESULT_TYPES
48 AppCacheUpdateJob(AppCacheServiceImpl* service, AppCacheGroup* group);
49 ~AppCacheUpdateJob() override;
51 // Triggers the update process or adds more info if this update is already
52 // in progress.
53 void StartUpdate(AppCacheHost* host, const GURL& new_master_resource);
55 private:
56 friend class content::AppCacheGroupTest;
57 friend class content::AppCacheUpdateJobTest;
58 class URLFetcher;
60 // Master entries have multiple hosts, for example, the same page is opened
61 // in different tabs.
62 typedef std::vector<AppCacheHost*> PendingHosts;
63 typedef std::map<GURL, PendingHosts> PendingMasters;
64 typedef std::map<GURL, URLFetcher*> PendingUrlFetches;
65 typedef std::map<int64, GURL> LoadingResponses;
67 static const int kRerunDelayMs = 1000;
69 // TODO(michaeln): Rework the set of states vs update types vs stored states.
70 // The NO_UPDATE state is really more of an update type. For all update types
71 // storing the results is relevant.
73 enum UpdateType {
74 UNKNOWN_TYPE,
75 UPGRADE_ATTEMPT,
76 CACHE_ATTEMPT,
79 enum InternalUpdateState {
80 FETCH_MANIFEST,
81 NO_UPDATE,
82 DOWNLOADING,
84 // Every state after this comment indicates the update is terminating.
85 REFETCH_MANIFEST,
86 CACHE_FAILURE,
87 CANCELLED,
88 COMPLETED,
91 enum StoredState {
92 UNSTORED,
93 STORING,
94 STORED,
97 struct UrlToFetch {
98 UrlToFetch(const GURL& url, bool checked, AppCacheResponseInfo* info);
99 ~UrlToFetch();
101 GURL url;
102 bool storage_checked;
103 scoped_refptr<AppCacheResponseInfo> existing_response_info;
106 class URLFetcher : public net::URLRequest::Delegate {
107 public:
108 enum FetchType {
109 MANIFEST_FETCH,
110 URL_FETCH,
111 MASTER_ENTRY_FETCH,
112 MANIFEST_REFETCH,
114 URLFetcher(const GURL& url,
115 FetchType fetch_type,
116 AppCacheUpdateJob* job);
117 ~URLFetcher() override;
118 void Start();
119 FetchType fetch_type() const { return fetch_type_; }
120 net::URLRequest* request() const { return request_.get(); }
121 const AppCacheEntry& existing_entry() const { return existing_entry_; }
122 const std::string& manifest_data() const { return manifest_data_; }
123 AppCacheResponseWriter* response_writer() const {
124 return response_writer_.get();
126 void set_existing_response_headers(net::HttpResponseHeaders* headers) {
127 existing_response_headers_ = headers;
129 void set_existing_entry(const AppCacheEntry& entry) {
130 existing_entry_ = entry;
132 ResultType result() const { return result_; }
133 int redirect_response_code() const { return redirect_response_code_; }
135 private:
136 // URLRequest::Delegate overrides
137 void OnReceivedRedirect(net::URLRequest* request,
138 const net::RedirectInfo& redirect_info,
139 bool* defer_redirect) override;
140 void OnResponseStarted(net::URLRequest* request) override;
141 void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
143 void AddConditionalHeaders(const net::HttpResponseHeaders* headers);
144 void OnWriteComplete(int result);
145 void ReadResponseData();
146 bool ConsumeResponseData(int bytes_read);
147 void OnResponseCompleted();
148 bool MaybeRetryRequest();
150 GURL url_;
151 AppCacheUpdateJob* job_;
152 FetchType fetch_type_;
153 int retry_503_attempts_;
154 scoped_refptr<net::IOBuffer> buffer_;
155 scoped_ptr<net::URLRequest> request_;
156 AppCacheEntry existing_entry_;
157 scoped_refptr<net::HttpResponseHeaders> existing_response_headers_;
158 std::string manifest_data_;
159 ResultType result_;
160 int redirect_response_code_;
161 scoped_ptr<AppCacheResponseWriter> response_writer_;
162 }; // class URLFetcher
164 AppCacheResponseWriter* CreateResponseWriter();
166 // Methods for AppCacheStorage::Delegate.
167 void OnResponseInfoLoaded(AppCacheResponseInfo* response_info,
168 int64 response_id) override;
169 void OnGroupAndNewestCacheStored(AppCacheGroup* group,
170 AppCache* newest_cache,
171 bool success,
172 bool would_exceed_quota) override;
173 void OnGroupMadeObsolete(AppCacheGroup* group,
174 bool success,
175 int response_code) override;
177 // Methods for AppCacheHost::Observer.
178 void OnCacheSelectionComplete(AppCacheHost* host) override {} // N/A
179 void OnDestructionImminent(AppCacheHost* host) override;
181 // Methods for AppCacheServiceImpl::Observer.
182 void OnServiceReinitialized(AppCacheStorageReference* old_storage) override;
184 void HandleCacheFailure(const AppCacheErrorDetails& details,
185 ResultType result,
186 const GURL& failed_resource_url);
188 void FetchManifest(bool is_first_fetch);
189 void HandleManifestFetchCompleted(URLFetcher* fetcher);
190 void ContinueHandleManifestFetchCompleted(bool changed);
192 void HandleUrlFetchCompleted(URLFetcher* fetcher);
193 void HandleMasterEntryFetchCompleted(URLFetcher* fetcher);
195 void HandleManifestRefetchCompleted(URLFetcher* fetcher);
196 void OnManifestInfoWriteComplete(int result);
197 void OnManifestDataWriteComplete(int result);
199 void StoreGroupAndCache();
201 void NotifySingleHost(AppCacheHost* host, AppCacheEventID event_id);
202 void NotifyAllAssociatedHosts(AppCacheEventID event_id);
203 void NotifyAllProgress(const GURL& url);
204 void NotifyAllFinalProgress();
205 void NotifyAllError(const AppCacheErrorDetails& detals);
206 void LogConsoleMessageToAll(const std::string& message);
207 void AddAllAssociatedHostsToNotifier(HostNotifier* notifier);
209 // Checks if manifest is byte for byte identical with the manifest
210 // in the newest application cache.
211 void CheckIfManifestChanged();
212 void OnManifestDataReadComplete(int result);
214 // Creates the list of files that may need to be fetched and initiates
215 // fetches. Section 6.9.4 steps 12-17
216 void BuildUrlFileList(const AppCacheManifest& manifest);
217 void AddUrlToFileList(const GURL& url, int type);
218 void FetchUrls();
219 void CancelAllUrlFetches();
220 bool ShouldSkipUrlFetch(const AppCacheEntry& entry);
222 // If entry already exists in the cache currently being updated, merge
223 // the entry type information with the existing entry.
224 // Returns true if entry exists in cache currently being updated.
225 bool AlreadyFetchedEntry(const GURL& url, int entry_type);
227 // TODO(jennb): Delete when update no longer fetches master entries directly.
228 // Creates the list of master entries that need to be fetched and initiates
229 // fetches.
230 void AddMasterEntryToFetchList(AppCacheHost* host, const GURL& url,
231 bool is_new);
232 void FetchMasterEntries();
233 void CancelAllMasterEntryFetches(const AppCacheErrorDetails& details);
235 // Asynchronously loads the entry from the newest complete cache if the
236 // HTTP caching semantics allow.
237 // Returns false if immediately obvious that data cannot be loaded from
238 // newest complete cache.
239 bool MaybeLoadFromNewestCache(const GURL& url, AppCacheEntry& entry);
240 void LoadFromNewestCacheFailed(const GURL& url,
241 AppCacheResponseInfo* newest_response_info);
243 // Does nothing if update process is still waiting for pending master
244 // entries or URL fetches to complete downloading. Otherwise, completes
245 // the update process.
246 void MaybeCompleteUpdate();
248 // Schedules a rerun of the entire update with the same parameters as
249 // this update job after a short delay.
250 void ScheduleUpdateRetry(int delay_ms);
252 void Cancel();
253 void ClearPendingMasterEntries();
254 void DiscardInprogressCache();
255 void DiscardDuplicateResponses();
257 void LogHistogramStats(ResultType result, const GURL& failed_resource_url);
258 void MadeProgress() { last_progress_time_ = base::Time::Now(); }
260 // Deletes this object after letting the stack unwind.
261 void DeleteSoon();
263 bool IsTerminating() { return internal_state_ >= REFETCH_MANIFEST ||
264 stored_state_ != UNSTORED; }
266 AppCacheServiceImpl* service_;
267 const GURL manifest_url_; // here for easier access
269 // Defined prior to refs to AppCaches and Groups because destruction
270 // order matters, the disabled_storage_reference_ must outlive those
271 // objects.
272 scoped_refptr<AppCacheStorageReference> disabled_storage_reference_;
274 scoped_refptr<AppCache> inprogress_cache_;
276 AppCacheGroup* group_;
278 UpdateType update_type_;
279 InternalUpdateState internal_state_;
280 base::Time last_progress_time_;
282 PendingMasters pending_master_entries_;
283 size_t master_entries_completed_;
285 // TODO(jennb): Delete when update no longer fetches master entries directly.
286 // Helper containers to track which pending master entries have yet to be
287 // fetched and which are currently being fetched. Master entries that
288 // are listed in the manifest may be fetched as a regular URL instead of
289 // as a separate master entry fetch to optimize against duplicate fetches.
290 std::set<GURL> master_entries_to_fetch_;
291 PendingUrlFetches master_entry_fetches_;
293 // URLs of files to fetch along with their flags.
294 AppCache::EntryMap url_file_list_;
295 size_t url_fetches_completed_;
297 // Helper container to track which urls have not been fetched yet. URLs are
298 // removed when the fetch is initiated. Flag indicates whether an attempt
299 // to load the URL from storage has already been tried and failed.
300 std::deque<UrlToFetch> urls_to_fetch_;
302 // Helper container to track which urls are being loaded from response
303 // storage.
304 LoadingResponses loading_responses_;
306 // Keep track of pending URL requests so we can cancel them if necessary.
307 URLFetcher* manifest_fetcher_;
308 PendingUrlFetches pending_url_fetches_;
310 // Temporary storage of manifest response data for parsing and comparison.
311 std::string manifest_data_;
312 scoped_ptr<net::HttpResponseInfo> manifest_response_info_;
313 scoped_ptr<AppCacheResponseWriter> manifest_response_writer_;
314 scoped_refptr<net::IOBuffer> read_manifest_buffer_;
315 std::string loaded_manifest_data_;
316 scoped_ptr<AppCacheResponseReader> manifest_response_reader_;
317 bool manifest_has_valid_mime_type_;
319 // New master entries added to the cache by this job, used to cleanup
320 // in error conditions.
321 std::vector<GURL> added_master_entries_;
323 // Response ids stored by this update job, used to cleanup in
324 // error conditions.
325 std::vector<int64> stored_response_ids_;
327 // In some cases we fetch the same resource multiple times, and then
328 // have to delete the duplicates upon successful update. These ids
329 // are also in the stored_response_ids_ collection so we only schedule
330 // these for deletion on success.
331 // TODO(michaeln): Rework when we no longer fetches master entries directly.
332 std::vector<int64> duplicate_response_ids_;
334 // Whether we've stored the resulting group/cache yet.
335 StoredState stored_state_;
337 AppCacheStorage* storage_;
339 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest, QueueUpdate);
341 DISALLOW_COPY_AND_ASSIGN(AppCacheUpdateJob);
344 } // namespace content
346 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_UPDATE_JOB_H_