| OLD | NEW |
| 1 // Copyright (c) 2010 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 "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "base/singleton.h" | 10 #include "base/singleton.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/url_request/url_request_about_job.h" | 14 #include "net/url_request/url_request_about_job.h" |
| 15 #include "net/url_request/url_request_data_job.h" | 15 #include "net/url_request/url_request_data_job.h" |
| 16 #include "net/url_request/url_request_error_job.h" | 16 #include "net/url_request/url_request_error_job.h" |
| 17 #include "net/url_request/url_request_file_job.h" | 17 #include "net/url_request/url_request_file_job.h" |
| 18 #include "net/url_request/url_request_ftp_job.h" | 18 #include "net/url_request/url_request_ftp_job.h" |
| 19 #include "net/url_request/url_request_http_job.h" | 19 #include "net/url_request/url_request_http_job.h" |
| 20 | 20 |
| 21 namespace net { |
| 22 |
| 21 // The built-in set of protocol factories | 23 // The built-in set of protocol factories |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| 24 struct SchemeToFactory { | 26 struct SchemeToFactory { |
| 25 const char* scheme; | 27 const char* scheme; |
| 26 net::URLRequest::ProtocolFactory* factory; | 28 URLRequest::ProtocolFactory* factory; |
| 27 }; | 29 }; |
| 28 | 30 |
| 29 } // namespace | 31 } // namespace |
| 30 | 32 |
| 31 namespace net { | |
| 32 | |
| 33 static const SchemeToFactory kBuiltinFactories[] = { | 33 static const SchemeToFactory kBuiltinFactories[] = { |
| 34 { "http", URLRequestHttpJob::Factory }, | 34 { "http", URLRequestHttpJob::Factory }, |
| 35 { "https", URLRequestHttpJob::Factory }, | 35 { "https", URLRequestHttpJob::Factory }, |
| 36 { "file", URLRequestFileJob::Factory }, | 36 { "file", URLRequestFileJob::Factory }, |
| 37 { "ftp", URLRequestFtpJob::Factory }, | 37 { "ftp", URLRequestFtpJob::Factory }, |
| 38 { "about", URLRequestAboutJob::Factory }, | 38 { "about", URLRequestAboutJob::Factory }, |
| 39 { "data", URLRequestDataJob::Factory }, | 39 { "data", URLRequestDataJob::Factory }, |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 // static | 42 // static |
| 43 URLRequestJobManager* URLRequestJobManager::GetInstance() { | 43 URLRequestJobManager* URLRequestJobManager::GetInstance() { |
| 44 return Singleton<URLRequestJobManager>::get(); | 44 return Singleton<URLRequestJobManager>::get(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 net::URLRequestJob* URLRequestJobManager::CreateJob( | 47 URLRequestJob* URLRequestJobManager::CreateJob( |
| 48 net::URLRequest* request) const { | 48 URLRequest* request) const { |
| 49 #ifndef NDEBUG | 49 #ifndef NDEBUG |
| 50 DCHECK(IsAllowedThread()); | 50 DCHECK(IsAllowedThread()); |
| 51 #endif | 51 #endif |
| 52 | 52 |
| 53 // If we are given an invalid URL, then don't even try to inspect the scheme. | 53 // If we are given an invalid URL, then don't even try to inspect the scheme. |
| 54 if (!request->url().is_valid()) | 54 if (!request->url().is_valid()) |
| 55 return new net::URLRequestErrorJob(request, net::ERR_INVALID_URL); | 55 return new URLRequestErrorJob(request, ERR_INVALID_URL); |
| 56 | 56 |
| 57 // We do this here to avoid asking interceptors about unsupported schemes. | 57 // We do this here to avoid asking interceptors about unsupported schemes. |
| 58 const std::string& scheme = request->url().scheme(); // already lowercase | 58 const std::string& scheme = request->url().scheme(); // already lowercase |
| 59 if (!SupportsScheme(scheme)) | 59 if (!SupportsScheme(scheme)) |
| 60 return new net::URLRequestErrorJob(request, net::ERR_UNKNOWN_URL_SCHEME); | 60 return new URLRequestErrorJob(request, ERR_UNKNOWN_URL_SCHEME); |
| 61 | 61 |
| 62 // THREAD-SAFETY NOTICE: | 62 // THREAD-SAFETY NOTICE: |
| 63 // We do not need to acquire the lock here since we are only reading our | 63 // We do not need to acquire the lock here since we are only reading our |
| 64 // data structures. They should only be modified on the current thread. | 64 // data structures. They should only be modified on the current thread. |
| 65 | 65 |
| 66 // See if the request should be intercepted. | 66 // See if the request should be intercepted. |
| 67 if (!(request->load_flags() & net::LOAD_DISABLE_INTERCEPT)) { | 67 if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { |
| 68 InterceptorList::const_iterator i; | 68 InterceptorList::const_iterator i; |
| 69 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 69 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
| 70 net::URLRequestJob* job = (*i)->MaybeIntercept(request); | 70 URLRequestJob* job = (*i)->MaybeIntercept(request); |
| 71 if (job) | 71 if (job) |
| 72 return job; | 72 return job; |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 // See if the request should be handled by a registered protocol factory. | 76 // See if the request should be handled by a registered protocol factory. |
| 77 // If the registered factory returns null, then we want to fall-back to the | 77 // If the registered factory returns null, then we want to fall-back to the |
| 78 // built-in protocol factory. | 78 // built-in protocol factory. |
| 79 FactoryMap::const_iterator i = factories_.find(scheme); | 79 FactoryMap::const_iterator i = factories_.find(scheme); |
| 80 if (i != factories_.end()) { | 80 if (i != factories_.end()) { |
| 81 net::URLRequestJob* job = i->second(request, scheme); | 81 URLRequestJob* job = i->second(request, scheme); |
| 82 if (job) | 82 if (job) |
| 83 return job; | 83 return job; |
| 84 } | 84 } |
| 85 | 85 |
| 86 // See if the request should be handled by a built-in protocol factory. | 86 // See if the request should be handled by a built-in protocol factory. |
| 87 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { | 87 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { |
| 88 if (scheme == kBuiltinFactories[i].scheme) { | 88 if (scheme == kBuiltinFactories[i].scheme) { |
| 89 net::URLRequestJob* job = (kBuiltinFactories[i].factory)(request, scheme); | 89 URLRequestJob* job = (kBuiltinFactories[i].factory)(request, scheme); |
| 90 DCHECK(job); // The built-in factories are not expected to fail! | 90 DCHECK(job); // The built-in factories are not expected to fail! |
| 91 return job; | 91 return job; |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 | 94 |
| 95 // If we reached here, then it means that a registered protocol factory | 95 // If we reached here, then it means that a registered protocol factory |
| 96 // wasn't interested in handling the URL. That is fairly unexpected, and we | 96 // wasn't interested in handling the URL. That is fairly unexpected, and we |
| 97 // don't know have a specific error to report here :-( | 97 // don't know have a specific error to report here :-( |
| 98 LOG(WARNING) << "Failed to map: " << request->url().spec(); | 98 LOG(WARNING) << "Failed to map: " << request->url().spec(); |
| 99 return new net::URLRequestErrorJob(request, net::ERR_FAILED); | 99 return new URLRequestErrorJob(request, ERR_FAILED); |
| 100 } | 100 } |
| 101 | 101 |
| 102 net::URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( | 102 URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( |
| 103 net::URLRequest* request, | 103 URLRequest* request, |
| 104 const GURL& location) const { | 104 const GURL& location) const { |
| 105 #ifndef NDEBUG | 105 #ifndef NDEBUG |
| 106 DCHECK(IsAllowedThread()); | 106 DCHECK(IsAllowedThread()); |
| 107 #endif | 107 #endif |
| 108 if ((request->load_flags() & net::LOAD_DISABLE_INTERCEPT) || | 108 if ((request->load_flags() & LOAD_DISABLE_INTERCEPT) || |
| 109 (request->status().status() == URLRequestStatus::CANCELED) || | 109 (request->status().status() == URLRequestStatus::CANCELED) || |
| 110 !request->url().is_valid() || | 110 !request->url().is_valid() || |
| 111 !SupportsScheme(request->url().scheme())) | 111 !SupportsScheme(request->url().scheme())) |
| 112 return NULL; | 112 return NULL; |
| 113 | 113 |
| 114 InterceptorList::const_iterator i; | 114 InterceptorList::const_iterator i; |
| 115 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 115 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
| 116 net::URLRequestJob* job = (*i)->MaybeInterceptRedirect(request, location); | 116 URLRequestJob* job = (*i)->MaybeInterceptRedirect(request, location); |
| 117 if (job) | 117 if (job) |
| 118 return job; | 118 return job; |
| 119 } | 119 } |
| 120 return NULL; | 120 return NULL; |
| 121 } | 121 } |
| 122 | 122 |
| 123 net::URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( | 123 URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( |
| 124 net::URLRequest* request) const { | 124 URLRequest* request) const { |
| 125 #ifndef NDEBUG | 125 #ifndef NDEBUG |
| 126 DCHECK(IsAllowedThread()); | 126 DCHECK(IsAllowedThread()); |
| 127 #endif | 127 #endif |
| 128 if ((request->load_flags() & net::LOAD_DISABLE_INTERCEPT) || | 128 if ((request->load_flags() & LOAD_DISABLE_INTERCEPT) || |
| 129 (request->status().status() == URLRequestStatus::CANCELED) || | 129 (request->status().status() == URLRequestStatus::CANCELED) || |
| 130 !request->url().is_valid() || | 130 !request->url().is_valid() || |
| 131 !SupportsScheme(request->url().scheme())) | 131 !SupportsScheme(request->url().scheme())) |
| 132 return NULL; | 132 return NULL; |
| 133 | 133 |
| 134 InterceptorList::const_iterator i; | 134 InterceptorList::const_iterator i; |
| 135 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 135 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
| 136 net::URLRequestJob* job = (*i)->MaybeInterceptResponse(request); | 136 URLRequestJob* job = (*i)->MaybeInterceptResponse(request); |
| 137 if (job) | 137 if (job) |
| 138 return job; | 138 return job; |
| 139 } | 139 } |
| 140 return NULL; | 140 return NULL; |
| 141 } | 141 } |
| 142 | 142 |
| 143 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) const { | 143 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) const { |
| 144 // The set of registered factories may change on another thread. | 144 // The set of registered factories may change on another thread. |
| 145 { | 145 { |
| 146 base::AutoLock locked(lock_); | 146 base::AutoLock locked(lock_); |
| 147 if (factories_.find(scheme) != factories_.end()) | 147 if (factories_.find(scheme) != factories_.end()) |
| 148 return true; | 148 return true; |
| 149 } | 149 } |
| 150 | 150 |
| 151 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) | 151 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) |
| 152 if (LowerCaseEqualsASCII(scheme, kBuiltinFactories[i].scheme)) | 152 if (LowerCaseEqualsASCII(scheme, kBuiltinFactories[i].scheme)) |
| 153 return true; | 153 return true; |
| 154 | 154 |
| 155 return false; | 155 return false; |
| 156 } | 156 } |
| 157 | 157 |
| 158 net::URLRequest::ProtocolFactory* URLRequestJobManager::RegisterProtocolFactory( | 158 URLRequest::ProtocolFactory* URLRequestJobManager::RegisterProtocolFactory( |
| 159 const std::string& scheme, | 159 const std::string& scheme, |
| 160 net::URLRequest::ProtocolFactory* factory) { | 160 URLRequest::ProtocolFactory* factory) { |
| 161 #ifndef NDEBUG | 161 #ifndef NDEBUG |
| 162 DCHECK(IsAllowedThread()); | 162 DCHECK(IsAllowedThread()); |
| 163 #endif | 163 #endif |
| 164 | 164 |
| 165 base::AutoLock locked(lock_); | 165 base::AutoLock locked(lock_); |
| 166 | 166 |
| 167 net::URLRequest::ProtocolFactory* old_factory; | 167 URLRequest::ProtocolFactory* old_factory; |
| 168 FactoryMap::iterator i = factories_.find(scheme); | 168 FactoryMap::iterator i = factories_.find(scheme); |
| 169 if (i != factories_.end()) { | 169 if (i != factories_.end()) { |
| 170 old_factory = i->second; | 170 old_factory = i->second; |
| 171 } else { | 171 } else { |
| 172 old_factory = NULL; | 172 old_factory = NULL; |
| 173 } | 173 } |
| 174 if (factory) { | 174 if (factory) { |
| 175 factories_[scheme] = factory; | 175 factories_[scheme] = factory; |
| 176 } else if (i != factories_.end()) { // uninstall any old one | 176 } else if (i != factories_.end()) { // uninstall any old one |
| 177 factories_.erase(i); | 177 factories_.erase(i); |
| 178 } | 178 } |
| 179 return old_factory; | 179 return old_factory; |
| 180 } | 180 } |
| 181 | 181 |
| 182 void URLRequestJobManager::RegisterRequestInterceptor( | 182 void URLRequestJobManager::RegisterRequestInterceptor( |
| 183 net::URLRequest::Interceptor* interceptor) { | 183 URLRequest::Interceptor* interceptor) { |
| 184 #ifndef NDEBUG | 184 #ifndef NDEBUG |
| 185 DCHECK(IsAllowedThread()); | 185 DCHECK(IsAllowedThread()); |
| 186 #endif | 186 #endif |
| 187 | 187 |
| 188 base::AutoLock locked(lock_); | 188 base::AutoLock locked(lock_); |
| 189 | 189 |
| 190 DCHECK(std::find(interceptors_.begin(), interceptors_.end(), interceptor) == | 190 DCHECK(std::find(interceptors_.begin(), interceptors_.end(), interceptor) == |
| 191 interceptors_.end()); | 191 interceptors_.end()); |
| 192 interceptors_.push_back(interceptor); | 192 interceptors_.push_back(interceptor); |
| 193 } | 193 } |
| 194 | 194 |
| 195 void URLRequestJobManager::UnregisterRequestInterceptor( | 195 void URLRequestJobManager::UnregisterRequestInterceptor( |
| 196 net::URLRequest::Interceptor* interceptor) { | 196 URLRequest::Interceptor* interceptor) { |
| 197 #ifndef NDEBUG | 197 #ifndef NDEBUG |
| 198 DCHECK(IsAllowedThread()); | 198 DCHECK(IsAllowedThread()); |
| 199 #endif | 199 #endif |
| 200 | 200 |
| 201 base::AutoLock locked(lock_); | 201 base::AutoLock locked(lock_); |
| 202 | 202 |
| 203 InterceptorList::iterator i = | 203 InterceptorList::iterator i = |
| 204 std::find(interceptors_.begin(), interceptors_.end(), interceptor); | 204 std::find(interceptors_.begin(), interceptors_.end(), interceptor); |
| 205 DCHECK(i != interceptors_.end()); | 205 DCHECK(i != interceptors_.end()); |
| 206 interceptors_.erase(i); | 206 interceptors_.erase(i); |
| 207 } | 207 } |
| 208 | 208 |
| 209 URLRequestJobManager::URLRequestJobManager() : enable_file_access_(false) { | 209 URLRequestJobManager::URLRequestJobManager() : enable_file_access_(false) { |
| 210 #ifndef NDEBUG | 210 #ifndef NDEBUG |
| 211 allowed_thread_ = 0; | 211 allowed_thread_ = 0; |
| 212 allowed_thread_initialized_ = false; | 212 allowed_thread_initialized_ = false; |
| 213 #endif | 213 #endif |
| 214 } | 214 } |
| 215 | 215 |
| 216 URLRequestJobManager::~URLRequestJobManager() {} | 216 URLRequestJobManager::~URLRequestJobManager() {} |
| 217 | 217 |
| 218 } // namespace net | 218 } // namespace net |
| OLD | NEW |