OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/url_request_job_factory_impl.h" | 5 #include "net/url_request/url_request_job_factory_impl.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
9 #include "net/base/load_flags.h" | 9 #include "net/base/load_flags.h" |
10 #include "net/url_request/url_request_job_manager.h" | 10 #include "net/url_request/url_request_job_manager.h" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 URLRequestJobFactoryImpl::URLRequestJobFactoryImpl() {} | 14 URLRequestJobFactoryImpl::URLRequestJobFactoryImpl() {} |
15 | 15 |
16 URLRequestJobFactoryImpl::~URLRequestJobFactoryImpl() { | 16 URLRequestJobFactoryImpl::~URLRequestJobFactoryImpl() { |
17 STLDeleteValues(&protocol_handler_map_); | 17 STLDeleteValues(&protocol_handler_map_); |
18 STLDeleteElements(&interceptors_); | |
19 } | 18 } |
20 | 19 |
21 bool URLRequestJobFactoryImpl::SetProtocolHandler( | 20 bool URLRequestJobFactoryImpl::SetProtocolHandler( |
22 const std::string& scheme, | 21 const std::string& scheme, |
23 ProtocolHandler* protocol_handler) { | 22 ProtocolHandler* protocol_handler) { |
24 DCHECK(CalledOnValidThread()); | 23 DCHECK(CalledOnValidThread()); |
25 | 24 |
26 if (!protocol_handler) { | 25 if (!protocol_handler) { |
27 ProtocolHandlerMap::iterator it = protocol_handler_map_.find(scheme); | 26 ProtocolHandlerMap::iterator it = protocol_handler_map_.find(scheme); |
28 if (it == protocol_handler_map_.end()) | 27 if (it == protocol_handler_map_.end()) |
29 return false; | 28 return false; |
30 | 29 |
31 delete it->second; | 30 delete it->second; |
32 protocol_handler_map_.erase(it); | 31 protocol_handler_map_.erase(it); |
33 return true; | 32 return true; |
34 } | 33 } |
35 | 34 |
36 if (ContainsKey(protocol_handler_map_, scheme)) | 35 if (ContainsKey(protocol_handler_map_, scheme)) |
37 return false; | 36 return false; |
38 protocol_handler_map_[scheme] = protocol_handler; | 37 protocol_handler_map_[scheme] = protocol_handler; |
39 return true; | 38 return true; |
40 } | 39 } |
41 | 40 |
42 void URLRequestJobFactoryImpl::AddInterceptor(Interceptor* interceptor) { | |
43 DCHECK(CalledOnValidThread()); | |
44 CHECK(interceptor); | |
45 | |
46 interceptors_.push_back(interceptor); | |
47 } | |
48 | |
49 URLRequestJob* URLRequestJobFactoryImpl::MaybeCreateJobWithInterceptor( | |
50 URLRequest* request, NetworkDelegate* network_delegate) const { | |
51 DCHECK(CalledOnValidThread()); | |
52 URLRequestJob* job = NULL; | |
53 | |
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); | |
58 if (job) | |
59 return job; | |
60 } | |
61 } | |
62 return NULL; | |
63 } | |
64 | |
65 URLRequestJob* URLRequestJobFactoryImpl::MaybeCreateJobWithProtocolHandler( | 41 URLRequestJob* URLRequestJobFactoryImpl::MaybeCreateJobWithProtocolHandler( |
66 const std::string& scheme, | 42 const std::string& scheme, |
67 URLRequest* request, | 43 URLRequest* request, |
68 NetworkDelegate* network_delegate) const { | 44 NetworkDelegate* network_delegate) const { |
69 DCHECK(CalledOnValidThread()); | 45 DCHECK(CalledOnValidThread()); |
70 ProtocolHandlerMap::const_iterator it = protocol_handler_map_.find(scheme); | 46 ProtocolHandlerMap::const_iterator it = protocol_handler_map_.find(scheme); |
71 if (it == protocol_handler_map_.end()) | 47 if (it == protocol_handler_map_.end()) |
72 return NULL; | 48 return NULL; |
73 return it->second->MaybeCreateJob(request, network_delegate); | 49 return it->second->MaybeCreateJob(request, network_delegate); |
74 } | 50 } |
75 | 51 |
76 URLRequestJob* URLRequestJobFactoryImpl::MaybeInterceptRedirect( | |
77 const GURL& location, | |
78 URLRequest* request, | |
79 NetworkDelegate* network_delegate) const { | |
80 DCHECK(CalledOnValidThread()); | |
81 URLRequestJob* job = NULL; | |
82 | |
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); | |
87 if (job) | |
88 return job; | |
89 } | |
90 } | |
91 return NULL; | |
92 } | |
93 | |
94 URLRequestJob* URLRequestJobFactoryImpl::MaybeInterceptResponse( | |
95 URLRequest* request, NetworkDelegate* network_delegate) const { | |
96 DCHECK(CalledOnValidThread()); | |
97 URLRequestJob* job = NULL; | |
98 | |
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); | |
103 if (job) | |
104 return job; | |
105 } | |
106 } | |
107 return NULL; | |
108 } | |
109 | |
110 bool URLRequestJobFactoryImpl::IsHandledProtocol( | 52 bool URLRequestJobFactoryImpl::IsHandledProtocol( |
111 const std::string& scheme) const { | 53 const std::string& scheme) const { |
112 DCHECK(CalledOnValidThread()); | 54 DCHECK(CalledOnValidThread()); |
113 InterceptorList::const_iterator i; | |
114 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | |
115 if ((*i)->WillHandleProtocol(scheme)) | |
116 return true; | |
117 } | |
118 return ContainsKey(protocol_handler_map_, scheme) || | 55 return ContainsKey(protocol_handler_map_, scheme) || |
119 URLRequestJobManager::GetInstance()->SupportsScheme(scheme); | 56 URLRequestJobManager::GetInstance()->SupportsScheme(scheme); |
120 } | 57 } |
121 | 58 |
122 bool URLRequestJobFactoryImpl::IsHandledURL(const GURL& url) const { | 59 bool URLRequestJobFactoryImpl::IsHandledURL(const GURL& url) const { |
123 if (!url.is_valid()) { | 60 if (!url.is_valid()) { |
124 // We handle error cases. | 61 // We handle error cases. |
125 return true; | 62 return true; |
126 } | 63 } |
127 return IsHandledProtocol(url.scheme()); | 64 return IsHandledProtocol(url.scheme()); |
128 } | 65 } |
129 | 66 |
130 } // namespace net | 67 } // namespace net |
OLD | NEW |