OLD | NEW |
---|---|
(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 const char cellular[] = "cellular"; | |
16 const char bluetooth[] = "bluetooth"; | |
17 const char ethernet[] = "ethernet"; | |
18 const char wifi[] = "wifi"; | |
19 const char other[] = "other"; | |
20 const char none[] = "none"; | |
adamk
2014/05/30 00:13:51
fwiw I don't think there's much use for these cons
jkarlin
2014/05/30 01:51:24
Done.
| |
21 | |
22 String connectionTypeToString(blink::WebConnectionType type) | |
23 { | |
24 switch (type) { | |
25 case blink::ConnectionTypeCellular: | |
26 return cellular; | |
27 case blink::ConnectionTypeBluetooth: | |
28 return bluetooth; | |
29 case blink::ConnectionTypeEthernet: | |
30 return ethernet; | |
31 case blink::ConnectionTypeWifi: | |
32 return wifi; | |
33 case blink::ConnectionTypeOther: | |
34 return other; | |
35 case blink::ConnectionTypeNone: | |
36 return none; | |
37 } | |
38 ASSERT_NOT_REACHED(); | |
39 return none; | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 namespace WebCore { | |
45 | |
46 PassRefPtrWillBeRawPtr<NetworkInformation> NetworkInformation::create(ExecutionC ontext* context) | |
47 { | |
48 RefPtrWillBeRawPtr<NetworkInformation> connection(adoptRefWillBeRefCountedGa rbageCollected(new NetworkInformation(context))); | |
49 connection->suspendIfNeeded(); | |
50 return connection.release(); | |
51 } | |
52 | |
53 NetworkInformation::~NetworkInformation() | |
54 { | |
55 ASSERT(!m_observing); | |
56 } | |
57 | |
58 String NetworkInformation::type() const | |
59 { | |
60 return connectionTypeToString(m_type); | |
61 } | |
62 | |
63 void NetworkInformation::connectionTypeChange(blink::WebConnectionType type) | |
64 { | |
65 ASSERT(executionContext()->isContextThread()); | |
66 | |
67 // This can happen if the observer removes and then adds itself again | |
68 // during notification. | |
69 if (m_type == type) | |
70 return; | |
71 | |
72 m_type = type; | |
73 dispatchEvent(Event::create(EventTypeNames::typechange)); | |
74 } | |
75 | |
76 const AtomicString& NetworkInformation::interfaceName() const | |
77 { | |
78 return EventTargetNames::NetworkInformation; | |
79 } | |
80 | |
81 ExecutionContext* NetworkInformation::executionContext() const | |
82 { | |
83 return ActiveDOMObject::executionContext(); | |
84 } | |
85 | |
86 bool NetworkInformation::addEventListener(const AtomicString& eventType, PassRef Ptr<EventListener> listener, bool useCapture) | |
87 { | |
88 if (!EventTargetWithInlineData::addEventListener(eventType, listener, useCap ture)) | |
89 return false; | |
90 startObserving(); | |
91 return true; | |
92 } | |
93 | |
94 bool NetworkInformation::removeEventListener(const AtomicString& eventType, Even tListener* listener, bool useCapture) | |
95 { | |
96 if (!EventTargetWithInlineData::removeEventListener(eventType, listener, use Capture)) | |
97 return false; | |
98 if (!hasEventListeners()) | |
99 stopObserving(); | |
100 return true; | |
101 } | |
102 | |
103 void NetworkInformation::removeAllEventListeners() | |
104 { | |
105 EventTargetWithInlineData::removeAllEventListeners(); | |
106 ASSERT(!hasEventListeners()); | |
107 stopObserving(); | |
108 } | |
109 | |
110 bool NetworkInformation::hasPendingActivity() const | |
111 { | |
112 ASSERT(m_contextStopped || m_observing == hasEventListeners()); | |
113 | |
114 // Prevent collection of this object when there are active listeners. | |
115 return m_observing; | |
116 } | |
117 | |
118 void NetworkInformation::stop() | |
119 { | |
120 m_contextStopped = true; | |
121 stopObserving(); | |
122 } | |
123 | |
124 void NetworkInformation::startObserving() | |
125 { | |
126 if (!m_observing && !m_contextStopped) { | |
127 networkStateNotifier().addObserver(this, executionContext()); | |
128 m_observing = true; | |
129 } | |
130 } | |
131 | |
132 void NetworkInformation::stopObserving() | |
133 { | |
134 if (m_observing) { | |
135 networkStateNotifier().removeObserver(this, executionContext()); | |
136 m_observing = false; | |
137 } | |
138 } | |
139 | |
140 NetworkInformation::NetworkInformation(ExecutionContext* context) | |
141 : ActiveDOMObject(context) | |
142 , m_type(networkStateNotifier().connectionType()) | |
143 , m_observing(false) | |
144 , m_contextStopped(false) | |
145 { | |
146 ScriptWrappable::init(this); | |
147 } | |
148 | |
149 } // namespace WebCore | |
OLD | NEW |