Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / loader / detachable_resource_handler.h
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/elapsed_timer.h"
14 #include "base/timer/timer.h"
15 #include "content/browser/loader/resource_handler.h"
16 #include "content/public/browser/resource_controller.h"
17 #include "net/url_request/url_request_status.h"
18
19 namespace net {
20 class IOBuffer;
21 class URLRequest;
22 }  // namespace net
23
24 namespace content {
25
26 // A ResourceHandler which delegates all calls to the next handler, unless
27 // detached. Once detached, it drives the request to completion itself. This is
28 // used for requests which outlive the owning renderer, such as <link
29 // rel=prefetch> and <a ping>. Requests do not start out detached so, e.g.,
30 // prefetches appear in DevTools and get placed in the renderer's local
31 // cache. If the request does not complete after a timeout on detach, it is
32 // cancelled.
33 //
34 // Note that, once detached, the request continues without the original next
35 // handler, so any policy decisions in that handler are skipped.
36 class DetachableResourceHandler : public ResourceHandler,
37                                   public ResourceController {
38  public:
39   DetachableResourceHandler(net::URLRequest* request,
40                             base::TimeDelta cancel_delay,
41                             scoped_ptr<ResourceHandler> next_handler);
42   virtual ~DetachableResourceHandler();
43
44   bool is_detached() const { return next_handler_ == NULL; }
45   void Detach();
46
47   void set_cancel_delay(base::TimeDelta cancel_delay) {
48     cancel_delay_ = cancel_delay;
49   }
50
51   // ResourceHandler implementation:
52   virtual void SetController(ResourceController* controller) OVERRIDE;
53   virtual bool OnUploadProgress(int request_id, uint64 position,
54                                 uint64 size) OVERRIDE;
55   virtual bool OnRequestRedirected(int request_id, const GURL& url,
56                                    ResourceResponse* response,
57                                    bool* defer) OVERRIDE;
58   virtual bool OnResponseStarted(int request_id,
59                                  ResourceResponse* response,
60                                  bool* defer) OVERRIDE;
61   virtual bool OnWillStart(int request_id, const GURL& url,
62                            bool* defer) OVERRIDE;
63   virtual bool OnBeforeNetworkStart(int request_id,
64                                     const GURL& url,
65                                     bool* defer) OVERRIDE;
66   virtual bool OnWillRead(int request_id,
67                           scoped_refptr<net::IOBuffer>* buf,
68                           int* buf_size,
69                           int min_size) OVERRIDE;
70   virtual bool OnReadCompleted(int request_id, int bytes_read,
71                                bool* defer) OVERRIDE;
72   virtual void OnResponseCompleted(int request_id,
73                                    const net::URLRequestStatus& status,
74                                    const std::string& security_info,
75                                    bool* defer) OVERRIDE;
76   virtual void OnDataDownloaded(int request_id, int bytes_downloaded) OVERRIDE;
77
78   // ResourceController implementation:
79   virtual void Resume() OVERRIDE;
80   virtual void Cancel() OVERRIDE;
81   virtual void CancelAndIgnore() OVERRIDE;
82   virtual void CancelWithError(int error_code) OVERRIDE;
83
84  private:
85   void TimedOut();
86
87   scoped_ptr<ResourceHandler> next_handler_;
88   scoped_refptr<net::IOBuffer> read_buffer_;
89
90   scoped_ptr<base::OneShotTimer<DetachableResourceHandler> > detached_timer_;
91   base::TimeDelta cancel_delay_;
92
93   bool is_deferred_;
94   bool is_finished_;
95   bool timed_out_;
96
97   bool response_started_;
98   base::ElapsedTimer time_since_start_;
99
100   // The status recorded from OnResponseCompleted. Value is
101   // net::URLRequestStatus::IO_PENDING if OnResponseCompleted is never received.
102   net::URLRequestStatus::Status status_;
103
104   DISALLOW_COPY_AND_ASSIGN(DetachableResourceHandler);
105 };
106
107 }  // namespace content
108
109 #endif  // CONTENT_BROWSER_LOADER_DETACHABLE_RESOURCE_HANDLER_H_