Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / content / browser / loader / throttling_resource_handler.h
1 // Copyright (c) 2012 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_THROTTLING_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_LOADER_THROTTLING_RESOURCE_HANDLER_H_
7
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_vector.h"
10 #include "content/browser/loader/layered_resource_handler.h"
11 #include "content/public/browser/resource_controller.h"
12 #include "url/gurl.h"
13
14 namespace net {
15 class URLRequest;
16 }
17
18 namespace content {
19
20 class ResourceThrottle;
21 struct ResourceResponse;
22
23 // Used to apply a list of ResourceThrottle instances to an URLRequest.
24 class ThrottlingResourceHandler : public LayeredResourceHandler,
25                                   public ResourceController {
26  public:
27   // Takes ownership of the ResourceThrottle instances.
28   ThrottlingResourceHandler(scoped_ptr<ResourceHandler> next_handler,
29                             net::URLRequest* request,
30                             ScopedVector<ResourceThrottle> throttles);
31   virtual ~ThrottlingResourceHandler();
32
33   // LayeredResourceHandler overrides:
34   virtual bool OnRequestRedirected(int request_id, const GURL& url,
35                                    ResourceResponse* response,
36                                    bool* defer) OVERRIDE;
37   virtual bool OnResponseStarted(int request_id,
38                                  ResourceResponse* response,
39                                  bool* defer) OVERRIDE;
40   virtual bool OnWillStart(int request_id, const GURL& url,
41                            bool* defer) OVERRIDE;
42   virtual bool OnBeforeNetworkStart(int request_id,
43                                   const GURL& url,
44                                   bool* defer) OVERRIDE;
45
46   // ResourceController implementation:
47   virtual void Cancel() OVERRIDE;
48   virtual void CancelAndIgnore() OVERRIDE;
49   virtual void CancelWithError(int error_code) OVERRIDE;
50   virtual void Resume() OVERRIDE;
51
52  private:
53   void ResumeStart();
54   void ResumeNetworkStart();
55   void ResumeRedirect();
56   void ResumeResponse();
57
58   // Called when the throttle at |throttle_index| defers a request.  Logs the
59   // name of the throttle that delayed the request.
60   void OnRequestDefered(int throttle_index);
61
62   enum DeferredStage {
63     DEFERRED_NONE,
64     DEFERRED_START,
65     DEFERRED_NETWORK_START,
66     DEFERRED_REDIRECT,
67     DEFERRED_RESPONSE
68   };
69   DeferredStage deferred_stage_;
70
71   ScopedVector<ResourceThrottle> throttles_;
72   size_t next_index_;
73
74   GURL deferred_url_;
75   scoped_refptr<ResourceResponse> deferred_response_;
76
77   bool cancelled_by_resource_throttle_;
78 };
79
80 }  // namespace content
81
82 #endif  // CONTENT_BROWSER_LOADER_THROTTLING_RESOURCE_HANDLER_H_