1 // Copyright 2014 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 #include "content/browser/appcache/appcache_interceptor.h"
7 #include "content/browser/appcache/appcache_backend_impl.h"
8 #include "content/browser/appcache/appcache_host.h"
9 #include "content/browser/appcache/appcache_request_handler.h"
10 #include "content/browser/appcache/appcache_service_impl.h"
11 #include "content/browser/appcache/appcache_url_request_job.h"
12 #include "content/common/appcache_interfaces.h"
17 AppCacheInterceptor
* AppCacheInterceptor::GetInstance() {
18 return Singleton
<AppCacheInterceptor
>::get();
21 void AppCacheInterceptor::SetHandler(net::URLRequest
* request
,
22 AppCacheRequestHandler
* handler
) {
23 request
->SetUserData(GetInstance(), handler
); // request takes ownership
26 AppCacheRequestHandler
* AppCacheInterceptor::GetHandler(
27 net::URLRequest
* request
) {
28 return reinterpret_cast<AppCacheRequestHandler
*>(
29 request
->GetUserData(GetInstance()));
32 void AppCacheInterceptor::SetExtraRequestInfo(
33 net::URLRequest
* request
,
34 AppCacheServiceImpl
* service
,
37 ResourceType resource_type
) {
38 if (!service
|| (host_id
== kAppCacheNoHostId
))
41 AppCacheBackendImpl
* backend
= service
->GetBackend(process_id
);
45 // TODO(michaeln): An invalid host id is indicative of bad data
46 // from a child process. How should we handle that here?
47 AppCacheHost
* host
= backend
->GetHost(host_id
);
51 // Create a handler for this request and associate it with the request.
52 AppCacheRequestHandler
* handler
=
53 host
->CreateRequestHandler(request
, resource_type
);
55 SetHandler(request
, handler
);
58 void AppCacheInterceptor::GetExtraResponseInfo(net::URLRequest
* request
,
61 DCHECK(*cache_id
== kAppCacheNoCacheId
);
62 DCHECK(manifest_url
->is_empty());
63 AppCacheRequestHandler
* handler
= GetHandler(request
);
65 handler
->GetExtraResponseInfo(cache_id
, manifest_url
);
68 void AppCacheInterceptor::PrepareForCrossSiteTransfer(
69 net::URLRequest
* request
,
71 AppCacheRequestHandler
* handler
= GetHandler(request
);
74 handler
->PrepareForCrossSiteTransfer(old_process_id
);
77 void AppCacheInterceptor::CompleteCrossSiteTransfer(
78 net::URLRequest
* request
,
81 AppCacheRequestHandler
* handler
= GetHandler(request
);
84 DCHECK_NE(kAppCacheNoHostId
, new_host_id
);
85 handler
->CompleteCrossSiteTransfer(new_process_id
,
89 AppCacheInterceptor::AppCacheInterceptor() {
90 net::URLRequest::Deprecated::RegisterRequestInterceptor(this);
93 AppCacheInterceptor::~AppCacheInterceptor() {
94 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this);
97 net::URLRequestJob
* AppCacheInterceptor::MaybeIntercept(
98 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) {
99 AppCacheRequestHandler
* handler
= GetHandler(request
);
102 return handler
->MaybeLoadResource(request
, network_delegate
);
105 net::URLRequestJob
* AppCacheInterceptor::MaybeInterceptRedirect(
106 net::URLRequest
* request
,
107 net::NetworkDelegate
* network_delegate
,
108 const GURL
& location
) {
109 AppCacheRequestHandler
* handler
= GetHandler(request
);
112 return handler
->MaybeLoadFallbackForRedirect(
113 request
, network_delegate
, location
);
116 net::URLRequestJob
* AppCacheInterceptor::MaybeInterceptResponse(
117 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) {
118 AppCacheRequestHandler
* handler
= GetHandler(request
);
121 return handler
->MaybeLoadFallbackForResponse(request
, network_delegate
);
124 } // namespace content