OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/browser/policy/cloud/test_request_interceptor.h" | 5 #include "chrome/browser/policy/cloud/test_request_interceptor.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <queue> | 8 #include <queue> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
15 #include "content/test/net/url_request_mock_http_job.h" | 15 #include "content/test/net/url_request_mock_http_job.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 #include "net/base/upload_bytes_element_reader.h" | 17 #include "net/base/upload_bytes_element_reader.h" |
18 #include "net/base/upload_data_stream.h" | 18 #include "net/base/upload_data_stream.h" |
19 #include "net/base/upload_element_reader.h" | 19 #include "net/base/upload_element_reader.h" |
20 #include "net/url_request/url_request_error_job.h" | 20 #include "net/url_request/url_request_error_job.h" |
21 #include "net/url_request/url_request_filter.h" | 21 #include "net/url_request/url_request_filter.h" |
22 #include "net/url_request/url_request_job_factory.h" | 22 #include "net/url_request/url_request_interceptor.h" |
23 #include "net/url_request/url_request_test_job.h" | 23 #include "net/url_request/url_request_test_job.h" |
24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
25 | 25 |
26 namespace em = enterprise_management; | 26 namespace em = enterprise_management; |
27 | 27 |
28 namespace policy { | 28 namespace policy { |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 // Helper callback for jobs that should fail with a network |error|. | 32 // Helper callback for jobs that should fail with a network |error|. |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 136 |
137 static const char kGoodHeaders[] = | 137 static const char kGoodHeaders[] = |
138 "HTTP/1.1 200 OK\0" | 138 "HTTP/1.1 200 OK\0" |
139 "Content-type: application/protobuf\0" | 139 "Content-type: application/protobuf\0" |
140 "\0"; | 140 "\0"; |
141 std::string headers(kGoodHeaders, arraysize(kGoodHeaders)); | 141 std::string headers(kGoodHeaders, arraysize(kGoodHeaders)); |
142 return new net::URLRequestTestJob( | 142 return new net::URLRequestTestJob( |
143 request, network_delegate, headers, data, true); | 143 request, network_delegate, headers, data, true); |
144 } | 144 } |
145 | 145 |
| 146 void RegisterHttpInterceptor( |
| 147 const std::string& hostname, |
| 148 scoped_ptr<net::URLRequestInterceptor> interceptor) { |
| 149 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
| 150 "http", hostname, interceptor.Pass()); |
| 151 } |
| 152 |
146 } // namespace | 153 } // namespace |
147 | 154 |
148 class TestRequestInterceptor::Delegate | 155 class TestRequestInterceptor::Delegate : public net::URLRequestInterceptor { |
149 : public net::URLRequestJobFactory::ProtocolHandler { | |
150 public: | 156 public: |
151 Delegate(const std::string& hostname, | 157 Delegate(const std::string& hostname, |
152 scoped_refptr<base::SequencedTaskRunner> io_task_runner); | 158 scoped_refptr<base::SequencedTaskRunner> io_task_runner); |
153 virtual ~Delegate(); | 159 virtual ~Delegate(); |
154 | 160 |
155 // ProtocolHandler implementation: | 161 // net::URLRequestInterceptor implementation: |
156 virtual net::URLRequestJob* MaybeCreateJob( | 162 virtual net::URLRequestJob* MaybeInterceptRequest( |
157 net::URLRequest* request, | 163 net::URLRequest* request, |
158 net::NetworkDelegate* network_delegate) const OVERRIDE; | 164 net::NetworkDelegate* network_delegate) const OVERRIDE; |
159 | 165 |
160 void GetPendingSize(size_t* pending_size) const; | 166 void GetPendingSize(size_t* pending_size) const; |
161 void PushJobCallback(const JobCallback& callback); | 167 void PushJobCallback(const JobCallback& callback); |
162 | 168 |
163 private: | 169 private: |
164 const std::string hostname_; | 170 const std::string hostname_; |
165 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 171 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
166 | 172 |
167 // The queue of pending callbacks. 'mutable' because MaybeCreateJob() is a | 173 // The queue of pending callbacks. 'mutable' because MaybeCreateJob() is a |
168 // const method; it can't reenter though, because it runs exclusively on | 174 // const method; it can't reenter though, because it runs exclusively on |
169 // the IO thread. | 175 // the IO thread. |
170 mutable std::queue<JobCallback> pending_job_callbacks_; | 176 mutable std::queue<JobCallback> pending_job_callbacks_; |
171 }; | 177 }; |
172 | 178 |
173 TestRequestInterceptor::Delegate::Delegate( | 179 TestRequestInterceptor::Delegate::Delegate( |
174 const std::string& hostname, | 180 const std::string& hostname, |
175 scoped_refptr<base::SequencedTaskRunner> io_task_runner) | 181 scoped_refptr<base::SequencedTaskRunner> io_task_runner) |
176 : hostname_(hostname), io_task_runner_(io_task_runner) {} | 182 : hostname_(hostname), io_task_runner_(io_task_runner) {} |
177 | 183 |
178 TestRequestInterceptor::Delegate::~Delegate() {} | 184 TestRequestInterceptor::Delegate::~Delegate() {} |
179 | 185 |
180 net::URLRequestJob* TestRequestInterceptor::Delegate::MaybeCreateJob( | 186 net::URLRequestJob* TestRequestInterceptor::Delegate::MaybeInterceptRequest( |
181 net::URLRequest* request, | 187 net::URLRequest* request, |
182 net::NetworkDelegate* network_delegate) const { | 188 net::NetworkDelegate* network_delegate) const { |
183 CHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 189 CHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
184 | 190 |
185 if (request->url().host() != hostname_) { | 191 if (request->url().host() != hostname_) { |
186 // Reject requests to other servers. | 192 // Reject requests to other servers. |
187 return ErrorJobCallback( | 193 return ErrorJobCallback( |
188 net::ERR_CONNECTION_REFUSED, request, network_delegate); | 194 net::ERR_CONNECTION_REFUSED, request, network_delegate); |
189 } | 195 } |
190 | 196 |
(...skipping 17 matching lines...) Expand all Loading... |
208 const JobCallback& callback) { | 214 const JobCallback& callback) { |
209 CHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 215 CHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
210 pending_job_callbacks_.push(callback); | 216 pending_job_callbacks_.push(callback); |
211 } | 217 } |
212 | 218 |
213 TestRequestInterceptor::TestRequestInterceptor(const std::string& hostname, | 219 TestRequestInterceptor::TestRequestInterceptor(const std::string& hostname, |
214 scoped_refptr<base::SequencedTaskRunner> io_task_runner) | 220 scoped_refptr<base::SequencedTaskRunner> io_task_runner) |
215 : hostname_(hostname), | 221 : hostname_(hostname), |
216 io_task_runner_(io_task_runner) { | 222 io_task_runner_(io_task_runner) { |
217 delegate_ = new Delegate(hostname_, io_task_runner_); | 223 delegate_ = new Delegate(hostname_, io_task_runner_); |
218 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> handler(delegate_); | 224 scoped_ptr<net::URLRequestInterceptor> interceptor(delegate_); |
219 PostToIOAndWait( | 225 PostToIOAndWait( |
220 base::Bind(&net::URLRequestFilter::AddHostnameProtocolHandler, | 226 base::Bind(&RegisterHttpInterceptor, hostname_, |
221 base::Unretained(net::URLRequestFilter::GetInstance()), | 227 base::Passed(&interceptor))); |
222 "http", hostname_, base::Passed(&handler))); | |
223 } | 228 } |
224 | 229 |
225 TestRequestInterceptor::~TestRequestInterceptor() { | 230 TestRequestInterceptor::~TestRequestInterceptor() { |
226 // RemoveHostnameHandler() destroys the |delegate_|, which is owned by | 231 // RemoveHostnameHandler() destroys the |delegate_|, which is owned by |
227 // the URLRequestFilter. | 232 // the URLRequestFilter. |
228 delegate_ = NULL; | 233 delegate_ = NULL; |
229 PostToIOAndWait( | 234 PostToIOAndWait( |
230 base::Bind(&net::URLRequestFilter::RemoveHostnameHandler, | 235 base::Bind(&net::URLRequestFilter::RemoveHostnameHandler, |
231 base::Unretained(net::URLRequestFilter::GetInstance()), | 236 base::Unretained(net::URLRequestFilter::GetInstance()), |
232 "http", hostname_)); | 237 "http", hostname_)); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 FROM_HERE, | 282 FROM_HERE, |
278 base::Bind( | 283 base::Bind( |
279 base::IgnoreResult(&base::MessageLoopProxy::PostTask), | 284 base::IgnoreResult(&base::MessageLoopProxy::PostTask), |
280 base::MessageLoopProxy::current(), | 285 base::MessageLoopProxy::current(), |
281 FROM_HERE, | 286 FROM_HERE, |
282 run_loop.QuitClosure())); | 287 run_loop.QuitClosure())); |
283 run_loop.Run(); | 288 run_loop.Run(); |
284 } | 289 } |
285 | 290 |
286 } // namespace policy | 291 } // namespace policy |
OLD | NEW |