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 #ifndef NetworkStateNotifier_h | 26 #ifndef NetworkStateNotifier_h |
27 #define NetworkStateNotifier_h | 27 #define NetworkStateNotifier_h |
28 | 28 |
29 #include "public/platform/WebNetworkConnection.h" | |
29 #include "wtf/FastAllocBase.h" | 30 #include "wtf/FastAllocBase.h" |
31 #include "wtf/HashMap.h" | |
30 #include "wtf/Noncopyable.h" | 32 #include "wtf/Noncopyable.h" |
31 #include "wtf/ThreadingPrimitives.h" | 33 #include "wtf/ThreadingPrimitives.h" |
34 #include "wtf/Vector.h" | |
32 | 35 |
33 namespace WebCore { | 36 namespace WebCore { |
34 | 37 |
38 class ExecutionContext; | |
39 | |
35 class NetworkStateNotifier { | 40 class NetworkStateNotifier { |
36 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); WTF_MAKE_FAST_ALLOCATED; | 41 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); WTF_MAKE_FAST_ALLOCATED; |
37 public: | 42 public: |
43 class NetworkStateObserver { | |
44 public: | |
45 // Will be called on the thread of the context passed in addObserver. | |
46 virtual void connectionTypeChange(blink::WebNetworkConnection::Connectio nType) = 0; | |
47 }; | |
48 | |
38 NetworkStateNotifier() | 49 NetworkStateNotifier() |
39 : m_isOnLine(true) { } | 50 : m_isOnLine(true) |
51 , m_type(blink::WebNetworkConnection::Other) | |
52 { | |
53 } | |
54 | |
55 ~NetworkStateNotifier(); | |
40 | 56 |
41 bool onLine() const | 57 bool onLine() const |
42 { | 58 { |
43 MutexLocker locker(m_mutex); | 59 MutexLocker locker(m_mutex); |
44 return m_isOnLine; | 60 return m_isOnLine; |
45 } | 61 } |
46 | 62 |
47 void setOnLine(bool); | 63 void setOnLine(bool); |
48 | 64 |
65 blink::WebNetworkConnection::ConnectionType connectionType() const | |
66 { | |
67 MutexLocker locker(m_mutex); | |
68 return m_type; | |
69 } | |
70 | |
71 void setWebConnectionType(blink::WebNetworkConnection::ConnectionType); | |
72 | |
73 // Must be called on the context's thread. An added observer must be | |
74 // removed before its ExecutionContext is deleted. | |
75 void addObserver(NetworkStateObserver*, ExecutionContext*); | |
76 void removeObserver(NetworkStateObserver*, ExecutionContext*); | |
77 | |
49 private: | 78 private: |
79 struct ObserverList { | |
80 ObserverList() | |
81 : iterating(false) | |
82 { | |
83 } | |
84 bool iterating; | |
85 Vector<NetworkStateObserver*> observers; | |
86 Vector<size_t> zeroedObservers; // Indices in observers that are 0. | |
87 }; | |
88 | |
89 typedef HashMap<ExecutionContext*, ObserverList*> ObserverListMap; | |
adamk
2014/05/21 09:21:31
This should store OwnPtrs instead of raw pointers:
jkarlin
2014/05/21 14:23:17
Done.
| |
90 | |
91 void notifyObserversOnContext(ExecutionContext*, blink::WebNetworkConnection ::ConnectionType); | |
92 | |
93 ObserverList* lockAndFindObserverList(ExecutionContext*); | |
94 | |
95 // Removed observers are nulled out in the list in case the list is being | |
96 // iterated over. Once done iterating, call this to clean up nulled | |
97 // observers. | |
98 void collectZeroedObservers(ExecutionContext*); | |
99 | |
50 mutable Mutex m_mutex; | 100 mutable Mutex m_mutex; |
51 bool m_isOnLine; | 101 bool m_isOnLine; |
102 blink::WebNetworkConnection::ConnectionType m_type; | |
103 ObserverListMap m_observers; | |
52 }; | 104 }; |
53 | 105 |
54 NetworkStateNotifier& networkStateNotifier(); | 106 NetworkStateNotifier& networkStateNotifier(); |
55 | 107 |
56 }; | 108 } // namespace WebCore |
57 | 109 |
58 #endif // NetworkStateNotifier_h | 110 #endif // NetworkStateNotifier_h |
OLD | NEW |