Generalize PageActivityAssertionToken
authorbarraclough@apple.com <barraclough@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 2 Dec 2014 20:30:17 +0000 (20:30 +0000)
committerbarraclough@apple.com <barraclough@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 2 Dec 2014 20:30:17 +0000 (20:30 +0000)
commit5cbd3b13fd52d7f0975f56bd85de0bdd61a695ef
tree5bb3cf9df64adfa8f864a567925a0c07ad9b28f1
parent9f2af17da3f2a2e452008ed90463c1f13a287b7e
Generalize PageActivityAssertionToken
https://bugs.webkit.org/show_bug.cgi?id=139106

Reviewed by Sam Weinig.

Source/WebCore:

PageActivityAssertionToken is a RAII mechanism implementing a counter, used by PageThrottler
to count user visible activity in progress on the page (currently page load and media playback).
Use of an RAII type is prevents a number of possible errors, including double counting a single
media element, or failing to decrement the count after a media element has been deallocated.

The current implementation has a number of drawbacks that have been addressed by this refactoring:
 - specific to single use in PageThrottler class - not reusable.
 - incomplete encapsulation - the counter and WeakPtrFactory that comprise the current implementation
   are not encapsulated (are in the client type, PageThrottler).
 - tokens are not shared - PageActivityAssertionToken instances are managed by std::unique, every
   increment requires an object allocation.
 - redundancy - the current implementation uses a WeakPtr to safely reference the PageThrottler, this
   is internally implemented using a reference counted type, resulting in two counters being
   incremented (one in the PageActivityAssertionToken, one in the PageThrottler).

In the reimplementation:
 - a callback is provided via a lambda function, which allows for easy reuse without a lot of
   boilerplate code.
 - the counter, callback and ownership of the otherwise weakly-owned token is encapsulated within the
   RefCounter type.
 - a single count within RefCounter::Count stores the counter value, and also manage the lifetime
   of this object.
 - standard RefPtrs are used to manage references to the RefCounter::Count.

* WebCore.xcodeproj/project.pbxproj:
    - removed PageActivityAssertionToken.cpp/.h
* html/HTMLMediaElement.cpp:
    - removed PageActivityAssertionToken.h
* html/HTMLMediaElement.h:
    - std::unique_ptr<PageActivityAssertionToken> -> RefPtr<RefCounter::Count>
* loader/FrameLoader.cpp:
    - removed PageActivityAssertionToken.h
* loader/FrameLoader.h:
    - std::unique_ptr<PageActivityAssertionToken> -> RefPtr<RefCounter::Count>
* loader/SubresourceLoader.cpp:
    - removed PageActivityAssertionToken.h
* loader/SubresourceLoader.h:
    - removed class PageActivityAssertionToken
* page/Page.cpp:
    - removed PageActivityAssertionToken.h
(WebCore::Page::Page):
    - removed Page* parameter to PageThrottler
* page/Page.h:
    - removed class PageActivityAssertionToken
* page/PageActivityAssertionToken.cpp: Removed.
* page/PageActivityAssertionToken.h: Removed.
    - removed PageActivityAssertionToken.cpp/.h
* page/PageThrottler.cpp:
(WebCore::PageThrottler::PageThrottler):
    - removed m_page, m_weakPtrFactory, m_activityCount; added m_pageActivityCounter.
(WebCore::PageThrottler::mediaActivityToken):
    - std::unique_ptr<PageActivityAssertionToken> -> PassRefPtr<RefCounter::Count>
(WebCore::PageThrottler::pageLoadActivityToken):
    - std::unique_ptr<PageActivityAssertionToken> -> PassRefPtr<RefCounter::Count>
(WebCore::PageThrottler::pageActivityCounterValueDidChange):
    - merged functionality of incrementActivityCount/decrementActivityCount
(WebCore::PageThrottler::incrementActivityCount): Deleted.
    - see pageActivityCounterValueDidChange
(WebCore::PageThrottler::decrementActivityCount): Deleted.
    - see pageActivityCounterValueDidChange
* page/PageThrottler.h:
(WebCore::PageThrottler::weakPtr): Deleted.
    - no longer required; this functionality is now encapsulated within RefCounter.

Source/WTF:

PageActivityAssertionToken is a RAII mechanism implementing a counter, used by PageThrottler
to count user visible activity in progress on the page (currently page load and media playback).
Use of an RAII type is prevents a number of possible errors, including double counting a single
media element, or failing to decrement the count after a media element has been deallocated.

The current implementation has a number of drawbacks that have been addressed by this refactoring:
 - specific to single use in PageThrottler class - not reusable.
 - incomplete encapsulation - the counter and WeakPtrFactory that comprise the current implementation
   are not encapsulated (are in the client type, PageThrottler).
 - tokens are not shared - PageActivityAssertionToken instances are managed by std::unique, every
   increment requires an object allocation.
 - redundancy - the current implementation uses a WeakPtr to safely reference the PageThrottler, this
   is internally implemented using a reference counted type, resulting in two counters being
   incremented (one in the PageActivityAssertionToken, one in the PageThrottler).

In the reimplementation:
 - a callback is provided via a lambda function, which allows for easy reuse without a lot of
   boilerplate code.
 - the counter, callback and ownership of the otherwise weakly-owned token is encapsulated within the
   RefCounter type.
 - a single count within RefCounter::Count stores the counter value, and also manage the lifetime
   of this object.
 - standard RefPtrs are used to manage references to the RefCounter::Count.

* WTF.xcodeproj/project.pbxproj:
    - added RefCounter.cpp/.h
* wtf/RefCounter.cpp: Added.
(WTF::RefCounter::Count::ref):
    - increment the counter.
(WTF::RefCounter::Count::deref):
    - decrement the counter, and delete as necessary.
(WTF::RefCounter::RefCounter):
    - create a RefCounter::Count.
(WTF::RefCounter::~RefCounter):
    - eagerly delete the Counter if it has no references, otherwise let it be deleted on last deref.
* wtf/RefCounter.h: Added.
(WTF::RefCounter::Count::Count):
    - initialize count to 0.
(WTF::RefCounter::RefCounter):
    - takes a lambda to be called when the value changes.
(WTF::RefCounter::count):
    - reference the counter (and in doing so increment the count).
(WTF::RefCounter::value):
    - access the current value of the counter.

Tools:

Add an API test for WTF::RefCounter.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/RefCounter.cpp: Added.
(TestWebKitAPI::TEST):
    - added RefCounter test.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@176683 268f45cc-cd09-0410-ab3c-d52691b4dbfc
23 files changed:
Source/WTF/ChangeLog
Source/WTF/WTF.vcxproj/WTF.vcxproj
Source/WTF/WTF.xcodeproj/project.pbxproj
Source/WTF/wtf/CMakeLists.txt
Source/WTF/wtf/RefCounter.cpp [moved from Source/WebCore/page/PageActivityAssertionToken.cpp with 52% similarity]
Source/WTF/wtf/RefCounter.h [moved from Source/WebCore/page/PageActivityAssertionToken.h with 58% similarity]
Source/WebCore/CMakeLists.txt
Source/WebCore/ChangeLog
Source/WebCore/WebCore.vcxproj/WebCore.vcxproj
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/html/HTMLMediaElement.cpp
Source/WebCore/html/HTMLMediaElement.h
Source/WebCore/loader/FrameLoader.cpp
Source/WebCore/loader/FrameLoader.h
Source/WebCore/loader/SubresourceLoader.cpp
Source/WebCore/loader/SubresourceLoader.h
Source/WebCore/page/Page.cpp
Source/WebCore/page/Page.h
Source/WebCore/page/PageThrottler.cpp
Source/WebCore/page/PageThrottler.h
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WTF/RefCounter.cpp [new file with mode: 0644]