url_request_job_manager.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
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
17template <typename T> struct DefaultSingletonTraits;
18
19namespace net {
20
21// This class is responsible for managing the set of protocol factories and
22// request interceptors that determine how an net::URLRequestJob gets created to
23// handle an net::URLRequest.
24//
25// MULTI-THREADING NOTICE:
26//   net::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//
32class URLRequestJobManager {
33 public:
34  // Returns the singleton instance.
35  static URLRequestJobManager* GetInstance();
36
37  // Instantiate an net::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  net::URLRequestJob* CreateJob(net::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  net::URLRequestJob* MaybeInterceptRedirect(net::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  net::URLRequestJob* MaybeInterceptResponse(net::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  net::URLRequest::ProtocolFactory* RegisterProtocolFactory(
62      const std::string& scheme, net::URLRequest::ProtocolFactory* factory);
63
64  // Register/unregister a request interceptor.
65  void RegisterRequestInterceptor(net::URLRequest::Interceptor* interceptor);
66  void UnregisterRequestInterceptor(net::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, net::URLRequest::ProtocolFactory*> FactoryMap;
73  typedef std::vector<net::URLRequest::Interceptor*> InterceptorList;
74  friend struct DefaultSingletonTraits<URLRequestJobManager>;
75
76  URLRequestJobManager();
77  ~URLRequestJobManager();
78
79  mutable Lock lock_;
80  FactoryMap factories_;
81  InterceptorList interceptors_;
82  bool enable_file_access_;
83
84#ifndef NDEBUG
85  // We use this to assert that CreateJob and the registration functions all
86  // run on the same thread.
87  mutable PlatformThreadId allowed_thread_;
88  mutable bool allowed_thread_initialized_;
89
90  // The first guy to call this function sets the allowed thread.  This way we
91  // avoid needing to define that thread externally.  Since we expect all
92  // callers to be on the same thread, we don't worry about threads racing to
93  // set the allowed thread.
94  bool IsAllowedThread() const {
95#if 0
96    if (!allowed_thread_initialized_) {
97      allowed_thread_ = PlatformThread::CurrentId();
98      allowed_thread_initialized_ = true;
99    }
100    return allowed_thread_ == PlatformThread::CurrentId();
101#else
102    // The previous version of this check used GetCurrentThread on Windows to
103    // get thread handles to compare. Unfortunately, GetCurrentThread returns
104    // a constant psuedo-handle (0xFFFFFFFE), and therefore IsAllowedThread
105    // always returned true. The above code that's turned off is the correct
106    // code, but causes the tree to turn red because some caller isn't
107    // respecting our thread requirements. We're turning off the check for now;
108    // bug http://b/issue?id=1338969 has been filed to fix things and turn the
109    // check back on.
110    return true;
111#endif
112  }
113#endif
114
115  DISALLOW_COPY_AND_ASSIGN(URLRequestJobManager);
116};
117
118}  // namespace net
119
120#endif  // NET_URL_REQUEST_URL_REQUEST_JOB_MANAGER_H_
121