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