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 virtual ~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 |
| 78 protected: |
| 79 virtual void notifyObserversOnContext(ExecutionContext*, blink::WebNetworkCo
nnection::ConnectionType); |
| 80 |
49 private: | 81 private: |
| 82 friend class TestNetworkStateNotifier; |
| 83 struct ObserverList { |
| 84 ObserverList() |
| 85 : iterating(false) |
| 86 { |
| 87 } |
| 88 bool iterating; |
| 89 Vector<NetworkStateObserver*> observers; |
| 90 Vector<size_t> zeroedObservers; // Indices in observers that are 0. |
| 91 }; |
| 92 |
| 93 typedef HashMap<ExecutionContext*, OwnPtr<ObserverList> > ObserverListMap; |
| 94 |
| 95 ObserverList* lockAndFindObserverList(ExecutionContext*); |
| 96 |
| 97 // Removed observers are nulled out in the list in case the list is being |
| 98 // iterated over. Once done iterating, call this to clean up nulled |
| 99 // observers. |
| 100 void collectZeroedObservers(ObserverList*, ExecutionContext*); |
| 101 |
50 mutable Mutex m_mutex; | 102 mutable Mutex m_mutex; |
51 bool m_isOnLine; | 103 bool m_isOnLine; |
| 104 blink::WebNetworkConnection::ConnectionType m_type; |
| 105 ObserverListMap m_observers; |
52 }; | 106 }; |
53 | 107 |
54 NetworkStateNotifier& networkStateNotifier(); | 108 NetworkStateNotifier& networkStateNotifier(); |
55 | 109 |
56 }; | 110 } // namespace WebCore |
57 | 111 |
58 #endif // NetworkStateNotifier_h | 112 #endif // NetworkStateNotifier_h |
OLD | NEW |