Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_url_request_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_URL_REQUEST_JOB_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_URL_REQUEST_JOB_H_
7
8 #include "base/memory/weak_ptr.h"
9 #include "content/common/content_export.h"
10 #include "content/common/service_worker/service_worker_status_code.h"
11 #include "content/common/service_worker/service_worker_types.h"
12 #include "net/http/http_byte_range.h"
13 #include "net/url_request/url_request_job.h"
14
15 namespace content {
16
17 class ServiceWorkerContextCore;
18 class ServiceWorkerFetchDispatcher;
19 class ServiceWorkerProviderHost;
20
21 class CONTENT_EXPORT ServiceWorkerURLRequestJob
22     : public net::URLRequestJob {
23  public:
24   ServiceWorkerURLRequestJob(
25       net::URLRequest* request,
26       net::NetworkDelegate* network_delegate,
27       base::WeakPtr<ServiceWorkerProviderHost> provider_host);
28
29   // Sets the response type.
30   void FallbackToNetwork();
31   void ForwardToServiceWorker();
32
33   bool ShouldFallbackToNetwork() const {
34     return response_type_ == FALLBACK_TO_NETWORK;
35   }
36   bool ShouldForwardToServiceWorker() const {
37     return response_type_ == FORWARD_TO_SERVICE_WORKER;
38   }
39
40   // net::URLRequestJob overrides:
41   virtual void Start() OVERRIDE;
42   virtual void Kill() OVERRIDE;
43   virtual net::LoadState GetLoadState() const OVERRIDE;
44   virtual bool GetCharset(std::string* charset) OVERRIDE;
45   virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
46   virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;
47   virtual int GetResponseCode() const OVERRIDE;
48   virtual void SetExtraRequestHeaders(
49       const net::HttpRequestHeaders& headers) OVERRIDE;
50   virtual bool ReadRawData(net::IOBuffer* buf,
51                            int buf_size,
52                            int *bytes_read) OVERRIDE;
53
54   const net::HttpResponseInfo* http_info() const;
55
56  protected:
57   virtual ~ServiceWorkerURLRequestJob();
58
59  private:
60   enum ResponseType {
61     NOT_DETERMINED,
62     FALLBACK_TO_NETWORK,
63     FORWARD_TO_SERVICE_WORKER,
64   };
65
66   // We start processing the request if Start() is called AND response_type_
67   // is determined.
68   void MaybeStartRequest();
69   void StartRequest();
70
71   // For FORWARD_TO_SERVICE_WORKER case.
72   void DidDispatchFetchEvent(ServiceWorkerStatusCode status,
73                              ServiceWorkerFetchEventResult fetch_result,
74                              const ServiceWorkerResponse& response);
75
76   void CreateResponseHeader(const ServiceWorkerResponse& response);
77
78   base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
79
80   ResponseType response_type_;
81   bool is_started_;
82
83   net::HttpByteRange byte_range_;
84   scoped_ptr<net::HttpResponseInfo> range_response_info_;
85   scoped_ptr<net::HttpResponseInfo> http_response_info_;
86
87   // Used when response type is FORWARD_TO_SERVICE_WORKER.
88   scoped_ptr<ServiceWorkerFetchDispatcher> fetch_dispatcher_;
89
90   base::WeakPtrFactory<ServiceWorkerURLRequestJob> weak_factory_;
91
92   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJob);
93 };
94
95 }  // namespace content
96
97 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_URL_REQUEST_JOB_H_