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 "content/browser/loader/resource_loader.h" | 5 #include "content/browser/loader/resource_loader.h" |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 } | 57 } |
58 | 58 |
59 // Returns the number of calls to GetClientCerts(). | 59 // Returns the number of calls to GetClientCerts(). |
60 int request_count() { | 60 int request_count() { |
61 return request_count_; | 61 return request_count_; |
62 } | 62 } |
63 | 63 |
64 // net::ClientCertStore: | 64 // net::ClientCertStore: |
65 virtual void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, | 65 virtual void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, |
66 net::CertificateList* selected_certs, | 66 net::CertificateList* selected_certs, |
67 const base::Closure& callback) OVERRIDE { | 67 const base::Closure& callback) override { |
68 ++request_count_; | 68 ++request_count_; |
69 requested_authorities_ = cert_request_info.cert_authorities; | 69 requested_authorities_ = cert_request_info.cert_authorities; |
70 *selected_certs = response_; | 70 *selected_certs = response_; |
71 callback.Run(); | 71 callback.Run(); |
72 } | 72 } |
73 | 73 |
74 private: | 74 private: |
75 const net::CertificateList response_; | 75 const net::CertificateList response_; |
76 int request_count_; | 76 int request_count_; |
77 std::vector<std::string> requested_authorities_; | 77 std::vector<std::string> requested_authorities_; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 return received_response_completed_; | 120 return received_response_completed_; |
121 } | 121 } |
122 const net::URLRequestStatus& status() const { return status_; } | 122 const net::URLRequestStatus& status() const { return status_; } |
123 int total_bytes_downloaded() const { return total_bytes_downloaded_; } | 123 int total_bytes_downloaded() const { return total_bytes_downloaded_; } |
124 | 124 |
125 void Resume() { | 125 void Resume() { |
126 controller()->Resume(); | 126 controller()->Resume(); |
127 } | 127 } |
128 | 128 |
129 // ResourceHandler implementation: | 129 // ResourceHandler implementation: |
130 virtual bool OnUploadProgress(uint64 position, uint64 size) OVERRIDE { | 130 virtual bool OnUploadProgress(uint64 position, uint64 size) override { |
131 NOTREACHED(); | 131 NOTREACHED(); |
132 return true; | 132 return true; |
133 } | 133 } |
134 | 134 |
135 virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | 135 virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info, |
136 ResourceResponse* response, | 136 ResourceResponse* response, |
137 bool* defer) OVERRIDE { | 137 bool* defer) override { |
138 NOTREACHED(); | 138 NOTREACHED(); |
139 return true; | 139 return true; |
140 } | 140 } |
141 | 141 |
142 virtual bool OnResponseStarted(ResourceResponse* response, | 142 virtual bool OnResponseStarted(ResourceResponse* response, |
143 bool* defer) OVERRIDE { | 143 bool* defer) override { |
144 EXPECT_FALSE(response_.get()); | 144 EXPECT_FALSE(response_.get()); |
145 response_ = response; | 145 response_ = response; |
146 return true; | 146 return true; |
147 } | 147 } |
148 | 148 |
149 virtual bool OnWillStart(const GURL& url, bool* defer) OVERRIDE { | 149 virtual bool OnWillStart(const GURL& url, bool* defer) override { |
150 EXPECT_TRUE(start_url_.is_empty()); | 150 EXPECT_TRUE(start_url_.is_empty()); |
151 start_url_ = url; | 151 start_url_ = url; |
152 *defer = defer_request_on_will_start_; | 152 *defer = defer_request_on_will_start_; |
153 return true; | 153 return true; |
154 } | 154 } |
155 | 155 |
156 virtual bool OnBeforeNetworkStart(const GURL& url, bool* defer) OVERRIDE { | 156 virtual bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { |
157 return true; | 157 return true; |
158 } | 158 } |
159 | 159 |
160 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 160 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
161 int* buf_size, | 161 int* buf_size, |
162 int min_size) OVERRIDE { | 162 int min_size) override { |
163 EXPECT_TRUE(expect_reads_); | 163 EXPECT_TRUE(expect_reads_); |
164 EXPECT_FALSE(received_on_will_read_); | 164 EXPECT_FALSE(received_on_will_read_); |
165 EXPECT_FALSE(received_eof_); | 165 EXPECT_FALSE(received_eof_); |
166 EXPECT_FALSE(received_response_completed_); | 166 EXPECT_FALSE(received_response_completed_); |
167 | 167 |
168 *buf = read_buffer_; | 168 *buf = read_buffer_; |
169 *buf_size = kReadBufSize; | 169 *buf_size = kReadBufSize; |
170 received_on_will_read_ = true; | 170 received_on_will_read_ = true; |
171 return true; | 171 return true; |
172 } | 172 } |
173 | 173 |
174 virtual bool OnReadCompleted(int bytes_read, bool* defer) OVERRIDE { | 174 virtual bool OnReadCompleted(int bytes_read, bool* defer) override { |
175 EXPECT_TRUE(received_on_will_read_); | 175 EXPECT_TRUE(received_on_will_read_); |
176 EXPECT_TRUE(expect_reads_); | 176 EXPECT_TRUE(expect_reads_); |
177 EXPECT_FALSE(received_response_completed_); | 177 EXPECT_FALSE(received_response_completed_); |
178 | 178 |
179 if (bytes_read == 0) { | 179 if (bytes_read == 0) { |
180 received_eof_ = true; | 180 received_eof_ = true; |
181 if (defer_eof_) { | 181 if (defer_eof_) { |
182 defer_eof_ = false; | 182 defer_eof_ = false; |
183 *defer = true; | 183 *defer = true; |
184 } | 184 } |
185 } | 185 } |
186 | 186 |
187 // Need another OnWillRead() call before seeing an OnReadCompleted(). | 187 // Need another OnWillRead() call before seeing an OnReadCompleted(). |
188 received_on_will_read_ = false; | 188 received_on_will_read_ = false; |
189 | 189 |
190 return !cancel_on_read_completed_; | 190 return !cancel_on_read_completed_; |
191 } | 191 } |
192 | 192 |
193 virtual void OnResponseCompleted(const net::URLRequestStatus& status, | 193 virtual void OnResponseCompleted(const net::URLRequestStatus& status, |
194 const std::string& security_info, | 194 const std::string& security_info, |
195 bool* defer) OVERRIDE { | 195 bool* defer) override { |
196 EXPECT_FALSE(received_response_completed_); | 196 EXPECT_FALSE(received_response_completed_); |
197 if (status.is_success() && expect_reads_) | 197 if (status.is_success() && expect_reads_) |
198 EXPECT_TRUE(received_eof_); | 198 EXPECT_TRUE(received_eof_); |
199 | 199 |
200 received_response_completed_ = true; | 200 received_response_completed_ = true; |
201 status_ = status; | 201 status_ = status; |
202 } | 202 } |
203 | 203 |
204 virtual void OnDataDownloaded(int bytes_downloaded) OVERRIDE { | 204 virtual void OnDataDownloaded(int bytes_downloaded) override { |
205 EXPECT_FALSE(expect_reads_); | 205 EXPECT_FALSE(expect_reads_); |
206 total_bytes_downloaded_ += bytes_downloaded; | 206 total_bytes_downloaded_ += bytes_downloaded; |
207 } | 207 } |
208 | 208 |
209 private: | 209 private: |
210 scoped_refptr<net::IOBuffer> read_buffer_; | 210 scoped_refptr<net::IOBuffer> read_buffer_; |
211 | 211 |
212 bool defer_request_on_will_start_; | 212 bool defer_request_on_will_start_; |
213 bool expect_reads_; | 213 bool expect_reads_; |
214 bool cancel_on_read_completed_; | 214 bool cancel_on_read_completed_; |
(...skipping 12 matching lines...) Expand all Loading... |
227 // records the arguments of the most recent call for later inspection. | 227 // records the arguments of the most recent call for later inspection. |
228 class SelectCertificateBrowserClient : public TestContentBrowserClient { | 228 class SelectCertificateBrowserClient : public TestContentBrowserClient { |
229 public: | 229 public: |
230 SelectCertificateBrowserClient() : call_count_(0) {} | 230 SelectCertificateBrowserClient() : call_count_(0) {} |
231 | 231 |
232 virtual void SelectClientCertificate( | 232 virtual void SelectClientCertificate( |
233 int render_process_id, | 233 int render_process_id, |
234 int render_view_id, | 234 int render_view_id, |
235 const net::HttpNetworkSession* network_session, | 235 const net::HttpNetworkSession* network_session, |
236 net::SSLCertRequestInfo* cert_request_info, | 236 net::SSLCertRequestInfo* cert_request_info, |
237 const base::Callback<void(net::X509Certificate*)>& callback) OVERRIDE { | 237 const base::Callback<void(net::X509Certificate*)>& callback) override { |
238 ++call_count_; | 238 ++call_count_; |
239 passed_certs_ = cert_request_info->client_certs; | 239 passed_certs_ = cert_request_info->client_certs; |
240 } | 240 } |
241 | 241 |
242 int call_count() { | 242 int call_count() { |
243 return call_count_; | 243 return call_count_; |
244 } | 244 } |
245 | 245 |
246 net::CertificateList passed_certs() { | 246 net::CertificateList passed_certs() { |
247 return passed_certs_; | 247 return passed_certs_; |
248 } | 248 } |
249 | 249 |
250 private: | 250 private: |
251 net::CertificateList passed_certs_; | 251 net::CertificateList passed_certs_; |
252 int call_count_; | 252 int call_count_; |
253 }; | 253 }; |
254 | 254 |
255 class ResourceContextStub : public MockResourceContext { | 255 class ResourceContextStub : public MockResourceContext { |
256 public: | 256 public: |
257 explicit ResourceContextStub(net::URLRequestContext* test_request_context) | 257 explicit ResourceContextStub(net::URLRequestContext* test_request_context) |
258 : MockResourceContext(test_request_context) {} | 258 : MockResourceContext(test_request_context) {} |
259 | 259 |
260 virtual scoped_ptr<net::ClientCertStore> CreateClientCertStore() OVERRIDE { | 260 virtual scoped_ptr<net::ClientCertStore> CreateClientCertStore() override { |
261 return dummy_cert_store_.Pass(); | 261 return dummy_cert_store_.Pass(); |
262 } | 262 } |
263 | 263 |
264 void SetClientCertStore(scoped_ptr<net::ClientCertStore> store) { | 264 void SetClientCertStore(scoped_ptr<net::ClientCertStore> store) { |
265 dummy_cert_store_ = store.Pass(); | 265 dummy_cert_store_ = store.Pass(); |
266 } | 266 } |
267 | 267 |
268 private: | 268 private: |
269 scoped_ptr<net::ClientCertStore> dummy_cert_store_; | 269 scoped_ptr<net::ClientCertStore> dummy_cert_store_; |
270 }; | 270 }; |
(...skipping 30 matching lines...) Expand all Loading... |
301 std::string test_data() const { | 301 std::string test_data() const { |
302 return net::URLRequestTestJob::test_data_1(); | 302 return net::URLRequestTestJob::test_data_1(); |
303 } | 303 } |
304 | 304 |
305 virtual scoped_ptr<ResourceHandler> WrapResourceHandler( | 305 virtual scoped_ptr<ResourceHandler> WrapResourceHandler( |
306 scoped_ptr<ResourceHandlerStub> leaf_handler, | 306 scoped_ptr<ResourceHandlerStub> leaf_handler, |
307 net::URLRequest* request) { | 307 net::URLRequest* request) { |
308 return leaf_handler.PassAs<ResourceHandler>(); | 308 return leaf_handler.PassAs<ResourceHandler>(); |
309 } | 309 } |
310 | 310 |
311 virtual void SetUp() OVERRIDE { | 311 virtual void SetUp() override { |
312 const int kRenderProcessId = 1; | 312 const int kRenderProcessId = 1; |
313 const int kRenderViewId = 2; | 313 const int kRenderViewId = 2; |
314 | 314 |
315 scoped_ptr<net::URLRequest> request( | 315 scoped_ptr<net::URLRequest> request( |
316 resource_context_.GetRequestContext()->CreateRequest( | 316 resource_context_.GetRequestContext()->CreateRequest( |
317 test_url(), | 317 test_url(), |
318 net::DEFAULT_PRIORITY, | 318 net::DEFAULT_PRIORITY, |
319 NULL /* delegate */, | 319 NULL /* delegate */, |
320 NULL /* cookie_store */)); | 320 NULL /* cookie_store */)); |
321 raw_ptr_to_request_ = request.get(); | 321 raw_ptr_to_request_ = request.get(); |
322 ResourceRequestInfo::AllocateForTesting(request.get(), | 322 ResourceRequestInfo::AllocateForTesting(request.get(), |
323 RESOURCE_TYPE_MAIN_FRAME, | 323 RESOURCE_TYPE_MAIN_FRAME, |
324 &resource_context_, | 324 &resource_context_, |
325 kRenderProcessId, | 325 kRenderProcessId, |
326 kRenderViewId, | 326 kRenderViewId, |
327 MSG_ROUTING_NONE, | 327 MSG_ROUTING_NONE, |
328 false); | 328 false); |
329 scoped_ptr<ResourceHandlerStub> resource_handler( | 329 scoped_ptr<ResourceHandlerStub> resource_handler( |
330 new ResourceHandlerStub(request.get())); | 330 new ResourceHandlerStub(request.get())); |
331 raw_ptr_resource_handler_ = resource_handler.get(); | 331 raw_ptr_resource_handler_ = resource_handler.get(); |
332 loader_.reset(new ResourceLoader( | 332 loader_.reset(new ResourceLoader( |
333 request.Pass(), | 333 request.Pass(), |
334 WrapResourceHandler(resource_handler.Pass(), raw_ptr_to_request_), | 334 WrapResourceHandler(resource_handler.Pass(), raw_ptr_to_request_), |
335 this)); | 335 this)); |
336 } | 336 } |
337 | 337 |
338 // ResourceLoaderDelegate: | 338 // ResourceLoaderDelegate: |
339 virtual ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( | 339 virtual ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( |
340 ResourceLoader* loader, | 340 ResourceLoader* loader, |
341 net::AuthChallengeInfo* auth_info) OVERRIDE { | 341 net::AuthChallengeInfo* auth_info) override { |
342 return NULL; | 342 return NULL; |
343 } | 343 } |
344 virtual bool HandleExternalProtocol(ResourceLoader* loader, | 344 virtual bool HandleExternalProtocol(ResourceLoader* loader, |
345 const GURL& url) OVERRIDE { | 345 const GURL& url) override { |
346 return false; | 346 return false; |
347 } | 347 } |
348 virtual void DidStartRequest(ResourceLoader* loader) OVERRIDE {} | 348 virtual void DidStartRequest(ResourceLoader* loader) override {} |
349 virtual void DidReceiveRedirect(ResourceLoader* loader, | 349 virtual void DidReceiveRedirect(ResourceLoader* loader, |
350 const GURL& new_url) OVERRIDE {} | 350 const GURL& new_url) override {} |
351 virtual void DidReceiveResponse(ResourceLoader* loader) OVERRIDE {} | 351 virtual void DidReceiveResponse(ResourceLoader* loader) override {} |
352 virtual void DidFinishLoading(ResourceLoader* loader) OVERRIDE {} | 352 virtual void DidFinishLoading(ResourceLoader* loader) override {} |
353 | 353 |
354 content::TestBrowserThreadBundle thread_bundle_; | 354 content::TestBrowserThreadBundle thread_bundle_; |
355 | 355 |
356 net::URLRequestJobFactoryImpl job_factory_; | 356 net::URLRequestJobFactoryImpl job_factory_; |
357 net::TestURLRequestContext test_url_request_context_; | 357 net::TestURLRequestContext test_url_request_context_; |
358 ResourceContextStub resource_context_; | 358 ResourceContextStub resource_context_; |
359 | 359 |
360 // The ResourceLoader owns the URLRequest and the ResourceHandler. | 360 // The ResourceLoader owns the URLRequest and the ResourceHandler. |
361 ResourceHandlerStub* raw_ptr_resource_handler_; | 361 ResourceHandlerStub* raw_ptr_resource_handler_; |
362 net::URLRequest* raw_ptr_to_request_; | 362 net::URLRequest* raw_ptr_to_request_; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 } | 494 } |
495 | 495 |
496 void ReleaseLoader() { | 496 void ReleaseLoader() { |
497 file_stream_ = NULL; | 497 file_stream_ = NULL; |
498 deletable_file_ = NULL; | 498 deletable_file_ = NULL; |
499 loader_.reset(); | 499 loader_.reset(); |
500 } | 500 } |
501 | 501 |
502 virtual scoped_ptr<ResourceHandler> WrapResourceHandler( | 502 virtual scoped_ptr<ResourceHandler> WrapResourceHandler( |
503 scoped_ptr<ResourceHandlerStub> leaf_handler, | 503 scoped_ptr<ResourceHandlerStub> leaf_handler, |
504 net::URLRequest* request) OVERRIDE { | 504 net::URLRequest* request) override { |
505 leaf_handler->set_expect_reads(false); | 505 leaf_handler->set_expect_reads(false); |
506 | 506 |
507 // Make a temporary file. | 507 // Make a temporary file. |
508 CHECK(base::CreateTemporaryFile(&temp_path_)); | 508 CHECK(base::CreateTemporaryFile(&temp_path_)); |
509 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | | 509 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | |
510 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; | 510 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; |
511 base::File file(temp_path_, flags); | 511 base::File file(temp_path_, flags); |
512 CHECK(file.IsValid()); | 512 CHECK(file.IsValid()); |
513 | 513 |
514 // Create mock file streams and a ShareableFileReference. | 514 // Create mock file streams and a ShareableFileReference. |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); | 722 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); |
723 EXPECT_EQ(test_data(), contents); | 723 EXPECT_EQ(test_data(), contents); |
724 | 724 |
725 // Release the loader. The file should be gone now. | 725 // Release the loader. The file should be gone now. |
726 ReleaseLoader(); | 726 ReleaseLoader(); |
727 base::RunLoop().RunUntilIdle(); | 727 base::RunLoop().RunUntilIdle(); |
728 EXPECT_FALSE(base::PathExists(temp_path())); | 728 EXPECT_FALSE(base::PathExists(temp_path())); |
729 } | 729 } |
730 | 730 |
731 } // namespace content | 731 } // namespace content |
OLD | NEW |