Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_write_to_cache_job.h
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_WRITE_TO_CACHE_JOB_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_WRITE_TO_CACHE_JOB_H_
7
8 #include <string>
9
10 #include "base/memory/weak_ptr.h"
11 #include "content/browser/service_worker/service_worker_disk_cache.h"
12 #include "content/browser/service_worker/service_worker_version.h"
13 #include "content/common/content_export.h"
14 #include "content/common/service_worker/service_worker_status_code.h"
15 #include "content/common/service_worker/service_worker_types.h"
16 #include "content/public/common/resource_type.h"
17 #include "net/url_request/url_request.h"
18 #include "net/url_request/url_request_job.h"
19
20 namespace content {
21
22 class ServiceWorkerContextCore;
23 class ServiceWorkerResponseWriter;
24 class ServiceWorkerVersions;
25
26 // A URLRequestJob derivative used to cache the main script
27 // and its imports during the initial install of a new version.
28 // Another separate URLRequest is started which will perform
29 // a network fetch. The response produced for that separate
30 // request is written to the service worker script cache and piped
31 // to the consumer of the ServiceWorkerWriteToCacheJob for delivery
32 // to the renderer process housing the worker.
33 class CONTENT_EXPORT ServiceWorkerWriteToCacheJob
34     : public net::URLRequestJob,
35       public net::URLRequest::Delegate {
36  public:
37   ServiceWorkerWriteToCacheJob(
38       net::URLRequest* request,
39       net::NetworkDelegate* network_delegate,
40       ResourceType resource_type,
41       base::WeakPtr<ServiceWorkerContextCore> context,
42       ServiceWorkerVersion* version,
43       int extra_load_flags,
44       int64 response_id);
45
46  private:
47   FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
48                            UpdateBefore24Hours);
49   FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
50                            UpdateAfter24Hours);
51
52   ~ServiceWorkerWriteToCacheJob() override;
53
54   // net::URLRequestJob overrides
55   void Start() override;
56   void Kill() override;
57   net::LoadState GetLoadState() const override;
58   bool GetCharset(std::string* charset) override;
59   bool GetMimeType(std::string* mime_type) const override;
60   void GetResponseInfo(net::HttpResponseInfo* info) override;
61   int GetResponseCode() const override;
62   void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override;
63   bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override;
64
65   const net::HttpResponseInfo* http_info() const;
66
67   // Methods to drive the net request forward and
68   // write data to the disk cache.
69   void InitNetRequest(int extra_load_flags);
70   void StartNetRequest();
71   net::URLRequestStatus ReadNetData(
72       net::IOBuffer* buf,
73       int buf_size,
74       int *bytes_read);
75   void WriteHeadersToCache();
76   void OnWriteHeadersComplete(int result);
77   void WriteDataToCache(int bytes_to_write);
78   void OnWriteDataComplete(int result);
79
80   // net::URLRequest::Delegate overrides that observe the net request.
81   void OnReceivedRedirect(net::URLRequest* request,
82                           const net::RedirectInfo& redirect_info,
83                           bool* defer_redirect) override;
84   void OnAuthRequired(net::URLRequest* request,
85                       net::AuthChallengeInfo* auth_info) override;
86   void OnCertificateRequested(
87       net::URLRequest* request,
88       net::SSLCertRequestInfo* cert_request_info) override;
89   void OnSSLCertificateError(net::URLRequest* request,
90                              const net::SSLInfo& ssl_info,
91                              bool fatal) override;
92   void OnBeforeNetworkStart(net::URLRequest* request, bool* defer) override;
93   void OnResponseStarted(net::URLRequest* request) override;
94   void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
95
96   void AsyncNotifyDoneHelper(const net::URLRequestStatus& status);
97
98   ResourceType resource_type_;  // Differentiate main script and imports
99   scoped_refptr<net::IOBuffer> io_buffer_;
100   scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_;
101   base::WeakPtr<ServiceWorkerContextCore> context_;
102   GURL url_;
103   int64 response_id_;
104   scoped_ptr<net::URLRequest> net_request_;
105   scoped_ptr<net::HttpResponseInfo> http_info_;
106   scoped_ptr<ServiceWorkerResponseWriter> writer_;
107   scoped_refptr<ServiceWorkerVersion> version_;
108   bool has_been_killed_;
109   bool did_notify_started_;
110   bool did_notify_finished_;
111   base::WeakPtrFactory<ServiceWorkerWriteToCacheJob> weak_factory_;
112
113   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerWriteToCacheJob);
114 };
115
116 }  // namespace content
117
118 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_WRITE_TO_CACHE_JOB_H_