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

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

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: Thread safe observer list 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 #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(
47 blink::WebNetworkConnection::ConnectionType) = 0;
adamk 2014/05/20 15:00:36 Nit: Don't break lines like this in Blink; there's
jkarlin 2014/05/20 16:20:23 Done.
48 };
49
50 typedef NetworkStateObserver* ObserverType;
adamk 2014/05/20 15:00:36 I found this typedef a bit confusing while reading
jkarlin 2014/05/20 16:20:23 Done.
51
38 NetworkStateNotifier() 52 NetworkStateNotifier()
39 : m_isOnLine(true) { } 53 : m_isOnLine(true)
54 , m_type(blink::WebNetworkConnection::Other)
55 {
56 }
40 57
41 bool onLine() const 58 bool onLine() const
42 { 59 {
43 MutexLocker locker(m_mutex); 60 MutexLocker locker(m_mutex);
44 return m_isOnLine; 61 return m_isOnLine;
45 } 62 }
46 63
47 void setOnLine(bool); 64 void setOnLine(bool);
48 65
66 blink::WebNetworkConnection::ConnectionType connectionType() const
67 {
68 MutexLocker locker(m_mutex);
69 return m_type;
70 }
71
72 void setWebConnectionType(blink::WebNetworkConnection::ConnectionType);
73
74 // Must be called on the context's thread. Do not call add/removeObserver
75 // while handling a connectionTypeChange notification as it mutates the
76 // container while it's being iterated over.
77 void addObserver(ObserverType, ExecutionContext*);
78 void removeObserver(ObserverType, ExecutionContext*);
79
49 private: 80 private:
81 struct ObserverList {
82 ObserverList()
83 : iterating(false)
84 {
85 }
86 bool iterating;
87 Vector<ObserverType> observers;
88 };
89
90 typedef HashMap<ExecutionContext*, ObserverList> ObserverListMap;
91
92 void notifyObserversOnContext(ExecutionContext*, blink::WebNetworkConnection ::ConnectionType);
93
50 mutable Mutex m_mutex; 94 mutable Mutex m_mutex;
51 bool m_isOnLine; 95 bool m_isOnLine;
96 blink::WebNetworkConnection::ConnectionType m_type;
97 ObserverListMap m_observers;
52 }; 98 };
53 99
54 NetworkStateNotifier& networkStateNotifier(); 100 NetworkStateNotifier& networkStateNotifier();
55 101 }
adamk 2014/05/20 15:00:36 Nit: I'd leave this blank line.
adamk 2014/05/20 15:00:36 and maybe add a "// namespace WebCore" comment
jkarlin 2014/05/20 16:20:23 Done.
56 };
57 102
58 #endif // NetworkStateNotifier_h 103 #endif // NetworkStateNotifier_h
OLDNEW
« no previous file with comments | « no previous file | Source/core/page/NetworkStateNotifier.cpp » ('j') | Source/core/page/NetworkStateNotifier.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698