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.
5 #ifndef NET_URL_REQUEST_URL_REQUEST_JOB_MANAGER_H_
6 #define NET_URL_REQUEST_URL_REQUEST_JOB_MANAGER_H_
10 #include "base/threading/thread_checker.h"
11 #include "net/base/net_export.h"
12 #include "net/url_request/url_request.h"
15 template <typename T
> struct DefaultSingletonTraits
;
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.
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 Interceptor instances being
27 // registered/unregistered or in any way poked on multiple threads.
28 class NET_EXPORT URLRequestJobManager
{
30 // Returns the singleton instance.
31 static URLRequestJobManager
* GetInstance();
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 URLRequestJob
* CreateJob(URLRequest
* request
,
37 NetworkDelegate
* network_delegate
) const;
39 // Allows interceptors to hijack the request after examining the new location
40 // of a redirect. Returns NULL if no interceptor intervenes.
41 URLRequestJob
* MaybeInterceptRedirect(URLRequest
* request
,
42 NetworkDelegate
* network_delegate
,
43 const GURL
& location
) const;
45 // Allows interceptors to hijack the request after examining the response
46 // status and headers. This is also called when there is no server response
47 // at all to allow interception of failed requests due to network errors.
48 // Returns NULL if no interceptor intervenes.
49 URLRequestJob
* MaybeInterceptResponse(
50 URLRequest
* request
, NetworkDelegate
* network_delegate
) const;
52 // Returns true if the manager has a built-in handler for |scheme|.
53 static bool SupportsScheme(const std::string
& scheme
);
56 friend struct base::DefaultSingletonTraits
<URLRequestJobManager
>;
58 URLRequestJobManager();
59 ~URLRequestJobManager();
61 // The first guy to call this function sets the allowed thread. This way we
62 // avoid needing to define that thread externally. Since we expect all
63 // callers to be on the same thread, we don't worry about threads racing to
64 // set the allowed thread.
65 bool IsAllowedThread() const {
67 return thread_checker_
.CalledOnValidThread();
70 // We use this to assert that CreateJob and the registration functions all
71 // run on the same thread.
72 base::ThreadChecker thread_checker_
;
74 // The previous version of this check used GetCurrentThread on Windows to
75 // get thread handles to compare. Unfortunately, GetCurrentThread returns
76 // a constant pseudo-handle (0xFFFFFFFE), and therefore IsAllowedThread
77 // always returned true. The above code that's turned off is the correct
78 // code, but causes the tree to turn red because some caller isn't
79 // respecting our thread requirements. We're turning off the check for now;
80 // bug http://b/issue?id=1338969 has been filed to fix things and turn the
86 DISALLOW_COPY_AND_ASSIGN(URLRequestJobManager
);
91 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_MANAGER_H_