- add sources.
[platform/framework/web/crosswalk.git] / src / webkit / browser / appcache / appcache_request_handler.h
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 appcache {
22
23 class AppCacheURLRequestJob;
24
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 WEBKIT_STORAGE_BROWSER_EXPORT AppCacheRequestHandler
31     : public base::SupportsUserData::Data,
32       public AppCacheHost::Observer,
33       public AppCacheStorage::Delegate  {
34  public:
35   virtual ~AppCacheRequestHandler();
36
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);
46
47   void GetExtraResponseInfo(int64* cache_id, GURL* manifest_url);
48
49   static bool IsMainResourceType(ResourceType::Type type) {
50     return ResourceType::IsFrame(type) ||
51            ResourceType::IsSharedWorker(type);
52   }
53
54  private:
55   friend class AppCacheHost;
56
57   // Callers should use AppCacheHost::CreateRequestHandler.
58   AppCacheRequestHandler(AppCacheHost* host, ResourceType::Type resource_type);
59
60   // AppCacheHost::Observer override
61   virtual void OnDestructionImminent(AppCacheHost* host) OVERRIDE;
62
63   // Helpers to instruct a waiting job with what response to
64   // deliver for the request we're handling.
65   void DeliverAppCachedResponse(const AppCacheEntry& entry, int64 cache_id,
66                                 int64 group_id, const GURL& manifest_url,
67                                 bool is_fallback,
68                                 const GURL& namespace_entry_url);
69   void DeliverNetworkResponse();
70   void DeliverErrorResponse();
71
72   // Helper to retrieve a pointer to the storage object.
73   AppCacheStorage* storage() const;
74
75   bool is_main_resource() const {
76     return IsMainResourceType(resource_type_);
77   }
78
79   // Main-resource loading -------------------------------------
80   // Frame and SharedWorker main resources are handled here.
81
82   void MaybeLoadMainResource(net::URLRequest* request,
83                              net::NetworkDelegate* network_delegate);
84
85   // AppCacheStorage::Delegate methods
86   virtual void OnMainResponseFound(
87       const GURL& url, const AppCacheEntry& entry,
88       const GURL& fallback_url, const AppCacheEntry& fallback_entry,
89       int64 cache_id, int64 group_id, const GURL& mainfest_url) OVERRIDE;
90
91   // Sub-resource loading -------------------------------------
92   // Dedicated worker and all manner of sub-resources are handled here.
93
94   void MaybeLoadSubResource(net::URLRequest* request,
95                             net::NetworkDelegate* network_delegate);
96   void ContinueMaybeLoadSubResource();
97
98   // AppCacheHost::Observer override
99   virtual void OnCacheSelectionComplete(AppCacheHost* host) OVERRIDE;
100
101   // Data members -----------------------------------------------
102
103   // What host we're servicing a request for.
104   AppCacheHost* host_;
105
106   // Frame vs subresource vs sharedworker loads are somewhat different.
107   ResourceType::Type resource_type_;
108
109   // Subresource requests wait until after cache selection completes.
110   bool is_waiting_for_cache_selection_;
111
112   // Info about the type of response we found for delivery.
113   // These are relevant for both main and subresource requests.
114   int64 found_group_id_;
115   int64 found_cache_id_;
116   AppCacheEntry found_entry_;
117   AppCacheEntry found_fallback_entry_;
118   GURL found_namespace_entry_url_;
119   GURL found_manifest_url_;
120   bool found_network_namespace_;
121
122   // True if a cache entry this handler attempted to return was
123   // not found in the disk cache. Once set, the handler will take
124   // no action on all subsequent intercept opportunities, so the
125   // request and any redirects will be handled by the network library.
126   bool cache_entry_not_found_;
127
128   // True if this->MaybeLoadResource(...) has been called in the past.
129   bool maybe_load_resource_executed_;
130
131   // The job we use to deliver a response.
132   scoped_refptr<AppCacheURLRequestJob> job_;
133
134   friend class AppCacheRequestHandlerTest;
135   DISALLOW_COPY_AND_ASSIGN(AppCacheRequestHandler);
136 };
137
138 }  // namespace appcache
139
140 #endif  // WEBKIT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_