| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser_test_utils.h" | 5 #include "content/public/test/browser_test_utils.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <tuple> | 8 #include <tuple> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 return std::move(http_response); | 354 return std::move(http_response); |
| 355 } | 355 } |
| 356 | 356 |
| 357 // Helper class used by the TestNavigationManager to pause navigations. | 357 // Helper class used by the TestNavigationManager to pause navigations. |
| 358 // Note: the throttle should be added to the *end* of the list of throttles, | 358 // Note: the throttle should be added to the *end* of the list of throttles, |
| 359 // so all NavigationThrottles that should be attached observe the | 359 // so all NavigationThrottles that should be attached observe the |
| 360 // WillStartRequest callback. RegisterThrottleForTesting has this behavior. | 360 // WillStartRequest callback. RegisterThrottleForTesting has this behavior. |
| 361 class TestNavigationManagerThrottle : public NavigationThrottle { | 361 class TestNavigationManagerThrottle : public NavigationThrottle { |
| 362 public: | 362 public: |
| 363 TestNavigationManagerThrottle(NavigationHandle* handle, | 363 TestNavigationManagerThrottle(NavigationHandle* handle, |
| 364 base::Closure on_will_start_request_closure) | 364 base::Closure on_will_start_request_closure, |
| 365 base::Closure on_will_process_response_closure) |
| 365 : NavigationThrottle(handle), | 366 : NavigationThrottle(handle), |
| 366 on_will_start_request_closure_(on_will_start_request_closure) {} | 367 on_will_start_request_closure_(on_will_start_request_closure), |
| 368 on_will_process_response_closure_(on_will_process_response_closure) {} |
| 367 ~TestNavigationManagerThrottle() override {} | 369 ~TestNavigationManagerThrottle() override {} |
| 368 | 370 |
| 369 private: | 371 private: |
| 370 // NavigationThrottle: | 372 // NavigationThrottle: |
| 371 NavigationThrottle::ThrottleCheckResult WillStartRequest() override { | 373 NavigationThrottle::ThrottleCheckResult WillStartRequest() override { |
| 372 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 374 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 373 on_will_start_request_closure_); | 375 on_will_start_request_closure_); |
| 374 return NavigationThrottle::DEFER; | 376 return NavigationThrottle::DEFER; |
| 375 } | 377 } |
| 376 | 378 |
| 379 NavigationThrottle::ThrottleCheckResult WillProcessResponse() override { |
| 380 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 381 on_will_process_response_closure_); |
| 382 return NavigationThrottle::DEFER; |
| 383 } |
| 384 |
| 377 base::Closure on_will_start_request_closure_; | 385 base::Closure on_will_start_request_closure_; |
| 386 base::Closure on_will_process_response_closure_; |
| 378 }; | 387 }; |
| 379 | 388 |
| 380 bool HasGzipHeader(const base::RefCountedMemory& maybe_gzipped) { | 389 bool HasGzipHeader(const base::RefCountedMemory& maybe_gzipped) { |
| 381 net::GZipHeader header; | 390 net::GZipHeader header; |
| 382 net::GZipHeader::Status header_status = net::GZipHeader::INCOMPLETE_HEADER; | 391 net::GZipHeader::Status header_status = net::GZipHeader::INCOMPLETE_HEADER; |
| 383 const char* header_end = nullptr; | 392 const char* header_end = nullptr; |
| 384 while (header_status == net::GZipHeader::INCOMPLETE_HEADER) { | 393 while (header_status == net::GZipHeader::INCOMPLETE_HEADER) { |
| 385 header_status = header.ReadMore(maybe_gzipped.front_as<char>(), | 394 header_status = header.ReadMore(maybe_gzipped.front_as<char>(), |
| 386 maybe_gzipped.size(), | 395 maybe_gzipped.size(), |
| 387 &header_end); | 396 &header_end); |
| (...skipping 1241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1629 FrameFocusedObserver::~FrameFocusedObserver() {} | 1638 FrameFocusedObserver::~FrameFocusedObserver() {} |
| 1630 | 1639 |
| 1631 void FrameFocusedObserver::Wait() { | 1640 void FrameFocusedObserver::Wait() { |
| 1632 impl_->Run(); | 1641 impl_->Run(); |
| 1633 } | 1642 } |
| 1634 | 1643 |
| 1635 TestNavigationManager::TestNavigationManager(WebContents* web_contents, | 1644 TestNavigationManager::TestNavigationManager(WebContents* web_contents, |
| 1636 const GURL& url) | 1645 const GURL& url) |
| 1637 : WebContentsObserver(web_contents), | 1646 : WebContentsObserver(web_contents), |
| 1638 url_(url), | 1647 url_(url), |
| 1639 navigation_paused_(false), | 1648 navigation_paused_in_will_start_(false), |
| 1649 navigation_paused_in_will_process_response_(false), |
| 1640 handle_(nullptr), | 1650 handle_(nullptr), |
| 1641 handled_navigation_(false), | 1651 handled_navigation_(false), |
| 1642 weak_factory_(this) {} | 1652 weak_factory_(this) {} |
| 1643 | 1653 |
| 1644 TestNavigationManager::~TestNavigationManager() { | 1654 TestNavigationManager::~TestNavigationManager() { |
| 1645 ResumeNavigation(); | 1655 ResumeNavigation(); |
| 1646 } | 1656 } |
| 1647 | 1657 |
| 1648 bool TestNavigationManager::WaitForWillStartRequest() { | 1658 bool TestNavigationManager::WaitForWillStartRequest() { |
| 1649 DCHECK(!did_finish_loop_runner_); | 1659 DCHECK(!did_finish_loop_runner_); |
| 1650 if (!handle_ && handled_navigation_) | 1660 if (!handle_ && handled_navigation_) |
| 1651 return true; | 1661 return true; |
| 1652 if (navigation_paused_) | 1662 if (navigation_paused_in_will_start_) |
| 1653 return true; | 1663 return true; |
| 1664 DCHECK(!navigation_paused_in_will_process_response_); |
| 1654 will_start_loop_runner_ = new MessageLoopRunner(); | 1665 will_start_loop_runner_ = new MessageLoopRunner(); |
| 1655 will_start_loop_runner_->Run(); | 1666 will_start_loop_runner_->Run(); |
| 1656 will_start_loop_runner_ = nullptr; | 1667 will_start_loop_runner_ = nullptr; |
| 1657 | 1668 |
| 1658 // This will only be false if DidFinishNavigation is called before | 1669 // This will only be false if DidFinishNavigation is called before |
| 1659 // OnWillStartRequest, which could occur if a throttle cancels the navigation | 1670 // OnWillStartRequest, which could occur if a throttle cancels the navigation |
| 1660 // before the TestNavigationManagerThrottle's method is called. | 1671 // before the TestNavigationManagerThrottle's method is called. |
| 1661 return !handled_navigation_; | 1672 return !handled_navigation_; |
| 1662 } | 1673 } |
| 1663 | 1674 |
| 1675 bool TestNavigationManager::WaitForWillProcessResponse() { |
| 1676 DCHECK(!did_finish_loop_runner_); |
| 1677 if (!handle_ && handled_navigation_) |
| 1678 return true; |
| 1679 if (navigation_paused_in_will_process_response_) |
| 1680 return true; |
| 1681 // Ensure the navigation is resumed if the manager paused it previously. |
| 1682 if (navigation_paused_in_will_start_) |
| 1683 ResumeNavigation(); |
| 1684 will_process_response_loop_runner_ = new MessageLoopRunner(); |
| 1685 will_process_response_loop_runner_->Run(); |
| 1686 will_process_response_loop_runner_ = nullptr; |
| 1687 |
| 1688 // This will only be false if DidFinishNavigation is called before |
| 1689 // OnWillProcessResponse. |
| 1690 return !handled_navigation_; |
| 1691 } |
| 1692 |
| 1664 void TestNavigationManager::WaitForNavigationFinished() { | 1693 void TestNavigationManager::WaitForNavigationFinished() { |
| 1665 DCHECK(!will_start_loop_runner_); | 1694 DCHECK(!will_start_loop_runner_); |
| 1666 if (!handle_ && handled_navigation_) | 1695 if (!handle_ && handled_navigation_) |
| 1667 return; | 1696 return; |
| 1668 // Ensure the navigation is resumed if the manager paused it previously. | 1697 // Ensure the navigation is resumed if the manager paused it previously. |
| 1669 if (navigation_paused_) | 1698 if (navigation_paused_in_will_start_ || |
| 1699 navigation_paused_in_will_process_response_) { |
| 1670 ResumeNavigation(); | 1700 ResumeNavigation(); |
| 1701 } |
| 1671 did_finish_loop_runner_ = new MessageLoopRunner(); | 1702 did_finish_loop_runner_ = new MessageLoopRunner(); |
| 1672 did_finish_loop_runner_->Run(); | 1703 did_finish_loop_runner_->Run(); |
| 1673 did_finish_loop_runner_ = nullptr; | 1704 did_finish_loop_runner_ = nullptr; |
| 1674 } | 1705 } |
| 1675 | 1706 |
| 1676 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) { | 1707 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) { |
| 1677 if (!ShouldMonitorNavigation(handle)) | 1708 if (!ShouldMonitorNavigation(handle)) |
| 1678 return; | 1709 return; |
| 1679 | 1710 |
| 1680 handle_ = handle; | 1711 handle_ = handle; |
| 1681 std::unique_ptr<NavigationThrottle> throttle( | 1712 std::unique_ptr<NavigationThrottle> throttle( |
| 1682 new TestNavigationManagerThrottle( | 1713 new TestNavigationManagerThrottle( |
| 1683 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest, | 1714 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest, |
| 1684 weak_factory_.GetWeakPtr()))); | 1715 weak_factory_.GetWeakPtr()), |
| 1716 base::Bind(&TestNavigationManager::OnWillProcessResponse, |
| 1717 weak_factory_.GetWeakPtr()))); |
| 1685 handle_->RegisterThrottleForTesting(std::move(throttle)); | 1718 handle_->RegisterThrottleForTesting(std::move(throttle)); |
| 1686 } | 1719 } |
| 1687 | 1720 |
| 1688 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) { | 1721 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) { |
| 1689 if (handle != handle_) | 1722 if (handle != handle_) |
| 1690 return; | 1723 return; |
| 1691 handle_ = nullptr; | 1724 handle_ = nullptr; |
| 1692 handled_navigation_ = true; | 1725 handled_navigation_ = true; |
| 1693 navigation_paused_ = false; | 1726 navigation_paused_in_will_start_ = false; |
| 1727 navigation_paused_in_will_process_response_ = false; |
| 1694 | 1728 |
| 1695 // Resume any clients that are waiting for the end of the navigation. Note | 1729 // Resume any clients that are waiting for the end of the navigation. Note |
| 1696 // that |will_start_loop_runner_| can be running if the navigation was | 1730 // that |will_start_loop_runner_| can be running if the navigation was |
| 1697 // cancelled while it was deferred. | 1731 // cancelled while it was deferred. |
| 1698 if (did_finish_loop_runner_) | 1732 if (did_finish_loop_runner_) |
| 1699 did_finish_loop_runner_->Quit(); | 1733 did_finish_loop_runner_->Quit(); |
| 1700 if (will_start_loop_runner_) | 1734 if (will_start_loop_runner_) |
| 1701 will_start_loop_runner_->Quit(); | 1735 will_start_loop_runner_->Quit(); |
| 1736 if (will_process_response_loop_runner_) |
| 1737 will_process_response_loop_runner_->Quit(); |
| 1702 } | 1738 } |
| 1703 | 1739 |
| 1704 void TestNavigationManager::OnWillStartRequest() { | 1740 void TestNavigationManager::OnWillStartRequest() { |
| 1705 navigation_paused_ = true; | 1741 navigation_paused_in_will_start_ = true; |
| 1706 if (will_start_loop_runner_) | 1742 if (will_start_loop_runner_) |
| 1707 will_start_loop_runner_->Quit(); | 1743 will_start_loop_runner_->Quit(); |
| 1708 | 1744 |
| 1709 // If waiting for the navigation to finish, resume the navigation. | 1745 // If waiting for further events in the navigation, resume the navigation. |
| 1746 if (did_finish_loop_runner_ || will_process_response_loop_runner_) |
| 1747 ResumeNavigation(); |
| 1748 } |
| 1749 |
| 1750 void TestNavigationManager::OnWillProcessResponse() { |
| 1751 navigation_paused_in_will_process_response_ = true; |
| 1752 DCHECK(!will_start_loop_runner_); |
| 1753 if (will_process_response_loop_runner_) |
| 1754 will_process_response_loop_runner_->Quit(); |
| 1755 |
| 1756 // If waiting for further events in the navigation, resume the navigation. |
| 1710 if (did_finish_loop_runner_) | 1757 if (did_finish_loop_runner_) |
| 1711 ResumeNavigation(); | 1758 ResumeNavigation(); |
| 1712 } | 1759 } |
| 1713 | 1760 |
| 1714 void TestNavigationManager::ResumeNavigation() { | 1761 void TestNavigationManager::ResumeNavigation() { |
| 1715 if (!navigation_paused_ || !handle_) | 1762 if (!(navigation_paused_in_will_start_ || |
| 1763 navigation_paused_in_will_process_response_) || |
| 1764 !handle_) { |
| 1716 return; | 1765 return; |
| 1717 navigation_paused_ = false; | 1766 } |
| 1767 navigation_paused_in_will_start_ = false; |
| 1768 navigation_paused_in_will_process_response_ = false; |
| 1718 handle_->Resume(); | 1769 handle_->Resume(); |
| 1719 } | 1770 } |
| 1720 | 1771 |
| 1721 bool TestNavigationManager::ShouldMonitorNavigation(NavigationHandle* handle) { | 1772 bool TestNavigationManager::ShouldMonitorNavigation(NavigationHandle* handle) { |
| 1722 if (handle_ || handle->GetURL() != url_) | 1773 if (handle_ || handle->GetURL() != url_) |
| 1723 return false; | 1774 return false; |
| 1724 if (handled_navigation_) | 1775 if (handled_navigation_) |
| 1725 return false; | 1776 return false; |
| 1726 return true; | 1777 return true; |
| 1727 } | 1778 } |
| 1728 | 1779 |
| 1729 } // namespace content | 1780 } // namespace content |
| OLD | NEW |