| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/frame_host/navigation_controller_impl.h" | 5 #include "content/browser/frame_host/navigation_controller_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> |
| 8 #include <utility> | 9 #include <utility> |
| 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/bind.h" | 12 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 12 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/sequenced_task_runner.h" |
| 13 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/test/histogram_tester.h" | 20 #include "base/test/histogram_tester.h" |
| 21 #include "base/threading/sequenced_task_runner_handle.h" |
| 16 #include "content/browser/frame_host/frame_navigation_entry.h" | 22 #include "content/browser/frame_host/frame_navigation_entry.h" |
| 17 #include "content/browser/frame_host/frame_tree.h" | 23 #include "content/browser/frame_host/frame_tree.h" |
| 18 #include "content/browser/frame_host/navigation_entry_impl.h" | 24 #include "content/browser/frame_host/navigation_entry_impl.h" |
| 19 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 25 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 20 #include "content/browser/web_contents/web_contents_impl.h" | 26 #include "content/browser/web_contents/web_contents_impl.h" |
| 21 #include "content/common/frame_messages.h" | 27 #include "content/common/frame_messages.h" |
| 22 #include "content/common/page_state_serialization.h" | 28 #include "content/common/page_state_serialization.h" |
| 23 #include "content/common/site_isolation_policy.h" | 29 #include "content/common/site_isolation_policy.h" |
| 30 #include "content/public/browser/navigation_controller.h" |
| 24 #include "content/public/browser/navigation_handle.h" | 31 #include "content/public/browser/navigation_handle.h" |
| 25 #include "content/public/browser/render_view_host.h" | 32 #include "content/public/browser/render_view_host.h" |
| 26 #include "content/public/browser/resource_controller.h" | 33 #include "content/public/browser/resource_controller.h" |
| 27 #include "content/public/browser/resource_dispatcher_host.h" | 34 #include "content/public/browser/resource_dispatcher_host.h" |
| 28 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 35 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 29 #include "content/public/browser/resource_throttle.h" | 36 #include "content/public/browser/resource_throttle.h" |
| 30 #include "content/public/browser/web_contents.h" | 37 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/browser/web_contents_delegate.h" | 38 #include "content/public/browser/web_contents_delegate.h" |
| 32 #include "content/public/browser/web_contents_observer.h" | 39 #include "content/public/browser/web_contents_observer.h" |
| 33 #include "content/public/common/bindings_policy.h" | 40 #include "content/public/common/bindings_policy.h" |
| 34 #include "content/public/common/browser_side_navigation_policy.h" | 41 #include "content/public/common/browser_side_navigation_policy.h" |
| 35 #include "content/public/common/renderer_preferences.h" | 42 #include "content/public/common/renderer_preferences.h" |
| 36 #include "content/public/common/url_constants.h" | 43 #include "content/public/common/url_constants.h" |
| 37 #include "content/public/test/browser_test_utils.h" | 44 #include "content/public/test/browser_test_utils.h" |
| 38 #include "content/public/test/content_browser_test.h" | 45 #include "content/public/test/content_browser_test.h" |
| 39 #include "content/public/test/content_browser_test_utils.h" | 46 #include "content/public/test/content_browser_test_utils.h" |
| 40 #include "content/public/test/test_navigation_observer.h" | 47 #include "content/public/test/test_navigation_observer.h" |
| 41 #include "content/public/test/test_utils.h" | 48 #include "content/public/test/test_utils.h" |
| 42 #include "content/shell/browser/shell.h" | 49 #include "content/shell/browser/shell.h" |
| 43 #include "content/shell/common/shell_switches.h" | 50 #include "content/shell/common/shell_switches.h" |
| 44 #include "content/test/content_browser_test_utils_internal.h" | 51 #include "content/test/content_browser_test_utils_internal.h" |
| 45 #include "content/test/test_frame_navigation_observer.h" | 52 #include "content/test/test_frame_navigation_observer.h" |
| 46 #include "net/dns/mock_host_resolver.h" | 53 #include "net/dns/mock_host_resolver.h" |
| 47 #include "net/test/embedded_test_server/embedded_test_server.h" | 54 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 55 #include "net/test/embedded_test_server/http_request.h" |
| 48 #include "net/test/url_request/url_request_failed_job.h" | 56 #include "net/test/url_request/url_request_failed_job.h" |
| 49 #include "testing/gmock/include/gmock/gmock-matchers.h" | 57 #include "testing/gmock/include/gmock/gmock-matchers.h" |
| 50 | 58 |
| 51 namespace { | 59 namespace { |
| 52 | 60 |
| 53 static std::string kAddNamedFrameScript = | 61 static std::string kAddNamedFrameScript = |
| 54 "var f = document.createElement('iframe');" | 62 "var f = document.createElement('iframe');" |
| 55 "f.name = 'foo-frame-name';" | 63 "f.name = 'foo-frame-name';" |
| 56 "document.body.appendChild(f);"; | 64 "document.body.appendChild(f);"; |
| 57 static std::string kAddFrameScript = | 65 static std::string kAddFrameScript = |
| (...skipping 6446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6504 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); | 6512 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 6505 histogram.ExpectTotalCount(kReloadToReloadMetricName, 4); | 6513 histogram.ExpectTotalCount(kReloadToReloadMetricName, 4); |
| 6506 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 3); | 6514 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 3); |
| 6507 | 6515 |
| 6508 controller.ReloadToRefreshContent(false); | 6516 controller.ReloadToRefreshContent(false); |
| 6509 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); | 6517 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 6510 histogram.ExpectTotalCount(kReloadToReloadMetricName, 4); | 6518 histogram.ExpectTotalCount(kReloadToReloadMetricName, 4); |
| 6511 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 3); | 6519 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 3); |
| 6512 } | 6520 } |
| 6513 | 6521 |
| 6522 namespace { |
| 6523 |
| 6524 class RequestMonitoringNavigationBrowserTest : public ContentBrowserTest { |
| 6525 public: |
| 6526 RequestMonitoringNavigationBrowserTest() : weak_factory_(this) {} |
| 6527 |
| 6528 const net::test_server::HttpRequest* FindAccumulatedRequest( |
| 6529 const GURL& url_to_find) { |
| 6530 // net::test_server::HttpRequest::GetURL hardcodes "http://localhost/" part. |
| 6531 GURL::Replacements replacements; |
| 6532 replacements.SetHostStr("localhost"); |
| 6533 replacements.ClearPort(); |
| 6534 replacements.SetSchemeStr("http"); |
| 6535 DCHECK(url_to_find.SchemeIsHTTPOrHTTPS()); |
| 6536 GURL canonical_url_to_find = url_to_find.ReplaceComponents(replacements); |
| 6537 |
| 6538 auto it = std::find_if( |
| 6539 accumulated_requests_.begin(), accumulated_requests_.end(), |
| 6540 [&canonical_url_to_find](const net::test_server::HttpRequest& request) { |
| 6541 return request.GetURL() == canonical_url_to_find; |
| 6542 }); |
| 6543 if (it == accumulated_requests_.end()) |
| 6544 return nullptr; |
| 6545 return &*it; |
| 6546 } |
| 6547 |
| 6548 protected: |
| 6549 void SetUpOnMainThread() override { |
| 6550 // Accumulate all http requests made to |embedded_test_server| into |
| 6551 // |accumulated_requests_| container. |
| 6552 embedded_test_server()->RegisterRequestMonitor(base::Bind( |
| 6553 &RequestMonitoringNavigationBrowserTest::MonitorRequestOnIoThread, |
| 6554 weak_factory_.GetWeakPtr(), base::SequencedTaskRunnerHandle::Get())); |
| 6555 |
| 6556 ASSERT_TRUE(embedded_test_server()->Start()); |
| 6557 } |
| 6558 |
| 6559 void TearDown() override { |
| 6560 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 6561 } |
| 6562 |
| 6563 private: |
| 6564 static void MonitorRequestOnIoThread( |
| 6565 const base::WeakPtr<RequestMonitoringNavigationBrowserTest>& weak_this, |
| 6566 const scoped_refptr<base::SequencedTaskRunner>& postback_task_runner, |
| 6567 const net::test_server::HttpRequest& request) { |
| 6568 postback_task_runner->PostTask( |
| 6569 FROM_HERE, |
| 6570 base::Bind( |
| 6571 &RequestMonitoringNavigationBrowserTest::MonitorRequestOnMainThread, |
| 6572 weak_this, request)); |
| 6573 } |
| 6574 |
| 6575 void MonitorRequestOnMainThread( |
| 6576 const net::test_server::HttpRequest& request) { |
| 6577 accumulated_requests_.push_back(request); |
| 6578 } |
| 6579 |
| 6580 std::vector<net::test_server::HttpRequest> accumulated_requests_; |
| 6581 base::WeakPtrFactory<RequestMonitoringNavigationBrowserTest> weak_factory_; |
| 6582 }; |
| 6583 |
| 6584 // Helper for waiting until the main frame of |web_contents| has loaded |
| 6585 // |expected_url| (and all subresources have finished loading). |
| 6586 class WebContentsLoadFinishedWaiter : public WebContentsObserver { |
| 6587 public: |
| 6588 WebContentsLoadFinishedWaiter(WebContents* web_contents, |
| 6589 const GURL& expected_url) |
| 6590 : WebContentsObserver(web_contents), |
| 6591 expected_url_(expected_url), |
| 6592 message_loop_runner_(new MessageLoopRunner) { |
| 6593 EXPECT_TRUE(web_contents != NULL); |
| 6594 } |
| 6595 |
| 6596 void Wait() { message_loop_runner_->Run(); } |
| 6597 |
| 6598 private: |
| 6599 void DidFinishLoad(RenderFrameHost* render_frame_host, |
| 6600 const GURL& url) override { |
| 6601 bool is_main_frame = !render_frame_host->GetParent(); |
| 6602 if (url == expected_url_ && is_main_frame) |
| 6603 message_loop_runner_->Quit(); |
| 6604 } |
| 6605 |
| 6606 GURL expected_url_; |
| 6607 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 6608 }; |
| 6609 |
| 6610 } // namespace { |
| 6611 |
| 6612 // Check that NavigationController::LoadURLParams::extra_headers are not copied |
| 6613 // to subresource requests. |
| 6614 IN_PROC_BROWSER_TEST_F(RequestMonitoringNavigationBrowserTest, |
| 6615 ExtraHeadersVsSubresources) { |
| 6616 GURL page_url = embedded_test_server()->GetURL("/page_with_image.html"); |
| 6617 GURL image_url = embedded_test_server()->GetURL("/blank.jpg"); |
| 6618 |
| 6619 // Navigate via LoadURLWithParams (setting |extra_headers| field). |
| 6620 WebContentsLoadFinishedWaiter waiter(shell()->web_contents(), page_url); |
| 6621 NavigationController::LoadURLParams load_url_params(page_url); |
| 6622 load_url_params.extra_headers = "X-ExtraHeadersVsSubresources: 1"; |
| 6623 shell()->web_contents()->GetController().LoadURLWithParams(load_url_params); |
| 6624 waiter.Wait(); |
| 6625 EXPECT_EQ(page_url, shell()->web_contents()->GetLastCommittedURL()); |
| 6626 |
| 6627 // Verify that the extra header was present for the page. |
| 6628 const net::test_server::HttpRequest* page_request = |
| 6629 FindAccumulatedRequest(page_url); |
| 6630 ASSERT_TRUE(page_request); |
| 6631 EXPECT_THAT(page_request->headers, |
| 6632 testing::Contains(testing::Key("X-ExtraHeadersVsSubresources"))); |
| 6633 |
| 6634 // Verify that the extra header was NOT present for the subresource. |
| 6635 const net::test_server::HttpRequest* image_request = |
| 6636 FindAccumulatedRequest(image_url); |
| 6637 ASSERT_TRUE(image_request); |
| 6638 EXPECT_THAT(image_request->headers, |
| 6639 testing::Not(testing::Contains( |
| 6640 testing::Key("X-ExtraHeadersVsSubresources")))); |
| 6641 } |
| 6642 |
| 6514 } // namespace content | 6643 } // namespace content |
| OLD | NEW |