Home | History | Annotate | Download | only in service_worker
      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   virtual ~ServiceWorkerWriteToCacheJob();
     53 
     54   // net::URLRequestJob overrides
     55   virtual void Start() OVERRIDE;
     56   virtual void Kill() OVERRIDE;
     57   virtual net::LoadState GetLoadState() const OVERRIDE;
     58   virtual bool GetCharset(std::string* charset) OVERRIDE;
     59   virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
     60   virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;
     61   virtual int GetResponseCode() const OVERRIDE;
     62   virtual void SetExtraRequestHeaders(
     63       const net::HttpRequestHeaders& headers) OVERRIDE;
     64   virtual bool ReadRawData(net::IOBuffer* buf,
     65                            int buf_size,
     66                            int *bytes_read) OVERRIDE;
     67 
     68   const net::HttpResponseInfo* http_info() const;
     69 
     70   // Methods to drive the net request forward and
     71   // write data to the disk cache.
     72   void InitNetRequest(int extra_load_flags);
     73   void StartNetRequest();
     74   net::URLRequestStatus ReadNetData(
     75       net::IOBuffer* buf,
     76       int buf_size,
     77       int *bytes_read);
     78   void WriteHeadersToCache();
     79   void OnWriteHeadersComplete(int result);
     80   void WriteDataToCache(int bytes_to_write);
     81   void OnWriteDataComplete(int result);
     82 
     83   // net::URLRequest::Delegate overrides that observe the net request.
     84   virtual void OnReceivedRedirect(
     85       net::URLRequest* request,
     86       const net::RedirectInfo& redirect_info,
     87       bool* defer_redirect) OVERRIDE;
     88   virtual void OnAuthRequired(
     89       net::URLRequest* request,
     90       net::AuthChallengeInfo* auth_info) OVERRIDE;
     91   virtual void OnCertificateRequested(
     92       net::URLRequest* request,
     93       net::SSLCertRequestInfo* cert_request_info) OVERRIDE;
     94   virtual void OnSSLCertificateError(
     95       net::URLRequest* request,
     96       const net::SSLInfo& ssl_info,
     97       bool fatal) OVERRIDE;
     98   virtual void OnBeforeNetworkStart(
     99       net::URLRequest* request,
    100       bool* defer) OVERRIDE;
    101   virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
    102   virtual void OnReadCompleted(
    103       net::URLRequest* request,
    104       int bytes_read) OVERRIDE;
    105 
    106   void AsyncNotifyDoneHelper(const net::URLRequestStatus& status);
    107 
    108   ResourceType resource_type_;  // Differentiate main script and imports
    109   scoped_refptr<net::IOBuffer> io_buffer_;
    110   scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_;
    111   base::WeakPtr<ServiceWorkerContextCore> context_;
    112   GURL url_;
    113   int64 response_id_;
    114   scoped_ptr<net::URLRequest> net_request_;
    115   scoped_ptr<net::HttpResponseInfo> http_info_;
    116   scoped_ptr<ServiceWorkerResponseWriter> writer_;
    117   scoped_refptr<ServiceWorkerVersion> version_;
    118   bool has_been_killed_;
    119   bool did_notify_started_;
    120   bool did_notify_finished_;
    121   base::WeakPtrFactory<ServiceWorkerWriteToCacheJob> weak_factory_;
    122 
    123   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerWriteToCacheJob);
    124 };
    125 
    126 }  // namespace content
    127 
    128 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_WRITE_TO_CACHE_JOB_H_
    129