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.
6 #include "modules/netinfo/NetworkInformation.h"
8 #include "core/dom/ExecutionContext.h"
9 #include "core/events/Event.h"
10 #include "core/page/NetworkStateNotifier.h"
11 #include "modules/EventTargetModules.h"
12 #include "wtf/text/WTFString.h"
16 String connectionTypeToString(blink::WebConnectionType type)
19 case blink::ConnectionTypeCellular:
21 case blink::ConnectionTypeBluetooth:
23 case blink::ConnectionTypeEthernet:
25 case blink::ConnectionTypeWifi:
27 case blink::ConnectionTypeOther:
29 case blink::ConnectionTypeNone:
31 case blink::ConnectionTypeUnknown:
42 NetworkInformation* NetworkInformation::create(ExecutionContext* context)
44 NetworkInformation* connection = new NetworkInformation(context);
45 connection->suspendIfNeeded();
49 NetworkInformation::~NetworkInformation()
54 String NetworkInformation::type() const
56 // m_type is only updated when listening for events, so ask networkStateNotifier
57 // if not listening (crbug.com/379841).
59 return connectionTypeToString(networkStateNotifier().connectionType());
61 // If observing, return m_type which changes when the event fires, per spec.
62 return connectionTypeToString(m_type);
65 void NetworkInformation::connectionTypeChange(WebConnectionType type)
67 ASSERT(executionContext()->isContextThread());
69 // This can happen if the observer removes and then adds itself again
70 // during notification.
75 dispatchEvent(Event::create(EventTypeNames::typechange));
78 const AtomicString& NetworkInformation::interfaceName() const
80 return EventTargetNames::NetworkInformation;
83 ExecutionContext* NetworkInformation::executionContext() const
85 return ActiveDOMObject::executionContext();
88 bool NetworkInformation::addEventListener(const AtomicString& eventType, PassRefPtr<EventListener> listener, bool useCapture)
90 if (!EventTargetWithInlineData::addEventListener(eventType, listener, useCapture))
96 bool NetworkInformation::removeEventListener(const AtomicString& eventType, PassRefPtr<EventListener> listener, bool useCapture)
98 if (!EventTargetWithInlineData::removeEventListener(eventType, listener, useCapture))
100 if (!hasEventListeners())
105 void NetworkInformation::removeAllEventListeners()
107 EventTargetWithInlineData::removeAllEventListeners();
108 ASSERT(!hasEventListeners());
112 bool NetworkInformation::hasPendingActivity() const
114 ASSERT(m_contextStopped || m_observing == hasEventListeners());
116 // Prevent collection of this object when there are active listeners.
120 void NetworkInformation::stop()
122 m_contextStopped = true;
126 void NetworkInformation::startObserving()
128 if (!m_observing && !m_contextStopped) {
129 m_type = networkStateNotifier().connectionType();
130 networkStateNotifier().addObserver(this, executionContext());
135 void NetworkInformation::stopObserving()
138 networkStateNotifier().removeObserver(this, executionContext());
143 NetworkInformation::NetworkInformation(ExecutionContext* context)
144 : ActiveDOMObject(context)
145 , m_type(networkStateNotifier().connectionType())
147 , m_contextStopped(false)