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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 MutexLocker locker(m_mutex); | 51 MutexLocker locker(m_mutex); |
52 if (m_isOnLine == onLine) | 52 if (m_isOnLine == onLine) |
53 return; | 53 return; |
54 | 54 |
55 m_isOnLine = onLine; | 55 m_isOnLine = onLine; |
56 } | 56 } |
57 | 57 |
58 Page::networkStateChanged(onLine); | 58 Page::networkStateChanged(onLine); |
59 } | 59 } |
60 | 60 |
61 void NetworkStateNotifier::setWebConnectionType(WebConnectionType type) | 61 void NetworkStateNotifier::setWebConnection(WebConnectionType type, double maxBa
ndwidthMbps) |
62 { | 62 { |
63 ASSERT(isMainThread()); | 63 ASSERT(isMainThread()); |
64 if (m_testUpdatesOnly) | 64 if (m_testUpdatesOnly) |
65 return; | 65 return; |
66 | 66 |
67 setWebConnectionTypeImpl(type); | 67 setWebConnectionImpl(type, maxBandwidthMbps); |
68 } | |
69 | |
70 void NetworkStateNotifier::setWebConnectionTypeImpl(WebConnectionType type) | |
71 { | |
72 ASSERT(isMainThread()); | |
73 | |
74 MutexLocker locker(m_mutex); | |
75 if (m_type == type) | |
76 return; | |
77 m_type = type; | |
78 | |
79 for (const auto& entry : m_observers) { | |
80 ExecutionContext* context = entry.key; | |
81 context->postTask(FROM_HERE, createCrossThreadTask(&NetworkStateNotifier
::notifyObserversOnContext, this, type)); | |
82 } | |
83 } | 68 } |
84 | 69 |
85 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, Execution
Context* context) | 70 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, Execution
Context* context) |
86 { | 71 { |
87 ASSERT(context->isContextThread()); | 72 ASSERT(context->isContextThread()); |
88 ASSERT(observer); | 73 ASSERT(observer); |
89 | 74 |
90 MutexLocker locker(m_mutex); | 75 MutexLocker locker(m_mutex); |
91 ObserverListMap::AddResult result = m_observers.add(context, nullptr); | 76 ObserverListMap::AddResult result = m_observers.add(context, nullptr); |
92 if (result.isNewEntry) | 77 if (result.isNewEntry) |
(...skipping 22 matching lines...) Expand all Loading... |
115 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty()) | 100 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty()) |
116 collectZeroedObservers(observerList, context); | 101 collectZeroedObservers(observerList, context); |
117 } | 102 } |
118 | 103 |
119 void NetworkStateNotifier::setTestUpdatesOnly(bool updatesOnly) | 104 void NetworkStateNotifier::setTestUpdatesOnly(bool updatesOnly) |
120 { | 105 { |
121 ASSERT(isMainThread()); | 106 ASSERT(isMainThread()); |
122 m_testUpdatesOnly = updatesOnly; | 107 m_testUpdatesOnly = updatesOnly; |
123 } | 108 } |
124 | 109 |
125 void NetworkStateNotifier::setWebConnectionTypeForTest(WebConnectionType type) | 110 void NetworkStateNotifier::setWebConnectionForTest(WebConnectionType type, doubl
e maxBandwidthMbps) |
126 { | 111 { |
127 ASSERT(isMainThread()); | 112 ASSERT(isMainThread()); |
128 ASSERT(m_testUpdatesOnly); | 113 ASSERT(m_testUpdatesOnly); |
129 setWebConnectionTypeImpl(type); | 114 setWebConnectionImpl(type, maxBandwidthMbps); |
130 } | 115 } |
131 | 116 |
132 void NetworkStateNotifier::notifyObserversOnContext(WebConnectionType type, Exec
utionContext* context) | 117 void NetworkStateNotifier::setWebConnectionImpl(WebConnectionType type, double m
axBandwidthMbps) |
| 118 { |
| 119 ASSERT(isMainThread()); |
| 120 |
| 121 MutexLocker locker(m_mutex); |
| 122 if (m_type == type && m_maxBandwidthMbps == maxBandwidthMbps) |
| 123 return; |
| 124 m_type = type; |
| 125 m_maxBandwidthMbps = maxBandwidthMbps; |
| 126 |
| 127 for (const auto& entry : m_observers) { |
| 128 ExecutionContext* context = entry.key; |
| 129 context->postTask(FROM_HERE, createCrossThreadTask(&NetworkStateNotifier
::notifyObserversOfConnectionChangeOnContext, this, type, maxBandwidthMbps)); |
| 130 } |
| 131 } |
| 132 |
| 133 void NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext(WebConnect
ionType type, double maxBandwidthMbps, ExecutionContext* context) |
133 { | 134 { |
134 ObserverList* observerList = lockAndFindObserverList(context); | 135 ObserverList* observerList = lockAndFindObserverList(context); |
135 | 136 |
136 // The context could have been removed before the notification task got to r
un. | 137 // The context could have been removed before the notification task got to r
un. |
137 if (!observerList) | 138 if (!observerList) |
138 return; | 139 return; |
139 | 140 |
140 ASSERT(context->isContextThread()); | 141 ASSERT(context->isContextThread()); |
141 | 142 |
142 observerList->iterating = true; | 143 observerList->iterating = true; |
143 | 144 |
144 for (size_t i = 0; i < observerList->observers.size(); ++i) { | 145 for (size_t i = 0; i < observerList->observers.size(); ++i) { |
145 // Observers removed during iteration are zeroed out, skip them. | 146 // Observers removed during iteration are zeroed out, skip them. |
146 if (observerList->observers[i]) | 147 if (observerList->observers[i]) |
147 observerList->observers[i]->connectionTypeChange(type); | 148 observerList->observers[i]->connectionChange(type, maxBandwidthMbps)
; |
148 } | 149 } |
149 | 150 |
150 observerList->iterating = false; | 151 observerList->iterating = false; |
151 | 152 |
152 if (!observerList->zeroedObservers.isEmpty()) | 153 if (!observerList->zeroedObservers.isEmpty()) |
153 collectZeroedObservers(observerList, context); | 154 collectZeroedObservers(observerList, context); |
154 } | 155 } |
155 | 156 |
156 NetworkStateNotifier::ObserverList* NetworkStateNotifier::lockAndFindObserverLis
t(ExecutionContext* context) | 157 NetworkStateNotifier::ObserverList* NetworkStateNotifier::lockAndFindObserverLis
t(ExecutionContext* context) |
157 { | 158 { |
(...skipping 14 matching lines...) Expand all Loading... |
172 | 173 |
173 list->zeroedObservers.clear(); | 174 list->zeroedObservers.clear(); |
174 | 175 |
175 if (list->observers.isEmpty()) { | 176 if (list->observers.isEmpty()) { |
176 MutexLocker locker(m_mutex); | 177 MutexLocker locker(m_mutex); |
177 m_observers.remove(context); // deletes list | 178 m_observers.remove(context); // deletes list |
178 } | 179 } |
179 } | 180 } |
180 | 181 |
181 } // namespace blink | 182 } // namespace blink |
OLD | NEW |