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"
18 class URLRequestContext
;
21 @
class CRNForwardingNetworkClientFactory
;
22 @
class CRNSimpleNetworkClientFactory
;
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
{
43 typedef base::Callback
<void(bool)> SSLCallback
;
45 class RequestTrackerFactory
{
47 // Returns false if |request| is associated to an invalid tracker and should
48 // be cancelled. In this case |tracker| is set to nullptr.
49 // Returns true if |request| is associated with a valid tracker or if the
50 // request is not associated to any tracker.
51 virtual bool GetRequestTracker(NSURLRequest
* request
,
52 base::WeakPtr
<RequestTracker
>* tracker
) = 0;
55 // Sets the RequestTrackerFactory. The factory has to be set before the
56 // GetRequestTracker() function can be called.
57 // Does not take ownership of |factory|.
58 static void SetRequestTrackerFactory(RequestTrackerFactory
* factory
);
60 // Returns false if |request| is associated to an invalid tracker and should
61 // be cancelled. In this case |tracker| is set to nullptr.
62 // Returns true if |request| is associated with a valid tracker or if the
63 // request is not associated to any tracker.
64 // Internally calls the RequestTrackerFactory.
65 static bool GetRequestTracker(NSURLRequest
* request
,
66 base::WeakPtr
<RequestTracker
>* tracker
);
70 base::WeakPtr
<RequestTracker
> GetWeakPtr();
72 // This function has to be called before using the tracker.
75 // Add a factory that may create network clients for requests going through
77 void AddNetworkClientFactory(CRNForwardingNetworkClientFactory
* factory
);
79 // Registers a factory with the class that will be added to all trackers.
80 // Requests without associated trackers can add clients from these factories
81 // using GlobalClientsHandlingAnyRequest().
82 // Only |-clientHandlingAnyRequest| will be called on |factory|, the other
83 // methods are not supported.
84 static void AddGlobalNetworkClientFactory(
85 CRNForwardingNetworkClientFactory
* factory
);
87 // Gets the request context associated with the tracker.
88 virtual URLRequestContext
* GetRequestContext() = 0;
90 // Network client generation methods. All of these four ClientsHandling...
91 // methods return an array of CRNForwardingNetworkClient instances, according
92 // to the CRNForwardingNetworkClientFactories added to the tracker. The array
93 // may be empty. The caller is responsible for taking ownership of the clients
96 // Static method that returns clients that can handle any request, for use
97 // in cases where a request isn't associated with any request_tracker.
98 static NSArray
* GlobalClientsHandlingAnyRequest();
100 // Returns clients that can handle any request.
101 NSArray
* ClientsHandlingAnyRequest();
102 // Returns clients that can handle |request|.
103 NSArray
* ClientsHandlingRequest(const URLRequest
& request
);
104 // Returns clients that can handle |request| with |response|.
105 NSArray
* ClientsHandlingRequestAndResponse(const URLRequest
& request
,
106 NSURLResponse
* response
);
107 // Returns clients that can handle a redirect of |request| to |new_url| based
108 // on |redirect_response|.
109 NSArray
* ClientsHandlingRedirect(const URLRequest
& request
,
111 NSURLResponse
* redirect_response
);
113 // Informs the tracker that a request has started.
114 virtual void StartRequest(URLRequest
* request
) = 0;
116 // Informs the tracker that the headers for the request are available.
117 virtual void CaptureHeaders(URLRequest
* request
) = 0;
119 // Informs the tracker the expected length of the result, if known.
120 virtual void CaptureExpectedLength(const URLRequest
* request
,
121 uint64_t length
) = 0;
123 // Informs the tracker that a request received par_trackert of its data.
124 virtual void CaptureReceivedBytes(const URLRequest
* request
,
125 uint64_t byte_count
) = 0;
127 // Informs the tracker that a certificate has been used.
128 virtual void CaptureCertificatePolicyCache(
129 const URLRequest
* request
,
130 const SSLCallback
& should_continue
) = 0;
132 // Notifies of the completion of a request. Success or failure.
133 virtual void StopRequest(URLRequest
* request
) = 0;
135 // Special case for a redirect as we fully expect another request to follow
137 virtual void StopRedirectedRequest(URLRequest
* request
) = 0;
139 // Called when there is an issue on the SSL certificate. The user must be
140 // informed and if |recoverable| is YES the user decision to continue or not
141 // will be send back via the |callback|. The callback must be safe to call
142 // from any thread. If recoverable is NO, invoking the callback should be a
144 virtual void OnSSLCertificateError(const URLRequest
* request
,
145 const SSLInfo
& ssl_info
,
147 const SSLCallback
& should_continue
) = 0;
149 // Gets and sets the cache mode.
150 CacheMode
GetCacheMode() const;
151 void SetCacheMode(RequestTracker::CacheMode mode
);
154 virtual ~RequestTracker();
156 void InvalidateWeakPtrs();
159 // Array of client factories that may be added by CRNHTTPProtocolHandler. The
160 // array lives on the IO thread.
161 base::scoped_nsobject
<NSMutableArray
> client_factories_
;
164 CacheMode cache_mode_
;
165 base::ThreadChecker thread_checker_
;
167 base::WeakPtrFactory
<RequestTracker
> weak_ptr_factory_
;
172 #endif // IOS_NET_REQUEST_TRACKER_H_