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/WebConnectionType.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::WebConnectionType) = 0; |
| 47 }; |
| 48 |
38 NetworkStateNotifier() | 49 NetworkStateNotifier() |
39 : m_isOnLine(true) { } | 50 : m_isOnLine(true) |
| 51 , m_type(blink::ConnectionTypeOther) |
| 52 { |
| 53 } |
40 | 54 |
41 bool onLine() const | 55 bool onLine() const |
42 { | 56 { |
43 MutexLocker locker(m_mutex); | 57 MutexLocker locker(m_mutex); |
44 return m_isOnLine; | 58 return m_isOnLine; |
45 } | 59 } |
46 | 60 |
47 void setOnLine(bool); | 61 void setOnLine(bool); |
48 | 62 |
| 63 blink::WebConnectionType connectionType() const |
| 64 { |
| 65 MutexLocker locker(m_mutex); |
| 66 return m_type; |
| 67 } |
| 68 |
| 69 void setWebConnectionType(blink::WebConnectionType); |
| 70 |
| 71 // Must be called on the context's thread. An added observer must be |
| 72 // removed before its ExecutionContext is deleted. |
| 73 void addObserver(NetworkStateObserver*, ExecutionContext*); |
| 74 void removeObserver(NetworkStateObserver*, ExecutionContext*); |
| 75 |
49 private: | 76 private: |
| 77 struct ObserverList { |
| 78 ObserverList() |
| 79 : iterating(false) |
| 80 { |
| 81 } |
| 82 bool iterating; |
| 83 Vector<NetworkStateObserver*> observers; |
| 84 Vector<size_t> zeroedObservers; // Indices in observers that are 0. |
| 85 }; |
| 86 |
| 87 typedef HashMap<ExecutionContext*, OwnPtr<ObserverList> > ObserverListMap; |
| 88 |
| 89 void notifyObserversOnContext(ExecutionContext*, blink::WebConnectionType); |
| 90 |
| 91 ObserverList* lockAndFindObserverList(ExecutionContext*); |
| 92 |
| 93 // Removed observers are nulled out in the list in case the list is being |
| 94 // iterated over. Once done iterating, call this to clean up nulled |
| 95 // observers. |
| 96 void collectZeroedObservers(ObserverList*, ExecutionContext*); |
| 97 |
50 mutable Mutex m_mutex; | 98 mutable Mutex m_mutex; |
51 bool m_isOnLine; | 99 bool m_isOnLine; |
| 100 blink::WebConnectionType m_type; |
| 101 ObserverListMap m_observers; |
52 }; | 102 }; |
53 | 103 |
54 NetworkStateNotifier& networkStateNotifier(); | 104 NetworkStateNotifier& networkStateNotifier(); |
55 | 105 |
56 }; | 106 } // namespace WebCore |
57 | 107 |
58 #endif // NetworkStateNotifier_h | 108 #endif // NetworkStateNotifier_h |
OLD | NEW |