Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / appcache / appcache_interceptor.cc
blob3385e5460cdfa10cf9c3463389a986ab9e70609e
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"
14 namespace content {
16 // static
17 AppCacheInterceptor* AppCacheInterceptor::GetInstance() {
18 return Singleton<AppCacheInterceptor>::get();
21 void AppCacheInterceptor::SetHandler(net::URLRequest* request,
22 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,
34 AppCacheServiceImpl* service,
35 int process_id,
36 int host_id,
37 ResourceType resource_type) {
38 if (!service || (host_id == kAppCacheNoHostId))
39 return;
41 AppCacheBackendImpl* backend = service->GetBackend(process_id);
42 if (!backend)
43 return;
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;
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);
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);
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);
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);
89 AppCacheInterceptor::AppCacheInterceptor() {
90 net::URLRequest::Deprecated::RegisterRequestInterceptor(this);
93 AppCacheInterceptor::~AppCacheInterceptor() {
94 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this);
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);
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);
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);
124 } // namespace content