Chromium Code Reviews| 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_(!web_contents->IsHidden()), |
|
mmenke
2014/08/15 18:57:09
It's not safe to muck with the web_contents here.
| |
| 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); | |
| 243 } | 245 } |
| 244 | 246 |
| 245 ~Client() { | 247 virtual ~Client() { |
| 246 // Update to default state and pause to ensure the scheduler has a | 248 // Update to default state and pause to ensure the scheduler has a |
| 247 // correct count of relevant types of clients. | 249 // correct count of relevant types of clients. |
| 248 is_visible_ = false; | 250 is_visible_ = false; |
| 249 is_audible_ = false; | 251 is_audible_ = false; |
| 250 is_paused_ = true; | 252 is_paused_ = true; |
| 251 UpdateThrottleState(); | 253 UpdateThrottleState(); |
| 252 } | 254 } |
| 253 | 255 |
| 256 // WebContentsObserver implementation. | |
| 257 virtual void WasShown() OVERRIDE { | |
| 258 OnVisibilityChanged(true); | |
| 259 } | |
| 260 | |
| 261 // WebContentsObserver implementation. | |
| 262 virtual void WasHidden() OVERRIDE { | |
| 263 OnVisibilityChanged(false); | |
| 264 } | |
| 265 | |
| 254 void ScheduleRequest( | 266 void ScheduleRequest( |
| 255 net::URLRequest* url_request, | 267 net::URLRequest* url_request, |
| 256 ScheduledResourceRequest* request) { | 268 ScheduledResourceRequest* request) { |
| 257 if (ShouldStartRequest(request) == START_REQUEST) { | 269 if (ShouldStartRequest(request) == START_REQUEST) { |
| 258 StartRequest(request); | 270 StartRequest(request); |
| 259 } else { | 271 } else { |
| 260 pending_requests_.Insert(request); | 272 pending_requests_.Insert(request); |
| 261 } | 273 } |
| 262 } | 274 } |
| 263 | 275 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 281 (*it)->set_accounted_as_delayable_request(false); | 293 (*it)->set_accounted_as_delayable_request(false); |
| 282 } | 294 } |
| 283 ClearInFlightRequests(); | 295 ClearInFlightRequests(); |
| 284 return unowned_requests; | 296 return unowned_requests; |
| 285 } | 297 } |
| 286 | 298 |
| 287 bool is_active() const { return is_visible_ || is_audible_; } | 299 bool is_active() const { return is_visible_ || is_audible_; } |
| 288 | 300 |
| 289 bool is_loaded() const { return is_loaded_; } | 301 bool is_loaded() const { return is_loaded_; } |
| 290 | 302 |
| 303 bool IsVisible() const { return is_visible_; } | |
| 304 | |
| 291 void OnAudibilityChanged(bool is_audible) { | 305 void OnAudibilityChanged(bool is_audible) { |
| 292 if (is_audible == is_audible_) { | 306 if (is_audible == is_audible_) { |
| 293 return; | 307 return; |
| 294 } | 308 } |
| 295 is_audible_ = is_audible; | 309 is_audible_ = is_audible; |
| 296 UpdateThrottleState(); | 310 UpdateThrottleState(); |
| 297 } | 311 } |
| 298 | 312 |
| 299 void OnVisibilityChanged(bool is_visible) { | 313 void OnVisibilityChanged(bool is_visible) { |
| 300 if (is_visible == is_visible_) { | 314 if (is_visible == is_visible_) { |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 724 | 738 |
| 725 ClientMap::iterator client_it = client_map_.find(request->client_id()); | 739 ClientMap::iterator client_it = client_map_.find(request->client_id()); |
| 726 if (client_it == client_map_.end()) { | 740 if (client_it == client_map_.end()) { |
| 727 return; | 741 return; |
| 728 } | 742 } |
| 729 | 743 |
| 730 Client* client = client_it->second; | 744 Client* client = client_it->second; |
| 731 client->RemoveRequest(request); | 745 client->RemoveRequest(request); |
| 732 } | 746 } |
| 733 | 747 |
| 734 void ResourceScheduler::OnClientCreated(int child_id, int route_id) { | 748 void ResourceScheduler::OnClientCreated(int child_id, |
| 749 int route_id, | |
| 750 WebContentsImpl* web_contents) { | |
| 735 DCHECK(CalledOnValidThread()); | 751 DCHECK(CalledOnValidThread()); |
| 736 ClientId client_id = MakeClientId(child_id, route_id); | 752 ClientId client_id = MakeClientId(child_id, route_id); |
| 737 DCHECK(!ContainsKey(client_map_, client_id)); | 753 DCHECK(!ContainsKey(client_map_, client_id)); |
| 738 | 754 |
| 739 Client* client = new Client(this); | 755 Client* client = new Client(this, web_contents); |
| 740 client_map_[client_id] = client; | 756 client_map_[client_id] = client; |
| 741 | 757 |
| 742 // TODO(aiolos): set Client visibility/audibility when signals are added | 758 // TODO(aiolos): set Client visibility/audibility when signals are added |
| 743 // this will UNTHROTTLE Clients as needed | 759 // this will UNTHROTTLE Clients as needed |
| 744 client->UpdateThrottleState(); | 760 client->UpdateThrottleState(); |
| 745 } | 761 } |
| 746 | 762 |
| 747 void ResourceScheduler::OnClientDeleted(int child_id, int route_id) { | 763 void ResourceScheduler::OnClientDeleted(int child_id, int route_id) { |
| 748 DCHECK(CalledOnValidThread()); | 764 DCHECK(CalledOnValidThread()); |
| 749 ClientId client_id = MakeClientId(child_id, route_id); | 765 ClientId client_id = MakeClientId(child_id, route_id); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 810 } | 826 } |
| 811 | 827 |
| 812 void ResourceScheduler::OnAudibilityChanged(int child_id, | 828 void ResourceScheduler::OnAudibilityChanged(int child_id, |
| 813 int route_id, | 829 int route_id, |
| 814 bool is_audible) { | 830 bool is_audible) { |
| 815 Client* client = GetClient(child_id, route_id); | 831 Client* client = GetClient(child_id, route_id); |
| 816 DCHECK(client); | 832 DCHECK(client); |
| 817 client->OnAudibilityChanged(is_audible); | 833 client->OnAudibilityChanged(is_audible); |
| 818 } | 834 } |
| 819 | 835 |
| 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, | 836 void ResourceScheduler::OnLoadingStateChanged(int child_id, |
| 829 int route_id, | 837 int route_id, |
| 830 bool is_loaded) { | 838 bool is_loaded) { |
| 831 Client* client = GetClient(child_id, route_id); | 839 Client* client = GetClient(child_id, route_id); |
| 832 DCHECK(client); | 840 DCHECK(client); |
| 833 client->OnLoadingStateChanged(is_loaded); | 841 client->OnLoadingStateChanged(is_loaded); |
| 834 } | 842 } |
| 835 | 843 |
| 844 bool ResourceScheduler::IsClientVisibleForTesting(int child_id, int route_id) { | |
| 845 Client* client = GetClient(child_id, route_id); | |
| 846 DCHECK(client); | |
| 847 return client->IsVisible(); | |
| 848 } | |
| 849 | |
| 836 ResourceScheduler::Client* ResourceScheduler::GetClient(int child_id, | 850 ResourceScheduler::Client* ResourceScheduler::GetClient(int child_id, |
| 837 int route_id) { | 851 int route_id) { |
| 838 ClientId client_id = MakeClientId(child_id, route_id); | 852 ClientId client_id = MakeClientId(child_id, route_id); |
| 839 ClientMap::iterator client_it = client_map_.find(client_id); | 853 ClientMap::iterator client_it = client_map_.find(client_id); |
| 840 if (client_it == client_map_.end()) { | 854 if (client_it == client_map_.end()) { |
| 841 return NULL; | 855 return NULL; |
| 842 } | 856 } |
| 843 return client_it->second; | 857 return client_it->second; |
| 844 } | 858 } |
| 845 | 859 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 960 client->ReprioritizeRequest( | 974 client->ReprioritizeRequest( |
| 961 request, old_priority_params, new_priority_params); | 975 request, old_priority_params, new_priority_params); |
| 962 } | 976 } |
| 963 | 977 |
| 964 ResourceScheduler::ClientId ResourceScheduler::MakeClientId( | 978 ResourceScheduler::ClientId ResourceScheduler::MakeClientId( |
| 965 int child_id, int route_id) { | 979 int child_id, int route_id) { |
| 966 return (static_cast<ResourceScheduler::ClientId>(child_id) << 32) | route_id; | 980 return (static_cast<ResourceScheduler::ClientId>(child_id) << 32) | route_id; |
| 967 } | 981 } |
| 968 | 982 |
| 969 } // namespace content | 983 } // namespace content |
| OLD | NEW |