| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #ifndef NetworkStateNotifier_h | 26 #ifndef NetworkStateNotifier_h |
| 27 #define NetworkStateNotifier_h | 27 #define NetworkStateNotifier_h |
| 28 | 28 |
| 29 #include <memory> |
| 29 #include "core/CoreExport.h" | 30 #include "core/CoreExport.h" |
| 30 #include "core/dom/ExecutionContext.h" | 31 #include "platform/WebTaskRunner.h" |
| 31 #include "public/platform/WebConnectionType.h" | 32 #include "public/platform/WebConnectionType.h" |
| 32 #include "wtf/Allocator.h" | 33 #include "wtf/Allocator.h" |
| 33 #include "wtf/HashMap.h" | 34 #include "wtf/HashMap.h" |
| 34 #include "wtf/Noncopyable.h" | 35 #include "wtf/Noncopyable.h" |
| 35 #include "wtf/ThreadingPrimitives.h" | 36 #include "wtf/ThreadingPrimitives.h" |
| 36 #include "wtf/Vector.h" | 37 #include "wtf/Vector.h" |
| 37 #include <memory> | |
| 38 | 38 |
| 39 namespace blink { | 39 namespace blink { |
| 40 | 40 |
| 41 class CORE_EXPORT NetworkStateNotifier { | 41 class CORE_EXPORT NetworkStateNotifier { |
| 42 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); | 42 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); |
| 43 USING_FAST_MALLOC(NetworkStateNotifier); | 43 USING_FAST_MALLOC(NetworkStateNotifier); |
| 44 | 44 |
| 45 public: | 45 public: |
| 46 class NetworkStateObserver { | 46 class NetworkStateObserver { |
| 47 public: | 47 public: |
| 48 // Will be called on the thread of the context passed in addObserver. | 48 // Will be called on the task runner that is passed in addObserver. |
| 49 virtual void connectionChange(WebConnectionType, | 49 virtual void connectionChange(WebConnectionType, |
| 50 double maxBandwidthMbps) = 0; | 50 double maxBandwidthMbps) = 0; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 NetworkStateNotifier() : m_hasOverride(false) {} | 53 NetworkStateNotifier() : m_hasOverride(false) {} |
| 54 | 54 |
| 55 // Can be called on any thread. | 55 // Can be called on any thread. |
| 56 bool onLine() const { | 56 bool onLine() const { |
| 57 MutexLocker locker(m_mutex); | 57 MutexLocker locker(m_mutex); |
| 58 const NetworkState& state = m_hasOverride ? m_override : m_state; | 58 const NetworkState& state = m_hasOverride ? m_override : m_state; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // When called, successive setWebConnectionType/setOnLine calls are stored, | 103 // When called, successive setWebConnectionType/setOnLine calls are stored, |
| 104 // and supplied overridden values are used instead until clearOverride() is | 104 // and supplied overridden values are used instead until clearOverride() is |
| 105 // called. This is used for layout tests (see crbug.com/377736) and inspector | 105 // called. This is used for layout tests (see crbug.com/377736) and inspector |
| 106 // emulation. | 106 // emulation. |
| 107 // | 107 // |
| 108 // Since this class is a singleton, tests must clear override when completed | 108 // Since this class is a singleton, tests must clear override when completed |
| 109 // to avoid indeterminate state across the test harness. | 109 // to avoid indeterminate state across the test harness. |
| 110 void setOverride(bool onLine, WebConnectionType, double maxBandwidthMbps); | 110 void setOverride(bool onLine, WebConnectionType, double maxBandwidthMbps); |
| 111 void clearOverride(); | 111 void clearOverride(); |
| 112 | 112 |
| 113 // Must be called on the context's thread. An added observer must be removed | 113 // Must be called on the given task runner. An added observer must be removed |
| 114 // before its ExecutionContext is deleted. It's possible for an observer to | 114 // before the observer or its execution context goes away. It's possible for |
| 115 // be called twice for the same event if it is first removed and then added | 115 // an observer to be called twice for the same event if it is first removed |
| 116 // during notification. | 116 // and then added during notification. |
| 117 void addObserver(NetworkStateObserver*, ExecutionContext*); | 117 void addObserver(NetworkStateObserver*, WebTaskRunner*); |
| 118 void removeObserver(NetworkStateObserver*, ExecutionContext*); | 118 void removeObserver(NetworkStateObserver*, WebTaskRunner*); |
| 119 | 119 |
| 120 private: | 120 private: |
| 121 struct ObserverList { | 121 struct ObserverList { |
| 122 ObserverList() : iterating(false) {} | 122 ObserverList() : iterating(false) {} |
| 123 bool iterating; | 123 bool iterating; |
| 124 Vector<NetworkStateObserver*> observers; | 124 Vector<NetworkStateObserver*> observers; |
| 125 Vector<size_t> zeroedObservers; // Indices in observers that are 0. | 125 Vector<size_t> zeroedObservers; // Indices in observers that are 0. |
| 126 }; | 126 }; |
| 127 | 127 |
| 128 struct NetworkState { | 128 struct NetworkState { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 142 public: | 142 public: |
| 143 explicit ScopedNotifier(NetworkStateNotifier&); | 143 explicit ScopedNotifier(NetworkStateNotifier&); |
| 144 ~ScopedNotifier(); | 144 ~ScopedNotifier(); |
| 145 | 145 |
| 146 private: | 146 private: |
| 147 NetworkStateNotifier& m_notifier; | 147 NetworkStateNotifier& m_notifier; |
| 148 NetworkState m_before; | 148 NetworkState m_before; |
| 149 }; | 149 }; |
| 150 | 150 |
| 151 // The ObserverListMap is cross-thread accessed, adding/removing Observers | 151 // The ObserverListMap is cross-thread accessed, adding/removing Observers |
| 152 // running within an ExecutionContext. Kept off-heap to ease cross-thread | 152 // running on a task runner. |
| 153 // allocation and use; the observers are (already) responsible for explicitly | |
| 154 // unregistering while finalizing. | |
| 155 using ObserverListMap = | 153 using ObserverListMap = |
| 156 HashMap<UntracedMember<ExecutionContext>, std::unique_ptr<ObserverList>>; | 154 HashMap<WebTaskRunner*, std::unique_ptr<ObserverList>>; |
| 157 | 155 |
| 158 void notifyObservers(WebConnectionType, double maxBandwidthMbps); | 156 void notifyObservers(WebConnectionType, double maxBandwidthMbps); |
| 159 void notifyObserversOfConnectionChangeOnContext(WebConnectionType, | 157 void notifyObserversOfConnectionChangeOnTaskRunner(WebConnectionType, |
| 160 double maxBandwidthMbps, | 158 double maxBandwidthMbps, |
| 161 ExecutionContext*); | 159 WebTaskRunner*); |
| 162 | 160 |
| 163 ObserverList* lockAndFindObserverList(ExecutionContext*); | 161 ObserverList* lockAndFindObserverList(WebTaskRunner*); |
| 164 | 162 |
| 165 // Removed observers are nulled out in the list in case the list is being | 163 // Removed observers are nulled out in the list in case the list is being |
| 166 // iterated over. Once done iterating, call this to clean up nulled | 164 // iterated over. Once done iterating, call this to clean up nulled |
| 167 // observers. | 165 // observers. |
| 168 void collectZeroedObservers(ObserverList*, ExecutionContext*); | 166 void collectZeroedObservers(ObserverList*, WebTaskRunner*); |
| 169 | 167 |
| 170 mutable Mutex m_mutex; | 168 mutable Mutex m_mutex; |
| 171 NetworkState m_state; | 169 NetworkState m_state; |
| 172 bool m_hasOverride; | 170 bool m_hasOverride; |
| 173 NetworkState m_override; | 171 NetworkState m_override; |
| 174 ObserverListMap m_observers; | 172 ObserverListMap m_observers; |
| 175 }; | 173 }; |
| 176 | 174 |
| 177 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); | 175 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); |
| 178 | 176 |
| 179 } // namespace blink | 177 } // namespace blink |
| 180 | 178 |
| 181 #endif // NetworkStateNotifier_h | 179 #endif // NetworkStateNotifier_h |
| OLD | NEW |