15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_ 6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define CONTENT_BROWSER_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/weak_ptr.h" 11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/browser/appcache/appcache_entry.h" 12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/browser/appcache/appcache_executable_handler.h" 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/browser/appcache/appcache_response.h" 14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/browser/appcache/appcache_storage.h" 15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/common/content_export.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_byte_range.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_job.h" 18a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace net { 207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class GrowableIOBuffer; 217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}; 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 23116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace content { 247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class AppCacheHost; 25116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass AppCacheRequestHandlerTest; 26116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass AppCacheURLRequestJobTest; 277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A net::URLRequestJob derivative that knows how to return a response stored 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the appcache. 30116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass CONTENT_EXPORT AppCacheURLRequestJob 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : public net::URLRequestJob, 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public AppCacheStorage::Delegate { 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AppCacheURLRequestJob(net::URLRequest* request, 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::NetworkDelegate* network_delegate, 367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) AppCacheStorage* storage, 37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch AppCacheHost* host, 38effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch bool is_main_resource); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Informs the job of what response it should deliver. Only one of these 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // methods should be called, and only once per job. A job will sit idle and 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // wait indefinitely until one of the deliver methods is called. 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void DeliverAppCachedResponse(const GURL& manifest_url, int64 group_id, 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64 cache_id, const AppCacheEntry& entry, 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_fallback); 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void DeliverNetworkResponse(); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void DeliverErrorResponse(); 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_waiting() const { 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return delivery_type_ == AWAITING_DELIVERY_ORDERS; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_delivering_appcache_response() const { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return delivery_type_ == APPCACHED_DELIVERY; 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_delivering_network_response() const { 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return delivery_type_ == NETWORK_DELIVERY; 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_delivering_error_response() const { 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return delivery_type_ == ERROR_DELIVERY; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Accessors for the info about the appcached response, if any, 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // that this job has been instructed to deliver. These are only 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // valid to call if is_delivering_appcache_response. 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const GURL& manifest_url() const { return manifest_url_; } 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64 group_id() const { return group_id_; } 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64 cache_id() const { return cache_id_; } 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const AppCacheEntry& entry() const { return entry_; } 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // net::URLRequestJob's Kill method is made public so the users of this 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // class in the appcache namespace can call it. 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Kill() OVERRIDE; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the job has been started by the net library. 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool has_been_started() const { 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return has_been_started_; 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the job has been killed. 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool has_been_killed() const { 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return has_been_killed_; 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the cache entry was not found in the disk cache. 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool cache_entry_not_found() const { 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return cache_entry_not_found_; 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~AppCacheURLRequestJob(); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 96a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch friend class content::AppCacheRequestHandlerTest; 97a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch friend class content::AppCacheURLRequestJobTest; 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum DeliveryType { 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AWAITING_DELIVERY_ORDERS, 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) APPCACHED_DELIVERY, 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NETWORK_DELIVERY, 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ERROR_DELIVERY 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if one of the Deliver methods has been called. 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool has_delivery_orders() const { 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return !is_waiting(); 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void MaybeBeginDelivery(); 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void BeginDelivery(); 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // For executable response handling. 1157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void BeginExecutableHandlerDelivery(); 1167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void OnExecutableSourceLoaded(int result); 1177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void InvokeExecutableHandler(AppCacheExecutableHandler* handler); 1187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void OnExecutableResponseCallback( 1197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const AppCacheExecutableHandler::Response& response); 1207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void BeginErrorDelivery(const char* message); 1217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // AppCacheStorage::Delegate methods 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void OnResponseInfoLoaded( 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AppCacheResponseInfo* response_info, int64 response_id) OVERRIDE; 1257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) virtual void OnCacheLoaded(AppCache* cache, int64 cache_id) OVERRIDE; 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::HttpResponseInfo* http_info() const; 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_range_request() const { return range_requested_.IsValid(); } 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SetupRangeResponse(); 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // AppCacheResponseReader completion callback 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void OnReadComplete(int result); 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // net::URLRequestJob methods, see url_request_job.h for doc comments 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Start() OVERRIDE; 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual net::LoadState GetLoadState() const OVERRIDE; 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool GetCharset(std::string* charset) OVERRIDE; 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE; 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool ReadRawData(net::IOBuffer* buf, 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int buf_size, 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int *bytes_read) OVERRIDE; 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sets extra request headers for Job types that support request headers. 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This is how we get informed of range-requests. 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetExtraRequestHeaders( 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::HttpRequestHeaders& headers) OVERRIDE; 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // FilterContext methods 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int GetResponseCode() const OVERRIDE; 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) AppCacheHost* host_; 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AppCacheStorage* storage_; 1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::TimeTicks start_time_tick_; 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool has_been_started_; 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool has_been_killed_; 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DeliveryType delivery_type_; 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL manifest_url_; 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64 group_id_; 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64 cache_id_; 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AppCacheEntry entry_; 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_fallback_; 163effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch bool is_main_resource_; // Used for histogram logging. 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool cache_entry_not_found_; 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<AppCacheResponseInfo> info_; 1667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) scoped_refptr<net::GrowableIOBuffer> handler_source_buffer_; 1677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) scoped_ptr<AppCacheResponseReader> handler_source_reader_; 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::HttpByteRange range_requested_; 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<net::HttpResponseInfo> range_response_info_; 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<AppCacheResponseReader> reader_; 1717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) scoped_refptr<AppCache> cache_; 1727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) scoped_refptr<AppCacheGroup> group_; 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::WeakPtrFactory<AppCacheURLRequestJob> weak_factory_; 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 176116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} // namespace content 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 178116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif // CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_ 179