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