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