Improve use of NeverDestroyed
[WebKit-https.git] / Source / WebCore / platform / network / NetworkStateNotifier.h
index c3a4290..f5e79b4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008, 2014 Apple Inc. All Rights Reserved.
+ * Copyright (C) 2008-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  */
 
-#ifndef NetworkStateNotifier_h
-#define NetworkStateNotifier_h
-
-#include <wtf/FastMalloc.h>
-#include <wtf/Function.h>
-#include <wtf/Noncopyable.h>
-#include <wtf/Vector.h>
-
-#if PLATFORM(MAC)
+#pragma once
 
+#include <wtf/Forward.h>
 #include <wtf/RetainPtr.h>
 #include "Timer.h"
 
-typedef const struct __CFArray * CFArrayRef;
-typedef const struct __SCDynamicStore * SCDynamicStoreRef;
+#if PLATFORM(IOS)
+OBJC_CLASS WebNetworkStateObserver;
+#endif
 
-#elif PLATFORM(WIN)
+#if PLATFORM(MAC)
+typedef const struct __SCDynamicStore * SCDynamicStoreRef;
+#endif
 
+#if PLATFORM(WIN)
 #include <windows.h>
-
-#elif PLATFORM(IOS)
-
-#include <wtf/RetainPtr.h>
-OBJC_CLASS WebNetworkStateObserver;
-
 #endif
 
 namespace WebCore {
 
 class NetworkStateNotifier {
-    WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); WTF_MAKE_FAST_ALLOCATED;
+    WTF_MAKE_NONCOPYABLE(NetworkStateNotifier);
+
 public:
-    NetworkStateNotifier();
-#if PLATFORM(IOS)
-    ~NetworkStateNotifier();
-#endif
-    void addNetworkStateChangeListener(WTF::Function<void (bool isOnLine)>&&);
+    static NetworkStateNotifier& singleton();
 
-    bool onLine() const;
+    bool onLine();
+    void addListener(WTF::Function<void(bool isOnLine)>&&);
 
 private:
-#if !PLATFORM(IOS)
-    bool m_isOnLine;
-#endif
-    Vector<WTF::Function<void (bool)>> m_listeners;
-
-    void notifyNetworkStateChange() const;
-    void updateState();
+    friend NeverDestroyed<NetworkStateNotifier>;
 
-#if PLATFORM(MAC)
-    void networkStateChangeTimerFired();
-
-    static void dynamicStoreCallback(SCDynamicStoreRef, CFArrayRef changedKeys, void *info); 
-
-    RetainPtr<SCDynamicStoreRef> m_store;
-    Timer m_networkStateChangeTimer;
+    NetworkStateNotifier();
 
-#elif PLATFORM(WIN)
-    static void CALLBACK addrChangeCallback(void*, BOOLEAN timedOut);
-    void addressChanged();
+    void updateStateWithoutNotifying();
+    void updateState();
+    void updateStateSoon();
+    void startObserving();
 
+#if PLATFORM(WIN)
     void registerForAddressChange();
-    HANDLE m_waitHandle;
-    OVERLAPPED m_overlapped;
-
-#elif PLATFORM(IOS)
-    void registerObserverIfNecessary() const;
-    friend void setOnLine(const NetworkStateNotifier*, bool);
-
-    mutable bool m_isOnLine;
-    mutable bool m_isOnLineInitialized;
-    mutable RetainPtr<WebNetworkStateObserver> m_observer;
+    static void CALLBACK addressChangeCallback(void*, BOOLEAN timedOut);
 #endif
-};
 
-#if !PLATFORM(COCOA) && !PLATFORM(WIN)
-
-inline NetworkStateNotifier::NetworkStateNotifier()
-    : m_isOnLine(true)
-{
-}
-
-inline void NetworkStateNotifier::updateState() { }
+    std::optional<bool> m_isOnLine;
+    Vector<WTF::Function<void(bool)>> m_listeners;
+    Timer m_updateStateTimer;
 
+#if PLATFORM(IOS)
+    RetainPtr<WebNetworkStateObserver> m_observer;
 #endif
 
-#if !PLATFORM(IOS)
-inline bool NetworkStateNotifier::onLine() const
-{
-    return m_isOnLine;
-}
+#if PLATFORM(MAC)
+    RetainPtr<SCDynamicStoreRef> m_store;
 #endif
 
-WEBCORE_EXPORT NetworkStateNotifier& networkStateNotifier();
+#if PLATFORM(WIN)
+    HANDLE m_waitHandle;
+    OVERLAPPED m_overlapped;
+#endif
+};
 
 } // namespace WebCore
-
-#endif // NetworkStateNotifier_h