Update V8 to version 4.6.6.
[chromium-blink-merge.git] / ios / net / request_tracker.h
blobf21cb887c7c12686258cbbefb145a5311104d6d7
1 // Copyright 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.
5 #ifndef IOS_NET_REQUEST_TRACKER_H_
6 #define IOS_NET_REQUEST_TRACKER_H_
8 #import <Foundation/Foundation.h>
10 #include "base/callback_forward.h"
11 #include "base/mac/scoped_nsobject.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/thread_checker.h"
15 namespace net {
16 class SSLInfo;
17 class URLRequest;
18 class URLRequestContext;
21 @class CRNForwardingNetworkClientFactory;
22 @class CRNSimpleNetworkClientFactory;
23 class GURL;
25 namespace net {
27 // RequestTracker can be used to observe the network requests and customize the
28 // behavior of the network stack with CRNForwardingNetworkClients.
29 // Each network request can be associated with a RequestTracker through the
30 // GetRequestTracker().
31 // RequestTracker requires a RequestTrackerFactory.
32 // The RequestTracker can be created on one thread and used on a different one.
33 class RequestTracker {
34 public:
35 enum CacheMode {
36 CACHE_NORMAL,
37 CACHE_RELOAD,
38 CACHE_HISTORY,
39 CACHE_BYPASS,
40 CACHE_ONLY,
43 typedef base::Callback<void(bool)> SSLCallback;
45 class RequestTrackerFactory {
46 public:
47 virtual ~RequestTrackerFactory();
49 // Returns false if |request| is associated to an invalid tracker and should
50 // be cancelled. In this case |tracker| is set to nullptr.
51 // Returns true if |request| is associated with a valid tracker or if the
52 // request is not associated to any tracker.
53 virtual bool GetRequestTracker(NSURLRequest* request,
54 base::WeakPtr<RequestTracker>* tracker) = 0;
57 // Sets the RequestTrackerFactory. The factory has to be set before the
58 // GetRequestTracker() function can be called.
59 // Does not take ownership of |factory|.
60 static void SetRequestTrackerFactory(RequestTrackerFactory* factory);
62 // Returns false if |request| is associated to an invalid tracker and should
63 // be cancelled. In this case |tracker| is set to nullptr.
64 // Returns true if |request| is associated with a valid tracker or if the
65 // request is not associated to any tracker.
66 // Internally calls the RequestTrackerFactory.
67 static bool GetRequestTracker(NSURLRequest* request,
68 base::WeakPtr<RequestTracker>* tracker);
70 RequestTracker();
72 base::WeakPtr<RequestTracker> GetWeakPtr();
74 // This function has to be called before using the tracker.
75 virtual void Init();
77 // Add a factory that may create network clients for requests going through
78 // this tracker.
79 void AddNetworkClientFactory(CRNForwardingNetworkClientFactory* factory);
81 // Registers a factory with the class that will be added to all trackers.
82 // Requests without associated trackers can add clients from these factories
83 // using GlobalClientsHandlingAnyRequest().
84 // Only |-clientHandlingAnyRequest| will be called on |factory|, the other
85 // methods are not supported.
86 static void AddGlobalNetworkClientFactory(
87 CRNForwardingNetworkClientFactory* factory);
89 // Gets the request context associated with the tracker.
90 virtual URLRequestContext* GetRequestContext() = 0;
92 // Network client generation methods. All of these four ClientsHandling...
93 // methods return an array of CRNForwardingNetworkClient instances, according
94 // to the CRNForwardingNetworkClientFactories added to the tracker. The array
95 // may be empty. The caller is responsible for taking ownership of the clients
96 // in the array.
98 // Static method that returns clients that can handle any request, for use
99 // in cases where a request isn't associated with any request_tracker.
100 static NSArray* GlobalClientsHandlingAnyRequest();
102 // Returns clients that can handle any request.
103 NSArray* ClientsHandlingAnyRequest();
104 // Returns clients that can handle |request|.
105 NSArray* ClientsHandlingRequest(const URLRequest& request);
106 // Returns clients that can handle |request| with |response|.
107 NSArray* ClientsHandlingRequestAndResponse(const URLRequest& request,
108 NSURLResponse* response);
109 // Returns clients that can handle a redirect of |request| to |new_url| based
110 // on |redirect_response|.
111 NSArray* ClientsHandlingRedirect(const URLRequest& request,
112 const GURL& new_url,
113 NSURLResponse* redirect_response);
115 // Informs the tracker that a request has started.
116 virtual void StartRequest(URLRequest* request) = 0;
118 // Informs the tracker that the headers for the request are available.
119 virtual void CaptureHeaders(URLRequest* request) = 0;
121 // Informs the tracker the expected length of the result, if known.
122 virtual void CaptureExpectedLength(const URLRequest* request,
123 uint64_t length) = 0;
125 // Informs the tracker that a request received par_trackert of its data.
126 virtual void CaptureReceivedBytes(const URLRequest* request,
127 uint64_t byte_count) = 0;
129 // Informs the tracker that a certificate has been used.
130 virtual void CaptureCertificatePolicyCache(
131 const URLRequest* request,
132 const SSLCallback& should_continue) = 0;
134 // Notifies of the completion of a request. Success or failure.
135 virtual void StopRequest(URLRequest* request) = 0;
137 // Special case for a redirect as we fully expect another request to follow
138 // very shortly.
139 virtual void StopRedirectedRequest(URLRequest* request) = 0;
141 // Called when there is an issue on the SSL certificate. The user must be
142 // informed and if |recoverable| is YES the user decision to continue or not
143 // will be send back via the |callback|. The callback must be safe to call
144 // from any thread. If recoverable is NO, invoking the callback should be a
145 // noop.
146 virtual void OnSSLCertificateError(const URLRequest* request,
147 const SSLInfo& ssl_info,
148 bool recoverable,
149 const SSLCallback& should_continue) = 0;
151 // Gets and sets the cache mode.
152 CacheMode GetCacheMode() const;
153 void SetCacheMode(RequestTracker::CacheMode mode);
155 protected:
156 virtual ~RequestTracker();
158 void InvalidateWeakPtrs();
160 private:
161 // Array of client factories that may be added by CRNHTTPProtocolHandler. The
162 // array lives on the IO thread.
163 base::scoped_nsobject<NSMutableArray> client_factories_;
165 bool initialized_;
166 CacheMode cache_mode_;
167 base::ThreadChecker thread_checker_;
169 base::WeakPtrFactory<RequestTracker> weak_ptr_factory_;
172 } // namespace net
174 #endif // IOS_NET_REQUEST_TRACKER_H_