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

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

Issue 2087293003: [DevTools] Network.emulateNetworkConditions now affects NetworkStateNotifier. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ScopedNotifier Created 4 years, 5 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 30 matching lines...) Expand all
41 class CORE_EXPORT NetworkStateNotifier { 41 class CORE_EXPORT NetworkStateNotifier {
42 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); USING_FAST_MALLOC(NetworkStateNo tifier); 42 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); USING_FAST_MALLOC(NetworkStateNo tifier);
43 public: 43 public:
44 class NetworkStateObserver { 44 class NetworkStateObserver {
45 public: 45 public:
46 // Will be called on the thread of the context passed in addObserver. 46 // Will be called on the thread of the context passed in addObserver.
47 virtual void connectionChange(WebConnectionType, double maxBandwidthMbps ) = 0; 47 virtual void connectionChange(WebConnectionType, double maxBandwidthMbps ) = 0;
48 }; 48 };
49 49
50 NetworkStateNotifier() 50 NetworkStateNotifier()
51 : m_initialized(false) 51 : m_hasOverride(false)
52 , m_isOnLine(true)
53 , m_type(WebConnectionTypeOther)
54 , m_maxBandwidthMbps(kInvalidMaxBandwidth)
55 , m_testUpdatesOnly(false)
56 { 52 {
57 } 53 }
58 54
59 // Can be called on any thread. 55 // Can be called on any thread.
60 bool onLine() const 56 bool onLine() const
61 { 57 {
62 MutexLocker locker(m_mutex); 58 MutexLocker locker(m_mutex);
63 ASSERT(m_initialized); 59 const Data& data = m_hasOverride ? m_overrideData : m_data;
64 return m_isOnLine; 60 DCHECK(data.onLineInitialized);
61 return data.onLine;
65 } 62 }
66 63
67 void setOnLine(bool); 64 void setOnLine(bool);
68 65
69 // Can be called on any thread. 66 // Can be called on any thread.
70 WebConnectionType connectionType() const 67 WebConnectionType connectionType() const
71 { 68 {
72 MutexLocker locker(m_mutex); 69 MutexLocker locker(m_mutex);
73 ASSERT(m_initialized); 70 const Data& data = m_hasOverride ? m_overrideData : m_data;
74 return m_type; 71 DCHECK(data.connectionInitialized);
72 return data.type;
75 } 73 }
76 74
77 // Can be called on any thread. 75 // Can be called on any thread.
78 bool isCellularConnectionType() const 76 bool isCellularConnectionType() const
79 { 77 {
80 switch (connectionType()) { 78 switch (connectionType()) {
81 case WebConnectionTypeCellular2G: 79 case WebConnectionTypeCellular2G:
82 case WebConnectionTypeCellular3G: 80 case WebConnectionTypeCellular3G:
83 case WebConnectionTypeCellular4G: 81 case WebConnectionTypeCellular4G:
84 return true; 82 return true;
85 case WebConnectionTypeBluetooth: 83 case WebConnectionTypeBluetooth:
86 case WebConnectionTypeEthernet: 84 case WebConnectionTypeEthernet:
87 case WebConnectionTypeWifi: 85 case WebConnectionTypeWifi:
88 case WebConnectionTypeWimax: 86 case WebConnectionTypeWimax:
89 case WebConnectionTypeOther: 87 case WebConnectionTypeOther:
90 case WebConnectionTypeNone: 88 case WebConnectionTypeNone:
91 case WebConnectionTypeUnknown: 89 case WebConnectionTypeUnknown:
92 return false; 90 return false;
93 } 91 }
94 ASSERT_NOT_REACHED(); 92 ASSERT_NOT_REACHED();
95 return false; 93 return false;
96 } 94 }
97 95
98 // Can be called on any thread. 96 // Can be called on any thread.
99 double maxBandwidth() const 97 double maxBandwidth() const
100 { 98 {
101 MutexLocker locker(m_mutex); 99 MutexLocker locker(m_mutex);
102 ASSERT(m_initialized); 100 const Data& data = m_hasOverride ? m_overrideData : m_data;
103 return m_maxBandwidthMbps; 101 DCHECK(data.connectionInitialized);
102 return data.maxBandwidthMbps;
104 } 103 }
105 104
106 void setWebConnection(WebConnectionType, double maxBandwidthMbps); 105 void setWebConnection(WebConnectionType, double maxBandwidthMbps);
107 106
107 // When called, successive setWebConnectionType/setOnLine calls are ignored,
108 // and supplied overridden values are used instead.
109 // This is used for layout tests (see crbug.com/377736) and inspector emulat ion.
110 //
111 // Since this class is a singleton, tests must clear override when completed to
112 // avoid indeterminate state across the test harness. When switching in or o ut of test
113 // mode, all state will be reset to default values.
114 void setOverride(bool onLine, WebConnectionType, double maxBandwidthMbps);
115 void clearOverride();
116
108 // Must be called on the context's thread. An added observer must be removed 117 // Must be called on the context's thread. An added observer must be removed
109 // before its ExecutionContext is deleted. It's possible for an observer to 118 // before its ExecutionContext is deleted. It's possible for an observer to
110 // be called twice for the same event if it is first removed and then added 119 // be called twice for the same event if it is first removed and then added
111 // during notification. 120 // during notification.
112 void addObserver(NetworkStateObserver*, ExecutionContext*); 121 void addObserver(NetworkStateObserver*, ExecutionContext*);
113 void removeObserver(NetworkStateObserver*, ExecutionContext*); 122 void removeObserver(NetworkStateObserver*, ExecutionContext*);
114 123
115 // The following functions are for testing purposes.
116
117 // When true, setWebConnectionType calls are ignored and only setWebConnecti onTypeForTest
118 // can update the connection type. This is used for layout tests (see crbug. com/377736).
119 //
120 // Since this class is a singleton, tests must call this with false when com pleted to
121 // avoid indeterminate state across the test harness. When switching in or o ut of test
122 // mode, all state will be reset to default values.
123 void setTestUpdatesOnly(bool);
124 // Tests should call this as it will change the type regardless of the value of m_testUpdatesOnly.
125 void setWebConnectionForTest(WebConnectionType, double maxBandwidthMbps);
126
127 private: 124 private:
128 struct ObserverList { 125 struct ObserverList {
129 ObserverList() 126 ObserverList()
130 : iterating(false) 127 : iterating(false)
131 { 128 {
132 } 129 }
133 bool iterating; 130 bool iterating;
134 Vector<NetworkStateObserver*> observers; 131 Vector<NetworkStateObserver*> observers;
135 Vector<size_t> zeroedObservers; // Indices in observers that are 0. 132 Vector<size_t> zeroedObservers; // Indices in observers that are 0.
136 }; 133 };
137 134
138 const int kInvalidMaxBandwidth = -1; 135 struct Data {
jkarlin 2016/06/28 14:23:41 How about a slightly more descriptive name such as
dgozman 2016/06/28 17:14:23 Done.
136 static const int kInvalidMaxBandwidth = -1;
137 Data()
138 : onLineInitialized(false)
139 , onLine(true)
140 , connectionInitialized(false)
141 , type(WebConnectionTypeOther)
142 , maxBandwidthMbps(kInvalidMaxBandwidth)
143 {
144 }
145 bool onLineInitialized;
jkarlin 2016/06/28 14:23:41 Please initialize the members here instead of in t
dgozman 2016/06/28 17:14:23 Done.
146 bool onLine;
147 bool connectionInitialized;
148 WebConnectionType type;
149 double maxBandwidthMbps;
150 };
139 151
140 void setWebConnectionImpl(WebConnectionType, double maxBandwidthMbps); 152 class ScopedNotifier {
jkarlin 2016/06/28 14:23:41 This class needs documentation
dgozman 2016/06/28 17:14:23 Done.
141 void setMaxBandwidthImpl(double maxBandwidthMbps); 153 public:
154 explicit ScopedNotifier(NetworkStateNotifier&);
jkarlin 2016/06/28 14:23:41 Why not use a raw pointer?
dgozman 2016/06/28 17:14:23 Because it's never null.
jkarlin 2016/06/28 17:57:11 Ah, sorry, Chrome style hiccup.
155 ~ScopedNotifier();
156 private:
157 NetworkStateNotifier& m_notifier;
158 Data m_before;
159 };
142 160
143 // The ObserverListMap is cross-thread accessed, adding/removing Observers r unning 161 // The ObserverListMap is cross-thread accessed, adding/removing Observers r unning
144 // within an ExecutionContext. Kept off-heap to ease cross-thread allocation and use; 162 // within an ExecutionContext. Kept off-heap to ease cross-thread allocation and use;
145 // the observers are (already) responsible for explicitly unregistering whil e finalizing. 163 // the observers are (already) responsible for explicitly unregistering whil e finalizing.
146 using ObserverListMap = HashMap<UntracedMember<ExecutionContext>, std::uniqu e_ptr<ObserverList>>; 164 using ObserverListMap = HashMap<UntracedMember<ExecutionContext>, std::uniqu e_ptr<ObserverList>>;
147 165
166 void notifyObservers(WebConnectionType, double maxBandwidthMbps);
148 void notifyObserversOfConnectionChangeOnContext(WebConnectionType, double ma xBandwidthMbps, ExecutionContext*); 167 void notifyObserversOfConnectionChangeOnContext(WebConnectionType, double ma xBandwidthMbps, ExecutionContext*);
149 168
150 ObserverList* lockAndFindObserverList(ExecutionContext*); 169 ObserverList* lockAndFindObserverList(ExecutionContext*);
151 170
152 // Removed observers are nulled out in the list in case the list is being 171 // Removed observers are nulled out in the list in case the list is being
153 // iterated over. Once done iterating, call this to clean up nulled 172 // iterated over. Once done iterating, call this to clean up nulled
154 // observers. 173 // observers.
155 void collectZeroedObservers(ObserverList*, ExecutionContext*); 174 void collectZeroedObservers(ObserverList*, ExecutionContext*);
156 175
157 mutable Mutex m_mutex; 176 mutable Mutex m_mutex;
158 bool m_initialized; 177 Data m_data;
159 bool m_isOnLine; 178 bool m_hasOverride;
160 WebConnectionType m_type; 179 Data m_overrideData;
161 double m_maxBandwidthMbps;
162 ObserverListMap m_observers; 180 ObserverListMap m_observers;
163 bool m_testUpdatesOnly;
164 }; 181 };
165 182
166 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); 183 CORE_EXPORT NetworkStateNotifier& networkStateNotifier();
167 184
168 } // namespace blink 185 } // namespace blink
169 186
170 #endif // NetworkStateNotifier_h 187 #endif // NetworkStateNotifier_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698