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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 return new URLRequestErrorJob( | 61 return new URLRequestErrorJob( |
62 request, network_delegate, ERR_UNKNOWN_URL_SCHEME); | 62 request, network_delegate, ERR_UNKNOWN_URL_SCHEME); |
63 } | 63 } |
64 | 64 |
65 // THREAD-SAFETY NOTICE: | 65 // THREAD-SAFETY NOTICE: |
66 // We do not need to acquire the lock here since we are only reading our | 66 // We do not need to acquire the lock here since we are only reading our |
67 // data structures. They should only be modified on the current thread. | 67 // data structures. They should only be modified on the current thread. |
68 | 68 |
69 // See if the request should be intercepted. | 69 // See if the request should be intercepted. |
70 // | 70 // |
71 | |
72 // TODO(pauljensen): Remove this when AppCacheInterceptor is a | |
73 // ProtocolHandler, see crbug.com/161547. | |
74 if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { | |
75 InterceptorList::const_iterator i; | |
76 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | |
77 URLRequestJob* job = (*i)->MaybeIntercept(request, network_delegate); | |
78 if (job) | |
79 return job; | |
80 } | |
81 } | |
82 | |
83 URLRequestJob* job = job_factory->MaybeCreateJobWithProtocolHandler( | 71 URLRequestJob* job = job_factory->MaybeCreateJobWithProtocolHandler( |
84 scheme, request, network_delegate); | 72 scheme, request, network_delegate); |
85 if (job) | 73 if (job) |
86 return job; | 74 return job; |
87 | 75 |
88 // See if the request should be handled by a built-in protocol factory. | 76 // See if the request should be handled by a built-in protocol factory. |
89 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { | 77 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { |
90 if (scheme == kBuiltinFactories[i].scheme) { | 78 if (scheme == kBuiltinFactories[i].scheme) { |
91 URLRequestJob* job = (kBuiltinFactories[i].factory)( | 79 URLRequestJob* job = (kBuiltinFactories[i].factory)( |
92 request, network_delegate, scheme); | 80 request, network_delegate, scheme); |
93 DCHECK(job); // The built-in factories are not expected to fail! | 81 DCHECK(job); // The built-in factories are not expected to fail! |
94 return job; | 82 return job; |
95 } | 83 } |
96 } | 84 } |
97 | 85 |
98 // If we reached here, then it means that a registered protocol factory | 86 // If we reached here, then it means that a registered protocol factory |
99 // wasn't interested in handling the URL. That is fairly unexpected, and we | 87 // wasn't interested in handling the URL. That is fairly unexpected, and we |
100 // don't have a specific error to report here :-( | 88 // don't have a specific error to report here :-( |
101 LOG(WARNING) << "Failed to map: " << request->url().spec(); | 89 LOG(WARNING) << "Failed to map: " << request->url().spec(); |
102 return new URLRequestErrorJob(request, network_delegate, ERR_FAILED); | 90 return new URLRequestErrorJob(request, network_delegate, ERR_FAILED); |
103 } | 91 } |
104 | 92 |
105 URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( | 93 URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect( |
106 URLRequest* request, | 94 URLRequest* request, |
107 NetworkDelegate* network_delegate, | 95 NetworkDelegate* network_delegate, |
108 const GURL& location) const { | 96 const GURL& location) const { |
109 DCHECK(IsAllowedThread()); | 97 DCHECK(IsAllowedThread()); |
110 if (!request->url().is_valid() || | 98 if (!request->url().is_valid() || |
111 request->load_flags() & LOAD_DISABLE_INTERCEPT || | |
112 request->status().status() == URLRequestStatus::CANCELED) { | 99 request->status().status() == URLRequestStatus::CANCELED) { |
113 return NULL; | 100 return NULL; |
114 } | 101 } |
115 | 102 |
116 const URLRequestJobFactory* job_factory = NULL; | 103 const URLRequestJobFactory* job_factory = NULL; |
117 job_factory = request->context()->job_factory(); | 104 job_factory = request->context()->job_factory(); |
118 | 105 |
119 const std::string& scheme = request->url().scheme(); // already lowercase | 106 const std::string& scheme = request->url().scheme(); // already lowercase |
120 if (!job_factory->IsHandledProtocol(scheme)) | 107 if (!job_factory->IsHandledProtocol(scheme)) |
121 return NULL; | 108 return NULL; |
122 | 109 |
123 InterceptorList::const_iterator i; | |
124 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | |
125 URLRequestJob* job = (*i)->MaybeInterceptRedirect(request, | |
126 network_delegate, | |
127 location); | |
128 if (job) | |
129 return job; | |
130 } | |
131 | |
132 URLRequestJob* job = | 110 URLRequestJob* job = |
133 request->context()->job_factory()->MaybeInterceptRedirect( | 111 request->context()->job_factory()->MaybeInterceptRedirect( |
134 request, network_delegate, location); | 112 request, network_delegate, location); |
135 if (job) | 113 if (job) |
136 return job; | 114 return job; |
137 | 115 |
138 return NULL; | 116 return NULL; |
139 } | 117 } |
140 | 118 |
141 URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( | 119 URLRequestJob* URLRequestJobManager::MaybeInterceptResponse( |
142 URLRequest* request, NetworkDelegate* network_delegate) const { | 120 URLRequest* request, NetworkDelegate* network_delegate) const { |
143 DCHECK(IsAllowedThread()); | 121 DCHECK(IsAllowedThread()); |
144 if (!request->url().is_valid() || | 122 if (!request->url().is_valid() || |
145 request->load_flags() & LOAD_DISABLE_INTERCEPT || | |
146 request->status().status() == URLRequestStatus::CANCELED) { | 123 request->status().status() == URLRequestStatus::CANCELED) { |
147 return NULL; | 124 return NULL; |
148 } | 125 } |
149 | 126 |
150 const URLRequestJobFactory* job_factory = NULL; | 127 const URLRequestJobFactory* job_factory = NULL; |
151 job_factory = request->context()->job_factory(); | 128 job_factory = request->context()->job_factory(); |
152 | 129 |
153 const std::string& scheme = request->url().scheme(); // already lowercase | 130 const std::string& scheme = request->url().scheme(); // already lowercase |
154 if (!job_factory->IsHandledProtocol(scheme)) | 131 if (!job_factory->IsHandledProtocol(scheme)) |
155 return NULL; | 132 return NULL; |
156 | 133 |
157 InterceptorList::const_iterator i; | |
158 for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { | |
159 URLRequestJob* job = (*i)->MaybeInterceptResponse(request, | |
160 network_delegate); | |
161 if (job) | |
162 return job; | |
163 } | |
164 | |
165 URLRequestJob* job = | 134 URLRequestJob* job = |
166 request->context()->job_factory()->MaybeInterceptResponse( | 135 request->context()->job_factory()->MaybeInterceptResponse( |
167 request, network_delegate); | 136 request, network_delegate); |
168 if (job) | 137 if (job) |
169 return job; | 138 return job; |
170 | 139 |
171 return NULL; | 140 return NULL; |
172 } | 141 } |
173 | 142 |
174 // static | 143 // static |
175 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) { | 144 bool URLRequestJobManager::SupportsScheme(const std::string& scheme) { |
176 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { | 145 for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) { |
177 if (LowerCaseEqualsASCII(scheme, kBuiltinFactories[i].scheme)) | 146 if (LowerCaseEqualsASCII(scheme, kBuiltinFactories[i].scheme)) |
178 return true; | 147 return true; |
179 } | 148 } |
180 | 149 |
181 return false; | 150 return false; |
182 } | 151 } |
183 | 152 |
184 void URLRequestJobManager::RegisterRequestInterceptor( | |
185 URLRequest::Interceptor* interceptor) { | |
186 DCHECK(IsAllowedThread()); | |
187 | |
188 base::AutoLock locked(lock_); | |
189 | |
190 DCHECK(std::find(interceptors_.begin(), interceptors_.end(), interceptor) == | |
191 interceptors_.end()); | |
192 interceptors_.push_back(interceptor); | |
193 } | |
194 | |
195 void URLRequestJobManager::UnregisterRequestInterceptor( | |
196 URLRequest::Interceptor* interceptor) { | |
197 DCHECK(IsAllowedThread()); | |
198 | |
199 base::AutoLock locked(lock_); | |
200 | |
201 InterceptorList::iterator i = | |
202 std::find(interceptors_.begin(), interceptors_.end(), interceptor); | |
203 DCHECK(i != interceptors_.end()); | |
204 interceptors_.erase(i); | |
205 } | |
206 | |
207 URLRequestJobManager::URLRequestJobManager() | 153 URLRequestJobManager::URLRequestJobManager() |
208 : allowed_thread_(0), | 154 : allowed_thread_(0), |
209 allowed_thread_initialized_(false) { | 155 allowed_thread_initialized_(false) { |
210 } | 156 } |
211 | 157 |
212 URLRequestJobManager::~URLRequestJobManager() {} | 158 URLRequestJobManager::~URLRequestJobManager() {} |
213 | 159 |
214 } // namespace net | 160 } // namespace net |
OLD | NEW |