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