| 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 24 matching lines...) Expand all Loading... |
| 35 #include "wtf/Threading.h" | 35 #include "wtf/Threading.h" |
| 36 | 36 |
| 37 namespace blink { | 37 namespace blink { |
| 38 | 38 |
| 39 NetworkStateNotifier& networkStateNotifier() | 39 NetworkStateNotifier& networkStateNotifier() |
| 40 { | 40 { |
| 41 DEFINE_THREAD_SAFE_STATIC_LOCAL(NetworkStateNotifier, networkStateNotifier,
new NetworkStateNotifier); | 41 DEFINE_THREAD_SAFE_STATIC_LOCAL(NetworkStateNotifier, networkStateNotifier,
new NetworkStateNotifier); |
| 42 return networkStateNotifier; | 42 return networkStateNotifier; |
| 43 } | 43 } |
| 44 | 44 |
| 45 NetworkStateNotifier::ScopedNotifier::ScopedNotifier(NetworkStateNotifier& notif
ier) |
| 46 : m_notifier(notifier) |
| 47 { |
| 48 DCHECK(isMainThread()); |
| 49 m_before = m_notifier.m_hasOverride ? m_notifier.m_override : m_notifier.m_s
tate; |
| 50 } |
| 51 |
| 52 NetworkStateNotifier::ScopedNotifier::~ScopedNotifier() |
| 53 { |
| 54 DCHECK(isMainThread()); |
| 55 const NetworkState& after = m_notifier.m_hasOverride ? m_notifier.m_override
: m_notifier.m_state; |
| 56 if ((after.type != m_before.type || after.maxBandwidthMbps != m_before.maxBa
ndwidthMbps) && m_before.connectionInitialized) |
| 57 m_notifier.notifyObservers(after.type, after.maxBandwidthMbps); |
| 58 if (after.onLine != m_before.onLine && m_before.onLineInitialized) |
| 59 Page::networkStateChanged(after.onLine); |
| 60 } |
| 61 |
| 45 void NetworkStateNotifier::setOnLine(bool onLine) | 62 void NetworkStateNotifier::setOnLine(bool onLine) |
| 46 { | 63 { |
| 47 ASSERT(isMainThread()); | 64 DCHECK(isMainThread()); |
| 48 | 65 ScopedNotifier notifier(*this); |
| 49 { | 66 { |
| 50 MutexLocker locker(m_mutex); | 67 MutexLocker locker(m_mutex); |
| 51 if (m_isOnLine == onLine) | 68 m_state.onLineInitialized = true; |
| 52 return; | 69 m_state.onLine = onLine; |
| 53 | |
| 54 m_isOnLine = onLine; | |
| 55 } | 70 } |
| 56 | |
| 57 Page::networkStateChanged(onLine); | |
| 58 } | 71 } |
| 59 | 72 |
| 60 void NetworkStateNotifier::setWebConnection(WebConnectionType type, double maxBa
ndwidthMbps) | 73 void NetworkStateNotifier::setWebConnection(WebConnectionType type, double maxBa
ndwidthMbps) |
| 61 { | 74 { |
| 62 ASSERT(isMainThread()); | 75 DCHECK(isMainThread()); |
| 63 if (m_testUpdatesOnly) | 76 ScopedNotifier notifier(*this); |
| 64 return; | 77 { |
| 65 | 78 MutexLocker locker(m_mutex); |
| 66 setWebConnectionImpl(type, maxBandwidthMbps); | 79 m_state.connectionInitialized = true; |
| 80 m_state.type = type; |
| 81 m_state.maxBandwidthMbps = maxBandwidthMbps; |
| 82 } |
| 67 } | 83 } |
| 68 | 84 |
| 69 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, Execution
Context* context) | 85 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, Execution
Context* context) |
| 70 { | 86 { |
| 71 ASSERT(context->isContextThread()); | 87 ASSERT(context->isContextThread()); |
| 72 ASSERT(observer); | 88 ASSERT(observer); |
| 73 | 89 |
| 74 MutexLocker locker(m_mutex); | 90 MutexLocker locker(m_mutex); |
| 75 ObserverListMap::AddResult result = m_observers.add(context, nullptr); | 91 ObserverListMap::AddResult result = m_observers.add(context, nullptr); |
| 76 if (result.isNewEntry) | 92 if (result.isNewEntry) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 93 size_t index = observers.find(observer); | 109 size_t index = observers.find(observer); |
| 94 if (index != kNotFound) { | 110 if (index != kNotFound) { |
| 95 observers[index] = 0; | 111 observers[index] = 0; |
| 96 observerList->zeroedObservers.append(index); | 112 observerList->zeroedObservers.append(index); |
| 97 } | 113 } |
| 98 | 114 |
| 99 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty()) | 115 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty()) |
| 100 collectZeroedObservers(observerList, context); | 116 collectZeroedObservers(observerList, context); |
| 101 } | 117 } |
| 102 | 118 |
| 103 void NetworkStateNotifier::setTestUpdatesOnly(bool updatesOnly) | 119 void NetworkStateNotifier::setOverride(bool onLine, WebConnectionType type, doub
le maxBandwidthMbps) |
| 104 { | 120 { |
| 105 ASSERT(isMainThread()); | 121 DCHECK(isMainThread()); |
| 106 MutexLocker locker(m_mutex); | 122 ScopedNotifier notifier(*this); |
| 107 | 123 { |
| 108 // Reset state to default when entering or leaving test mode. | 124 MutexLocker locker(m_mutex); |
| 109 if (updatesOnly != m_testUpdatesOnly) { | 125 m_hasOverride = true; |
| 110 m_isOnLine = true; | 126 m_override.onLineInitialized = true; |
| 111 m_type = WebConnectionTypeOther; | 127 m_override.onLine = onLine; |
| 112 m_maxBandwidthMbps = std::numeric_limits<double>::infinity(); | 128 m_override.connectionInitialized = true; |
| 129 m_override.type = type; |
| 130 m_override.maxBandwidthMbps = maxBandwidthMbps; |
| 113 } | 131 } |
| 114 | |
| 115 m_testUpdatesOnly = updatesOnly; | |
| 116 } | 132 } |
| 117 | 133 |
| 118 void NetworkStateNotifier::setWebConnectionForTest(WebConnectionType type, doubl
e maxBandwidthMbps) | 134 void NetworkStateNotifier::clearOverride() |
| 119 { | 135 { |
| 120 ASSERT(isMainThread()); | 136 DCHECK(isMainThread()); |
| 121 ASSERT(m_testUpdatesOnly); | 137 ScopedNotifier notifier(*this); |
| 122 setWebConnectionImpl(type, maxBandwidthMbps); | 138 { |
| 139 MutexLocker locker(m_mutex); |
| 140 m_hasOverride = false; |
| 141 } |
| 123 } | 142 } |
| 124 | 143 |
| 125 void NetworkStateNotifier::setWebConnectionImpl(WebConnectionType type, double m
axBandwidthMbps) | 144 void NetworkStateNotifier::notifyObservers(WebConnectionType type, double maxBan
dwidthMbps) |
| 126 { | 145 { |
| 127 ASSERT(isMainThread()); | 146 DCHECK(isMainThread()); |
| 128 | |
| 129 MutexLocker locker(m_mutex); | |
| 130 m_initialized = true; | |
| 131 | |
| 132 if (m_type == type && m_maxBandwidthMbps == maxBandwidthMbps) | |
| 133 return; | |
| 134 m_type = type; | |
| 135 m_maxBandwidthMbps = maxBandwidthMbps; | |
| 136 | |
| 137 for (const auto& entry : m_observers) { | 147 for (const auto& entry : m_observers) { |
| 138 ExecutionContext* context = entry.key; | 148 ExecutionContext* context = entry.key; |
| 139 context->postTask(BLINK_FROM_HERE, createCrossThreadTask(&NetworkStateNo
tifier::notifyObserversOfConnectionChangeOnContext, crossThreadUnretained(this),
type, maxBandwidthMbps)); | 149 context->postTask(BLINK_FROM_HERE, createCrossThreadTask(&NetworkStateNo
tifier::notifyObserversOfConnectionChangeOnContext, crossThreadUnretained(this),
type, maxBandwidthMbps)); |
| 140 } | 150 } |
| 141 } | 151 } |
| 142 | 152 |
| 143 void NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext(WebConnect
ionType type, double maxBandwidthMbps, ExecutionContext* context) | 153 void NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext(WebConnect
ionType type, double maxBandwidthMbps, ExecutionContext* context) |
| 144 { | 154 { |
| 145 ObserverList* observerList = lockAndFindObserverList(context); | 155 ObserverList* observerList = lockAndFindObserverList(context); |
| 146 | 156 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 | 193 |
| 184 list->zeroedObservers.clear(); | 194 list->zeroedObservers.clear(); |
| 185 | 195 |
| 186 if (list->observers.isEmpty()) { | 196 if (list->observers.isEmpty()) { |
| 187 MutexLocker locker(m_mutex); | 197 MutexLocker locker(m_mutex); |
| 188 m_observers.remove(context); // deletes list | 198 m_observers.remove(context); // deletes list |
| 189 } | 199 } |
| 190 } | 200 } |
| 191 | 201 |
| 192 } // namespace blink | 202 } // namespace blink |
| OLD | NEW |