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 #include "net/url_request/url_request_job_factory_impl.h"
7 #include "base/stl_util.h"
8 #include "googleurl/src/gurl.h"
9 #include "net/base/load_flags.h"
10 #include "net/url_request/url_request_job_manager.h"
14 URLRequestJobFactoryImpl::URLRequestJobFactoryImpl() {}
16 URLRequestJobFactoryImpl::~URLRequestJobFactoryImpl() {
17 STLDeleteValues(&protocol_handler_map_
);
18 STLDeleteElements(&interceptors_
);
21 bool URLRequestJobFactoryImpl::SetProtocolHandler(
22 const std::string
& scheme
,
23 ProtocolHandler
* protocol_handler
) {
24 DCHECK(CalledOnValidThread());
26 if (!protocol_handler
) {
27 ProtocolHandlerMap::iterator it
= protocol_handler_map_
.find(scheme
);
28 if (it
== protocol_handler_map_
.end())
32 protocol_handler_map_
.erase(it
);
36 if (ContainsKey(protocol_handler_map_
, scheme
))
38 protocol_handler_map_
[scheme
] = protocol_handler
;
42 void URLRequestJobFactoryImpl::AddInterceptor(Interceptor
* interceptor
) {
43 DCHECK(CalledOnValidThread());
46 interceptors_
.push_back(interceptor
);
49 URLRequestJob
* URLRequestJobFactoryImpl::MaybeCreateJobWithInterceptor(
50 URLRequest
* request
, NetworkDelegate
* network_delegate
) const {
51 DCHECK(CalledOnValidThread());
52 URLRequestJob
* job
= NULL
;
54 if (!(request
->load_flags() & LOAD_DISABLE_INTERCEPT
)) {
55 InterceptorList::const_iterator i
;
56 for (i
= interceptors_
.begin(); i
!= interceptors_
.end(); ++i
) {
57 job
= (*i
)->MaybeIntercept(request
, network_delegate
);
65 URLRequestJob
* URLRequestJobFactoryImpl::MaybeCreateJobWithProtocolHandler(
66 const std::string
& scheme
,
68 NetworkDelegate
* network_delegate
) const {
69 DCHECK(CalledOnValidThread());
70 ProtocolHandlerMap::const_iterator it
= protocol_handler_map_
.find(scheme
);
71 if (it
== protocol_handler_map_
.end())
73 return it
->second
->MaybeCreateJob(request
, network_delegate
);
76 URLRequestJob
* URLRequestJobFactoryImpl::MaybeInterceptRedirect(
79 NetworkDelegate
* network_delegate
) const {
80 DCHECK(CalledOnValidThread());
81 URLRequestJob
* job
= NULL
;
83 if (!(request
->load_flags() & LOAD_DISABLE_INTERCEPT
)) {
84 InterceptorList::const_iterator i
;
85 for (i
= interceptors_
.begin(); i
!= interceptors_
.end(); ++i
) {
86 job
= (*i
)->MaybeInterceptRedirect(location
, request
, network_delegate
);
94 URLRequestJob
* URLRequestJobFactoryImpl::MaybeInterceptResponse(
95 URLRequest
* request
, NetworkDelegate
* network_delegate
) const {
96 DCHECK(CalledOnValidThread());
97 URLRequestJob
* job
= NULL
;
99 if (!(request
->load_flags() & LOAD_DISABLE_INTERCEPT
)) {
100 InterceptorList::const_iterator i
;
101 for (i
= interceptors_
.begin(); i
!= interceptors_
.end(); ++i
) {
102 job
= (*i
)->MaybeInterceptResponse(request
, network_delegate
);
110 bool URLRequestJobFactoryImpl::IsHandledProtocol(
111 const std::string
& scheme
) const {
112 DCHECK(CalledOnValidThread());
113 InterceptorList::const_iterator i
;
114 for (i
= interceptors_
.begin(); i
!= interceptors_
.end(); ++i
) {
115 if ((*i
)->WillHandleProtocol(scheme
))
118 return ContainsKey(protocol_handler_map_
, scheme
) ||
119 URLRequestJobManager::GetInstance()->SupportsScheme(scheme
);
122 bool URLRequestJobFactoryImpl::IsHandledURL(const GURL
& url
) const {
123 if (!url
.is_valid()) {
124 // We handle error cases.
127 return IsHandledProtocol(url
.scheme());