1 // Copyright (c) 2011 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 NET_URL_REQUEST_URL_REQUEST_JOB_MANAGER_H_ 6 #define NET_URL_REQUEST_URL_REQUEST_JOB_MANAGER_H_ 7 #pragma once 8 9 #include <map> 10 #include <string> 11 #include <vector> 12 13 #include "base/synchronization/lock.h" 14 #include "base/threading/platform_thread.h" 15 #include "net/url_request/url_request.h" 16 17 template <typename T> struct DefaultSingletonTraits; 18 19 namespace net { 20 21 // This class is responsible for managing the set of protocol factories and 22 // request interceptors that determine how an URLRequestJob gets created to 23 // handle an URLRequest. 24 // 25 // MULTI-THREADING NOTICE: 26 // URLRequest is designed to have all consumers on a single thread, and 27 // so no attempt is made to support ProtocolFactory or Interceptor instances 28 // being registered/unregistered or in any way poked on multiple threads. 29 // However, we do support checking for supported schemes FROM ANY THREAD 30 // (i.e., it is safe to call SupportsScheme on any thread). 31 // 32 class URLRequestJobManager { 33 public: 34 // Returns the singleton instance. 35 static URLRequestJobManager* GetInstance(); 36 37 // Instantiate an URLRequestJob implementation based on the registered 38 // interceptors and protocol factories. This will always succeed in 39 // returning a job unless we are--in the extreme case--out of memory. 40 URLRequestJob* CreateJob(URLRequest* request) const; 41 42 // Allows interceptors to hijack the request after examining the new location 43 // of a redirect. Returns NULL if no interceptor intervenes. 44 URLRequestJob* MaybeInterceptRedirect(URLRequest* request, 45 const GURL& location) const; 46 47 // Allows interceptors to hijack the request after examining the response 48 // status and headers. This is also called when there is no server response 49 // at all to allow interception of failed requests due to network errors. 50 // Returns NULL if no interceptor intervenes. 51 URLRequestJob* MaybeInterceptResponse(URLRequest* request) const; 52 53 // Returns true if there is a protocol factory registered for the given 54 // scheme. Note: also returns true if there is a built-in handler for the 55 // given scheme. 56 bool SupportsScheme(const std::string& scheme) const; 57 58 // Register a protocol factory associated with the given scheme. The factory 59 // parameter may be null to clear any existing association. Returns the 60 // previously registered protocol factory if any. 61 URLRequest::ProtocolFactory* RegisterProtocolFactory( 62 const std::string& scheme, URLRequest::ProtocolFactory* factory); 63 64 // Register/unregister a request interceptor. 65 void RegisterRequestInterceptor(URLRequest::Interceptor* interceptor); 66 void UnregisterRequestInterceptor(URLRequest::Interceptor* interceptor); 67 68 void set_enable_file_access(bool enable) { enable_file_access_ = enable; } 69 bool enable_file_access() const { return enable_file_access_; } 70 71 private: 72 typedef std::map<std::string, URLRequest::ProtocolFactory*> FactoryMap; 73 typedef std::vector<URLRequest::Interceptor*> InterceptorList; 74 friend struct DefaultSingletonTraits<URLRequestJobManager>; 75 76 URLRequestJobManager(); 77 ~URLRequestJobManager(); 78 79 #ifndef NDEBUG 80 // The first guy to call this function sets the allowed thread. This way we 81 // avoid needing to define that thread externally. Since we expect all 82 // callers to be on the same thread, we don't worry about threads racing to 83 // set the allowed thread. 84 bool IsAllowedThread() const { 85 #if 0 86 if (!allowed_thread_initialized_) { 87 allowed_thread_ = base::PlatformThread::CurrentId(); 88 allowed_thread_initialized_ = true; 89 } 90 return allowed_thread_ == base::PlatformThread::CurrentId(); 91 #else 92 // The previous version of this check used GetCurrentThread on Windows to 93 // get thread handles to compare. Unfortunately, GetCurrentThread returns 94 // a constant psuedo-handle (0xFFFFFFFE), and therefore IsAllowedThread 95 // always returned true. The above code that's turned off is the correct 96 // code, but causes the tree to turn red because some caller isn't 97 // respecting our thread requirements. We're turning off the check for now; 98 // bug http://b/issue?id=1338969 has been filed to fix things and turn the 99 // check back on. 100 return true; 101 #endif 102 } 103 104 // We use this to assert that CreateJob and the registration functions all 105 // run on the same thread. 106 mutable base::PlatformThreadId allowed_thread_; 107 mutable bool allowed_thread_initialized_; 108 #endif 109 110 mutable base::Lock lock_; 111 FactoryMap factories_; 112 InterceptorList interceptors_; 113 bool enable_file_access_; 114 115 DISALLOW_COPY_AND_ASSIGN(URLRequestJobManager); 116 }; 117 118 } // namespace net 119 120 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_MANAGER_H_ 121