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_manager.h" | 5 #include "net/url_request/url_request_job_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 { "data", URLRequestDataJob::Factory }, | 42 { "data", URLRequestDataJob::Factory }, |
43 }; | 43 }; |
44 | 44 |
45 // static | 45 // static |
46 URLRequestJobManager* URLRequestJobManager::GetInstance() { | 46 URLRequestJobManager* URLRequestJobManager::GetInstance() { |
47 return Singleton<URLRequestJobManager>::get(); | 47 return Singleton<URLRequestJobManager>::get(); |
48 } | 48 } |
49 | 49 |
50 URLRequestJob* URLRequestJobManager::CreateJob( | 50 URLRequestJob* URLRequestJobManager::CreateJob( |
51 URLRequest* request) const { | 51 URLRequest* request) const { |
52 #ifndef NDEBUG | |
53 DCHECK(IsAllowedThread()); | 52 DCHECK(IsAllowedThread()); |
54 #endif | |
55 | 53 |
56 // If we are given an invalid URL, then don't even try to inspect the scheme. | 54 // If we are given an invalid URL, then don't even try to inspect the scheme. |
57 if (!request->url().is_valid()) | 55 if (!request->url().is_valid()) |
58 return new URLRequestErrorJob(request, ERR_INVALID_URL); | 56 return new URLRequestErrorJob(request, ERR_INVALID_URL); |
59 | 57 |
60 // We do this here to avoid asking interceptors about unsupported schemes. | 58 // We do this here to avoid asking interceptors about unsupported schemes. |
61 const URLRequestJobFactory* job_factory = NULL; | 59 const URLRequestJobFactory* job_factory = NULL; |
62 if (request->context()) | 60 if (request->context()) |
63 job_factory = request->context()->job_factory(); | 61 job_factory = request->context()->job_factory(); |
64 | 62 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 // If we reached here, then it means that a registered protocol factory | 123 // If we reached here, then it means that a registered protocol factory |
126 // wasn't interested in handling the URL. That is fairly unexpected, and we | 124 // wasn't interested in handling the URL. That is fairly unexpected, and we |
127 // don't know have a specific error to report here :-( | 125 // don't know have a specific error to report here :-( |
128 LOG(WARNING) << "Failed to map: " << request->url().spec(); | 126 LOG(WARNING) << "Failed to map: " << request->url().spec(); |
129 return new URLRequestErrorJob(request, ERR_FAILED); | 127 return new URLRequestErrorJob(request, ERR_FAILED); |
130 } | 128 } |
131 | 129 |
132 URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( | 130 URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( |
133 URLRequest* request, | 131 URLRequest* request, |
134 const GURL& location) const { | 132 const GURL& location) const { |
135 #ifndef NDEBUG | |
136 DCHECK(IsAllowedThread()); | 133 DCHECK(IsAllowedThread()); |
137 #endif | 134 if (!request->url().is_valid() || |
138 if ((request->load_flags() & LOAD_DISABLE_INTERCEPT) || | 135 request->load_flags() & LOAD_DISABLE_INTERCEPT || |
139 (request->status().status() == URLRequestStatus::CANCELED) || | 136 request->status().status() == URLRequestStatus::CANCELED) { |
140 !request->url().is_valid() || | |
141 !SupportsScheme(request->url().scheme())) | |
142 return NULL; | 137 return NULL; |
| 138 } |
| 139 |
| 140 const URLRequestJobFactory* job_factory = NULL; |
| 141 if (request->context()) |
| 142 job_factory = request->context()->job_factory(); |
| 143 |
| 144 const std::string& scheme = request->url().scheme(); // already lowercase |
| 145 if (job_factory) { |
| 146 if (!job_factory->IsHandledProtocol(scheme)) { |
| 147 return NULL; |
| 148 } |
| 149 } else if (!SupportsScheme(scheme)) { |
| 150 return NULL; |
| 151 } |
| 152 |
| 153 URLRequestJob* job = NULL; |
| 154 if (job_factory) |
| 155 job = job_factory->MaybeInterceptRedirect(location, request); |
| 156 if (job) |
| 157 return job; |
143 | 158 |
144 InterceptorList::const_iterator i; | 159 InterceptorList::const_iterator i; |
145 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 160 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
146 URLRequestJob* job = (*i)->MaybeInterceptRedirect(request, location); | 161 job = (*i)->MaybeInterceptRedirect(request, location); |
147 if (job) | 162 if (job) |
148 return job; | 163 return job; |
149 } | 164 } |
150 return NULL; | 165 return NULL; |
151 } | 166 } |
152 | 167 |
153 URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( | 168 URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( |
154 URLRequest* request) const { | 169 URLRequest* request) const { |
155 #ifndef NDEBUG | |
156 DCHECK(IsAllowedThread()); | 170 DCHECK(IsAllowedThread()); |
157 #endif | 171 if (!request->url().is_valid() || |
158 if ((request->load_flags() & LOAD_DISABLE_INTERCEPT) || | 172 request->load_flags() & LOAD_DISABLE_INTERCEPT || |
159 (request->status().status() == URLRequestStatus::CANCELED) || | 173 request->status().status() == URLRequestStatus::CANCELED) { |
160 !request->url().is_valid() || | |
161 !SupportsScheme(request->url().scheme())) | |
162 return NULL; | 174 return NULL; |
| 175 } |
| 176 |
| 177 const URLRequestJobFactory* job_factory = NULL; |
| 178 if (request->context()) |
| 179 job_factory = request->context()->job_factory(); |
| 180 |
| 181 const std::string& scheme = request->url().scheme(); // already lowercase |
| 182 if (job_factory) { |
| 183 if (!job_factory->IsHandledProtocol(scheme)) { |
| 184 return NULL; |
| 185 } |
| 186 } else if (!SupportsScheme(scheme)) { |
| 187 return NULL; |
| 188 } |
| 189 |
| 190 URLRequestJob* job = NULL; |
| 191 if (job_factory) |
| 192 job = job_factory->MaybeInterceptResponse(request); |
| 193 if (job) |
| 194 return job; |
163 | 195 |
164 InterceptorList::const_iterator i; | 196 InterceptorList::const_iterator i; |
165 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 197 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
166 URLRequestJob* job = (*i)->MaybeInterceptResponse(request); | 198 job = (*i)->MaybeInterceptResponse(request); |
167 if (job) | 199 if (job) |
168 return job; | 200 return job; |
169 } | 201 } |
170 return NULL; | 202 return NULL; |
171 } | 203 } |
172 | 204 |
173 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) const { | 205 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) const { |
174 // The set of registered factories may change on another thread. | 206 // The set of registered factories may change on another thread. |
175 { | 207 { |
176 base::AutoLock locked(lock_); | 208 base::AutoLock locked(lock_); |
177 if (factories_.find(scheme) != factories_.end()) | 209 if (factories_.find(scheme) != factories_.end()) |
178 return true; | 210 return true; |
179 } | 211 } |
180 | 212 |
181 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) | 213 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) |
182 if (LowerCaseEqualsASCII(scheme, kBuiltinFactories[i].scheme)) | 214 if (LowerCaseEqualsASCII(scheme, kBuiltinFactories[i].scheme)) |
183 return true; | 215 return true; |
184 | 216 |
185 return false; | 217 return false; |
186 } | 218 } |
187 | 219 |
188 URLRequest::ProtocolFactory* URLRequestJobManager::RegisterProtocolFactory( | 220 URLRequest::ProtocolFactory* URLRequestJobManager::RegisterProtocolFactory( |
189 const std::string& scheme, | 221 const std::string& scheme, |
190 URLRequest::ProtocolFactory* factory) { | 222 URLRequest::ProtocolFactory* factory) { |
191 #ifndef NDEBUG | |
192 DCHECK(IsAllowedThread()); | 223 DCHECK(IsAllowedThread()); |
193 #endif | |
194 | 224 |
195 base::AutoLock locked(lock_); | 225 base::AutoLock locked(lock_); |
196 | 226 |
197 URLRequest::ProtocolFactory* old_factory; | 227 URLRequest::ProtocolFactory* old_factory; |
198 FactoryMap::iterator i = factories_.find(scheme); | 228 FactoryMap::iterator i = factories_.find(scheme); |
199 if (i != factories_.end()) { | 229 if (i != factories_.end()) { |
200 old_factory = i->second; | 230 old_factory = i->second; |
201 } else { | 231 } else { |
202 old_factory = NULL; | 232 old_factory = NULL; |
203 } | 233 } |
204 if (factory) { | 234 if (factory) { |
205 factories_[scheme] = factory; | 235 factories_[scheme] = factory; |
206 } else if (i != factories_.end()) { // uninstall any old one | 236 } else if (i != factories_.end()) { // uninstall any old one |
207 factories_.erase(i); | 237 factories_.erase(i); |
208 } | 238 } |
209 return old_factory; | 239 return old_factory; |
210 } | 240 } |
211 | 241 |
212 void URLRequestJobManager::RegisterRequestInterceptor( | 242 void URLRequestJobManager::RegisterRequestInterceptor( |
213 URLRequest::Interceptor* interceptor) { | 243 URLRequest::Interceptor* interceptor) { |
214 #ifndef NDEBUG | |
215 DCHECK(IsAllowedThread()); | 244 DCHECK(IsAllowedThread()); |
216 #endif | |
217 | 245 |
218 base::AutoLock locked(lock_); | 246 base::AutoLock locked(lock_); |
219 | 247 |
220 DCHECK(std::find(interceptors_.begin(), interceptors_.end(), interceptor) == | 248 DCHECK(std::find(interceptors_.begin(), interceptors_.end(), interceptor) == |
221 interceptors_.end()); | 249 interceptors_.end()); |
222 interceptors_.push_back(interceptor); | 250 interceptors_.push_back(interceptor); |
223 } | 251 } |
224 | 252 |
225 void URLRequestJobManager::UnregisterRequestInterceptor( | 253 void URLRequestJobManager::UnregisterRequestInterceptor( |
226 URLRequest::Interceptor* interceptor) { | 254 URLRequest::Interceptor* interceptor) { |
227 #ifndef NDEBUG | |
228 DCHECK(IsAllowedThread()); | 255 DCHECK(IsAllowedThread()); |
229 #endif | |
230 | 256 |
231 base::AutoLock locked(lock_); | 257 base::AutoLock locked(lock_); |
232 | 258 |
233 InterceptorList::iterator i = | 259 InterceptorList::iterator i = |
234 std::find(interceptors_.begin(), interceptors_.end(), interceptor); | 260 std::find(interceptors_.begin(), interceptors_.end(), interceptor); |
235 DCHECK(i != interceptors_.end()); | 261 DCHECK(i != interceptors_.end()); |
236 interceptors_.erase(i); | 262 interceptors_.erase(i); |
237 } | 263 } |
238 | 264 |
239 URLRequestJobManager::URLRequestJobManager() : enable_file_access_(false) { | 265 URLRequestJobManager::URLRequestJobManager() : enable_file_access_(false) { |
240 #ifndef NDEBUG | 266 #ifndef NDEBUG |
241 allowed_thread_ = 0; | 267 allowed_thread_ = 0; |
242 allowed_thread_initialized_ = false; | 268 allowed_thread_initialized_ = false; |
243 #endif | 269 #endif |
244 } | 270 } |
245 | 271 |
246 URLRequestJobManager::~URLRequestJobManager() {} | 272 URLRequestJobManager::~URLRequestJobManager() {} |
247 | 273 |
248 } // namespace net | 274 } // namespace net |
OLD | NEW |