blob: 96ecfa503c9c2cd2caf41322c76e5e000ced4a3e [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
John Abd-El-Malek46248032018-01-17 19:11:2330namespace network {
31struct ResourceResponseHead;
32}
33
[email protected]073f24d2014-04-09 00:00:2334namespace content {
ananta46f3e942017-05-09 04:56:3035class AppCacheJob;
anantaa2c8ec62017-06-09 23:44:0536class AppCacheNavigationHandleCore;
Anantanarayanan Iyengar2130d272017-09-01 18:21:1437class AppCacheSubresourceURLFactory;
ananta4e8a5292017-05-04 03:07:1538class AppCacheRequest;
[email protected]073f24d2014-04-09 00:00:2339class AppCacheRequestHandlerTest;
[email protected]3367fc1d2009-11-09 00:09:2140class AppCacheURLRequestJob;
ananta2533bff62017-07-12 04:07:2741class AppCacheHost;
[email protected]3367fc1d2009-11-09 00:09:2142
[email protected]6981d9632010-11-30 21:34:0243// An instance is created for each net::URLRequest. The instance survives all
44// http transactions involved in the processing of its net::URLRequest, and is
[email protected]97e3edc2009-09-15 22:07:1545// given the opportunity to hijack the request along the way. Callers
46// should use AppCacheHost::CreateRequestHandler to manufacture instances
47// that can retrieve resources for a particular host.
[email protected]98d6d4562014-06-25 20:57:5548class CONTENT_EXPORT AppCacheRequestHandler
[email protected]ea8e1812012-02-15 22:07:3449 : public base::SupportsUserData::Data,
[email protected]bfe19482011-09-30 22:14:5750 public AppCacheHost::Observer,
michaelnb8618402016-08-02 20:14:3351 public AppCacheServiceImpl::Observer,
anantaa2c8ec62017-06-09 23:44:0552 public AppCacheStorage::Delegate,
53 public URLLoaderRequestHandler {
[email protected]97e3edc2009-09-15 22:07:1554 public:
dchengc2282aa2014-10-21 12:07:5855 ~AppCacheRequestHandler() override;
[email protected]7e8e3dd2009-09-18 01:05:0956
[email protected]3367fc1d2009-11-09 00:09:2157 // These are called on each request intercept opportunity.
Michael Nordman85243a82017-10-24 01:24:5058 // When the NetworkService is not enabled, the AppCacheInterceptor calls these
59 // methods directly. When the NetworkService is enabled, the various
60 // MaybeCreateLoader methods below call call these internally.
ananta46f3e942017-05-09 04:56:3061 AppCacheJob* MaybeLoadResource(net::NetworkDelegate* network_delegate);
62 AppCacheJob* MaybeLoadFallbackForRedirect(
[email protected]9f170462012-08-24 01:06:5863 net::NetworkDelegate* network_delegate,
64 const GURL& location);
ananta46f3e942017-05-09 04:56:3065 AppCacheJob* MaybeLoadFallbackForResponse(
mmenke5289cbfd2015-10-29 19:36:1766 net::NetworkDelegate* network_delegate);
[email protected]97e3edc2009-09-15 22:07:1567
avib7348942015-12-25 20:57:1068 void GetExtraResponseInfo(int64_t* cache_id, GURL* manifest_url);
[email protected]97e3edc2009-09-15 22:07:1569
[email protected]5fcb289e2014-02-04 00:34:1970 // Methods to support cross site navigations.
71 void PrepareForCrossSiteTransfer(int old_process_id);
72 void CompleteCrossSiteTransfer(int new_process_id, int new_host_id);
michaelnbfea6ec2014-12-09 23:16:1373 void MaybeCompleteCrossSiteTransferInOldProcess(int old_process_id);
[email protected]5fcb289e2014-02-04 00:34:1974
michaeln054c2fd42016-01-16 02:33:4875 // Useful for detecting storage partition mismatches in the context
76 // of cross site transfer navigations.
77 bool SanityCheckIsSameService(AppCacheService* service) {
78 return !host_ || (host_->service() == service);
79 }
80
Michael Nordman85243a82017-10-24 01:24:5081 // NetworkService loading
Anantanarayanan Iyengard125c4602017-08-19 00:46:5682
Michael Nordman85243a82017-10-24 01:24:5083 // URLLoaderRequestHandler overrides - main resource loading.
84 // These methods are used by the NavigationURLLoaderNetworkService.
85 // Internally they use same methods used by the network library based impl,
86 // MaybeLoadResource and MaybeLoadFallbackForResponse.
87 // Eventually one of the Deliver*Response() methods is called and the
88 // LoaderCallback is invoked.
John Abd-El-Malek1df61792018-01-12 20:40:4589 void MaybeCreateLoader(const network::ResourceRequest& resource_request,
Michael Nordman85243a82017-10-24 01:24:5090 ResourceContext* resource_context,
91 LoaderCallback callback) override;
92 // MaybeCreateLoaderForResponse always returns synchronously.
93 bool MaybeCreateLoaderForResponse(
John Abd-El-Malek46248032018-01-17 19:11:2394 const network::ResourceResponseHead& response,
John Abd-El-Malekb165dc52018-01-18 17:12:1895 network::mojom::URLLoaderPtr* loader,
Tsuyoshi Horob8d512a2018-01-25 17:01:5996 network::mojom::URLLoaderClientRequest* client_request,
97 ThrottlingURLLoader* url_loader) override;
Kinuko Yasuda250577c2017-10-29 02:51:2498 base::Optional<SubresourceLoaderParams> MaybeCreateSubresourceLoaderParams()
99 override;
Michael Nordman85243a82017-10-24 01:24:50100
101 // These methods are used for subresource loading by the
102 // AppCacheSubresourceURLFactory::SubresourceLoader class.
103 // Internally they use same methods used by the network library based impl,
104 // MaybeLoadResource, MaybeLoadFallbackForResponse, and
105 // MaybeLoadFallbackForRedirect. Eventually one of the Deliver*Response()
106 // methods is called and the LoaderCallback is invoked.
John Abd-El-Malek1df61792018-01-12 20:40:45107 void MaybeCreateSubresourceLoader(
108 const network::ResourceRequest& resource_request,
109 LoaderCallback callback);
John Abd-El-Malek46248032018-01-17 19:11:23110 void MaybeFallbackForSubresourceResponse(
111 const network::ResourceResponseHead& response,
112 LoaderCallback callback);
Michael Nordman85243a82017-10-24 01:24:50113 void MaybeFallbackForSubresourceRedirect(
114 const net::RedirectInfo& redirect_info,
115 LoaderCallback callback);
116 void MaybeFollowSubresourceRedirect(const net::RedirectInfo& redirect_info,
117 LoaderCallback callback);
[email protected]15f9cded2010-05-20 20:51:06118
anantaa2c8ec62017-06-09 23:44:05119 static std::unique_ptr<AppCacheRequestHandler>
120 InitializeForNavigationNetworkService(
John Abd-El-Malek1df61792018-01-12 20:40:45121 const network::ResourceRequest& request,
anantae60d1d42017-06-20 04:16:27122 AppCacheNavigationHandleCore* appcache_handle_core,
123 URLLoaderFactoryGetter* url_loader_factory_getter);
anantaa2c8ec62017-06-09 23:44:05124
Michael Nordman85243a82017-10-24 01:24:50125 static bool IsMainResourceType(ResourceType type) {
126 return IsResourceTypeFrame(type) || type == RESOURCE_TYPE_SHARED_WORKER;
127 }
128
Anantanarayanan Iyengard125c4602017-08-19 00:46:56129 // Called by unittests to indicate that we are in test mode.
130 static void SetRunningInTests(bool in_tests);
Anantanarayanan Iyengard125c4602017-08-19 00:46:56131 static bool IsRunningInTests();
ananta2533bff62017-07-12 04:07:27132
[email protected]97e3edc2009-09-15 22:07:15133 private:
134 friend class AppCacheHost;
Michael Nordman85243a82017-10-24 01:24:50135 friend class AppCacheRequestHandlerTest;
[email protected]97e3edc2009-09-15 22:07:15136
[email protected]7e8e3dd2009-09-18 01:05:09137 // Callers should use AppCacheHost::CreateRequestHandler.
ananta4e8a5292017-05-04 03:07:15138 AppCacheRequestHandler(AppCacheHost* host,
139 ResourceType resource_type,
140 bool should_reset_appcache,
141 std::unique_ptr<AppCacheRequest> request);
[email protected]97e3edc2009-09-15 22:07:15142
[email protected]3367fc1d2009-11-09 00:09:21143 // AppCacheHost::Observer override
dchengc2282aa2014-10-21 12:07:58144 void OnDestructionImminent(AppCacheHost* host) override;
[email protected]97e3edc2009-09-15 22:07:15145
michaelnb8618402016-08-02 20:14:33146 // AppCacheServiceImpl::Observer override
147 void OnServiceDestructionImminent(AppCacheServiceImpl* service) override;
148
[email protected]3367fc1d2009-11-09 00:09:21149 // Helpers to instruct a waiting job with what response to
150 // deliver for the request we're handling.
avib7348942015-12-25 20:57:10151 void DeliverAppCachedResponse(const AppCacheEntry& entry,
152 int64_t cache_id,
avib7348942015-12-25 20:57:10153 const GURL& manifest_url,
[email protected]2abcade2012-01-05 00:19:40154 bool is_fallback,
155 const GURL& namespace_entry_url);
[email protected]3367fc1d2009-11-09 00:09:21156 void DeliverNetworkResponse();
157 void DeliverErrorResponse();
158
mmenke5289cbfd2015-10-29 19:36:17159 // Called just before the request is restarted. Grabs the reason for
160 // restarting, so can correctly continue to handle the request.
Michael Nordman85243a82017-10-24 01:24:50161 void OnPrepareToRestartURLRequest();
mmenke5289cbfd2015-10-29 19:36:17162
ananta46f3e942017-05-09 04:56:30163 // Helper method to create an AppCacheJob and populate job_.
mmenke5289cbfd2015-10-29 19:36:17164 // Caller takes ownership of returned value.
ananta46f3e942017-05-09 04:56:30165 std::unique_ptr<AppCacheJob> CreateJob(
mmenkeed0498b2015-12-08 23:20:42166 net::NetworkDelegate* network_delegate);
mmenke5289cbfd2015-10-29 19:36:17167
[email protected]3367fc1d2009-11-09 00:09:21168 // Helper to retrieve a pointer to the storage object.
[email protected]15f9cded2010-05-20 20:51:06169 AppCacheStorage* storage() const;
170
171 bool is_main_resource() const {
172 return IsMainResourceType(resource_type_);
173 }
[email protected]3367fc1d2009-11-09 00:09:21174
175 // Main-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06176 // Frame and SharedWorker main resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21177
ananta46f3e942017-05-09 04:56:30178 std::unique_ptr<AppCacheJob> MaybeLoadMainResource(
mmenke5289cbfd2015-10-29 19:36:17179 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21180
181 // AppCacheStorage::Delegate methods
dchengc2282aa2014-10-21 12:07:58182 void OnMainResponseFound(const GURL& url,
183 const AppCacheEntry& entry,
184 const GURL& fallback_url,
185 const AppCacheEntry& fallback_entry,
avib7348942015-12-25 20:57:10186 int64_t cache_id,
187 int64_t group_id,
dchengc2282aa2014-10-21 12:07:58188 const GURL& mainfest_url) override;
[email protected]3367fc1d2009-11-09 00:09:21189
Kinuko Yasuda8757204a2017-11-04 07:50:13190 // NetworkService loading:
Ken Rockot387ddd5a2018-01-30 19:18:40191 // Called when a |callback| that is originally given to |MaybeCreateLoader()|
192 // runs for the main resource. This flips |should_create_subresource_loader_|
193 // if a non-null |handler| is given. Always invokes |callback| with |handler|.
Kinuko Yasuda8757204a2017-11-04 07:50:13194 void RunLoaderCallbackForMainResource(
195 LoaderCallback callback,
Ken Rockot387ddd5a2018-01-30 19:18:40196 SingleRequestURLLoaderFactory::RequestHandler handler);
Kinuko Yasuda8757204a2017-11-04 07:50:13197
[email protected]3367fc1d2009-11-09 00:09:21198 // Sub-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06199 // Dedicated worker and all manner of sub-resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21200
ananta46f3e942017-05-09 04:56:30201 std::unique_ptr<AppCacheJob> MaybeLoadSubResource(
mmenke5289cbfd2015-10-29 19:36:17202 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21203 void ContinueMaybeLoadSubResource();
204
205 // AppCacheHost::Observer override
dchengc2282aa2014-10-21 12:07:58206 void OnCacheSelectionComplete(AppCacheHost* host) override;
[email protected]3367fc1d2009-11-09 00:09:21207
208 // Data members -----------------------------------------------
209
210 // What host we're servicing a request for.
[email protected]7e8e3dd2009-09-18 01:05:09211 AppCacheHost* host_;
[email protected]3367fc1d2009-11-09 00:09:21212
[email protected]15f9cded2010-05-20 20:51:06213 // Frame vs subresource vs sharedworker loads are somewhat different.
[email protected]6c1e05212014-07-31 00:59:40214 ResourceType resource_type_;
[email protected]3367fc1d2009-11-09 00:09:21215
eustas27876842014-11-27 10:48:00216 // True if corresponding AppCache group should be resetted before load.
217 bool should_reset_appcache_;
218
[email protected]3367fc1d2009-11-09 00:09:21219 // Subresource requests wait until after cache selection completes.
220 bool is_waiting_for_cache_selection_;
221
222 // Info about the type of response we found for delivery.
223 // These are relevant for both main and subresource requests.
avib7348942015-12-25 20:57:10224 int64_t found_group_id_;
225 int64_t found_cache_id_;
[email protected]3367fc1d2009-11-09 00:09:21226 AppCacheEntry found_entry_;
227 AppCacheEntry found_fallback_entry_;
[email protected]2abcade2012-01-05 00:19:40228 GURL found_namespace_entry_url_;
[email protected]3367fc1d2009-11-09 00:09:21229 GURL found_manifest_url_;
230 bool found_network_namespace_;
231
[email protected]1e1f7d52010-08-25 21:38:20232 // True if a cache entry this handler attempted to return was
233 // not found in the disk cache. Once set, the handler will take
234 // no action on all subsequent intercept opportunities, so the
235 // request and any redirects will be handled by the network library.
236 bool cache_entry_not_found_;
237
mmenke5289cbfd2015-10-29 19:36:17238 // True if the next time this request is started, the response should be
239 // delivered from the network, bypassing the AppCache. Cleared after the next
240 // intercept opportunity.
241 bool is_delivering_network_response_;
242
[email protected]17d647852012-09-08 17:45:52243 // True if this->MaybeLoadResource(...) has been called in the past.
244 bool maybe_load_resource_executed_;
245
mmenke5289cbfd2015-10-29 19:36:17246 // The job we use to deliver a response. Only NULL during the following times:
247 // 1) Before request has started a job.
248 // 2) Request is not being handled by appcache.
249 // 3) Request has been cancelled, and the job killed.
ananta46f3e942017-05-09 04:56:30250 base::WeakPtr<AppCacheJob> job_;
[email protected]3367fc1d2009-11-09 00:09:21251
[email protected]5fcb289e2014-02-04 00:34:19252 // During a cross site navigation, we transfer ownership the AppcacheHost
253 // from the old processes structures over to the new structures.
dcheng59716272016-04-09 05:19:08254 std::unique_ptr<AppCacheHost> host_for_cross_site_transfer_;
michaelnbfea6ec2014-12-09 23:16:13255 int old_process_id_;
256 int old_host_id_;
[email protected]5fcb289e2014-02-04 00:34:19257
mmenke5289cbfd2015-10-29 19:36:17258 // Cached information about the response being currently served by the
259 // AppCache, if there is one.
260 int cache_id_;
261 GURL manifest_url_;
262
michaelnb8618402016-08-02 20:14:33263 // Backptr to the central service object.
264 AppCacheServiceImpl* service_;
265
ananta4e8a5292017-05-04 03:07:15266 std::unique_ptr<AppCacheRequest> request_;
267
ananta2533bff62017-07-12 04:07:27268 // Network service related members.
269
Michael Nordman85243a82017-10-24 01:24:50270 LoaderCallback loader_callback_;
anantaa2c8ec62017-06-09 23:44:05271
Kinuko Yasuda8757204a2017-11-04 07:50:13272 // Flipped to true if AppCache wants to handle subresource requests
Ken Rockot387ddd5a2018-01-30 19:18:40273 // (i.e. when |loader_callback_| is fired with a non-null
274 // RequestHandler for non-error cases.
Kinuko Yasuda8757204a2017-11-04 07:50:13275 bool should_create_subresource_loader_ = false;
276
anantab9800e52017-07-29 18:04:13277 // Points to the getter for the network URL loader.
anantae60d1d42017-06-20 04:16:27278 scoped_refptr<URLLoaderFactoryGetter> network_url_loader_factory_getter_;
279
ananta2533bff62017-07-12 04:07:27280 // The AppCache host instance. We pass this to the
281 // AppCacheSubresourceURLFactory instance on creation.
282 base::WeakPtr<AppCacheHost> appcache_host_;
283
Kinuko Yasuda8757204a2017-11-04 07:50:13284 base::WeakPtrFactory<AppCacheRequestHandler> weak_factory_;
285
[email protected]3367fc1d2009-11-09 00:09:21286 DISALLOW_COPY_AND_ASSIGN(AppCacheRequestHandler);
[email protected]97e3edc2009-09-15 22:07:15287};
288
[email protected]98d6d4562014-06-25 20:57:55289} // namespace content
[email protected]97e3edc2009-09-15 22:07:15290
[email protected]98d6d4562014-06-25 20:57:55291#endif // CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_