Chromium Code Reviews| Index: Source/modules/netinfo/NetworkInformation.cpp |
| diff --git a/Source/modules/netinfo/NetworkInformation.cpp b/Source/modules/netinfo/NetworkInformation.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6688d19921ec2dd9d91bd4ee0c9e31a5a15e1830 |
| --- /dev/null |
| +++ b/Source/modules/netinfo/NetworkInformation.cpp |
| @@ -0,0 +1,146 @@ |
| +// 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/page/NetworkStateNotifier.h" |
| +#include "modules/EventTargetModules.h" |
| + |
| +namespace { |
| + |
| +const char kCellular[] = "cellular"; |
| +const char kBluetooth[] = "bluetooth"; |
| +const char kEthernet[] = "ethernet"; |
| +const char kWifi[] = "wifi"; |
| +const char kOther[] = "other"; |
| +const char kNone[] = "none"; |
| + |
| +String connectionTypeToString(blink::WebConnectionType type) |
| +{ |
| + switch (type) { |
| + case blink::ConnectionTypeCellular: |
| + return kCellular; |
|
Inactive
2014/05/28 20:18:20
So we keep constructing WTF Strings from const cha
jkarlin
2014/05/29 11:37:56
Thanks for the suggestion! Done.
|
| + case blink::ConnectionTypeBluetooth: |
| + return kBluetooth; |
| + case blink::ConnectionTypeEthernet: |
| + return kEthernet; |
| + case blink::ConnectionTypeWifi: |
| + return kWifi; |
| + case blink::ConnectionTypeOther: |
| + return kOther; |
| + case blink::ConnectionTypeNone: |
| + return kNone; |
| + } |
| + ASSERT_NOT_REACHED(); |
| + return kNone; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace WebCore { |
| + |
| +PassRefPtrWillBeRawPtr<NetworkInformation> NetworkInformation::create(ExecutionContext* context) |
| +{ |
| + RefPtrWillBeRawPtr<NetworkInformation> connection(adoptRefWillBeRefCountedGarbageCollected(new NetworkInformation(context))); |
| + connection->suspendIfNeeded(); |
| + return connection.release(); |
| +} |
| + |
| +NetworkInformation::~NetworkInformation() |
| +{ |
| + ASSERT(!m_observing); |
| +} |
| + |
| +String NetworkInformation::type() const |
| +{ |
| + return connectionTypeToString(m_type); |
| +} |
| + |
| +void NetworkInformation::connectionTypeChange(blink::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)); |
|
Inactive
2014/05/28 20:18:20
The spec says to queue the event but it seems we a
adamk
2014/05/28 22:44:08
I noticed this too, but I think we're effectively
Inactive
2014/05/28 22:47:28
It makes sense, thanks for clarifying.
jkarlin
2014/05/29 11:37:56
Done.
jkarlin
2014/05/29 11:39:45
I should clarify that by 'done' I'm just agreeing
|
| +} |
| + |
| +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, 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(hasEventListeners() == m_observing); |
| + |
| + // Prevent collection of this object when there are active listeners. |
| + return m_observing; |
| +} |
| + |
| +void NetworkInformation::stop() |
| +{ |
| + stopObserving(); |
| +} |
| + |
| +void NetworkInformation::startObserving() |
| +{ |
| + if (!m_observing) { |
| + 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_observing(false) |
| +{ |
| + ScriptWrappable::init(this); |
| + m_type = networkStateNotifier().connectionType(); |
|
Inactive
2014/05/28 20:18:20
nit: Could be in the initializer list?
jkarlin
2014/05/29 11:37:56
Done.
|
| +} |
| + |
| +} // namespace WebCore |