| 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/location.h" | 9 #include "base/location.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 : ResourceHandler(request), | 249 : ResourceHandler(request), |
| 250 read_buffer_(new net::IOBuffer(kReadBufSize)), | 250 read_buffer_(new net::IOBuffer(kReadBufSize)), |
| 251 defer_request_on_will_start_(false), | 251 defer_request_on_will_start_(false), |
| 252 expect_reads_(true), | 252 expect_reads_(true), |
| 253 cancel_on_read_completed_(false), | 253 cancel_on_read_completed_(false), |
| 254 defer_eof_(false), | 254 defer_eof_(false), |
| 255 received_on_will_read_(false), | 255 received_on_will_read_(false), |
| 256 received_eof_(false), | 256 received_eof_(false), |
| 257 received_response_completed_(false), | 257 received_response_completed_(false), |
| 258 received_request_redirected_(false), | 258 received_request_redirected_(false), |
| 259 total_bytes_downloaded_(0), | 259 total_bytes_downloaded_(0) {} |
| 260 upload_position_(0) {} | |
| 261 | 260 |
| 262 // If true, defers the resource load in OnWillStart. | 261 // If true, defers the resource load in OnWillStart. |
| 263 void set_defer_request_on_will_start(bool defer_request_on_will_start) { | 262 void set_defer_request_on_will_start(bool defer_request_on_will_start) { |
| 264 defer_request_on_will_start_ = defer_request_on_will_start; | 263 defer_request_on_will_start_ = defer_request_on_will_start; |
| 265 } | 264 } |
| 266 | 265 |
| 267 // If true, expect OnWillRead / OnReadCompleted pairs for handling | 266 // If true, expect OnWillRead / OnReadCompleted pairs for handling |
| 268 // data. Otherwise, expect OnDataDownloaded. | 267 // data. Otherwise, expect OnDataDownloaded. |
| 269 void set_expect_reads(bool expect_reads) { expect_reads_ = expect_reads; } | 268 void set_expect_reads(bool expect_reads) { expect_reads_ = expect_reads; } |
| 270 | 269 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 287 bool received_request_redirected() const { | 286 bool received_request_redirected() const { |
| 288 return received_request_redirected_; | 287 return received_request_redirected_; |
| 289 } | 288 } |
| 290 const net::URLRequestStatus& status() const { return status_; } | 289 const net::URLRequestStatus& status() const { return status_; } |
| 291 int total_bytes_downloaded() const { return total_bytes_downloaded_; } | 290 int total_bytes_downloaded() const { return total_bytes_downloaded_; } |
| 292 | 291 |
| 293 void Resume() { | 292 void Resume() { |
| 294 controller()->Resume(); | 293 controller()->Resume(); |
| 295 } | 294 } |
| 296 | 295 |
| 297 // Waits until OnUploadProgress is called and returns the upload position. | |
| 298 uint64 WaitForUploadProgress() { | |
| 299 wait_for_progress_loop_.reset(new base::RunLoop()); | |
| 300 wait_for_progress_loop_->Run(); | |
| 301 wait_for_progress_loop_.reset(); | |
| 302 return upload_position_; | |
| 303 } | |
| 304 | |
| 305 // ResourceHandler implementation: | |
| 306 bool OnUploadProgress(uint64 position, uint64 size) override { | |
| 307 EXPECT_LE(position, size); | |
| 308 EXPECT_GT(position, upload_position_); | |
| 309 upload_position_ = position; | |
| 310 if (wait_for_progress_loop_) | |
| 311 wait_for_progress_loop_->Quit(); | |
| 312 return true; | |
| 313 } | |
| 314 | |
| 315 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | 296 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, |
| 316 ResourceResponse* response, | 297 ResourceResponse* response, |
| 317 bool* defer) override { | 298 bool* defer) override { |
| 318 redirect_response_ = response; | 299 redirect_response_ = response; |
| 319 received_request_redirected_ = true; | 300 received_request_redirected_ = true; |
| 320 return true; | 301 return true; |
| 321 } | 302 } |
| 322 | 303 |
| 323 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { | 304 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { |
| 324 EXPECT_FALSE(response_.get()); | 305 EXPECT_FALSE(response_.get()); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 GURL start_url_; | 378 GURL start_url_; |
| 398 scoped_refptr<ResourceResponse> response_; | 379 scoped_refptr<ResourceResponse> response_; |
| 399 scoped_refptr<ResourceResponse> redirect_response_; | 380 scoped_refptr<ResourceResponse> redirect_response_; |
| 400 bool received_on_will_read_; | 381 bool received_on_will_read_; |
| 401 bool received_eof_; | 382 bool received_eof_; |
| 402 bool received_response_completed_; | 383 bool received_response_completed_; |
| 403 bool received_request_redirected_; | 384 bool received_request_redirected_; |
| 404 net::URLRequestStatus status_; | 385 net::URLRequestStatus status_; |
| 405 int total_bytes_downloaded_; | 386 int total_bytes_downloaded_; |
| 406 scoped_ptr<base::RunLoop> wait_for_progress_loop_; | 387 scoped_ptr<base::RunLoop> wait_for_progress_loop_; |
| 407 uint64 upload_position_; | |
| 408 }; | 388 }; |
| 409 | 389 |
| 410 // Test browser client that captures calls to SelectClientCertificates and | 390 // Test browser client that captures calls to SelectClientCertificates and |
| 411 // records the arguments of the most recent call for later inspection. | 391 // records the arguments of the most recent call for later inspection. |
| 412 class SelectCertificateBrowserClient : public TestContentBrowserClient { | 392 class SelectCertificateBrowserClient : public TestContentBrowserClient { |
| 413 public: | 393 public: |
| 414 SelectCertificateBrowserClient() : call_count_(0) {} | 394 SelectCertificateBrowserClient() : call_count_(0) {} |
| 415 | 395 |
| 416 void SelectClientCertificate( | 396 void SelectClientCertificate( |
| 417 WebContents* web_contents, | 397 WebContents* web_contents, |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 raw_ptr_to_request_(NULL) { | 490 raw_ptr_to_request_(NULL) { |
| 511 test_url_request_context_.set_job_factory(&job_factory_); | 491 test_url_request_context_.set_job_factory(&job_factory_); |
| 512 } | 492 } |
| 513 | 493 |
| 514 GURL test_url() const { return net::URLRequestTestJob::test_url_1(); } | 494 GURL test_url() const { return net::URLRequestTestJob::test_url_1(); } |
| 515 | 495 |
| 516 std::string test_data() const { | 496 std::string test_data() const { |
| 517 return net::URLRequestTestJob::test_data_1(); | 497 return net::URLRequestTestJob::test_data_1(); |
| 518 } | 498 } |
| 519 | 499 |
| 520 // Waits until upload progress reaches |target_position| | |
| 521 void WaitForUploadProgress(uint64 target_position) { | |
| 522 while (true) { | |
| 523 uint64 position = raw_ptr_resource_handler_->WaitForUploadProgress(); | |
| 524 EXPECT_LE(position, target_position); | |
| 525 loader_->OnUploadProgressACK(); | |
| 526 if (position == target_position) | |
| 527 break; | |
| 528 } | |
| 529 } | |
| 530 | |
| 531 virtual scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> | 500 virtual scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> |
| 532 CreateProtocolHandler() { | 501 CreateProtocolHandler() { |
| 533 return net::URLRequestTestJob::CreateProtocolHandler(); | 502 return net::URLRequestTestJob::CreateProtocolHandler(); |
| 534 } | 503 } |
| 535 | 504 |
| 536 virtual scoped_ptr<ResourceHandler> WrapResourceHandler( | 505 virtual scoped_ptr<ResourceHandler> WrapResourceHandler( |
| 537 scoped_ptr<ResourceHandlerStub> leaf_handler, | 506 scoped_ptr<ResourceHandlerStub> leaf_handler, |
| 538 net::URLRequest* request) { | 507 net::URLRequest* request) { |
| 539 return leaf_handler.Pass(); | 508 return leaf_handler.Pass(); |
| 540 } | 509 } |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); | 794 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); |
| 826 | 795 |
| 827 raw_ptr_resource_handler_->Resume(); | 796 raw_ptr_resource_handler_->Resume(); |
| 828 base::RunLoop().RunUntilIdle(); | 797 base::RunLoop().RunUntilIdle(); |
| 829 | 798 |
| 830 EXPECT_TRUE(raw_ptr_resource_handler_->received_response_completed()); | 799 EXPECT_TRUE(raw_ptr_resource_handler_->received_response_completed()); |
| 831 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 800 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 832 raw_ptr_resource_handler_->status().status()); | 801 raw_ptr_resource_handler_->status().status()); |
| 833 } | 802 } |
| 834 | 803 |
| 835 // Tests that progress is reported correctly while uploading. | |
| 836 // TODO(andresantoso): Add test for the redirect case. | |
| 837 TEST_F(ResourceLoaderTest, UploadProgress) { | |
| 838 // Set up a test server. | |
| 839 net::test_server::EmbeddedTestServer server; | |
| 840 ASSERT_TRUE(server.InitializeAndWaitUntilReady()); | |
| 841 base::FilePath path; | |
| 842 PathService::Get(content::DIR_TEST_DATA, &path); | |
| 843 server.ServeFilesFromDirectory(path); | |
| 844 | |
| 845 scoped_ptr<net::URLRequest> request( | |
| 846 resource_context_.GetRequestContext()->CreateRequest( | |
| 847 server.GetURL("/title1.html"), | |
| 848 net::DEFAULT_PRIORITY, | |
| 849 nullptr /* delegate */)); | |
| 850 | |
| 851 // Start an upload. | |
| 852 auto stream = new NonChunkedUploadDataStream(10); | |
| 853 request->set_upload(make_scoped_ptr(stream)); | |
| 854 | |
| 855 SetUpResourceLoader(request.Pass()); | |
| 856 loader_->StartRequest(); | |
| 857 | |
| 858 stream->AppendData("xx"); | |
| 859 WaitForUploadProgress(2); | |
| 860 | |
| 861 stream->AppendData("yyy"); | |
| 862 WaitForUploadProgress(5); | |
| 863 | |
| 864 stream->AppendData("zzzzz"); | |
| 865 WaitForUploadProgress(10); | |
| 866 } | |
| 867 | |
| 868 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest { | 804 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest { |
| 869 public: | 805 public: |
| 870 ResourceLoaderRedirectToFileTest() | 806 ResourceLoaderRedirectToFileTest() |
| 871 : file_stream_(NULL), | 807 : file_stream_(NULL), |
| 872 redirect_to_file_resource_handler_(NULL) { | 808 redirect_to_file_resource_handler_(NULL) { |
| 873 } | 809 } |
| 874 | 810 |
| 875 base::FilePath temp_path() const { return temp_path_; } | 811 base::FilePath temp_path() const { return temp_path_; } |
| 876 ShareableFileReference* deletable_file() const { | 812 ShareableFileReference* deletable_file() const { |
| 877 return deletable_file_.get(); | 813 return deletable_file_.get(); |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1188 ASSERT_TRUE( | 1124 ASSERT_TRUE( |
| 1189 CertStore::GetInstance()->RetrieveCert(deserialized.cert_id, &cert)); | 1125 CertStore::GetInstance()->RetrieveCert(deserialized.cert_id, &cert)); |
| 1190 EXPECT_TRUE(cert->Equals(GetTestCert().get())); | 1126 EXPECT_TRUE(cert->Equals(GetTestCert().get())); |
| 1191 | 1127 |
| 1192 EXPECT_EQ(kTestCertError, deserialized.cert_status); | 1128 EXPECT_EQ(kTestCertError, deserialized.cert_status); |
| 1193 EXPECT_EQ(kTestConnectionStatus, deserialized.connection_status); | 1129 EXPECT_EQ(kTestConnectionStatus, deserialized.connection_status); |
| 1194 EXPECT_EQ(kTestSecurityBits, deserialized.security_bits); | 1130 EXPECT_EQ(kTestSecurityBits, deserialized.security_bits); |
| 1195 } | 1131 } |
| 1196 | 1132 |
| 1197 } // namespace content | 1133 } // namespace content |
| OLD | NEW |