summaryrefslogtreecommitdiff
path: root/chromium/content/browser/appcache/appcache_request_handler.h
blob: 96ecfa503c9c2cd2caf41322c76e5e000ced4a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_
#define CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_

#include <stdint.h>

#include <memory>

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/supports_user_data.h"
#include "content/browser/appcache/appcache_entry.h"
#include "content/browser/appcache/appcache_host.h"
#include "content/browser/appcache/appcache_request_handler.h"
#include "content/browser/appcache/appcache_service_impl.h"
#include "content/browser/loader/url_loader_request_handler.h"
#include "content/browser/url_loader_factory_getter.h"
#include "content/common/content_export.h"
#include "content/public/common/resource_type.h"

namespace net {
class NetworkDelegate;
class URLRequest;
}  // namespace net

namespace network {
struct ResourceResponseHead;
}

namespace content {
class AppCacheJob;
class AppCacheNavigationHandleCore;
class AppCacheSubresourceURLFactory;
class AppCacheRequest;
class AppCacheRequestHandlerTest;
class AppCacheURLRequestJob;
class AppCacheHost;

// An instance is created for each net::URLRequest. The instance survives all
// http transactions involved in the processing of its net::URLRequest, and is
// given the opportunity to hijack the request along the way. Callers
// should use AppCacheHost::CreateRequestHandler to manufacture instances
// that can retrieve resources for a particular host.
class CONTENT_EXPORT AppCacheRequestHandler
    : public base::SupportsUserData::Data,
      public AppCacheHost::Observer,
      public AppCacheServiceImpl::Observer,
      public AppCacheStorage::Delegate,
      public URLLoaderRequestHandler {
 public:
  ~AppCacheRequestHandler() override;

  // These are called on each request intercept opportunity.
  // When the NetworkService is not enabled, the AppCacheInterceptor calls these
  // methods directly. When the NetworkService is enabled, the various
  // MaybeCreateLoader methods below call call these internally.
  AppCacheJob* MaybeLoadResource(net::NetworkDelegate* network_delegate);
  AppCacheJob* MaybeLoadFallbackForRedirect(
      net::NetworkDelegate* network_delegate,
      const GURL& location);
  AppCacheJob* MaybeLoadFallbackForResponse(
      net::NetworkDelegate* network_delegate);

  void GetExtraResponseInfo(int64_t* cache_id, GURL* manifest_url);

  // Methods to support cross site navigations.
  void PrepareForCrossSiteTransfer(int old_process_id);
  void CompleteCrossSiteTransfer(int new_process_id, int new_host_id);
  void MaybeCompleteCrossSiteTransferInOldProcess(int old_process_id);

  // Useful for detecting storage partition mismatches in the context
  // of cross site transfer navigations.
  bool SanityCheckIsSameService(AppCacheService* service) {
    return !host_ || (host_->service() == service);
  }

  // NetworkService loading

  // URLLoaderRequestHandler overrides - main resource loading.
  // These methods are used by the NavigationURLLoaderNetworkService.
  // Internally they use same methods used by the network library based impl,
  // MaybeLoadResource and MaybeLoadFallbackForResponse.
  // Eventually one of the Deliver*Response() methods is called and the
  // LoaderCallback is invoked.
  void MaybeCreateLoader(const network::ResourceRequest& resource_request,
                         ResourceContext* resource_context,
                         LoaderCallback callback) override;
  // MaybeCreateLoaderForResponse always returns synchronously.
  bool MaybeCreateLoaderForResponse(
      const network::ResourceResponseHead& response,
      network::mojom::URLLoaderPtr* loader,
      network::mojom::URLLoaderClientRequest* client_request,
      ThrottlingURLLoader* url_loader) override;
  base::Optional<SubresourceLoaderParams> MaybeCreateSubresourceLoaderParams()
      override;

  // These methods are used for subresource loading by the
  // AppCacheSubresourceURLFactory::SubresourceLoader class.
  // Internally they use same methods used by the network library based impl,
  // MaybeLoadResource, MaybeLoadFallbackForResponse, and
  // MaybeLoadFallbackForRedirect. Eventually one of the Deliver*Response()
  // methods is called and the LoaderCallback is invoked.
  void MaybeCreateSubresourceLoader(
      const network::ResourceRequest& resource_request,
      LoaderCallback callback);
  void MaybeFallbackForSubresourceResponse(
      const network::ResourceResponseHead& response,
      LoaderCallback callback);
  void MaybeFallbackForSubresourceRedirect(
      const net::RedirectInfo& redirect_info,
      LoaderCallback callback);
  void MaybeFollowSubresourceRedirect(const net::RedirectInfo& redirect_info,
                                      LoaderCallback callback);

  static std::unique_ptr<AppCacheRequestHandler>
  InitializeForNavigationNetworkService(
      const network::ResourceRequest& request,
      AppCacheNavigationHandleCore* appcache_handle_core,
      URLLoaderFactoryGetter* url_loader_factory_getter);

  static bool IsMainResourceType(ResourceType type) {
    return IsResourceTypeFrame(type) || type == RESOURCE_TYPE_SHARED_WORKER;
  }

  // Called by unittests to indicate that we are in test mode.
  static void SetRunningInTests(bool in_tests);
  static bool IsRunningInTests();

 private:
  friend class AppCacheHost;
  friend class AppCacheRequestHandlerTest;

  // Callers should use AppCacheHost::CreateRequestHandler.
  AppCacheRequestHandler(AppCacheHost* host,
                         ResourceType resource_type,
                         bool should_reset_appcache,
                         std::unique_ptr<AppCacheRequest> request);

  // AppCacheHost::Observer override
  void OnDestructionImminent(AppCacheHost* host) override;

  // AppCacheServiceImpl::Observer override
  void OnServiceDestructionImminent(AppCacheServiceImpl* service) override;

  // Helpers to instruct a waiting job with what response to
  // deliver for the request we're handling.
  void DeliverAppCachedResponse(const AppCacheEntry& entry,
                                int64_t cache_id,
                                const GURL& manifest_url,
                                bool is_fallback,
                                const GURL& namespace_entry_url);
  void DeliverNetworkResponse();
  void DeliverErrorResponse();

  // Called just before the request is restarted. Grabs the reason for
  // restarting, so can correctly continue to handle the request.
  void OnPrepareToRestartURLRequest();

  // Helper method to create an AppCacheJob and populate job_.
  // Caller takes ownership of returned value.
  std::unique_ptr<AppCacheJob> CreateJob(
      net::NetworkDelegate* network_delegate);

  // Helper to retrieve a pointer to the storage object.
  AppCacheStorage* storage() const;

  bool is_main_resource() const {
    return IsMainResourceType(resource_type_);
  }

  // Main-resource loading -------------------------------------
  // Frame and SharedWorker main resources are handled here.

  std::unique_ptr<AppCacheJob> MaybeLoadMainResource(
      net::NetworkDelegate* network_delegate);

  // AppCacheStorage::Delegate methods
  void OnMainResponseFound(const GURL& url,
                           const AppCacheEntry& entry,
                           const GURL& fallback_url,
                           const AppCacheEntry& fallback_entry,
                           int64_t cache_id,
                           int64_t group_id,
                           const GURL& mainfest_url) override;

  // NetworkService loading:
  // Called when a |callback| that is originally given to |MaybeCreateLoader()|
  // runs for the main resource. This flips |should_create_subresource_loader_|
  // if a non-null |handler| is given. Always invokes |callback| with |handler|.
  void RunLoaderCallbackForMainResource(
      LoaderCallback callback,
      SingleRequestURLLoaderFactory::RequestHandler handler);

  // Sub-resource loading -------------------------------------
  // Dedicated worker and all manner of sub-resources are handled here.

  std::unique_ptr<AppCacheJob> MaybeLoadSubResource(
      net::NetworkDelegate* network_delegate);
  void ContinueMaybeLoadSubResource();

  // AppCacheHost::Observer override
  void OnCacheSelectionComplete(AppCacheHost* host) override;

  // Data members -----------------------------------------------

  // What host we're servicing a request for.
  AppCacheHost* host_;

  // Frame vs subresource vs sharedworker loads are somewhat different.
  ResourceType resource_type_;

  // True if corresponding AppCache group should be resetted before load.
  bool should_reset_appcache_;

  // Subresource requests wait until after cache selection completes.
  bool is_waiting_for_cache_selection_;

  // Info about the type of response we found for delivery.
  // These are relevant for both main and subresource requests.
  int64_t found_group_id_;
  int64_t found_cache_id_;
  AppCacheEntry found_entry_;
  AppCacheEntry found_fallback_entry_;
  GURL found_namespace_entry_url_;
  GURL found_manifest_url_;
  bool found_network_namespace_;

  // True if a cache entry this handler attempted to return was
  // not found in the disk cache. Once set, the handler will take
  // no action on all subsequent intercept opportunities, so the
  // request and any redirects will be handled by the network library.
  bool cache_entry_not_found_;

  // True if the next time this request is started, the response should be
  // delivered from the network, bypassing the AppCache. Cleared after the next
  // intercept opportunity.
  bool is_delivering_network_response_;

  // True if this->MaybeLoadResource(...) has been called in the past.
  bool maybe_load_resource_executed_;

  // The job we use to deliver a response. Only NULL during the following times:
  // 1) Before request has started a job.
  // 2) Request is not being handled by appcache.
  // 3) Request has been cancelled, and the job killed.
  base::WeakPtr<AppCacheJob> job_;

  // During a cross site navigation, we transfer ownership the AppcacheHost
  // from the old processes structures over to the new structures.
  std::unique_ptr<AppCacheHost> host_for_cross_site_transfer_;
  int old_process_id_;
  int old_host_id_;

  // Cached information about the response being currently served by the
  // AppCache, if there is one.
  int cache_id_;
  GURL manifest_url_;

  // Backptr to the central service object.
  AppCacheServiceImpl* service_;

  std::unique_ptr<AppCacheRequest> request_;

  // Network service related members.

  LoaderCallback loader_callback_;

  // Flipped to true if AppCache wants to handle subresource requests
  // (i.e. when |loader_callback_| is fired with a non-null
  // RequestHandler for non-error cases.
  bool should_create_subresource_loader_ = false;

  // Points to the getter for the network URL loader.
  scoped_refptr<URLLoaderFactoryGetter> network_url_loader_factory_getter_;

  // The AppCache host instance. We pass this to the
  // AppCacheSubresourceURLFactory instance on creation.
  base::WeakPtr<AppCacheHost> appcache_host_;

  base::WeakPtrFactory<AppCacheRequestHandler> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(AppCacheRequestHandler);
};

}  // namespace content

#endif  // CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_