Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1184)

Side by Side Diff: third_party/WebKit/Source/platform/loader/fetch/Resource.cpp

Issue 2876453002: Stop using Resource::ResourceCallback and make Resource have a TaskHandle (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/platform/loader/fetch/Resource.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) 2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) 3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) 4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) 5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
6 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All 6 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All
7 rights reserved. 7 rights reserved.
8 8
9 This library is free software; you can redistribute it and/or 9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public 10 modify it under the terms of the GNU Library General Public
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 WebSecurityOrigin(security_origin_), 239 WebSecurityOrigin(security_origin_),
240 GetResponse().CacheStorageCacheName()); 240 GetResponse().CacheStorageCacheName());
241 } else { 241 } else {
242 Platform::Current()->CacheMetadataInCacheStorage( 242 Platform::Current()->CacheMetadataInCacheStorage(
243 GetResponse().Url(), GetResponse().ResponseTime(), nullptr, 0, 243 GetResponse().Url(), GetResponse().ResponseTime(), nullptr, 0,
244 WebSecurityOrigin(security_origin_), 244 WebSecurityOrigin(security_origin_),
245 GetResponse().CacheStorageCacheName()); 245 GetResponse().CacheStorageCacheName());
246 } 246 }
247 } 247 }
248 248
249 // This class cannot be on-heap because the first callbackHandler() call
250 // instantiates the singleton object while we can call it in the
251 // pre-finalization step.
252 class Resource::ResourceCallback final {
253 public:
254 static ResourceCallback& CallbackHandler();
255 void Schedule(Resource*);
256 void Cancel(Resource*);
257 bool IsScheduled(Resource*) const;
258
259 private:
260 ResourceCallback();
261
262 void RunTask();
263 TaskHandle task_handle_;
264 HashSet<Persistent<Resource>> resources_with_pending_clients_;
265 };
266
267 Resource::ResourceCallback& Resource::ResourceCallback::CallbackHandler() {
268 DEFINE_STATIC_LOCAL(ResourceCallback, callback_handler, ());
269 return callback_handler;
270 }
271
272 Resource::ResourceCallback::ResourceCallback() {}
273
274 void Resource::ResourceCallback::Schedule(Resource* resource) {
275 if (!task_handle_.IsActive()) {
276 // WTF::unretained(this) is safe because a posted task is canceled when
277 // |task_handle_| is destroyed on the dtor of this ResourceCallback.
278 task_handle_ =
279 Platform::Current()
280 ->CurrentThread()
281 ->Scheduler()
282 ->LoadingTaskRunner()
283 ->PostCancellableTask(
284 BLINK_FROM_HERE,
285 WTF::Bind(&ResourceCallback::RunTask, WTF::Unretained(this)));
286 }
287 resources_with_pending_clients_.insert(resource);
288 }
289
290 void Resource::ResourceCallback::Cancel(Resource* resource) {
291 resources_with_pending_clients_.erase(resource);
292 if (task_handle_.IsActive() && resources_with_pending_clients_.IsEmpty())
293 task_handle_.Cancel();
294 }
295
296 bool Resource::ResourceCallback::IsScheduled(Resource* resource) const {
297 return resources_with_pending_clients_.Contains(resource);
298 }
299
300 void Resource::ResourceCallback::RunTask() {
301 HeapVector<Member<Resource>> resources;
302 for (const Member<Resource>& resource : resources_with_pending_clients_)
303 resources.push_back(resource.Get());
304 resources_with_pending_clients_.clear();
305
306 for (const auto& resource : resources)
307 resource->FinishPendingClients();
308 }
309
310 Resource::Resource(const ResourceRequest& request, 249 Resource::Resource(const ResourceRequest& request,
311 Type type, 250 Type type,
312 const ResourceLoaderOptions& options) 251 const ResourceLoaderOptions& options)
313 : load_finish_time_(0), 252 : load_finish_time_(0),
314 identifier_(0), 253 identifier_(0),
315 encoded_size_(0), 254 encoded_size_(0),
316 encoded_size_memory_usage_(0), 255 encoded_size_memory_usage_(0),
317 decoded_size_(0), 256 decoded_size_(0),
318 overhead_size_(CalculateOverheadSize()), 257 overhead_size_(CalculateOverheadSize()),
319 preload_count_(0), 258 preload_count_(0),
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 clients_.insert(client); 654 clients_.insert(client);
716 return; 655 return;
717 } 656 }
718 657
719 // If an error has occurred or we have existing data to send to the new client 658 // If an error has occurred or we have existing data to send to the new client
720 // and the resource type supprts it, send it asynchronously. 659 // and the resource type supprts it, send it asynchronously.
721 if ((ErrorOccurred() || !GetResponse().IsNull()) && 660 if ((ErrorOccurred() || !GetResponse().IsNull()) &&
722 !TypeNeedsSynchronousCacheHit(GetType()) && 661 !TypeNeedsSynchronousCacheHit(GetType()) &&
723 !needs_synchronous_cache_hit_) { 662 !needs_synchronous_cache_hit_) {
724 clients_awaiting_callback_.insert(client); 663 clients_awaiting_callback_.insert(client);
725 ResourceCallback::CallbackHandler().Schedule(this); 664 if (!async_callback_task_handle_.IsActive()) {
665 async_callback_task_handle_ =
666 Platform::Current()
667 ->CurrentThread()
668 ->Scheduler()
669 ->LoadingTaskRunner()
670 ->PostCancellableTask(BLINK_FROM_HERE,
671 WTF::Bind(&Resource::FinishPendingClients,
672 WrapWeakPersistent(this)));
673 }
726 return; 674 return;
727 } 675 }
728 676
729 clients_.insert(client); 677 clients_.insert(client);
730 DidAddClient(client); 678 DidAddClient(client);
731 return; 679 return;
732 } 680 }
733 681
734 void Resource::RemoveClient(ResourceClient* client) { 682 void Resource::RemoveClient(ResourceClient* client) {
735 CHECK(!is_add_remove_client_prohibited_); 683 CHECK(!is_add_remove_client_prohibited_);
736 684
737 // This code may be called in a pre-finalizer, where weak members in the 685 // This code may be called in a pre-finalizer, where weak members in the
738 // HashCountedSet are already swept out. 686 // HashCountedSet are already swept out.
739 687
740 if (finished_clients_.Contains(client)) 688 if (finished_clients_.Contains(client))
741 finished_clients_.erase(client); 689 finished_clients_.erase(client);
742 else if (clients_awaiting_callback_.Contains(client)) 690 else if (clients_awaiting_callback_.Contains(client))
743 clients_awaiting_callback_.erase(client); 691 clients_awaiting_callback_.erase(client);
744 else 692 else
745 clients_.erase(client); 693 clients_.erase(client);
746 694
747 if (clients_awaiting_callback_.IsEmpty()) 695 if (clients_awaiting_callback_.IsEmpty())
yhirano 2017/05/10 06:59:17 The original code contains |task_runner_.IsActive(
horo 2017/05/10 07:05:30 Done.
748 ResourceCallback::CallbackHandler().Cancel(this); 696 async_callback_task_handle_.Cancel();
749 697
750 DidRemoveClientOrObserver(); 698 DidRemoveClientOrObserver();
751 } 699 }
752 700
753 void Resource::DidRemoveClientOrObserver() { 701 void Resource::DidRemoveClientOrObserver() {
754 if (!HasClientsOrObservers() && is_alive_) { 702 if (!HasClientsOrObservers() && is_alive_) {
755 is_alive_ = false; 703 is_alive_ = false;
756 AllClientsAndObserversRemoved(); 704 AllClientsAndObserversRemoved();
757 705
758 // RFC2616 14.9.2: 706 // RFC2616 14.9.2:
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 768
821 // When revalidation starts after waiting clients are scheduled and 769 // When revalidation starts after waiting clients are scheduled and
822 // before they are added here. In such cases, we just add the clients 770 // before they are added here. In such cases, we just add the clients
823 // to |clients_| without DidAddClient(), as in Resource::AddClient(). 771 // to |clients_| without DidAddClient(), as in Resource::AddClient().
824 if (!is_revalidating_) 772 if (!is_revalidating_)
825 DidAddClient(client); 773 DidAddClient(client);
826 } 774 }
827 775
828 // It is still possible for the above loop to finish a new client 776 // It is still possible for the above loop to finish a new client
829 // synchronously. If there's no client waiting we should deschedule. 777 // synchronously. If there's no client waiting we should deschedule.
830 bool scheduled = ResourceCallback::CallbackHandler().IsScheduled(this); 778 bool scheduled = async_callback_task_handle_.IsActive();
831 if (scheduled && clients_awaiting_callback_.IsEmpty()) 779 if (scheduled && clients_awaiting_callback_.IsEmpty())
832 ResourceCallback::CallbackHandler().Cancel(this); 780 async_callback_task_handle_.Cancel();
833 781
834 // Prevent the case when there are clients waiting but no callback scheduled. 782 // Prevent the case when there are clients waiting but no callback scheduled.
835 DCHECK(clients_awaiting_callback_.IsEmpty() || scheduled); 783 DCHECK(clients_awaiting_callback_.IsEmpty() || scheduled);
836 } 784 }
837 785
838 void Resource::Prune() { 786 void Resource::Prune() {
839 DestroyDecodedDataIfPossible(); 787 DestroyDecodedDataIfPossible();
840 } 788 }
841 789
842 void Resource::OnPurgeMemory() { 790 void Resource::OnPurgeMemory() {
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 case Resource::kMedia: 1068 case Resource::kMedia:
1121 case Resource::kManifest: 1069 case Resource::kManifest:
1122 case Resource::kMock: 1070 case Resource::kMock:
1123 return false; 1071 return false;
1124 } 1072 }
1125 NOTREACHED(); 1073 NOTREACHED();
1126 return false; 1074 return false;
1127 } 1075 }
1128 1076
1129 } // namespace blink 1077 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/loader/fetch/Resource.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698