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