| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "base/singleton.h" | 10 #include "base/singleton.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 #endif | 44 #endif |
| 45 } | 45 } |
| 46 | 46 |
| 47 URLRequestJobManager::~URLRequestJobManager() {} | 47 URLRequestJobManager::~URLRequestJobManager() {} |
| 48 | 48 |
| 49 // static | 49 // static |
| 50 URLRequestJobManager* URLRequestJobManager::GetInstance() { | 50 URLRequestJobManager* URLRequestJobManager::GetInstance() { |
| 51 return Singleton<URLRequestJobManager>::get(); | 51 return Singleton<URLRequestJobManager>::get(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 URLRequestJob* URLRequestJobManager::CreateJob(net::URLRequest* request) const { | 54 net::URLRequestJob* URLRequestJobManager::CreateJob( |
| 55 net::URLRequest* request) const { |
| 55 #ifndef NDEBUG | 56 #ifndef NDEBUG |
| 56 DCHECK(IsAllowedThread()); | 57 DCHECK(IsAllowedThread()); |
| 57 #endif | 58 #endif |
| 58 | 59 |
| 59 // If we are given an invalid URL, then don't even try to inspect the scheme. | 60 // If we are given an invalid URL, then don't even try to inspect the scheme. |
| 60 if (!request->url().is_valid()) | 61 if (!request->url().is_valid()) |
| 61 return new URLRequestErrorJob(request, net::ERR_INVALID_URL); | 62 return new URLRequestErrorJob(request, net::ERR_INVALID_URL); |
| 62 | 63 |
| 63 // We do this here to avoid asking interceptors about unsupported schemes. | 64 // We do this here to avoid asking interceptors about unsupported schemes. |
| 64 const std::string& scheme = request->url().scheme(); // already lowercase | 65 const std::string& scheme = request->url().scheme(); // already lowercase |
| 65 if (!SupportsScheme(scheme)) | 66 if (!SupportsScheme(scheme)) |
| 66 return new URLRequestErrorJob(request, net::ERR_UNKNOWN_URL_SCHEME); | 67 return new URLRequestErrorJob(request, net::ERR_UNKNOWN_URL_SCHEME); |
| 67 | 68 |
| 68 // THREAD-SAFETY NOTICE: | 69 // THREAD-SAFETY NOTICE: |
| 69 // We do not need to acquire the lock here since we are only reading our | 70 // We do not need to acquire the lock here since we are only reading our |
| 70 // data structures. They should only be modified on the current thread. | 71 // data structures. They should only be modified on the current thread. |
| 71 | 72 |
| 72 // See if the request should be intercepted. | 73 // See if the request should be intercepted. |
| 73 if (!(request->load_flags() & net::LOAD_DISABLE_INTERCEPT)) { | 74 if (!(request->load_flags() & net::LOAD_DISABLE_INTERCEPT)) { |
| 74 InterceptorList::const_iterator i; | 75 InterceptorList::const_iterator i; |
| 75 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 76 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
| 76 URLRequestJob* job = (*i)->MaybeIntercept(request); | 77 net::URLRequestJob* job = (*i)->MaybeIntercept(request); |
| 77 if (job) | 78 if (job) |
| 78 return job; | 79 return job; |
| 79 } | 80 } |
| 80 } | 81 } |
| 81 | 82 |
| 82 // See if the request should be handled by a registered protocol factory. | 83 // See if the request should be handled by a registered protocol factory. |
| 83 // If the registered factory returns null, then we want to fall-back to the | 84 // If the registered factory returns null, then we want to fall-back to the |
| 84 // built-in protocol factory. | 85 // built-in protocol factory. |
| 85 FactoryMap::const_iterator i = factories_.find(scheme); | 86 FactoryMap::const_iterator i = factories_.find(scheme); |
| 86 if (i != factories_.end()) { | 87 if (i != factories_.end()) { |
| 87 URLRequestJob* job = i->second(request, scheme); | 88 net::URLRequestJob* job = i->second(request, scheme); |
| 88 if (job) | 89 if (job) |
| 89 return job; | 90 return job; |
| 90 } | 91 } |
| 91 | 92 |
| 92 // See if the request should be handled by a built-in protocol factory. | 93 // See if the request should be handled by a built-in protocol factory. |
| 93 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { | 94 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { |
| 94 if (scheme == kBuiltinFactories[i].scheme) { | 95 if (scheme == kBuiltinFactories[i].scheme) { |
| 95 URLRequestJob* job = (kBuiltinFactories[i].factory)(request, scheme); | 96 net::URLRequestJob* job = (kBuiltinFactories[i].factory)(request, scheme); |
| 96 DCHECK(job); // The built-in factories are not expected to fail! | 97 DCHECK(job); // The built-in factories are not expected to fail! |
| 97 return job; | 98 return job; |
| 98 } | 99 } |
| 99 } | 100 } |
| 100 | 101 |
| 101 // If we reached here, then it means that a registered protocol factory | 102 // If we reached here, then it means that a registered protocol factory |
| 102 // wasn't interested in handling the URL. That is fairly unexpected, and we | 103 // wasn't interested in handling the URL. That is fairly unexpected, and we |
| 103 // don't know have a specific error to report here :-( | 104 // don't know have a specific error to report here :-( |
| 104 LOG(WARNING) << "Failed to map: " << request->url().spec(); | 105 LOG(WARNING) << "Failed to map: " << request->url().spec(); |
| 105 return new URLRequestErrorJob(request, net::ERR_FAILED); | 106 return new URLRequestErrorJob(request, net::ERR_FAILED); |
| 106 } | 107 } |
| 107 | 108 |
| 108 URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( | 109 net::URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( |
| 109 net::URLRequest* request, | 110 net::URLRequest* request, |
| 110 const GURL& location) const { | 111 const GURL& location) const { |
| 111 #ifndef NDEBUG | 112 #ifndef NDEBUG |
| 112 DCHECK(IsAllowedThread()); | 113 DCHECK(IsAllowedThread()); |
| 113 #endif | 114 #endif |
| 114 if ((request->load_flags() & net::LOAD_DISABLE_INTERCEPT) || | 115 if ((request->load_flags() & net::LOAD_DISABLE_INTERCEPT) || |
| 115 (request->status().status() == URLRequestStatus::CANCELED) || | 116 (request->status().status() == URLRequestStatus::CANCELED) || |
| 116 !request->url().is_valid() || | 117 !request->url().is_valid() || |
| 117 !SupportsScheme(request->url().scheme())) | 118 !SupportsScheme(request->url().scheme())) |
| 118 return NULL; | 119 return NULL; |
| 119 | 120 |
| 120 InterceptorList::const_iterator i; | 121 InterceptorList::const_iterator i; |
| 121 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 122 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
| 122 URLRequestJob* job = (*i)->MaybeInterceptRedirect(request, location); | 123 net::URLRequestJob* job = (*i)->MaybeInterceptRedirect(request, location); |
| 123 if (job) | 124 if (job) |
| 124 return job; | 125 return job; |
| 125 } | 126 } |
| 126 return NULL; | 127 return NULL; |
| 127 } | 128 } |
| 128 | 129 |
| 129 URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( | 130 net::URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( |
| 130 net::URLRequest* request) const { | 131 net::URLRequest* request) const { |
| 131 #ifndef NDEBUG | 132 #ifndef NDEBUG |
| 132 DCHECK(IsAllowedThread()); | 133 DCHECK(IsAllowedThread()); |
| 133 #endif | 134 #endif |
| 134 if ((request->load_flags() & net::LOAD_DISABLE_INTERCEPT) || | 135 if ((request->load_flags() & net::LOAD_DISABLE_INTERCEPT) || |
| 135 (request->status().status() == URLRequestStatus::CANCELED) || | 136 (request->status().status() == URLRequestStatus::CANCELED) || |
| 136 !request->url().is_valid() || | 137 !request->url().is_valid() || |
| 137 !SupportsScheme(request->url().scheme())) | 138 !SupportsScheme(request->url().scheme())) |
| 138 return NULL; | 139 return NULL; |
| 139 | 140 |
| 140 InterceptorList::const_iterator i; | 141 InterceptorList::const_iterator i; |
| 141 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 142 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
| 142 URLRequestJob* job = (*i)->MaybeInterceptResponse(request); | 143 net::URLRequestJob* job = (*i)->MaybeInterceptResponse(request); |
| 143 if (job) | 144 if (job) |
| 144 return job; | 145 return job; |
| 145 } | 146 } |
| 146 return NULL; | 147 return NULL; |
| 147 } | 148 } |
| 148 | 149 |
| 149 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) const { | 150 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) const { |
| 150 // The set of registered factories may change on another thread. | 151 // The set of registered factories may change on another thread. |
| 151 { | 152 { |
| 152 AutoLock locked(lock_); | 153 AutoLock locked(lock_); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 DCHECK(IsAllowedThread()); | 205 DCHECK(IsAllowedThread()); |
| 205 #endif | 206 #endif |
| 206 | 207 |
| 207 AutoLock locked(lock_); | 208 AutoLock locked(lock_); |
| 208 | 209 |
| 209 InterceptorList::iterator i = | 210 InterceptorList::iterator i = |
| 210 std::find(interceptors_.begin(), interceptors_.end(), interceptor); | 211 std::find(interceptors_.begin(), interceptors_.end(), interceptor); |
| 211 DCHECK(i != interceptors_.end()); | 212 DCHECK(i != interceptors_.end()); |
| 212 interceptors_.erase(i); | 213 interceptors_.erase(i); |
| 213 } | 214 } |
| OLD | NEW |