Add strong typing to RefCounter interface, return value as a bool.
https://bugs.webkit.org/show_bug.cgi?id=139776

Reviewed by Geoff Garen.

Currently all token vended by a RefCounter have the same type - Ref<RefCounter::Count>.
This means there is no compile time type checking to prevent mistakes. Update the count()
method to token<>(), templated on type used to identify the token being returned.
Calls to token<T>() will return a result of type RefCounter::Token<T>.

There are a few problems with the fact the counter will return you an exact count of the
number of outstanding tokens:
  - It is desirable to only fire the callback on zero-edge changes; it is more consistent
    to do so if the value is only readable as a boolean.
  - It is desirable to provide the value as an argument to the callback, however to make
    this useful for integer values it is also necessary to indicate the direction of change
    (0->1 is often interesting where 2->1 is not).
  - There is a mismatch between the precision of returning a count, and the inherent
    imprecision of a token based mechanism, where it may be difficult to guarantee
    absolutely no unnecessary refcount churn, and thus unintentional counter values.

Source/WebCore:

* page/PageThrottler.cpp:
(WebCore::m_mediaActivityCounter):
(WebCore::m_pageLoadActivityCounter):
    - lambdas now passed the value.
(WebCore::PageThrottler::mediaActivityToken):
(WebCore::PageThrottler::pageLoadActivityToken):
    - count() -> token<>().
* page/PageThrottler.h:
    - specify tpoken type for PageActivityAssertionToken.

Source/WebKit2:

Removed PluginProcessManager::m_processSuppressionEnabled. Now the callback only fires on
zero-edge transitions we no longer need this to filter changes.

* UIProcess/Plugins/PluginProcessManager.cpp:
(WebKit::PluginProcessManager::PluginProcessManager):
    - updateProcessSuppressionState -> updateProcessSuppressionDisabled.
* UIProcess/Plugins/PluginProcessManager.h:
(WebKit::PluginProcessManager::processSuppressionDisabledForPageCount): Deleted.
(WebKit::PluginProcessManager::processSuppressionDisabledToken):
    - processSuppressionDisabledForPageCount -> processSuppressionDisabledToken.
(WebKit::PluginProcessManager::processSuppressionEnabled): Deleted.
(WebKit::PluginProcessManager::processSuppressionDisabled):
    - processSuppressionEnabled -> processSuppressionDisabled.
* UIProcess/Plugins/PluginProcessProxy.cpp:
(WebKit::PluginProcessProxy::didFinishLaunching):
    - processSuppressionEnabled -> processSuppressionDisabled.
* UIProcess/Plugins/mac/PluginProcessManagerMac.mm:
(WebKit::PluginProcessManager::updateProcessSuppressionState): Deleted.
(WebKit::PluginProcessManager::updateProcessSuppressionDisabled):
    - updateProcessSuppressionState -> updateProcessSuppressionDisabled
* UIProcess/ProcessThrottler.h:
    - added UserObservablePageToken, ProcessSuppressionDisabledToken types.
* UIProcess/WebContext.cpp:
(WebKit::WebContext::WebContext):
(WebKit::m_processSuppressionDisabledForPageCounter):
    - lambda now has bool argument.
* UIProcess/WebContext.h:
(WebKit::WebContext::userObservablePageCount):
(WebKit::WebContext::processSuppressionDisabledForPageCount):
    - count() -> token<>(), changed return type.
* UIProcess/WebPageProxy.h:
    - changed types of token members.
* UIProcess/mac/WebContextMac.mm:
(WebKit::WebContext::updateProcessSuppressionState):
    renamed m_pluginProcessManagerProcessSuppressionDisabledCount -> m_pluginProcessManagerProcessSuppressionDisabledToken.

Source/WTF:

* wtf/RefCounter.cpp:
(WTF::RefCounter::Count::ref):
(WTF::RefCounter::Count::deref):
    - only call the callback on zero-edge changes; provide the value.
(WTF::RefCounter::RefCounter):
    - callback now takes a bool argument.
* wtf/RefCounter.h:
(WTF::RefCounter::Token::Token):
    - New opaque type to reference the RefCounter::Count.
(WTF::RefCounter::Token::operator!):
    - ! operator checks for null / anasigned Tokens.
(WTF::RefCounter::RefCounter):
    - callback now takes a bool argument.
(WTF::RefCounter::token):
    - renamed from count(), templated on type of token returned.
(WTF::RefCounter::value):
    - now returns a bool.
(WTF::RefCounter::Token<T>::Token):
(WTF::=):
    - Tokens can be copied & assigned.
(WTF::RefCounter::count): Deleted.
    - renamed to token<>().

Tools:

* TestWebKitAPI/Tests/WTF/RefCounter.cpp:
(TestWebKitAPI::TEST):
    - update API test.



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@177541 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WTF/wtf/RefCounter.h b/Source/WTF/wtf/RefCounter.h
index a10694c..446c47d 100644
--- a/Source/WTF/wtf/RefCounter.h
+++ b/Source/WTF/wtf/RefCounter.h
@@ -34,7 +34,7 @@
 
 class RefCounter {
     WTF_MAKE_NONCOPYABLE(RefCounter);
-public:
+
     class Count {
         WTF_MAKE_NONCOPYABLE(Count);
     public:
@@ -54,24 +54,86 @@
         unsigned m_value;
     };
 
-    WTF_EXPORT_PRIVATE RefCounter(std::function<void()> = []() { });
+public:
+    template<typename T>
+    class Token  {
+    public:
+        Token() { }
+        Token(std::nullptr_t) { }
+        inline Token(const Token<T>& token);
+        inline Token(Token<T>&& token);
+
+        inline Token<T>& operator=(std::nullptr_t);
+        inline Token<T>& operator=(const Token<T>& o);
+        inline Token<T>& operator=(Token<T>&& o);
+
+        bool operator!() const { return !m_ptr; }
+
+    private:
+        friend class RefCounter;
+        inline Token(Count* count);
+
+        RefPtr<Count> m_ptr;
+    };
+
+    WTF_EXPORT_PRIVATE RefCounter(std::function<void(bool)> = [](bool) { });
     WTF_EXPORT_PRIVATE ~RefCounter();
 
-    Ref<Count> count() const
+    template<typename T>
+    Token<T> token() const
     {
-        return *m_count;
+        return Token<T>(m_count);
     }
 
-    unsigned value() const
+    bool value() const
     {
         return m_count->m_value;
     }
 
 private:
-    std::function<void()> m_valueDidChange;
+    std::function<void(bool)> m_valueDidChange;
     Count* m_count;
 };
 
+template<class T>
+inline RefCounter::Token<T>::Token(Count* count)
+    : m_ptr(count)
+{
+}
+
+template<class T>
+inline RefCounter::Token<T>::Token(const RefCounter::Token<T>::Token<T>& token)
+    : m_ptr(token.m_ptr)
+{
+}
+
+template<class T>
+inline RefCounter::Token<T>::Token(RefCounter::Token<T>::Token<T>&& token)
+    : m_ptr(token.m_ptr)
+{
+}
+
+template<class T>
+inline RefCounter::Token<T>& RefCounter::Token<T>::operator=(std::nullptr_t)
+{
+    m_ptr = nullptr;
+    return *this;
+}
+
+template<class T>
+inline RefCounter::Token<T>& RefCounter::Token<T>::operator=(const RefCounter::Token<T>& o)
+{
+    m_ptr = o.m_ptr;
+    return *this;
+}
+
+template<class T>
+inline RefCounter::Token<T>& RefCounter::Token<T>::operator=(RefCounter::Token<T>&& o)
+{
+    m_ptr = o.m_ptr;
+    return *this;
+}
+
 } // namespace WTF
 
 using WTF::RefCounter;