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