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
3 // found in the LICENSE file.
5 #include "webkit/appcache/appcache_interceptor.h"
7 #include "webkit/appcache/appcache_backend_impl.h"
8 #include "webkit/appcache/appcache_host.h"
9 #include "webkit/appcache/appcache_interfaces.h"
10 #include "webkit/appcache/appcache_request_handler.h"
11 #include "webkit/appcache/appcache_service.h"
12 #include "webkit/appcache/appcache_url_request_job.h"
17 AppCacheInterceptor
* AppCacheInterceptor::GetInstance() {
18 return Singleton
<AppCacheInterceptor
>::get();
21 void AppCacheInterceptor::SetHandler(
22 net::URLRequest
* request
, 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
, AppCacheService
* service
, int process_id
,
34 int host_id
, ResourceType::Type resource_type
) {
35 if (!service
|| (host_id
== kNoHostId
))
38 AppCacheBackendImpl
* backend
= service
->GetBackend(process_id
);
42 // TODO(michaeln): An invalid host id is indicative of bad data
43 // from a child process. How should we handle that here?
44 AppCacheHost
* host
= backend
->GetHost(host_id
);
48 // Create a handler for this request and associate it with the request.
49 AppCacheRequestHandler
* handler
=
50 host
->CreateRequestHandler(request
, resource_type
);
52 SetHandler(request
, handler
);
55 void AppCacheInterceptor::GetExtraResponseInfo(net::URLRequest
* request
,
58 DCHECK(*cache_id
== kNoCacheId
);
59 DCHECK(manifest_url
->is_empty());
60 AppCacheRequestHandler
* handler
= GetHandler(request
);
62 handler
->GetExtraResponseInfo(cache_id
, manifest_url
);
65 AppCacheInterceptor::AppCacheInterceptor() {
66 net::URLRequest::Deprecated::RegisterRequestInterceptor(this);
69 AppCacheInterceptor::~AppCacheInterceptor() {
70 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this);
73 net::URLRequestJob
* AppCacheInterceptor::MaybeIntercept(
74 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) {
75 AppCacheRequestHandler
* handler
= GetHandler(request
);
78 return handler
->MaybeLoadResource(request
, network_delegate
);
81 net::URLRequestJob
* AppCacheInterceptor::MaybeInterceptRedirect(
82 net::URLRequest
* request
,
83 net::NetworkDelegate
* network_delegate
,
84 const GURL
& location
) {
85 AppCacheRequestHandler
* handler
= GetHandler(request
);
88 return handler
->MaybeLoadFallbackForRedirect(
89 request
, network_delegate
, location
);
92 net::URLRequestJob
* AppCacheInterceptor::MaybeInterceptResponse(
93 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) {
94 AppCacheRequestHandler
* handler
= GetHandler(request
);
97 return handler
->MaybeLoadFallbackForResponse(request
, network_delegate
);
100 } // namespace appcache