Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: Source/modules/netinfo/NetworkInformation.cpp

Issue 291203002: Adds NetInfo v3 Web API to Blink. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@netinfo1
Patch Set: Forgot the common layout test file Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
12 namespace {
13
14 const char kCellular[] = "cellular";
15 const char kBluetooth[] = "bluetooth";
16 const char kEthernet[] = "ethernet";
17 const char kWifi[] = "wifi";
18 const char kOther[] = "other";
19 const char kNone[] = "none";
20
21 String connectionTypeToString(blink::WebConnectionType type)
22 {
23 switch (type) {
24 case blink::ConnectionTypeCellular:
25 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.
26 case blink::ConnectionTypeBluetooth:
27 return kBluetooth;
28 case blink::ConnectionTypeEthernet:
29 return kEthernet;
30 case blink::ConnectionTypeWifi:
31 return kWifi;
32 case blink::ConnectionTypeOther:
33 return kOther;
34 case blink::ConnectionTypeNone:
35 return kNone;
36 }
37 ASSERT_NOT_REACHED();
38 return kNone;
39 }
40
41 } // namespace
42
43 namespace WebCore {
44
45 PassRefPtrWillBeRawPtr<NetworkInformation> NetworkInformation::create(ExecutionC ontext* context)
46 {
47 RefPtrWillBeRawPtr<NetworkInformation> connection(adoptRefWillBeRefCountedGa rbageCollected(new NetworkInformation(context)));
48 connection->suspendIfNeeded();
49 return connection.release();
50 }
51
52 NetworkInformation::~NetworkInformation()
53 {
54 ASSERT(!m_observing);
55 }
56
57 String NetworkInformation::type() const
58 {
59 return connectionTypeToString(m_type);
60 }
61
62 void NetworkInformation::connectionTypeChange(blink::WebConnectionType type)
63 {
64 ASSERT(executionContext()->isContextThread());
65
66 // This can happen if the observer removes and then adds itself again
67 // during notification.
68 if (m_type == type)
69 return;
70
71 m_type = type;
72 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
73 }
74
75 const AtomicString& NetworkInformation::interfaceName() const
76 {
77 return EventTargetNames::NetworkInformation;
78 }
79
80 ExecutionContext* NetworkInformation::executionContext() const
81 {
82 return ActiveDOMObject::executionContext();
83 }
84
85 bool NetworkInformation::addEventListener(const AtomicString& eventType, PassRef Ptr<EventListener> listener, bool useCapture)
86 {
87 if (!EventTargetWithInlineData::addEventListener(eventType, listener, useCap ture))
88 return false;
89 startObserving();
90 return true;
91 }
92
93 bool NetworkInformation::removeEventListener(const AtomicString& eventType, Even tListener* listener, bool useCapture)
94 {
95 if (!EventTargetWithInlineData::removeEventListener(eventType, listener, use Capture))
96 return false;
97 if (!hasEventListeners())
98 stopObserving();
99 return true;
100 }
101
102 void NetworkInformation::removeAllEventListeners()
103 {
104 EventTargetWithInlineData::removeAllEventListeners();
105 ASSERT(!hasEventListeners());
106 stopObserving();
107 }
108
109 bool NetworkInformation::hasPendingActivity() const
110 {
111 ASSERT(hasEventListeners() == m_observing);
112
113 // Prevent collection of this object when there are active listeners.
114 return m_observing;
115 }
116
117 void NetworkInformation::stop()
118 {
119 stopObserving();
120 }
121
122 void NetworkInformation::startObserving()
123 {
124 if (!m_observing) {
125 networkStateNotifier().addObserver(this, executionContext());
126 m_observing = true;
127 }
128 }
129
130 void NetworkInformation::stopObserving()
131 {
132 if (m_observing) {
133 networkStateNotifier().removeObserver(this, executionContext());
134 m_observing = false;
135 }
136 }
137
138 NetworkInformation::NetworkInformation(ExecutionContext* context)
139 : ActiveDOMObject(context)
140 , m_observing(false)
141 {
142 ScriptWrappable::init(this);
143 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.
144 }
145
146 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698