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& state = m_hasOverride ? m_override : m_state; |
64 return m_isOnLine; | 60 DCHECK(state.onLineInitialized); |
| 61 return state.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& state = m_hasOverride ? m_override : m_state; |
74 return m_type; | 71 DCHECK(state.connectionInitialized); |
| 72 return state.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& state = m_hasOverride ? m_override : m_state; |
103 return m_maxBandwidthMbps; | 101 DCHECK(state.connectionInitialized); |
| 102 return state.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 stored, |
| 108 // and supplied overridden values are used instead until clearOverride() is
called. |
| 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. |
| 113 void setOverride(bool onLine, WebConnectionType, double maxBandwidthMbps); |
| 114 void clearOverride(); |
| 115 |
108 // Must be called on the context's thread. An added observer must be removed | 116 // 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 | 117 // 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 | 118 // be called twice for the same event if it is first removed and then added |
111 // during notification. | 119 // during notification. |
112 void addObserver(NetworkStateObserver*, ExecutionContext*); | 120 void addObserver(NetworkStateObserver*, ExecutionContext*); |
113 void removeObserver(NetworkStateObserver*, ExecutionContext*); | 121 void removeObserver(NetworkStateObserver*, ExecutionContext*); |
114 | 122 |
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: | 123 private: |
128 struct ObserverList { | 124 struct ObserverList { |
129 ObserverList() | 125 ObserverList() |
130 : iterating(false) | 126 : iterating(false) |
131 { | 127 { |
132 } | 128 } |
133 bool iterating; | 129 bool iterating; |
134 Vector<NetworkStateObserver*> observers; | 130 Vector<NetworkStateObserver*> observers; |
135 Vector<size_t> zeroedObservers; // Indices in observers that are 0. | 131 Vector<size_t> zeroedObservers; // Indices in observers that are 0. |
136 }; | 132 }; |
137 | 133 |
138 const int kInvalidMaxBandwidth = -1; | 134 struct NetworkState { |
| 135 static const int kInvalidMaxBandwidth = -1; |
| 136 bool onLineInitialized = false; |
| 137 bool onLine = true; |
| 138 bool connectionInitialized = false; |
| 139 WebConnectionType type = WebConnectionTypeOther; |
| 140 double maxBandwidthMbps = kInvalidMaxBandwidth; |
| 141 }; |
139 | 142 |
140 void setWebConnectionImpl(WebConnectionType, double maxBandwidthMbps); | 143 // This helper scope issues required notifications when mutating the state i
f something has changed. |
141 void setMaxBandwidthImpl(double maxBandwidthMbps); | 144 // It's only possible to mutate the state on the main thread. |
| 145 // Note that ScopedNotifier must be destroyed when not holding a lock |
| 146 // so that onLine notifications can be dispatched without a deadlock. |
| 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 |