OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef IOS_NET_REQUEST_TRACKER_H_ |
| 6 #define IOS_NET_REQUEST_TRACKER_H_ |
| 7 |
| 8 #import <Foundation/Foundation.h> |
| 9 |
| 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" |
| 14 |
| 15 namespace net { |
| 16 class SSLInfo; |
| 17 class URLRequest; |
| 18 class URLRequestContext; |
| 19 } |
| 20 |
| 21 @class CRNForwardingNetworkClientFactory; |
| 22 @class CRNSimpleNetworkClientFactory; |
| 23 class GURL; |
| 24 |
| 25 namespace net { |
| 26 |
| 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, |
| 41 }; |
| 42 |
| 43 typedef base::Callback<void(bool)> SSLCallback; |
| 44 |
| 45 class RequestTrackerFactory { |
| 46 public: |
| 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; |
| 53 }; |
| 54 |
| 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); |
| 59 |
| 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); |
| 67 |
| 68 RequestTracker(); |
| 69 |
| 70 base::WeakPtr<RequestTracker> GetWeakPtr(); |
| 71 |
| 72 // This function has to be called before using the tracker. |
| 73 virtual void Init(); |
| 74 |
| 75 // Add a factory that may create network clients for requests going through |
| 76 // this tracker. |
| 77 void AddNetworkClientFactory(CRNForwardingNetworkClientFactory* factory); |
| 78 |
| 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); |
| 86 |
| 87 // Gets the request context associated with the tracker. |
| 88 virtual URLRequestContext* GetRequestContext() = 0; |
| 89 |
| 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 |
| 94 // in the array. |
| 95 |
| 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(); |
| 99 |
| 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, |
| 110 const GURL& new_url, |
| 111 NSURLResponse* redirect_response); |
| 112 |
| 113 // Informs the tracker that a request has started. |
| 114 virtual void StartRequest(URLRequest* request) = 0; |
| 115 |
| 116 // Informs the tracker that the headers for the request are available. |
| 117 virtual void CaptureHeaders(URLRequest* request) = 0; |
| 118 |
| 119 // Informs the tracker the expected length of the result, if known. |
| 120 virtual void CaptureExpectedLength(const URLRequest* request, |
| 121 uint64_t length) = 0; |
| 122 |
| 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; |
| 126 |
| 127 // Informs the tracker that a certificate has been used. |
| 128 virtual void CaptureCertificatePolicyCache( |
| 129 const URLRequest* request, |
| 130 const SSLCallback& should_continue) = 0; |
| 131 |
| 132 // Notifies of the completion of a request. Success or failure. |
| 133 virtual void StopRequest(URLRequest* request) = 0; |
| 134 |
| 135 // Special case for a redirect as we fully expect another request to follow |
| 136 // very shortly. |
| 137 virtual void StopRedirectedRequest(URLRequest* request) = 0; |
| 138 |
| 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 |
| 143 // noop. |
| 144 virtual void OnSSLCertificateError(const URLRequest* request, |
| 145 const SSLInfo& ssl_info, |
| 146 bool recoverable, |
| 147 const SSLCallback& should_continue) = 0; |
| 148 |
| 149 // Gets and sets the cache mode. |
| 150 CacheMode GetCacheMode() const; |
| 151 void SetCacheMode(RequestTracker::CacheMode mode); |
| 152 |
| 153 protected: |
| 154 virtual ~RequestTracker(); |
| 155 |
| 156 void InvalidateWeakPtrs(); |
| 157 |
| 158 private: |
| 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_; |
| 162 |
| 163 bool initialized_; |
| 164 CacheMode cache_mode_; |
| 165 base::ThreadChecker thread_checker_; |
| 166 |
| 167 base::WeakPtrFactory<RequestTracker> weak_ptr_factory_; |
| 168 }; |
| 169 |
| 170 } // namespace net |
| 171 |
| 172 #endif // IOS_NET_REQUEST_TRACKER_H_ |
OLD | NEW |