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

Side by Side Diff: Source/modules/netinfo/NetworkInformation.cpp

Issue 291203002: Adds NetInfo v3 Web API to Blink. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@netinfo1
Patch Set: Comment typo Created 6 years, 6 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
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "modules/netinfo/NetworkInformation.h"
7
8 #include "core/dom/ExecutionContext.h"
9 #include "core/page/NetworkStateNotifier.h"
10 #include "modules/EventTargetModules.h"
11 #include "wtf/text/WTFString.h"
12
13 namespace {
14
15 String connectionTypeToString(blink::WebConnectionType type)
16 {
17 switch (type) {
18 case blink::ConnectionTypeCellular:
19 return "cellular";
20 case blink::ConnectionTypeBluetooth:
21 return "bluetooth";
22 case blink::ConnectionTypeEthernet:
23 return "ethernet";
24 case blink::ConnectionTypeWifi:
25 return "wifi";
26 case blink::ConnectionTypeOther:
27 return "other";
28 case blink::ConnectionTypeNone:
29 return "none";
30 }
31 ASSERT_NOT_REACHED();
32 return "none";
33 }
34
35 } // namespace
36
37 namespace WebCore {
38
39 PassRefPtrWillBeRawPtr<NetworkInformation> NetworkInformation::create(ExecutionC ontext* context)
40 {
41 RefPtrWillBeRawPtr<NetworkInformation> connection(adoptRefWillBeRefCountedGa rbageCollected(new NetworkInformation(context)));
42 connection->suspendIfNeeded();
43 return connection.release();
44 }
45
46 NetworkInformation::~NetworkInformation()
47 {
48 ASSERT(!m_observing);
49 }
50
51 String NetworkInformation::type() const
52 {
53 return connectionTypeToString(m_type);
54 }
55
56 void NetworkInformation::connectionTypeChange(blink::WebConnectionType type)
57 {
58 ASSERT(executionContext()->isContextThread());
59
60 // This can happen if the observer removes and then adds itself again
61 // during notification.
62 if (m_type == type)
63 return;
64
65 m_type = type;
66 dispatchEvent(Event::create(EventTypeNames::typechange));
67 }
68
69 const AtomicString& NetworkInformation::interfaceName() const
70 {
71 return EventTargetNames::NetworkInformation;
72 }
73
74 ExecutionContext* NetworkInformation::executionContext() const
75 {
76 return ActiveDOMObject::executionContext();
77 }
78
79 bool NetworkInformation::addEventListener(const AtomicString& eventType, PassRef Ptr<EventListener> listener, bool useCapture)
80 {
81 if (!EventTargetWithInlineData::addEventListener(eventType, listener, useCap ture))
82 return false;
83 startObserving();
84 return true;
85 }
86
87 bool NetworkInformation::removeEventListener(const AtomicString& eventType, Even tListener* listener, bool useCapture)
88 {
89 if (!EventTargetWithInlineData::removeEventListener(eventType, listener, use Capture))
90 return false;
91 if (!hasEventListeners())
92 stopObserving();
93 return true;
94 }
95
96 void NetworkInformation::removeAllEventListeners()
97 {
98 EventTargetWithInlineData::removeAllEventListeners();
99 ASSERT(!hasEventListeners());
100 stopObserving();
101 }
102
103 bool NetworkInformation::hasPendingActivity() const
104 {
105 ASSERT(m_contextStopped || m_observing == hasEventListeners());
106
107 // Prevent collection of this object when there are active listeners.
108 return m_observing;
109 }
110
111 void NetworkInformation::stop()
112 {
113 m_contextStopped = true;
114 stopObserving();
115 }
116
117 void NetworkInformation::startObserving()
118 {
119 if (!m_observing && !m_contextStopped) {
120 networkStateNotifier().addObserver(this, executionContext());
121 m_observing = true;
122 }
123 }
124
125 void NetworkInformation::stopObserving()
126 {
127 if (m_observing) {
128 networkStateNotifier().removeObserver(this, executionContext());
129 m_observing = false;
130 }
131 }
132
133 NetworkInformation::NetworkInformation(ExecutionContext* context)
134 : ActiveDOMObject(context)
135 , m_type(networkStateNotifier().connectionType())
136 , m_observing(false)
137 , m_contextStopped(false)
138 {
139 ScriptWrappable::init(this);
140 }
141
142 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/netinfo/NetworkInformation.h ('k') | Source/modules/netinfo/NetworkInformation.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698