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. 4 5 #ifndef WEBKIT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_ 6 #define WEBKIT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_ 7 8 #include "base/compiler_specific.h" 9 #include "base/supports_user_data.h" 10 #include "webkit/browser/appcache/appcache_entry.h" 11 #include "webkit/browser/appcache/appcache_host.h" 12 #include "webkit/browser/webkit_storage_browser_export.h" 13 #include "webkit/common/resource_type.h" 14 15 namespace net { 16 class NetworkDelegate; 17 class URLRequest; 18 class URLRequestJob; 19 } // namespace net 20 21 namespace content { 22 class AppCacheRequestHandlerTest; 23 } 24 25 namespace appcache { 26 27 class AppCacheURLRequestJob; 28 29 // An instance is created for each net::URLRequest. The instance survives all 30 // http transactions involved in the processing of its net::URLRequest, and is 31 // given the opportunity to hijack the request along the way. Callers 32 // should use AppCacheHost::CreateRequestHandler to manufacture instances 33 // that can retrieve resources for a particular host. 34 class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheRequestHandler 35 : public base::SupportsUserData::Data, 36 public AppCacheHost::Observer, 37 public AppCacheStorage::Delegate { 38 public: 39 virtual ~AppCacheRequestHandler(); 40 41 // These are called on each request intercept opportunity. 42 AppCacheURLRequestJob* MaybeLoadResource( 43 net::URLRequest* request, net::NetworkDelegate* network_delegate); 44 AppCacheURLRequestJob* MaybeLoadFallbackForRedirect( 45 net::URLRequest* request, 46 net::NetworkDelegate* network_delegate, 47 const GURL& location); 48 AppCacheURLRequestJob* MaybeLoadFallbackForResponse( 49 net::URLRequest* request, net::NetworkDelegate* network_delegate); 50 51 void GetExtraResponseInfo(int64* cache_id, GURL* manifest_url); 52 53 // Methods to support cross site navigations. 54 void PrepareForCrossSiteTransfer(int old_process_id); 55 void CompleteCrossSiteTransfer(int new_process_id, int new_host_id); 56 IsMainResourceType(ResourceType::Type type)57 static bool IsMainResourceType(ResourceType::Type type) { 58 return ResourceType::IsFrame(type) || 59 ResourceType::IsSharedWorker(type); 60 } 61 62 private: 63 friend class AppCacheHost; 64 65 // Callers should use AppCacheHost::CreateRequestHandler. 66 AppCacheRequestHandler(AppCacheHost* host, ResourceType::Type resource_type); 67 68 // AppCacheHost::Observer override 69 virtual void OnDestructionImminent(AppCacheHost* host) OVERRIDE; 70 71 // Helpers to instruct a waiting job with what response to 72 // deliver for the request we're handling. 73 void DeliverAppCachedResponse(const AppCacheEntry& entry, int64 cache_id, 74 int64 group_id, const GURL& manifest_url, 75 bool is_fallback, 76 const GURL& namespace_entry_url); 77 void DeliverNetworkResponse(); 78 void DeliverErrorResponse(); 79 80 // Helper to retrieve a pointer to the storage object. 81 AppCacheStorage* storage() const; 82 is_main_resource()83 bool is_main_resource() const { 84 return IsMainResourceType(resource_type_); 85 } 86 87 // Main-resource loading ------------------------------------- 88 // Frame and SharedWorker main resources are handled here. 89 90 void MaybeLoadMainResource(net::URLRequest* request, 91 net::NetworkDelegate* network_delegate); 92 93 // AppCacheStorage::Delegate methods 94 virtual void OnMainResponseFound( 95 const GURL& url, const AppCacheEntry& entry, 96 const GURL& fallback_url, const AppCacheEntry& fallback_entry, 97 int64 cache_id, int64 group_id, const GURL& mainfest_url) OVERRIDE; 98 99 // Sub-resource loading ------------------------------------- 100 // Dedicated worker and all manner of sub-resources are handled here. 101 102 void MaybeLoadSubResource(net::URLRequest* request, 103 net::NetworkDelegate* network_delegate); 104 void ContinueMaybeLoadSubResource(); 105 106 // AppCacheHost::Observer override 107 virtual void OnCacheSelectionComplete(AppCacheHost* host) OVERRIDE; 108 109 // Data members ----------------------------------------------- 110 111 // What host we're servicing a request for. 112 AppCacheHost* host_; 113 114 // Frame vs subresource vs sharedworker loads are somewhat different. 115 ResourceType::Type resource_type_; 116 117 // Subresource requests wait until after cache selection completes. 118 bool is_waiting_for_cache_selection_; 119 120 // Info about the type of response we found for delivery. 121 // These are relevant for both main and subresource requests. 122 int64 found_group_id_; 123 int64 found_cache_id_; 124 AppCacheEntry found_entry_; 125 AppCacheEntry found_fallback_entry_; 126 GURL found_namespace_entry_url_; 127 GURL found_manifest_url_; 128 bool found_network_namespace_; 129 130 // True if a cache entry this handler attempted to return was 131 // not found in the disk cache. Once set, the handler will take 132 // no action on all subsequent intercept opportunities, so the 133 // request and any redirects will be handled by the network library. 134 bool cache_entry_not_found_; 135 136 // True if this->MaybeLoadResource(...) has been called in the past. 137 bool maybe_load_resource_executed_; 138 139 // The job we use to deliver a response. 140 scoped_refptr<AppCacheURLRequestJob> job_; 141 142 // During a cross site navigation, we transfer ownership the AppcacheHost 143 // from the old processes structures over to the new structures. 144 scoped_ptr<AppCacheHost> host_for_cross_site_transfer_; 145 146 friend class content::AppCacheRequestHandlerTest; 147 DISALLOW_COPY_AND_ASSIGN(AppCacheRequestHandler); 148 }; 149 150 } // namespace appcache 151 152 #endif // WEBKIT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_ 153