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