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

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

Issue 2710023006: Move NetworkStateNotifier from core (and web) into platform/network (Closed)
Patch Set: rebase Created 3 years, 9 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
(Empty)
1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
24 */
25
26 #ifndef NetworkStateNotifier_h
27 #define NetworkStateNotifier_h
28
29 #include <memory>
30 #include "core/CoreExport.h"
31 #include "platform/CrossThreadCopier.h"
32 #include "platform/WebTaskRunner.h"
33 #include "public/platform/WebConnectionType.h"
34 #include "wtf/Allocator.h"
35 #include "wtf/HashMap.h"
36 #include "wtf/Noncopyable.h"
37 #include "wtf/ThreadingPrimitives.h"
38 #include "wtf/Vector.h"
39
40 namespace blink {
41
42 class CORE_EXPORT NetworkStateNotifier {
43 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier);
44 USING_FAST_MALLOC(NetworkStateNotifier);
45
46 public:
47 struct NetworkState {
48 static const int kInvalidMaxBandwidth = -1;
49 bool onLineInitialized = false;
50 bool onLine = true;
51 bool connectionInitialized = false;
52 WebConnectionType type = WebConnectionTypeOther;
53 double maxBandwidthMbps = kInvalidMaxBandwidth;
54 };
55
56 class NetworkStateObserver {
57 public:
58 // Will be called on the task runner that is passed in add*Observer.
59 virtual void connectionChange(WebConnectionType, double maxBandwidthMbps) {}
60 virtual void onLineStateChange(bool onLine) {}
61 };
62
63 NetworkStateNotifier() : m_hasOverride(false) {}
64
65 // Can be called on any thread.
66 bool onLine() const {
67 MutexLocker locker(m_mutex);
68 const NetworkState& state = m_hasOverride ? m_override : m_state;
69 DCHECK(state.onLineInitialized);
70 return state.onLine;
71 }
72
73 void setOnLine(bool);
74
75 // Can be called on any thread.
76 WebConnectionType connectionType() const {
77 MutexLocker locker(m_mutex);
78 const NetworkState& state = m_hasOverride ? m_override : m_state;
79 DCHECK(state.connectionInitialized);
80 return state.type;
81 }
82
83 // Can be called on any thread.
84 bool isCellularConnectionType() const {
85 switch (connectionType()) {
86 case WebConnectionTypeCellular2G:
87 case WebConnectionTypeCellular3G:
88 case WebConnectionTypeCellular4G:
89 return true;
90 case WebConnectionTypeBluetooth:
91 case WebConnectionTypeEthernet:
92 case WebConnectionTypeWifi:
93 case WebConnectionTypeWimax:
94 case WebConnectionTypeOther:
95 case WebConnectionTypeNone:
96 case WebConnectionTypeUnknown:
97 return false;
98 }
99 ASSERT_NOT_REACHED();
100 return false;
101 }
102
103 // Can be called on any thread.
104 double maxBandwidth() const {
105 MutexLocker locker(m_mutex);
106 const NetworkState& state = m_hasOverride ? m_override : m_state;
107 DCHECK(state.connectionInitialized);
108 return state.maxBandwidthMbps;
109 }
110
111 void setWebConnection(WebConnectionType, double maxBandwidthMbps);
112
113 // When called, successive setWebConnectionType/setOnLine calls are stored,
114 // and supplied overridden values are used instead until clearOverride() is
115 // called. This is used for layout tests (see crbug.com/377736) and inspector
116 // emulation.
117 //
118 // Since this class is a singleton, tests must clear override when completed
119 // to avoid indeterminate state across the test harness.
120 void setOverride(bool onLine, WebConnectionType, double maxBandwidthMbps);
121 void clearOverride();
122
123 // Must be called on the given task runner. An added observer must be removed
124 // before the observer or its execution context goes away. It's possible for
125 // an observer to be called twice for the same event if it is first removed
126 // and then added during notification.
127 void addConnectionObserver(NetworkStateObserver*, PassRefPtr<WebTaskRunner>);
128 void addOnLineObserver(NetworkStateObserver*, PassRefPtr<WebTaskRunner>);
129 void removeConnectionObserver(NetworkStateObserver*,
130 PassRefPtr<WebTaskRunner>);
131 void removeOnLineObserver(NetworkStateObserver*, PassRefPtr<WebTaskRunner>);
132
133 private:
134 struct ObserverList {
135 ObserverList() : iterating(false) {}
136 bool iterating;
137 Vector<NetworkStateObserver*> observers;
138 Vector<size_t> zeroedObservers; // Indices in observers that are 0.
139 };
140
141 // This helper scope issues required notifications when mutating the state if
142 // something has changed. It's only possible to mutate the state on the main
143 // thread. Note that ScopedNotifier must be destroyed when not holding a lock
144 // so that onLine notifications can be dispatched without a deadlock.
145 class ScopedNotifier {
146 public:
147 explicit ScopedNotifier(NetworkStateNotifier&);
148 ~ScopedNotifier();
149
150 private:
151 NetworkStateNotifier& m_notifier;
152 NetworkState m_before;
153 };
154
155 enum class ObserverType {
156 ONLINE_STATE,
157 CONNECTION_TYPE,
158 };
159
160 // The ObserverListMap is cross-thread accessed, adding/removing Observers
161 // running on a task runner.
162 using ObserverListMap =
163 HashMap<RefPtr<WebTaskRunner>, std::unique_ptr<ObserverList>>;
164
165 void notifyObservers(ObserverListMap&, ObserverType, const NetworkState&);
166 void notifyObserversOnTaskRunner(ObserverListMap*,
167 ObserverType,
168 PassRefPtr<WebTaskRunner>,
169 const NetworkState&);
170
171 void addObserver(ObserverListMap&,
172 NetworkStateObserver*,
173 PassRefPtr<WebTaskRunner>);
174 void removeObserver(ObserverListMap&,
175 NetworkStateObserver*,
176 PassRefPtr<WebTaskRunner>);
177
178 ObserverList* lockAndFindObserverList(ObserverListMap&,
179 PassRefPtr<WebTaskRunner>);
180
181 // Removed observers are nulled out in the list in case the list is being
182 // iterated over. Once done iterating, call this to clean up nulled
183 // observers.
184 void collectZeroedObservers(ObserverListMap&,
185 ObserverList*,
186 PassRefPtr<WebTaskRunner>);
187
188 mutable Mutex m_mutex;
189 NetworkState m_state;
190 bool m_hasOverride;
191 NetworkState m_override;
192
193 ObserverListMap m_connectionObservers;
194 ObserverListMap m_onLineStateObservers;
195 };
196
197 CORE_EXPORT NetworkStateNotifier& networkStateNotifier();
198
199 } // namespace blink
200
201 #endif // NetworkStateNotifier_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/page/BUILD.gn ('k') | third_party/WebKit/Source/core/page/NetworkStateNotifier.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698