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 "config.h" | 26 #include "config.h" |
27 #include "core/page/NetworkStateNotifier.h" | 27 #include "core/page/NetworkStateNotifier.h" |
28 | 28 |
| 29 #include "core/dom/ExecutionContext.h" |
29 #include "core/page/Page.h" | 30 #include "core/page/Page.h" |
30 #include "wtf/Assertions.h" | 31 #include "wtf/Assertions.h" |
| 32 #include "wtf/Functional.h" |
31 #include "wtf/MainThread.h" | 33 #include "wtf/MainThread.h" |
32 #include "wtf/StdLibExtras.h" | 34 #include "wtf/StdLibExtras.h" |
33 #include "wtf/Threading.h" | 35 #include "wtf/Threading.h" |
34 | 36 |
35 namespace WebCore { | 37 namespace WebCore { |
36 | 38 |
| 39 NetworkStateNotifier::~NetworkStateNotifier() |
| 40 { |
| 41 } |
| 42 |
37 NetworkStateNotifier& networkStateNotifier() | 43 NetworkStateNotifier& networkStateNotifier() |
38 { | 44 { |
39 AtomicallyInitializedStatic(NetworkStateNotifier*, networkStateNotifier = ne
w NetworkStateNotifier); | 45 AtomicallyInitializedStatic(NetworkStateNotifier*, networkStateNotifier = ne
w NetworkStateNotifier); |
40 return *networkStateNotifier; | 46 return *networkStateNotifier; |
41 } | 47 } |
42 | 48 |
43 void NetworkStateNotifier::setOnLine(bool onLine) | 49 void NetworkStateNotifier::setOnLine(bool onLine) |
44 { | 50 { |
45 ASSERT(isMainThread()); | 51 ASSERT(isMainThread()); |
46 | 52 |
47 { | 53 { |
48 MutexLocker locker(m_mutex); | 54 MutexLocker locker(m_mutex); |
49 if (m_isOnLine == onLine) | 55 if (m_isOnLine == onLine) |
50 return; | 56 return; |
51 | 57 |
52 m_isOnLine = onLine; | 58 m_isOnLine = onLine; |
53 } | 59 } |
54 | 60 |
55 Page::networkStateChanged(onLine); | 61 Page::networkStateChanged(onLine); |
56 } | 62 } |
57 | 63 |
| 64 void NetworkStateNotifier::setWebConnectionType(blink::WebNetworkConnection::Con
nectionType type) |
| 65 { |
| 66 ASSERT(isMainThread()); |
| 67 |
| 68 MutexLocker locker(m_mutex); |
| 69 if (m_type == type) |
| 70 return; |
| 71 m_type = type; |
| 72 |
| 73 for (ObserverListMap::iterator it = m_observers.begin(); it != m_observers.e
nd(); ++it) { |
| 74 ExecutionContext* context = it->key; |
| 75 context->postTask(bind(&NetworkStateNotifier::notifyObserversOnContext,
this, context, type)); |
| 76 } |
58 } | 77 } |
| 78 |
| 79 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, Execution
Context* context) |
| 80 { |
| 81 ASSERT(context->isContextThread()); |
| 82 ASSERT(observer); |
| 83 |
| 84 MutexLocker locker(m_mutex); |
| 85 ObserverListMap::AddResult result = m_observers.add(context, nullptr); |
| 86 if (result.isNewEntry) |
| 87 result.storedValue->value = adoptPtr(new ObserverList); |
| 88 |
| 89 ASSERT(result.storedValue->value->observers.find(observer) == kNotFound); |
| 90 result.storedValue->value->observers.append(observer); |
| 91 } |
| 92 |
| 93 void NetworkStateNotifier::removeObserver(NetworkStateObserver* observer, Execut
ionContext* context) |
| 94 { |
| 95 ASSERT(context->isContextThread()); |
| 96 ASSERT(observer); |
| 97 |
| 98 ObserverList* observerList = lockAndFindObserverList(context); |
| 99 if (!observerList) |
| 100 return; |
| 101 |
| 102 Vector<NetworkStateObserver*>& observers = observerList->observers; |
| 103 size_t index = observers.find(observer); |
| 104 if (index != kNotFound) { |
| 105 observers[index] = 0; |
| 106 observerList->zeroedObservers.append(index); |
| 107 } |
| 108 |
| 109 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty()) |
| 110 collectZeroedObservers(observerList, context); |
| 111 } |
| 112 |
| 113 void NetworkStateNotifier::notifyObserversOnContext(ExecutionContext* context, b
link::WebNetworkConnection::ConnectionType type) |
| 114 { |
| 115 ObserverList* observerList = lockAndFindObserverList(context); |
| 116 |
| 117 // The context could have been removed before the notification task got to r
un. |
| 118 if (!observerList) |
| 119 return; |
| 120 |
| 121 ASSERT(context->isContextThread()); |
| 122 |
| 123 observerList->iterating = true; |
| 124 |
| 125 for (size_t i = 0; i < observerList->observers.size(); ++i) { |
| 126 // Observers removed during iteration are zeroed out, skip them. |
| 127 if (observerList->observers[i]) |
| 128 observerList->observers[i]->connectionTypeChange(type); |
| 129 } |
| 130 |
| 131 observerList->iterating = false; |
| 132 |
| 133 if (!observerList->zeroedObservers.isEmpty()) |
| 134 collectZeroedObservers(observerList, context); |
| 135 } |
| 136 |
| 137 NetworkStateNotifier::ObserverList* NetworkStateNotifier::lockAndFindObserverLis
t(ExecutionContext* context) |
| 138 { |
| 139 MutexLocker locker(m_mutex); |
| 140 ObserverListMap::iterator it = m_observers.find(context); |
| 141 return it == m_observers.end() ? 0 : it->value.get(); |
| 142 } |
| 143 |
| 144 void NetworkStateNotifier::collectZeroedObservers(ObserverList* list, ExecutionC
ontext* context) |
| 145 { |
| 146 ASSERT(context->isContextThread()); |
| 147 ASSERT(!list->iterating); |
| 148 |
| 149 // If any observers were removed during the iteration they will have |
| 150 // 0 values, clean them up. |
| 151 for (size_t i = 0; i < list->zeroedObservers.size(); ++i) |
| 152 list->observers.remove(list->zeroedObservers[i]); |
| 153 |
| 154 list->zeroedObservers.clear(); |
| 155 |
| 156 if (list->observers.isEmpty()) { |
| 157 MutexLocker locker(m_mutex); |
| 158 m_observers.remove(context); // deletes list |
| 159 } |
| 160 } |
| 161 |
| 162 } // namespace WebCore |
OLD | NEW |