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/browser/resource_dispatcher_host.h" | 5 #include "content/public/browser/resource_dispatcher_host.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
22 #include "content/browser/download/download_manager_impl.h" | 22 #include "content/browser/download/download_manager_impl.h" |
23 #include "content/browser/loader/loader_globals.h" | |
23 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 24 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
24 #include "content/browser/web_contents/web_contents_impl.h" | 25 #include "content/browser/web_contents/web_contents_impl.h" |
25 #include "content/public/browser/browser_context.h" | 26 #include "content/public/browser/browser_context.h" |
26 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 28 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
28 #include "content/public/browser/resource_request_info.h" | 29 #include "content/public/browser/resource_request_info.h" |
29 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
30 #include "content/public/common/browser_side_navigation_policy.h" | 31 #include "content/public/common/browser_side_navigation_policy.h" |
31 #include "content/public/common/previews_state.h" | 32 #include "content/public/common/previews_state.h" |
32 #include "content/public/common/url_constants.h" | 33 #include "content/public/common/url_constants.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
53 namespace content { | 54 namespace content { |
54 | 55 |
55 class ResourceDispatcherHostBrowserTest : public ContentBrowserTest, | 56 class ResourceDispatcherHostBrowserTest : public ContentBrowserTest, |
56 public DownloadManager::Observer { | 57 public DownloadManager::Observer { |
57 public: | 58 public: |
58 ResourceDispatcherHostBrowserTest() : got_downloads_(false) {} | 59 ResourceDispatcherHostBrowserTest() : got_downloads_(false) {} |
59 | 60 |
60 protected: | 61 protected: |
61 void SetUpOnMainThread() override { | 62 void SetUpOnMainThread() override { |
62 base::FilePath path = GetTestFilePath("", ""); | 63 base::FilePath path = GetTestFilePath("", ""); |
63 BrowserThread::PostTask( | 64 LoaderGlobals::Get()->io_thread_task_runner()->PostTask( |
jam
2017/03/29 15:44:32
ditto, this is a browser test so it won't move. no
ananta
2017/03/29 19:41:04
Done.
| |
64 BrowserThread::IO, FROM_HERE, | 65 FROM_HERE, base::Bind(&net::URLRequestMockHTTPJob::AddUrlHandlers, path, |
65 base::Bind( | 66 make_scoped_refptr( |
66 &net::URLRequestMockHTTPJob::AddUrlHandlers, path, | 67 content::BrowserThread::GetBlockingPool()))); |
67 make_scoped_refptr(content::BrowserThread::GetBlockingPool()))); | 68 LoaderGlobals::Get()->io_thread_task_runner()->PostTask( |
68 BrowserThread::PostTask( | 69 FROM_HERE, base::Bind(&net::URLRequestFailedJob::AddUrlHandler)); |
69 BrowserThread::IO, FROM_HERE, | |
70 base::Bind(&net::URLRequestFailedJob::AddUrlHandler)); | |
71 } | 70 } |
72 | 71 |
73 void OnDownloadCreated(DownloadManager* manager, | 72 void OnDownloadCreated(DownloadManager* manager, |
74 DownloadItem* item) override { | 73 DownloadItem* item) override { |
75 if (!got_downloads_) | 74 if (!got_downloads_) |
76 got_downloads_ = !!manager->InProgressCount(); | 75 got_downloads_ = !!manager->InProgressCount(); |
77 } | 76 } |
78 | 77 |
79 void CheckTitleTest(const GURL& url, | 78 void CheckTitleTest(const GURL& url, |
80 const std::string& expected_title) { | 79 const std::string& expected_title) { |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 | 248 |
250 // Responses with a HungResponse for the specified URL to hang on the request, | 249 // Responses with a HungResponse for the specified URL to hang on the request, |
251 // and cancells all requests from specifield |child_id|. | 250 // and cancells all requests from specifield |child_id|. |
252 std::unique_ptr<net::test_server::HttpResponse> CancelOnRequest( | 251 std::unique_ptr<net::test_server::HttpResponse> CancelOnRequest( |
253 const std::string& relative_url, | 252 const std::string& relative_url, |
254 int child_id, | 253 int child_id, |
255 const net::test_server::HttpRequest& request) { | 254 const net::test_server::HttpRequest& request) { |
256 if (request.relative_url != relative_url) | 255 if (request.relative_url != relative_url) |
257 return nullptr; | 256 return nullptr; |
258 | 257 |
259 content::BrowserThread::PostTask( | 258 LoaderGlobals::Get()->io_thread_task_runner()->PostTask( |
260 content::BrowserThread::IO, | |
261 FROM_HERE, | 259 FROM_HERE, |
262 base::Bind(&ResourceDispatcherHostImpl::CancelRequestsForProcess, | 260 base::Bind(&ResourceDispatcherHostImpl::CancelRequestsForProcess, |
263 base::Unretained(ResourceDispatcherHostImpl::Get()), | 261 base::Unretained(ResourceDispatcherHostImpl::Get()), |
264 child_id)); | 262 child_id)); |
265 | 263 |
266 return base::MakeUnique<net::test_server::HungResponse>(); | 264 return base::MakeUnique<net::test_server::HungResponse>(); |
267 } | 265 } |
268 | 266 |
269 } // namespace | 267 } // namespace |
270 | 268 |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
616 | 614 |
617 ~PreviewsStateResourceDispatcherHostDelegate() override {} | 615 ~PreviewsStateResourceDispatcherHostDelegate() override {} |
618 | 616 |
619 // ResourceDispatcherHostDelegate implementation: | 617 // ResourceDispatcherHostDelegate implementation: |
620 void RequestBeginning( | 618 void RequestBeginning( |
621 net::URLRequest* request, | 619 net::URLRequest* request, |
622 ResourceContext* resource_context, | 620 ResourceContext* resource_context, |
623 AppCacheService* appcache_service, | 621 AppCacheService* appcache_service, |
624 ResourceType resource_type, | 622 ResourceType resource_type, |
625 std::vector<std::unique_ptr<ResourceThrottle>>* throttles) override { | 623 std::vector<std::unique_ptr<ResourceThrottle>>* throttles) override { |
626 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 624 DCHECK(LoaderGlobals::Get() |
625 ->io_thread_task_runner() | |
626 ->BelongsToCurrentThread()); | |
627 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | 627 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
628 if (request->url() != main_frame_url_ && request->url() != subresource_url_ | 628 if (request->url() != main_frame_url_ && request->url() != subresource_url_ |
629 && request->url() != iframe_url_) | 629 && request->url() != iframe_url_) |
630 return; | 630 return; |
631 if (request->url() == main_frame_url_) { | 631 if (request->url() == main_frame_url_) { |
632 EXPECT_FALSE(main_frame_url_seen_); | 632 EXPECT_FALSE(main_frame_url_seen_); |
633 main_frame_url_seen_ = true; | 633 main_frame_url_seen_ = true; |
634 } else if (request->url() == subresource_url_) { | 634 } else if (request->url() == subresource_url_) { |
635 EXPECT_TRUE(main_frame_url_seen_); | 635 EXPECT_TRUE(main_frame_url_seen_); |
636 EXPECT_FALSE(subresource_url_seen_); | 636 EXPECT_FALSE(subresource_url_seen_); |
637 subresource_url_seen_ = true; | 637 subresource_url_seen_ = true; |
638 } else if (request->url() == iframe_url_) { | 638 } else if (request->url() == iframe_url_) { |
639 EXPECT_TRUE(main_frame_url_seen_); | 639 EXPECT_TRUE(main_frame_url_seen_); |
640 EXPECT_FALSE(iframe_url_seen_); | 640 EXPECT_FALSE(iframe_url_seen_); |
641 iframe_url_seen_ = true; | 641 iframe_url_seen_ = true; |
642 } | 642 } |
643 EXPECT_EQ(previews_state_, info->GetPreviewsState()); | 643 EXPECT_EQ(previews_state_, info->GetPreviewsState()); |
644 } | 644 } |
645 | 645 |
646 void SetDelegate() { | 646 void SetDelegate() { |
647 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 647 DCHECK(LoaderGlobals::Get() |
648 ->io_thread_task_runner() | |
649 ->BelongsToCurrentThread()); | |
648 ResourceDispatcherHost::Get()->SetDelegate(this); | 650 ResourceDispatcherHost::Get()->SetDelegate(this); |
649 } | 651 } |
650 | 652 |
651 PreviewsState GetPreviewsState( | 653 PreviewsState GetPreviewsState( |
652 const net::URLRequest& request, | 654 const net::URLRequest& request, |
653 content::ResourceContext* resource_context) override { | 655 content::ResourceContext* resource_context) override { |
654 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 656 DCHECK(LoaderGlobals::Get() |
657 ->io_thread_task_runner() | |
658 ->BelongsToCurrentThread()); | |
655 EXPECT_FALSE(should_get_previews_state_called_); | 659 EXPECT_FALSE(should_get_previews_state_called_); |
656 should_get_previews_state_called_ = true; | 660 should_get_previews_state_called_ = true; |
657 EXPECT_EQ(main_frame_url_, request.url()); | 661 EXPECT_EQ(main_frame_url_, request.url()); |
658 return previews_state_; | 662 return previews_state_; |
659 } | 663 } |
660 | 664 |
661 void Reset(PreviewsState previews_state) { | 665 void Reset(PreviewsState previews_state) { |
662 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 666 DCHECK(LoaderGlobals::Get() |
667 ->io_thread_task_runner() | |
668 ->BelongsToCurrentThread()); | |
663 main_frame_url_seen_ = false; | 669 main_frame_url_seen_ = false; |
664 subresource_url_seen_ = false; | 670 subresource_url_seen_ = false; |
665 iframe_url_seen_ = false; | 671 iframe_url_seen_ = false; |
666 previews_state_ = previews_state; | 672 previews_state_ = previews_state; |
667 should_get_previews_state_called_ = false; | 673 should_get_previews_state_called_ = false; |
668 } | 674 } |
669 | 675 |
670 void CheckResourcesRequested(bool should_get_previews_state_called) { | 676 void CheckResourcesRequested(bool should_get_previews_state_called) { |
671 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 677 DCHECK(LoaderGlobals::Get() |
678 ->io_thread_task_runner() | |
679 ->BelongsToCurrentThread()); | |
672 EXPECT_EQ(should_get_previews_state_called, | 680 EXPECT_EQ(should_get_previews_state_called, |
673 should_get_previews_state_called_); | 681 should_get_previews_state_called_); |
674 EXPECT_TRUE(main_frame_url_seen_); | 682 EXPECT_TRUE(main_frame_url_seen_); |
675 EXPECT_TRUE(subresource_url_seen_); | 683 EXPECT_TRUE(subresource_url_seen_); |
676 EXPECT_TRUE(iframe_url_seen_); | 684 EXPECT_TRUE(iframe_url_seen_); |
677 } | 685 } |
678 | 686 |
679 private: | 687 private: |
680 const GURL main_frame_url_; | 688 const GURL main_frame_url_; |
681 const GURL subresource_url_; | 689 const GURL subresource_url_; |
(...skipping 19 matching lines...) Expand all Loading... | |
701 void SetUpOnMainThread() override { | 709 void SetUpOnMainThread() override { |
702 ContentBrowserTest::SetUpOnMainThread(); | 710 ContentBrowserTest::SetUpOnMainThread(); |
703 | 711 |
704 ASSERT_TRUE(embedded_test_server()->Start()); | 712 ASSERT_TRUE(embedded_test_server()->Start()); |
705 | 713 |
706 delegate_.reset(new PreviewsStateResourceDispatcherHostDelegate( | 714 delegate_.reset(new PreviewsStateResourceDispatcherHostDelegate( |
707 embedded_test_server()->GetURL("/page_with_iframe.html"), | 715 embedded_test_server()->GetURL("/page_with_iframe.html"), |
708 embedded_test_server()->GetURL("/image.jpg"), | 716 embedded_test_server()->GetURL("/image.jpg"), |
709 embedded_test_server()->GetURL("/title1.html"))); | 717 embedded_test_server()->GetURL("/title1.html"))); |
710 | 718 |
711 content::BrowserThread::PostTask( | 719 LoaderGlobals::Get()->io_thread_task_runner()->PostTask( |
712 content::BrowserThread::IO, | 720 FROM_HERE, |
713 FROM_HERE, | 721 base::Bind(&PreviewsStateResourceDispatcherHostDelegate::SetDelegate, |
714 base::Bind(&PreviewsStateResourceDispatcherHostDelegate::SetDelegate, | 722 base::Unretained(delegate_.get()))); |
715 base::Unretained(delegate_.get()))); | |
716 } | 723 } |
717 | 724 |
718 void Reset(PreviewsState previews_state) { | 725 void Reset(PreviewsState previews_state) { |
719 content::BrowserThread::PostTask( | 726 LoaderGlobals::Get()->io_thread_task_runner()->PostTask( |
720 content::BrowserThread::IO, FROM_HERE, | 727 FROM_HERE, |
721 base::Bind(&PreviewsStateResourceDispatcherHostDelegate::Reset, | 728 base::Bind(&PreviewsStateResourceDispatcherHostDelegate::Reset, |
722 base::Unretained(delegate_.get()), previews_state)); | 729 base::Unretained(delegate_.get()), previews_state)); |
723 } | 730 } |
724 | 731 |
725 void CheckResourcesRequested( | 732 void CheckResourcesRequested( |
726 bool should_get_previews_state_called) { | 733 bool should_get_previews_state_called) { |
727 content::BrowserThread::PostTask( | 734 LoaderGlobals::Get()->io_thread_task_runner()->PostTask( |
728 content::BrowserThread::IO, FROM_HERE, | 735 FROM_HERE, base::Bind(&PreviewsStateResourceDispatcherHostDelegate:: |
729 base::Bind(&PreviewsStateResourceDispatcherHostDelegate:: | 736 CheckResourcesRequested, |
730 CheckResourcesRequested, | 737 base::Unretained(delegate_.get()), |
731 base::Unretained(delegate_.get()), | 738 should_get_previews_state_called)); |
732 should_get_previews_state_called)); | |
733 } | 739 } |
734 | 740 |
735 private: | 741 private: |
736 std::unique_ptr<PreviewsStateResourceDispatcherHostDelegate> delegate_; | 742 std::unique_ptr<PreviewsStateResourceDispatcherHostDelegate> delegate_; |
737 }; | 743 }; |
738 | 744 |
739 // Test that navigating calls GetPreviewsState with SERVER_LOFI_ON. | 745 // Test that navigating calls GetPreviewsState with SERVER_LOFI_ON. |
740 IN_PROC_BROWSER_TEST_F(PreviewsStateResourceDispatcherHostBrowserTest, | 746 IN_PROC_BROWSER_TEST_F(PreviewsStateResourceDispatcherHostBrowserTest, |
741 ShouldEnableLoFiModeOn) { | 747 ShouldEnableLoFiModeOn) { |
742 // Navigate with ShouldEnableLoFiMode returning true. | 748 // Navigate with ShouldEnableLoFiMode returning true. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
862 ~RequestDataResourceDispatcherHostBrowserTest() override {} | 868 ~RequestDataResourceDispatcherHostBrowserTest() override {} |
863 | 869 |
864 protected: | 870 protected: |
865 void SetUpOnMainThread() override { | 871 void SetUpOnMainThread() override { |
866 ContentBrowserTest::SetUpOnMainThread(); | 872 ContentBrowserTest::SetUpOnMainThread(); |
867 | 873 |
868 ASSERT_TRUE(embedded_test_server()->Start()); | 874 ASSERT_TRUE(embedded_test_server()->Start()); |
869 | 875 |
870 delegate_.reset(new RequestDataResourceDispatcherHostDelegate()); | 876 delegate_.reset(new RequestDataResourceDispatcherHostDelegate()); |
871 | 877 |
872 content::BrowserThread::PostTask( | 878 LoaderGlobals::Get()->io_thread_task_runner()->PostTask( |
873 content::BrowserThread::IO, FROM_HERE, | 879 FROM_HERE, |
874 base::Bind(&RequestDataResourceDispatcherHostDelegate::SetDelegate, | 880 base::Bind(&RequestDataResourceDispatcherHostDelegate::SetDelegate, |
875 base::Unretained(delegate_.get()))); | 881 base::Unretained(delegate_.get()))); |
876 } | 882 } |
877 | 883 |
878 protected: | 884 protected: |
879 std::unique_ptr<RequestDataResourceDispatcherHostDelegate> delegate_; | 885 std::unique_ptr<RequestDataResourceDispatcherHostDelegate> delegate_; |
880 }; | 886 }; |
881 | 887 |
882 IN_PROC_BROWSER_TEST_F(RequestDataResourceDispatcherHostBrowserTest, Basic) { | 888 IN_PROC_BROWSER_TEST_F(RequestDataResourceDispatcherHostBrowserTest, Basic) { |
883 GURL top_url(embedded_test_server()->GetURL("/page_with_subresources.html")); | 889 GURL top_url(embedded_test_server()->GetURL("/page_with_subresources.html")); |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1138 EXPECT_EQ(top_origin, delegate_->data()[2]->initiator); | 1144 EXPECT_EQ(top_origin, delegate_->data()[2]->initiator); |
1139 | 1145 |
1140 // Cross-origin subresource requests have a unique first-party, and an | 1146 // Cross-origin subresource requests have a unique first-party, and an |
1141 // initiator that matches the document in which they're embedded. | 1147 // initiator that matches the document in which they're embedded. |
1142 EXPECT_EQ(nested_js_url, delegate_->data()[3]->url); | 1148 EXPECT_EQ(nested_js_url, delegate_->data()[3]->url); |
1143 EXPECT_EQ(kURLWithUniqueOrigin, delegate_->data()[3]->first_party); | 1149 EXPECT_EQ(kURLWithUniqueOrigin, delegate_->data()[3]->first_party); |
1144 EXPECT_EQ(nested_origin, delegate_->data()[3]->initiator); | 1150 EXPECT_EQ(nested_origin, delegate_->data()[3]->initiator); |
1145 } | 1151 } |
1146 | 1152 |
1147 } // namespace content | 1153 } // namespace content |
OLD | NEW |