blob: 8bf60f9a5f936fc403fe4c7a928472831e5ad01e [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"
[email protected]7491ad02014-07-05 19:10:0723#include "content/public/common/resource_type.h"
Kinuko Yasudabe4ad292018-07-06 01:33:3924#include "services/network/public/cpp/shared_url_loader_factory.h"
[email protected]97e3edc2009-09-15 22:07:1525
[email protected]edfe7fab2010-11-28 13:11:5226namespace net {
[email protected]9f170462012-08-24 01:06:5827class NetworkDelegate;
[email protected]97e3edc2009-09-15 22:07:1528class URLRequest;
[email protected]edfe7fab2010-11-28 13:11:5229} // namespace net
[email protected]97e3edc2009-09-15 22:07:1530
John Abd-El-Malek46248032018-01-17 19:11:2331namespace network {
32struct ResourceResponseHead;
33}
34
[email protected]073f24d2014-04-09 00:00:2335namespace content {
ananta46f3e942017-05-09 04:56:3036class AppCacheJob;
Anantanarayanan Iyengar2130d272017-09-01 18:21:1437class AppCacheSubresourceURLFactory;
ananta4e8a5292017-05-04 03:07:1538class AppCacheRequest;
[email protected]073f24d2014-04-09 00:00:2339class AppCacheRequestHandlerTest;
ananta2533bff62017-07-12 04:07:2740class AppCacheHost;
[email protected]3367fc1d2009-11-09 00:09:2141
[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
[email protected]ea8e1812012-02-15 22:07:3448 : public base::SupportsUserData::Data,
[email protected]bfe19482011-09-30 22:14:5749 public AppCacheHost::Observer,
michaelnb8618402016-08-02 20:14:3350 public AppCacheServiceImpl::Observer,
anantaa2c8ec62017-06-09 23:44:0551 public AppCacheStorage::Delegate,
Kinuko Yasuda7f3e1722018-03-26 08:58:5852 public NavigationLoaderInterceptor {
[email protected]97e3edc2009-09-15 22:07:1553 public:
Clark DuVallf77e164d2019-10-24 23:53:2554 using AppCacheLoaderCallback =
55 base::OnceCallback<void(SingleRequestURLLoaderFactory::RequestHandler)>;
56
dchengc2282aa2014-10-21 12:07:5857 ~AppCacheRequestHandler() override;
[email protected]7e8e3dd2009-09-18 01:05:0958
Michael Nordman85243a82017-10-24 01:24:5059 // NetworkService loading
Anantanarayanan Iyengard125c4602017-08-19 00:46:5660
Kinuko Yasuda7f3e1722018-03-26 08:58:5861 // NavigationLoaderInterceptor overrides - main resource loading.
arthursonzogni1fd60e62018-05-09 15:57:3262 // These methods are used by the NavigationURLLoaderImpl.
Victor Costanc65793b2019-09-02 23:38:3663 // Internally they use MaybeLoadResource and MaybeLoadFallbackForResponse.
Michael Nordman85243a82017-10-24 01:24:5064 // Eventually one of the Deliver*Response() methods is called and the
65 // LoaderCallback is invoked.
Matt Falkenhagen43765122018-08-22 19:37:2266 void MaybeCreateLoader(
67 const network::ResourceRequest& tentative_resource_request,
Clark DuVall1cc8323a2019-06-28 00:27:0568 BrowserContext* browser_context,
Matt Falkenhagen43765122018-08-22 19:37:2269 LoaderCallback callback,
70 FallbackCallback fallback_callback) override;
Michael Nordman85243a82017-10-24 01:24:5071 // MaybeCreateLoaderForResponse always returns synchronously.
72 bool MaybeCreateLoaderForResponse(
Tsuyoshi Horod0a953d2019-01-23 04:38:1473 const network::ResourceRequest& request,
Minggang Wang47a78222019-06-28 17:05:4674 const network::ResourceResponseHead& response_head,
75 mojo::ScopedDataPipeConsumerHandle* response_body,
John Abd-El-Malekb165dc52018-01-18 17:12:1876 network::mojom::URLLoaderPtr* loader,
Tsuyoshi Horob8d512a2018-01-25 17:01:5977 network::mojom::URLLoaderClientRequest* client_request,
Minggang Wangf8b459d2019-10-25 11:08:3878 blink::ThrottlingURLLoader* url_loader,
Tsuyoshi Horo91727e92019-09-26 05:01:0279 bool* skip_other_interceptors,
80 bool* will_return_unsafe_redirect) override;
Kinuko Yasuda250577c2017-10-29 02:51:2481 base::Optional<SubresourceLoaderParams> MaybeCreateSubresourceLoaderParams()
82 override;
Michael Nordman85243a82017-10-24 01:24:5083
84 // These methods are used for subresource loading by the
85 // AppCacheSubresourceURLFactory::SubresourceLoader class.
Victor Costanc65793b2019-09-02 23:38:3686 // Internally they use MaybeLoadResource, MaybeLoadFallbackForResponse, and
Michael Nordman85243a82017-10-24 01:24:5087 // MaybeLoadFallbackForRedirect. Eventually one of the Deliver*Response()
88 // methods is called and the LoaderCallback is invoked.
John Abd-El-Malek1df61792018-01-12 20:40:4589 void MaybeCreateSubresourceLoader(
90 const network::ResourceRequest& resource_request,
Clark DuVallf77e164d2019-10-24 23:53:2591 AppCacheLoaderCallback callback);
John Abd-El-Malek46248032018-01-17 19:11:2392 void MaybeFallbackForSubresourceResponse(
93 const network::ResourceResponseHead& response,
Clark DuVallf77e164d2019-10-24 23:53:2594 AppCacheLoaderCallback callback);
Michael Nordman85243a82017-10-24 01:24:5095 void MaybeFallbackForSubresourceRedirect(
96 const net::RedirectInfo& redirect_info,
Clark DuVallf77e164d2019-10-24 23:53:2597 AppCacheLoaderCallback callback);
Michael Nordman85243a82017-10-24 01:24:5098 void MaybeFollowSubresourceRedirect(const net::RedirectInfo& redirect_info,
Clark DuVallf77e164d2019-10-24 23:53:2599 AppCacheLoaderCallback callback);
[email protected]15f9cded2010-05-20 20:51:06100
anantaa2c8ec62017-06-09 23:44:05101 static std::unique_ptr<AppCacheRequestHandler>
Hiroki Nakagawaff43e4472018-07-26 07:06:46102 InitializeForMainResourceNetworkService(
John Abd-El-Malek1df61792018-01-12 20:40:45103 const network::ResourceRequest& request,
John Abd-El-Malek0d9f17b2018-12-05 00:02:38104 base::WeakPtr<AppCacheHost> appcache_host);
anantaa2c8ec62017-06-09 23:44:05105
Hiroki Nakagawa599efcb2019-02-27 12:22:33106 static bool IsMainResourceType(ResourceType type);
Michael Nordman85243a82017-10-24 01:24:50107
Anantanarayanan Iyengard125c4602017-08-19 00:46:56108 // Called by unittests to indicate that we are in test mode.
109 static void SetRunningInTests(bool in_tests);
Anantanarayanan Iyengard125c4602017-08-19 00:46:56110 static bool IsRunningInTests();
ananta2533bff62017-07-12 04:07:27111
[email protected]97e3edc2009-09-15 22:07:15112 private:
113 friend class AppCacheHost;
Michael Nordman85243a82017-10-24 01:24:50114 friend class AppCacheRequestHandlerTest;
[email protected]97e3edc2009-09-15 22:07:15115
[email protected]7e8e3dd2009-09-18 01:05:09116 // Callers should use AppCacheHost::CreateRequestHandler.
ananta4e8a5292017-05-04 03:07:15117 AppCacheRequestHandler(AppCacheHost* host,
118 ResourceType resource_type,
119 bool should_reset_appcache,
120 std::unique_ptr<AppCacheRequest> request);
[email protected]97e3edc2009-09-15 22:07:15121
Clark DuVallf77e164d2019-10-24 23:53:25122 void MaybeCreateLoaderInternal(
123 const network::ResourceRequest& resource_request,
124 AppCacheLoaderCallback callback);
125
Victor Costanc65793b2019-09-02 23:38:36126 // AppCacheHost::Observer.
dchengc2282aa2014-10-21 12:07:58127 void OnDestructionImminent(AppCacheHost* host) override;
[email protected]97e3edc2009-09-15 22:07:15128
Victor Costanc65793b2019-09-02 23:38:36129 // AppCacheServiceImpl::Observer.
michaelnb8618402016-08-02 20:14:33130 void OnServiceDestructionImminent(AppCacheServiceImpl* service) override;
131
[email protected]3367fc1d2009-11-09 00:09:21132 // Helpers to instruct a waiting job with what response to
133 // deliver for the request we're handling.
avib7348942015-12-25 20:57:10134 void DeliverAppCachedResponse(const AppCacheEntry& entry,
135 int64_t cache_id,
avib7348942015-12-25 20:57:10136 const GURL& manifest_url,
[email protected]2abcade2012-01-05 00:19:40137 bool is_fallback,
138 const GURL& namespace_entry_url);
[email protected]3367fc1d2009-11-09 00:09:21139 void DeliverNetworkResponse();
140 void DeliverErrorResponse();
141
ananta46f3e942017-05-09 04:56:30142 // Helper method to create an AppCacheJob and populate job_.
mmenke5289cbfd2015-10-29 19:36:17143 // Caller takes ownership of returned value.
ananta46f3e942017-05-09 04:56:30144 std::unique_ptr<AppCacheJob> CreateJob(
mmenkeed0498b2015-12-08 23:20:42145 net::NetworkDelegate* network_delegate);
mmenke5289cbfd2015-10-29 19:36:17146
[email protected]3367fc1d2009-11-09 00:09:21147 // Helper to retrieve a pointer to the storage object.
[email protected]15f9cded2010-05-20 20:51:06148 AppCacheStorage* storage() const;
149
150 bool is_main_resource() const {
151 return IsMainResourceType(resource_type_);
152 }
[email protected]3367fc1d2009-11-09 00:09:21153
Victor Costanc65793b2019-09-02 23:38:36154 // These are called on each request intercept opportunity, by the various
155 // MaybeCreateLoader methods in the public API.
156 AppCacheJob* MaybeLoadResource(net::NetworkDelegate* network_delegate);
157 AppCacheJob* MaybeLoadFallbackForRedirect(
158 net::NetworkDelegate* network_delegate,
159 const GURL& location);
160 AppCacheJob* MaybeLoadFallbackForResponse(
161 net::NetworkDelegate* network_delegate);
162
163 void GetExtraResponseInfo(int64_t* cache_id, GURL* manifest_url);
164
[email protected]3367fc1d2009-11-09 00:09:21165 // Main-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06166 // Frame and SharedWorker main resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21167
ananta46f3e942017-05-09 04:56:30168 std::unique_ptr<AppCacheJob> MaybeLoadMainResource(
mmenke5289cbfd2015-10-29 19:36:17169 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21170
171 // AppCacheStorage::Delegate methods
dchengc2282aa2014-10-21 12:07:58172 void OnMainResponseFound(const GURL& url,
173 const AppCacheEntry& entry,
174 const GURL& fallback_url,
175 const AppCacheEntry& fallback_entry,
avib7348942015-12-25 20:57:10176 int64_t cache_id,
177 int64_t group_id,
dchengc2282aa2014-10-21 12:07:58178 const GURL& mainfest_url) override;
[email protected]3367fc1d2009-11-09 00:09:21179
Kinuko Yasuda8757204a2017-11-04 07:50:13180 // NetworkService loading:
Ken Rockot387ddd5a2018-01-30 19:18:40181 // Called when a |callback| that is originally given to |MaybeCreateLoader()|
182 // runs for the main resource. This flips |should_create_subresource_loader_|
183 // if a non-null |handler| is given. Always invokes |callback| with |handler|.
Kinuko Yasuda8757204a2017-11-04 07:50:13184 void RunLoaderCallbackForMainResource(
Clark DuVallf77e164d2019-10-24 23:53:25185 int frame_tree_node_id,
186 BrowserContext* browser_context,
Kinuko Yasuda8757204a2017-11-04 07:50:13187 LoaderCallback callback,
Ken Rockot387ddd5a2018-01-30 19:18:40188 SingleRequestURLLoaderFactory::RequestHandler handler);
Kinuko Yasuda8757204a2017-11-04 07:50:13189
[email protected]3367fc1d2009-11-09 00:09:21190 // Sub-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06191 // Dedicated worker and all manner of sub-resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21192
ananta46f3e942017-05-09 04:56:30193 std::unique_ptr<AppCacheJob> MaybeLoadSubResource(
mmenke5289cbfd2015-10-29 19:36:17194 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21195 void ContinueMaybeLoadSubResource();
196
197 // AppCacheHost::Observer override
dchengc2282aa2014-10-21 12:07:58198 void OnCacheSelectionComplete(AppCacheHost* host) override;
[email protected]3367fc1d2009-11-09 00:09:21199
200 // Data members -----------------------------------------------
201
202 // What host we're servicing a request for.
[email protected]7e8e3dd2009-09-18 01:05:09203 AppCacheHost* host_;
[email protected]3367fc1d2009-11-09 00:09:21204
[email protected]15f9cded2010-05-20 20:51:06205 // Frame vs subresource vs sharedworker loads are somewhat different.
[email protected]6c1e05212014-07-31 00:59:40206 ResourceType resource_type_;
[email protected]3367fc1d2009-11-09 00:09:21207
eustas27876842014-11-27 10:48:00208 // True if corresponding AppCache group should be resetted before load.
209 bool should_reset_appcache_;
210
[email protected]3367fc1d2009-11-09 00:09:21211 // Subresource requests wait until after cache selection completes.
212 bool is_waiting_for_cache_selection_;
213
214 // Info about the type of response we found for delivery.
215 // These are relevant for both main and subresource requests.
avib7348942015-12-25 20:57:10216 int64_t found_group_id_;
217 int64_t found_cache_id_;
[email protected]3367fc1d2009-11-09 00:09:21218 AppCacheEntry found_entry_;
219 AppCacheEntry found_fallback_entry_;
[email protected]2abcade2012-01-05 00:19:40220 GURL found_namespace_entry_url_;
[email protected]3367fc1d2009-11-09 00:09:21221 GURL found_manifest_url_;
222 bool found_network_namespace_;
223
[email protected]1e1f7d52010-08-25 21:38:20224 // True if a cache entry this handler attempted to return was
225 // not found in the disk cache. Once set, the handler will take
226 // no action on all subsequent intercept opportunities, so the
227 // request and any redirects will be handled by the network library.
228 bool cache_entry_not_found_;
229
mmenke5289cbfd2015-10-29 19:36:17230 // True if the next time this request is started, the response should be
231 // delivered from the network, bypassing the AppCache. Cleared after the next
232 // intercept opportunity.
233 bool is_delivering_network_response_;
234
[email protected]17d647852012-09-08 17:45:52235 // True if this->MaybeLoadResource(...) has been called in the past.
236 bool maybe_load_resource_executed_;
237
mmenke5289cbfd2015-10-29 19:36:17238 // The job we use to deliver a response. Only NULL during the following times:
239 // 1) Before request has started a job.
240 // 2) Request is not being handled by appcache.
241 // 3) Request has been cancelled, and the job killed.
ananta46f3e942017-05-09 04:56:30242 base::WeakPtr<AppCacheJob> job_;
[email protected]3367fc1d2009-11-09 00:09:21243
mmenke5289cbfd2015-10-29 19:36:17244 // Cached information about the response being currently served by the
245 // AppCache, if there is one.
246 int cache_id_;
247 GURL manifest_url_;
248
michaelnb8618402016-08-02 20:14:33249 // Backptr to the central service object.
250 AppCacheServiceImpl* service_;
251
ananta4e8a5292017-05-04 03:07:15252 std::unique_ptr<AppCacheRequest> request_;
253
ananta2533bff62017-07-12 04:07:27254 // Network service related members.
255
Clark DuVallf77e164d2019-10-24 23:53:25256 AppCacheLoaderCallback loader_callback_;
anantaa2c8ec62017-06-09 23:44:05257
Kinuko Yasuda8757204a2017-11-04 07:50:13258 // Flipped to true if AppCache wants to handle subresource requests
Ken Rockot387ddd5a2018-01-30 19:18:40259 // (i.e. when |loader_callback_| is fired with a non-null
260 // RequestHandler for non-error cases.
Kinuko Yasuda8757204a2017-11-04 07:50:13261 bool should_create_subresource_loader_ = false;
262
ananta2533bff62017-07-12 04:07:27263 // The AppCache host instance. We pass this to the
264 // AppCacheSubresourceURLFactory instance on creation.
265 base::WeakPtr<AppCacheHost> appcache_host_;
266
Jeremy Roman3bca4bf2019-07-11 03:41:25267 base::WeakPtrFactory<AppCacheRequestHandler> weak_factory_{this};
Kinuko Yasuda8757204a2017-11-04 07:50:13268
[email protected]3367fc1d2009-11-09 00:09:21269 DISALLOW_COPY_AND_ASSIGN(AppCacheRequestHandler);
[email protected]97e3edc2009-09-15 22:07:15270};
271
[email protected]98d6d4562014-06-25 20:57:55272} // namespace content
[email protected]97e3edc2009-09-15 22:07:15273
[email protected]98d6d4562014-06-25 20:57:55274#endif // CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_