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