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/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 90 |
91 private: | 91 private: |
92 // A ResourceThrottle which defers the request at WillStartRequest time until | 92 // A ResourceThrottle which defers the request at WillStartRequest time until |
93 // a test-supplied callback completes. Notifies |tracker| when the request is | 93 // a test-supplied callback completes. Notifies |tracker| when the request is |
94 // destroyed. | 94 // destroyed. |
95 class CallbackRunningResourceThrottle : public ResourceThrottle { | 95 class CallbackRunningResourceThrottle : public ResourceThrottle { |
96 public: | 96 public: |
97 CallbackRunningResourceThrottle(net::URLRequest* request, | 97 CallbackRunningResourceThrottle(net::URLRequest* request, |
98 TestResourceDispatcherHostDelegate* tracker, | 98 TestResourceDispatcherHostDelegate* tracker, |
99 const RequestDeferredHook& run_on_start) | 99 const RequestDeferredHook& run_on_start) |
100 : request_(request), | 100 : resumed_(false), |
| 101 request_(request), |
101 tracker_(tracker), | 102 tracker_(tracker), |
102 run_on_start_(run_on_start), | 103 run_on_start_(run_on_start), |
103 weak_factory_(this) {} | 104 weak_factory_(this) {} |
104 | 105 |
105 void WillStartRequest(bool* defer) override { | 106 void WillStartRequest(bool* defer) override { |
106 *defer = true; | 107 *defer = true; |
107 base::Closure resume_request_on_io_thread = base::Bind( | 108 base::Closure resume_request_on_io_thread = base::Bind( |
108 base::IgnoreResult(&BrowserThread::PostTask), BrowserThread::IO, | 109 base::IgnoreResult(&BrowserThread::PostTask), BrowserThread::IO, |
109 FROM_HERE, base::Bind(&CallbackRunningResourceThrottle::Resume, | 110 FROM_HERE, base::Bind(&CallbackRunningResourceThrottle::Resume, |
110 weak_factory_.GetWeakPtr())); | 111 weak_factory_.GetWeakPtr())); |
111 BrowserThread::PostTask( | 112 BrowserThread::PostTask( |
112 BrowserThread::UI, FROM_HERE, | 113 BrowserThread::UI, FROM_HERE, |
113 base::Bind(run_on_start_, resume_request_on_io_thread)); | 114 base::Bind(run_on_start_, resume_request_on_io_thread)); |
114 } | 115 } |
115 | 116 |
116 ~CallbackRunningResourceThrottle() override { | 117 ~CallbackRunningResourceThrottle() override { |
117 // If the request is deleted without being cancelled, its status will | 118 // If the request is deleted without being cancelled, its status will |
118 // indicate it succeeded, so have to check if the request is still pending | 119 // indicate it succeeded, so have to check if the request is still pending |
119 // as well. | 120 // as well. If the request never even started, the throttle will never |
| 121 // resume it. Check this condition as well to allow for early |
| 122 // cancellation. |
120 tracker_->OnTrackedRequestDestroyed(!request_->is_pending() && | 123 tracker_->OnTrackedRequestDestroyed(!request_->is_pending() && |
121 request_->status().is_success()); | 124 request_->status().is_success() && |
| 125 resumed_); |
122 } | 126 } |
123 | 127 |
124 // ResourceThrottle implementation: | 128 // ResourceThrottle implementation: |
125 const char* GetNameForLogging() const override { | 129 const char* GetNameForLogging() const override { |
126 return "CallbackRunningResourceThrottle"; | 130 return "CallbackRunningResourceThrottle"; |
127 } | 131 } |
128 | 132 |
129 private: | 133 private: |
130 void Resume() { controller()->Resume(); } | 134 void Resume() { |
| 135 resumed_ = true; |
| 136 controller()->Resume(); |
| 137 } |
| 138 |
| 139 bool resumed_; |
131 net::URLRequest* request_; | 140 net::URLRequest* request_; |
132 TestResourceDispatcherHostDelegate* tracker_; | 141 TestResourceDispatcherHostDelegate* tracker_; |
133 RequestDeferredHook run_on_start_; | 142 RequestDeferredHook run_on_start_; |
134 base::WeakPtrFactory<CallbackRunningResourceThrottle> weak_factory_; | 143 base::WeakPtrFactory<CallbackRunningResourceThrottle> weak_factory_; |
135 | 144 |
136 DISALLOW_COPY_AND_ASSIGN(CallbackRunningResourceThrottle); | 145 DISALLOW_COPY_AND_ASSIGN(CallbackRunningResourceThrottle); |
137 }; | 146 }; |
138 | 147 |
139 void SetTrackedURLOnIOThread(const GURL& tracked_url, | 148 void SetTrackedURLOnIOThread(const GURL& tracked_url, |
140 const RequestDeferredHook& run_on_start, | 149 const RequestDeferredHook& run_on_start, |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 base::StringPrintf("document.getElementById('child-0').src='%s'", | 278 base::StringPrintf("document.getElementById('child-0').src='%s'", |
270 target_resource.spec().c_str()))); | 279 target_resource.spec().c_str()))); |
271 | 280 |
272 // Wait for the scenario to play out. If this returns false, it means the | 281 // Wait for the scenario to play out. If this returns false, it means the |
273 // request did not succeed, which is good in this case. | 282 // request did not succeed, which is good in this case. |
274 EXPECT_FALSE(tracking_delegate().WaitForTrackedURLAndGetCompleted()) | 283 EXPECT_FALSE(tracking_delegate().WaitForTrackedURLAndGetCompleted()) |
275 << "Request should have been cancelled before reaching the renderer."; | 284 << "Request should have been cancelled before reaching the renderer."; |
276 } | 285 } |
277 | 286 |
278 } // namespace content | 287 } // namespace content |
OLD | NEW |