blob: 5da57db554ee3dcd3fddf6c14da33919aec66082 [file] [log] [blame]
[email protected]0a608842011-09-08 10:55:191// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]97e3edc2009-09-15 22:07:152// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]98d6d4562014-06-25 20:57:555#ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_
6#define CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_
[email protected]97e3edc2009-09-15 22:07:157
avib7348942015-12-25 20:57:108#include <stdint.h>
9
dcheng59716272016-04-09 05:19:0810#include <memory>
11
[email protected]bfe19482011-09-30 22:14:5712#include "base/compiler_specific.h"
avib7348942015-12-25 20:57:1013#include "base/macros.h"
mmenke5289cbfd2015-10-29 19:36:1714#include "base/memory/weak_ptr.h"
[email protected]ea8e1812012-02-15 22:07:3415#include "base/supports_user_data.h"
[email protected]98d6d4562014-06-25 20:57:5516#include "content/browser/appcache/appcache_entry.h"
17#include "content/browser/appcache/appcache_host.h"
Anantanarayanan Iyengard125c4602017-08-19 00:46:5618#include "content/browser/appcache/appcache_request_handler.h"
michaelnb8618402016-08-02 20:14:3319#include "content/browser/appcache/appcache_service_impl.h"
anantaa2c8ec62017-06-09 23:44:0520#include "content/browser/loader/url_loader_request_handler.h"
anantae60d1d42017-06-20 04:16:2721#include "content/browser/url_loader_factory_getter.h"
[email protected]98d6d4562014-06-25 20:57:5522#include "content/common/content_export.h"
[email protected]7491ad02014-07-05 19:10:0723#include "content/public/common/resource_type.h"
[email protected]97e3edc2009-09-15 22:07:1524
[email protected]edfe7fab2010-11-28 13:11:5225namespace net {
[email protected]9f170462012-08-24 01:06:5826class NetworkDelegate;
[email protected]97e3edc2009-09-15 22:07:1527class URLRequest;
[email protected]edfe7fab2010-11-28 13:11:5228} // namespace net
[email protected]97e3edc2009-09-15 22:07:1529
[email protected]073f24d2014-04-09 00:00:2330namespace content {
ananta46f3e942017-05-09 04:56:3031class AppCacheJob;
anantaa2c8ec62017-06-09 23:44:0532class AppCacheNavigationHandleCore;
Anantanarayanan Iyengar2130d272017-09-01 18:21:1433class AppCacheSubresourceURLFactory;
ananta4e8a5292017-05-04 03:07:1534class AppCacheRequest;
[email protected]073f24d2014-04-09 00:00:2335class AppCacheRequestHandlerTest;
[email protected]3367fc1d2009-11-09 00:09:2136class AppCacheURLRequestJob;
ananta2533bff62017-07-12 04:07:2737class AppCacheHost;
[email protected]3367fc1d2009-11-09 00:09:2138
[email protected]6981d9632010-11-30 21:34:0239// An instance is created for each net::URLRequest. The instance survives all
40// http transactions involved in the processing of its net::URLRequest, and is
[email protected]97e3edc2009-09-15 22:07:1541// given the opportunity to hijack the request along the way. Callers
42// should use AppCacheHost::CreateRequestHandler to manufacture instances
43// that can retrieve resources for a particular host.
[email protected]98d6d4562014-06-25 20:57:5544class CONTENT_EXPORT AppCacheRequestHandler
[email protected]ea8e1812012-02-15 22:07:3445 : public base::SupportsUserData::Data,
[email protected]bfe19482011-09-30 22:14:5746 public AppCacheHost::Observer,
michaelnb8618402016-08-02 20:14:3347 public AppCacheServiceImpl::Observer,
anantaa2c8ec62017-06-09 23:44:0548 public AppCacheStorage::Delegate,
49 public URLLoaderRequestHandler {
[email protected]97e3edc2009-09-15 22:07:1550 public:
dchengc2282aa2014-10-21 12:07:5851 ~AppCacheRequestHandler() override;
[email protected]7e8e3dd2009-09-18 01:05:0952
[email protected]3367fc1d2009-11-09 00:09:2153 // These are called on each request intercept opportunity.
Michael Nordman85243a82017-10-24 01:24:5054 // When the NetworkService is not enabled, the AppCacheInterceptor calls these
55 // methods directly. When the NetworkService is enabled, the various
56 // MaybeCreateLoader methods below call call these internally.
ananta46f3e942017-05-09 04:56:3057 AppCacheJob* MaybeLoadResource(net::NetworkDelegate* network_delegate);
58 AppCacheJob* MaybeLoadFallbackForRedirect(
[email protected]9f170462012-08-24 01:06:5859 net::NetworkDelegate* network_delegate,
60 const GURL& location);
ananta46f3e942017-05-09 04:56:3061 AppCacheJob* MaybeLoadFallbackForResponse(
mmenke5289cbfd2015-10-29 19:36:1762 net::NetworkDelegate* network_delegate);
[email protected]97e3edc2009-09-15 22:07:1563
avib7348942015-12-25 20:57:1064 void GetExtraResponseInfo(int64_t* cache_id, GURL* manifest_url);
[email protected]97e3edc2009-09-15 22:07:1565
[email protected]5fcb289e2014-02-04 00:34:1966 // Methods to support cross site navigations.
67 void PrepareForCrossSiteTransfer(int old_process_id);
68 void CompleteCrossSiteTransfer(int new_process_id, int new_host_id);
michaelnbfea6ec2014-12-09 23:16:1369 void MaybeCompleteCrossSiteTransferInOldProcess(int old_process_id);
[email protected]5fcb289e2014-02-04 00:34:1970
michaeln054c2fd42016-01-16 02:33:4871 // Useful for detecting storage partition mismatches in the context
72 // of cross site transfer navigations.
73 bool SanityCheckIsSameService(AppCacheService* service) {
74 return !host_ || (host_->service() == service);
75 }
76
Michael Nordman85243a82017-10-24 01:24:5077 // NetworkService loading
Anantanarayanan Iyengard125c4602017-08-19 00:46:5678
Michael Nordman85243a82017-10-24 01:24:5079 // URLLoaderRequestHandler overrides - main resource loading.
80 // These methods are used by the NavigationURLLoaderNetworkService.
81 // Internally they use same methods used by the network library based impl,
82 // MaybeLoadResource and MaybeLoadFallbackForResponse.
83 // Eventually one of the Deliver*Response() methods is called and the
84 // LoaderCallback is invoked.
85 void MaybeCreateLoader(const ResourceRequest& resource_request,
86 ResourceContext* resource_context,
87 LoaderCallback callback) override;
88 // MaybeCreateLoaderForResponse always returns synchronously.
89 bool MaybeCreateLoaderForResponse(
90 const ResourceResponseHead& response,
91 mojom::URLLoaderPtr* loader,
92 mojom::URLLoaderClientRequest* client_request) override;
Kinuko Yasuda250577c2017-10-29 02:51:2493 base::Optional<SubresourceLoaderParams> MaybeCreateSubresourceLoaderParams()
94 override;
Michael Nordman85243a82017-10-24 01:24:5095
96 // These methods are used for subresource loading by the
97 // AppCacheSubresourceURLFactory::SubresourceLoader class.
98 // Internally they use same methods used by the network library based impl,
99 // MaybeLoadResource, MaybeLoadFallbackForResponse, and
100 // MaybeLoadFallbackForRedirect. Eventually one of the Deliver*Response()
101 // methods is called and the LoaderCallback is invoked.
102 void MaybeCreateSubresourceLoader(const ResourceRequest& resource_request,
103 LoaderCallback callback);
104 void MaybeFallbackForSubresourceResponse(const ResourceResponseHead& response,
105 LoaderCallback callback);
106 void MaybeFallbackForSubresourceRedirect(
107 const net::RedirectInfo& redirect_info,
108 LoaderCallback callback);
109 void MaybeFollowSubresourceRedirect(const net::RedirectInfo& redirect_info,
110 LoaderCallback callback);
[email protected]15f9cded2010-05-20 20:51:06111
anantaa2c8ec62017-06-09 23:44:05112 static std::unique_ptr<AppCacheRequestHandler>
113 InitializeForNavigationNetworkService(
114 const ResourceRequest& request,
anantae60d1d42017-06-20 04:16:27115 AppCacheNavigationHandleCore* appcache_handle_core,
116 URLLoaderFactoryGetter* url_loader_factory_getter);
anantaa2c8ec62017-06-09 23:44:05117
Michael Nordman85243a82017-10-24 01:24:50118 static bool IsMainResourceType(ResourceType type) {
119 return IsResourceTypeFrame(type) || type == RESOURCE_TYPE_SHARED_WORKER;
120 }
121
Anantanarayanan Iyengard125c4602017-08-19 00:46:56122 // Called by unittests to indicate that we are in test mode.
123 static void SetRunningInTests(bool in_tests);
Anantanarayanan Iyengard125c4602017-08-19 00:46:56124 static bool IsRunningInTests();
ananta2533bff62017-07-12 04:07:27125
[email protected]97e3edc2009-09-15 22:07:15126 private:
127 friend class AppCacheHost;
Michael Nordman85243a82017-10-24 01:24:50128 friend class AppCacheRequestHandlerTest;
[email protected]97e3edc2009-09-15 22:07:15129
[email protected]7e8e3dd2009-09-18 01:05:09130 // Callers should use AppCacheHost::CreateRequestHandler.
ananta4e8a5292017-05-04 03:07:15131 AppCacheRequestHandler(AppCacheHost* host,
132 ResourceType resource_type,
133 bool should_reset_appcache,
134 std::unique_ptr<AppCacheRequest> request);
[email protected]97e3edc2009-09-15 22:07:15135
[email protected]3367fc1d2009-11-09 00:09:21136 // AppCacheHost::Observer override
dchengc2282aa2014-10-21 12:07:58137 void OnDestructionImminent(AppCacheHost* host) override;
[email protected]97e3edc2009-09-15 22:07:15138
michaelnb8618402016-08-02 20:14:33139 // AppCacheServiceImpl::Observer override
140 void OnServiceDestructionImminent(AppCacheServiceImpl* service) override;
141
[email protected]3367fc1d2009-11-09 00:09:21142 // Helpers to instruct a waiting job with what response to
143 // deliver for the request we're handling.
avib7348942015-12-25 20:57:10144 void DeliverAppCachedResponse(const AppCacheEntry& entry,
145 int64_t cache_id,
avib7348942015-12-25 20:57:10146 const GURL& manifest_url,
[email protected]2abcade2012-01-05 00:19:40147 bool is_fallback,
148 const GURL& namespace_entry_url);
[email protected]3367fc1d2009-11-09 00:09:21149 void DeliverNetworkResponse();
150 void DeliverErrorResponse();
151
mmenke5289cbfd2015-10-29 19:36:17152 // Called just before the request is restarted. Grabs the reason for
153 // restarting, so can correctly continue to handle the request.
Michael Nordman85243a82017-10-24 01:24:50154 void OnPrepareToRestartURLRequest();
mmenke5289cbfd2015-10-29 19:36:17155
ananta46f3e942017-05-09 04:56:30156 // Helper method to create an AppCacheJob and populate job_.
mmenke5289cbfd2015-10-29 19:36:17157 // Caller takes ownership of returned value.
ananta46f3e942017-05-09 04:56:30158 std::unique_ptr<AppCacheJob> CreateJob(
mmenkeed0498b2015-12-08 23:20:42159 net::NetworkDelegate* network_delegate);
mmenke5289cbfd2015-10-29 19:36:17160
[email protected]3367fc1d2009-11-09 00:09:21161 // Helper to retrieve a pointer to the storage object.
[email protected]15f9cded2010-05-20 20:51:06162 AppCacheStorage* storage() const;
163
164 bool is_main_resource() const {
165 return IsMainResourceType(resource_type_);
166 }
[email protected]3367fc1d2009-11-09 00:09:21167
168 // Main-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06169 // Frame and SharedWorker main resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21170
ananta46f3e942017-05-09 04:56:30171 std::unique_ptr<AppCacheJob> MaybeLoadMainResource(
mmenke5289cbfd2015-10-29 19:36:17172 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21173
174 // AppCacheStorage::Delegate methods
dchengc2282aa2014-10-21 12:07:58175 void OnMainResponseFound(const GURL& url,
176 const AppCacheEntry& entry,
177 const GURL& fallback_url,
178 const AppCacheEntry& fallback_entry,
avib7348942015-12-25 20:57:10179 int64_t cache_id,
180 int64_t group_id,
dchengc2282aa2014-10-21 12:07:58181 const GURL& mainfest_url) override;
[email protected]3367fc1d2009-11-09 00:09:21182
183 // Sub-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06184 // Dedicated worker and all manner of sub-resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21185
ananta46f3e942017-05-09 04:56:30186 std::unique_ptr<AppCacheJob> MaybeLoadSubResource(
mmenke5289cbfd2015-10-29 19:36:17187 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21188 void ContinueMaybeLoadSubResource();
189
190 // AppCacheHost::Observer override
dchengc2282aa2014-10-21 12:07:58191 void OnCacheSelectionComplete(AppCacheHost* host) override;
[email protected]3367fc1d2009-11-09 00:09:21192
[email protected]3367fc1d2009-11-09 00:09:21193 // Data members -----------------------------------------------
194
195 // What host we're servicing a request for.
[email protected]7e8e3dd2009-09-18 01:05:09196 AppCacheHost* host_;
[email protected]3367fc1d2009-11-09 00:09:21197
[email protected]15f9cded2010-05-20 20:51:06198 // Frame vs subresource vs sharedworker loads are somewhat different.
[email protected]6c1e05212014-07-31 00:59:40199 ResourceType resource_type_;
[email protected]3367fc1d2009-11-09 00:09:21200
eustas27876842014-11-27 10:48:00201 // True if corresponding AppCache group should be resetted before load.
202 bool should_reset_appcache_;
203
[email protected]3367fc1d2009-11-09 00:09:21204 // Subresource requests wait until after cache selection completes.
205 bool is_waiting_for_cache_selection_;
206
207 // Info about the type of response we found for delivery.
208 // These are relevant for both main and subresource requests.
avib7348942015-12-25 20:57:10209 int64_t found_group_id_;
210 int64_t found_cache_id_;
[email protected]3367fc1d2009-11-09 00:09:21211 AppCacheEntry found_entry_;
212 AppCacheEntry found_fallback_entry_;
[email protected]2abcade2012-01-05 00:19:40213 GURL found_namespace_entry_url_;
[email protected]3367fc1d2009-11-09 00:09:21214 GURL found_manifest_url_;
215 bool found_network_namespace_;
216
[email protected]1e1f7d52010-08-25 21:38:20217 // True if a cache entry this handler attempted to return was
218 // not found in the disk cache. Once set, the handler will take
219 // no action on all subsequent intercept opportunities, so the
220 // request and any redirects will be handled by the network library.
221 bool cache_entry_not_found_;
222
mmenke5289cbfd2015-10-29 19:36:17223 // True if the next time this request is started, the response should be
224 // delivered from the network, bypassing the AppCache. Cleared after the next
225 // intercept opportunity.
226 bool is_delivering_network_response_;
227
[email protected]17d647852012-09-08 17:45:52228 // True if this->MaybeLoadResource(...) has been called in the past.
229 bool maybe_load_resource_executed_;
230
mmenke5289cbfd2015-10-29 19:36:17231 // The job we use to deliver a response. Only NULL during the following times:
232 // 1) Before request has started a job.
233 // 2) Request is not being handled by appcache.
234 // 3) Request has been cancelled, and the job killed.
ananta46f3e942017-05-09 04:56:30235 base::WeakPtr<AppCacheJob> job_;
[email protected]3367fc1d2009-11-09 00:09:21236
[email protected]5fcb289e2014-02-04 00:34:19237 // During a cross site navigation, we transfer ownership the AppcacheHost
238 // from the old processes structures over to the new structures.
dcheng59716272016-04-09 05:19:08239 std::unique_ptr<AppCacheHost> host_for_cross_site_transfer_;
michaelnbfea6ec2014-12-09 23:16:13240 int old_process_id_;
241 int old_host_id_;
[email protected]5fcb289e2014-02-04 00:34:19242
mmenke5289cbfd2015-10-29 19:36:17243 // Cached information about the response being currently served by the
244 // AppCache, if there is one.
245 int cache_id_;
246 GURL manifest_url_;
247
michaelnb8618402016-08-02 20:14:33248 // Backptr to the central service object.
249 AppCacheServiceImpl* service_;
250
ananta4e8a5292017-05-04 03:07:15251 std::unique_ptr<AppCacheRequest> request_;
252
ananta2533bff62017-07-12 04:07:27253 // Network service related members.
254
Michael Nordman85243a82017-10-24 01:24:50255 LoaderCallback loader_callback_;
anantaa2c8ec62017-06-09 23:44:05256
anantab9800e52017-07-29 18:04:13257 // Points to the getter for the network URL loader.
anantae60d1d42017-06-20 04:16:27258 scoped_refptr<URLLoaderFactoryGetter> network_url_loader_factory_getter_;
259
ananta2533bff62017-07-12 04:07:27260 // The AppCache host instance. We pass this to the
261 // AppCacheSubresourceURLFactory instance on creation.
262 base::WeakPtr<AppCacheHost> appcache_host_;
263
[email protected]3367fc1d2009-11-09 00:09:21264 DISALLOW_COPY_AND_ASSIGN(AppCacheRequestHandler);
[email protected]97e3edc2009-09-15 22:07:15265};
266
[email protected]98d6d4562014-06-25 20:57:55267} // namespace content
[email protected]97e3edc2009-09-15 22:07:15268
[email protected]98d6d4562014-06-25 20:57:55269#endif // CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_