| 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
| 7 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 7 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 8 #include "content/public/browser/navigation_entry.h" | 8 #include "content/public/browser/navigation_entry.h" |
| 9 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 9 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 10 #include "content/public/browser/resource_throttle.h" | 10 #include "content/public/browser/resource_throttle.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 57 |
| 58 // Create a RunLoop that will be stopped once the request for the tracked | 58 // Create a RunLoop that will be stopped once the request for the tracked |
| 59 // URL has been destroyed, to allow tracking the URL while also waiting for | 59 // URL has been destroyed, to allow tracking the URL while also waiting for |
| 60 // other events. | 60 // other events. |
| 61 run_loop_.reset(new base::RunLoop()); | 61 run_loop_.reset(new base::RunLoop()); |
| 62 | 62 |
| 63 BrowserThread::PostTask( | 63 BrowserThread::PostTask( |
| 64 BrowserThread::IO, FROM_HERE, | 64 BrowserThread::IO, FROM_HERE, |
| 65 base::Bind( | 65 base::Bind( |
| 66 &TrackingResourceDispatcherHostDelegate::SetTrackedURLOnIOThread, | 66 &TrackingResourceDispatcherHostDelegate::SetTrackedURLOnIOThread, |
| 67 base::Unretained(this), | 67 base::Unretained(this), tracked_url, run_loop_->QuitClosure())); |
| 68 tracked_url)); | |
| 69 } | 68 } |
| 70 | 69 |
| 71 // Waits until the tracked URL has been requests, and the request for it has | 70 // Waits until the tracked URL has been requested, and the request for it has |
| 72 // been destroyed. | 71 // been destroyed. |
| 73 bool WaitForTrackedURLAndGetCompleted() { | 72 bool WaitForTrackedURLAndGetCompleted() { |
| 74 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 73 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 75 run_loop_->Run(); | 74 run_loop_->Run(); |
| 76 run_loop_.reset(); | 75 run_loop_.reset(); |
| 77 return tracked_request_completed_; | 76 return tracked_request_completed_; |
| 78 } | 77 } |
| 79 | 78 |
| 80 private: | 79 private: |
| 81 // ResourceThrottle attached to request for the tracked URL. On destruction, | 80 // ResourceThrottle attached to request for the tracked URL. On destruction, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 100 return "TrackingThrottle"; | 99 return "TrackingThrottle"; |
| 101 } | 100 } |
| 102 | 101 |
| 103 private: | 102 private: |
| 104 net::URLRequest* request_; | 103 net::URLRequest* request_; |
| 105 TrackingResourceDispatcherHostDelegate* tracker_; | 104 TrackingResourceDispatcherHostDelegate* tracker_; |
| 106 | 105 |
| 107 DISALLOW_COPY_AND_ASSIGN(TrackingThrottle); | 106 DISALLOW_COPY_AND_ASSIGN(TrackingThrottle); |
| 108 }; | 107 }; |
| 109 | 108 |
| 110 void SetTrackedURLOnIOThread(const GURL& tracked_url) { | 109 void SetTrackedURLOnIOThread(const GURL& tracked_url, |
| 110 const base::Closure& run_loop_quit_closure) { |
| 111 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 111 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 112 throttle_created_ = false; | 112 throttle_created_ = false; |
| 113 tracked_url_ = tracked_url; | 113 tracked_url_ = tracked_url; |
| 114 run_loop_quit_closure_ = run_loop_quit_closure; |
| 114 } | 115 } |
| 115 | 116 |
| 116 void OnTrackedRequestDestroyed(bool completed) { | 117 void OnTrackedRequestDestroyed(bool completed) { |
| 117 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 118 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 118 tracked_request_completed_ = completed; | 119 tracked_request_completed_ = completed; |
| 119 tracked_url_ = GURL(); | 120 tracked_url_ = GURL(); |
| 120 | 121 |
| 121 BrowserThread::PostTask( | 122 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 122 BrowserThread::UI, FROM_HERE, run_loop_->QuitClosure()); | 123 run_loop_quit_closure_); |
| 123 } | 124 } |
| 124 | 125 |
| 125 // These live on the IO thread. | 126 // These live on the IO thread. |
| 126 GURL tracked_url_; | 127 GURL tracked_url_; |
| 127 bool throttle_created_; | 128 bool throttle_created_; |
| 129 base::Closure run_loop_quit_closure_; |
| 128 | 130 |
| 129 // This is created and destroyed on the UI thread, but stopped on the IO | 131 // This lives on the UI thread. |
| 130 // thread. | |
| 131 scoped_ptr<base::RunLoop> run_loop_; | 132 scoped_ptr<base::RunLoop> run_loop_; |
| 132 | 133 |
| 133 // Set on the IO thread while |run_loop_| is non-NULL, read on the UI thread | 134 // Set on the IO thread while |run_loop_| is non-NULL, read on the UI thread |
| 134 // after deleting run_loop_. | 135 // after deleting run_loop_. |
| 135 bool tracked_request_completed_; | 136 bool tracked_request_completed_; |
| 136 | 137 |
| 137 DISALLOW_COPY_AND_ASSIGN(TrackingResourceDispatcherHostDelegate); | 138 DISALLOW_COPY_AND_ASSIGN(TrackingResourceDispatcherHostDelegate); |
| 138 }; | 139 }; |
| 139 | 140 |
| 140 // WebContentsDelegate that fails to open a URL when there's a request that | 141 // WebContentsDelegate that fails to open a URL when there's a request that |
| (...skipping 26 matching lines...) Expand all Loading... |
| 167 | 168 |
| 168 class CrossSiteTransferTest : public ContentBrowserTest { | 169 class CrossSiteTransferTest : public ContentBrowserTest { |
| 169 public: | 170 public: |
| 170 CrossSiteTransferTest() : old_delegate_(NULL) { | 171 CrossSiteTransferTest() : old_delegate_(NULL) { |
| 171 } | 172 } |
| 172 | 173 |
| 173 // ContentBrowserTest implementation: | 174 // ContentBrowserTest implementation: |
| 174 void SetUpOnMainThread() override { | 175 void SetUpOnMainThread() override { |
| 175 BrowserThread::PostTask( | 176 BrowserThread::PostTask( |
| 176 BrowserThread::IO, FROM_HERE, | 177 BrowserThread::IO, FROM_HERE, |
| 177 base::Bind( | 178 base::Bind(&CrossSiteTransferTest::InjectResourceDispatcherHostDelegate, |
| 178 &CrossSiteTransferTest::InjectResourceDisptcherHostDelegate, | 179 base::Unretained(this))); |
| 179 base::Unretained(this))); | |
| 180 host_resolver()->AddRule("*", "127.0.0.1"); | 180 host_resolver()->AddRule("*", "127.0.0.1"); |
| 181 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 181 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 182 content::SetupCrossSiteRedirector(embedded_test_server()); | 182 content::SetupCrossSiteRedirector(embedded_test_server()); |
| 183 } | 183 } |
| 184 | 184 |
| 185 void TearDownOnMainThread() override { | 185 void TearDownOnMainThread() override { |
| 186 BrowserThread::PostTask( | 186 BrowserThread::PostTask( |
| 187 BrowserThread::IO, FROM_HERE, | 187 BrowserThread::IO, FROM_HERE, |
| 188 base::Bind( | 188 base::Bind( |
| 189 &CrossSiteTransferTest::RestoreResourceDisptcherHostDelegate, | 189 &CrossSiteTransferTest::RestoreResourceDisptcherHostDelegate, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 204 bool result = ExecuteScript(window->web_contents(), script); | 204 bool result = ExecuteScript(window->web_contents(), script); |
| 205 EXPECT_TRUE(result); | 205 EXPECT_TRUE(result); |
| 206 if (should_wait_for_navigation) | 206 if (should_wait_for_navigation) |
| 207 load_observer.Wait(); | 207 load_observer.Wait(); |
| 208 } | 208 } |
| 209 | 209 |
| 210 void SetUpCommandLine(base::CommandLine* command_line) override { | 210 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 211 IsolateAllSitesForTesting(command_line); | 211 IsolateAllSitesForTesting(command_line); |
| 212 } | 212 } |
| 213 | 213 |
| 214 void InjectResourceDisptcherHostDelegate() { | 214 void InjectResourceDispatcherHostDelegate() { |
| 215 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 215 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 216 old_delegate_ = ResourceDispatcherHostImpl::Get()->delegate(); | 216 old_delegate_ = ResourceDispatcherHostImpl::Get()->delegate(); |
| 217 ResourceDispatcherHostImpl::Get()->SetDelegate(&tracking_delegate_); | 217 ResourceDispatcherHostImpl::Get()->SetDelegate(&tracking_delegate_); |
| 218 } | 218 } |
| 219 | 219 |
| 220 void RestoreResourceDisptcherHostDelegate() { | 220 void RestoreResourceDisptcherHostDelegate() { |
| 221 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 221 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 222 ResourceDispatcherHostImpl::Get()->SetDelegate(old_delegate_); | 222 ResourceDispatcherHostImpl::Get()->SetDelegate(old_delegate_); |
| 223 old_delegate_ = NULL; | 223 old_delegate_ = NULL; |
| 224 } | 224 } |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 EXPECT_EQ(0, controller.GetCurrentEntryIndex()); | 425 EXPECT_EQ(0, controller.GetCurrentEntryIndex()); |
| 426 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL()); | 426 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL()); |
| 427 | 427 |
| 428 // Make sure the request for url2 did not complete. | 428 // Make sure the request for url2 did not complete. |
| 429 EXPECT_FALSE(tracking_delegate().WaitForTrackedURLAndGetCompleted()); | 429 EXPECT_FALSE(tracking_delegate().WaitForTrackedURLAndGetCompleted()); |
| 430 | 430 |
| 431 shell()->web_contents()->SetDelegate(old_delegate); | 431 shell()->web_contents()->SetDelegate(old_delegate); |
| 432 } | 432 } |
| 433 | 433 |
| 434 } // namespace content | 434 } // namespace content |
| OLD | NEW |