1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_
8 #include "base/compiler_specific.h"
9 #include "base/supports_user_data.h"
10 #include "content/browser/appcache/appcache_entry.h"
11 #include "content/browser/appcache/appcache_host.h"
12 #include "content/common/content_export.h"
13 #include "content/public/common/resource_type.h"
16 class NetworkDelegate
;
22 class AppCacheRequestHandlerTest
;
23 class AppCacheURLRequestJob
;
25 // An instance is created for each net::URLRequest. The instance survives all
26 // http transactions involved in the processing of its net::URLRequest, and is
27 // given the opportunity to hijack the request along the way. Callers
28 // should use AppCacheHost::CreateRequestHandler to manufacture instances
29 // that can retrieve resources for a particular host.
30 class CONTENT_EXPORT AppCacheRequestHandler
31 : public base::SupportsUserData::Data
,
32 public AppCacheHost::Observer
,
33 public AppCacheStorage::Delegate
{
35 ~AppCacheRequestHandler() override
;
37 // These are called on each request intercept opportunity.
38 AppCacheURLRequestJob
* MaybeLoadResource(
39 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
);
40 AppCacheURLRequestJob
* MaybeLoadFallbackForRedirect(
41 net::URLRequest
* request
,
42 net::NetworkDelegate
* network_delegate
,
43 const GURL
& location
);
44 AppCacheURLRequestJob
* MaybeLoadFallbackForResponse(
45 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
);
47 void GetExtraResponseInfo(int64
* cache_id
, GURL
* manifest_url
);
49 // Methods to support cross site navigations.
50 void PrepareForCrossSiteTransfer(int old_process_id
);
51 void CompleteCrossSiteTransfer(int new_process_id
, int new_host_id
);
52 void MaybeCompleteCrossSiteTransferInOldProcess(int old_process_id
);
54 static bool IsMainResourceType(ResourceType type
) {
55 return IsResourceTypeFrame(type
) ||
56 type
== RESOURCE_TYPE_SHARED_WORKER
;
60 friend class AppCacheHost
;
62 // Callers should use AppCacheHost::CreateRequestHandler.
63 AppCacheRequestHandler(AppCacheHost
* host
, ResourceType resource_type
,
64 bool should_reset_appcache
);
66 // AppCacheHost::Observer override
67 void OnDestructionImminent(AppCacheHost
* host
) override
;
69 // Helpers to instruct a waiting job with what response to
70 // deliver for the request we're handling.
71 void DeliverAppCachedResponse(const AppCacheEntry
& entry
, int64 cache_id
,
72 int64 group_id
, const GURL
& manifest_url
,
74 const GURL
& namespace_entry_url
);
75 void DeliverNetworkResponse();
76 void DeliverErrorResponse();
78 // Helper to retrieve a pointer to the storage object.
79 AppCacheStorage
* storage() const;
81 bool is_main_resource() const {
82 return IsMainResourceType(resource_type_
);
85 // Main-resource loading -------------------------------------
86 // Frame and SharedWorker main resources are handled here.
88 void MaybeLoadMainResource(net::URLRequest
* request
,
89 net::NetworkDelegate
* network_delegate
);
91 // AppCacheStorage::Delegate methods
92 void OnMainResponseFound(const GURL
& url
,
93 const AppCacheEntry
& entry
,
94 const GURL
& fallback_url
,
95 const AppCacheEntry
& fallback_entry
,
98 const GURL
& mainfest_url
) override
;
100 // Sub-resource loading -------------------------------------
101 // Dedicated worker and all manner of sub-resources are handled here.
103 void MaybeLoadSubResource(net::URLRequest
* request
,
104 net::NetworkDelegate
* network_delegate
);
105 void ContinueMaybeLoadSubResource();
107 // AppCacheHost::Observer override
108 void OnCacheSelectionComplete(AppCacheHost
* host
) override
;
110 // Data members -----------------------------------------------
112 // What host we're servicing a request for.
115 // Frame vs subresource vs sharedworker loads are somewhat different.
116 ResourceType resource_type_
;
118 // True if corresponding AppCache group should be resetted before load.
119 bool should_reset_appcache_
;
121 // Subresource requests wait until after cache selection completes.
122 bool is_waiting_for_cache_selection_
;
124 // Info about the type of response we found for delivery.
125 // These are relevant for both main and subresource requests.
126 int64 found_group_id_
;
127 int64 found_cache_id_
;
128 AppCacheEntry found_entry_
;
129 AppCacheEntry found_fallback_entry_
;
130 GURL found_namespace_entry_url_
;
131 GURL found_manifest_url_
;
132 bool found_network_namespace_
;
134 // True if a cache entry this handler attempted to return was
135 // not found in the disk cache. Once set, the handler will take
136 // no action on all subsequent intercept opportunities, so the
137 // request and any redirects will be handled by the network library.
138 bool cache_entry_not_found_
;
140 // True if this->MaybeLoadResource(...) has been called in the past.
141 bool maybe_load_resource_executed_
;
143 // The job we use to deliver a response.
144 scoped_refptr
<AppCacheURLRequestJob
> job_
;
146 // During a cross site navigation, we transfer ownership the AppcacheHost
147 // from the old processes structures over to the new structures.
148 scoped_ptr
<AppCacheHost
> host_for_cross_site_transfer_
;
152 friend class content::AppCacheRequestHandlerTest
;
153 DISALLOW_COPY_AND_ASSIGN(AppCacheRequestHandler
);
156 } // namespace content
158 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_