summaryrefslogtreecommitdiff
path: root/chromium/content/public/browser/resource_throttle.h
blob: 4ac8fd4d6633197eaf37c740470090a1150630be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_PUBLIC_BROWSER_RESOURCE_THROTTLE_H_
#define CONTENT_PUBLIC_BROWSER_RESOURCE_THROTTLE_H_

#include "content/common/content_export.h"

namespace net {
struct RedirectInfo;
}

namespace content {

class AsyncRevalidationDriver;
class ResourceController;
class ThrottlingResourceHandler;

// A ResourceThrottle gets notified at various points during the process of
// loading a resource.  At each stage, it has the opportunity to defer the
// resource load.  The ResourceController interface may be used to resume a
// deferred resource load, or it may be used to cancel a resource load at any
// time.
class CONTENT_EXPORT ResourceThrottle {
 public:
  // An interface to get notified when the throttle implementation considers
  // it's ready to resume the deferred resource load. The throttle
  // implementation may also tell the delegate to cancel the resource loading.
  class CONTENT_EXPORT Delegate {
   public:
    // Cancels the resource load.
    virtual void Cancel() = 0;
    // Marks the resource load as ignored by the resource handler, and then
    // cancels the resource load.
    virtual void CancelAndIgnore() = 0;
    // Cancels the resource load with the specified error code.
    virtual void CancelWithError(int error_code) = 0;
    // Tells the delegate to resume the deferred resource load.
    virtual void Resume() = 0;

   protected:
    virtual ~Delegate() {}
  };

  virtual ~ResourceThrottle() {}

  // Called before the resource request is started.
  virtual void WillStartRequest(bool* defer) {}

  // Called when the request was redirected.  |redirect_info| contains the
  // redirect responses's HTTP status code and some information about the new
  // request that will be sent if the redirect is followed, including the new
  // URL and new method.
  virtual void WillRedirectRequest(const net::RedirectInfo& redirect_info,
                                   bool* defer) {}

  // Called when the response headers and meta data are available.
  virtual void WillProcessResponse(bool* defer) {}

  // Returns the name of the throttle, as a UTF-8 C-string, for logging
  // purposes.  nullptr is not allowed.  Caller does *not* take ownership of the
  // returned string.
  virtual const char* GetNameForLogging() const = 0;

  // Whether this ResourceThrottle needs to execute WillProcessResponse before
  // any part of the response body is read. Normally this is false. This should
  // be set to true if the ResourceThrottle wants to ensure that no part of the
  // response body will be cached if the request is canceled in
  // WillProcessResponse.
  virtual bool MustProcessResponseBeforeReadingBody();

  void set_delegate_for_testing(Delegate* delegate) { delegate_ = delegate; }

 protected:
  ResourceThrottle() : delegate_(nullptr) {}

  // Helper methods for subclasses. When these methods are called, methods with
  // the same name on |delegate_| are called.
  void Cancel();
  void CancelAndIgnore();
  void CancelWithError(int error_code);
  void Resume();

 private:
  friend class AsyncRevalidationDriver;
  friend class ThrottlingResourceHandler;

  void set_delegate(Delegate* delegate) { delegate_ = delegate; }

  Delegate* delegate_;
};

}  // namespace content

#endif  // CONTENT_PUBLIC_BROWSER_RESOURCE_THROTTLE_H_