blob: b60e0cb074380751bbed01e7ae849f2d4201078d [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]98d6d4562014-06-25 20:57:5515#include "content/browser/appcache/appcache_entry.h"
16#include "content/browser/appcache/appcache_host.h"
michaelnb8618402016-08-02 20:14:3317#include "content/browser/appcache/appcache_service_impl.h"
Kinuko Yasuda7f3e1722018-03-26 08:58:5818#include "content/browser/loader/navigation_loader_interceptor.h"
Clark DuVallf77e164d2019-10-24 23:53:2519#include "content/browser/loader/single_request_url_loader_factory.h"
Dave Tapuskac83e57a82021-03-12 04:15:4620#include "content/browser/renderer_host/frame_tree_node.h"
[email protected]98d6d4562014-06-25 20:57:5521#include "content/common/content_export.h"
Julie Jeongeun Kim633d2e72019-10-29 09:03:0322#include "mojo/public/cpp/bindings/pending_receiver.h"
Julie Jeongeun Kim917c197902019-11-26 03:36:1423#include "mojo/public/cpp/bindings/pending_remote.h"
Kinuko Yasudabe4ad292018-07-06 01:33:3924#include "services/network/public/cpp/shared_url_loader_factory.h"
Lucas Furukawa Gadani68c380b2019-12-04 19:38:3425#include "services/network/public/mojom/url_response_head.mojom-forward.h"
Minggang Wangee5af392020-02-05 02:55:2826#include "third_party/blink/public/mojom/loader/resource_load_info.mojom-shared.h"
[email protected]97e3edc2009-09-15 22:07:1527
[email protected]edfe7fab2010-11-28 13:11:5228namespace net {
[email protected]9f170462012-08-24 01:06:5829class NetworkDelegate;
[email protected]97e3edc2009-09-15 22:07:1530class URLRequest;
[email protected]edfe7fab2010-11-28 13:11:5231} // namespace net
[email protected]97e3edc2009-09-15 22:07:1532
[email protected]073f24d2014-04-09 00:00:2333namespace content {
Victor Costanb9693952020-02-21 09:38:1034class AppCacheURLLoader;
Anantanarayanan Iyengar2130d272017-09-01 18:21:1435class AppCacheSubresourceURLFactory;
ananta4e8a5292017-05-04 03:07:1536class AppCacheRequest;
[email protected]073f24d2014-04-09 00:00:2337class AppCacheRequestHandlerTest;
ananta2533bff62017-07-12 04:07:2738class AppCacheHost;
[email protected]3367fc1d2009-11-09 00:09:2139
Marijn Kruisselbrinkb590da52021-01-14 17:48:0740CONTENT_EXPORT extern const base::Feature kAppCacheAlwaysFallbackToNetwork;
41
[email protected]6981d9632010-11-30 21:34:0242// An instance is created for each net::URLRequest. The instance survives all
43// http transactions involved in the processing of its net::URLRequest, and is
[email protected]97e3edc2009-09-15 22:07:1544// given the opportunity to hijack the request along the way. Callers
45// should use AppCacheHost::CreateRequestHandler to manufacture instances
46// that can retrieve resources for a particular host.
Finditb796d002018-10-21 07:15:2247class CONTENT_EXPORT AppCacheRequestHandler
Victor Costanad5a56c92020-02-20 19:28:4348 : public AppCacheHost::Observer,
michaelnb8618402016-08-02 20:14:3349 public AppCacheServiceImpl::Observer,
anantaa2c8ec62017-06-09 23:44:0550 public AppCacheStorage::Delegate,
Kinuko Yasuda7f3e1722018-03-26 08:58:5851 public NavigationLoaderInterceptor {
[email protected]97e3edc2009-09-15 22:07:1552 public:
Clark DuVallf77e164d2019-10-24 23:53:2553 using AppCacheLoaderCallback =
54 base::OnceCallback<void(SingleRequestURLLoaderFactory::RequestHandler)>;
55
dchengc2282aa2014-10-21 12:07:5856 ~AppCacheRequestHandler() override;
[email protected]7e8e3dd2009-09-18 01:05:0957
Michael Nordman85243a82017-10-24 01:24:5058 // NetworkService loading
Anantanarayanan Iyengard125c4602017-08-19 00:46:5659
Kinuko Yasuda7f3e1722018-03-26 08:58:5860 // NavigationLoaderInterceptor overrides - main resource loading.
arthursonzogni1fd60e62018-05-09 15:57:3261 // These methods are used by the NavigationURLLoaderImpl.
Victor Costanc65793b2019-09-02 23:38:3662 // Internally they use MaybeLoadResource and MaybeLoadFallbackForResponse.
Michael Nordman85243a82017-10-24 01:24:5063 // Eventually one of the Deliver*Response() methods is called and the
64 // LoaderCallback is invoked.
Matt Falkenhagen43765122018-08-22 19:37:2265 void MaybeCreateLoader(
66 const network::ResourceRequest& tentative_resource_request,
Clark DuVall1cc8323a2019-06-28 00:27:0567 BrowserContext* browser_context,
Matt Falkenhagen43765122018-08-22 19:37:2268 LoaderCallback callback,
69 FallbackCallback fallback_callback) override;
Michael Nordman85243a82017-10-24 01:24:5070 // MaybeCreateLoaderForResponse always returns synchronously.
71 bool MaybeCreateLoaderForResponse(
Tsuyoshi Horod0a953d2019-01-23 04:38:1472 const network::ResourceRequest& request,
Lucas Furukawa Gadanid661c0d2019-12-02 19:58:1673 network::mojom::URLResponseHeadPtr* response_head,
Minggang Wang47a78222019-06-28 17:05:4674 mojo::ScopedDataPipeConsumerHandle* response_body,
Julie Jeongeun Kim917c197902019-11-26 03:36:1475 mojo::PendingRemote<network::mojom::URLLoader>* loader,
Julie Jeongeun Kim633d2e72019-10-29 09:03:0376 mojo::PendingReceiver<network::mojom::URLLoaderClient>* client_receiver,
Minggang Wangf8b459d2019-10-25 11:08:3877 blink::ThrottlingURLLoader* url_loader,
Tsuyoshi Horo91727e92019-09-26 05:01:0278 bool* skip_other_interceptors,
79 bool* will_return_unsafe_redirect) override;
Anton Bikineevf62d1bf2021-05-15 17:56:0780 absl::optional<SubresourceLoaderParams> MaybeCreateSubresourceLoaderParams()
Kinuko Yasuda250577c2017-10-29 02:51:2481 override;
Michael Nordman85243a82017-10-24 01:24:5082
83 // These methods are used for subresource loading by the
84 // AppCacheSubresourceURLFactory::SubresourceLoader class.
Victor Costanc65793b2019-09-02 23:38:3685 // Internally they use MaybeLoadResource, MaybeLoadFallbackForResponse, and
Michael Nordman85243a82017-10-24 01:24:5086 // MaybeLoadFallbackForRedirect. Eventually one of the Deliver*Response()
87 // methods is called and the LoaderCallback is invoked.
John Abd-El-Malek1df61792018-01-12 20:40:4588 void MaybeCreateSubresourceLoader(
89 const network::ResourceRequest& resource_request,
Clark DuVallf77e164d2019-10-24 23:53:2590 AppCacheLoaderCallback callback);
John Abd-El-Malek46248032018-01-17 19:11:2391 void MaybeFallbackForSubresourceResponse(
Lucas Furukawa Gadani68c380b2019-12-04 19:38:3492 network::mojom::URLResponseHeadPtr response,
Clark DuVallf77e164d2019-10-24 23:53:2593 AppCacheLoaderCallback callback);
Michael Nordman85243a82017-10-24 01:24:5094 void MaybeFallbackForSubresourceRedirect(
95 const net::RedirectInfo& redirect_info,
Clark DuVallf77e164d2019-10-24 23:53:2596 AppCacheLoaderCallback callback);
Michael Nordman85243a82017-10-24 01:24:5097 void MaybeFollowSubresourceRedirect(const net::RedirectInfo& redirect_info,
Clark DuVallf77e164d2019-10-24 23:53:2598 AppCacheLoaderCallback callback);
[email protected]15f9cded2010-05-20 20:51:0699
anantaa2c8ec62017-06-09 23:44:05100 static std::unique_ptr<AppCacheRequestHandler>
Hiroki Nakagawaff43e4472018-07-26 07:06:46101 InitializeForMainResourceNetworkService(
John Abd-El-Malek1df61792018-01-12 20:40:45102 const network::ResourceRequest& request,
Dave Tapuskac83e57a82021-03-12 04:15:46103 base::WeakPtr<AppCacheHost> appcache_host,
104 int frame_tree_node_id);
anantaa2c8ec62017-06-09 23:44:05105
Julie Jeongeun Kimea064682020-10-19 12:10:33106 static bool IsMainRequestDestination(
107 network::mojom::RequestDestination destination);
Michael Nordman85243a82017-10-24 01:24:50108
Anantanarayanan Iyengard125c4602017-08-19 00:46:56109 // Called by unittests to indicate that we are in test mode.
110 static void SetRunningInTests(bool in_tests);
Anantanarayanan Iyengard125c4602017-08-19 00:46:56111 static bool IsRunningInTests();
ananta2533bff62017-07-12 04:07:27112
[email protected]97e3edc2009-09-15 22:07:15113 private:
114 friend class AppCacheHost;
Michael Nordman85243a82017-10-24 01:24:50115 friend class AppCacheRequestHandlerTest;
[email protected]97e3edc2009-09-15 22:07:15116
[email protected]7e8e3dd2009-09-18 01:05:09117 // Callers should use AppCacheHost::CreateRequestHandler.
ananta4e8a5292017-05-04 03:07:15118 AppCacheRequestHandler(AppCacheHost* host,
Julie Jeongeun Kimea064682020-10-19 12:10:33119 network::mojom::RequestDestination request_destination,
ananta4e8a5292017-05-04 03:07:15120 bool should_reset_appcache,
Dave Tapuskac83e57a82021-03-12 04:15:46121 std::unique_ptr<AppCacheRequest> request,
122 int frame_tree_node_id);
[email protected]97e3edc2009-09-15 22:07:15123
Clark DuVallf77e164d2019-10-24 23:53:25124 void MaybeCreateLoaderInternal(
125 const network::ResourceRequest& resource_request,
126 AppCacheLoaderCallback callback);
127
Victor Costanc65793b2019-09-02 23:38:36128 // AppCacheHost::Observer.
dchengc2282aa2014-10-21 12:07:58129 void OnDestructionImminent(AppCacheHost* host) override;
[email protected]97e3edc2009-09-15 22:07:15130
Victor Costanc65793b2019-09-02 23:38:36131 // AppCacheServiceImpl::Observer.
michaelnb8618402016-08-02 20:14:33132 void OnServiceDestructionImminent(AppCacheServiceImpl* service) override;
133
[email protected]3367fc1d2009-11-09 00:09:21134 // Helpers to instruct a waiting job with what response to
135 // deliver for the request we're handling.
avib7348942015-12-25 20:57:10136 void DeliverAppCachedResponse(const AppCacheEntry& entry,
137 int64_t cache_id,
avib7348942015-12-25 20:57:10138 const GURL& manifest_url,
[email protected]2abcade2012-01-05 00:19:40139 bool is_fallback,
140 const GURL& namespace_entry_url);
[email protected]3367fc1d2009-11-09 00:09:21141 void DeliverNetworkResponse();
142 void DeliverErrorResponse();
143
Victor Costanb9693952020-02-21 09:38:10144 // Helper method to create an AppCacheURLLoader and populate job_.
mmenke5289cbfd2015-10-29 19:36:17145 // Caller takes ownership of returned value.
Victor Costanb9693952020-02-21 09:38:10146 std::unique_ptr<AppCacheURLLoader> CreateLoader(
mmenkeed0498b2015-12-08 23:20:42147 net::NetworkDelegate* network_delegate);
mmenke5289cbfd2015-10-29 19:36:17148
[email protected]3367fc1d2009-11-09 00:09:21149 // Helper to retrieve a pointer to the storage object.
[email protected]15f9cded2010-05-20 20:51:06150 AppCacheStorage* storage() const;
151
152 bool is_main_resource() const {
Julie Jeongeun Kimea064682020-10-19 12:10:33153 return IsMainRequestDestination(request_destination_);
[email protected]15f9cded2010-05-20 20:51:06154 }
[email protected]3367fc1d2009-11-09 00:09:21155
Victor Costanc65793b2019-09-02 23:38:36156 // These are called on each request intercept opportunity, by the various
157 // MaybeCreateLoader methods in the public API.
Victor Costanb9693952020-02-21 09:38:10158 AppCacheURLLoader* MaybeLoadResource(net::NetworkDelegate* network_delegate);
159 AppCacheURLLoader* MaybeLoadFallbackForRedirect(
Victor Costanc65793b2019-09-02 23:38:36160 net::NetworkDelegate* network_delegate,
161 const GURL& location);
Victor Costanb9693952020-02-21 09:38:10162 AppCacheURLLoader* MaybeLoadFallbackForResponse(
Victor Costanc65793b2019-09-02 23:38:36163 net::NetworkDelegate* network_delegate);
164
165 void GetExtraResponseInfo(int64_t* cache_id, GURL* manifest_url);
166
[email protected]3367fc1d2009-11-09 00:09:21167 // Main-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06168 // Frame and SharedWorker main resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21169
Victor Costanb9693952020-02-21 09:38:10170 std::unique_ptr<AppCacheURLLoader> MaybeLoadMainResource(
mmenke5289cbfd2015-10-29 19:36:17171 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21172
173 // AppCacheStorage::Delegate methods
dchengc2282aa2014-10-21 12:07:58174 void OnMainResponseFound(const GURL& url,
175 const AppCacheEntry& entry,
176 const GURL& fallback_url,
177 const AppCacheEntry& fallback_entry,
avib7348942015-12-25 20:57:10178 int64_t cache_id,
179 int64_t group_id,
dchengc2282aa2014-10-21 12:07:58180 const GURL& mainfest_url) override;
[email protected]3367fc1d2009-11-09 00:09:21181
Kinuko Yasuda8757204a2017-11-04 07:50:13182 // NetworkService loading:
Ken Rockot387ddd5a2018-01-30 19:18:40183 // Called when a |callback| that is originally given to |MaybeCreateLoader()|
184 // runs for the main resource. This flips |should_create_subresource_loader_|
185 // if a non-null |handler| is given. Always invokes |callback| with |handler|.
Kinuko Yasuda8757204a2017-11-04 07:50:13186 void RunLoaderCallbackForMainResource(
Clark DuVallf77e164d2019-10-24 23:53:25187 BrowserContext* browser_context,
Kinuko Yasuda8757204a2017-11-04 07:50:13188 LoaderCallback callback,
Ken Rockot387ddd5a2018-01-30 19:18:40189 SingleRequestURLLoaderFactory::RequestHandler handler);
Kinuko Yasuda8757204a2017-11-04 07:50:13190
[email protected]3367fc1d2009-11-09 00:09:21191 // Sub-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06192 // Dedicated worker and all manner of sub-resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21193
Victor Costanb9693952020-02-21 09:38:10194 std::unique_ptr<AppCacheURLLoader> MaybeLoadSubResource(
mmenke5289cbfd2015-10-29 19:36:17195 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21196 void ContinueMaybeLoadSubResource();
197
198 // AppCacheHost::Observer override
dchengc2282aa2014-10-21 12:07:58199 void OnCacheSelectionComplete(AppCacheHost* host) override;
[email protected]3367fc1d2009-11-09 00:09:21200
201 // Data members -----------------------------------------------
202
203 // What host we're servicing a request for.
[email protected]7e8e3dd2009-09-18 01:05:09204 AppCacheHost* host_;
[email protected]3367fc1d2009-11-09 00:09:21205
[email protected]15f9cded2010-05-20 20:51:06206 // Frame vs subresource vs sharedworker loads are somewhat different.
Julie Jeongeun Kimea064682020-10-19 12:10:33207 network::mojom::RequestDestination request_destination_;
[email protected]3367fc1d2009-11-09 00:09:21208
eustas27876842014-11-27 10:48:00209 // True if corresponding AppCache group should be resetted before load.
210 bool should_reset_appcache_;
211
[email protected]3367fc1d2009-11-09 00:09:21212 // Subresource requests wait until after cache selection completes.
213 bool is_waiting_for_cache_selection_;
214
215 // Info about the type of response we found for delivery.
216 // These are relevant for both main and subresource requests.
avib7348942015-12-25 20:57:10217 int64_t found_group_id_;
218 int64_t found_cache_id_;
[email protected]3367fc1d2009-11-09 00:09:21219 AppCacheEntry found_entry_;
220 AppCacheEntry found_fallback_entry_;
[email protected]2abcade2012-01-05 00:19:40221 GURL found_namespace_entry_url_;
[email protected]3367fc1d2009-11-09 00:09:21222 GURL found_manifest_url_;
223 bool found_network_namespace_;
224
[email protected]1e1f7d52010-08-25 21:38:20225 // True if a cache entry this handler attempted to return was
226 // not found in the disk cache. Once set, the handler will take
227 // no action on all subsequent intercept opportunities, so the
228 // request and any redirects will be handled by the network library.
229 bool cache_entry_not_found_;
230
mmenke5289cbfd2015-10-29 19:36:17231 // True if the next time this request is started, the response should be
232 // delivered from the network, bypassing the AppCache. Cleared after the next
233 // intercept opportunity.
234 bool is_delivering_network_response_;
235
[email protected]17d647852012-09-08 17:45:52236 // True if this->MaybeLoadResource(...) has been called in the past.
237 bool maybe_load_resource_executed_;
238
mmenke5289cbfd2015-10-29 19:36:17239 // The job we use to deliver a response. Only NULL during the following times:
240 // 1) Before request has started a job.
241 // 2) Request is not being handled by appcache.
242 // 3) Request has been cancelled, and the job killed.
Victor Costanb9693952020-02-21 09:38:10243 base::WeakPtr<AppCacheURLLoader> loader_;
[email protected]3367fc1d2009-11-09 00:09:21244
mmenke5289cbfd2015-10-29 19:36:17245 // Cached information about the response being currently served by the
246 // AppCache, if there is one.
247 int cache_id_;
248 GURL manifest_url_;
249
michaelnb8618402016-08-02 20:14:33250 // Backptr to the central service object.
251 AppCacheServiceImpl* service_;
252
ananta4e8a5292017-05-04 03:07:15253 std::unique_ptr<AppCacheRequest> request_;
254
ananta2533bff62017-07-12 04:07:27255 // Network service related members.
256
Clark DuVallf77e164d2019-10-24 23:53:25257 AppCacheLoaderCallback loader_callback_;
anantaa2c8ec62017-06-09 23:44:05258
Kinuko Yasuda8757204a2017-11-04 07:50:13259 // Flipped to true if AppCache wants to handle subresource requests
Ken Rockot387ddd5a2018-01-30 19:18:40260 // (i.e. when |loader_callback_| is fired with a non-null
261 // RequestHandler for non-error cases.
Kinuko Yasuda8757204a2017-11-04 07:50:13262 bool should_create_subresource_loader_ = false;
263
Dave Tapuskac83e57a82021-03-12 04:15:46264 int frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
265
ananta2533bff62017-07-12 04:07:27266 // The AppCache host instance. We pass this to the
267 // AppCacheSubresourceURLFactory instance on creation.
268 base::WeakPtr<AppCacheHost> appcache_host_;
269
Jeremy Roman3bca4bf2019-07-11 03:41:25270 base::WeakPtrFactory<AppCacheRequestHandler> weak_factory_{this};
Kinuko Yasuda8757204a2017-11-04 07:50:13271
[email protected]3367fc1d2009-11-09 00:09:21272 DISALLOW_COPY_AND_ASSIGN(AppCacheRequestHandler);
[email protected]97e3edc2009-09-15 22:07:15273};
274
[email protected]98d6d4562014-06-25 20:57:55275} // namespace content
[email protected]97e3edc2009-09-15 22:07:15276
[email protected]98d6d4562014-06-25 20:57:55277#endif // CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_