| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "webkit/appcache/appcache_request_handler.h" | 5 #include "webkit/appcache/appcache_request_handler.h" |
| 6 | 6 |
| 7 #include "net/url_request/url_request.h" | 7 #include "net/url_request/url_request.h" |
| 8 #include "net/url_request/url_request_job.h" | 8 #include "net/url_request/url_request_job.h" |
| 9 #include "webkit/appcache/appcache.h" | 9 #include "webkit/appcache/appcache.h" |
| 10 #include "webkit/appcache/appcache_policy.h" | 10 #include "webkit/appcache/appcache_policy.h" |
| 11 #include "webkit/appcache/appcache_url_request_job.h" | 11 #include "webkit/appcache/appcache_url_request_job.h" |
| 12 | 12 |
| 13 namespace appcache { | 13 namespace appcache { |
| 14 | 14 |
| 15 AppCacheRequestHandler::AppCacheRequestHandler(AppCacheHost* host, | 15 AppCacheRequestHandler::AppCacheRequestHandler( |
| 16 ResourceType::Type resource_type) | 16 AppCacheHost* host, ResourceType::Type resource_type) |
| 17 : host_(host), resource_type_(resource_type), | 17 : host_(host), resource_type_(resource_type), |
| 18 is_waiting_for_cache_selection_(false), found_cache_id_(0), | 18 is_waiting_for_cache_selection_(false), found_group_id_(0), |
| 19 found_network_namespace_(false), cache_entry_not_found_(false) { | 19 found_cache_id_(0), found_network_namespace_(false), |
| 20 cache_entry_not_found_(false) { |
| 20 DCHECK(host_); | 21 DCHECK(host_); |
| 21 host_->AddObserver(this); | 22 host_->AddObserver(this); |
| 22 } | 23 } |
| 23 | 24 |
| 24 AppCacheRequestHandler::~AppCacheRequestHandler() { | 25 AppCacheRequestHandler::~AppCacheRequestHandler() { |
| 25 if (host_) { | 26 if (host_) { |
| 26 storage()->CancelDelegateCallbacks(this); | 27 storage()->CancelDelegateCallbacks(this); |
| 27 host_->RemoveObserver(this); | 28 host_->RemoveObserver(this); |
| 28 } | 29 } |
| 29 } | 30 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 if (request->url().GetOrigin() == location.GetOrigin()) | 97 if (request->url().GetOrigin() == location.GetOrigin()) |
| 97 return NULL; | 98 return NULL; |
| 98 | 99 |
| 99 DCHECK(!job_); // our jobs never generate redirects | 100 DCHECK(!job_); // our jobs never generate redirects |
| 100 | 101 |
| 101 if (found_fallback_entry_.has_response_id()) { | 102 if (found_fallback_entry_.has_response_id()) { |
| 102 // 6.9.6, step 4: If this results in a redirect to another origin, | 103 // 6.9.6, step 4: If this results in a redirect to another origin, |
| 103 // get the resource of the fallback entry. | 104 // get the resource of the fallback entry. |
| 104 job_ = new AppCacheURLRequestJob(request, storage()); | 105 job_ = new AppCacheURLRequestJob(request, storage()); |
| 105 DeliverAppCachedResponse( | 106 DeliverAppCachedResponse( |
| 106 found_fallback_entry_, found_cache_id_, found_manifest_url_, | 107 found_fallback_entry_, found_cache_id_, found_group_id_, |
| 107 true, found_fallback_url_); | 108 found_manifest_url_, true, found_fallback_url_); |
| 108 } else if (!found_network_namespace_) { | 109 } else if (!found_network_namespace_) { |
| 109 // 6.9.6, step 6: Fail the resource load. | 110 // 6.9.6, step 6: Fail the resource load. |
| 110 job_ = new AppCacheURLRequestJob(request, storage()); | 111 job_ = new AppCacheURLRequestJob(request, storage()); |
| 111 DeliverErrorResponse(); | 112 DeliverErrorResponse(); |
| 112 } else { | 113 } else { |
| 113 // 6.9.6 step 3 and 5: Fetch the resource normally. | 114 // 6.9.6 step 3 and 5: Fetch the resource normally. |
| 114 } | 115 } |
| 115 | 116 |
| 116 return job_; | 117 return job_; |
| 117 } | 118 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 148 std::string header_value; | 149 std::string header_value; |
| 149 request->GetResponseHeaderByName(kFallbackOverrideHeader, &header_value); | 150 request->GetResponseHeaderByName(kFallbackOverrideHeader, &header_value); |
| 150 if (header_value == kFallbackOverrideValue) | 151 if (header_value == kFallbackOverrideValue) |
| 151 return NULL; | 152 return NULL; |
| 152 } | 153 } |
| 153 | 154 |
| 154 // 6.9.6, step 4: If this results in a 4xx or 5xx status code | 155 // 6.9.6, step 4: If this results in a 4xx or 5xx status code |
| 155 // or there were network errors, get the resource of the fallback entry. | 156 // or there were network errors, get the resource of the fallback entry. |
| 156 job_ = new AppCacheURLRequestJob(request, storage()); | 157 job_ = new AppCacheURLRequestJob(request, storage()); |
| 157 DeliverAppCachedResponse( | 158 DeliverAppCachedResponse( |
| 158 found_fallback_entry_, found_cache_id_, found_manifest_url_, | 159 found_fallback_entry_, found_cache_id_, found_group_id_, |
| 159 true, found_fallback_url_); | 160 found_manifest_url_, true, found_fallback_url_); |
| 160 return job_; | 161 return job_; |
| 161 } | 162 } |
| 162 | 163 |
| 163 void AppCacheRequestHandler::OnDestructionImminent(AppCacheHost* host) { | 164 void AppCacheRequestHandler::OnDestructionImminent(AppCacheHost* host) { |
| 164 storage()->CancelDelegateCallbacks(this); | 165 storage()->CancelDelegateCallbacks(this); |
| 165 host_ = NULL; // no need to RemoveObserver, the host is being deleted | 166 host_ = NULL; // no need to RemoveObserver, the host is being deleted |
| 166 | 167 |
| 167 // Since the host is being deleted, we don't have to complete any job | 168 // Since the host is being deleted, we don't have to complete any job |
| 168 // that is current running. It's destined for the bit bucket anyway. | 169 // that is current running. It's destined for the bit bucket anyway. |
| 169 if (job_) { | 170 if (job_) { |
| 170 job_->Kill(); | 171 job_->Kill(); |
| 171 job_ = NULL; | 172 job_ = NULL; |
| 172 } | 173 } |
| 173 } | 174 } |
| 174 | 175 |
| 175 void AppCacheRequestHandler::DeliverAppCachedResponse( | 176 void AppCacheRequestHandler::DeliverAppCachedResponse( |
| 176 const AppCacheEntry& entry, int64 cache_id, const GURL& manifest_url, | 177 const AppCacheEntry& entry, int64 cache_id, int64 group_id, |
| 177 bool is_fallback, const GURL& fallback_url) { | 178 const GURL& manifest_url, bool is_fallback, const GURL& fallback_url) { |
| 178 DCHECK(host_ && job_ && job_->is_waiting()); | 179 DCHECK(host_ && job_ && job_->is_waiting()); |
| 179 DCHECK(entry.has_response_id()); | 180 DCHECK(entry.has_response_id()); |
| 180 | 181 |
| 181 if (ResourceType::IsFrame(resource_type_) && is_fallback) { | 182 if (ResourceType::IsFrame(resource_type_) && is_fallback) { |
| 182 DCHECK(!fallback_url.is_empty()); | 183 DCHECK(!fallback_url.is_empty()); |
| 183 host_->NotifyMainResourceFallback(fallback_url); | 184 host_->NotifyMainResourceFallback(fallback_url); |
| 184 } | 185 } |
| 185 | 186 |
| 186 job_->DeliverAppCachedResponse(manifest_url, cache_id, entry, is_fallback); | 187 job_->DeliverAppCachedResponse(manifest_url, group_id, cache_id, |
| 188 entry, is_fallback); |
| 187 } | 189 } |
| 188 | 190 |
| 189 void AppCacheRequestHandler::DeliverErrorResponse() { | 191 void AppCacheRequestHandler::DeliverErrorResponse() { |
| 190 DCHECK(job_ && job_->is_waiting()); | 192 DCHECK(job_ && job_->is_waiting()); |
| 191 job_->DeliverErrorResponse(); | 193 job_->DeliverErrorResponse(); |
| 192 } | 194 } |
| 193 | 195 |
| 194 void AppCacheRequestHandler::DeliverNetworkResponse() { | 196 void AppCacheRequestHandler::DeliverNetworkResponse() { |
| 195 DCHECK(job_ && job_->is_waiting()); | 197 DCHECK(job_ && job_->is_waiting()); |
| 196 job_->DeliverNetworkResponse(); | 198 job_->DeliverNetworkResponse(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 211 // We may have to wait for our storage query to complete, but | 213 // We may have to wait for our storage query to complete, but |
| 212 // this query can also complete syncrhonously. | 214 // this query can also complete syncrhonously. |
| 213 job_ = new AppCacheURLRequestJob(request, storage()); | 215 job_ = new AppCacheURLRequestJob(request, storage()); |
| 214 storage()->FindResponseForMainRequest( | 216 storage()->FindResponseForMainRequest( |
| 215 request->url(), preferred_manifest_url, this); | 217 request->url(), preferred_manifest_url, this); |
| 216 } | 218 } |
| 217 | 219 |
| 218 void AppCacheRequestHandler::OnMainResponseFound( | 220 void AppCacheRequestHandler::OnMainResponseFound( |
| 219 const GURL& url, const AppCacheEntry& entry, | 221 const GURL& url, const AppCacheEntry& entry, |
| 220 const GURL& fallback_url, const AppCacheEntry& fallback_entry, | 222 const GURL& fallback_url, const AppCacheEntry& fallback_entry, |
| 221 int64 cache_id, const GURL& manifest_url) { | 223 int64 cache_id, int64 group_id, const GURL& manifest_url) { |
| 222 DCHECK(job_); | 224 DCHECK(job_); |
| 223 DCHECK(host_); | 225 DCHECK(host_); |
| 224 DCHECK(is_main_resource()); | 226 DCHECK(is_main_resource()); |
| 225 DCHECK(!entry.IsForeign()); | 227 DCHECK(!entry.IsForeign()); |
| 226 DCHECK(!fallback_entry.IsForeign()); | 228 DCHECK(!fallback_entry.IsForeign()); |
| 227 DCHECK(!(entry.has_response_id() && fallback_entry.has_response_id())); | 229 DCHECK(!(entry.has_response_id() && fallback_entry.has_response_id())); |
| 228 | 230 |
| 229 if (!job_) | 231 if (!job_) |
| 230 return; | 232 return; |
| 231 | 233 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 252 host_->LoadMainResourceCache(cache_id); | 254 host_->LoadMainResourceCache(cache_id); |
| 253 host_->set_preferred_manifest_url(manifest_url); | 255 host_->set_preferred_manifest_url(manifest_url); |
| 254 } | 256 } |
| 255 | 257 |
| 256 // 6.11.1 Navigating across documents, steps 10 and 14. | 258 // 6.11.1 Navigating across documents, steps 10 and 14. |
| 257 | 259 |
| 258 found_entry_ = entry; | 260 found_entry_ = entry; |
| 259 found_fallback_url_ = fallback_url; | 261 found_fallback_url_ = fallback_url; |
| 260 found_fallback_entry_ = fallback_entry; | 262 found_fallback_entry_ = fallback_entry; |
| 261 found_cache_id_ = cache_id; | 263 found_cache_id_ = cache_id; |
| 264 found_group_id_ = group_id; |
| 262 found_manifest_url_ = manifest_url; | 265 found_manifest_url_ = manifest_url; |
| 263 found_network_namespace_ = false; // not applicable to main requests | 266 found_network_namespace_ = false; // not applicable to main requests |
| 264 | 267 |
| 265 if (found_entry_.has_response_id()) { | 268 if (found_entry_.has_response_id()) { |
| 266 DeliverAppCachedResponse( | 269 DeliverAppCachedResponse( |
| 267 found_entry_, found_cache_id_, found_manifest_url_, | 270 found_entry_, found_cache_id_, found_group_id_, found_manifest_url_, |
| 268 false, GURL()); | 271 false, GURL()); |
| 269 } else { | 272 } else { |
| 270 DeliverNetworkResponse(); | 273 DeliverNetworkResponse(); |
| 271 } | 274 } |
| 272 } | 275 } |
| 273 | 276 |
| 274 // Sub-resource handling ---------------------------------------------- | 277 // Sub-resource handling ---------------------------------------------- |
| 275 | 278 |
| 276 void AppCacheRequestHandler::MaybeLoadSubResource( | 279 void AppCacheRequestHandler::MaybeLoadSubResource( |
| 277 net::URLRequest* request) { | 280 net::URLRequest* request) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 306 AppCache* cache = host_->associated_cache(); | 309 AppCache* cache = host_->associated_cache(); |
| 307 storage()->FindResponseForSubRequest( | 310 storage()->FindResponseForSubRequest( |
| 308 host_->associated_cache(), url, | 311 host_->associated_cache(), url, |
| 309 &found_entry_, &found_fallback_entry_, &found_network_namespace_); | 312 &found_entry_, &found_fallback_entry_, &found_network_namespace_); |
| 310 | 313 |
| 311 if (found_entry_.has_response_id()) { | 314 if (found_entry_.has_response_id()) { |
| 312 // Step 2: If there's an entry, get it instead. | 315 // Step 2: If there's an entry, get it instead. |
| 313 DCHECK(!found_network_namespace_ && | 316 DCHECK(!found_network_namespace_ && |
| 314 !found_fallback_entry_.has_response_id()); | 317 !found_fallback_entry_.has_response_id()); |
| 315 found_cache_id_ = cache->cache_id(); | 318 found_cache_id_ = cache->cache_id(); |
| 319 found_group_id_ = cache->owning_group()->group_id(); |
| 316 found_manifest_url_ = cache->owning_group()->manifest_url(); | 320 found_manifest_url_ = cache->owning_group()->manifest_url(); |
| 317 DeliverAppCachedResponse( | 321 DeliverAppCachedResponse( |
| 318 found_entry_, found_cache_id_, found_manifest_url_, | 322 found_entry_, found_cache_id_, found_group_id_, found_manifest_url_, |
| 319 false, GURL()); | 323 false, GURL()); |
| 320 return; | 324 return; |
| 321 } | 325 } |
| 322 | 326 |
| 323 if (found_fallback_entry_.has_response_id()) { | 327 if (found_fallback_entry_.has_response_id()) { |
| 324 // Step 4: Fetch the resource normally, if this results | 328 // Step 4: Fetch the resource normally, if this results |
| 325 // in certain conditions, then use the fallback. | 329 // in certain conditions, then use the fallback. |
| 326 DCHECK(!found_network_namespace_ && | 330 DCHECK(!found_network_namespace_ && |
| 327 !found_entry_.has_response_id()); | 331 !found_entry_.has_response_id()); |
| 328 found_cache_id_ = cache->cache_id(); | 332 found_cache_id_ = cache->cache_id(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 355 if (!host_->associated_cache() || | 359 if (!host_->associated_cache() || |
| 356 !host_->associated_cache()->is_complete()) { | 360 !host_->associated_cache()->is_complete()) { |
| 357 DeliverNetworkResponse(); | 361 DeliverNetworkResponse(); |
| 358 return; | 362 return; |
| 359 } | 363 } |
| 360 | 364 |
| 361 ContinueMaybeLoadSubResource(); | 365 ContinueMaybeLoadSubResource(); |
| 362 } | 366 } |
| 363 | 367 |
| 364 } // namespace appcache | 368 } // namespace appcache |
| OLD | NEW |