Chromium Code Reviews| 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 |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "core/page/NetworkStateNotifier.h" | 26 #include "core/page/NetworkStateNotifier.h" |
| 27 | 27 |
| 28 #include "core/dom/ExecutionContext.h" | |
| 29 #include "core/dom/ExecutionContextTask.h" | |
| 30 #include "core/dom/TaskRunnerHelper.h" | 28 #include "core/dom/TaskRunnerHelper.h" |
| 31 #include "core/page/Page.h" | 29 #include "core/page/Page.h" |
| 30 #include "platform/CrossThreadFunctional.h" | |
| 32 #include "wtf/Assertions.h" | 31 #include "wtf/Assertions.h" |
| 33 #include "wtf/Functional.h" | 32 #include "wtf/Functional.h" |
| 34 #include "wtf/PtrUtil.h" | 33 #include "wtf/PtrUtil.h" |
| 35 #include "wtf/StdLibExtras.h" | 34 #include "wtf/StdLibExtras.h" |
| 36 #include "wtf/Threading.h" | 35 #include "wtf/Threading.h" |
| 37 | 36 |
| 38 namespace blink { | 37 namespace blink { |
| 39 | 38 |
| 40 NetworkStateNotifier& networkStateNotifier() { | 39 NetworkStateNotifier& networkStateNotifier() { |
| 41 DEFINE_THREAD_SAFE_STATIC_LOCAL(NetworkStateNotifier, networkStateNotifier, | 40 DEFINE_THREAD_SAFE_STATIC_LOCAL(NetworkStateNotifier, networkStateNotifier, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 ScopedNotifier notifier(*this); | 78 ScopedNotifier notifier(*this); |
| 80 { | 79 { |
| 81 MutexLocker locker(m_mutex); | 80 MutexLocker locker(m_mutex); |
| 82 m_state.connectionInitialized = true; | 81 m_state.connectionInitialized = true; |
| 83 m_state.type = type; | 82 m_state.type = type; |
| 84 m_state.maxBandwidthMbps = maxBandwidthMbps; | 83 m_state.maxBandwidthMbps = maxBandwidthMbps; |
| 85 } | 84 } |
| 86 } | 85 } |
| 87 | 86 |
| 88 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, | 87 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, |
| 89 ExecutionContext* context) { | 88 WebTaskRunner* taskRunner) { |
| 90 ASSERT(context->isContextThread()); | 89 DCHECK(taskRunner->runsTasksOnCurrentThread()); |
| 91 ASSERT(observer); | 90 DCHECK(observer); |
| 92 | 91 |
| 93 MutexLocker locker(m_mutex); | 92 MutexLocker locker(m_mutex); |
| 94 ObserverListMap::AddResult result = m_observers.insert(context, nullptr); | 93 ObserverListMap::AddResult result = m_observers.insert(taskRunner, nullptr); |
| 95 if (result.isNewEntry) | 94 if (result.isNewEntry) |
| 96 result.storedValue->value = WTF::wrapUnique(new ObserverList); | 95 result.storedValue->value = WTF::wrapUnique(new ObserverList); |
| 97 | 96 |
| 98 ASSERT(result.storedValue->value->observers.find(observer) == kNotFound); | 97 DCHECK(result.storedValue->value->observers.find(observer) == kNotFound); |
| 99 result.storedValue->value->observers.push_back(observer); | 98 result.storedValue->value->observers.push_back(observer); |
| 100 } | 99 } |
| 101 | 100 |
| 102 void NetworkStateNotifier::removeObserver(NetworkStateObserver* observer, | 101 void NetworkStateNotifier::removeObserver(NetworkStateObserver* observer, |
| 103 ExecutionContext* context) { | 102 WebTaskRunner* taskRunner) { |
| 104 ASSERT(context->isContextThread()); | 103 DCHECK(taskRunner->runsTasksOnCurrentThread()); |
| 105 ASSERT(observer); | 104 DCHECK(observer); |
| 106 | 105 |
| 107 ObserverList* observerList = lockAndFindObserverList(context); | 106 ObserverList* observerList = lockAndFindObserverList(taskRunner); |
| 108 if (!observerList) | 107 if (!observerList) |
| 109 return; | 108 return; |
| 110 | 109 |
| 111 Vector<NetworkStateObserver*>& observers = observerList->observers; | 110 Vector<NetworkStateObserver*>& observers = observerList->observers; |
| 112 size_t index = observers.find(observer); | 111 size_t index = observers.find(observer); |
| 113 if (index != kNotFound) { | 112 if (index != kNotFound) { |
| 114 observers[index] = 0; | 113 observers[index] = 0; |
| 115 observerList->zeroedObservers.push_back(index); | 114 observerList->zeroedObservers.push_back(index); |
| 116 } | 115 } |
| 117 | 116 |
| 118 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty()) | 117 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty()) |
| 119 collectZeroedObservers(observerList, context); | 118 collectZeroedObservers(observerList, taskRunner); |
| 120 } | 119 } |
| 121 | 120 |
| 122 void NetworkStateNotifier::setOverride(bool onLine, | 121 void NetworkStateNotifier::setOverride(bool onLine, |
| 123 WebConnectionType type, | 122 WebConnectionType type, |
| 124 double maxBandwidthMbps) { | 123 double maxBandwidthMbps) { |
| 125 DCHECK(isMainThread()); | 124 DCHECK(isMainThread()); |
| 126 ScopedNotifier notifier(*this); | 125 ScopedNotifier notifier(*this); |
| 127 { | 126 { |
| 128 MutexLocker locker(m_mutex); | 127 MutexLocker locker(m_mutex); |
| 129 m_hasOverride = true; | 128 m_hasOverride = true; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 140 ScopedNotifier notifier(*this); | 139 ScopedNotifier notifier(*this); |
| 141 { | 140 { |
| 142 MutexLocker locker(m_mutex); | 141 MutexLocker locker(m_mutex); |
| 143 m_hasOverride = false; | 142 m_hasOverride = false; |
| 144 } | 143 } |
| 145 } | 144 } |
| 146 | 145 |
| 147 void NetworkStateNotifier::notifyObservers(WebConnectionType type, | 146 void NetworkStateNotifier::notifyObservers(WebConnectionType type, |
| 148 double maxBandwidthMbps) { | 147 double maxBandwidthMbps) { |
| 149 DCHECK(isMainThread()); | 148 DCHECK(isMainThread()); |
| 149 MutexLocker locker(m_mutex); | |
|
jkarlin
2017/02/15 19:26:33
Phew. Thanks for this! Looks like it got deleted i
| |
| 150 for (const auto& entry : m_observers) { | 150 for (const auto& entry : m_observers) { |
| 151 ExecutionContext* context = entry.key; | 151 WebTaskRunner* taskRunner = entry.key; |
| 152 context->postTask( | 152 taskRunner->postTask( |
| 153 TaskType::Networking, BLINK_FROM_HERE, | 153 BLINK_FROM_HERE, |
| 154 createCrossThreadTask( | 154 crossThreadBind( |
| 155 &NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext, | 155 &NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext, |
| 156 crossThreadUnretained(this), type, maxBandwidthMbps)); | 156 crossThreadUnretained(this), type, maxBandwidthMbps, |
| 157 crossThreadUnretained(taskRunner))); | |
| 157 } | 158 } |
| 158 } | 159 } |
| 159 | 160 |
| 160 void NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext( | 161 void NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext( |
|
jkarlin
2017/02/15 19:26:33
Please rename this to ...OnTaskRunner
kinuko
2017/02/16 03:55:00
Done. Thanks =)
| |
| 161 WebConnectionType type, | 162 WebConnectionType type, |
| 162 double maxBandwidthMbps, | 163 double maxBandwidthMbps, |
| 163 ExecutionContext* context) { | 164 WebTaskRunner* taskRunner) { |
| 164 ObserverList* observerList = lockAndFindObserverList(context); | 165 ObserverList* observerList = lockAndFindObserverList(taskRunner); |
| 165 | 166 |
| 166 // The context could have been removed before the notification task got to | 167 // The context could have been removed before the notification task got to |
| 167 // run. | 168 // run. |
| 168 if (!observerList) | 169 if (!observerList) |
| 169 return; | 170 return; |
| 170 | 171 |
| 171 ASSERT(context->isContextThread()); | 172 DCHECK(taskRunner->runsTasksOnCurrentThread()); |
| 172 | 173 |
| 173 observerList->iterating = true; | 174 observerList->iterating = true; |
| 174 | 175 |
| 175 for (size_t i = 0; i < observerList->observers.size(); ++i) { | 176 for (size_t i = 0; i < observerList->observers.size(); ++i) { |
| 176 // Observers removed during iteration are zeroed out, skip them. | 177 // Observers removed during iteration are zeroed out, skip them. |
| 177 if (observerList->observers[i]) | 178 if (observerList->observers[i]) |
| 178 observerList->observers[i]->connectionChange(type, maxBandwidthMbps); | 179 observerList->observers[i]->connectionChange(type, maxBandwidthMbps); |
| 179 } | 180 } |
| 180 | 181 |
| 181 observerList->iterating = false; | 182 observerList->iterating = false; |
| 182 | 183 |
| 183 if (!observerList->zeroedObservers.isEmpty()) | 184 if (!observerList->zeroedObservers.isEmpty()) |
| 184 collectZeroedObservers(observerList, context); | 185 collectZeroedObservers(observerList, taskRunner); |
| 185 } | 186 } |
| 186 | 187 |
| 187 NetworkStateNotifier::ObserverList* | 188 NetworkStateNotifier::ObserverList* |
| 188 NetworkStateNotifier::lockAndFindObserverList(ExecutionContext* context) { | 189 NetworkStateNotifier::lockAndFindObserverList(WebTaskRunner* taskRunner) { |
| 189 MutexLocker locker(m_mutex); | 190 MutexLocker locker(m_mutex); |
| 190 ObserverListMap::iterator it = m_observers.find(context); | 191 ObserverListMap::iterator it = m_observers.find(taskRunner); |
| 191 return it == m_observers.end() ? nullptr : it->value.get(); | 192 return it == m_observers.end() ? nullptr : it->value.get(); |
| 192 } | 193 } |
| 193 | 194 |
| 194 void NetworkStateNotifier::collectZeroedObservers(ObserverList* list, | 195 void NetworkStateNotifier::collectZeroedObservers(ObserverList* list, |
| 195 ExecutionContext* context) { | 196 WebTaskRunner* taskRunner) { |
| 196 ASSERT(context->isContextThread()); | 197 DCHECK(taskRunner->runsTasksOnCurrentThread()); |
| 197 ASSERT(!list->iterating); | 198 DCHECK(!list->iterating); |
| 198 | 199 |
| 199 // If any observers were removed during the iteration they will have | 200 // If any observers were removed during the iteration they will have |
| 200 // 0 values, clean them up. | 201 // 0 values, clean them up. |
| 201 for (size_t i = 0; i < list->zeroedObservers.size(); ++i) | 202 for (size_t i = 0; i < list->zeroedObservers.size(); ++i) |
| 202 list->observers.remove(list->zeroedObservers[i]); | 203 list->observers.remove(list->zeroedObservers[i]); |
| 203 | 204 |
| 204 list->zeroedObservers.clear(); | 205 list->zeroedObservers.clear(); |
| 205 | 206 |
| 206 if (list->observers.isEmpty()) { | 207 if (list->observers.isEmpty()) { |
| 207 MutexLocker locker(m_mutex); | 208 MutexLocker locker(m_mutex); |
| 208 m_observers.erase(context); // deletes list | 209 m_observers.erase(taskRunner); // deletes list |
| 209 } | 210 } |
| 210 } | 211 } |
| 211 | 212 |
| 212 } // namespace blink | 213 } // namespace blink |
| OLD | NEW |