Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / appcache / appcache_interceptor.cc
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 class AppCacheInterceptor::StartInterceptor
17     : public net::URLRequestInterceptor {
18  public:
19   StartInterceptor() {}
20   ~StartInterceptor() override {}
21   net::URLRequestJob* MaybeInterceptRequest(
22       net::URLRequest* request,
23       net::NetworkDelegate* network_delegate) const override {
24     AppCacheRequestHandler* handler = GetHandler(request);
25     if (!handler)
26       return NULL;
27     return handler->MaybeLoadResource(request, network_delegate);
28   }
29
30  private:
31   DISALLOW_COPY_AND_ASSIGN(StartInterceptor);
32 };
33
34
35 // static
36 AppCacheInterceptor* AppCacheInterceptor::GetInstance() {
37   return Singleton<AppCacheInterceptor>::get();
38 }
39
40 // static
41 scoped_ptr<net::URLRequestInterceptor>
42 AppCacheInterceptor::CreateStartInterceptor() {
43   return scoped_ptr<net::URLRequestInterceptor>(
44       new StartInterceptor);
45 }
46
47 void AppCacheInterceptor::SetHandler(net::URLRequest* request,
48                                      AppCacheRequestHandler* handler) {
49   request->SetUserData(GetInstance(), handler);  // request takes ownership
50 }
51
52 AppCacheRequestHandler* AppCacheInterceptor::GetHandler(
53     net::URLRequest* request) {
54   return static_cast<AppCacheRequestHandler*>(
55       request->GetUserData(GetInstance()));
56 }
57
58 void AppCacheInterceptor::SetExtraRequestInfo(
59     net::URLRequest* request,
60     AppCacheServiceImpl* service,
61     int process_id,
62     int host_id,
63     ResourceType resource_type) {
64   if (!service || (host_id == kAppCacheNoHostId))
65     return;
66
67   AppCacheBackendImpl* backend = service->GetBackend(process_id);
68   if (!backend)
69     return;
70
71   // TODO(michaeln): An invalid host id is indicative of bad data
72   // from a child process. How should we handle that here?
73   AppCacheHost* host = backend->GetHost(host_id);
74   if (!host)
75     return;
76
77   // Create a handler for this request and associate it with the request.
78   AppCacheRequestHandler* handler =
79       host->CreateRequestHandler(request, resource_type);
80   if (handler)
81     SetHandler(request, handler);
82 }
83
84 void AppCacheInterceptor::GetExtraResponseInfo(net::URLRequest* request,
85                                                int64* cache_id,
86                                                GURL* manifest_url) {
87   DCHECK(*cache_id == kAppCacheNoCacheId);
88   DCHECK(manifest_url->is_empty());
89   AppCacheRequestHandler* handler = GetHandler(request);
90   if (handler)
91     handler->GetExtraResponseInfo(cache_id, manifest_url);
92 }
93
94 void AppCacheInterceptor::PrepareForCrossSiteTransfer(
95     net::URLRequest* request,
96     int old_process_id) {
97   AppCacheRequestHandler* handler = GetHandler(request);
98   if (!handler)
99     return;
100   handler->PrepareForCrossSiteTransfer(old_process_id);
101 }
102
103 void AppCacheInterceptor::CompleteCrossSiteTransfer(
104     net::URLRequest* request,
105     int new_process_id,
106     int new_host_id) {
107   AppCacheRequestHandler* handler = GetHandler(request);
108   if (!handler)
109     return;
110   DCHECK_NE(kAppCacheNoHostId, new_host_id);
111   handler->CompleteCrossSiteTransfer(new_process_id,
112                                      new_host_id);
113 }
114
115 AppCacheInterceptor::AppCacheInterceptor() {
116   net::URLRequest::Deprecated::RegisterRequestInterceptor(this);
117 }
118
119 AppCacheInterceptor::~AppCacheInterceptor() {
120   net::URLRequest::Deprecated::UnregisterRequestInterceptor(this);
121 }
122
123 net::URLRequestJob* AppCacheInterceptor::MaybeIntercept(
124     net::URLRequest* request, net::NetworkDelegate* network_delegate) {
125   // Intentionally empty, handled by class StartInterceptor.
126   return NULL;
127 }
128
129 net::URLRequestJob* AppCacheInterceptor::MaybeInterceptRedirect(
130     net::URLRequest* request,
131     net::NetworkDelegate* network_delegate,
132     const GURL& location) {
133   AppCacheRequestHandler* handler = GetHandler(request);
134   if (!handler)
135     return NULL;
136   return handler->MaybeLoadFallbackForRedirect(
137       request, network_delegate, location);
138 }
139
140 net::URLRequestJob* AppCacheInterceptor::MaybeInterceptResponse(
141     net::URLRequest* request, net::NetworkDelegate* network_delegate) {
142   AppCacheRequestHandler* handler = GetHandler(request);
143   if (!handler)
144     return NULL;
145   return handler->MaybeLoadFallbackForResponse(request, network_delegate);
146 }
147
148 }  // namespace content