Chromium Code Reviews| 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() : 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 net::URLRequest* request) const override { | |
| 102 if (add_data_reduction_proxy_data_) { | |
| 103 data_reduction_proxy::DataReductionProxyData* data = | |
| 104 data_reduction_proxy::DataReductionProxyData::DataForRequest(request, | |
| 105 true); | |
| 106 data->set_used_data_reduction_proxy(true); | |
| 107 } | |
| 108 return ChromeResourceDispatcherHostDelegate::GetNavigationData(request); | |
| 109 } | |
| 110 | |
| 111 bool add_data_reduction_proxy_data_; | |
|
bengr
2016/04/29 21:14:21
Ideally, you'd rename as should_add_data_reduction
RyanSturm
2016/05/02 19:52:19
Done.
| |
| 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 it verify NavigationData being | |
|
bengr
2016/04/29 21:14:21
"and it verify" ?
RyanSturm
2016/05/02 19:52:20
Done.
| |
| 119 // added to NavigationHandle and pause/resume execution of the test. | |
| 120 class DidFinishNavigationObserver : public content::WebContentsObserver { | |
| 121 public: | |
| 122 explicit 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 message_loop_runner_(new content::MessageLoopRunner) {} | |
| 127 ~DidFinishNavigationObserver() override {} | |
| 128 | |
| 129 // Runs a nested message loop and blocks until the full load has | |
| 130 // completed. | |
| 131 void Wait() { message_loop_runner_->Run(); } | |
| 132 | |
| 133 void DidFinishNavigation( | |
| 134 content::NavigationHandle* navigation_handle) override { | |
| 135 ChromeNavigationData* data = static_cast<ChromeNavigationData*>( | |
| 136 navigation_handle->GetNavigationData()); | |
| 137 if (!add_data_reduction_proxy_data_) { | |
| 138 EXPECT_TRUE(!data->GetDataReductionProxyData()); | |
|
bengr
2016/04/29 21:14:21
I would flip this around:
EXPECT_FALSE(data->GetDa
RyanSturm
2016/05/02 19:52:19
Done.
| |
| 139 } else { | |
| 140 EXPECT_FALSE(!data->GetDataReductionProxyData()); | |
| 141 EXPECT_TRUE( | |
| 142 data->GetDataReductionProxyData()->get_used_data_reduction_proxy()); | |
| 143 } | |
| 144 if (message_loop_runner_->loop_running()) | |
| 145 message_loop_runner_->Quit(); | |
| 146 } | |
| 147 | |
| 148 std::unique_ptr<content::NavigationData> cloned_data; | |
|
bengr
2016/04/29 21:14:21
Where is this used?
RyanSturm
2016/05/02 19:52:20
Done.
| |
| 149 | |
| 150 private: | |
| 151 bool add_data_reduction_proxy_data_; | |
| 152 // The MessageLoopRunner used to spin the message loop. | |
|
bengr
2016/04/29 21:14:21
This comment is pretty useless. Delete.
RyanSturm
2016/05/02 19:52:19
Done.
| |
| 153 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
| 154 DISALLOW_COPY_AND_ASSIGN(DidFinishNavigationObserver); | |
| 155 }; | |
| 156 | |
| 97 } // namespace | 157 } // namespace |
| 98 | 158 |
| 99 class ChromeResourceDispatcherHostDelegateBrowserTest : | 159 class ChromeResourceDispatcherHostDelegateBrowserTest : |
| 100 public InProcessBrowserTest { | 160 public InProcessBrowserTest { |
| 101 public: | 161 public: |
| 102 ChromeResourceDispatcherHostDelegateBrowserTest() {} | 162 ChromeResourceDispatcherHostDelegateBrowserTest() {} |
| 103 | 163 |
| 104 void SetUpOnMainThread() override { | 164 void SetUpOnMainThread() override { |
| 105 InProcessBrowserTest::SetUpOnMainThread(); | 165 InProcessBrowserTest::SetUpOnMainThread(); |
| 106 // Hook navigations with our delegate. | 166 // Hook navigations with our delegate. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 128 (*it)->SetServerURLForTest(dm_url_.spec()); | 188 (*it)->SetServerURLForTest(dm_url_.spec()); |
| 129 (*it)->UpdateHeader(kTestPolicyHeader); | 189 (*it)->UpdateHeader(kTestPolicyHeader); |
| 130 } | 190 } |
| 131 } | 191 } |
| 132 | 192 |
| 133 void TearDownOnMainThread() override { | 193 void TearDownOnMainThread() override { |
| 134 content::ResourceDispatcherHost::Get()->SetDelegate(NULL); | 194 content::ResourceDispatcherHost::Get()->SetDelegate(NULL); |
| 135 dispatcher_host_delegate_.reset(); | 195 dispatcher_host_delegate_.reset(); |
| 136 } | 196 } |
| 137 | 197 |
| 198 void SetAddDataReductionProxyData(bool add_data) { | |
|
bengr
2016/04/29 21:14:21
should_add_data
RyanSturm
2016/05/02 19:52:20
Done.
| |
| 199 dispatcher_host_delegate_->add_data_reduction_proxy_data_ = add_data; | |
| 200 } | |
| 201 | |
| 138 protected: | 202 protected: |
| 139 // The fake URL for DMServer we are using. | 203 // The fake URL for DMServer we are using. |
| 140 GURL dm_url_; | 204 GURL dm_url_; |
| 141 std::unique_ptr<TestDispatcherHostDelegate> dispatcher_host_delegate_; | 205 std::unique_ptr<TestDispatcherHostDelegate> dispatcher_host_delegate_; |
| 142 | |
| 143 private: | 206 private: |
| 144 DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegateBrowserTest); | 207 DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegateBrowserTest); |
| 145 }; | 208 }; |
| 146 | 209 |
| 147 | 210 |
| 148 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, | 211 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, |
| 149 NoPolicyHeader) { | 212 NoPolicyHeader) { |
| 150 // When fetching non-DMServer URLs, we should not add a policy header to the | 213 // When fetching non-DMServer URLs, we should not add a policy header to the |
| 151 // request. | 214 // request. |
| 152 DCHECK(!embedded_test_server()->base_url().spec().empty()); | 215 DCHECK(!embedded_test_server()->base_url().spec().empty()); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 175 redirect_url += kServerRedirectUrl; | 238 redirect_url += kServerRedirectUrl; |
| 176 redirect_url += "?"; | 239 redirect_url += "?"; |
| 177 redirect_url += dm_url_.spec(); | 240 redirect_url += dm_url_.spec(); |
| 178 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL( | 241 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL( |
| 179 redirect_url)); | 242 redirect_url)); |
| 180 std::string value; | 243 std::string value; |
| 181 ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader( | 244 ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader( |
| 182 policy::kChromePolicyHeader, &value)); | 245 policy::kChromePolicyHeader, &value)); |
| 183 ASSERT_EQ(kTestPolicyHeader, value); | 246 ASSERT_EQ(kTestPolicyHeader, value); |
| 184 } | 247 } |
| 248 | |
| 249 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, | |
| 250 NavigationDataProcessed) { | |
| 251 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url()); | |
| 252 { | |
| 253 DidFinishNavigationObserver nav_observer( | |
| 254 browser()->tab_strip_model()->GetActiveWebContents(), false); | |
| 255 EXPECT_TRUE(content::ExecuteScript( | |
| 256 browser()->tab_strip_model()->GetActiveWebContents(), | |
| 257 "window.location.href = '" + | |
| 258 embedded_test_server()->GetURL("/google/google.html").spec() + | |
| 259 "';")); | |
| 260 nav_observer.Wait(); | |
| 261 } | |
| 262 SetAddDataReductionProxyData(true); | |
| 263 { | |
| 264 DidFinishNavigationObserver nav_observer( | |
| 265 browser()->tab_strip_model()->GetActiveWebContents(), true); | |
| 266 EXPECT_TRUE(content::ExecuteScript( | |
| 267 browser()->tab_strip_model()->GetActiveWebContents(), | |
| 268 "window.location.href = '" + embedded_test_server()->base_url().spec() + | |
|
bengr
2016/04/29 21:14:21
This is weird to me. Are you just checking that yo
RyanSturm
2016/05/02 19:52:20
Fixed the weirdness.
| |
| 269 "';")); | |
| 270 nav_observer.Wait(); | |
| 271 } | |
| 272 } | |
| OLD | NEW |