Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(545)

Side by Side Diff: Source/core/page/NetworkStateNotifier.cpp

Issue 289333003: Adds type information to the NetworkStateNotifier. Also allows for registration of observers. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Now it's safe to add/remove observers during notification. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 deleteAllValues(m_observers);
adamk 2014/05/21 09:21:31 This won't be needed if you use OwnPtrs as values.
jkarlin 2014/05/21 14:23:17 Done.
42 }
43
37 NetworkStateNotifier& networkStateNotifier() 44 NetworkStateNotifier& networkStateNotifier()
38 { 45 {
39 AtomicallyInitializedStatic(NetworkStateNotifier*, networkStateNotifier = ne w NetworkStateNotifier); 46 AtomicallyInitializedStatic(NetworkStateNotifier*, networkStateNotifier = ne w NetworkStateNotifier);
40 return *networkStateNotifier; 47 return *networkStateNotifier;
41 } 48 }
42 49
43 void NetworkStateNotifier::setOnLine(bool onLine) 50 void NetworkStateNotifier::setOnLine(bool onLine)
44 { 51 {
45 ASSERT(isMainThread()); 52 ASSERT(isMainThread());
46 53
47 { 54 {
48 MutexLocker locker(m_mutex); 55 MutexLocker locker(m_mutex);
49 if (m_isOnLine == onLine) 56 if (m_isOnLine == onLine)
50 return; 57 return;
51 58
52 m_isOnLine = onLine; 59 m_isOnLine = onLine;
53 } 60 }
54 61
55 Page::networkStateChanged(onLine); 62 Page::networkStateChanged(onLine);
56 } 63 }
57 64
65 void NetworkStateNotifier::setWebConnectionType(blink::WebNetworkConnection::Con nectionType type)
66 {
67 ASSERT(isMainThread());
68
69 MutexLocker locker(m_mutex);
70 if (m_type == type)
71 return;
72 m_type = type;
73
74 for (ObserverListMap::iterator it = m_observers.begin(); it != m_observers.e nd(); ++it) {
75 ExecutionContext* context = it->key;
76 context->postTask(bind(&NetworkStateNotifier::notifyObserversOnContext, this, context, type));
77 }
58 } 78 }
79
80 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, Execution Context* context)
81 {
82 ASSERT(context->isContextThread());
83 ASSERT(observer);
84
85 MutexLocker locker(m_mutex);
86 ObserverListMap::iterator it = m_observers.find(context);
87 if (it == m_observers.end()) {
88 m_observers.set(context, new ObserverList());
89 it = m_observers.find(context);
adamk 2014/05/21 09:21:31 The idiomatic way to handle this song and dance in
jkarlin 2014/05/21 14:23:17 Done.
90 }
91
92 it->value->observers.append(observer);
adamk 2014/05/21 09:21:31 Maybe add an ASSERT that the observer isn't alread
jkarlin 2014/05/21 14:23:17 Done.
93 }
94
95 void NetworkStateNotifier::removeObserver(NetworkStateObserver* observer, Execut ionContext* context)
96 {
97 ASSERT(context->isContextThread());
98 ASSERT(observer);
99
100 ObserverList* observerList = lockAndFindObserverList(context);
101 if (!observerList)
102 return;
103
104 Vector<NetworkStateObserver*>& observers = observerList->observers;
105 size_t index = observers.find(observer);
106 if (index != kNotFound) {
107 observers[index] = 0;
108 observerList->zeroedObservers.append(index);
109 }
110
111 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty())
112 collectZeroedObservers(context);
adamk 2014/05/21 09:21:31 All this zeroed observers logic is pretty tricky.
jkarlin 2014/05/21 14:23:17 Done.
113 }
114
115 void NetworkStateNotifier::notifyObserversOnContext(ExecutionContext* context, b link::WebNetworkConnection::ConnectionType type)
116 {
117 ASSERT(context->isContextThread());
118
119 ObserverList* observerList = lockAndFindObserverList(context);
120
121 // The context could have been removed before the notification task got to r un.
adamk 2014/05/21 09:21:31 If this happens then the ASSERT above could crash.
jkarlin 2014/05/21 14:23:17 Done.
122 if (!observerList)
123 return;
124
125 observerList->iterating = true;
126
127 for (size_t i = 0; i < observerList->observers.size(); ++i) {
128 // Observers removed during iteration are zeroed out, skip them.
129 if (observerList->observers[i])
130 observerList->observers[i]->connectionTypeChange(type);
131 }
132
133 observerList->iterating = false;
134
135 if (!observerList->zeroedObservers.isEmpty())
136 collectZeroedObservers(context);
137 }
138
139 NetworkStateNotifier::ObserverList* NetworkStateNotifier::lockAndFindObserverLis t(ExecutionContext* context)
140 {
141 MutexLocker locker(m_mutex);
142 ObserverListMap::iterator it = m_observers.find(context);
143 return it == m_observers.end() ? 0 : it->value;
144 }
145
146 void NetworkStateNotifier::collectZeroedObservers(ExecutionContext* context)
adamk 2014/05/21 09:21:31 Please add a context thread assertion here.
jkarlin 2014/05/21 14:23:17 Done.
147 {
148 // If any observers were removed during the iteration they will have
149 // 0 values, clean them up.
150 ObserverList* list = lockAndFindObserverList(context);
adamk 2014/05/21 09:21:31 Maybe the callers should just pass in the list?
jkarlin 2014/05/21 14:23:17 Done.
151 ASSERT(list);
adamk 2014/05/21 09:21:31 I think you also want to ASSERT(!list->iterating)
jkarlin 2014/05/21 14:23:17 Done.
152
153 for (size_t i = 0; i < list->zeroedObservers.size(); ++i)
154 list->observers.remove(list->zeroedObservers[i]);
155
156 list->zeroedObservers.clear();
157
158 {
159 MutexLocker locker(m_mutex);
160 if (list->observers.isEmpty()) {
161 m_observers.remove(context);
162 delete list;
adamk 2014/05/21 09:21:31 This delete can also go away if you use OwnPtrs as
jkarlin 2014/05/21 14:23:17 Done.
163 }
164 }
165 }
166
167 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698