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

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: browser test Created 4 years, 6 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
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_initialized(false)
52 , m_isOnLine(true) 52 , m_isOnLine(true)
53 , m_type(WebConnectionTypeOther) 53 , m_type(WebConnectionTypeOther)
54 , m_maxBandwidthMbps(kInvalidMaxBandwidth) 54 , m_maxBandwidthMbps(kInvalidMaxBandwidth)
55 , m_testUpdatesOnly(false) 55 , m_hasOverride(false)
56 , m_overrideOnLine(true)
57 , m_overrideType(WebConnectionTypeOther)
58 , m_overrideMaxBandwidthMbps(kInvalidMaxBandwidth)
56 { 59 {
57 } 60 }
58 61
59 // Can be called on any thread. 62 // Can be called on any thread.
60 bool onLine() const 63 bool onLine() const
61 { 64 {
62 MutexLocker locker(m_mutex); 65 MutexLocker locker(m_mutex);
63 ASSERT(m_initialized); 66 ASSERT(m_initialized);
64 return m_isOnLine; 67 return m_hasOverride ? m_overrideOnLine : m_isOnLine;
65 } 68 }
66 69
67 void setOnLine(bool); 70 void setOnLine(bool);
68 71
69 // Can be called on any thread. 72 // Can be called on any thread.
70 WebConnectionType connectionType() const 73 WebConnectionType connectionType() const
71 { 74 {
72 MutexLocker locker(m_mutex); 75 MutexLocker locker(m_mutex);
73 ASSERT(m_initialized); 76 ASSERT(m_initialized);
74 return m_type; 77 return m_hasOverride ? m_overrideType : m_type;
75 } 78 }
76 79
77 // Can be called on any thread. 80 // Can be called on any thread.
78 bool isCellularConnectionType() const 81 bool isCellularConnectionType() const
79 { 82 {
80 switch (connectionType()) { 83 switch (connectionType()) {
81 case WebConnectionTypeCellular2G: 84 case WebConnectionTypeCellular2G:
82 case WebConnectionTypeCellular3G: 85 case WebConnectionTypeCellular3G:
83 case WebConnectionTypeCellular4G: 86 case WebConnectionTypeCellular4G:
84 return true; 87 return true;
85 case WebConnectionTypeBluetooth: 88 case WebConnectionTypeBluetooth:
86 case WebConnectionTypeEthernet: 89 case WebConnectionTypeEthernet:
87 case WebConnectionTypeWifi: 90 case WebConnectionTypeWifi:
88 case WebConnectionTypeWimax: 91 case WebConnectionTypeWimax:
89 case WebConnectionTypeOther: 92 case WebConnectionTypeOther:
90 case WebConnectionTypeNone: 93 case WebConnectionTypeNone:
91 case WebConnectionTypeUnknown: 94 case WebConnectionTypeUnknown:
92 return false; 95 return false;
93 } 96 }
94 ASSERT_NOT_REACHED(); 97 ASSERT_NOT_REACHED();
95 return false; 98 return false;
96 } 99 }
97 100
98 // Can be called on any thread. 101 // Can be called on any thread.
99 double maxBandwidth() const 102 double maxBandwidth() const
100 { 103 {
101 MutexLocker locker(m_mutex); 104 MutexLocker locker(m_mutex);
102 ASSERT(m_initialized); 105 ASSERT(m_initialized);
103 return m_maxBandwidthMbps; 106 return m_hasOverride ? m_overrideMaxBandwidthMbps : m_maxBandwidthMbps;
104 } 107 }
105 108
106 void setWebConnection(WebConnectionType, double maxBandwidthMbps); 109 void setWebConnection(WebConnectionType, double maxBandwidthMbps);
107 110
111 // When called, successive setWebConnectionType/setOnLine calls are ignored,
112 // and supplied overridden values are used instead.
113 // This is used for layout tests (see crbug.com/377736) and inspector emulat ion.
114 //
115 // Since this class is a singleton, tests must clear override when completed to
116 // avoid indeterminate state across the test harness. When switching in or o ut of test
117 // mode, all state will be reset to default values.
118 void setOverride(bool onLine, WebConnectionType, double maxBandwidthMbps);
119 void clearOverride();
120
108 // Must be called on the context's thread. An added observer must be removed 121 // 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 122 // 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 123 // be called twice for the same event if it is first removed and then added
111 // during notification. 124 // during notification.
112 void addObserver(NetworkStateObserver*, ExecutionContext*); 125 void addObserver(NetworkStateObserver*, ExecutionContext*);
113 void removeObserver(NetworkStateObserver*, ExecutionContext*); 126 void removeObserver(NetworkStateObserver*, ExecutionContext*);
114 127
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: 128 private:
128 struct ObserverList { 129 struct ObserverList {
129 ObserverList() 130 ObserverList()
130 : iterating(false) 131 : iterating(false)
131 { 132 {
132 } 133 }
133 bool iterating; 134 bool iterating;
134 Vector<NetworkStateObserver*> observers; 135 Vector<NetworkStateObserver*> observers;
135 Vector<size_t> zeroedObservers; // Indices in observers that are 0. 136 Vector<size_t> zeroedObservers; // Indices in observers that are 0.
136 }; 137 };
137 138
138 const int kInvalidMaxBandwidth = -1; 139 const int kInvalidMaxBandwidth = -1;
139 140
140 void setWebConnectionImpl(WebConnectionType, double maxBandwidthMbps);
141 void setMaxBandwidthImpl(double maxBandwidthMbps);
142
143 // The ObserverListMap is cross-thread accessed, adding/removing Observers r unning 141 // 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; 142 // 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. 143 // the observers are (already) responsible for explicitly unregistering whil e finalizing.
146 using ObserverListMap = HashMap<UntracedMember<ExecutionContext>, std::uniqu e_ptr<ObserverList>>; 144 using ObserverListMap = HashMap<UntracedMember<ExecutionContext>, std::uniqu e_ptr<ObserverList>>;
147 145
146 void notifyObservers();
148 void notifyObserversOfConnectionChangeOnContext(WebConnectionType, double ma xBandwidthMbps, ExecutionContext*); 147 void notifyObserversOfConnectionChangeOnContext(WebConnectionType, double ma xBandwidthMbps, ExecutionContext*);
149 148
150 ObserverList* lockAndFindObserverList(ExecutionContext*); 149 ObserverList* lockAndFindObserverList(ExecutionContext*);
151 150
152 // Removed observers are nulled out in the list in case the list is being 151 // 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 152 // iterated over. Once done iterating, call this to clean up nulled
154 // observers. 153 // observers.
155 void collectZeroedObservers(ObserverList*, ExecutionContext*); 154 void collectZeroedObservers(ObserverList*, ExecutionContext*);
156 155
157 mutable Mutex m_mutex; 156 mutable Mutex m_mutex;
158 bool m_initialized; 157 bool m_initialized;
159 bool m_isOnLine; 158 bool m_isOnLine;
160 WebConnectionType m_type; 159 WebConnectionType m_type;
161 double m_maxBandwidthMbps; 160 double m_maxBandwidthMbps;
162 ObserverListMap m_observers; 161 ObserverListMap m_observers;
163 bool m_testUpdatesOnly; 162 bool m_hasOverride;
163 bool m_overrideOnLine;
164 WebConnectionType m_overrideType;
165 double m_overrideMaxBandwidthMbps;
164 }; 166 };
165 167
166 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); 168 CORE_EXPORT NetworkStateNotifier& networkStateNotifier();
167 169
168 } // namespace blink 170 } // namespace blink
169 171
170 #endif // NetworkStateNotifier_h 172 #endif // NetworkStateNotifier_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698