Home | History | Annotate | Download | only in loader
      1 // Copyright 2013 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_LOADER_DETACHABLE_RESOURCE_HANDLER_H_
      6 #define CONTENT_BROWSER_LOADER_DETACHABLE_RESOURCE_HANDLER_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/compiler_specific.h"
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/time/time.h"
     13 #include "base/timer/timer.h"
     14 #include "content/browser/loader/resource_handler.h"
     15 #include "content/public/browser/resource_controller.h"
     16 
     17 namespace net {
     18 class IOBuffer;
     19 class URLRequest;
     20 }  // namespace net
     21 
     22 namespace content {
     23 
     24 // A ResourceHandler which delegates all calls to the next handler, unless
     25 // detached. Once detached, it drives the request to completion itself. This is
     26 // used for requests which outlive the owning renderer, such as <link
     27 // rel=prefetch> and <a ping>. Requests do not start out detached so, e.g.,
     28 // prefetches appear in DevTools and get placed in the renderer's local
     29 // cache. If the request does not complete after a timeout on detach, it is
     30 // cancelled.
     31 //
     32 // Note that, once detached, the request continues without the original next
     33 // handler, so any policy decisions in that handler are skipped.
     34 class DetachableResourceHandler : public ResourceHandler,
     35                                   public ResourceController {
     36  public:
     37   DetachableResourceHandler(net::URLRequest* request,
     38                             base::TimeDelta cancel_delay,
     39                             scoped_ptr<ResourceHandler> next_handler);
     40   virtual ~DetachableResourceHandler();
     41 
     42   bool is_detached() const { return next_handler_ == NULL; }
     43   void Detach();
     44 
     45   void set_cancel_delay(base::TimeDelta cancel_delay) {
     46     cancel_delay_ = cancel_delay;
     47   }
     48 
     49   // ResourceHandler implementation:
     50   virtual void SetController(ResourceController* controller) OVERRIDE;
     51   virtual bool OnUploadProgress(int request_id, uint64 position,
     52                                 uint64 size) OVERRIDE;
     53   virtual bool OnRequestRedirected(int request_id, const GURL& url,
     54                                    ResourceResponse* response,
     55                                    bool* defer) OVERRIDE;
     56   virtual bool OnResponseStarted(int request_id,
     57                                  ResourceResponse* response,
     58                                  bool* defer) OVERRIDE;
     59   virtual bool OnWillStart(int request_id, const GURL& url,
     60                            bool* defer) OVERRIDE;
     61   virtual bool OnWillRead(int request_id,
     62                           scoped_refptr<net::IOBuffer>* buf,
     63                           int* buf_size,
     64                           int min_size) OVERRIDE;
     65   virtual bool OnReadCompleted(int request_id, int bytes_read,
     66                                bool* defer) OVERRIDE;
     67   virtual void OnResponseCompleted(int request_id,
     68                                    const net::URLRequestStatus& status,
     69                                    const std::string& security_info,
     70                                    bool* defer) OVERRIDE;
     71   virtual void OnDataDownloaded(int request_id, int bytes_downloaded) OVERRIDE;
     72 
     73   // ResourceController implementation:
     74   virtual void Resume() OVERRIDE;
     75   virtual void Cancel() OVERRIDE;
     76   virtual void CancelAndIgnore() OVERRIDE;
     77   virtual void CancelWithError(int error_code) OVERRIDE;
     78 
     79  private:
     80   scoped_ptr<ResourceHandler> next_handler_;
     81   scoped_refptr<net::IOBuffer> read_buffer_;
     82 
     83   scoped_ptr<base::OneShotTimer<DetachableResourceHandler> > detached_timer_;
     84   base::TimeDelta cancel_delay_;
     85 
     86   bool is_deferred_;
     87   bool is_finished_;
     88 
     89   DISALLOW_COPY_AND_ASSIGN(DetachableResourceHandler);
     90 };
     91 
     92 }  // namespace content
     93 
     94 #endif  // CONTENT_BROWSER_LOADER_DETACHABLE_RESOURCE_HANDLER_H_
     95