Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / appcache / appcache_interceptor.cc
blob4ca2c6998e75d2680c331e08accb2af71605277c
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.
17 namespace content {
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,
33 int process_id,
34 int host_id,
35 ResourceType resource_type,
36 bool should_reset_appcache) {
37 if (!service || (host_id == kAppCacheNoHostId))
38 return;
40 AppCacheBackendImpl* backend = service->GetBackend(process_id);
41 if (!backend)
42 return;
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);
47 if (!host)
48 return;
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);
53 if (handler)
54 SetHandler(request, handler);
57 void AppCacheInterceptor::GetExtraResponseInfo(net::URLRequest* request,
58 int64* cache_id,
59 GURL* manifest_url) {
60 DCHECK(*cache_id == kAppCacheNoCacheId);
61 DCHECK(manifest_url->is_empty());
62 AppCacheRequestHandler* handler = GetHandler(request);
63 if (handler)
64 handler->GetExtraResponseInfo(cache_id, manifest_url);
67 void AppCacheInterceptor::PrepareForCrossSiteTransfer(
68 net::URLRequest* request,
69 int old_process_id) {
70 AppCacheRequestHandler* handler = GetHandler(request);
71 if (!handler)
72 return;
73 handler->PrepareForCrossSiteTransfer(old_process_id);
76 void AppCacheInterceptor::CompleteCrossSiteTransfer(
77 net::URLRequest* request,
78 int new_process_id,
79 int new_host_id) {
80 AppCacheRequestHandler* handler = GetHandler(request);
81 if (!handler)
82 return;
83 DCHECK_NE(kAppCacheNoHostId, new_host_id);
84 handler->CompleteCrossSiteTransfer(new_process_id,
85 new_host_id);
88 void AppCacheInterceptor::MaybeCompleteCrossSiteTransferInOldProcess(
89 net::URLRequest* request,
90 int process_id) {
91 AppCacheRequestHandler* handler = GetHandler(request);
92 if (!handler)
93 return;
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);
106 if (!handler)
107 return NULL;
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);
116 if (!handler)
117 return NULL;
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);
125 if (!handler)
126 return NULL;
127 return handler->MaybeLoadFallbackForResponse(request, network_delegate);
130 } // namespace content