| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WEBKIT_APPCACHE_APPCACHE_UPDATE_JOB_H_ | 5 #ifndef WEBKIT_APPCACHE_APPCACHE_UPDATE_JOB_H_ |
| 6 #define WEBKIT_APPCACHE_APPCACHE_UPDATE_JOB_H_ | 6 #define WEBKIT_APPCACHE_APPCACHE_UPDATE_JOB_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| 11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
| 12 #include "base/task.h" | 12 #include "base/task.h" |
| 13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 14 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
| 15 #include "webkit/appcache/appcache.h" | 15 #include "webkit/appcache/appcache.h" |
| 16 #include "webkit/appcache/appcache_interfaces.h" | 16 #include "webkit/appcache/appcache_interfaces.h" |
| 17 #include "webkit/appcache/appcache_storage.h" |
| 17 | 18 |
| 18 namespace appcache { | 19 namespace appcache { |
| 19 | 20 |
| 20 struct UpdateJobInfo; | 21 class UpdateJobInfo; |
| 21 | 22 |
| 22 // Application cache Update algorithm and state. | 23 // Application cache Update algorithm and state. |
| 23 class AppCacheUpdateJob : public URLRequest::Delegate { | 24 class AppCacheUpdateJob : public URLRequest::Delegate, |
| 25 public AppCacheStorage::Delegate { |
| 24 public: | 26 public: |
| 25 AppCacheUpdateJob(AppCacheService* service, AppCacheGroup* group); | 27 AppCacheUpdateJob(AppCacheService* service, AppCacheGroup* group); |
| 26 ~AppCacheUpdateJob(); | 28 ~AppCacheUpdateJob(); |
| 27 | 29 |
| 28 // Triggers the update process or adds more info if this update is already | 30 // Triggers the update process or adds more info if this update is already |
| 29 // in progress. | 31 // in progress. |
| 30 void StartUpdate(AppCacheHost* host, const GURL& new_master_resource); | 32 void StartUpdate(AppCacheHost* host, const GURL& new_master_resource); |
| 31 | 33 |
| 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: | 34 private: |
| 37 friend class ScopedRunnableMethodFactory<AppCacheUpdateJob>; | 35 friend class ScopedRunnableMethodFactory<AppCacheUpdateJob>; |
| 38 friend class AppCacheUpdateJobTest; | 36 friend class AppCacheUpdateJobTest; |
| 37 friend class UpdateJobInfo; |
| 39 | 38 |
| 40 // Master entries have multiple hosts, for example, the same page is opened | 39 // Master entries have multiple hosts, for example, the same page is opened |
| 41 // in different tabs. | 40 // in different tabs. |
| 42 // TODO(jennb): detect when hosts are deleted | 41 // TODO(jennb): detect when hosts are deleted |
| 43 typedef std::vector<AppCacheHost*> PendingHosts; | 42 typedef std::vector<AppCacheHost*> PendingHosts; |
| 44 typedef std::map<GURL, PendingHosts> PendingMasters; | 43 typedef std::map<GURL, PendingHosts> PendingMasters; |
| 45 typedef std::map<GURL, URLRequest*> PendingUrlFetches; | 44 typedef std::map<GURL, URLRequest*> PendingUrlFetches; |
| 46 typedef std::pair<GURL, bool> UrlToFetch; // flag TRUE if storage checked | 45 typedef std::pair<GURL, bool> UrlToFetch; // flag TRUE if storage checked |
| 47 | 46 |
| 48 static const int kRerunDelayMs = 1000; | 47 static const int kRerunDelayMs = 1000; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 64 }; | 63 }; |
| 65 | 64 |
| 66 // Methods for URLRequest::Delegate. | 65 // Methods for URLRequest::Delegate. |
| 67 void OnResponseStarted(URLRequest* request); | 66 void OnResponseStarted(URLRequest* request); |
| 68 void OnReadCompleted(URLRequest* request, int bytes_read); | 67 void OnReadCompleted(URLRequest* request, int bytes_read); |
| 69 void OnReceivedRedirect(URLRequest* request, | 68 void OnReceivedRedirect(URLRequest* request, |
| 70 const GURL& new_url, | 69 const GURL& new_url, |
| 71 bool* defer_redirect); | 70 bool* defer_redirect); |
| 72 // TODO(jennb): any other delegate callbacks to handle? certificate? | 71 // TODO(jennb): any other delegate callbacks to handle? certificate? |
| 73 | 72 |
| 73 // Methods for AppCacheStorage::Delegate. |
| 74 void OnGroupAndNewestCacheStored(AppCacheGroup* group, bool success); |
| 75 void OnGroupMadeObsolete(AppCacheGroup* group, bool success); |
| 76 |
| 74 void FetchManifest(bool is_first_fetch); | 77 void FetchManifest(bool is_first_fetch); |
| 75 | 78 |
| 76 void OnResponseCompleted(URLRequest* request); | 79 void OnResponseCompleted(URLRequest* request); |
| 77 | 80 |
| 78 // Retries a 503 request with retry-after header of 0. | 81 // Retries a 503 request with retry-after header of 0. |
| 79 // Returns true if request should be retried and deletes original request. | 82 // Returns true if request should be retried and deletes original request. |
| 80 bool RetryRequest(URLRequest* request); | 83 bool RetryRequest(URLRequest* request); |
| 81 | 84 |
| 82 void ReadResponseData(URLRequest* request); | 85 void ReadResponseData(URLRequest* request); |
| 83 | 86 |
| 84 // Returns false if response data is processed asynchronously, in which | 87 // Returns false if response data is processed asynchronously, in which |
| 85 // case a callback will be invoked when it is safe to continue reading | 88 // case ReadResponseData will be invoked when it is safe to continue |
| 86 // more response data from the request. | 89 // reading more response data from the request. |
| 87 bool ConsumeResponseData(URLRequest* request, | 90 bool ConsumeResponseData(URLRequest* request, |
| 88 UpdateJobInfo* info, | 91 UpdateJobInfo* info, |
| 89 int bytes_read); | 92 int bytes_read); |
| 93 void OnWriteResponseComplete(int result, URLRequest* request, |
| 94 UpdateJobInfo* info); |
| 90 | 95 |
| 91 void HandleManifestFetchCompleted(URLRequest* request); | 96 void HandleManifestFetchCompleted(URLRequest* request); |
| 92 void ContinueHandleManifestFetchCompleted(bool changed); | 97 void ContinueHandleManifestFetchCompleted(bool changed); |
| 98 |
| 93 void HandleUrlFetchCompleted(URLRequest* request); | 99 void HandleUrlFetchCompleted(URLRequest* request); |
| 100 |
| 94 void HandleManifestRefetchCompleted(URLRequest* request); | 101 void HandleManifestRefetchCompleted(URLRequest* request); |
| 102 void OnManifestInfoWriteComplete(int result); |
| 103 void OnManifestDataWriteComplete(int result); |
| 104 void CompleteInprogressCache(); |
| 105 void HandleManifestRefetchFailure(); |
| 95 | 106 |
| 96 void NotifySingleHost(AppCacheHost* host, EventID event_id); | 107 void NotifySingleHost(AppCacheHost* host, EventID event_id); |
| 97 void NotifyAllPendingMasterHosts(EventID event_id); | 108 void NotifyAllPendingMasterHosts(EventID event_id); |
| 98 void NotifyAllAssociatedHosts(EventID event_id); | 109 void NotifyAllAssociatedHosts(EventID event_id); |
| 99 | 110 |
| 100 // Checks if manifest is byte for byte identical with the manifest | 111 // Checks if manifest is byte for byte identical with the manifest |
| 101 // in the newest application cache. | 112 // in the newest application cache. |
| 102 void CheckIfManifestChanged(); | 113 void CheckIfManifestChanged(); |
| 103 void ContinueCheckIfManifestChanged(const std::string& loaded_manifest); | 114 void ContinueCheckIfManifestChanged(const std::string& loaded_manifest); |
| 104 | 115 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 AppCacheService* service_; | 161 AppCacheService* service_; |
| 151 | 162 |
| 152 scoped_refptr<AppCache> inprogress_cache_; | 163 scoped_refptr<AppCache> inprogress_cache_; |
| 153 | 164 |
| 154 // Hold a reference to the newly created cache until this update is | 165 // Hold a reference to the newly created cache until this update is |
| 155 // destroyed. The host that started the update will add a reference to the | 166 // destroyed. The host that started the update will add a reference to the |
| 156 // new cache when notified that the update is complete. AppCacheGroup sends | 167 // new cache when notified that the update is complete. AppCacheGroup sends |
| 157 // the notification when its status is set to IDLE in ~AppCacheUpdateJob. | 168 // the notification when its status is set to IDLE in ~AppCacheUpdateJob. |
| 158 scoped_refptr<AppCache> protect_new_cache_; | 169 scoped_refptr<AppCache> protect_new_cache_; |
| 159 | 170 |
| 171 // Hold a reference to the group's newest cache (prior to update) in order |
| 172 // to restore the group's newest cache if storage fails. |
| 173 scoped_refptr<AppCache> protect_former_newest_cache_; |
| 174 |
| 160 AppCacheGroup* group_; | 175 AppCacheGroup* group_; |
| 161 | 176 |
| 162 UpdateType update_type_; | 177 UpdateType update_type_; |
| 163 InternalUpdateState internal_state_; | 178 InternalUpdateState internal_state_; |
| 164 | 179 |
| 165 PendingMasters pending_master_entries_; | 180 PendingMasters pending_master_entries_; |
| 166 size_t master_entries_completed_; | 181 size_t master_entries_completed_; |
| 167 | 182 |
| 168 // URLs of files to fetch along with their flags. | 183 // URLs of files to fetch along with their flags. |
| 169 AppCache::EntryMap url_file_list_; | 184 AppCache::EntryMap url_file_list_; |
| 170 size_t url_fetches_completed_; | 185 size_t url_fetches_completed_; |
| 171 | 186 |
| 172 // Helper container to track which urls have not been fetched yet. URLs are | 187 // Helper container to track which urls have not been fetched yet. URLs are |
| 173 // removed when the fetch is initiated. Flag indicates whether an attempt | 188 // removed when the fetch is initiated. Flag indicates whether an attempt |
| 174 // to load the URL from storage has already been tried and failed. | 189 // to load the URL from storage has already been tried and failed. |
| 175 std::deque<UrlToFetch> urls_to_fetch_; | 190 std::deque<UrlToFetch> urls_to_fetch_; |
| 176 | 191 |
| 177 // Keep track of pending URL requests so we can cancel them if necessary. | 192 // Keep track of pending URL requests so we can cancel them if necessary. |
| 178 URLRequest* manifest_url_request_; | 193 URLRequest* manifest_url_request_; |
| 179 PendingUrlFetches pending_url_fetches_; | 194 PendingUrlFetches pending_url_fetches_; |
| 180 | 195 |
| 181 // Temporary storage of manifest response data for parsing and comparison. | 196 // Temporary storage of manifest response data for parsing and comparison. |
| 182 std::string manifest_data_; | 197 std::string manifest_data_; |
| 183 std::string manifest_refetch_data_; | 198 std::string manifest_refetch_data_; |
| 199 scoped_ptr<net::HttpResponseInfo> manifest_response_info_; |
| 200 scoped_ptr<AppCacheResponseWriter> manifest_response_writer_; |
| 201 |
| 202 net::CompletionCallbackImpl<AppCacheUpdateJob> manifest_info_write_callback_; |
| 203 net::CompletionCallbackImpl<AppCacheUpdateJob> manifest_data_write_callback_; |
| 184 | 204 |
| 185 // TODO(jennb): delete when able to mock storage behavior | 205 // TODO(jennb): delete when able to mock storage behavior |
| 186 bool simulate_manifest_changed_; | 206 bool simulate_manifest_changed_; |
| 187 | 207 |
| 188 DISALLOW_COPY_AND_ASSIGN(AppCacheUpdateJob); | 208 DISALLOW_COPY_AND_ASSIGN(AppCacheUpdateJob); |
| 189 }; | 209 }; |
| 190 | 210 |
| 191 } // namespace appcache | 211 } // namespace appcache |
| 192 | 212 |
| 193 #endif // WEBKIT_APPCACHE_APPCACHE_UPDATE_JOB_H_ | 213 #endif // WEBKIT_APPCACHE_APPCACHE_UPDATE_JOB_H_ |
| OLD | NEW |