This source file includes following definitions.
- maybe_load_resource_executed_
- storage
- MaybeLoadResource
- MaybeLoadFallbackForRedirect
- MaybeLoadFallbackForResponse
- GetExtraResponseInfo
- PrepareForCrossSiteTransfer
- CompleteCrossSiteTransfer
- OnDestructionImminent
- DeliverAppCachedResponse
- DeliverErrorResponse
- DeliverNetworkResponse
- MaybeLoadMainResource
- OnMainResponseFound
- MaybeLoadSubResource
- ContinueMaybeLoadSubResource
- OnCacheSelectionComplete
#include "webkit/browser/appcache/appcache_request_handler.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_job.h"
#include "webkit/browser/appcache/appcache.h"
#include "webkit/browser/appcache/appcache_backend_impl.h"
#include "webkit/browser/appcache/appcache_policy.h"
#include "webkit/browser/appcache/appcache_url_request_job.h"
namespace appcache {
AppCacheRequestHandler::AppCacheRequestHandler(
AppCacheHost* host, ResourceType::Type resource_type)
: host_(host), resource_type_(resource_type),
is_waiting_for_cache_selection_(false), found_group_id_(0),
found_cache_id_(0), found_network_namespace_(false),
cache_entry_not_found_(false), maybe_load_resource_executed_(false) {
DCHECK(host_);
host_->AddObserver(this);
}
AppCacheRequestHandler::~AppCacheRequestHandler() {
if (host_) {
storage()->CancelDelegateCallbacks(this);
host_->RemoveObserver(this);
}
}
AppCacheStorage* AppCacheRequestHandler::storage() const {
DCHECK(host_);
return host_->storage();
}
AppCacheURLRequestJob* AppCacheRequestHandler::MaybeLoadResource(
net::URLRequest* request, net::NetworkDelegate* network_delegate) {
maybe_load_resource_executed_ = true;
if (!host_ || !IsSchemeAndMethodSupported(request) || cache_entry_not_found_)
return NULL;
if (job_.get()) {
DCHECK(job_->is_delivering_network_response() ||
job_->cache_entry_not_found());
if (job_->cache_entry_not_found())
cache_entry_not_found_ = true;
job_ = NULL;
storage()->CancelDelegateCallbacks(this);
return NULL;
}
found_entry_ = AppCacheEntry();
found_fallback_entry_ = AppCacheEntry();
found_cache_id_ = kNoCacheId;
found_manifest_url_ = GURL();
found_network_namespace_ = false;
if (is_main_resource())
MaybeLoadMainResource(request, network_delegate);
else
MaybeLoadSubResource(request, network_delegate);
if (job_.get() && job_->is_delivering_network_response()) {
DCHECK(!job_->has_been_started());
job_ = NULL;
}
return job_.get();
}
AppCacheURLRequestJob* AppCacheRequestHandler::MaybeLoadFallbackForRedirect(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const GURL& location) {
if (!host_ || !IsSchemeAndMethodSupported(request) || cache_entry_not_found_)
return NULL;
if (is_main_resource())
return NULL;
if (!maybe_load_resource_executed_)
return NULL;
if (request->url().GetOrigin() == location.GetOrigin())
return NULL;
DCHECK(!job_.get());
if (found_fallback_entry_.has_response_id()) {
job_ = new AppCacheURLRequestJob(request, network_delegate,
storage(), host_, is_main_resource());
DeliverAppCachedResponse(
found_fallback_entry_, found_cache_id_, found_group_id_,
found_manifest_url_, true, found_namespace_entry_url_);
} else if (!found_network_namespace_) {
job_ = new AppCacheURLRequestJob(request, network_delegate,
storage(), host_, is_main_resource());
DeliverErrorResponse();
} else {
}
return job_.get();
}
AppCacheURLRequestJob* AppCacheRequestHandler::MaybeLoadFallbackForResponse(
net::URLRequest* request, net::NetworkDelegate* network_delegate) {
if (!host_ || !IsSchemeAndMethodSupported(request) || cache_entry_not_found_)
return NULL;
if (!found_fallback_entry_.has_response_id())
return NULL;
if (request->status().status() == net::URLRequestStatus::CANCELED) {
return NULL;
}
if (job_.get()) {
DCHECK(!job_->is_delivering_network_response());
return NULL;
}
if (request->status().is_success()) {
int code_major = request->GetResponseCode() / 100;
if (code_major !=4 && code_major != 5)
return NULL;
const std::string kFallbackOverrideHeader(
"x-chromium-appcache-fallback-override");
const std::string kFallbackOverrideValue(
"disallow-fallback");
std::string header_value;
request->GetResponseHeaderByName(kFallbackOverrideHeader, &header_value);
if (header_value == kFallbackOverrideValue)
return NULL;
}
job_ = new AppCacheURLRequestJob(request, network_delegate,
storage(), host_, is_main_resource());
DeliverAppCachedResponse(
found_fallback_entry_, found_cache_id_, found_group_id_,
found_manifest_url_, true, found_namespace_entry_url_);
return job_.get();
}
void AppCacheRequestHandler::GetExtraResponseInfo(
int64* cache_id, GURL* manifest_url) {
if (job_.get() && job_->is_delivering_appcache_response()) {
*cache_id = job_->cache_id();
*manifest_url = job_->manifest_url();
}
}
void AppCacheRequestHandler::PrepareForCrossSiteTransfer(int old_process_id) {
if (!host_)
return;
AppCacheBackendImpl* backend = host_->service()->GetBackend(old_process_id);
host_for_cross_site_transfer_ = backend->TransferHostOut(host_->host_id());
DCHECK_EQ(host_, host_for_cross_site_transfer_.get());
}
void AppCacheRequestHandler::CompleteCrossSiteTransfer(
int new_process_id, int new_host_id) {
if (!host_for_cross_site_transfer_.get())
return;
DCHECK_EQ(host_, host_for_cross_site_transfer_.get());
AppCacheBackendImpl* backend = host_->service()->GetBackend(new_process_id);
backend->TransferHostIn(new_host_id, host_for_cross_site_transfer_.Pass());
}
void AppCacheRequestHandler::OnDestructionImminent(AppCacheHost* host) {
storage()->CancelDelegateCallbacks(this);
host_ = NULL;
if (job_.get()) {
job_->Kill();
job_ = NULL;
}
}
void AppCacheRequestHandler::DeliverAppCachedResponse(
const AppCacheEntry& entry, int64 cache_id, int64 group_id,
const GURL& manifest_url, bool is_fallback,
const GURL& namespace_entry_url) {
DCHECK(host_ && job_.get() && job_->is_waiting());
DCHECK(entry.has_response_id());
if (ResourceType::IsFrame(resource_type_) && !namespace_entry_url.is_empty())
host_->NotifyMainResourceIsNamespaceEntry(namespace_entry_url);
job_->DeliverAppCachedResponse(manifest_url, group_id, cache_id,
entry, is_fallback);
}
void AppCacheRequestHandler::DeliverErrorResponse() {
DCHECK(job_.get() && job_->is_waiting());
job_->DeliverErrorResponse();
}
void AppCacheRequestHandler::DeliverNetworkResponse() {
DCHECK(job_.get() && job_->is_waiting());
job_->DeliverNetworkResponse();
}
void AppCacheRequestHandler::MaybeLoadMainResource(
net::URLRequest* request, net::NetworkDelegate* network_delegate) {
DCHECK(!job_.get());
DCHECK(host_);
const AppCacheHost* spawning_host =
ResourceType::IsSharedWorker(resource_type_) ?
host_ : host_->GetSpawningHost();
GURL preferred_manifest_url = spawning_host ?
spawning_host->preferred_manifest_url() : GURL();
job_ = new AppCacheURLRequestJob(request, network_delegate,
storage(), host_, is_main_resource());
storage()->FindResponseForMainRequest(
request->url(), preferred_manifest_url, this);
}
void AppCacheRequestHandler::OnMainResponseFound(
const GURL& url, const AppCacheEntry& entry,
const GURL& namespace_entry_url, const AppCacheEntry& fallback_entry,
int64 cache_id, int64 group_id, const GURL& manifest_url) {
DCHECK(job_.get());
DCHECK(host_);
DCHECK(is_main_resource());
DCHECK(!entry.IsForeign());
DCHECK(!fallback_entry.IsForeign());
DCHECK(!(entry.has_response_id() && fallback_entry.has_response_id()));
if (!job_.get())
return;
AppCachePolicy* policy = host_->service()->appcache_policy();
bool was_blocked_by_policy = !manifest_url.is_empty() && policy &&
!policy->CanLoadAppCache(manifest_url, host_->first_party_url());
if (was_blocked_by_policy) {
if (ResourceType::IsFrame(resource_type_)) {
host_->NotifyMainResourceBlocked(manifest_url);
} else {
DCHECK(ResourceType::IsSharedWorker(resource_type_));
host_->frontend()->OnContentBlocked(host_->host_id(), manifest_url);
}
DeliverNetworkResponse();
return;
}
if (ResourceType::IsFrame(resource_type_) && cache_id != kNoCacheId) {
host_->LoadMainResourceCache(cache_id);
host_->set_preferred_manifest_url(manifest_url);
}
found_entry_ = entry;
found_namespace_entry_url_ = namespace_entry_url;
found_fallback_entry_ = fallback_entry;
found_cache_id_ = cache_id;
found_group_id_ = group_id;
found_manifest_url_ = manifest_url;
found_network_namespace_ = false;
if (found_entry_.has_response_id()) {
DCHECK(!found_fallback_entry_.has_response_id());
DeliverAppCachedResponse(
found_entry_, found_cache_id_, found_group_id_, found_manifest_url_,
false, found_namespace_entry_url_);
} else {
DeliverNetworkResponse();
}
}
void AppCacheRequestHandler::MaybeLoadSubResource(
net::URLRequest* request, net::NetworkDelegate* network_delegate) {
DCHECK(!job_.get());
if (host_->is_selection_pending()) {
is_waiting_for_cache_selection_ = true;
job_ = new AppCacheURLRequestJob(request, network_delegate,
storage(), host_, is_main_resource());
return;
}
if (!host_->associated_cache() ||
!host_->associated_cache()->is_complete() ||
host_->associated_cache()->owning_group()->is_being_deleted()) {
return;
}
job_ = new AppCacheURLRequestJob(request, network_delegate,
storage(), host_, is_main_resource());
ContinueMaybeLoadSubResource();
}
void AppCacheRequestHandler::ContinueMaybeLoadSubResource() {
DCHECK(job_.get());
DCHECK(host_->associated_cache() && host_->associated_cache()->is_complete());
const GURL& url = job_->request()->url();
AppCache* cache = host_->associated_cache();
storage()->FindResponseForSubRequest(
host_->associated_cache(), url,
&found_entry_, &found_fallback_entry_, &found_network_namespace_);
if (found_entry_.has_response_id()) {
DCHECK(!found_network_namespace_ &&
!found_fallback_entry_.has_response_id());
found_cache_id_ = cache->cache_id();
found_group_id_ = cache->owning_group()->group_id();
found_manifest_url_ = cache->owning_group()->manifest_url();
DeliverAppCachedResponse(
found_entry_, found_cache_id_, found_group_id_, found_manifest_url_,
false, GURL());
return;
}
if (found_fallback_entry_.has_response_id()) {
DCHECK(!found_network_namespace_ &&
!found_entry_.has_response_id());
found_cache_id_ = cache->cache_id();
found_manifest_url_ = cache->owning_group()->manifest_url();
DeliverNetworkResponse();
return;
}
if (found_network_namespace_) {
DCHECK(!found_entry_.has_response_id() &&
!found_fallback_entry_.has_response_id());
DeliverNetworkResponse();
return;
}
DeliverErrorResponse();
}
void AppCacheRequestHandler::OnCacheSelectionComplete(AppCacheHost* host) {
DCHECK(host == host_);
if (is_main_resource())
return;
if (!is_waiting_for_cache_selection_)
return;
is_waiting_for_cache_selection_ = false;
if (!host_->associated_cache() ||
!host_->associated_cache()->is_complete()) {
DeliverNetworkResponse();
return;
}
ContinueMaybeLoadSubResource();
}
}