1 // Copyright 2014 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_SERVICE_WORKER_SERVICE_WORKER_READ_FROM_CACHE_JOB_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_READ_FROM_CACHE_JOB_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/weak_ptr.h"
10 #include "content/browser/service_worker/service_worker_disk_cache.h"
11 #include "content/common/content_export.h"
12 #include "net/http/http_byte_range.h"
13 #include "net/url_request/url_request_job.h"
17 class ServiceWorkerContextCore;
18 class ServiceWorkerResponseReader;
20 // A URLRequestJob derivative used to retrieve script resources
21 // from the service workers script cache. It uses a response reader
22 // and pipes the response to the consumer of this url request job.
23 class CONTENT_EXPORT ServiceWorkerReadFromCacheJob
24 : public net::URLRequestJob {
26 ServiceWorkerReadFromCacheJob(
27 net::URLRequest* request,
28 net::NetworkDelegate* network_delegate,
29 base::WeakPtr<ServiceWorkerContextCore> context,
33 virtual ~ServiceWorkerReadFromCacheJob();
35 // net::URLRequestJob overrides
36 virtual void Start() OVERRIDE;
37 virtual void Kill() OVERRIDE;
38 virtual net::LoadState GetLoadState() const OVERRIDE;
39 virtual bool GetCharset(std::string* charset) OVERRIDE;
40 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
41 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;
42 virtual int GetResponseCode() const OVERRIDE;
43 virtual void SetExtraRequestHeaders(
44 const net::HttpRequestHeaders& headers) OVERRIDE;
45 virtual bool ReadRawData(net::IOBuffer* buf,
47 int *bytes_read) OVERRIDE;
49 // Reader completion callbacks.
50 void OnReadInfoComplete(int result);
51 void OnReadComplete(int result);
54 const net::HttpResponseInfo* http_info() const;
55 bool is_range_request() const { return range_requested_.IsValid(); }
56 void SetupRangeResponse(int response_data_size);
58 base::WeakPtr<ServiceWorkerContextCore> context_;
60 scoped_ptr<ServiceWorkerResponseReader> reader_;
61 scoped_refptr<HttpResponseInfoIOBuffer> http_info_io_buffer_;
62 scoped_ptr<net::HttpResponseInfo> http_info_;
63 net::HttpByteRange range_requested_;
64 scoped_ptr<net::HttpResponseInfo> range_response_info_;
65 bool has_been_killed_;
66 base::WeakPtrFactory<ServiceWorkerReadFromCacheJob> weak_factory_;
68 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerReadFromCacheJob);
71 } // namespace content
73 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_READ_FROM_CACHE_JOB_