Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / webkit / browser / appcache / appcache_url_request_job.h
1 // Copyright (c) 2012 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_URL_REQUEST_JOB_H_
6 #define WEBKIT_BROWSER_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_
7
8 #include <string>
9
10 #include "base/memory/weak_ptr.h"
11 #include "net/http/http_byte_range.h"
12 #include "net/url_request/url_request_job.h"
13 #include "webkit/browser/appcache/appcache_entry.h"
14 #include "webkit/browser/appcache/appcache_executable_handler.h"
15 #include "webkit/browser/appcache/appcache_response.h"
16 #include "webkit/browser/appcache/appcache_storage.h"
17 #include "webkit/browser/webkit_storage_browser_export.h"
18
19 namespace net {
20 class GrowableIOBuffer;
21 };
22
23 namespace appcache {
24
25 class AppCacheHost;
26
27 // A net::URLRequestJob derivative that knows how to return a response stored
28 // in the appcache.
29 class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheURLRequestJob
30     : public net::URLRequestJob,
31       public AppCacheStorage::Delegate {
32  public:
33   AppCacheURLRequestJob(net::URLRequest* request,
34                         net::NetworkDelegate* network_delegate,
35                         AppCacheStorage* storage,
36                         AppCacheHost* host,
37                         bool is_main_resource);
38
39   // Informs the job of what response it should deliver. Only one of these
40   // methods should be called, and only once per job. A job will sit idle and
41   // wait indefinitely until one of the deliver methods is called.
42   void DeliverAppCachedResponse(const GURL& manifest_url, int64 group_id,
43                                 int64 cache_id, const AppCacheEntry& entry,
44                                 bool is_fallback);
45   void DeliverNetworkResponse();
46   void DeliverErrorResponse();
47
48   bool is_waiting() const {
49     return delivery_type_ == AWAITING_DELIVERY_ORDERS;
50   }
51
52   bool is_delivering_appcache_response() const {
53     return delivery_type_ == APPCACHED_DELIVERY;
54   }
55
56   bool is_delivering_network_response() const {
57     return delivery_type_ == NETWORK_DELIVERY;
58   }
59
60   bool is_delivering_error_response() const {
61     return delivery_type_ == ERROR_DELIVERY;
62   }
63
64   // Accessors for the info about the appcached response, if any,
65   // that this job has been instructed to deliver. These are only
66   // valid to call if is_delivering_appcache_response.
67   const GURL& manifest_url() const { return manifest_url_; }
68   int64 group_id() const { return group_id_; }
69   int64 cache_id() const { return cache_id_; }
70   const AppCacheEntry& entry() const { return entry_; }
71
72   // net::URLRequestJob's Kill method is made public so the users of this
73   // class in the appcache namespace can call it.
74   virtual void Kill() OVERRIDE;
75
76   // Returns true if the job has been started by the net library.
77   bool has_been_started() const {
78     return has_been_started_;
79   }
80
81   // Returns true if the job has been killed.
82   bool has_been_killed() const {
83     return has_been_killed_;
84   }
85
86   // Returns true if the cache entry was not found in the disk cache.
87   bool cache_entry_not_found() const {
88     return cache_entry_not_found_;
89   }
90
91  protected:
92   virtual ~AppCacheURLRequestJob();
93
94  private:
95   friend class AppCacheRequestHandlerTest;
96   friend class AppCacheURLRequestJobTest;
97
98   enum DeliveryType {
99     AWAITING_DELIVERY_ORDERS,
100     APPCACHED_DELIVERY,
101     NETWORK_DELIVERY,
102     ERROR_DELIVERY
103   };
104
105   // Returns true if one of the Deliver methods has been called.
106   bool has_delivery_orders() const {
107     return !is_waiting();
108   }
109
110   void MaybeBeginDelivery();
111   void BeginDelivery();
112
113   // For executable response handling.
114   void BeginExecutableHandlerDelivery();
115   void OnExecutableSourceLoaded(int result);
116   void InvokeExecutableHandler(AppCacheExecutableHandler* handler);
117   void OnExecutableResponseCallback(
118       const AppCacheExecutableHandler::Response& response);
119   void BeginErrorDelivery(const char* message);
120
121   // AppCacheStorage::Delegate methods
122   virtual void OnResponseInfoLoaded(
123       AppCacheResponseInfo* response_info, int64 response_id) OVERRIDE;
124   virtual void OnCacheLoaded(AppCache* cache, int64 cache_id) OVERRIDE;
125
126   const net::HttpResponseInfo* http_info() const;
127   bool is_range_request() const { return range_requested_.IsValid(); }
128   void SetupRangeResponse();
129
130   // AppCacheResponseReader completion callback
131   void OnReadComplete(int result);
132
133   // net::URLRequestJob methods, see url_request_job.h for doc comments
134   virtual void Start() OVERRIDE;
135   virtual net::LoadState GetLoadState() const OVERRIDE;
136   virtual bool GetCharset(std::string* charset) OVERRIDE;
137   virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;
138   virtual bool ReadRawData(net::IOBuffer* buf,
139                            int buf_size,
140                            int *bytes_read) OVERRIDE;
141
142   // Sets extra request headers for Job types that support request headers.
143   // This is how we get informed of range-requests.
144   virtual void SetExtraRequestHeaders(
145       const net::HttpRequestHeaders& headers) OVERRIDE;
146
147   // FilterContext methods
148   virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
149   virtual int GetResponseCode() const OVERRIDE;
150
151   AppCacheHost* host_;
152   AppCacheStorage* storage_;
153   base::TimeTicks start_time_tick_;
154   bool has_been_started_;
155   bool has_been_killed_;
156   DeliveryType delivery_type_;
157   GURL manifest_url_;
158   int64 group_id_;
159   int64 cache_id_;
160   AppCacheEntry entry_;
161   bool is_fallback_;
162   bool is_main_resource_;  // Used for histogram logging.
163   bool cache_entry_not_found_;
164   scoped_refptr<AppCacheResponseInfo> info_;
165   scoped_refptr<net::GrowableIOBuffer> handler_source_buffer_;
166   scoped_ptr<AppCacheResponseReader> handler_source_reader_;
167   net::HttpByteRange range_requested_;
168   scoped_ptr<net::HttpResponseInfo> range_response_info_;
169   scoped_ptr<AppCacheResponseReader> reader_;
170   scoped_refptr<AppCache> cache_;
171   scoped_refptr<AppCacheGroup> group_;
172   base::WeakPtrFactory<AppCacheURLRequestJob> weak_factory_;
173 };
174
175 }  // namespace appcache
176
177 #endif  // WEBKIT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_