| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/public/test/test_download_request_handler.h" | 5 #include "content/public/test/test_download_request_handler.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | 7 #include <inttypes.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 BrowserThread::PostTaskAndReplyWithResult( | 631 BrowserThread::PostTaskAndReplyWithResult( |
| 632 BrowserThread::IO, FROM_HERE, | 632 BrowserThread::IO, FROM_HERE, |
| 633 base::Bind(&Interceptor::Register, url_, | 633 base::Bind(&Interceptor::Register, url_, |
| 634 base::SequencedTaskRunnerHandle::Get()), | 634 base::SequencedTaskRunnerHandle::Get()), |
| 635 base::Bind(&StoreValueAndInvokeClosure<base::WeakPtr<Interceptor>>, | 635 base::Bind(&StoreValueAndInvokeClosure<base::WeakPtr<Interceptor>>, |
| 636 run_loop.QuitClosure(), &interceptor_)); | 636 run_loop.QuitClosure(), &interceptor_)); |
| 637 run_loop.Run(); | 637 run_loop.Run(); |
| 638 } | 638 } |
| 639 | 639 |
| 640 void TestDownloadRequestHandler::StartServing(const Parameters& parameters) { | 640 void TestDownloadRequestHandler::StartServing(const Parameters& parameters) { |
| 641 DCHECK(CalledOnValidThread()); | 641 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 642 Interceptor::JobFactory job_factory = | 642 Interceptor::JobFactory job_factory = |
| 643 base::Bind(&PartialResponseJob::Factory, parameters); | 643 base::Bind(&PartialResponseJob::Factory, parameters); |
| 644 // Interceptor, if valid, is already registered and serving requests. We just | 644 // Interceptor, if valid, is already registered and serving requests. We just |
| 645 // need to set the correct job factory for it to start serving using the new | 645 // need to set the correct job factory for it to start serving using the new |
| 646 // parameters. | 646 // parameters. |
| 647 BrowserThread::PostTask( | 647 BrowserThread::PostTask( |
| 648 BrowserThread::IO, FROM_HERE, | 648 BrowserThread::IO, FROM_HERE, |
| 649 base::Bind(&Interceptor::SetJobFactory, interceptor_, job_factory)); | 649 base::Bind(&Interceptor::SetJobFactory, interceptor_, job_factory)); |
| 650 } | 650 } |
| 651 | 651 |
| 652 void TestDownloadRequestHandler::StartServingStaticResponse( | 652 void TestDownloadRequestHandler::StartServingStaticResponse( |
| 653 const base::StringPiece& headers) { | 653 const base::StringPiece& headers) { |
| 654 DCHECK(CalledOnValidThread()); | 654 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 655 Parameters parameters; | 655 Parameters parameters; |
| 656 parameters.on_start_handler = base::Bind( | 656 parameters.on_start_handler = base::Bind( |
| 657 &RespondToOnStartedCallbackWithStaticHeaders, headers.as_string()); | 657 &RespondToOnStartedCallbackWithStaticHeaders, headers.as_string()); |
| 658 StartServing(parameters); | 658 StartServing(parameters); |
| 659 } | 659 } |
| 660 | 660 |
| 661 // static | 661 // static |
| 662 void TestDownloadRequestHandler::GetPatternBytes(int seed, | 662 void TestDownloadRequestHandler::GetPatternBytes(int seed, |
| 663 int64_t starting_offset, | 663 int64_t starting_offset, |
| 664 int length, | 664 int length, |
| 665 char* buffer) { | 665 char* buffer) { |
| 666 int64_t seed_offset = starting_offset / sizeof(int64_t); | 666 int64_t seed_offset = starting_offset / sizeof(int64_t); |
| 667 int64_t first_byte_position = starting_offset % sizeof(int64_t); | 667 int64_t first_byte_position = starting_offset % sizeof(int64_t); |
| 668 while (length > 0) { | 668 while (length > 0) { |
| 669 uint64_t data = XorShift64StarWithIndex(seed, seed_offset); | 669 uint64_t data = XorShift64StarWithIndex(seed, seed_offset); |
| 670 int length_to_copy = | 670 int length_to_copy = |
| 671 std::min(length, static_cast<int>(sizeof(data) - first_byte_position)); | 671 std::min(length, static_cast<int>(sizeof(data) - first_byte_position)); |
| 672 memcpy(buffer, reinterpret_cast<char*>(&data) + first_byte_position, | 672 memcpy(buffer, reinterpret_cast<char*>(&data) + first_byte_position, |
| 673 length_to_copy); | 673 length_to_copy); |
| 674 buffer += length_to_copy; | 674 buffer += length_to_copy; |
| 675 length -= length_to_copy; | 675 length -= length_to_copy; |
| 676 ++seed_offset; | 676 ++seed_offset; |
| 677 first_byte_position = 0; | 677 first_byte_position = 0; |
| 678 } | 678 } |
| 679 } | 679 } |
| 680 | 680 |
| 681 TestDownloadRequestHandler::~TestDownloadRequestHandler() { | 681 TestDownloadRequestHandler::~TestDownloadRequestHandler() { |
| 682 DCHECK(CalledOnValidThread()); | 682 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 683 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 683 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 684 base::Bind(&Interceptor::Unregister, interceptor_)); | 684 base::Bind(&Interceptor::Unregister, interceptor_)); |
| 685 } | 685 } |
| 686 | 686 |
| 687 void TestDownloadRequestHandler::GetCompletedRequestInfo( | 687 void TestDownloadRequestHandler::GetCompletedRequestInfo( |
| 688 TestDownloadRequestHandler::CompletedRequests* requests) { | 688 TestDownloadRequestHandler::CompletedRequests* requests) { |
| 689 DCHECK(CalledOnValidThread()); | 689 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 690 base::RunLoop run_loop; | 690 base::RunLoop run_loop; |
| 691 BrowserThread::PostTaskAndReply( | 691 BrowserThread::PostTaskAndReply( |
| 692 BrowserThread::IO, FROM_HERE, | 692 BrowserThread::IO, FROM_HERE, |
| 693 base::Bind(&Interceptor::GetAndResetCompletedRequests, interceptor_, | 693 base::Bind(&Interceptor::GetAndResetCompletedRequests, interceptor_, |
| 694 requests), | 694 requests), |
| 695 run_loop.QuitClosure()); | 695 run_loop.QuitClosure()); |
| 696 run_loop.Run(); | 696 run_loop.Run(); |
| 697 } | 697 } |
| 698 | 698 |
| 699 } // namespace content | 699 } // namespace content |
| OLD | NEW |