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