| 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 "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.
h" | 5 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.
h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 |
| 9 #include <memory> |
| 8 #include <utility> | 10 #include <utility> |
| 9 | 11 |
| 10 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 11 #include "base/macros.h" | 13 #include "base/macros.h" |
| 12 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 13 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/policy/cloud/policy_header_service_factory.h" | 16 #include "chrome/browser/policy/cloud/policy_header_service_factory.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/renderer_host/chrome_navigation_data.h" |
| 16 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/test/base/in_process_browser_test.h" | 21 #include "chrome/test/base/in_process_browser_test.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" | 22 #include "chrome/test/base/ui_test_utils.h" |
| 23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" |
| 19 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 24 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
| 20 #include "components/policy/core/common/cloud/policy_header_io_helper.h" | 25 #include "components/policy/core/common/cloud/policy_header_io_helper.h" |
| 21 #include "components/policy/core/common/cloud/policy_header_service.h" | 26 #include "components/policy/core/common/cloud/policy_header_service.h" |
| 22 #include "components/policy/core/common/policy_switches.h" | 27 #include "components/policy/core/common/policy_switches.h" |
| 28 #include "content/public/browser/navigation_data.h" |
| 29 #include "content/public/browser/navigation_handle.h" |
| 23 #include "content/public/browser/resource_dispatcher_host.h" | 30 #include "content/public/browser/resource_dispatcher_host.h" |
| 31 #include "content/public/browser/web_contents_observer.h" |
| 32 #include "content/public/test/browser_test_utils.h" |
| 24 #include "net/http/http_request_headers.h" | 33 #include "net/http/http_request_headers.h" |
| 25 #include "net/test/embedded_test_server/embedded_test_server.h" | 34 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 26 #include "net/test/embedded_test_server/http_request.h" | 35 #include "net/test/embedded_test_server/http_request.h" |
| 27 #include "net/test/embedded_test_server/http_response.h" | 36 #include "net/test/embedded_test_server/http_response.h" |
| 28 #include "net/url_request/url_request.h" | 37 #include "net/url_request/url_request.h" |
| 29 | 38 |
| 30 using content::ResourceType; | 39 using content::ResourceType; |
| 31 | 40 |
| 32 namespace { | 41 namespace { |
| 33 static const char kTestPolicyHeader[] = "test_header"; | 42 static const char kTestPolicyHeader[] = "test_header"; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 51 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( | 60 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( |
| 52 new net::test_server::BasicHttpResponse); | 61 new net::test_server::BasicHttpResponse); |
| 53 http_response->set_code(net::HTTP_OK); | 62 http_response->set_code(net::HTTP_OK); |
| 54 http_response->set_content("Success"); | 63 http_response->set_content("Success"); |
| 55 return std::move(http_response); | 64 return std::move(http_response); |
| 56 } | 65 } |
| 57 } | 66 } |
| 58 | 67 |
| 59 class TestDispatcherHostDelegate : public ChromeResourceDispatcherHostDelegate { | 68 class TestDispatcherHostDelegate : public ChromeResourceDispatcherHostDelegate { |
| 60 public: | 69 public: |
| 61 TestDispatcherHostDelegate() {} | 70 TestDispatcherHostDelegate() : should_add_data_reduction_proxy_data_(false) {} |
| 62 ~TestDispatcherHostDelegate() override {} | 71 ~TestDispatcherHostDelegate() override {} |
| 63 | 72 |
| 64 void RequestBeginning( | 73 void RequestBeginning( |
| 65 net::URLRequest* request, | 74 net::URLRequest* request, |
| 66 content::ResourceContext* resource_context, | 75 content::ResourceContext* resource_context, |
| 67 content::AppCacheService* appcache_service, | 76 content::AppCacheService* appcache_service, |
| 68 ResourceType resource_type, | 77 ResourceType resource_type, |
| 69 ScopedVector<content::ResourceThrottle>* throttles) override { | 78 ScopedVector<content::ResourceThrottle>* throttles) override { |
| 70 ChromeResourceDispatcherHostDelegate::RequestBeginning( | 79 ChromeResourceDispatcherHostDelegate::RequestBeginning( |
| 71 request, | 80 request, |
| 72 resource_context, | 81 resource_context, |
| 73 appcache_service, | 82 appcache_service, |
| 74 resource_type, | 83 resource_type, |
| 75 throttles); | 84 throttles); |
| 76 request_headers_.MergeFrom(request->extra_request_headers()); | 85 request_headers_.MergeFrom(request->extra_request_headers()); |
| 77 } | 86 } |
| 78 | 87 |
| 79 void OnRequestRedirected(const GURL& redirect_url, | 88 void OnRequestRedirected(const GURL& redirect_url, |
| 80 net::URLRequest* request, | 89 net::URLRequest* request, |
| 81 content::ResourceContext* resource_context, | 90 content::ResourceContext* resource_context, |
| 82 content::ResourceResponse* response) override { | 91 content::ResourceResponse* response) override { |
| 83 ChromeResourceDispatcherHostDelegate::OnRequestRedirected( | 92 ChromeResourceDispatcherHostDelegate::OnRequestRedirected( |
| 84 redirect_url, | 93 redirect_url, |
| 85 request, | 94 request, |
| 86 resource_context, | 95 resource_context, |
| 87 response); | 96 response); |
| 88 request_headers_.MergeFrom(request->extra_request_headers()); | 97 request_headers_.MergeFrom(request->extra_request_headers()); |
| 89 } | 98 } |
| 90 | 99 |
| 100 content::NavigationData* GetNavigationData( |
| 101 net::URLRequest* request) const override { |
| 102 if (request && should_add_data_reduction_proxy_data_) { |
| 103 data_reduction_proxy::DataReductionProxyData* data = |
| 104 data_reduction_proxy::DataReductionProxyData:: |
| 105 GetDataAndCreateIfNecessary(request); |
| 106 data->set_used_data_reduction_proxy(true); |
| 107 } |
| 108 return ChromeResourceDispatcherHostDelegate::GetNavigationData(request); |
| 109 } |
| 110 |
| 111 bool should_add_data_reduction_proxy_data_; |
| 91 net::HttpRequestHeaders request_headers_; | 112 net::HttpRequestHeaders request_headers_; |
| 92 | 113 |
| 93 private: | 114 private: |
| 94 DISALLOW_COPY_AND_ASSIGN(TestDispatcherHostDelegate); | 115 DISALLOW_COPY_AND_ASSIGN(TestDispatcherHostDelegate); |
| 95 }; | 116 }; |
| 96 | 117 |
| 118 // Helper class to track DidFinishNavigation and verify that NavigationData is |
| 119 // added to NavigationHandle and pause/resume execution of the test. |
| 120 class DidFinishNavigationObserver : public content::WebContentsObserver { |
| 121 public: |
| 122 DidFinishNavigationObserver(content::WebContents* web_contents, |
| 123 bool add_data_reduction_proxy_data) |
| 124 : content::WebContentsObserver(web_contents), |
| 125 add_data_reduction_proxy_data_(add_data_reduction_proxy_data) {} |
| 126 ~DidFinishNavigationObserver() override {} |
| 127 |
| 128 void DidFinishNavigation( |
| 129 content::NavigationHandle* navigation_handle) override { |
| 130 ChromeNavigationData* data = static_cast<ChromeNavigationData*>( |
| 131 navigation_handle->GetNavigationData()); |
| 132 if (add_data_reduction_proxy_data_) { |
| 133 EXPECT_TRUE(data->GetDataReductionProxyData()); |
| 134 EXPECT_TRUE( |
| 135 data->GetDataReductionProxyData()->used_data_reduction_proxy()); |
| 136 } else { |
| 137 EXPECT_FALSE(data->GetDataReductionProxyData()); |
| 138 } |
| 139 } |
| 140 |
| 141 private: |
| 142 bool add_data_reduction_proxy_data_; |
| 143 DISALLOW_COPY_AND_ASSIGN(DidFinishNavigationObserver); |
| 144 }; |
| 145 |
| 97 } // namespace | 146 } // namespace |
| 98 | 147 |
| 99 class ChromeResourceDispatcherHostDelegateBrowserTest : | 148 class ChromeResourceDispatcherHostDelegateBrowserTest : |
| 100 public InProcessBrowserTest { | 149 public InProcessBrowserTest { |
| 101 public: | 150 public: |
| 102 ChromeResourceDispatcherHostDelegateBrowserTest() {} | 151 ChromeResourceDispatcherHostDelegateBrowserTest() {} |
| 103 | 152 |
| 104 void SetUpOnMainThread() override { | 153 void SetUpOnMainThread() override { |
| 105 InProcessBrowserTest::SetUpOnMainThread(); | 154 InProcessBrowserTest::SetUpOnMainThread(); |
| 106 // Hook navigations with our delegate. | 155 // Hook navigations with our delegate. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 128 (*it)->SetServerURLForTest(dm_url_.spec()); | 177 (*it)->SetServerURLForTest(dm_url_.spec()); |
| 129 (*it)->UpdateHeader(kTestPolicyHeader); | 178 (*it)->UpdateHeader(kTestPolicyHeader); |
| 130 } | 179 } |
| 131 } | 180 } |
| 132 | 181 |
| 133 void TearDownOnMainThread() override { | 182 void TearDownOnMainThread() override { |
| 134 content::ResourceDispatcherHost::Get()->SetDelegate(NULL); | 183 content::ResourceDispatcherHost::Get()->SetDelegate(NULL); |
| 135 dispatcher_host_delegate_.reset(); | 184 dispatcher_host_delegate_.reset(); |
| 136 } | 185 } |
| 137 | 186 |
| 187 void SetShouldAddDataReductionProxyData(bool add_data) { |
| 188 dispatcher_host_delegate_->should_add_data_reduction_proxy_data_ = add_data; |
| 189 } |
| 190 |
| 138 protected: | 191 protected: |
| 139 // The fake URL for DMServer we are using. | 192 // The fake URL for DMServer we are using. |
| 140 GURL dm_url_; | 193 GURL dm_url_; |
| 141 std::unique_ptr<TestDispatcherHostDelegate> dispatcher_host_delegate_; | 194 std::unique_ptr<TestDispatcherHostDelegate> dispatcher_host_delegate_; |
| 142 | |
| 143 private: | 195 private: |
| 144 DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegateBrowserTest); | 196 DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegateBrowserTest); |
| 145 }; | 197 }; |
| 146 | 198 |
| 147 | 199 |
| 148 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, | 200 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, |
| 149 NoPolicyHeader) { | 201 NoPolicyHeader) { |
| 150 // When fetching non-DMServer URLs, we should not add a policy header to the | 202 // When fetching non-DMServer URLs, we should not add a policy header to the |
| 151 // request. | 203 // request. |
| 152 DCHECK(!embedded_test_server()->base_url().spec().empty()); | 204 DCHECK(!embedded_test_server()->base_url().spec().empty()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 175 redirect_url += kServerRedirectUrl; | 227 redirect_url += kServerRedirectUrl; |
| 176 redirect_url += "?"; | 228 redirect_url += "?"; |
| 177 redirect_url += dm_url_.spec(); | 229 redirect_url += dm_url_.spec(); |
| 178 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL( | 230 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL( |
| 179 redirect_url)); | 231 redirect_url)); |
| 180 std::string value; | 232 std::string value; |
| 181 ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader( | 233 ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader( |
| 182 policy::kChromePolicyHeader, &value)); | 234 policy::kChromePolicyHeader, &value)); |
| 183 ASSERT_EQ(kTestPolicyHeader, value); | 235 ASSERT_EQ(kTestPolicyHeader, value); |
| 184 } | 236 } |
| 237 |
| 238 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, |
| 239 NavigationDataProcessed) { |
| 240 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url()); |
| 241 { |
| 242 DidFinishNavigationObserver nav_observer( |
| 243 browser()->tab_strip_model()->GetActiveWebContents(), false); |
| 244 ui_test_utils::NavigateToURL( |
| 245 browser(), embedded_test_server()->GetURL("/google/google.html")); |
| 246 } |
| 247 SetShouldAddDataReductionProxyData(true); |
| 248 { |
| 249 DidFinishNavigationObserver nav_observer( |
| 250 browser()->tab_strip_model()->GetActiveWebContents(), true); |
| 251 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url()); |
| 252 } |
| 253 } |
| OLD | NEW |