blob: 57362f43c43401c22a99359f867f93d71e69cc5f [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"
[email protected]98d6d4562014-06-25 20:57:5521#include "content/common/content_export.h"
[email protected]7491ad02014-07-05 19:10:0722#include "content/public/common/resource_type.h"
Kinuko Yasudabe4ad292018-07-06 01:33:3923#include "services/network/public/cpp/shared_url_loader_factory.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;
Anantanarayanan Iyengar2130d272017-09-01 18:21:1436class AppCacheSubresourceURLFactory;
ananta4e8a5292017-05-04 03:07:1537class AppCacheRequest;
[email protected]073f24d2014-04-09 00:00:2338class AppCacheRequestHandlerTest;
[email protected]3367fc1d2009-11-09 00:09:2139class AppCacheURLRequestJob;
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.
[email protected]98d6d4562014-06-25 20:57:5547class 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:
dchengc2282aa2014-10-21 12:07:5854 ~AppCacheRequestHandler() override;
[email protected]7e8e3dd2009-09-18 01:05:0955
[email protected]3367fc1d2009-11-09 00:09:2156 // These are called on each request intercept opportunity.
Michael Nordman85243a82017-10-24 01:24:5057 // When the NetworkService is not enabled, the AppCacheInterceptor calls these
58 // methods directly. When the NetworkService is enabled, the various
59 // MaybeCreateLoader methods below call call these internally.
ananta46f3e942017-05-09 04:56:3060 AppCacheJob* MaybeLoadResource(net::NetworkDelegate* network_delegate);
61 AppCacheJob* MaybeLoadFallbackForRedirect(
[email protected]9f170462012-08-24 01:06:5862 net::NetworkDelegate* network_delegate,
63 const GURL& location);
ananta46f3e942017-05-09 04:56:3064 AppCacheJob* MaybeLoadFallbackForResponse(
mmenke5289cbfd2015-10-29 19:36:1765 net::NetworkDelegate* network_delegate);
[email protected]97e3edc2009-09-15 22:07:1566
avib7348942015-12-25 20:57:1067 void GetExtraResponseInfo(int64_t* cache_id, GURL* manifest_url);
[email protected]97e3edc2009-09-15 22:07:1568
Michael Nordman85243a82017-10-24 01:24:5069 // NetworkService loading
Anantanarayanan Iyengard125c4602017-08-19 00:46:5670
Kinuko Yasuda7f3e1722018-03-26 08:58:5871 // NavigationLoaderInterceptor overrides - main resource loading.
arthursonzogni1fd60e62018-05-09 15:57:3272 // These methods are used by the NavigationURLLoaderImpl.
Michael Nordman85243a82017-10-24 01:24:5073 // Internally they use same methods used by the network library based impl,
74 // MaybeLoadResource and MaybeLoadFallbackForResponse.
75 // Eventually one of the Deliver*Response() methods is called and the
76 // LoaderCallback is invoked.
Matt Falkenhagen43765122018-08-22 19:37:2277 void MaybeCreateLoader(
78 const network::ResourceRequest& tentative_resource_request,
79 ResourceContext* resource_context,
80 LoaderCallback callback,
81 FallbackCallback fallback_callback) override;
Michael Nordman85243a82017-10-24 01:24:5082 // MaybeCreateLoaderForResponse always returns synchronously.
83 bool MaybeCreateLoaderForResponse(
John Abd-El-Malek46248032018-01-17 19:11:2384 const network::ResourceResponseHead& response,
John Abd-El-Malekb165dc52018-01-18 17:12:1885 network::mojom::URLLoaderPtr* loader,
Tsuyoshi Horob8d512a2018-01-25 17:01:5986 network::mojom::URLLoaderClientRequest* client_request,
87 ThrottlingURLLoader* url_loader) override;
Kinuko Yasuda250577c2017-10-29 02:51:2488 base::Optional<SubresourceLoaderParams> MaybeCreateSubresourceLoaderParams()
89 override;
Michael Nordman85243a82017-10-24 01:24:5090
91 // These methods are used for subresource loading by the
92 // AppCacheSubresourceURLFactory::SubresourceLoader class.
93 // Internally they use same methods used by the network library based impl,
94 // MaybeLoadResource, MaybeLoadFallbackForResponse, and
95 // MaybeLoadFallbackForRedirect. Eventually one of the Deliver*Response()
96 // methods is called and the LoaderCallback is invoked.
John Abd-El-Malek1df61792018-01-12 20:40:4597 void MaybeCreateSubresourceLoader(
98 const network::ResourceRequest& resource_request,
99 LoaderCallback callback);
John Abd-El-Malek46248032018-01-17 19:11:23100 void MaybeFallbackForSubresourceResponse(
101 const network::ResourceResponseHead& response,
102 LoaderCallback callback);
Michael Nordman85243a82017-10-24 01:24:50103 void MaybeFallbackForSubresourceRedirect(
104 const net::RedirectInfo& redirect_info,
105 LoaderCallback callback);
106 void MaybeFollowSubresourceRedirect(const net::RedirectInfo& redirect_info,
107 LoaderCallback callback);
[email protected]15f9cded2010-05-20 20:51:06108
anantaa2c8ec62017-06-09 23:44:05109 static std::unique_ptr<AppCacheRequestHandler>
Hiroki Nakagawaff43e4472018-07-26 07:06:46110 InitializeForMainResourceNetworkService(
John Abd-El-Malek1df61792018-01-12 20:40:45111 const network::ResourceRequest& request,
Hiroki Nakagawaff43e4472018-07-26 07:06:46112 base::WeakPtr<AppCacheHost> appcache_host,
Kinuko Yasudabe4ad292018-07-06 01:33:39113 scoped_refptr<network::SharedURLLoaderFactory> network_loader_factory);
anantaa2c8ec62017-06-09 23:44:05114
Michael Nordman85243a82017-10-24 01:24:50115 static bool IsMainResourceType(ResourceType type) {
116 return IsResourceTypeFrame(type) || type == RESOURCE_TYPE_SHARED_WORKER;
117 }
118
Anantanarayanan Iyengard125c4602017-08-19 00:46:56119 // Called by unittests to indicate that we are in test mode.
120 static void SetRunningInTests(bool in_tests);
Anantanarayanan Iyengard125c4602017-08-19 00:46:56121 static bool IsRunningInTests();
ananta2533bff62017-07-12 04:07:27122
[email protected]97e3edc2009-09-15 22:07:15123 private:
124 friend class AppCacheHost;
Michael Nordman85243a82017-10-24 01:24:50125 friend class AppCacheRequestHandlerTest;
[email protected]97e3edc2009-09-15 22:07:15126
[email protected]7e8e3dd2009-09-18 01:05:09127 // Callers should use AppCacheHost::CreateRequestHandler.
ananta4e8a5292017-05-04 03:07:15128 AppCacheRequestHandler(AppCacheHost* host,
129 ResourceType resource_type,
130 bool should_reset_appcache,
131 std::unique_ptr<AppCacheRequest> request);
[email protected]97e3edc2009-09-15 22:07:15132
[email protected]3367fc1d2009-11-09 00:09:21133 // AppCacheHost::Observer override
dchengc2282aa2014-10-21 12:07:58134 void OnDestructionImminent(AppCacheHost* host) override;
[email protected]97e3edc2009-09-15 22:07:15135
michaelnb8618402016-08-02 20:14:33136 // AppCacheServiceImpl::Observer override
137 void OnServiceDestructionImminent(AppCacheServiceImpl* service) override;
138
[email protected]3367fc1d2009-11-09 00:09:21139 // Helpers to instruct a waiting job with what response to
140 // deliver for the request we're handling.
avib7348942015-12-25 20:57:10141 void DeliverAppCachedResponse(const AppCacheEntry& entry,
142 int64_t cache_id,
avib7348942015-12-25 20:57:10143 const GURL& manifest_url,
[email protected]2abcade2012-01-05 00:19:40144 bool is_fallback,
145 const GURL& namespace_entry_url);
[email protected]3367fc1d2009-11-09 00:09:21146 void DeliverNetworkResponse();
147 void DeliverErrorResponse();
148
mmenke5289cbfd2015-10-29 19:36:17149 // Called just before the request is restarted. Grabs the reason for
150 // restarting, so can correctly continue to handle the request.
Michael Nordman85243a82017-10-24 01:24:50151 void OnPrepareToRestartURLRequest();
mmenke5289cbfd2015-10-29 19:36:17152
ananta46f3e942017-05-09 04:56:30153 // Helper method to create an AppCacheJob and populate job_.
mmenke5289cbfd2015-10-29 19:36:17154 // Caller takes ownership of returned value.
ananta46f3e942017-05-09 04:56:30155 std::unique_ptr<AppCacheJob> CreateJob(
mmenkeed0498b2015-12-08 23:20:42156 net::NetworkDelegate* network_delegate);
mmenke5289cbfd2015-10-29 19:36:17157
[email protected]3367fc1d2009-11-09 00:09:21158 // Helper to retrieve a pointer to the storage object.
[email protected]15f9cded2010-05-20 20:51:06159 AppCacheStorage* storage() const;
160
161 bool is_main_resource() const {
162 return IsMainResourceType(resource_type_);
163 }
[email protected]3367fc1d2009-11-09 00:09:21164
165 // 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(
185 LoaderCallback callback,
Ken Rockot387ddd5a2018-01-30 19:18:40186 SingleRequestURLLoaderFactory::RequestHandler handler);
Kinuko Yasuda8757204a2017-11-04 07:50:13187
[email protected]3367fc1d2009-11-09 00:09:21188 // Sub-resource loading -------------------------------------
[email protected]15f9cded2010-05-20 20:51:06189 // Dedicated worker and all manner of sub-resources are handled here.
[email protected]3367fc1d2009-11-09 00:09:21190
ananta46f3e942017-05-09 04:56:30191 std::unique_ptr<AppCacheJob> MaybeLoadSubResource(
mmenke5289cbfd2015-10-29 19:36:17192 net::NetworkDelegate* network_delegate);
[email protected]3367fc1d2009-11-09 00:09:21193 void ContinueMaybeLoadSubResource();
194
195 // AppCacheHost::Observer override
dchengc2282aa2014-10-21 12:07:58196 void OnCacheSelectionComplete(AppCacheHost* host) override;
[email protected]3367fc1d2009-11-09 00:09:21197
198 // Data members -----------------------------------------------
199
200 // What host we're servicing a request for.
[email protected]7e8e3dd2009-09-18 01:05:09201 AppCacheHost* host_;
[email protected]3367fc1d2009-11-09 00:09:21202
[email protected]15f9cded2010-05-20 20:51:06203 // Frame vs subresource vs sharedworker loads are somewhat different.
[email protected]6c1e05212014-07-31 00:59:40204 ResourceType resource_type_;
[email protected]3367fc1d2009-11-09 00:09:21205
eustas27876842014-11-27 10:48:00206 // True if corresponding AppCache group should be resetted before load.
207 bool should_reset_appcache_;
208
[email protected]3367fc1d2009-11-09 00:09:21209 // Subresource requests wait until after cache selection completes.
210 bool is_waiting_for_cache_selection_;
211
212 // Info about the type of response we found for delivery.
213 // These are relevant for both main and subresource requests.
avib7348942015-12-25 20:57:10214 int64_t found_group_id_;
215 int64_t found_cache_id_;
[email protected]3367fc1d2009-11-09 00:09:21216 AppCacheEntry found_entry_;
217 AppCacheEntry found_fallback_entry_;
[email protected]2abcade2012-01-05 00:19:40218 GURL found_namespace_entry_url_;
[email protected]3367fc1d2009-11-09 00:09:21219 GURL found_manifest_url_;
220 bool found_network_namespace_;
221
[email protected]1e1f7d52010-08-25 21:38:20222 // True if a cache entry this handler attempted to return was
223 // not found in the disk cache. Once set, the handler will take
224 // no action on all subsequent intercept opportunities, so the
225 // request and any redirects will be handled by the network library.
226 bool cache_entry_not_found_;
227
mmenke5289cbfd2015-10-29 19:36:17228 // True if the next time this request is started, the response should be
229 // delivered from the network, bypassing the AppCache. Cleared after the next
230 // intercept opportunity.
231 bool is_delivering_network_response_;
232
[email protected]17d647852012-09-08 17:45:52233 // True if this->MaybeLoadResource(...) has been called in the past.
234 bool maybe_load_resource_executed_;
235
mmenke5289cbfd2015-10-29 19:36:17236 // The job we use to deliver a response. Only NULL during the following times:
237 // 1) Before request has started a job.
238 // 2) Request is not being handled by appcache.
239 // 3) Request has been cancelled, and the job killed.
ananta46f3e942017-05-09 04:56:30240 base::WeakPtr<AppCacheJob> job_;
[email protected]3367fc1d2009-11-09 00:09:21241
mmenke5289cbfd2015-10-29 19:36:17242 // Cached information about the response being currently served by the
243 // AppCache, if there is one.
244 int cache_id_;
245 GURL manifest_url_;
246
michaelnb8618402016-08-02 20:14:33247 // Backptr to the central service object.
248 AppCacheServiceImpl* service_;
249
ananta4e8a5292017-05-04 03:07:15250 std::unique_ptr<AppCacheRequest> request_;
251
ananta2533bff62017-07-12 04:07:27252 // Network service related members.
253
Michael Nordman85243a82017-10-24 01:24:50254 LoaderCallback loader_callback_;
anantaa2c8ec62017-06-09 23:44:05255
Kinuko Yasuda8757204a2017-11-04 07:50:13256 // Flipped to true if AppCache wants to handle subresource requests
Ken Rockot387ddd5a2018-01-30 19:18:40257 // (i.e. when |loader_callback_| is fired with a non-null
258 // RequestHandler for non-error cases.
Kinuko Yasuda8757204a2017-11-04 07:50:13259 bool should_create_subresource_loader_ = false;
260
Kinuko Yasudabe4ad292018-07-06 01:33:39261 // The network URL loader factory.
262 scoped_refptr<network::SharedURLLoaderFactory> network_loader_factory_;
anantae60d1d42017-06-20 04:16:27263
ananta2533bff62017-07-12 04:07:27264 // The AppCache host instance. We pass this to the
265 // AppCacheSubresourceURLFactory instance on creation.
266 base::WeakPtr<AppCacheHost> appcache_host_;
267
Kinuko Yasuda8757204a2017-11-04 07:50:13268 base::WeakPtrFactory<AppCacheRequestHandler> weak_factory_;
269
[email protected]3367fc1d2009-11-09 00:09:21270 DISALLOW_COPY_AND_ASSIGN(AppCacheRequestHandler);
[email protected]97e3edc2009-09-15 22:07:15271};
272
[email protected]98d6d4562014-06-25 20:57:55273} // namespace content
[email protected]97e3edc2009-09-15 22:07:15274
[email protected]98d6d4562014-06-25 20:57:55275#endif // CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_