blob: 0ab99d0d3c0da8dd5463d941cf297dc11cf6e948 [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"
Kinuko Yasuda7f3e1722018-03-26 08:58:5820#include "content/browser/loader/navigation_loader_interceptor.h"
Clark DuVallf77e164d2019-10-24 23:53:2521#include "content/browser/loader/single_request_url_loader_factory.h"
[email protected]98d6d4562014-06-25 20:57:5522#include "content/common/content_export.h"
Julie Jeongeun Kim633d2e72019-10-29 09:03:0323#include "mojo/public/cpp/bindings/pending_receiver.h"
Julie Jeongeun Kim917c197902019-11-26 03:36:1424#include "mojo/public/cpp/bindings/pending_remote.h"
Kinuko Yasudabe4ad292018-07-06 01:33:3925#include "services/network/public/cpp/shared_url_loader_factory.h"
Lucas Furukawa Gadani68c380b2019-12-04 19:38:3426#include "services/network/public/mojom/url_response_head.mojom-forward.h"
Minggang Wangee5af392020-02-05 02:55:2827#include "third_party/blink/public/mojom/loader/resource_load_info.mojom-shared.h"
[email protected]97e3edc2009-09-15 22:07:1528
[email protected]edfe7fab2010-11-28 13:11:5229namespace net {
[email protected]9f170462012-08-24 01:06:5830class NetworkDelegate;
[email protected]97e3edc2009-09-15 22:07:1531class URLRequest;
[email protected]edfe7fab2010-11-28 13:11:5232} // namespace net
[email protected]97e3edc2009-09-15 22:07:1533
[email protected]073f24d2014-04-09 00:00:2334namespace content {
ananta46f3e942017-05-09 04:56:3035class AppCacheJob;
Anantanarayanan Iyengar2130d272017-09-01 18:21:1436class AppCacheSubresourceURLFactory;
ananta4e8a5292017-05-04 03:07:1537class AppCacheRequest;
[email protected]073f24d2014-04-09 00:00:2338class AppCacheRequestHandlerTest;
ananta2533bff62017-07-12 04:07:2739class AppCacheHost;
[email protected]3367fc1d2009-11-09 00:09:2140
[email protected]6981d9632010-11-30 21:34:0241// An instance is created for each net::URLRequest. The instance survives all
42// http transactions involved in the processing of its net::URLRequest, and is
[email protected]97e3edc2009-09-15 22:07:1543// given the opportunity to hijack the request along the way. Callers
44// should use AppCacheHost::CreateRequestHandler to manufacture instances
45// that can retrieve resources for a particular host.
Finditb796d002018-10-21 07:15:2246class CONTENT_EXPORT AppCacheRequestHandler
[email protected]ea8e1812012-02-15 22:07:3447 : public base::SupportsUserData::Data,
[email protected]bfe19482011-09-30 22:14:5748 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;
Kinuko Yasuda250577c2017-10-29 02:51:2480 base::Optional<SubresourceLoaderParams> MaybeCreateSubresourceLoaderParams()
81 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,
John Abd-El-Malek0d9f17b2018-12-05 00:02:38103 base::WeakPtr<AppCacheHost> appcache_host);
anantaa2c8ec62017-06-09 23:44:05104
Minggang Wangee5af392020-02-05 02:55:28105 static bool IsMainResourceType(blink::mojom::ResourceType type);
Michael Nordman85243a82017-10-24 01:24:50106
Anantanarayanan Iyengard125c4602017-08-19 00:46:56107 // Called by unittests to indicate that we are in test mode.
108 static void SetRunningInTests(bool in_tests);
Anantanarayanan Iyengard125c4602017-08-19 00:46:56109 static bool IsRunningInTests();
ananta2533bff62017-07-12 04:07:27110
[email protected]97e3edc2009-09-15 22:07:15111 private:
112 friend class AppCacheHost;
Michael Nordman85243a82017-10-24 01:24:50113 friend class AppCacheRequestHandlerTest;
[email protected]97e3edc2009-09-15 22:07:15114
[email protected]7e8e3dd2009-09-18 01:05:09115 // Callers should use AppCacheHost::CreateRequestHandler.
ananta4e8a5292017-05-04 03:07:15116 AppCacheRequestHandler(AppCacheHost* host,
Minggang Wangee5af392020-02-05 02:55:28117 blink::mojom::ResourceType resource_type,
ananta4e8a5292017-05-04 03:07:15118 bool should_reset_appcache,
119 std::unique_ptr<AppCacheRequest> request);
[email protected]97e3edc2009-09-15 22:07:15120
Clark DuVallf77e164d2019-10-24 23:53:25121 void MaybeCreateLoaderInternal(
122 const network::ResourceRequest& resource_request,
123 AppCacheLoaderCallback callback);
124
Victor Costanc65793b2019-09-02 23:38:36125 // AppCacheHost::Observer.
dchengc2282aa2014-10-21 12:07:58126 void OnDestructionImminent(AppCacheHost* host) override;
[email protected]97e3edc2009-09-15 22:07:15127
Victor Costanc65793b2019-09-02 23:38:36128 // AppCacheServiceImpl::Observer.
michaelnb8618402016-08-02 20:14:33129 void OnServiceDestructionImminent(AppCacheServiceImpl* service) override;
130
[email protected]3367fc1d2009-11-09 00:09:21131 // Helpers to instruct a waiting job with what response to
132 // deliver for the request we're handling.
avib7348942015-12-25 20:57:10133 void DeliverAppCachedResponse(const AppCacheEntry& entry,
134 int64_t cache_id,
avib7348942015-12-25 20:57:10135 const GURL& manifest_url,
[email protected]2abcade2012-01-05 00:19:40136 bool is_fallback,
137 const GURL& namespace_entry_url);
[email protected]3367fc1d2009-11-09 00:09:21138 void DeliverNetworkResponse();
139 void DeliverErrorResponse();
140
ananta46f3e942017-05-09 04:56:30141 // Helper method to create an AppCacheJob and populate job_.
mmenke5289cbfd2015-10-29 19:36:17142 // Caller takes ownership of returned value.
ananta46f3e942017-05-09 04:56:30143 std::unique_ptr<AppCacheJob> CreateJob(
mmenkeed0498b2015-12-08 23:20:42144 net::NetworkDelegate* network_delegate);
mmenke5289cbfd2015-10-29 19:36:17145
[email protected]3367fc1d2009-11-09 00:09:21146 // Helper to retrieve a pointer to the storage object.
[email protected]15f9cded2010-05-20 20:51:06147 AppCacheStorage* storage() const;
148
149 bool is_main_resource() const {
150 return IsMainResourceType(resource_type_);
151 }
[email protected]3367fc1d2009-11-09 00:09:21152
Victor Costanc65793b2019-09-02 23:38:36153 // These are called on each request intercept opportunity, by the various
154 // MaybeCreateLoader methods in the public API.
155 AppCacheJob* MaybeLoadResource(net::NetworkDelegate* network_delegate);
156 AppCacheJob* MaybeLoadFallbackForRedirect(
157 net::NetworkDelegate* network_delegate,
158 const GURL& location);
159 AppCacheJob* MaybeLoadFallbackForResponse(
160 net::NetworkDelegate* network_delegate);
161
162 void GetExtraResponseInfo(int64_t* cache_id, GURL* manifest_url);
163
[email protected]3367fc1d2009-11-09 00:09:21164 // Main-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06165 // Frame and SharedWorker main resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21166
ananta46f3e942017-05-09 04:56:30167 std::unique_ptr<AppCacheJob> MaybeLoadMainResource(
mmenke5289cbfd2015-10-29 19:36:17168 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21169
170 // AppCacheStorage::Delegate methods
dchengc2282aa2014-10-21 12:07:58171 void OnMainResponseFound(const GURL& url,
172 const AppCacheEntry& entry,
173 const GURL& fallback_url,
174 const AppCacheEntry& fallback_entry,
avib7348942015-12-25 20:57:10175 int64_t cache_id,
176 int64_t group_id,
dchengc2282aa2014-10-21 12:07:58177 const GURL& mainfest_url) override;
[email protected]3367fc1d2009-11-09 00:09:21178
Kinuko Yasuda8757204a2017-11-04 07:50:13179 // NetworkService loading:
Ken Rockot387ddd5a2018-01-30 19:18:40180 // Called when a |callback| that is originally given to |MaybeCreateLoader()|
181 // runs for the main resource. This flips |should_create_subresource_loader_|
182 // if a non-null |handler| is given. Always invokes |callback| with |handler|.
Kinuko Yasuda8757204a2017-11-04 07:50:13183 void RunLoaderCallbackForMainResource(
Clark DuVallf77e164d2019-10-24 23:53:25184 int frame_tree_node_id,
185 BrowserContext* browser_context,
Kinuko Yasuda8757204a2017-11-04 07:50:13186 LoaderCallback callback,
Ken Rockot387ddd5a2018-01-30 19:18:40187 SingleRequestURLLoaderFactory::RequestHandler handler);
Kinuko Yasuda8757204a2017-11-04 07:50:13188
[email protected]3367fc1d2009-11-09 00:09:21189 // Sub-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06190 // Dedicated worker and all manner of sub-resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21191
ananta46f3e942017-05-09 04:56:30192 std::unique_ptr<AppCacheJob> MaybeLoadSubResource(
mmenke5289cbfd2015-10-29 19:36:17193 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21194 void ContinueMaybeLoadSubResource();
195
196 // AppCacheHost::Observer override
dchengc2282aa2014-10-21 12:07:58197 void OnCacheSelectionComplete(AppCacheHost* host) override;
[email protected]3367fc1d2009-11-09 00:09:21198
199 // Data members -----------------------------------------------
200
201 // What host we're servicing a request for.
[email protected]7e8e3dd2009-09-18 01:05:09202 AppCacheHost* host_;
[email protected]3367fc1d2009-11-09 00:09:21203
[email protected]15f9cded2010-05-20 20:51:06204 // Frame vs subresource vs sharedworker loads are somewhat different.
Minggang Wangee5af392020-02-05 02:55:28205 blink::mojom::ResourceType resource_type_;
[email protected]3367fc1d2009-11-09 00:09:21206
eustas27876842014-11-27 10:48:00207 // True if corresponding AppCache group should be resetted before load.
208 bool should_reset_appcache_;
209
[email protected]3367fc1d2009-11-09 00:09:21210 // Subresource requests wait until after cache selection completes.
211 bool is_waiting_for_cache_selection_;
212
213 // Info about the type of response we found for delivery.
214 // These are relevant for both main and subresource requests.
avib7348942015-12-25 20:57:10215 int64_t found_group_id_;
216 int64_t found_cache_id_;
[email protected]3367fc1d2009-11-09 00:09:21217 AppCacheEntry found_entry_;
218 AppCacheEntry found_fallback_entry_;
[email protected]2abcade2012-01-05 00:19:40219 GURL found_namespace_entry_url_;
[email protected]3367fc1d2009-11-09 00:09:21220 GURL found_manifest_url_;
221 bool found_network_namespace_;
222
[email protected]1e1f7d52010-08-25 21:38:20223 // True if a cache entry this handler attempted to return was
224 // not found in the disk cache. Once set, the handler will take
225 // no action on all subsequent intercept opportunities, so the
226 // request and any redirects will be handled by the network library.
227 bool cache_entry_not_found_;
228
mmenke5289cbfd2015-10-29 19:36:17229 // True if the next time this request is started, the response should be
230 // delivered from the network, bypassing the AppCache. Cleared after the next
231 // intercept opportunity.
232 bool is_delivering_network_response_;
233
[email protected]17d647852012-09-08 17:45:52234 // True if this->MaybeLoadResource(...) has been called in the past.
235 bool maybe_load_resource_executed_;
236
mmenke5289cbfd2015-10-29 19:36:17237 // The job we use to deliver a response. Only NULL during the following times:
238 // 1) Before request has started a job.
239 // 2) Request is not being handled by appcache.
240 // 3) Request has been cancelled, and the job killed.
ananta46f3e942017-05-09 04:56:30241 base::WeakPtr<AppCacheJob> job_;
[email protected]3367fc1d2009-11-09 00:09:21242
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
Clark DuVallf77e164d2019-10-24 23:53:25255 AppCacheLoaderCallback loader_callback_;
anantaa2c8ec62017-06-09 23:44:05256
Kinuko Yasuda8757204a2017-11-04 07:50:13257 // Flipped to true if AppCache wants to handle subresource requests
Ken Rockot387ddd5a2018-01-30 19:18:40258 // (i.e. when |loader_callback_| is fired with a non-null
259 // RequestHandler for non-error cases.
Kinuko Yasuda8757204a2017-11-04 07:50:13260 bool should_create_subresource_loader_ = false;
261
ananta2533bff62017-07-12 04:07:27262 // The AppCache host instance. We pass this to the
263 // AppCacheSubresourceURLFactory instance on creation.
264 base::WeakPtr<AppCacheHost> appcache_host_;
265
Jeremy Roman3bca4bf2019-07-11 03:41:25266 base::WeakPtrFactory<AppCacheRequestHandler> weak_factory_{this};
Kinuko Yasuda8757204a2017-11-04 07:50:13267
[email protected]3367fc1d2009-11-09 00:09:21268 DISALLOW_COPY_AND_ASSIGN(AppCacheRequestHandler);
[email protected]97e3edc2009-09-15 22:07:15269};
270
[email protected]98d6d4562014-06-25 20:57:55271} // namespace content
[email protected]97e3edc2009-09-15 22:07:15272
[email protected]98d6d4562014-06-25 20:57:55273#endif // CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_