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 "content/browser/loader/resource_scheduler.h" | 5 #include "content/browser/loader/resource_scheduler.h" |
6 | 6 |
7 #include "base/memory/scoped_vector.h" | 7 #include "base/memory/scoped_vector.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/test/mock_entropy_provider.h" | 12 #include "base/test/mock_entropy_provider.h" |
13 #include "base/timer/mock_timer.h" | 13 #include "base/timer/mock_timer.h" |
14 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
15 #include "content/browser/browser_thread_impl.h" | 15 #include "content/browser/browser_thread_impl.h" |
16 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 16 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
17 #include "content/browser/loader/resource_message_filter.h" | |
18 #include "content/browser/loader/resource_request_info_impl.h" | |
19 #include "content/common/resource_messages.h" | |
20 #include "content/public/browser/resource_context.h" | 17 #include "content/public/browser/resource_context.h" |
21 #include "content/public/browser/resource_controller.h" | 18 #include "content/public/browser/resource_controller.h" |
22 #include "content/public/browser/resource_throttle.h" | 19 #include "content/public/browser/resource_throttle.h" |
23 #include "content/public/common/content_switches.h" | 20 #include "content/public/common/content_switches.h" |
24 #include "content/public/common/process_type.h" | |
25 #include "content/public/common/resource_type.h" | |
26 #include "content/public/test/mock_render_process_host.h" | 21 #include "content/public/test/mock_render_process_host.h" |
27 #include "content/public/test/test_browser_context.h" | 22 #include "content/public/test/test_browser_context.h" |
28 #include "content/test/test_render_view_host_factory.h" | 23 #include "content/test/test_render_view_host_factory.h" |
29 #include "content/test/test_web_contents.h" | 24 #include "content/test/test_web_contents.h" |
30 #include "net/base/host_port_pair.h" | 25 #include "net/base/host_port_pair.h" |
31 #include "net/base/request_priority.h" | 26 #include "net/base/request_priority.h" |
32 #include "net/http/http_server_properties_impl.h" | 27 #include "net/http/http_server_properties_impl.h" |
33 #include "net/url_request/url_request.h" | 28 #include "net/url_request/url_request.h" |
34 #include "net/url_request/url_request_test_util.h" | 29 #include "net/url_request/url_request_test_util.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 11 matching lines...) Expand all Loading... |
47 const int kRouteId = 75; | 42 const int kRouteId = 75; |
48 const int kChildId2 = 43; | 43 const int kChildId2 = 43; |
49 const int kRouteId2 = 67; | 44 const int kRouteId2 = 67; |
50 const int kBackgroundChildId = 35; | 45 const int kBackgroundChildId = 35; |
51 const int kBackgroundRouteId = 43; | 46 const int kBackgroundRouteId = 43; |
52 const int kBackgroundChildId2 = 54; | 47 const int kBackgroundChildId2 = 54; |
53 const int kBackgroundRouteId2 = 82; | 48 const int kBackgroundRouteId2 = 82; |
54 | 49 |
55 class TestRequest : public ResourceController { | 50 class TestRequest : public ResourceController { |
56 public: | 51 public: |
57 TestRequest(scoped_ptr<ResourceThrottle> throttle, | 52 TestRequest(scoped_ptr<net::URLRequest> url_request, |
58 scoped_ptr<net::URLRequest> url_request) | 53 scoped_ptr<ResourceThrottle> throttle, |
| 54 ResourceScheduler* scheduler) |
59 : started_(false), | 55 : started_(false), |
| 56 url_request_(url_request.Pass()), |
60 throttle_(throttle.Pass()), | 57 throttle_(throttle.Pass()), |
61 url_request_(url_request.Pass()) { | 58 scheduler_(scheduler) { |
62 throttle_->set_controller_for_testing(this); | 59 throttle_->set_controller_for_testing(this); |
63 } | 60 } |
64 ~TestRequest() override {} | 61 ~TestRequest() override { |
| 62 // The URLRequest must still be valid when the ScheduledResourceRequest is |
| 63 // destroyed, so that it can unregister itself. |
| 64 throttle_.reset(); |
| 65 } |
65 | 66 |
66 bool started() const { return started_; } | 67 bool started() const { return started_; } |
67 | 68 |
68 void Start() { | 69 void Start() { |
69 bool deferred = false; | 70 bool deferred = false; |
70 throttle_->WillStartRequest(&deferred); | 71 throttle_->WillStartRequest(&deferred); |
71 started_ = !deferred; | 72 started_ = !deferred; |
72 } | 73 } |
73 | 74 |
| 75 void ChangePriority(net::RequestPriority new_priority, int intra_priority) { |
| 76 scheduler_->ReprioritizeRequest(url_request_.get(), new_priority, |
| 77 intra_priority); |
| 78 } |
| 79 |
74 void Cancel() override { | 80 void Cancel() override { |
75 // Alert the scheduler that the request can be deleted. | 81 // Alert the scheduler that the request can be deleted. |
76 throttle_.reset(0); | 82 throttle_.reset(); |
77 } | 83 } |
78 | 84 |
79 const net::URLRequest* url_request() const { return url_request_.get(); } | 85 const net::URLRequest* url_request() const { return url_request_.get(); } |
80 | 86 |
81 protected: | 87 protected: |
82 // ResourceController interface: | 88 // ResourceController interface: |
83 void CancelAndIgnore() override {} | 89 void CancelAndIgnore() override {} |
84 void CancelWithError(int error_code) override {} | 90 void CancelWithError(int error_code) override {} |
85 void Resume() override { started_ = true; } | 91 void Resume() override { started_ = true; } |
86 | 92 |
87 private: | 93 private: |
88 bool started_; | 94 bool started_; |
| 95 scoped_ptr<net::URLRequest> url_request_; |
89 scoped_ptr<ResourceThrottle> throttle_; | 96 scoped_ptr<ResourceThrottle> throttle_; |
90 scoped_ptr<net::URLRequest> url_request_; | 97 ResourceScheduler* scheduler_; |
91 }; | 98 }; |
92 | 99 |
93 class CancelingTestRequest : public TestRequest { | 100 class CancelingTestRequest : public TestRequest { |
94 public: | 101 public: |
95 CancelingTestRequest(scoped_ptr<ResourceThrottle> throttle, | 102 CancelingTestRequest(scoped_ptr<net::URLRequest> url_request, |
96 scoped_ptr<net::URLRequest> url_request) | 103 scoped_ptr<ResourceThrottle> throttle, |
97 : TestRequest(throttle.Pass(), url_request.Pass()) {} | 104 ResourceScheduler* scheduler) |
| 105 : TestRequest(url_request.Pass(), throttle.Pass(), scheduler) {} |
98 | 106 |
99 void set_request_to_cancel(scoped_ptr<TestRequest> request_to_cancel) { | 107 void set_request_to_cancel(scoped_ptr<TestRequest> request_to_cancel) { |
100 request_to_cancel_ = request_to_cancel.Pass(); | 108 request_to_cancel_ = request_to_cancel.Pass(); |
101 } | 109 } |
102 | 110 |
103 private: | 111 private: |
104 void Resume() override { | 112 void Resume() override { |
105 TestRequest::Resume(); | 113 TestRequest::Resume(); |
106 request_to_cancel_.reset(); | 114 request_to_cancel_.reset(); |
107 } | 115 } |
108 | 116 |
109 scoped_ptr<TestRequest> request_to_cancel_; | 117 scoped_ptr<TestRequest> request_to_cancel_; |
110 }; | 118 }; |
111 | 119 |
112 class FakeResourceContext : public ResourceContext { | 120 class FakeResourceContext : public ResourceContext { |
113 private: | 121 private: |
114 net::HostResolver* GetHostResolver() override { return NULL; } | 122 net::HostResolver* GetHostResolver() override { return NULL; } |
115 net::URLRequestContext* GetRequestContext() override { return NULL; } | 123 net::URLRequestContext* GetRequestContext() override { return NULL; } |
116 }; | 124 }; |
117 | 125 |
118 class FakeResourceMessageFilter : public ResourceMessageFilter { | |
119 public: | |
120 FakeResourceMessageFilter(int child_id) | |
121 : ResourceMessageFilter( | |
122 child_id, | |
123 PROCESS_TYPE_RENDERER, | |
124 NULL /* appcache_service */, | |
125 NULL /* blob_storage_context */, | |
126 NULL /* file_system_context */, | |
127 NULL /* service_worker_context */, | |
128 NULL /* host_zoom_level_context */, | |
129 base::Bind(&FakeResourceMessageFilter::GetContexts, | |
130 base::Unretained(this))) { | |
131 } | |
132 | |
133 private: | |
134 ~FakeResourceMessageFilter() override {} | |
135 | |
136 void GetContexts(const ResourceHostMsg_Request& request, | |
137 ResourceContext** resource_context, | |
138 net::URLRequestContext** request_context) { | |
139 *resource_context = &context_; | |
140 *request_context = NULL; | |
141 } | |
142 | |
143 FakeResourceContext context_; | |
144 }; | |
145 | |
146 class ResourceSchedulerTest : public testing::Test { | 126 class ResourceSchedulerTest : public testing::Test { |
147 protected: | 127 protected: |
148 ResourceSchedulerTest() | 128 ResourceSchedulerTest() |
149 : next_request_id_(0), | 129 : ui_thread_(BrowserThread::UI, &message_loop_), |
150 ui_thread_(BrowserThread::UI, &message_loop_), | |
151 io_thread_(BrowserThread::IO, &message_loop_), | 130 io_thread_(BrowserThread::IO, &message_loop_), |
152 field_trial_list_(new base::MockEntropyProvider()) { | 131 field_trial_list_(new base::MockEntropyProvider()) { |
153 InitializeScheduler(); | 132 InitializeScheduler(); |
154 context_.set_http_server_properties(http_server_properties_.GetWeakPtr()); | 133 context_.set_http_server_properties(http_server_properties_.GetWeakPtr()); |
155 } | 134 } |
156 | 135 |
157 ~ResourceSchedulerTest() override { | 136 ~ResourceSchedulerTest() override { |
158 CleanupScheduler(); | 137 CleanupScheduler(); |
159 } | 138 } |
160 | 139 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 return base::FieldTrialList::CreateTrialsFromString( | 171 return base::FieldTrialList::CreateTrialsFromString( |
193 force_field_trial_argument, | 172 force_field_trial_argument, |
194 base::FieldTrialList::DONT_ACTIVATE_TRIALS, | 173 base::FieldTrialList::DONT_ACTIVATE_TRIALS, |
195 std::set<std::string>()); | 174 std::set<std::string>()); |
196 } | 175 } |
197 | 176 |
198 scoped_ptr<net::URLRequest> NewURLRequestWithChildAndRoute( | 177 scoped_ptr<net::URLRequest> NewURLRequestWithChildAndRoute( |
199 const char* url, | 178 const char* url, |
200 net::RequestPriority priority, | 179 net::RequestPriority priority, |
201 int child_id, | 180 int child_id, |
202 int route_id, | 181 int route_id) { |
203 bool is_async) { | |
204 scoped_ptr<net::URLRequest> url_request( | 182 scoped_ptr<net::URLRequest> url_request( |
205 context_.CreateRequest(GURL(url), priority, NULL)); | 183 context_.CreateRequest(GURL(url), priority, NULL)); |
206 ResourceRequestInfoImpl* info = new ResourceRequestInfoImpl( | |
207 PROCESS_TYPE_RENDERER, // process_type | |
208 child_id, // child_id | |
209 route_id, // route_id | |
210 -1, // frame_tree_node_id | |
211 0, // origin_pid | |
212 ++next_request_id_, // request_id | |
213 MSG_ROUTING_NONE, // render_frame_id | |
214 false, // is_main_frame | |
215 false, // parent_is_main_frame | |
216 0, // parent_render_frame_id | |
217 RESOURCE_TYPE_SUB_RESOURCE, // resource_type | |
218 ui::PAGE_TRANSITION_LINK, // transition_type | |
219 false, // should_replace_current_entry | |
220 false, // is_download | |
221 false, // is_stream | |
222 true, // allow_download | |
223 false, // has_user_gesture | |
224 false, // enable_load_timing | |
225 false, // enable_upload_progress | |
226 false, // do_not_prompt_for_login | |
227 blink::WebReferrerPolicyDefault, // referrer_policy | |
228 blink::WebPageVisibilityStateVisible, // visibility_state | |
229 NULL, // context | |
230 base::WeakPtr<ResourceMessageFilter>(), // filter | |
231 is_async); // is_async | |
232 info->AssociateWithRequest(url_request.get()); | |
233 return url_request.Pass(); | 184 return url_request.Pass(); |
234 } | 185 } |
235 | 186 |
236 scoped_ptr<net::URLRequest> NewURLRequest(const char* url, | 187 scoped_ptr<net::URLRequest> NewURLRequest(const char* url, |
237 net::RequestPriority priority) { | 188 net::RequestPriority priority) { |
238 return NewURLRequestWithChildAndRoute( | 189 return NewURLRequestWithChildAndRoute(url, priority, kChildId, kRouteId); |
239 url, priority, kChildId, kRouteId, true); | |
240 } | 190 } |
241 | 191 |
242 TestRequest* NewRequestWithRoute(const char* url, | 192 TestRequest* NewRequestWithRoute(const char* url, |
243 net::RequestPriority priority, | 193 net::RequestPriority priority, |
244 int route_id) { | 194 int route_id) { |
245 return NewRequestWithChildAndRoute(url, priority, route_id, kChildId); | 195 return NewRequestWithChildAndRoute(url, priority, route_id, kChildId); |
246 } | 196 } |
247 | 197 |
248 TestRequest* NewRequestWithChildAndRoute(const char* url, | 198 TestRequest* NewRequestWithChildAndRoute(const char* url, |
249 net::RequestPriority priority, | 199 net::RequestPriority priority, |
(...skipping 27 matching lines...) Expand all Loading... |
277 int child_id, | 227 int child_id, |
278 int route_id) { | 228 int route_id) { |
279 return GetNewTestRequest(url, priority, child_id, route_id, false); | 229 return GetNewTestRequest(url, priority, child_id, route_id, false); |
280 } | 230 } |
281 | 231 |
282 TestRequest* GetNewTestRequest(const char* url, | 232 TestRequest* GetNewTestRequest(const char* url, |
283 net::RequestPriority priority, | 233 net::RequestPriority priority, |
284 int child_id, | 234 int child_id, |
285 int route_id, | 235 int route_id, |
286 bool is_async) { | 236 bool is_async) { |
287 scoped_ptr<net::URLRequest> url_request(NewURLRequestWithChildAndRoute( | 237 scoped_ptr<net::URLRequest> url_request( |
288 url, priority, child_id, route_id, is_async)); | 238 NewURLRequestWithChildAndRoute(url, priority, child_id, route_id)); |
289 scoped_ptr<ResourceThrottle> throttle( | 239 scoped_ptr<ResourceThrottle> throttle(scheduler_->ScheduleRequest( |
290 scheduler_->ScheduleRequest(child_id, route_id, url_request.get())); | 240 child_id, route_id, is_async, url_request.get())); |
291 TestRequest* request = new TestRequest(throttle.Pass(), url_request.Pass()); | 241 TestRequest* request = |
| 242 new TestRequest(url_request.Pass(), throttle.Pass(), scheduler()); |
292 request->Start(); | 243 request->Start(); |
293 return request; | 244 return request; |
294 } | 245 } |
295 | 246 |
296 void ChangeRequestPriority(TestRequest* request, | 247 void ChangeRequestPriority(TestRequest* request, |
297 net::RequestPriority new_priority, | 248 net::RequestPriority new_priority, |
298 int intra_priority = 0) { | 249 int intra_priority = 0) { |
299 scoped_refptr<FakeResourceMessageFilter> filter( | 250 request->ChangePriority(new_priority, intra_priority); |
300 new FakeResourceMessageFilter(kChildId)); | |
301 const ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest( | |
302 request->url_request()); | |
303 const GlobalRequestID& id = info->GetGlobalRequestID(); | |
304 ResourceHostMsg_DidChangePriority msg(id.request_id, new_priority, | |
305 intra_priority); | |
306 rdh_.OnMessageReceived(msg, filter.get()); | |
307 } | 251 } |
308 | 252 |
309 void FireCoalescingTimer() { | 253 void FireCoalescingTimer() { |
310 EXPECT_TRUE(mock_timer_->IsRunning()); | 254 EXPECT_TRUE(mock_timer_->IsRunning()); |
311 mock_timer_->Fire(); | 255 mock_timer_->Fire(); |
312 } | 256 } |
313 | 257 |
314 ResourceScheduler* scheduler() { | 258 ResourceScheduler* scheduler() { |
315 return scheduler_.get(); | 259 return scheduler_.get(); |
316 } | 260 } |
317 | 261 |
318 int next_request_id_; | |
319 base::MessageLoopForIO message_loop_; | 262 base::MessageLoopForIO message_loop_; |
320 BrowserThreadImpl ui_thread_; | 263 BrowserThreadImpl ui_thread_; |
321 BrowserThreadImpl io_thread_; | 264 BrowserThreadImpl io_thread_; |
322 ResourceDispatcherHostImpl rdh_; | 265 ResourceDispatcherHostImpl rdh_; |
323 scoped_ptr<ResourceScheduler> scheduler_; | 266 scoped_ptr<ResourceScheduler> scheduler_; |
324 base::FieldTrialList field_trial_list_; | 267 base::FieldTrialList field_trial_list_; |
325 base::MockTimer* mock_timer_; | 268 base::MockTimer* mock_timer_; |
326 net::HttpServerPropertiesImpl http_server_properties_; | 269 net::HttpServerPropertiesImpl http_server_properties_; |
327 net::TestURLRequestContext context_; | 270 net::TestURLRequestContext context_; |
328 }; | 271 }; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 high2.reset(); | 372 high2.reset(); |
430 EXPECT_TRUE(low2->started()); | 373 EXPECT_TRUE(low2->started()); |
431 } | 374 } |
432 | 375 |
433 TEST_F(ResourceSchedulerTest, CancelOtherRequestsWhileResuming) { | 376 TEST_F(ResourceSchedulerTest, CancelOtherRequestsWhileResuming) { |
434 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST)); | 377 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST)); |
435 scoped_ptr<TestRequest> low1(NewRequest("http://host/low1", net::LOWEST)); | 378 scoped_ptr<TestRequest> low1(NewRequest("http://host/low1", net::LOWEST)); |
436 | 379 |
437 scoped_ptr<net::URLRequest> url_request( | 380 scoped_ptr<net::URLRequest> url_request( |
438 NewURLRequest("http://host/low2", net::LOWEST)); | 381 NewURLRequest("http://host/low2", net::LOWEST)); |
439 scoped_ptr<ResourceThrottle> throttle( | 382 scoped_ptr<ResourceThrottle> throttle(scheduler()->ScheduleRequest( |
440 scheduler()->ScheduleRequest(kChildId, kRouteId, url_request.get())); | 383 kChildId, kRouteId, true, url_request.get())); |
441 scoped_ptr<CancelingTestRequest> low2(new CancelingTestRequest( | 384 scoped_ptr<CancelingTestRequest> low2(new CancelingTestRequest( |
442 throttle.Pass(), url_request.Pass())); | 385 url_request.Pass(), throttle.Pass(), scheduler())); |
443 low2->Start(); | 386 low2->Start(); |
444 | 387 |
445 scoped_ptr<TestRequest> low3(NewRequest("http://host/low3", net::LOWEST)); | 388 scoped_ptr<TestRequest> low3(NewRequest("http://host/low3", net::LOWEST)); |
446 low2->set_request_to_cancel(low3.Pass()); | 389 low2->set_request_to_cancel(low3.Pass()); |
447 scoped_ptr<TestRequest> low4(NewRequest("http://host/low4", net::LOWEST)); | 390 scoped_ptr<TestRequest> low4(NewRequest("http://host/low4", net::LOWEST)); |
448 | 391 |
449 EXPECT_TRUE(high->started()); | 392 EXPECT_TRUE(high->started()); |
450 EXPECT_FALSE(low2->started()); | 393 EXPECT_FALSE(low2->started()); |
451 high.reset(); | 394 high.reset(); |
452 EXPECT_TRUE(low1->started()); | 395 EXPECT_TRUE(low1->started()); |
(...skipping 1903 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2356 EXPECT_FALSE(lowest->started()); | 2299 EXPECT_FALSE(lowest->started()); |
2357 scheduler_->OnClientDeleted(kChildId2, kRouteId2); | 2300 scheduler_->OnClientDeleted(kChildId2, kRouteId2); |
2358 high.reset(); | 2301 high.reset(); |
2359 delayable_requests.clear(); | 2302 delayable_requests.clear(); |
2360 EXPECT_TRUE(lowest->started()); | 2303 EXPECT_TRUE(lowest->started()); |
2361 } | 2304 } |
2362 | 2305 |
2363 } // unnamed namespace | 2306 } // unnamed namespace |
2364 | 2307 |
2365 } // namespace content | 2308 } // namespace content |
OLD | NEW |