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