| 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 15 matching lines...) Expand all Loading... |
| 26 namespace content { | 26 namespace content { |
| 27 | 27 |
| 28 // Tracks a single request for a specified URL, and allows waiting until the | 28 // Tracks a single request for a specified URL, and allows waiting until the |
| 29 // request is destroyed, and then inspecting whether it completed successfully. | 29 // request is destroyed, and then inspecting whether it completed successfully. |
| 30 class TrackingResourceDispatcherHostDelegate | 30 class TrackingResourceDispatcherHostDelegate |
| 31 : public ShellResourceDispatcherHostDelegate { | 31 : public ShellResourceDispatcherHostDelegate { |
| 32 public: | 32 public: |
| 33 TrackingResourceDispatcherHostDelegate() : throttle_created_(false) { | 33 TrackingResourceDispatcherHostDelegate() : throttle_created_(false) { |
| 34 } | 34 } |
| 35 | 35 |
| 36 virtual void RequestBeginning( | 36 void RequestBeginning(net::URLRequest* request, |
| 37 net::URLRequest* request, | 37 ResourceContext* resource_context, |
| 38 ResourceContext* resource_context, | 38 AppCacheService* appcache_service, |
| 39 AppCacheService* appcache_service, | 39 ResourceType resource_type, |
| 40 ResourceType resource_type, | 40 ScopedVector<ResourceThrottle>* throttles) override { |
| 41 ScopedVector<ResourceThrottle>* throttles) override { | |
| 42 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 41 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 43 ShellResourceDispatcherHostDelegate::RequestBeginning( | 42 ShellResourceDispatcherHostDelegate::RequestBeginning( |
| 44 request, resource_context, appcache_service, resource_type, throttles); | 43 request, resource_context, appcache_service, resource_type, throttles); |
| 45 // Expect only a single request for the tracked url. | 44 // Expect only a single request for the tracked url. |
| 46 ASSERT_FALSE(throttle_created_); | 45 ASSERT_FALSE(throttle_created_); |
| 47 // If this is a request for the tracked URL, add a throttle to track it. | 46 // If this is a request for the tracked URL, add a throttle to track it. |
| 48 if (request->url() == tracked_url_) | 47 if (request->url() == tracked_url_) |
| 49 throttles->push_back(new TrackingThrottle(request, this)); | 48 throttles->push_back(new TrackingThrottle(request, this)); |
| 50 } | 49 } |
| 51 | 50 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 81 private: | 80 private: |
| 82 // ResourceThrottle attached to request for the tracked URL. On destruction, | 81 // ResourceThrottle attached to request for the tracked URL. On destruction, |
| 83 // passes the final URLRequestStatus back to the delegate. | 82 // passes the final URLRequestStatus back to the delegate. |
| 84 class TrackingThrottle : public ResourceThrottle { | 83 class TrackingThrottle : public ResourceThrottle { |
| 85 public: | 84 public: |
| 86 TrackingThrottle(net::URLRequest* request, | 85 TrackingThrottle(net::URLRequest* request, |
| 87 TrackingResourceDispatcherHostDelegate* tracker) | 86 TrackingResourceDispatcherHostDelegate* tracker) |
| 88 : request_(request), tracker_(tracker) { | 87 : request_(request), tracker_(tracker) { |
| 89 } | 88 } |
| 90 | 89 |
| 91 virtual ~TrackingThrottle() { | 90 ~TrackingThrottle() override { |
| 92 // If the request is deleted without being cancelled, its status will | 91 // If the request is deleted without being cancelled, its status will |
| 93 // indicate it succeeded, so have to check if the request is still pending | 92 // indicate it succeeded, so have to check if the request is still pending |
| 94 // as well. | 93 // as well. |
| 95 tracker_->OnTrackedRequestDestroyed( | 94 tracker_->OnTrackedRequestDestroyed( |
| 96 !request_->is_pending() && request_->status().is_success()); | 95 !request_->is_pending() && request_->status().is_success()); |
| 97 } | 96 } |
| 98 | 97 |
| 99 // ResourceThrottle implementation: | 98 // ResourceThrottle implementation: |
| 100 virtual const char* GetNameForLogging() const override { | 99 const char* GetNameForLogging() const override { |
| 101 return "TrackingThrottle"; | 100 return "TrackingThrottle"; |
| 102 } | 101 } |
| 103 | 102 |
| 104 private: | 103 private: |
| 105 net::URLRequest* request_; | 104 net::URLRequest* request_; |
| 106 TrackingResourceDispatcherHostDelegate* tracker_; | 105 TrackingResourceDispatcherHostDelegate* tracker_; |
| 107 | 106 |
| 108 DISALLOW_COPY_AND_ASSIGN(TrackingThrottle); | 107 DISALLOW_COPY_AND_ASSIGN(TrackingThrottle); |
| 109 }; | 108 }; |
| 110 | 109 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 137 | 136 |
| 138 DISALLOW_COPY_AND_ASSIGN(TrackingResourceDispatcherHostDelegate); | 137 DISALLOW_COPY_AND_ASSIGN(TrackingResourceDispatcherHostDelegate); |
| 139 }; | 138 }; |
| 140 | 139 |
| 141 // WebContentsDelegate that fails to open a URL when there's a request that | 140 // WebContentsDelegate that fails to open a URL when there's a request that |
| 142 // needs to be transferred between renderers. | 141 // needs to be transferred between renderers. |
| 143 class NoTransferRequestDelegate : public WebContentsDelegate { | 142 class NoTransferRequestDelegate : public WebContentsDelegate { |
| 144 public: | 143 public: |
| 145 NoTransferRequestDelegate() {} | 144 NoTransferRequestDelegate() {} |
| 146 | 145 |
| 147 virtual WebContents* OpenURLFromTab(WebContents* source, | 146 WebContents* OpenURLFromTab(WebContents* source, |
| 148 const OpenURLParams& params) override { | 147 const OpenURLParams& params) override { |
| 149 bool is_transfer = | 148 bool is_transfer = |
| 150 (params.transferred_global_request_id != GlobalRequestID()); | 149 (params.transferred_global_request_id != GlobalRequestID()); |
| 151 if (is_transfer) | 150 if (is_transfer) |
| 152 return NULL; | 151 return NULL; |
| 153 NavigationController::LoadURLParams load_url_params(params.url); | 152 NavigationController::LoadURLParams load_url_params(params.url); |
| 154 load_url_params.referrer = params.referrer; | 153 load_url_params.referrer = params.referrer; |
| 155 load_url_params.frame_tree_node_id = params.frame_tree_node_id; | 154 load_url_params.frame_tree_node_id = params.frame_tree_node_id; |
| 156 load_url_params.transition_type = params.transition; | 155 load_url_params.transition_type = params.transition; |
| 157 load_url_params.extra_headers = params.extra_headers; | 156 load_url_params.extra_headers = params.extra_headers; |
| 158 load_url_params.should_replace_current_entry = | 157 load_url_params.should_replace_current_entry = |
| 159 params.should_replace_current_entry; | 158 params.should_replace_current_entry; |
| 160 load_url_params.is_renderer_initiated = true; | 159 load_url_params.is_renderer_initiated = true; |
| 161 source->GetController().LoadURLWithParams(load_url_params); | 160 source->GetController().LoadURLWithParams(load_url_params); |
| 162 return source; | 161 return source; |
| 163 } | 162 } |
| 164 | 163 |
| 165 private: | 164 private: |
| 166 DISALLOW_COPY_AND_ASSIGN(NoTransferRequestDelegate); | 165 DISALLOW_COPY_AND_ASSIGN(NoTransferRequestDelegate); |
| 167 }; | 166 }; |
| 168 | 167 |
| 169 class CrossSiteTransferTest : public ContentBrowserTest { | 168 class CrossSiteTransferTest : public ContentBrowserTest { |
| 170 public: | 169 public: |
| 171 CrossSiteTransferTest() : old_delegate_(NULL) { | 170 CrossSiteTransferTest() : old_delegate_(NULL) { |
| 172 } | 171 } |
| 173 | 172 |
| 174 // ContentBrowserTest implementation: | 173 // ContentBrowserTest implementation: |
| 175 virtual void SetUpOnMainThread() override { | 174 void SetUpOnMainThread() override { |
| 176 BrowserThread::PostTask( | 175 BrowserThread::PostTask( |
| 177 BrowserThread::IO, FROM_HERE, | 176 BrowserThread::IO, FROM_HERE, |
| 178 base::Bind( | 177 base::Bind( |
| 179 &CrossSiteTransferTest::InjectResourceDisptcherHostDelegate, | 178 &CrossSiteTransferTest::InjectResourceDisptcherHostDelegate, |
| 180 base::Unretained(this))); | 179 base::Unretained(this))); |
| 181 } | 180 } |
| 182 | 181 |
| 183 virtual void TearDownOnMainThread() override { | 182 void TearDownOnMainThread() override { |
| 184 BrowserThread::PostTask( | 183 BrowserThread::PostTask( |
| 185 BrowserThread::IO, FROM_HERE, | 184 BrowserThread::IO, FROM_HERE, |
| 186 base::Bind( | 185 base::Bind( |
| 187 &CrossSiteTransferTest::RestoreResourceDisptcherHostDelegate, | 186 &CrossSiteTransferTest::RestoreResourceDisptcherHostDelegate, |
| 188 base::Unretained(this))); | 187 base::Unretained(this))); |
| 189 } | 188 } |
| 190 | 189 |
| 191 protected: | 190 protected: |
| 192 void NavigateToURLContentInitiated(Shell* window, | 191 void NavigateToURLContentInitiated(Shell* window, |
| 193 const GURL& url, | 192 const GURL& url, |
| 194 bool should_replace_current_entry, | 193 bool should_replace_current_entry, |
| 195 bool should_wait_for_navigation) { | 194 bool should_wait_for_navigation) { |
| 196 std::string script; | 195 std::string script; |
| 197 if (should_replace_current_entry) | 196 if (should_replace_current_entry) |
| 198 script = base::StringPrintf("location.replace('%s')", url.spec().c_str()); | 197 script = base::StringPrintf("location.replace('%s')", url.spec().c_str()); |
| 199 else | 198 else |
| 200 script = base::StringPrintf("location.href = '%s'", url.spec().c_str()); | 199 script = base::StringPrintf("location.href = '%s'", url.spec().c_str()); |
| 201 TestNavigationObserver load_observer(shell()->web_contents(), 1); | 200 TestNavigationObserver load_observer(shell()->web_contents(), 1); |
| 202 bool result = ExecuteScript(window->web_contents(), script); | 201 bool result = ExecuteScript(window->web_contents(), script); |
| 203 EXPECT_TRUE(result); | 202 EXPECT_TRUE(result); |
| 204 if (should_wait_for_navigation) | 203 if (should_wait_for_navigation) |
| 205 load_observer.Wait(); | 204 load_observer.Wait(); |
| 206 } | 205 } |
| 207 | 206 |
| 208 virtual void SetUpCommandLine(CommandLine* command_line) override { | 207 void SetUpCommandLine(CommandLine* command_line) override { |
| 209 // Use --site-per-process to force process swaps for cross-site transfers. | 208 // Use --site-per-process to force process swaps for cross-site transfers. |
| 210 command_line->AppendSwitch(switches::kSitePerProcess); | 209 command_line->AppendSwitch(switches::kSitePerProcess); |
| 211 } | 210 } |
| 212 | 211 |
| 213 void InjectResourceDisptcherHostDelegate() { | 212 void InjectResourceDisptcherHostDelegate() { |
| 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 215 old_delegate_ = ResourceDispatcherHostImpl::Get()->delegate(); | 214 old_delegate_ = ResourceDispatcherHostImpl::Get()->delegate(); |
| 216 ResourceDispatcherHostImpl::Get()->SetDelegate(&tracking_delegate_); | 215 ResourceDispatcherHostImpl::Get()->SetDelegate(&tracking_delegate_); |
| 217 } | 216 } |
| 218 | 217 |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 EXPECT_EQ(0, controller.GetCurrentEntryIndex()); | 453 EXPECT_EQ(0, controller.GetCurrentEntryIndex()); |
| 455 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL()); | 454 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL()); |
| 456 | 455 |
| 457 // Make sure the request for url2 did not complete. | 456 // Make sure the request for url2 did not complete. |
| 458 EXPECT_FALSE(tracking_delegate().WaitForTrackedURLAndGetCompleted()); | 457 EXPECT_FALSE(tracking_delegate().WaitForTrackedURLAndGetCompleted()); |
| 459 | 458 |
| 460 shell()->web_contents()->SetDelegate(old_delegate); | 459 shell()->web_contents()->SetDelegate(old_delegate); |
| 461 } | 460 } |
| 462 | 461 |
| 463 } // namespace content | 462 } // namespace content |
| OLD | NEW |