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"
13 #include "net/url_request/url_request.h"
15 static int kHandlerKey
; // Value is not used.
19 void AppCacheInterceptor::SetHandler(net::URLRequest
* request
,
20 AppCacheRequestHandler
* handler
) {
21 request
->SetUserData(&kHandlerKey
, handler
); // request takes ownership
24 AppCacheRequestHandler
* AppCacheInterceptor::GetHandler(
25 net::URLRequest
* request
) {
26 return static_cast<AppCacheRequestHandler
*>(
27 request
->GetUserData(&kHandlerKey
));
30 void AppCacheInterceptor::SetExtraRequestInfo(
31 net::URLRequest
* request
,
32 AppCacheServiceImpl
* service
,
35 ResourceType resource_type
,
36 bool should_reset_appcache
) {
37 if (!service
|| (host_id
== kAppCacheNoHostId
))
40 AppCacheBackendImpl
* backend
= service
->GetBackend(process_id
);
44 // TODO(michaeln): An invalid host id is indicative of bad data
45 // from a child process. How should we handle that here?
46 AppCacheHost
* host
= backend
->GetHost(host_id
);
50 // Create a handler for this request and associate it with the request.
51 AppCacheRequestHandler
* handler
=
52 host
->CreateRequestHandler(request
, resource_type
, should_reset_appcache
);
54 SetHandler(request
, handler
);
57 void AppCacheInterceptor::GetExtraResponseInfo(net::URLRequest
* request
,
60 DCHECK(*cache_id
== kAppCacheNoCacheId
);
61 DCHECK(manifest_url
->is_empty());
62 AppCacheRequestHandler
* handler
= GetHandler(request
);
64 handler
->GetExtraResponseInfo(cache_id
, manifest_url
);
67 void AppCacheInterceptor::PrepareForCrossSiteTransfer(
68 net::URLRequest
* request
,
70 AppCacheRequestHandler
* handler
= GetHandler(request
);
73 handler
->PrepareForCrossSiteTransfer(old_process_id
);
76 void AppCacheInterceptor::CompleteCrossSiteTransfer(
77 net::URLRequest
* request
,
80 AppCacheRequestHandler
* handler
= GetHandler(request
);
83 DCHECK_NE(kAppCacheNoHostId
, new_host_id
);
84 handler
->CompleteCrossSiteTransfer(new_process_id
,
88 void AppCacheInterceptor::MaybeCompleteCrossSiteTransferInOldProcess(
89 net::URLRequest
* request
,
91 AppCacheRequestHandler
* handler
= GetHandler(request
);
94 handler
->MaybeCompleteCrossSiteTransferInOldProcess(process_id
);
97 AppCacheInterceptor::AppCacheInterceptor() {
100 AppCacheInterceptor::~AppCacheInterceptor() {
103 net::URLRequestJob
* AppCacheInterceptor::MaybeInterceptRequest(
104 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) const {
105 AppCacheRequestHandler
* handler
= GetHandler(request
);
108 return handler
->MaybeLoadResource(request
, network_delegate
);
111 net::URLRequestJob
* AppCacheInterceptor::MaybeInterceptRedirect(
112 net::URLRequest
* request
,
113 net::NetworkDelegate
* network_delegate
,
114 const GURL
& location
) const {
115 AppCacheRequestHandler
* handler
= GetHandler(request
);
118 return handler
->MaybeLoadFallbackForRedirect(
119 request
, network_delegate
, location
);
122 net::URLRequestJob
* AppCacheInterceptor::MaybeInterceptResponse(
123 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) const {
124 AppCacheRequestHandler
* handler
= GetHandler(request
);
127 return handler
->MaybeLoadFallbackForResponse(request
, network_delegate
);
130 } // namespace content