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

Side by Side Diff: third_party/WebKit/Source/core/page/NetworkStateNotifier.h

Issue 2704083002: Remove Page dependency from NetworkStateNotifier (Closed)
Patch Set: nullcheck for tests Created 3 years, 10 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
OLDNEW
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
(...skipping 10 matching lines...) Expand all
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 <memory>
30 #include "core/CoreExport.h" 30 #include "core/CoreExport.h"
31 #include "platform/CrossThreadCopier.h"
31 #include "platform/WebTaskRunner.h" 32 #include "platform/WebTaskRunner.h"
32 #include "public/platform/WebConnectionType.h" 33 #include "public/platform/WebConnectionType.h"
33 #include "wtf/Allocator.h" 34 #include "wtf/Allocator.h"
34 #include "wtf/HashMap.h" 35 #include "wtf/HashMap.h"
35 #include "wtf/Noncopyable.h" 36 #include "wtf/Noncopyable.h"
36 #include "wtf/ThreadingPrimitives.h" 37 #include "wtf/ThreadingPrimitives.h"
37 #include "wtf/Vector.h" 38 #include "wtf/Vector.h"
38 39
39 namespace blink { 40 namespace blink {
40 41
41 class CORE_EXPORT NetworkStateNotifier { 42 class CORE_EXPORT NetworkStateNotifier {
42 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); 43 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier);
43 USING_FAST_MALLOC(NetworkStateNotifier); 44 USING_FAST_MALLOC(NetworkStateNotifier);
44 45
45 public: 46 public:
46 class NetworkStateObserver { 47 class NetworkStateObserver {
47 public: 48 public:
48 // Will be called on the task runner that is passed in addObserver. 49 // Will be called on the task runner that is passed in add*Observer.
49 virtual void connectionChange(WebConnectionType, 50 virtual void connectionChange(WebConnectionType, double maxBandwidthMbps) {}
50 double maxBandwidthMbps) = 0; 51 virtual void onLineStateChange(bool onLine) {}
51 }; 52 };
52 53
53 NetworkStateNotifier() : m_hasOverride(false) {} 54 NetworkStateNotifier() : m_hasOverride(false) {}
54 55
55 // Can be called on any thread. 56 // Can be called on any thread.
56 bool onLine() const { 57 bool onLine() const {
57 MutexLocker locker(m_mutex); 58 MutexLocker locker(m_mutex);
58 const NetworkState& state = m_hasOverride ? m_override : m_state; 59 const NetworkState& state = m_hasOverride ? m_override : m_state;
59 DCHECK(state.onLineInitialized); 60 DCHECK(state.onLineInitialized);
60 return state.onLine; 61 return state.onLine;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 // 108 //
108 // Since this class is a singleton, tests must clear override when completed 109 // Since this class is a singleton, tests must clear override when completed
109 // to avoid indeterminate state across the test harness. 110 // to avoid indeterminate state across the test harness.
110 void setOverride(bool onLine, WebConnectionType, double maxBandwidthMbps); 111 void setOverride(bool onLine, WebConnectionType, double maxBandwidthMbps);
111 void clearOverride(); 112 void clearOverride();
112 113
113 // Must be called on the given task runner. An added observer must be removed 114 // Must be called on the given task runner. An added observer must be removed
114 // before the observer or its execution context goes away. It's possible for 115 // before the observer or its execution context goes away. It's possible for
115 // an observer to be called twice for the same event if it is first removed 116 // an observer to be called twice for the same event if it is first removed
116 // and then added during notification. 117 // and then added during notification.
117 void addObserver(NetworkStateObserver*, WebTaskRunner*); 118 void addConnectionObserver(NetworkStateObserver*, PassRefPtr<WebTaskRunner>);
118 void removeObserver(NetworkStateObserver*, WebTaskRunner*); 119 void addOnLineObserver(NetworkStateObserver*, PassRefPtr<WebTaskRunner>);
120 void removeConnectionObserver(NetworkStateObserver*,
121 PassRefPtr<WebTaskRunner>);
122 void removeOnLineObserver(NetworkStateObserver*, PassRefPtr<WebTaskRunner>);
119 123
120 private: 124 private:
121 struct ObserverList { 125 struct ObserverList {
122 ObserverList() : iterating(false) {} 126 ObserverList() : iterating(false) {}
123 bool iterating; 127 bool iterating;
124 Vector<NetworkStateObserver*> observers; 128 Vector<NetworkStateObserver*> observers;
125 Vector<size_t> zeroedObservers; // Indices in observers that are 0. 129 Vector<size_t> zeroedObservers; // Indices in observers that are 0.
126 }; 130 };
127 131
128 struct NetworkState { 132 struct NetworkState {
129 static const int kInvalidMaxBandwidth = -1; 133 static const int kInvalidMaxBandwidth = -1;
130 bool onLineInitialized = false; 134 bool onLineInitialized = false;
131 bool onLine = true; 135 bool onLine = true;
132 bool connectionInitialized = false; 136 bool connectionInitialized = false;
133 WebConnectionType type = WebConnectionTypeOther; 137 WebConnectionType type = WebConnectionTypeOther;
134 double maxBandwidthMbps = kInvalidMaxBandwidth; 138 double maxBandwidthMbps = kInvalidMaxBandwidth;
135 }; 139 };
140 friend CrossThreadCopier<NetworkStateNotifier::NetworkState>;
136 141
137 // This helper scope issues required notifications when mutating the state if 142 // This helper scope issues required notifications when mutating the state if
138 // something has changed. It's only possible to mutate the state on the main 143 // something has changed. It's only possible to mutate the state on the main
139 // thread. Note that ScopedNotifier must be destroyed when not holding a lock 144 // thread. Note that ScopedNotifier must be destroyed when not holding a lock
140 // so that onLine notifications can be dispatched without a deadlock. 145 // so that onLine notifications can be dispatched without a deadlock.
141 class ScopedNotifier { 146 class ScopedNotifier {
142 public: 147 public:
143 explicit ScopedNotifier(NetworkStateNotifier&); 148 explicit ScopedNotifier(NetworkStateNotifier&);
144 ~ScopedNotifier(); 149 ~ScopedNotifier();
145 150
146 private: 151 private:
147 NetworkStateNotifier& m_notifier; 152 NetworkStateNotifier& m_notifier;
148 NetworkState m_before; 153 NetworkState m_before;
149 }; 154 };
150 155
156 enum class ObserverType {
157 ONLINE_STATE,
158 CONNECTION_TYPE,
159 };
160
151 // The ObserverListMap is cross-thread accessed, adding/removing Observers 161 // The ObserverListMap is cross-thread accessed, adding/removing Observers
152 // running on a task runner. 162 // running on a task runner.
153 using ObserverListMap = 163 using ObserverListMap =
154 HashMap<WebTaskRunner*, std::unique_ptr<ObserverList>>; 164 HashMap<RefPtr<WebTaskRunner>, std::unique_ptr<ObserverList>>;
155 165
156 void notifyObservers(WebConnectionType, double maxBandwidthMbps); 166 void notifyObservers(ObserverListMap&, ObserverType, const NetworkState&);
157 void notifyObserversOfConnectionChangeOnTaskRunner(WebConnectionType, 167 void notifyObserversOnTaskRunner(ObserverListMap*,
158 double maxBandwidthMbps, 168 ObserverType,
159 WebTaskRunner*); 169 PassRefPtr<WebTaskRunner>,
170 const NetworkState&);
160 171
161 ObserverList* lockAndFindObserverList(WebTaskRunner*); 172 void addObserver(ObserverListMap&,
173 NetworkStateObserver*,
174 PassRefPtr<WebTaskRunner>);
175 void removeObserver(ObserverListMap&,
176 NetworkStateObserver*,
177 PassRefPtr<WebTaskRunner>);
178
179 ObserverList* lockAndFindObserverList(ObserverListMap&,
180 PassRefPtr<WebTaskRunner>);
162 181
163 // Removed observers are nulled out in the list in case the list is being 182 // Removed observers are nulled out in the list in case the list is being
164 // iterated over. Once done iterating, call this to clean up nulled 183 // iterated over. Once done iterating, call this to clean up nulled
165 // observers. 184 // observers.
166 void collectZeroedObservers(ObserverList*, WebTaskRunner*); 185 void collectZeroedObservers(ObserverListMap&,
186 ObserverList*,
187 PassRefPtr<WebTaskRunner>);
167 188
168 mutable Mutex m_mutex; 189 mutable Mutex m_mutex;
169 NetworkState m_state; 190 NetworkState m_state;
170 bool m_hasOverride; 191 bool m_hasOverride;
171 NetworkState m_override; 192 NetworkState m_override;
172 ObserverListMap m_observers; 193
194 ObserverListMap m_connectionObservers;
195 ObserverListMap m_onLineStateObservers;
173 }; 196 };
174 197
175 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); 198 CORE_EXPORT NetworkStateNotifier& networkStateNotifier();
176 199
177 } // namespace blink 200 } // namespace blink
178 201
179 #endif // NetworkStateNotifier_h 202 #endif // NetworkStateNotifier_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/dom/Document.cpp ('k') | third_party/WebKit/Source/core/page/NetworkStateNotifier.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698