Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/callback.h" | 5 #include "base/callback.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "content/browser/frame_host/render_frame_host_impl.h" | 12 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 13 #include "content/browser/loader/loader_globals.h" | |
| 13 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 14 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 14 #include "content/browser/web_contents/web_contents_impl.h" | 15 #include "content/browser/web_contents/web_contents_impl.h" |
| 15 #include "content/common/frame_messages.h" | 16 #include "content/common/frame_messages.h" |
| 16 #include "content/public/browser/resource_dispatcher_host.h" | 17 #include "content/public/browser/resource_dispatcher_host.h" |
| 17 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 18 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 18 #include "content/public/browser/resource_throttle.h" | 19 #include "content/public/browser/resource_throttle.h" |
| 19 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/test/browser_test_utils.h" | 21 #include "content/public/test/browser_test_utils.h" |
| 21 #include "content/public/test/content_browser_test.h" | 22 #include "content/public/test/content_browser_test.h" |
| 22 #include "content/public/test/content_browser_test_utils.h" | 23 #include "content/public/test/content_browser_test_utils.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 39 public: | 40 public: |
| 40 using RequestDeferredHook = base::Callback<void(const base::Closure& resume)>; | 41 using RequestDeferredHook = base::Callback<void(const base::Closure& resume)>; |
| 41 TestResourceDispatcherHostDelegate() : throttle_created_(false) {} | 42 TestResourceDispatcherHostDelegate() : throttle_created_(false) {} |
| 42 | 43 |
| 43 void RequestBeginning( | 44 void RequestBeginning( |
| 44 net::URLRequest* request, | 45 net::URLRequest* request, |
| 45 ResourceContext* resource_context, | 46 ResourceContext* resource_context, |
| 46 AppCacheService* appcache_service, | 47 AppCacheService* appcache_service, |
| 47 ResourceType resource_type, | 48 ResourceType resource_type, |
| 48 std::vector<std::unique_ptr<ResourceThrottle>>* throttles) override { | 49 std::vector<std::unique_ptr<ResourceThrottle>>* throttles) override { |
| 49 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 50 CHECK(LoaderGlobals::Get() |
|
jam
2017/03/29 15:44:32
this file isn't going to moveee (browser tests nee
ananta
2017/03/29 19:41:04
ok. thanks. done
| |
| 51 ->io_thread_task_runner() | |
| 52 ->BelongsToCurrentThread()); | |
| 50 ShellResourceDispatcherHostDelegate::RequestBeginning( | 53 ShellResourceDispatcherHostDelegate::RequestBeginning( |
| 51 request, resource_context, appcache_service, resource_type, throttles); | 54 request, resource_context, appcache_service, resource_type, throttles); |
| 52 | 55 |
| 53 // If this is a request for the tracked URL, add a throttle to track it. | 56 // If this is a request for the tracked URL, add a throttle to track it. |
| 54 if (request->url() == tracked_url_) { | 57 if (request->url() == tracked_url_) { |
| 55 // Expect only a single request for the tracked url. | 58 // Expect only a single request for the tracked url. |
| 56 ASSERT_FALSE(throttle_created_); | 59 ASSERT_FALSE(throttle_created_); |
| 57 throttle_created_ = true; | 60 throttle_created_ = true; |
| 58 | 61 |
| 59 throttles->push_back(base::MakeUnique<CallbackRunningResourceThrottle>( | 62 throttles->push_back(base::MakeUnique<CallbackRunningResourceThrottle>( |
| 60 request, this, run_on_start_)); | 63 request, this, run_on_start_)); |
| 61 } | 64 } |
| 62 } | 65 } |
| 63 | 66 |
| 64 // Starts tracking a URL. The request for previously tracked URL, if any, | 67 // Starts tracking a URL. The request for previously tracked URL, if any, |
| 65 // must have been made and deleted before calling this function. | 68 // must have been made and deleted before calling this function. |
| 66 void SetTrackedURL(const GURL& tracked_url, | 69 void SetTrackedURL(const GURL& tracked_url, |
| 67 const RequestDeferredHook& run_on_start) { | 70 const RequestDeferredHook& run_on_start) { |
| 68 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 71 CHECK(LoaderGlobals::Get() |
| 72 ->main_thread_task_runner() | |
| 73 ->BelongsToCurrentThread()); | |
| 69 // Should not currently be tracking any URL. | 74 // Should not currently be tracking any URL. |
| 70 ASSERT_FALSE(run_loop_); | 75 ASSERT_FALSE(run_loop_); |
| 71 | 76 |
| 72 // Create a RunLoop that will be stopped once the request for the tracked | 77 // Create a RunLoop that will be stopped once the request for the tracked |
| 73 // URL has been destroyed, to allow tracking the URL while also waiting for | 78 // URL has been destroyed, to allow tracking the URL while also waiting for |
| 74 // other events. | 79 // other events. |
| 75 run_loop_.reset(new base::RunLoop()); | 80 run_loop_.reset(new base::RunLoop()); |
| 76 | 81 |
| 77 BrowserThread::PostTask( | 82 LoaderGlobals::Get()->io_thread_task_runner()->PostTask( |
| 78 BrowserThread::IO, FROM_HERE, | 83 FROM_HERE, |
| 79 base::Bind(&TestResourceDispatcherHostDelegate::SetTrackedURLOnIOThread, | 84 base::Bind(&TestResourceDispatcherHostDelegate::SetTrackedURLOnIOThread, |
| 80 base::Unretained(this), tracked_url, run_on_start, | 85 base::Unretained(this), tracked_url, run_on_start, |
| 81 run_loop_->QuitClosure())); | 86 run_loop_->QuitClosure())); |
| 82 } | 87 } |
| 83 | 88 |
| 84 // Waits until the tracked URL has been requested, and the request for it has | 89 // Waits until the tracked URL has been requested, and the request for it has |
| 85 // been destroyed. | 90 // been destroyed. |
| 86 bool WaitForTrackedURLAndGetCompleted() { | 91 bool WaitForTrackedURLAndGetCompleted() { |
| 87 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 92 CHECK(LoaderGlobals::Get() |
| 93 ->main_thread_task_runner() | |
| 94 ->BelongsToCurrentThread()); | |
| 88 run_loop_->Run(); | 95 run_loop_->Run(); |
| 89 run_loop_.reset(); | 96 run_loop_.reset(); |
| 90 return tracked_request_completed_; | 97 return tracked_request_completed_; |
| 91 } | 98 } |
| 92 | 99 |
| 93 private: | 100 private: |
| 94 // A ResourceThrottle which defers the request at WillStartRequest time until | 101 // A ResourceThrottle which defers the request at WillStartRequest time until |
| 95 // a test-supplied callback completes. Notifies |tracker| when the request is | 102 // a test-supplied callback completes. Notifies |tracker| when the request is |
| 96 // destroyed. | 103 // destroyed. |
| 97 class CallbackRunningResourceThrottle : public ResourceThrottle { | 104 class CallbackRunningResourceThrottle : public ResourceThrottle { |
| 98 public: | 105 public: |
| 99 CallbackRunningResourceThrottle(net::URLRequest* request, | 106 CallbackRunningResourceThrottle(net::URLRequest* request, |
| 100 TestResourceDispatcherHostDelegate* tracker, | 107 TestResourceDispatcherHostDelegate* tracker, |
| 101 const RequestDeferredHook& run_on_start) | 108 const RequestDeferredHook& run_on_start) |
| 102 : resumed_(false), | 109 : resumed_(false), |
| 103 request_(request), | 110 request_(request), |
| 104 tracker_(tracker), | 111 tracker_(tracker), |
| 105 run_on_start_(run_on_start), | 112 run_on_start_(run_on_start), |
| 106 weak_factory_(this) {} | 113 weak_factory_(this) {} |
| 107 | 114 |
| 108 void WillStartRequest(bool* defer) override { | 115 void WillStartRequest(bool* defer) override { |
| 109 *defer = true; | 116 *defer = true; |
| 110 base::Closure resume_request_on_io_thread = base::Bind( | 117 base::Closure resume_request_on_io_thread = base::Bind( |
| 111 base::IgnoreResult(&BrowserThread::PostTask), BrowserThread::IO, | 118 base::IgnoreResult(&base::SingleThreadTaskRunner::PostTask), |
| 112 FROM_HERE, base::Bind(&CallbackRunningResourceThrottle::MarkAndResume, | 119 LoaderGlobals::Get()->io_thread_task_runner().get(), FROM_HERE, |
| 113 weak_factory_.GetWeakPtr())); | 120 base::Bind(&CallbackRunningResourceThrottle::MarkAndResume, |
| 114 BrowserThread::PostTask( | 121 weak_factory_.GetWeakPtr())); |
| 115 BrowserThread::UI, FROM_HERE, | 122 LoaderGlobals::Get()->main_thread_task_runner()->PostTask( |
| 116 base::Bind(run_on_start_, resume_request_on_io_thread)); | 123 FROM_HERE, base::Bind(run_on_start_, resume_request_on_io_thread)); |
| 117 } | 124 } |
| 118 | 125 |
| 119 ~CallbackRunningResourceThrottle() override { | 126 ~CallbackRunningResourceThrottle() override { |
| 120 // If the request is deleted without being cancelled, its status will | 127 // If the request is deleted without being cancelled, its status will |
| 121 // indicate it succeeded, so have to check if the request is still pending | 128 // indicate it succeeded, so have to check if the request is still pending |
| 122 // as well. If the request never even started, the throttle will never | 129 // as well. If the request never even started, the throttle will never |
| 123 // resume it. Check this condition as well to allow for early | 130 // resume it. Check this condition as well to allow for early |
| 124 // cancellation. | 131 // cancellation. |
| 125 tracker_->OnTrackedRequestDestroyed(!request_->is_pending() && | 132 tracker_->OnTrackedRequestDestroyed(!request_->is_pending() && |
| 126 request_->status().is_success() && | 133 request_->status().is_success() && |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 143 TestResourceDispatcherHostDelegate* tracker_; | 150 TestResourceDispatcherHostDelegate* tracker_; |
| 144 RequestDeferredHook run_on_start_; | 151 RequestDeferredHook run_on_start_; |
| 145 base::WeakPtrFactory<CallbackRunningResourceThrottle> weak_factory_; | 152 base::WeakPtrFactory<CallbackRunningResourceThrottle> weak_factory_; |
| 146 | 153 |
| 147 DISALLOW_COPY_AND_ASSIGN(CallbackRunningResourceThrottle); | 154 DISALLOW_COPY_AND_ASSIGN(CallbackRunningResourceThrottle); |
| 148 }; | 155 }; |
| 149 | 156 |
| 150 void SetTrackedURLOnIOThread(const GURL& tracked_url, | 157 void SetTrackedURLOnIOThread(const GURL& tracked_url, |
| 151 const RequestDeferredHook& run_on_start, | 158 const RequestDeferredHook& run_on_start, |
| 152 const base::Closure& run_loop_quit_closure) { | 159 const base::Closure& run_loop_quit_closure) { |
| 153 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 160 CHECK(LoaderGlobals::Get() |
| 161 ->io_thread_task_runner() | |
| 162 ->BelongsToCurrentThread()); | |
| 154 throttle_created_ = false; | 163 throttle_created_ = false; |
| 155 tracked_url_ = tracked_url; | 164 tracked_url_ = tracked_url; |
| 156 run_on_start_ = run_on_start; | 165 run_on_start_ = run_on_start; |
| 157 run_loop_quit_closure_ = run_loop_quit_closure; | 166 run_loop_quit_closure_ = run_loop_quit_closure; |
| 158 } | 167 } |
| 159 | 168 |
| 160 void OnTrackedRequestDestroyed(bool completed) { | 169 void OnTrackedRequestDestroyed(bool completed) { |
| 161 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 170 CHECK(LoaderGlobals::Get() |
| 171 ->io_thread_task_runner() | |
| 172 ->BelongsToCurrentThread()); | |
| 162 tracked_request_completed_ = completed; | 173 tracked_request_completed_ = completed; |
| 163 tracked_url_ = GURL(); | 174 tracked_url_ = GURL(); |
| 164 run_on_start_ = RequestDeferredHook(); | 175 run_on_start_ = RequestDeferredHook(); |
| 165 | 176 |
| 166 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 177 LoaderGlobals::Get()->main_thread_task_runner()->PostTask( |
| 167 run_loop_quit_closure_); | 178 FROM_HERE, run_loop_quit_closure_); |
| 168 } | 179 } |
| 169 | 180 |
| 170 // These live on the IO thread. | 181 // These live on the IO thread. |
| 171 GURL tracked_url_; | 182 GURL tracked_url_; |
| 172 bool throttle_created_; | 183 bool throttle_created_; |
| 173 base::Closure run_loop_quit_closure_; | 184 base::Closure run_loop_quit_closure_; |
| 174 RequestDeferredHook run_on_start_; | 185 RequestDeferredHook run_on_start_; |
| 175 | 186 |
| 176 // This lives on the UI thread. | 187 // This lives on the UI thread. |
| 177 std::unique_ptr<base::RunLoop> run_loop_; | 188 std::unique_ptr<base::RunLoop> run_loop_; |
| 178 | 189 |
| 179 // Set on the IO thread while |run_loop_| is non-nullptr, read on the UI | 190 // Set on the IO thread while |run_loop_| is non-nullptr, read on the UI |
| 180 // thread after deleting run_loop_. | 191 // thread after deleting run_loop_. |
| 181 bool tracked_request_completed_; | 192 bool tracked_request_completed_; |
| 182 | 193 |
| 183 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherHostDelegate); | 194 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherHostDelegate); |
| 184 }; | 195 }; |
| 185 | 196 |
| 186 class CrossSiteResourceHandlerTest : public ContentBrowserTest { | 197 class CrossSiteResourceHandlerTest : public ContentBrowserTest { |
| 187 public: | 198 public: |
| 188 CrossSiteResourceHandlerTest() : old_delegate_(nullptr) {} | 199 CrossSiteResourceHandlerTest() : old_delegate_(nullptr) {} |
| 189 | 200 |
| 190 // ContentBrowserTest implementation: | 201 // ContentBrowserTest implementation: |
| 191 void SetUpOnMainThread() override { | 202 void SetUpOnMainThread() override { |
| 192 BrowserThread::PostTask( | 203 LoaderGlobals::Get()->io_thread_task_runner()->PostTask( |
| 193 BrowserThread::IO, FROM_HERE, | 204 FROM_HERE, |
| 194 base::Bind( | 205 base::Bind( |
| 195 &CrossSiteResourceHandlerTest::InjectResourceDispatcherHostDelegate, | 206 &CrossSiteResourceHandlerTest::InjectResourceDispatcherHostDelegate, |
| 196 base::Unretained(this))); | 207 base::Unretained(this))); |
| 197 host_resolver()->AddRule("*", "127.0.0.1"); | 208 host_resolver()->AddRule("*", "127.0.0.1"); |
| 198 content::SetupCrossSiteRedirector(embedded_test_server()); | 209 content::SetupCrossSiteRedirector(embedded_test_server()); |
| 199 ASSERT_TRUE(embedded_test_server()->Start()); | 210 ASSERT_TRUE(embedded_test_server()->Start()); |
| 200 } | 211 } |
| 201 | 212 |
| 202 void TearDownOnMainThread() override { | 213 void TearDownOnMainThread() override { |
| 203 BrowserThread::PostTask( | 214 LoaderGlobals::Get()->io_thread_task_runner()->PostTask( |
| 204 BrowserThread::IO, FROM_HERE, | 215 FROM_HERE, base::Bind(&CrossSiteResourceHandlerTest:: |
| 205 base::Bind(&CrossSiteResourceHandlerTest:: | 216 RestoreResourceDispatcherHostDelegate, |
| 206 RestoreResourceDispatcherHostDelegate, | 217 base::Unretained(this))); |
| 207 base::Unretained(this))); | |
| 208 } | 218 } |
| 209 | 219 |
| 210 protected: | 220 protected: |
| 211 void SetUpCommandLine(base::CommandLine* command_line) override { | 221 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 212 IsolateAllSitesForTesting(command_line); | 222 IsolateAllSitesForTesting(command_line); |
| 213 } | 223 } |
| 214 | 224 |
| 215 void InjectResourceDispatcherHostDelegate() { | 225 void InjectResourceDispatcherHostDelegate() { |
| 216 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 226 DCHECK(LoaderGlobals::Get() |
| 227 ->io_thread_task_runner() | |
| 228 ->BelongsToCurrentThread()); | |
| 217 old_delegate_ = ResourceDispatcherHostImpl::Get()->delegate(); | 229 old_delegate_ = ResourceDispatcherHostImpl::Get()->delegate(); |
| 218 ResourceDispatcherHostImpl::Get()->SetDelegate(&tracking_delegate_); | 230 ResourceDispatcherHostImpl::Get()->SetDelegate(&tracking_delegate_); |
| 219 } | 231 } |
| 220 | 232 |
| 221 void RestoreResourceDispatcherHostDelegate() { | 233 void RestoreResourceDispatcherHostDelegate() { |
| 222 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 234 DCHECK(LoaderGlobals::Get() |
| 235 ->io_thread_task_runner() | |
| 236 ->BelongsToCurrentThread()); | |
| 223 ResourceDispatcherHostImpl::Get()->SetDelegate(old_delegate_); | 237 ResourceDispatcherHostImpl::Get()->SetDelegate(old_delegate_); |
| 224 old_delegate_ = nullptr; | 238 old_delegate_ = nullptr; |
| 225 } | 239 } |
| 226 | 240 |
| 227 TestResourceDispatcherHostDelegate& tracking_delegate() { | 241 TestResourceDispatcherHostDelegate& tracking_delegate() { |
| 228 return tracking_delegate_; | 242 return tracking_delegate_; |
| 229 } | 243 } |
| 230 | 244 |
| 231 private: | 245 private: |
| 232 TestResourceDispatcherHostDelegate tracking_delegate_; | 246 TestResourceDispatcherHostDelegate tracking_delegate_; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 base::StringPrintf("document.getElementById('child-0').src='%s'", | 294 base::StringPrintf("document.getElementById('child-0').src='%s'", |
| 281 target_resource.spec().c_str()))); | 295 target_resource.spec().c_str()))); |
| 282 | 296 |
| 283 // Wait for the scenario to play out. If this returns false, it means the | 297 // Wait for the scenario to play out. If this returns false, it means the |
| 284 // request did not succeed, which is good in this case. | 298 // request did not succeed, which is good in this case. |
| 285 EXPECT_FALSE(tracking_delegate().WaitForTrackedURLAndGetCompleted()) | 299 EXPECT_FALSE(tracking_delegate().WaitForTrackedURLAndGetCompleted()) |
| 286 << "Request should have been cancelled before reaching the renderer."; | 300 << "Request should have been cancelled before reaching the renderer."; |
| 287 } | 301 } |
| 288 | 302 |
| 289 } // namespace content | 303 } // namespace content |
| OLD | NEW |