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/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/strings/string_number_conversions.h" |
8 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
9 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 11 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 12 #include "content/browser/loader/resource_message_filter.h" |
10 #include "content/browser/loader/resource_request_info_impl.h" | 13 #include "content/browser/loader/resource_request_info_impl.h" |
| 14 #include "content/common/resource_messages.h" |
| 15 #include "content/public/browser/resource_context.h" |
11 #include "content/public/browser/resource_controller.h" | 16 #include "content/public/browser/resource_controller.h" |
12 #include "content/public/browser/resource_throttle.h" | 17 #include "content/public/browser/resource_throttle.h" |
13 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
14 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webkit/glue/resource_type.h" | 21 #include "webkit/glue/resource_type.h" |
17 | 22 |
18 namespace content { | 23 namespace content { |
19 | 24 |
20 namespace { | 25 namespace { |
(...skipping 14 matching lines...) Expand all Loading... |
35 } | 40 } |
36 | 41 |
37 bool started() const { return started_; } | 42 bool started() const { return started_; } |
38 | 43 |
39 void Start() { | 44 void Start() { |
40 bool deferred = false; | 45 bool deferred = false; |
41 throttle_->WillStartRequest(&deferred); | 46 throttle_->WillStartRequest(&deferred); |
42 started_ = !deferred; | 47 started_ = !deferred; |
43 } | 48 } |
44 | 49 |
| 50 const net::URLRequest* url_request() const { return url_request_.get(); } |
| 51 |
45 protected: | 52 protected: |
46 // ResourceController interface: | 53 // ResourceController interface: |
47 virtual void Cancel() OVERRIDE {} | 54 virtual void Cancel() OVERRIDE {} |
48 virtual void CancelAndIgnore() OVERRIDE {} | 55 virtual void CancelAndIgnore() OVERRIDE {} |
49 virtual void CancelWithError(int error_code) OVERRIDE {} | 56 virtual void CancelWithError(int error_code) OVERRIDE {} |
50 virtual void Resume() OVERRIDE { started_ = true; } | 57 virtual void Resume() OVERRIDE { started_ = true; } |
51 | 58 |
52 private: | 59 private: |
53 bool started_; | 60 bool started_; |
54 scoped_ptr<ResourceThrottle> throttle_; | 61 scoped_ptr<ResourceThrottle> throttle_; |
(...skipping 13 matching lines...) Expand all Loading... |
68 | 75 |
69 private: | 76 private: |
70 virtual void Resume() OVERRIDE { | 77 virtual void Resume() OVERRIDE { |
71 TestRequest::Resume(); | 78 TestRequest::Resume(); |
72 request_to_cancel_.reset(); | 79 request_to_cancel_.reset(); |
73 } | 80 } |
74 | 81 |
75 scoped_ptr<TestRequest> request_to_cancel_; | 82 scoped_ptr<TestRequest> request_to_cancel_; |
76 }; | 83 }; |
77 | 84 |
| 85 class TestResourceContext : public ResourceContext { |
| 86 private: |
| 87 virtual net::HostResolver* GetHostResolver() OVERRIDE { return NULL; } |
| 88 virtual net::URLRequestContext* GetRequestContext() OVERRIDE { return NULL; } |
| 89 }; |
| 90 |
| 91 class TestURLRequestContextSelector |
| 92 : public ResourceMessageFilter::URLRequestContextSelector { |
| 93 private: |
| 94 virtual net::URLRequestContext* GetRequestContext( |
| 95 ResourceType::Type) OVERRIDE { |
| 96 return NULL; |
| 97 } |
| 98 }; |
| 99 |
| 100 class TestResourceMessageFilter : public ResourceMessageFilter { |
| 101 public: |
| 102 TestResourceMessageFilter(int child_id) |
| 103 : ResourceMessageFilter(child_id, |
| 104 PROCESS_TYPE_RENDERER, |
| 105 &context_, |
| 106 NULL /* appcache_service */, |
| 107 NULL /* blob_storage_context */, |
| 108 NULL /* file_system_context */, |
| 109 new TestURLRequestContextSelector) { |
| 110 } |
| 111 |
| 112 private: |
| 113 virtual ~TestResourceMessageFilter() {} |
| 114 |
| 115 TestResourceContext context_; |
| 116 }; |
| 117 |
78 class ResourceSchedulerTest : public testing::Test { | 118 class ResourceSchedulerTest : public testing::Test { |
79 protected: | 119 protected: |
80 ResourceSchedulerTest() | 120 ResourceSchedulerTest() |
81 : message_loop_(MessageLoop::TYPE_IO), | 121 : next_request_id_(0), |
82 ui_thread_(BrowserThread::UI, &message_loop_) { | 122 message_loop_(MessageLoop::TYPE_IO), |
| 123 ui_thread_(BrowserThread::UI, &message_loop_), |
| 124 io_thread_(BrowserThread::IO, &message_loop_) { |
83 scheduler_.OnClientCreated(kChildId, kRouteId); | 125 scheduler_.OnClientCreated(kChildId, kRouteId); |
84 } | 126 } |
85 | 127 |
86 virtual ~ResourceSchedulerTest() { | 128 virtual ~ResourceSchedulerTest() { |
87 scheduler_.OnClientDeleted(kChildId, kRouteId); | 129 scheduler_.OnClientDeleted(kChildId, kRouteId); |
88 } | 130 } |
89 | 131 |
90 scoped_ptr<net::URLRequest> NewURLRequest(const char* url, | 132 scoped_ptr<net::URLRequest> NewURLRequest(const char* url, |
91 net::RequestPriority priority, | 133 net::RequestPriority priority, |
92 int route_id = kRouteId) { | 134 int route_id = kRouteId) { |
93 scoped_ptr<net::URLRequest> url_request( | 135 scoped_ptr<net::URLRequest> url_request( |
94 context_.CreateRequest(GURL(url), NULL)); | 136 context_.CreateRequest(GURL(url), NULL)); |
95 url_request->set_priority(priority); | 137 url_request->set_priority(priority); |
96 ResourceRequestInfo::AllocateForTesting( | 138 ResourceRequestInfoImpl* info = new ResourceRequestInfoImpl( |
97 url_request.get(), ResourceType::SUB_RESOURCE, NULL, kChildId, | 139 PROCESS_TYPE_RENDERER, // process_type |
98 route_id); | 140 kChildId, // child_id |
| 141 route_id, // route_id |
| 142 0, // origin_pid |
| 143 ++next_request_id_, // request_id |
| 144 false, // is_main_frame |
| 145 0, // frame_id |
| 146 false, // parent_is_main_frame |
| 147 0, // parent_frame_id |
| 148 ResourceType::SUB_RESOURCE, // resource_type |
| 149 PAGE_TRANSITION_LINK, // transition_type |
| 150 false, // is_download |
| 151 true, // allow_download |
| 152 false, // has_user_gesture |
| 153 WebKit::WebReferrerPolicyDefault, // referrer_policy |
| 154 NULL, // context |
| 155 false); // is_async |
| 156 info->AssociateWithRequest(url_request.get()); |
99 return url_request.Pass(); | 157 return url_request.Pass(); |
100 } | 158 } |
101 | 159 |
102 TestRequest* NewRequest(const char* url, net::RequestPriority priority, | 160 TestRequest* NewRequest(const char* url, net::RequestPriority priority, |
103 int route_id = kRouteId) { | 161 int route_id = kRouteId) { |
104 scoped_ptr<net::URLRequest> url_request( | 162 scoped_ptr<net::URLRequest> url_request( |
105 NewURLRequest(url, priority, route_id)); | 163 NewURLRequest(url, priority, route_id)); |
106 scoped_ptr<ResourceThrottle> throttle(scheduler_.ScheduleRequest( | 164 scoped_ptr<ResourceThrottle> throttle(scheduler_.ScheduleRequest( |
107 kChildId, route_id, url_request.get())); | 165 kChildId, route_id, url_request.get())); |
108 TestRequest* request = new TestRequest(throttle.Pass(), url_request.Pass()); | 166 TestRequest* request = new TestRequest(throttle.Pass(), url_request.Pass()); |
109 request->Start(); | 167 request->Start(); |
110 return request; | 168 return request; |
111 } | 169 } |
112 | 170 |
| 171 void ChangeRequestPriority(TestRequest* request, |
| 172 net::RequestPriority new_priority) { |
| 173 scoped_refptr<TestResourceMessageFilter> filter( |
| 174 new TestResourceMessageFilter(kChildId)); |
| 175 const ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest( |
| 176 request->url_request()); |
| 177 const GlobalRequestID& id = info->GetGlobalRequestID(); |
| 178 ResourceHostMsg_DidChangePriority msg( |
| 179 kRouteId, id.request_id, new_priority); |
| 180 bool ok = false; |
| 181 rdh_.OnMessageReceived(msg, filter.get(), &ok); |
| 182 EXPECT_TRUE(ok); |
| 183 } |
| 184 |
| 185 int next_request_id_; |
113 MessageLoop message_loop_; | 186 MessageLoop message_loop_; |
114 BrowserThreadImpl ui_thread_; | 187 BrowserThreadImpl ui_thread_; |
| 188 BrowserThreadImpl io_thread_; |
115 ResourceDispatcherHostImpl rdh_; | 189 ResourceDispatcherHostImpl rdh_; |
116 ResourceScheduler scheduler_; | 190 ResourceScheduler scheduler_; |
117 net::TestURLRequestContext context_; | 191 net::TestURLRequestContext context_; |
118 }; | 192 }; |
119 | 193 |
120 TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) { | 194 TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) { |
121 scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOWEST)); | 195 scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOWEST)); |
122 EXPECT_TRUE(request->started()); | 196 EXPECT_TRUE(request->started()); |
123 } | 197 } |
124 | 198 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 scoped_ptr<TestRequest> low4(NewRequest("http://host/low4", net::LOWEST)); | 260 scoped_ptr<TestRequest> low4(NewRequest("http://host/low4", net::LOWEST)); |
187 | 261 |
188 EXPECT_TRUE(high->started()); | 262 EXPECT_TRUE(high->started()); |
189 EXPECT_FALSE(low2->started()); | 263 EXPECT_FALSE(low2->started()); |
190 high.reset(); | 264 high.reset(); |
191 EXPECT_TRUE(low1->started()); | 265 EXPECT_TRUE(low1->started()); |
192 EXPECT_TRUE(low2->started()); | 266 EXPECT_TRUE(low2->started()); |
193 EXPECT_TRUE(low4->started()); | 267 EXPECT_TRUE(low4->started()); |
194 } | 268 } |
195 | 269 |
| 270 TEST_F(ResourceSchedulerTest, LimitedNumberOfLowPriorityRequestsInFlight) { |
| 271 // We only load low priority resources if there's a body. |
| 272 scheduler_.OnWillInsertBody(kChildId, kRouteId); |
| 273 |
| 274 // Throw in one high priority request to make sure that's not a factor. |
| 275 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST)); |
| 276 EXPECT_TRUE(high->started()); |
| 277 |
| 278 const int kMaxNumLowPriorityRequestsPerClient = 10; // Should match the .cc. |
| 279 ScopedVector<TestRequest> lows; |
| 280 for (int i = 0; i < kMaxNumLowPriorityRequestsPerClient; ++i) { |
| 281 string url = "http://host/low" + base::IntToString(i); |
| 282 lows.push_back(NewRequest(url.c_str(), net::LOWEST)); |
| 283 EXPECT_TRUE(lows[i]->started()); |
| 284 } |
| 285 |
| 286 scoped_ptr<TestRequest> last(NewRequest("http://host/last", net::LOWEST)); |
| 287 EXPECT_FALSE(last->started()); |
| 288 high.reset(); |
| 289 EXPECT_FALSE(last->started()); |
| 290 lows.erase(lows.begin()); |
| 291 EXPECT_TRUE(last->started()); |
| 292 } |
| 293 |
| 294 TEST_F(ResourceSchedulerTest, RaisePriorityAndStart) { |
| 295 // Dummy to enforce scheduling. |
| 296 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST)); |
| 297 |
| 298 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST)); |
| 299 EXPECT_FALSE(request->started()); |
| 300 |
| 301 ChangeRequestPriority(request.get(), net::HIGHEST); |
| 302 EXPECT_TRUE(request->started()); |
| 303 } |
| 304 |
| 305 TEST_F(ResourceSchedulerTest, RaisePriorityInQueue) { |
| 306 // Dummy to enforce scheduling. |
| 307 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST)); |
| 308 |
| 309 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE)); |
| 310 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE)); |
| 311 EXPECT_FALSE(request->started()); |
| 312 EXPECT_FALSE(idle->started()); |
| 313 |
| 314 ChangeRequestPriority(request.get(), net::LOWEST); |
| 315 EXPECT_FALSE(request->started()); |
| 316 EXPECT_FALSE(idle->started()); |
| 317 |
| 318 const int kMaxNumLowPriorityRequestsPerClient = 10; // Should match the .cc. |
| 319 ScopedVector<TestRequest> lows; |
| 320 for (int i = 0; i < kMaxNumLowPriorityRequestsPerClient - 1; ++i) { |
| 321 string url = "http://host/low" + base::IntToString(i); |
| 322 lows.push_back(NewRequest(url.c_str(), net::LOWEST)); |
| 323 } |
| 324 |
| 325 scheduler_.OnWillInsertBody(kChildId, kRouteId); |
| 326 EXPECT_TRUE(request->started()); |
| 327 EXPECT_FALSE(idle->started()); |
| 328 } |
| 329 |
| 330 TEST_F(ResourceSchedulerTest, LowerPriority) { |
| 331 // Dummy to enforce scheduling. |
| 332 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST)); |
| 333 |
| 334 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST)); |
| 335 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE)); |
| 336 EXPECT_FALSE(request->started()); |
| 337 EXPECT_FALSE(idle->started()); |
| 338 |
| 339 ChangeRequestPriority(request.get(), net::IDLE); |
| 340 EXPECT_FALSE(request->started()); |
| 341 EXPECT_FALSE(idle->started()); |
| 342 |
| 343 const int kMaxNumLowPriorityRequestsPerClient = 10; // Should match the .cc. |
| 344 ScopedVector<TestRequest> lows; |
| 345 for (int i = 0; i < kMaxNumLowPriorityRequestsPerClient - 1; ++i) { |
| 346 string url = "http://host/low" + base::IntToString(i); |
| 347 lows.push_back(NewRequest(url.c_str(), net::LOWEST)); |
| 348 } |
| 349 |
| 350 scheduler_.OnWillInsertBody(kChildId, kRouteId); |
| 351 EXPECT_FALSE(request->started()); |
| 352 EXPECT_TRUE(idle->started()); |
| 353 } |
| 354 |
196 } // unnamed namespace | 355 } // unnamed namespace |
197 | 356 |
198 } // namespace content | 357 } // namespace content |
OLD | NEW |