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