| 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 std::unique_ptr<content::NavigationData> GetNavigationData( |
| 101 const net::URLRequest* request) const override { |
| 102 if (request && should_add_data_reduction_proxy_data_) { |
| 103 net::URLRequest* mutable_request = const_cast<net::URLRequest*>(request); |
| 104 data_reduction_proxy::DataReductionProxyData* data = |
| 105 data_reduction_proxy::DataReductionProxyData:: |
| 106 GetDataAndCreateIfNecessary(mutable_request); |
| 107 data->set_used_data_reduction_proxy(true); |
| 108 } |
| 109 return ChromeResourceDispatcherHostDelegate::GetNavigationData(request); |
| 110 } |
| 111 |
| 112 bool should_add_data_reduction_proxy_data_; |
| 91 net::HttpRequestHeaders request_headers_; | 113 net::HttpRequestHeaders request_headers_; |
| 92 | 114 |
| 93 private: | 115 private: |
| 94 DISALLOW_COPY_AND_ASSIGN(TestDispatcherHostDelegate); | 116 DISALLOW_COPY_AND_ASSIGN(TestDispatcherHostDelegate); |
| 95 }; | 117 }; |
| 96 | 118 |
| 119 // Helper class to track DidFinishNavigation and verify that NavigationData is |
| 120 // added to NavigationHandle and pause/resume execution of the test. |
| 121 class DidFinishNavigationObserver : public content::WebContentsObserver { |
| 122 public: |
| 123 explicit DidFinishNavigationObserver(content::WebContents* web_contents, |
| 124 bool add_data_reduction_proxy_data) |
| 125 : content::WebContentsObserver(web_contents), |
| 126 add_data_reduction_proxy_data_(add_data_reduction_proxy_data) {} |
| 127 ~DidFinishNavigationObserver() override {} |
| 128 |
| 129 void DidFinishNavigation( |
| 130 content::NavigationHandle* navigation_handle) override { |
| 131 ChromeNavigationData* data = static_cast<ChromeNavigationData*>( |
| 132 navigation_handle->GetNavigationData()); |
| 133 if (!add_data_reduction_proxy_data_) { |
| 134 EXPECT_FALSE(data->GetDataReductionProxyData()); |
| 135 } else { |
| 136 EXPECT_TRUE(data->GetDataReductionProxyData()); |
| 137 EXPECT_TRUE( |
| 138 data->GetDataReductionProxyData()->used_data_reduction_proxy()); |
| 139 } |
| 140 } |
| 141 |
| 142 private: |
| 143 bool add_data_reduction_proxy_data_; |
| 144 DISALLOW_COPY_AND_ASSIGN(DidFinishNavigationObserver); |
| 145 }; |
| 146 |
| 97 } // namespace | 147 } // namespace |
| 98 | 148 |
| 99 class ChromeResourceDispatcherHostDelegateBrowserTest : | 149 class ChromeResourceDispatcherHostDelegateBrowserTest : |
| 100 public InProcessBrowserTest { | 150 public InProcessBrowserTest { |
| 101 public: | 151 public: |
| 102 ChromeResourceDispatcherHostDelegateBrowserTest() {} | 152 ChromeResourceDispatcherHostDelegateBrowserTest() {} |
| 103 | 153 |
| 104 void SetUpOnMainThread() override { | 154 void SetUpOnMainThread() override { |
| 105 InProcessBrowserTest::SetUpOnMainThread(); | 155 InProcessBrowserTest::SetUpOnMainThread(); |
| 106 // Hook navigations with our delegate. | 156 // Hook navigations with our delegate. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 128 (*it)->SetServerURLForTest(dm_url_.spec()); | 178 (*it)->SetServerURLForTest(dm_url_.spec()); |
| 129 (*it)->UpdateHeader(kTestPolicyHeader); | 179 (*it)->UpdateHeader(kTestPolicyHeader); |
| 130 } | 180 } |
| 131 } | 181 } |
| 132 | 182 |
| 133 void TearDownOnMainThread() override { | 183 void TearDownOnMainThread() override { |
| 134 content::ResourceDispatcherHost::Get()->SetDelegate(NULL); | 184 content::ResourceDispatcherHost::Get()->SetDelegate(NULL); |
| 135 dispatcher_host_delegate_.reset(); | 185 dispatcher_host_delegate_.reset(); |
| 136 } | 186 } |
| 137 | 187 |
| 188 void SetShouldAddDataReductionProxyData(bool add_data) { |
| 189 dispatcher_host_delegate_->should_add_data_reduction_proxy_data_ = add_data; |
| 190 } |
| 191 |
| 138 protected: | 192 protected: |
| 139 // The fake URL for DMServer we are using. | 193 // The fake URL for DMServer we are using. |
| 140 GURL dm_url_; | 194 GURL dm_url_; |
| 141 std::unique_ptr<TestDispatcherHostDelegate> dispatcher_host_delegate_; | 195 std::unique_ptr<TestDispatcherHostDelegate> dispatcher_host_delegate_; |
| 142 | |
| 143 private: | 196 private: |
| 144 DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegateBrowserTest); | 197 DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegateBrowserTest); |
| 145 }; | 198 }; |
| 146 | 199 |
| 147 | 200 |
| 148 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, | 201 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, |
| 149 NoPolicyHeader) { | 202 NoPolicyHeader) { |
| 150 // When fetching non-DMServer URLs, we should not add a policy header to the | 203 // When fetching non-DMServer URLs, we should not add a policy header to the |
| 151 // request. | 204 // request. |
| 152 DCHECK(!embedded_test_server()->base_url().spec().empty()); | 205 DCHECK(!embedded_test_server()->base_url().spec().empty()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 175 redirect_url += kServerRedirectUrl; | 228 redirect_url += kServerRedirectUrl; |
| 176 redirect_url += "?"; | 229 redirect_url += "?"; |
| 177 redirect_url += dm_url_.spec(); | 230 redirect_url += dm_url_.spec(); |
| 178 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL( | 231 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL( |
| 179 redirect_url)); | 232 redirect_url)); |
| 180 std::string value; | 233 std::string value; |
| 181 ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader( | 234 ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader( |
| 182 policy::kChromePolicyHeader, &value)); | 235 policy::kChromePolicyHeader, &value)); |
| 183 ASSERT_EQ(kTestPolicyHeader, value); | 236 ASSERT_EQ(kTestPolicyHeader, value); |
| 184 } | 237 } |
| 238 |
| 239 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, |
| 240 NavigationDataProcessed) { |
| 241 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url()); |
| 242 { |
| 243 DidFinishNavigationObserver nav_observer( |
| 244 browser()->tab_strip_model()->GetActiveWebContents(), false); |
| 245 ui_test_utils::NavigateToURL( |
| 246 browser(), embedded_test_server()->GetURL("/google/google.html")); |
| 247 } |
| 248 SetShouldAddDataReductionProxyData(true); |
| 249 { |
| 250 DidFinishNavigationObserver nav_observer( |
| 251 browser()->tab_strip_model()->GetActiveWebContents(), true); |
| 252 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url()); |
| 253 } |
| 254 } |
| OLD | NEW |