Chromium Code Reviews| 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/NetworkInfoConnection.h" | |
| 7 | |
| 8 #include "RuntimeEnabledFeatures.h" | |
| 9 #include "core/dom/ExecutionContext.h" | |
| 10 #include "core/page/NetworkStateNotifier.h" | |
| 11 #include "core/workers/WorkerGlobalScope.h" | |
|
adamk
2014/05/27 21:44:27
Hmm, do you need this include for something?
jkarlin
2014/05/28 13:31:30
Done.
| |
| 12 #include "modules/EventTargetModules.h" | |
| 13 #include "public/platform/WebString.h" | |
|
adamk
2014/05/27 21:44:27
I don't think you want this include
jkarlin
2014/05/28 13:31:30
Done.
| |
| 14 #include "v8.h" | |
|
adamk
2014/05/27 21:44:27
Or this one
jkarlin
2014/05/28 13:31:30
Done.
| |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const char kCellular[] = "cellular"; | |
| 19 const char kBluetooth[] = "bluetooth"; | |
| 20 const char kEthernet[] = "ethernet"; | |
| 21 const char kWifi[] = "wifi"; | |
| 22 const char kOther[] = "other"; | |
| 23 const char kNone[] = "none"; | |
| 24 | |
| 25 String connectionTypeToString(blink::WebConnectionType type) | |
| 26 { | |
| 27 switch (type) { | |
| 28 case blink::ConnectionTypeCellular: | |
| 29 return kCellular; | |
| 30 case blink::ConnectionTypeBluetooth: | |
| 31 return kBluetooth; | |
| 32 case blink::ConnectionTypeEthernet: | |
| 33 return kEthernet; | |
| 34 case blink::ConnectionTypeWifi: | |
| 35 return kWifi; | |
| 36 case blink::ConnectionTypeOther: | |
| 37 return kOther; | |
| 38 case blink::ConnectionTypeNone: | |
| 39 return kNone; | |
| 40 } | |
| 41 ASSERT_NOT_REACHED(); | |
| 42 return kNone; | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 namespace WebCore { | |
| 48 | |
| 49 PassRefPtrWillBeRawPtr<NetworkInfoConnection> NetworkInfoConnection::create(Exec utionContext* context) | |
| 50 { | |
| 51 RefPtrWillBeRawPtr<NetworkInfoConnection> connection(adoptRefWillBeRefCounte dGarbageCollected(new NetworkInfoConnection(context))); | |
| 52 connection->suspendIfNeeded(); | |
| 53 return connection.release(); | |
| 54 } | |
| 55 | |
| 56 NetworkInfoConnection::~NetworkInfoConnection() | |
| 57 { | |
| 58 ASSERT(!m_observing); | |
| 59 } | |
| 60 | |
| 61 String NetworkInfoConnection::type() const | |
| 62 { | |
| 63 return connectionTypeToString(m_type); | |
| 64 } | |
| 65 | |
| 66 void NetworkInfoConnection::connectionTypeChange(blink::WebConnectionType type) | |
| 67 { | |
| 68 ASSERT(executionContext()->isContextThread()); | |
| 69 | |
| 70 if (m_type == type) | |
|
adamk
2014/05/27 21:44:27
Doesn't NetworkChangeNotifier guarantee that it'll
jkarlin
2014/05/28 13:31:30
It's actually possible for the same observer to ge
| |
| 71 return; | |
| 72 | |
| 73 m_type = type; | |
| 74 dispatchEvent(Event::create(EventTypeNames::typechange)); | |
| 75 } | |
| 76 | |
| 77 const AtomicString& NetworkInfoConnection::interfaceName() const | |
| 78 { | |
| 79 return EventTargetNames::NetworkInfoConnection; | |
| 80 } | |
| 81 | |
| 82 ExecutionContext* NetworkInfoConnection::executionContext() const | |
| 83 { | |
| 84 return ActiveDOMObject::executionContext(); | |
| 85 } | |
| 86 | |
| 87 bool NetworkInfoConnection::addEventListener(const AtomicString& eventType, Pass RefPtr<EventListener> listener, bool useCapture) | |
| 88 { | |
| 89 bool hadEvents = hasEventListeners(); | |
|
adamk
2014/05/27 21:44:27
This call to hasEventListeners() seems unnecessary
jkarlin
2014/05/28 13:31:30
Done.
| |
| 90 bool ret = EventTargetWithInlineData::addEventListener(eventType, listener, useCapture); | |
|
adamk
2014/05/27 21:44:27
The return value should be telling you whether the
jkarlin
2014/05/28 13:31:30
Done.
| |
| 91 if (!hadEvents && hasEventListeners()) | |
| 92 startObserving(); | |
| 93 return ret; | |
| 94 } | |
| 95 | |
| 96 bool NetworkInfoConnection::removeEventListener(const AtomicString& eventType, E ventListener* listener, bool useCapture) | |
| 97 { | |
| 98 bool hadEvents = hasEventListeners(); | |
|
adamk
2014/05/27 21:44:27
Same here, no need to store the previous value.
jkarlin
2014/05/28 13:31:30
Done.
| |
| 99 bool ret = EventTargetWithInlineData::removeEventListener(eventType, listene r, useCapture); | |
|
adamk
2014/05/27 21:44:27
Same here, this could be:
if (!EventTargetWithInl
jkarlin
2014/05/28 13:31:30
Done.
| |
| 100 if (hadEvents && !hasEventListeners()) | |
| 101 stopObserving(); | |
| 102 return ret; | |
| 103 } | |
| 104 | |
| 105 void NetworkInfoConnection::removeAllEventListeners() | |
| 106 { | |
| 107 bool hadEvents = hasEventListeners(); | |
|
adamk
2014/05/27 21:44:27
And here.
jkarlin
2014/05/28 13:31:30
Done.
| |
| 108 EventTargetWithInlineData::removeAllEventListeners(); | |
| 109 if (hadEvents && !hasEventListeners()) | |
|
adamk
2014/05/27 21:44:27
I'd rather that you change this to an ASSERT(!hasE
jkarlin
2014/05/28 13:31:30
Done.
| |
| 110 stopObserving(); | |
|
adamk
2014/05/27 21:44:27
...and then always call stopObserving() when remov
jkarlin
2014/05/28 13:31:30
Done.
| |
| 111 } | |
| 112 | |
| 113 void NetworkInfoConnection::trace(Visitor*) | |
| 114 { | |
| 115 } | |
| 116 | |
| 117 bool NetworkInfoConnection::hasPendingActivity() const | |
| 118 { | |
| 119 // Prevent collection of this object when there are active listeners. | |
|
adamk
2014/05/27 21:44:27
Can you add an ASSERT(hasEventListeners()) here?
jkarlin
2014/05/28 13:31:30
Done.
| |
| 120 return m_observing; | |
| 121 } | |
| 122 | |
| 123 void NetworkInfoConnection::stop() | |
| 124 { | |
| 125 stopObserving(); | |
| 126 } | |
| 127 | |
| 128 void NetworkInfoConnection::startObserving() | |
| 129 { | |
| 130 if (!m_observing) { | |
| 131 networkStateNotifier().addObserver(this, executionContext()); | |
| 132 m_observing = true; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 void NetworkInfoConnection::stopObserving() | |
| 137 { | |
| 138 if (m_observing) { | |
| 139 networkStateNotifier().removeObserver(this, executionContext()); | |
| 140 m_observing = false; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 NetworkInfoConnection::NetworkInfoConnection(ExecutionContext* context) | |
| 145 : ActiveDOMObject(context) | |
| 146 , m_observing(false) | |
| 147 { | |
| 148 ScriptWrappable::init(this); | |
| 149 m_type = networkStateNotifier().connectionType(); | |
| 150 } | |
| 151 | |
| 152 } // namespace WebCore | |
| OLD | NEW |