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. 4 5 #include "content/browser/appcache/appcache_interceptor.h" 6 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 14 namespace content { 15 16 // static 17 AppCacheInterceptor* AppCacheInterceptor::GetInstance() { 18 return Singleton<AppCacheInterceptor>::get(); 19 } 20 21 void AppCacheInterceptor::SetHandler(net::URLRequest* request, 22 AppCacheRequestHandler* handler) { 23 request->SetUserData(GetInstance(), handler); // request takes ownership 24 } 25 26 AppCacheRequestHandler* AppCacheInterceptor::GetHandler( 27 net::URLRequest* request) { 28 return reinterpret_cast<AppCacheRequestHandler*>( 29 request->GetUserData(GetInstance())); 30 } 31 32 void AppCacheInterceptor::SetExtraRequestInfo( 33 net::URLRequest* request, 34 AppCacheServiceImpl* service, 35 int process_id, 36 int host_id, 37 ResourceType resource_type) { 38 if (!service || (host_id == kAppCacheNoHostId)) 39 return; 40 41 AppCacheBackendImpl* backend = service->GetBackend(process_id); 42 if (!backend) 43 return; 44 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); 48 if (!host) 49 return; 50 51 // Create a handler for this request and associate it with the request. 52 AppCacheRequestHandler* handler = 53 host->CreateRequestHandler(request, resource_type); 54 if (handler) 55 SetHandler(request, handler); 56 } 57 58 void AppCacheInterceptor::GetExtraResponseInfo(net::URLRequest* request, 59 int64* cache_id, 60 GURL* manifest_url) { 61 DCHECK(*cache_id == kAppCacheNoCacheId); 62 DCHECK(manifest_url->is_empty()); 63 AppCacheRequestHandler* handler = GetHandler(request); 64 if (handler) 65 handler->GetExtraResponseInfo(cache_id, manifest_url); 66 } 67 68 void AppCacheInterceptor::PrepareForCrossSiteTransfer( 69 net::URLRequest* request, 70 int old_process_id) { 71 AppCacheRequestHandler* handler = GetHandler(request); 72 if (!handler) 73 return; 74 handler->PrepareForCrossSiteTransfer(old_process_id); 75 } 76 77 void AppCacheInterceptor::CompleteCrossSiteTransfer( 78 net::URLRequest* request, 79 int new_process_id, 80 int new_host_id) { 81 AppCacheRequestHandler* handler = GetHandler(request); 82 if (!handler) 83 return; 84 DCHECK_NE(kAppCacheNoHostId, new_host_id); 85 handler->CompleteCrossSiteTransfer(new_process_id, 86 new_host_id); 87 } 88 89 AppCacheInterceptor::AppCacheInterceptor() { 90 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); 91 } 92 93 AppCacheInterceptor::~AppCacheInterceptor() { 94 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); 95 } 96 97 net::URLRequestJob* AppCacheInterceptor::MaybeIntercept( 98 net::URLRequest* request, net::NetworkDelegate* network_delegate) { 99 AppCacheRequestHandler* handler = GetHandler(request); 100 if (!handler) 101 return NULL; 102 return handler->MaybeLoadResource(request, network_delegate); 103 } 104 105 net::URLRequestJob* AppCacheInterceptor::MaybeInterceptRedirect( 106 net::URLRequest* request, 107 net::NetworkDelegate* network_delegate, 108 const GURL& location) { 109 AppCacheRequestHandler* handler = GetHandler(request); 110 if (!handler) 111 return NULL; 112 return handler->MaybeLoadFallbackForRedirect( 113 request, network_delegate, location); 114 } 115 116 net::URLRequestJob* AppCacheInterceptor::MaybeInterceptResponse( 117 net::URLRequest* request, net::NetworkDelegate* network_delegate) { 118 AppCacheRequestHandler* handler = GetHandler(request); 119 if (!handler) 120 return NULL; 121 return handler->MaybeLoadFallbackForResponse(request, network_delegate); 122 } 123 124 } // namespace content 125