| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 30 matching lines...) Expand all Loading... |
| 41 { "about", URLRequestAboutJob::Factory }, | 41 { "about", URLRequestAboutJob::Factory }, |
| 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, NetworkDelegate* network_delegate) const { |
| 52 DCHECK(IsAllowedThread()); | 52 DCHECK(IsAllowedThread()); |
| 53 | 53 |
| 54 // 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. |
| 55 if (!request->url().is_valid()) | 55 if (!request->url().is_valid()) |
| 56 return new URLRequestErrorJob(request, ERR_INVALID_URL); | 56 return new URLRequestErrorJob(request, network_delegate, ERR_INVALID_URL); |
| 57 | 57 |
| 58 // We do this here to avoid asking interceptors about unsupported schemes. | 58 // We do this here to avoid asking interceptors about unsupported schemes. |
| 59 const URLRequestJobFactory* job_factory = NULL; | 59 const URLRequestJobFactory* job_factory = NULL; |
| 60 job_factory = request->context()->job_factory(); | 60 job_factory = request->context()->job_factory(); |
| 61 | 61 |
| 62 const std::string& scheme = request->url().scheme(); // already lowercase | 62 const std::string& scheme = request->url().scheme(); // already lowercase |
| 63 if (job_factory) { | 63 if (job_factory) { |
| 64 if (!job_factory->IsHandledProtocol(scheme)) { | 64 if (!job_factory->IsHandledProtocol(scheme)) { |
| 65 return new URLRequestErrorJob(request, ERR_UNKNOWN_URL_SCHEME); | 65 return new URLRequestErrorJob( |
| 66 request, network_delegate, ERR_UNKNOWN_URL_SCHEME); |
| 66 } | 67 } |
| 67 } else if (!SupportsScheme(scheme)) { | 68 } else if (!SupportsScheme(scheme)) { |
| 68 return new URLRequestErrorJob(request, ERR_UNKNOWN_URL_SCHEME); | 69 return new URLRequestErrorJob( |
| 70 request, network_delegate, ERR_UNKNOWN_URL_SCHEME); |
| 69 } | 71 } |
| 70 | 72 |
| 71 // THREAD-SAFETY NOTICE: | 73 // THREAD-SAFETY NOTICE: |
| 72 // We do not need to acquire the lock here since we are only reading our | 74 // We do not need to acquire the lock here since we are only reading our |
| 73 // data structures. They should only be modified on the current thread. | 75 // data structures. They should only be modified on the current thread. |
| 74 | 76 |
| 75 // See if the request should be intercepted. | 77 // See if the request should be intercepted. |
| 76 // | 78 // |
| 77 | 79 |
| 78 if (job_factory) { | 80 if (job_factory) { |
| 79 URLRequestJob* job = job_factory->MaybeCreateJobWithInterceptor(request); | 81 URLRequestJob* job = job_factory->MaybeCreateJobWithInterceptor(request); |
| 80 if (job) | 82 if (job) |
| 81 return job; | 83 return job; |
| 82 } | 84 } |
| 83 | 85 |
| 84 // TODO(willchan): Remove this in favor of URLRequestJobFactory::Interceptor. | 86 // TODO(willchan): Remove this in favor of URLRequestJobFactory::Interceptor. |
| 85 if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { | 87 if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { |
| 86 InterceptorList::const_iterator i; | 88 InterceptorList::const_iterator i; |
| 87 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 89 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
| 88 URLRequestJob* job = (*i)->MaybeIntercept(request); | 90 URLRequestJob* job = (*i)->MaybeIntercept(request); |
| 89 if (job) | 91 if (job) |
| 90 return job; | 92 return job; |
| 91 } | 93 } |
| 92 } | 94 } |
| 93 | 95 |
| 94 if (job_factory) { | 96 if (job_factory) { |
| 95 URLRequestJob* job = | 97 URLRequestJob* job = job_factory->MaybeCreateJobWithProtocolHandler( |
| 96 job_factory->MaybeCreateJobWithProtocolHandler(scheme, request); | 98 scheme, request, network_delegate); |
| 97 if (job) | 99 if (job) |
| 98 return job; | 100 return job; |
| 99 } | 101 } |
| 100 | 102 |
| 101 // TODO(willchan): Remove this in favor of | 103 // TODO(willchan): Remove this in favor of |
| 102 // URLRequestJobFactory::ProtocolHandler. | 104 // URLRequestJobFactory::ProtocolHandler. |
| 103 // See if the request should be handled by a registered protocol factory. | 105 // See if the request should be handled by a registered protocol factory. |
| 104 // If the registered factory returns null, then we want to fall-back to the | 106 // If the registered factory returns null, then we want to fall-back to the |
| 105 // built-in protocol factory. | 107 // built-in protocol factory. |
| 106 FactoryMap::const_iterator i = factories_.find(scheme); | 108 FactoryMap::const_iterator i = factories_.find(scheme); |
| 107 if (i != factories_.end()) { | 109 if (i != factories_.end()) { |
| 108 URLRequestJob* job = i->second(request, scheme); | 110 URLRequestJob* job = i->second(request, scheme); |
| 109 if (job) | 111 if (job) |
| 110 return job; | 112 return job; |
| 111 } | 113 } |
| 112 | 114 |
| 113 // See if the request should be handled by a built-in protocol factory. | 115 // See if the request should be handled by a built-in protocol factory. |
| 114 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { | 116 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { |
| 115 if (scheme == kBuiltinFactories[i].scheme) { | 117 if (scheme == kBuiltinFactories[i].scheme) { |
| 116 URLRequestJob* job = (kBuiltinFactories[i].factory)(request, scheme); | 118 URLRequestJob* job = (kBuiltinFactories[i].factory)(request, scheme); |
| 117 DCHECK(job); // The built-in factories are not expected to fail! | 119 DCHECK(job); // The built-in factories are not expected to fail! |
| 118 return job; | 120 return job; |
| 119 } | 121 } |
| 120 } | 122 } |
| 121 | 123 |
| 122 // If we reached here, then it means that a registered protocol factory | 124 // If we reached here, then it means that a registered protocol factory |
| 123 // wasn't interested in handling the URL. That is fairly unexpected, and we | 125 // wasn't interested in handling the URL. That is fairly unexpected, and we |
| 124 // don't have a specific error to report here :-( | 126 // don't have a specific error to report here :-( |
| 125 LOG(WARNING) << "Failed to map: " << request->url().spec(); | 127 LOG(WARNING) << "Failed to map: " << request->url().spec(); |
| 126 return new URLRequestErrorJob(request, ERR_FAILED); | 128 return new URLRequestErrorJob(request, network_delegate, ERR_FAILED); |
| 127 } | 129 } |
| 128 | 130 |
| 129 URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( | 131 URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( |
| 130 URLRequest* request, | 132 URLRequest* request, |
| 131 const GURL& location) const { | 133 const GURL& location) const { |
| 132 DCHECK(IsAllowedThread()); | 134 DCHECK(IsAllowedThread()); |
| 133 if (!request->url().is_valid() || | 135 if (!request->url().is_valid() || |
| 134 request->load_flags() & LOAD_DISABLE_INTERCEPT || | 136 request->load_flags() & LOAD_DISABLE_INTERCEPT || |
| 135 request->status().status() == URLRequestStatus::CANCELED) { | 137 request->status().status() == URLRequestStatus::CANCELED) { |
| 136 return NULL; | 138 return NULL; |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 } | 262 } |
| 261 | 263 |
| 262 URLRequestJobManager::URLRequestJobManager() | 264 URLRequestJobManager::URLRequestJobManager() |
| 263 : allowed_thread_(0), | 265 : allowed_thread_(0), |
| 264 allowed_thread_initialized_(false) { | 266 allowed_thread_initialized_(false) { |
| 265 } | 267 } |
| 266 | 268 |
| 267 URLRequestJobManager::~URLRequestJobManager() {} | 269 URLRequestJobManager::~URLRequestJobManager() {} |
| 268 | 270 |
| 269 } // namespace net | 271 } // namespace net |
| OLD | NEW |