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 void NetworkStateNotifier::setOnLine(bool onLine) | 45 void NetworkStateNotifier::setOnLine(bool onLineValue) |
46 { | 46 { |
47 ASSERT(isMainThread()); | 47 DCHECK(isMainThread()); |
48 | 48 |
49 { | 49 { |
50 MutexLocker locker(m_mutex); | 50 MutexLocker locker(m_mutex); |
51 if (m_isOnLine == onLine) | 51 if (m_isOnLine == onLineValue) |
52 return; | 52 return; |
53 | 53 |
54 m_isOnLine = onLine; | 54 m_isOnLine = onLineValue; |
55 | |
56 if (m_hasOverride) | |
57 return; | |
55 } | 58 } |
56 | 59 |
57 Page::networkStateChanged(onLine); | 60 Page::networkStateChanged(onLine()); |
58 } | 61 } |
59 | 62 |
60 void NetworkStateNotifier::setWebConnection(WebConnectionType type, double maxBa ndwidthMbps) | 63 void NetworkStateNotifier::setWebConnection(WebConnectionType type, double maxBa ndwidthMbps) |
61 { | 64 { |
62 ASSERT(isMainThread()); | 65 DCHECK(isMainThread()); |
63 if (m_testUpdatesOnly) | |
64 return; | |
65 | 66 |
66 setWebConnectionImpl(type, maxBandwidthMbps); | 67 { |
68 MutexLocker locker(m_mutex); | |
69 m_initialized = true; | |
70 | |
71 if (m_type == type && m_maxBandwidthMbps == maxBandwidthMbps) | |
72 return; | |
73 m_type = type; | |
74 m_maxBandwidthMbps = maxBandwidthMbps; | |
75 | |
76 if (m_hasOverride) | |
77 return; | |
78 } | |
79 | |
80 notifyObservers(); | |
67 } | 81 } |
68 | 82 |
69 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, Execution Context* context) | 83 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, Execution Context* context) |
70 { | 84 { |
71 ASSERT(context->isContextThread()); | 85 ASSERT(context->isContextThread()); |
72 ASSERT(observer); | 86 ASSERT(observer); |
73 | 87 |
74 MutexLocker locker(m_mutex); | 88 MutexLocker locker(m_mutex); |
75 ObserverListMap::AddResult result = m_observers.add(context, nullptr); | 89 ObserverListMap::AddResult result = m_observers.add(context, nullptr); |
76 if (result.isNewEntry) | 90 if (result.isNewEntry) |
(...skipping 16 matching lines...) Expand all Loading... | |
93 size_t index = observers.find(observer); | 107 size_t index = observers.find(observer); |
94 if (index != kNotFound) { | 108 if (index != kNotFound) { |
95 observers[index] = 0; | 109 observers[index] = 0; |
96 observerList->zeroedObservers.append(index); | 110 observerList->zeroedObservers.append(index); |
97 } | 111 } |
98 | 112 |
99 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty()) | 113 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty()) |
100 collectZeroedObservers(observerList, context); | 114 collectZeroedObservers(observerList, context); |
101 } | 115 } |
102 | 116 |
103 void NetworkStateNotifier::setTestUpdatesOnly(bool updatesOnly) | 117 void NetworkStateNotifier::setOverride(bool onLineValue, WebConnectionType type, double maxBandwidthMbps) |
118 { | |
119 DCHECK(isMainThread()); | |
120 | |
121 { | |
122 MutexLocker locker(m_mutex); | |
123 m_initialized = true; | |
124 m_hasOverride = true; | |
125 m_overrideOnLine = onLineValue; | |
126 m_overrideType = type; | |
127 m_overrideMaxBandwidthMbps = maxBandwidthMbps; | |
128 } | |
129 | |
130 notifyObservers(); | |
131 Page::networkStateChanged(onLine()); | |
132 } | |
133 | |
134 void NetworkStateNotifier::clearOverride() | |
135 { | |
136 DCHECK(isMainThread()); | |
137 | |
138 { | |
139 MutexLocker locker(m_mutex); | |
140 DCHECK(m_initialized); | |
141 m_hasOverride = false; | |
142 } | |
143 | |
144 notifyObservers(); | |
145 Page::networkStateChanged(onLine()); | |
146 } | |
147 | |
148 void NetworkStateNotifier::notifyObservers() | |
104 { | 149 { |
105 ASSERT(isMainThread()); | 150 ASSERT(isMainThread()); |
106 MutexLocker locker(m_mutex); | 151 WebConnectionType type = connectionType(); |
107 | 152 double maxBandwidthMbps = maxBandwidth(); |
jkarlin
2016/06/23 12:53:46
The underlying values of type and bandwidth might
dgozman
2016/06/23 15:00:22
How is that possible? Both notifyObservers and all
jkarlin
2016/06/24 14:47:50
Oh right, good point about this all running on the
| |
108 // Reset state to default when entering or leaving test mode. | |
109 if (updatesOnly != m_testUpdatesOnly) { | |
110 m_isOnLine = true; | |
111 m_type = WebConnectionTypeOther; | |
112 m_maxBandwidthMbps = std::numeric_limits<double>::infinity(); | |
113 } | |
114 | |
115 m_testUpdatesOnly = updatesOnly; | |
116 } | |
117 | |
118 void NetworkStateNotifier::setWebConnectionForTest(WebConnectionType type, doubl e maxBandwidthMbps) | |
119 { | |
120 ASSERT(isMainThread()); | |
121 ASSERT(m_testUpdatesOnly); | |
122 setWebConnectionImpl(type, maxBandwidthMbps); | |
123 } | |
124 | |
125 void NetworkStateNotifier::setWebConnectionImpl(WebConnectionType type, double m axBandwidthMbps) | |
126 { | |
127 ASSERT(isMainThread()); | |
128 | 153 |
129 MutexLocker locker(m_mutex); | 154 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) { | 155 for (const auto& entry : m_observers) { |
138 ExecutionContext* context = entry.key; | 156 ExecutionContext* context = entry.key; |
139 context->postTask(BLINK_FROM_HERE, createCrossThreadTask(&NetworkStateNo tifier::notifyObserversOfConnectionChangeOnContext, AllowCrossThreadAccess(this) , type, maxBandwidthMbps)); | 157 context->postTask(BLINK_FROM_HERE, createCrossThreadTask(&NetworkStateNo tifier::notifyObserversOfConnectionChangeOnContext, AllowCrossThreadAccess(this) , type, maxBandwidthMbps)); |
140 } | 158 } |
141 } | 159 } |
142 | 160 |
143 void NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext(WebConnect ionType type, double maxBandwidthMbps, ExecutionContext* context) | 161 void NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext(WebConnect ionType type, double maxBandwidthMbps, ExecutionContext* context) |
144 { | 162 { |
145 ObserverList* observerList = lockAndFindObserverList(context); | 163 ObserverList* observerList = lockAndFindObserverList(context); |
146 | 164 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 | 201 |
184 list->zeroedObservers.clear(); | 202 list->zeroedObservers.clear(); |
185 | 203 |
186 if (list->observers.isEmpty()) { | 204 if (list->observers.isEmpty()) { |
187 MutexLocker locker(m_mutex); | 205 MutexLocker locker(m_mutex); |
188 m_observers.remove(context); // deletes list | 206 m_observers.remove(context); // deletes list |
189 } | 207 } |
190 } | 208 } |
191 | 209 |
192 } // namespace blink | 210 } // namespace blink |
OLD | NEW |