1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "modules/netinfo/NetworkInformation.h"
#include "core/dom/ExecutionContext.h"
#include "core/events/Event.h"
#include "core/page/NetworkStateNotifier.h"
#include "modules/EventTargetModules.h"
#include "wtf/text/WTFString.h"
namespace {
String connectionTypeToString(blink::WebConnectionType type)
{
switch (type) {
case blink::ConnectionTypeCellular:
return "cellular";
case blink::ConnectionTypeBluetooth:
return "bluetooth";
case blink::ConnectionTypeEthernet:
return "ethernet";
case blink::ConnectionTypeWifi:
return "wifi";
case blink::ConnectionTypeOther:
return "other";
case blink::ConnectionTypeNone:
return "none";
case blink::ConnectionTypeUnknown:
return "unknown";
}
ASSERT_NOT_REACHED();
return "none";
}
} // namespace
namespace blink {
NetworkInformation* NetworkInformation::create(ExecutionContext* context)
{
NetworkInformation* connection = new NetworkInformation(context);
connection->suspendIfNeeded();
return connection;
}
NetworkInformation::~NetworkInformation()
{
ASSERT(!m_observing);
}
String NetworkInformation::type() const
{
// m_type is only updated when listening for events, so ask networkStateNotifier
// if not listening (crbug.com/379841).
if (!m_observing)
return connectionTypeToString(networkStateNotifier().connectionType());
// If observing, return m_type which changes when the event fires, per spec.
return connectionTypeToString(m_type);
}
void NetworkInformation::connectionTypeChange(WebConnectionType type)
{
ASSERT(executionContext()->isContextThread());
// This can happen if the observer removes and then adds itself again
// during notification.
if (m_type == type)
return;
m_type = type;
dispatchEvent(Event::create(EventTypeNames::typechange));
}
const AtomicString& NetworkInformation::interfaceName() const
{
return EventTargetNames::NetworkInformation;
}
ExecutionContext* NetworkInformation::executionContext() const
{
return ActiveDOMObject::executionContext();
}
bool NetworkInformation::addEventListener(const AtomicString& eventType, PassRefPtr<EventListener> listener, bool useCapture)
{
if (!EventTargetWithInlineData::addEventListener(eventType, listener, useCapture))
return false;
startObserving();
return true;
}
bool NetworkInformation::removeEventListener(const AtomicString& eventType, PassRefPtr<EventListener> listener, bool useCapture)
{
if (!EventTargetWithInlineData::removeEventListener(eventType, listener, useCapture))
return false;
if (!hasEventListeners())
stopObserving();
return true;
}
void NetworkInformation::removeAllEventListeners()
{
EventTargetWithInlineData::removeAllEventListeners();
ASSERT(!hasEventListeners());
stopObserving();
}
bool NetworkInformation::hasPendingActivity() const
{
ASSERT(m_contextStopped || m_observing == hasEventListeners());
// Prevent collection of this object when there are active listeners.
return m_observing;
}
void NetworkInformation::stop()
{
m_contextStopped = true;
stopObserving();
}
void NetworkInformation::startObserving()
{
if (!m_observing && !m_contextStopped) {
m_type = networkStateNotifier().connectionType();
networkStateNotifier().addObserver(this, executionContext());
m_observing = true;
}
}
void NetworkInformation::stopObserving()
{
if (m_observing) {
networkStateNotifier().removeObserver(this, executionContext());
m_observing = false;
}
}
NetworkInformation::NetworkInformation(ExecutionContext* context)
: ActiveDOMObject(context)
, m_type(networkStateNotifier().connectionType())
, m_observing(false)
, m_contextStopped(false)
{
}
void NetworkInformation::trace(Visitor* visitor)
{
RefCountedGarbageCollectedEventTargetWithInlineData<NetworkInformation>::trace(visitor);
ActiveDOMObject::trace(visitor);
}
} // namespace blink
|