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( |
| 82 request, network_delegate); |
80 if (job) | 83 if (job) |
81 return job; | 84 return job; |
82 } | 85 } |
83 | 86 |
84 // TODO(willchan): Remove this in favor of URLRequestJobFactory::Interceptor. | 87 // TODO(willchan): Remove this in favor of URLRequestJobFactory::Interceptor. |
85 if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { | 88 if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { |
86 InterceptorList::const_iterator i; | 89 InterceptorList::const_iterator i; |
87 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 90 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
88 URLRequestJob* job = (*i)->MaybeIntercept(request); | 91 URLRequestJob* job = (*i)->MaybeIntercept(request, network_delegate); |
89 if (job) | 92 if (job) |
90 return job; | 93 return job; |
91 } | 94 } |
92 } | 95 } |
93 | 96 |
94 if (job_factory) { | 97 if (job_factory) { |
95 URLRequestJob* job = | 98 URLRequestJob* job = job_factory->MaybeCreateJobWithProtocolHandler( |
96 job_factory->MaybeCreateJobWithProtocolHandler(scheme, request); | 99 scheme, request, network_delegate); |
97 if (job) | 100 if (job) |
98 return job; | 101 return job; |
99 } | 102 } |
100 | 103 |
101 // TODO(willchan): Remove this in favor of | 104 // TODO(willchan): Remove this in favor of |
102 // URLRequestJobFactory::ProtocolHandler. | 105 // URLRequestJobFactory::ProtocolHandler. |
103 // See if the request should be handled by a registered protocol factory. | 106 // 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 | 107 // If the registered factory returns null, then we want to fall-back to the |
105 // built-in protocol factory. | 108 // built-in protocol factory. |
106 FactoryMap::const_iterator i = factories_.find(scheme); | 109 FactoryMap::const_iterator i = factories_.find(scheme); |
107 if (i != factories_.end()) { | 110 if (i != factories_.end()) { |
108 URLRequestJob* job = i->second(request, scheme); | 111 URLRequestJob* job = i->second(request, network_delegate, scheme); |
109 if (job) | 112 if (job) |
110 return job; | 113 return job; |
111 } | 114 } |
112 | 115 |
113 // See if the request should be handled by a built-in protocol factory. | 116 // See if the request should be handled by a built-in protocol factory. |
114 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { | 117 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { |
115 if (scheme == kBuiltinFactories[i].scheme) { | 118 if (scheme == kBuiltinFactories[i].scheme) { |
116 URLRequestJob* job = (kBuiltinFactories[i].factory)(request, scheme); | 119 URLRequestJob* job = (kBuiltinFactories[i].factory)( |
| 120 request, network_delegate, scheme); |
117 DCHECK(job); // The built-in factories are not expected to fail! | 121 DCHECK(job); // The built-in factories are not expected to fail! |
118 return job; | 122 return job; |
119 } | 123 } |
120 } | 124 } |
121 | 125 |
122 // If we reached here, then it means that a registered protocol factory | 126 // 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 | 127 // wasn't interested in handling the URL. That is fairly unexpected, and we |
124 // don't have a specific error to report here :-( | 128 // don't have a specific error to report here :-( |
125 LOG(WARNING) << "Failed to map: " << request->url().spec(); | 129 LOG(WARNING) << "Failed to map: " << request->url().spec(); |
126 return new URLRequestErrorJob(request, ERR_FAILED); | 130 return new URLRequestErrorJob(request, network_delegate, ERR_FAILED); |
127 } | 131 } |
128 | 132 |
129 URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( | 133 URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( |
130 URLRequest* request, | 134 URLRequest* request, |
| 135 NetworkDelegate* network_delegate, |
131 const GURL& location) const { | 136 const GURL& location) const { |
132 DCHECK(IsAllowedThread()); | 137 DCHECK(IsAllowedThread()); |
133 if (!request->url().is_valid() || | 138 if (!request->url().is_valid() || |
134 request->load_flags() & LOAD_DISABLE_INTERCEPT || | 139 request->load_flags() & LOAD_DISABLE_INTERCEPT || |
135 request->status().status() == URLRequestStatus::CANCELED) { | 140 request->status().status() == URLRequestStatus::CANCELED) { |
136 return NULL; | 141 return NULL; |
137 } | 142 } |
138 | 143 |
139 const URLRequestJobFactory* job_factory = NULL; | 144 const URLRequestJobFactory* job_factory = NULL; |
140 job_factory = request->context()->job_factory(); | 145 job_factory = request->context()->job_factory(); |
141 | 146 |
142 const std::string& scheme = request->url().scheme(); // already lowercase | 147 const std::string& scheme = request->url().scheme(); // already lowercase |
143 if (job_factory) { | 148 if (job_factory) { |
144 if (!job_factory->IsHandledProtocol(scheme)) { | 149 if (!job_factory->IsHandledProtocol(scheme)) { |
145 return NULL; | 150 return NULL; |
146 } | 151 } |
147 } else if (!SupportsScheme(scheme)) { | 152 } else if (!SupportsScheme(scheme)) { |
148 return NULL; | 153 return NULL; |
149 } | 154 } |
150 | 155 |
151 URLRequestJob* job = NULL; | 156 URLRequestJob* job = NULL; |
152 if (job_factory) | 157 if (job_factory) |
153 job = job_factory->MaybeInterceptRedirect(location, request); | 158 job = job_factory->MaybeInterceptRedirect( |
| 159 location, request, network_delegate); |
154 if (job) | 160 if (job) |
155 return job; | 161 return job; |
156 | 162 |
157 InterceptorList::const_iterator i; | 163 InterceptorList::const_iterator i; |
158 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 164 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
159 job = (*i)->MaybeInterceptRedirect(request, location); | 165 job = (*i)->MaybeInterceptRedirect(request, network_delegate, location); |
160 if (job) | 166 if (job) |
161 return job; | 167 return job; |
162 } | 168 } |
163 return NULL; | 169 return NULL; |
164 } | 170 } |
165 | 171 |
166 URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( | 172 URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( |
167 URLRequest* request) const { | 173 URLRequest* request, NetworkDelegate* network_delegate) const { |
168 DCHECK(IsAllowedThread()); | 174 DCHECK(IsAllowedThread()); |
169 if (!request->url().is_valid() || | 175 if (!request->url().is_valid() || |
170 request->load_flags() & LOAD_DISABLE_INTERCEPT || | 176 request->load_flags() & LOAD_DISABLE_INTERCEPT || |
171 request->status().status() == URLRequestStatus::CANCELED) { | 177 request->status().status() == URLRequestStatus::CANCELED) { |
172 return NULL; | 178 return NULL; |
173 } | 179 } |
174 | 180 |
175 const URLRequestJobFactory* job_factory = NULL; | 181 const URLRequestJobFactory* job_factory = NULL; |
176 job_factory = request->context()->job_factory(); | 182 job_factory = request->context()->job_factory(); |
177 | 183 |
178 const std::string& scheme = request->url().scheme(); // already lowercase | 184 const std::string& scheme = request->url().scheme(); // already lowercase |
179 if (job_factory) { | 185 if (job_factory) { |
180 if (!job_factory->IsHandledProtocol(scheme)) { | 186 if (!job_factory->IsHandledProtocol(scheme)) { |
181 return NULL; | 187 return NULL; |
182 } | 188 } |
183 } else if (!SupportsScheme(scheme)) { | 189 } else if (!SupportsScheme(scheme)) { |
184 return NULL; | 190 return NULL; |
185 } | 191 } |
186 | 192 |
187 URLRequestJob* job = NULL; | 193 URLRequestJob* job = NULL; |
188 if (job_factory) | 194 if (job_factory) |
189 job = job_factory->MaybeInterceptResponse(request); | 195 job = job_factory->MaybeInterceptResponse(request, network_delegate); |
190 if (job) | 196 if (job) |
191 return job; | 197 return job; |
192 | 198 |
193 InterceptorList::const_iterator i; | 199 InterceptorList::const_iterator i; |
194 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | 200 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { |
195 job = (*i)->MaybeInterceptResponse(request); | 201 job = (*i)->MaybeInterceptResponse(request, network_delegate); |
196 if (job) | 202 if (job) |
197 return job; | 203 return job; |
198 } | 204 } |
199 return NULL; | 205 return NULL; |
200 } | 206 } |
201 | 207 |
202 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) const { | 208 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) const { |
203 // The set of registered factories may change on another thread. | 209 // The set of registered factories may change on another thread. |
204 { | 210 { |
205 base::AutoLock locked(lock_); | 211 base::AutoLock locked(lock_); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 } | 266 } |
261 | 267 |
262 URLRequestJobManager::URLRequestJobManager() | 268 URLRequestJobManager::URLRequestJobManager() |
263 : allowed_thread_(0), | 269 : allowed_thread_(0), |
264 allowed_thread_initialized_(false) { | 270 allowed_thread_initialized_(false) { |
265 } | 271 } |
266 | 272 |
267 URLRequestJobManager::~URLRequestJobManager() {} | 273 URLRequestJobManager::~URLRequestJobManager() {} |
268 | 274 |
269 } // namespace net | 275 } // namespace net |
OLD | NEW |