OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "modules/sensor/SensorProviderProxy.h" | |
6 | |
7 #include "core/frame/LocalFrame.h" | |
8 #include "modules/sensor/SensorProxy.h" | |
9 #include "platform/mojo/MojoHelper.h" | |
10 #include "public/platform/InterfaceProvider.h" | |
11 | |
12 namespace blink { | |
13 | |
14 const char SensorProviderProxy::s_supplementKey[] = "SensorProvider"; | |
15 | |
16 // SensorProviderProxy | |
17 SensorProviderProxy::SensorProviderProxy(LocalFrame* frame) | |
18 : PageVisibilityObserver(frame->document() ? frame->document()->page() : nul lptr) | |
19 , m_frame(frame) | |
20 { | |
21 frame->interfaceProvider()->getInterface(mojo::GetProxy(&m_sensorProvider)); | |
22 m_sensorProvider.set_connection_error_handler(convertToBaseCallback(WTF::bin d(&SensorProviderProxy::handleSensorError, wrapWeakPersistent(this)))); | |
23 } | |
24 | |
25 SensorProviderProxy* SensorProviderProxy::getOrCreateForFrame(LocalFrame* frame) | |
26 { | |
27 DCHECK(frame); | |
28 SensorProviderProxy* result = static_cast<SensorProviderProxy*>(Supplement<L ocalFrame>::from(*frame, s_supplementKey)); | |
29 if (!result) { | |
30 result = new SensorProviderProxy(frame); | |
31 Supplement<LocalFrame>::provideTo(*frame, s_supplementKey, result); | |
32 } | |
33 return result; | |
34 } | |
35 | |
36 SensorProviderProxy::~SensorProviderProxy() | |
37 { | |
38 } | |
39 | |
40 DEFINE_TRACE(SensorProviderProxy) | |
41 { | |
42 visitor->trace(m_sensors); | |
43 visitor->trace(m_frame); | |
44 PageVisibilityObserver::trace(visitor); | |
45 Supplement<LocalFrame>::trace(visitor); | |
46 } | |
47 | |
48 void SensorProviderProxy::removeSensor(SensorProxy* sensor) | |
49 { | |
50 m_sensors.remove(sensor); | |
51 } | |
52 | |
53 SensorProxy* SensorProviderProxy::getOrCreateSensor(device::mojom::blink::Sensor Type type) | |
54 { | |
55 for (SensorProxy* sensor : m_sensors) { | |
56 if (sensor->type() == type) | |
timvolodine
2016/09/01 19:02:06
maybe this could be hashed by type for efficiency,
Mikhail
2016/09/02 08:23:43
Added TODO comment.
| |
57 return sensor; | |
58 } | |
59 SensorProxy* sensor = new SensorProxy(type, this); | |
60 m_sensors.add(sensor); | |
61 | |
62 return sensor; | |
63 } | |
64 | |
65 void SensorProviderProxy::handleSensorError() | |
66 { | |
67 SensorsSet copy(m_sensors); | |
68 for (SensorProxy* sensor : copy) { | |
69 // All sensors are invalidated. | |
70 sensor->handleSensorError(); | |
71 } | |
72 DCHECK(m_sensors.isEmpty()); | |
73 if (m_frame) | |
74 m_frame->removeSupplement(s_supplementKey); | |
75 } | |
76 | |
77 void SensorProviderProxy::pageVisibilityChanged() | |
78 { | |
79 if (!m_frame) | |
80 return; | |
81 | |
82 Page* page = m_frame->document()->page(); | |
83 DCHECK(page); | |
84 | |
85 if (page->visibilityState() != PageVisibilityStateVisible) { | |
86 for (SensorProxy* sensor : m_sensors) | |
87 sensor->suspend(); | |
88 } else { | |
89 for (SensorProxy* sensor : m_sensors) | |
90 sensor->resume(); | |
91 } | |
92 } | |
93 | |
94 } // namespace blink | |
OLD | NEW |