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 <set> | 5 #include <set> |
6 | 6 |
7 #include "content/browser/loader/resource_scheduler.h" | 7 #include "content/browser/loader/resource_scheduler.h" |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "content/common/resource_messages.h" | 10 #include "content/common/resource_messages.h" |
11 #include "content/browser/loader/resource_message_delegate.h" | 11 #include "content/browser/loader/resource_message_delegate.h" |
12 #include "content/public/browser/resource_controller.h" | 12 #include "content/public/browser/resource_controller.h" |
13 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
14 #include "content/public/browser/resource_throttle.h" | 14 #include "content/public/browser/resource_throttle.h" |
15 #include "content/public/browser/web_contents_observer.h" | |
15 #include "ipc/ipc_message_macros.h" | 16 #include "ipc/ipc_message_macros.h" |
16 #include "net/base/host_port_pair.h" | 17 #include "net/base/host_port_pair.h" |
17 #include "net/base/load_flags.h" | 18 #include "net/base/load_flags.h" |
18 #include "net/base/request_priority.h" | 19 #include "net/base/request_priority.h" |
19 #include "net/http/http_server_properties.h" | 20 #include "net/http/http_server_properties.h" |
20 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
21 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
22 | 23 |
23 namespace content { | 24 namespace content { |
24 | 25 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 } | 222 } |
222 | 223 |
223 void ResourceScheduler::RequestQueue::Insert( | 224 void ResourceScheduler::RequestQueue::Insert( |
224 ScheduledResourceRequest* request) { | 225 ScheduledResourceRequest* request) { |
225 DCHECK(!ContainsKey(pointers_, request)); | 226 DCHECK(!ContainsKey(pointers_, request)); |
226 request->set_fifo_ordering(MakeFifoOrderingId()); | 227 request->set_fifo_ordering(MakeFifoOrderingId()); |
227 pointers_[request] = queue_.insert(request); | 228 pointers_[request] = queue_.insert(request); |
228 } | 229 } |
229 | 230 |
230 // Each client represents a tab. | 231 // Each client represents a tab. |
231 class ResourceScheduler::Client { | 232 class ResourceScheduler::Client : public content::WebContentsObserver { |
232 public: | 233 public: |
233 explicit Client(ResourceScheduler* scheduler) | 234 explicit Client(ResourceScheduler* scheduler, WebContentsImpl* web_contents) |
234 : is_audible_(false), | 235 : is_audible_(false), |
235 is_visible_(false), | 236 is_visible_(false), |
aiolos (Not reviewing)
2014/08/15 01:45:38
is_visible_ should just be set once here.
Zhen Wang
2014/08/15 18:43:01
Done.
| |
236 is_loaded_(false), | 237 is_loaded_(false), |
237 is_paused_(false), | 238 is_paused_(false), |
238 has_body_(false), | 239 has_body_(false), |
239 using_spdy_proxy_(false), | 240 using_spdy_proxy_(false), |
240 total_delayable_count_(0), | 241 total_delayable_count_(0), |
241 throttle_state_(ResourceScheduler::THROTTLED) { | 242 throttle_state_(ResourceScheduler::THROTTLED) { |
242 scheduler_ = scheduler; | 243 scheduler_ = scheduler; |
244 Observe(web_contents); | |
245 is_visible_ = !web_contents->IsHidden(); | |
243 } | 246 } |
244 | 247 |
245 ~Client() { | 248 virtual ~Client() { |
246 // Update to default state and pause to ensure the scheduler has a | 249 // Update to default state and pause to ensure the scheduler has a |
247 // correct count of relevant types of clients. | 250 // correct count of relevant types of clients. |
248 is_visible_ = false; | 251 is_visible_ = false; |
249 is_audible_ = false; | 252 is_audible_ = false; |
250 is_paused_ = true; | 253 is_paused_ = true; |
251 UpdateThrottleState(); | 254 UpdateThrottleState(); |
252 } | 255 } |
253 | 256 |
257 // WebContentsObserver implementation. | |
258 virtual void WasShown() OVERRIDE { | |
259 OnVisibilityChanged(true); | |
260 } | |
261 | |
262 // WebContentsObserver implementation. | |
263 virtual void WasHidden() OVERRIDE { | |
264 OnVisibilityChanged(false); | |
265 } | |
266 | |
254 void ScheduleRequest( | 267 void ScheduleRequest( |
255 net::URLRequest* url_request, | 268 net::URLRequest* url_request, |
256 ScheduledResourceRequest* request) { | 269 ScheduledResourceRequest* request) { |
257 if (ShouldStartRequest(request) == START_REQUEST) { | 270 if (ShouldStartRequest(request) == START_REQUEST) { |
258 StartRequest(request); | 271 StartRequest(request); |
259 } else { | 272 } else { |
260 pending_requests_.Insert(request); | 273 pending_requests_.Insert(request); |
261 } | 274 } |
262 } | 275 } |
263 | 276 |
(...skipping 17 matching lines...) Expand all Loading... | |
281 (*it)->set_accounted_as_delayable_request(false); | 294 (*it)->set_accounted_as_delayable_request(false); |
282 } | 295 } |
283 ClearInFlightRequests(); | 296 ClearInFlightRequests(); |
284 return unowned_requests; | 297 return unowned_requests; |
285 } | 298 } |
286 | 299 |
287 bool is_active() const { return is_visible_ || is_audible_; } | 300 bool is_active() const { return is_visible_ || is_audible_; } |
288 | 301 |
289 bool is_loaded() const { return is_loaded_; } | 302 bool is_loaded() const { return is_loaded_; } |
290 | 303 |
304 bool IsVisible() const { return is_visible_; } | |
305 | |
291 void OnAudibilityChanged(bool is_audible) { | 306 void OnAudibilityChanged(bool is_audible) { |
292 if (is_audible == is_audible_) { | 307 if (is_audible == is_audible_) { |
293 return; | 308 return; |
294 } | 309 } |
295 is_audible_ = is_audible; | 310 is_audible_ = is_audible; |
296 UpdateThrottleState(); | 311 UpdateThrottleState(); |
297 } | 312 } |
298 | 313 |
299 void OnVisibilityChanged(bool is_visible) { | 314 void OnVisibilityChanged(bool is_visible) { |
300 if (is_visible == is_visible_) { | 315 if (is_visible == is_visible_) { |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
724 | 739 |
725 ClientMap::iterator client_it = client_map_.find(request->client_id()); | 740 ClientMap::iterator client_it = client_map_.find(request->client_id()); |
726 if (client_it == client_map_.end()) { | 741 if (client_it == client_map_.end()) { |
727 return; | 742 return; |
728 } | 743 } |
729 | 744 |
730 Client* client = client_it->second; | 745 Client* client = client_it->second; |
731 client->RemoveRequest(request); | 746 client->RemoveRequest(request); |
732 } | 747 } |
733 | 748 |
734 void ResourceScheduler::OnClientCreated(int child_id, int route_id) { | 749 void ResourceScheduler::OnClientCreated(int child_id, |
750 int route_id, | |
751 WebContentsImpl* web_contents) { | |
735 DCHECK(CalledOnValidThread()); | 752 DCHECK(CalledOnValidThread()); |
736 ClientId client_id = MakeClientId(child_id, route_id); | 753 ClientId client_id = MakeClientId(child_id, route_id); |
737 DCHECK(!ContainsKey(client_map_, client_id)); | 754 DCHECK(!ContainsKey(client_map_, client_id)); |
738 | 755 |
739 Client* client = new Client(this); | 756 Client* client = new Client(this, web_contents); |
740 client_map_[client_id] = client; | 757 client_map_[client_id] = client; |
741 | 758 |
742 // TODO(aiolos): set Client visibility/audibility when signals are added | 759 // TODO(aiolos): set Client visibility/audibility when signals are added |
743 // this will UNTHROTTLE Clients as needed | 760 // this will UNTHROTTLE Clients as needed |
744 client->UpdateThrottleState(); | 761 client->UpdateThrottleState(); |
745 } | 762 } |
746 | 763 |
747 void ResourceScheduler::OnClientDeleted(int child_id, int route_id) { | 764 void ResourceScheduler::OnClientDeleted(int child_id, int route_id) { |
748 DCHECK(CalledOnValidThread()); | 765 DCHECK(CalledOnValidThread()); |
749 ClientId client_id = MakeClientId(child_id, route_id); | 766 ClientId client_id = MakeClientId(child_id, route_id); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
810 } | 827 } |
811 | 828 |
812 void ResourceScheduler::OnAudibilityChanged(int child_id, | 829 void ResourceScheduler::OnAudibilityChanged(int child_id, |
813 int route_id, | 830 int route_id, |
814 bool is_audible) { | 831 bool is_audible) { |
815 Client* client = GetClient(child_id, route_id); | 832 Client* client = GetClient(child_id, route_id); |
816 DCHECK(client); | 833 DCHECK(client); |
817 client->OnAudibilityChanged(is_audible); | 834 client->OnAudibilityChanged(is_audible); |
818 } | 835 } |
819 | 836 |
820 void ResourceScheduler::OnVisibilityChanged(int child_id, | |
821 int route_id, | |
822 bool is_visible) { | |
823 Client* client = GetClient(child_id, route_id); | |
824 DCHECK(client); | |
825 client->OnVisibilityChanged(is_visible); | |
826 } | |
827 | |
828 void ResourceScheduler::OnLoadingStateChanged(int child_id, | 837 void ResourceScheduler::OnLoadingStateChanged(int child_id, |
829 int route_id, | 838 int route_id, |
830 bool is_loaded) { | 839 bool is_loaded) { |
831 Client* client = GetClient(child_id, route_id); | 840 Client* client = GetClient(child_id, route_id); |
832 DCHECK(client); | 841 DCHECK(client); |
833 client->OnLoadingStateChanged(is_loaded); | 842 client->OnLoadingStateChanged(is_loaded); |
834 } | 843 } |
835 | 844 |
845 bool ResourceScheduler::IsClientVisible(int child_id, int route_id) { | |
846 Client* client = GetClient(child_id, route_id); | |
847 DCHECK(client); | |
848 return client->IsVisible(); | |
849 } | |
850 | |
836 ResourceScheduler::Client* ResourceScheduler::GetClient(int child_id, | 851 ResourceScheduler::Client* ResourceScheduler::GetClient(int child_id, |
837 int route_id) { | 852 int route_id) { |
838 ClientId client_id = MakeClientId(child_id, route_id); | 853 ClientId client_id = MakeClientId(child_id, route_id); |
839 ClientMap::iterator client_it = client_map_.find(client_id); | 854 ClientMap::iterator client_it = client_map_.find(client_id); |
840 if (client_it == client_map_.end()) { | 855 if (client_it == client_map_.end()) { |
841 return NULL; | 856 return NULL; |
842 } | 857 } |
843 return client_it->second; | 858 return client_it->second; |
844 } | 859 } |
845 | 860 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
960 client->ReprioritizeRequest( | 975 client->ReprioritizeRequest( |
961 request, old_priority_params, new_priority_params); | 976 request, old_priority_params, new_priority_params); |
962 } | 977 } |
963 | 978 |
964 ResourceScheduler::ClientId ResourceScheduler::MakeClientId( | 979 ResourceScheduler::ClientId ResourceScheduler::MakeClientId( |
965 int child_id, int route_id) { | 980 int child_id, int route_id) { |
966 return (static_cast<ResourceScheduler::ClientId>(child_id) << 32) | route_id; | 981 return (static_cast<ResourceScheduler::ClientId>(child_id) << 32) | route_id; |
967 } | 982 } |
968 | 983 |
969 } // namespace content | 984 } // namespace content |
OLD | NEW |