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 12 matching lines...) Expand all Loading... |
23 namespace { | 23 namespace { |
24 | 24 |
25 struct SchemeToFactory { | 25 struct SchemeToFactory { |
26 const char* scheme; | 26 const char* scheme; |
27 URLRequest::ProtocolFactory* factory; | 27 URLRequest::ProtocolFactory* factory; |
28 }; | 28 }; |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 static const SchemeToFactory kBuiltinFactories[] = { | 32 static const SchemeToFactory kBuiltinFactories[] = { |
33 { "http", URLRequestHttpJob::Factory }, | 33 {"http", URLRequestHttpJob::Factory}, |
34 { "https", URLRequestHttpJob::Factory }, | 34 {"https", URLRequestHttpJob::Factory}, |
35 | 35 |
36 #if !defined(OS_IOS) | 36 #if !defined(OS_IOS) |
37 { "ws", URLRequestHttpJob::Factory }, | 37 {"ws", URLRequestHttpJob::Factory}, |
38 { "wss", URLRequestHttpJob::Factory }, | 38 {"wss", URLRequestHttpJob::Factory}, |
39 #endif // !defined(OS_IOS) | 39 #endif // !defined(OS_IOS) |
40 | |
41 }; | 40 }; |
42 | 41 |
43 // static | 42 // static |
44 URLRequestJobManager* URLRequestJobManager::GetInstance() { | 43 URLRequestJobManager* URLRequestJobManager::GetInstance() { |
45 return Singleton<URLRequestJobManager>::get(); | 44 return Singleton<URLRequestJobManager>::get(); |
46 } | 45 } |
47 | 46 |
48 URLRequestJob* URLRequestJobManager::CreateJob( | 47 URLRequestJob* URLRequestJobManager::CreateJob( |
49 URLRequest* request, NetworkDelegate* network_delegate) const { | 48 URLRequest* request, |
| 49 NetworkDelegate* network_delegate) const { |
50 DCHECK(IsAllowedThread()); | 50 DCHECK(IsAllowedThread()); |
51 | 51 |
52 // If we are given an invalid URL, then don't even try to inspect the scheme. | 52 // If we are given an invalid URL, then don't even try to inspect the scheme. |
53 if (!request->url().is_valid()) | 53 if (!request->url().is_valid()) |
54 return new URLRequestErrorJob(request, network_delegate, ERR_INVALID_URL); | 54 return new URLRequestErrorJob(request, network_delegate, ERR_INVALID_URL); |
55 | 55 |
56 // We do this here to avoid asking interceptors about unsupported schemes. | 56 // We do this here to avoid asking interceptors about unsupported schemes. |
57 const URLRequestJobFactory* job_factory = NULL; | 57 const URLRequestJobFactory* job_factory = NULL; |
58 job_factory = request->context()->job_factory(); | 58 job_factory = request->context()->job_factory(); |
59 | 59 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 FactoryMap::const_iterator i = factories_.find(scheme); | 101 FactoryMap::const_iterator i = factories_.find(scheme); |
102 if (i != factories_.end()) { | 102 if (i != factories_.end()) { |
103 URLRequestJob* job = i->second(request, network_delegate, scheme); | 103 URLRequestJob* job = i->second(request, network_delegate, scheme); |
104 if (job) | 104 if (job) |
105 return job; | 105 return job; |
106 } | 106 } |
107 | 107 |
108 // See if the request should be handled by a built-in protocol factory. | 108 // See if the request should be handled by a built-in protocol factory. |
109 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { | 109 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { |
110 if (scheme == kBuiltinFactories[i].scheme) { | 110 if (scheme == kBuiltinFactories[i].scheme) { |
111 URLRequestJob* job = (kBuiltinFactories[i].factory)( | 111 URLRequestJob* job = |
112 request, network_delegate, scheme); | 112 (kBuiltinFactories[i].factory)(request, network_delegate, scheme); |
113 DCHECK(job); // The built-in factories are not expected to fail! | 113 DCHECK(job); // The built-in factories are not expected to fail! |
114 return job; | 114 return job; |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 // If we reached here, then it means that a registered protocol factory | 118 // If we reached here, then it means that a registered protocol factory |
119 // wasn't interested in handling the URL. That is fairly unexpected, and we | 119 // wasn't interested in handling the URL. That is fairly unexpected, and we |
120 // don't have a specific error to report here :-( | 120 // don't have a specific error to report here :-( |
121 LOG(WARNING) << "Failed to map: " << request->url().spec(); | 121 LOG(WARNING) << "Failed to map: " << request->url().spec(); |
122 return new URLRequestErrorJob(request, network_delegate, ERR_FAILED); | 122 return new URLRequestErrorJob(request, network_delegate, ERR_FAILED); |
(...skipping 17 matching lines...) Expand all Loading... |
140 if (job_factory) { | 140 if (job_factory) { |
141 if (!job_factory->IsHandledProtocol(scheme)) { | 141 if (!job_factory->IsHandledProtocol(scheme)) { |
142 return NULL; | 142 return NULL; |
143 } | 143 } |
144 } else if (!SupportsScheme(scheme)) { | 144 } else if (!SupportsScheme(scheme)) { |
145 return NULL; | 145 return NULL; |
146 } | 146 } |
147 | 147 |
148 InterceptorList::const_iterator i; | 148 InterceptorList::const_iterator i; |
149 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 149 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
150 URLRequestJob* job = (*i)->MaybeInterceptRedirect(request, | 150 URLRequestJob* job = |
151 network_delegate, | 151 (*i)->MaybeInterceptRedirect(request, network_delegate, location); |
152 location); | |
153 if (job) | 152 if (job) |
154 return job; | 153 return job; |
155 } | 154 } |
156 return NULL; | 155 return NULL; |
157 } | 156 } |
158 | 157 |
159 URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( | 158 URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( |
160 URLRequest* request, NetworkDelegate* network_delegate) const { | 159 URLRequest* request, |
| 160 NetworkDelegate* network_delegate) const { |
161 DCHECK(IsAllowedThread()); | 161 DCHECK(IsAllowedThread()); |
162 if (!request->url().is_valid() || | 162 if (!request->url().is_valid() || |
163 request->load_flags() & LOAD_DISABLE_INTERCEPT || | 163 request->load_flags() & LOAD_DISABLE_INTERCEPT || |
164 request->status().status() == URLRequestStatus::CANCELED) { | 164 request->status().status() == URLRequestStatus::CANCELED) { |
165 return NULL; | 165 return NULL; |
166 } | 166 } |
167 | 167 |
168 const URLRequestJobFactory* job_factory = NULL; | 168 const URLRequestJobFactory* job_factory = NULL; |
169 job_factory = request->context()->job_factory(); | 169 job_factory = request->context()->job_factory(); |
170 | 170 |
171 const std::string& scheme = request->url().scheme(); // already lowercase | 171 const std::string& scheme = request->url().scheme(); // already lowercase |
172 if (job_factory) { | 172 if (job_factory) { |
173 if (!job_factory->IsHandledProtocol(scheme)) { | 173 if (!job_factory->IsHandledProtocol(scheme)) { |
174 return NULL; | 174 return NULL; |
175 } | 175 } |
176 } else if (!SupportsScheme(scheme)) { | 176 } else if (!SupportsScheme(scheme)) { |
177 return NULL; | 177 return NULL; |
178 } | 178 } |
179 | 179 |
180 InterceptorList::const_iterator i; | 180 InterceptorList::const_iterator i; |
181 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 181 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
182 URLRequestJob* job = (*i)->MaybeInterceptResponse(request, | 182 URLRequestJob* job = |
183 network_delegate); | 183 (*i)->MaybeInterceptResponse(request, network_delegate); |
184 if (job) | 184 if (job) |
185 return job; | 185 return job; |
186 } | 186 } |
187 return NULL; | 187 return NULL; |
188 } | 188 } |
189 | 189 |
190 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) const { | 190 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) const { |
191 // The set of registered factories may change on another thread. | 191 // The set of registered factories may change on another thread. |
192 { | 192 { |
193 base::AutoLock locked(lock_); | 193 base::AutoLock locked(lock_); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 | 241 |
242 base::AutoLock locked(lock_); | 242 base::AutoLock locked(lock_); |
243 | 243 |
244 InterceptorList::iterator i = | 244 InterceptorList::iterator i = |
245 std::find(interceptors_.begin(), interceptors_.end(), interceptor); | 245 std::find(interceptors_.begin(), interceptors_.end(), interceptor); |
246 DCHECK(i != interceptors_.end()); | 246 DCHECK(i != interceptors_.end()); |
247 interceptors_.erase(i); | 247 interceptors_.erase(i); |
248 } | 248 } |
249 | 249 |
250 URLRequestJobManager::URLRequestJobManager() | 250 URLRequestJobManager::URLRequestJobManager() |
251 : allowed_thread_(0), | 251 : allowed_thread_(0), allowed_thread_initialized_(false) { |
252 allowed_thread_initialized_(false) { | |
253 } | 252 } |
254 | 253 |
255 URLRequestJobManager::~URLRequestJobManager() {} | 254 URLRequestJobManager::~URLRequestJobManager() { |
| 255 } |
256 | 256 |
257 } // namespace net | 257 } // namespace net |
OLD | NEW |