1 // Copyright (c) 2011 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_FACTORY_H_
6 #define NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_H_
12 #include "base/basictypes.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "net/base/net_export.h"
23 class NET_EXPORT URLRequestJobFactory
24 : NON_EXPORTED_BASE(public base::NonThreadSafe
) {
26 class NET_EXPORT ProtocolHandler
{
28 virtual ~ProtocolHandler();
30 virtual URLRequestJob
* MaybeCreateJob(URLRequest
* request
) const = 0;
33 class NET_EXPORT Interceptor
{
35 virtual ~Interceptor();
37 // Called for every request made. Should return a new job to handle the
38 // request if it should be intercepted, or NULL to allow the request to
39 // be handled in the normal manner.
40 virtual URLRequestJob
* MaybeIntercept(URLRequest
* request
) const = 0;
42 // Called after having received a redirect response, but prior to the
43 // the request delegate being informed of the redirect. Can return a new
44 // job to replace the existing job if it should be intercepted, or NULL
45 // to allow the normal handling to continue. If a new job is provided,
46 // the delegate never sees the original redirect response, instead the
47 // response produced by the intercept job will be returned.
48 virtual URLRequestJob
* MaybeInterceptRedirect(
50 URLRequest
* request
) const = 0;
52 // Called after having received a final response, but prior to the
53 // the request delegate being informed of the response. This is also
54 // called when there is no server response at all to allow interception
55 // on DNS or network errors. Can return a new job to replace the existing
56 // job if it should be intercepted, or NULL to allow the normal handling to
57 // continue. If a new job is provided, the delegate never sees the original
58 // response, instead the response produced by the intercept job will be
60 virtual URLRequestJob
* MaybeInterceptResponse(
61 URLRequest
* request
) const = 0;
63 // Returns true if this interceptor handles requests for URLs with the
64 // given protocol. Returning false does not imply that this interceptor
65 // can't or won't handle requests with the given protocol.
66 virtual bool WillHandleProtocol(const std::string
& protocol
) const {
71 URLRequestJobFactory();
72 ~URLRequestJobFactory();
74 // Sets the ProtocolHandler for a scheme. Returns true on success, false on
75 // failure (a ProtocolHandler already exists for |scheme|). On success,
76 // URLRequestJobFactory takes ownership of |protocol_handler|.
77 bool SetProtocolHandler(const std::string
& scheme
,
78 ProtocolHandler
* protocol_handler
);
80 // Takes ownership of |interceptor|. Adds it to the end of the Interceptor
82 void AddInterceptor(Interceptor
* interceptor
);
84 URLRequestJob
* MaybeCreateJobWithInterceptor(URLRequest
* request
) const;
86 URLRequestJob
* MaybeCreateJobWithProtocolHandler(const std::string
& scheme
,
87 URLRequest
* request
) const;
89 URLRequestJob
* MaybeInterceptRedirect(const GURL
& location
,
90 URLRequest
* request
) const;
92 URLRequestJob
* MaybeInterceptResponse(URLRequest
* request
) const;
94 bool IsHandledProtocol(const std::string
& scheme
) const;
96 bool IsHandledURL(const GURL
& url
) const;
99 typedef std::map
<std::string
, ProtocolHandler
*> ProtocolHandlerMap
;
100 typedef std::vector
<Interceptor
*> InterceptorList
;
102 ProtocolHandlerMap protocol_handler_map_
;
103 InterceptorList interceptors_
;
105 DISALLOW_COPY_AND_ASSIGN(URLRequestJobFactory
);
110 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_H_