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 |
(...skipping 30 matching lines...) Expand all Loading... | |
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 NetworkState& data = m_hasOverride ? m_override : m_state; |
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 NetworkState& data = m_hasOverride ? m_override : m_state; |
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 NetworkState& data = m_hasOverride ? m_override : m_state; |
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, | |
jkarlin
2016/06/28 17:57:12
Hmm, not quite ignored, since they're stored and w
dgozman
2016/06/29 01:17:33
Done.
| |
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. | |
jkarlin
2016/06/28 17:57:12
This last sentence is no longer true.
dgozman
2016/06/29 01:17:33
Good catch! Done.
| |
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 NetworkState { |
136 static const int kInvalidMaxBandwidth = -1; | |
137 bool onLineInitialized = false; | |
138 bool onLine = true; | |
139 bool connectionInitialized = false; | |
140 WebConnectionType type = WebConnectionTypeOther; | |
141 double maxBandwidthMbps = kInvalidMaxBandwidth; | |
142 }; | |
139 | 143 |
140 void setWebConnectionImpl(WebConnectionType, double maxBandwidthMbps); | 144 // This helper scope acquires the lock to access the members of NetworkState Notifier |
141 void setMaxBandwidthImpl(double maxBandwidthMbps); | 145 // and issues required notifications when mutating the state, if something c hanged. |
146 // It's only possible to mutate the state on main thread. | |
jkarlin
2016/06/28 17:57:12
s/on main thread/on the main thread/
dgozman
2016/06/29 01:17:33
Done.
| |
147 class ScopedNotifier { | |
148 public: | |
149 explicit ScopedNotifier(NetworkStateNotifier&); | |
150 ~ScopedNotifier(); | |
151 private: | |
152 NetworkStateNotifier& m_notifier; | |
153 NetworkState m_before; | |
154 }; | |
142 | 155 |
143 // The ObserverListMap is cross-thread accessed, adding/removing Observers r unning | 156 // 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; | 157 // 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. | 158 // the observers are (already) responsible for explicitly unregistering whil e finalizing. |
146 using ObserverListMap = HashMap<UntracedMember<ExecutionContext>, std::uniqu e_ptr<ObserverList>>; | 159 using ObserverListMap = HashMap<UntracedMember<ExecutionContext>, std::uniqu e_ptr<ObserverList>>; |
147 | 160 |
161 void notifyObservers(WebConnectionType, double maxBandwidthMbps); | |
148 void notifyObserversOfConnectionChangeOnContext(WebConnectionType, double ma xBandwidthMbps, ExecutionContext*); | 162 void notifyObserversOfConnectionChangeOnContext(WebConnectionType, double ma xBandwidthMbps, ExecutionContext*); |
149 | 163 |
150 ObserverList* lockAndFindObserverList(ExecutionContext*); | 164 ObserverList* lockAndFindObserverList(ExecutionContext*); |
151 | 165 |
152 // Removed observers are nulled out in the list in case the list is being | 166 // 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 | 167 // iterated over. Once done iterating, call this to clean up nulled |
154 // observers. | 168 // observers. |
155 void collectZeroedObservers(ObserverList*, ExecutionContext*); | 169 void collectZeroedObservers(ObserverList*, ExecutionContext*); |
156 | 170 |
157 mutable Mutex m_mutex; | 171 mutable Mutex m_mutex; |
158 bool m_initialized; | 172 NetworkState m_state; |
159 bool m_isOnLine; | 173 bool m_hasOverride; |
160 WebConnectionType m_type; | 174 NetworkState m_override; |
161 double m_maxBandwidthMbps; | |
162 ObserverListMap m_observers; | 175 ObserverListMap m_observers; |
163 bool m_testUpdatesOnly; | |
164 }; | 176 }; |
165 | 177 |
166 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); | 178 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); |
167 | 179 |
168 } // namespace blink | 180 } // namespace blink |
169 | 181 |
170 #endif // NetworkStateNotifier_h | 182 #endif // NetworkStateNotifier_h |
OLD | NEW |