1 // Copyright 2011 The Chromium Authors 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 NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_H_ 6 #define NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_H_ 7 8 #include <map> 9 #include <memory> 10 #include <string> 11 12 #include "base/compiler_specific.h" 13 #include "base/threading/thread_checker.h" 14 #include "net/base/net_export.h" 15 16 class GURL; 17 18 namespace net { 19 20 class URLRequest; 21 class URLRequestInterceptor; 22 class URLRequestJob; 23 24 // Creates URLRequestJobs for URLRequests. Internally uses a mapping of schemes 25 // to ProtocolHandlers, which handle the actual requests. 26 class NET_EXPORT URLRequestJobFactory { 27 public: 28 class NET_EXPORT ProtocolHandler { 29 public: 30 virtual ~ProtocolHandler(); 31 32 // Creates a URLRequestJob for the particular protocol. Never returns 33 // nullptr. 34 virtual std::unique_ptr<URLRequestJob> CreateJob( 35 URLRequest* request) const = 0; 36 37 // Indicates if it should be safe to redirect to |location|. Should handle 38 // protocols handled by MaybeCreateJob(). 39 virtual bool IsSafeRedirectTarget(const GURL& location) const; 40 }; 41 42 URLRequestJobFactory(); 43 44 URLRequestJobFactory(const URLRequestJobFactory&) = delete; 45 URLRequestJobFactory& operator=(const URLRequestJobFactory&) = delete; 46 47 virtual ~URLRequestJobFactory(); 48 49 // Sets the ProtocolHandler for a scheme. Returns true on success, false on 50 // failure (a ProtocolHandler already exists for |scheme|). 51 bool SetProtocolHandler(const std::string& scheme, 52 std::unique_ptr<ProtocolHandler> protocol_handler); 53 54 // Creates a URLRequestJob for |request|. Returns a URLRequestJob that fails 55 // with net::Error code if unable to handle request->url(). 56 // 57 // Virtual for tests. 58 virtual std::unique_ptr<URLRequestJob> CreateJob(URLRequest* request) const; 59 60 // Returns true if it's safe to redirect to |location|. 61 // 62 // Virtual for tests. 63 virtual bool IsSafeRedirectTarget(const GURL& location) const; 64 65 protected: 66 // Protected for (test-only) subclasses. 67 THREAD_CHECKER(thread_checker_); 68 69 private: 70 // For testing only. 71 friend class URLRequestFilter; 72 73 using ProtocolHandlerMap = 74 std::map<std::string, std::unique_ptr<ProtocolHandler>>; 75 76 // Sets a global URLRequestInterceptor for testing purposes. The interceptor 77 // is given the chance to intercept any request before the corresponding 78 // ProtocolHandler. If an interceptor is set, the old interceptor must be 79 // cleared before setting a new one. 80 static void SetInterceptorForTesting(URLRequestInterceptor* interceptor); 81 82 ProtocolHandlerMap protocol_handler_map_; 83 }; 84 85 } // namespace net 86 87 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_H_ 88