OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/sensor/SensorProxy.h" | 5 #include "modules/sensor/SensorProxy.h" |
6 | 6 |
7 #include "core/frame/LocalFrame.h" | 7 #include "core/frame/LocalFrame.h" |
8 #include "modules/permissions/PermissionUtils.h" | |
8 #include "modules/sensor/SensorProviderProxy.h" | 9 #include "modules/sensor/SensorProviderProxy.h" |
9 #include "modules/sensor/SensorReading.h" | 10 #include "modules/sensor/SensorReading.h" |
11 #include "platform/UserGestureIndicator.h" | |
10 #include "platform/mojo/MojoHelper.h" | 12 #include "platform/mojo/MojoHelper.h" |
13 #include "platform/weborigin/SecurityOrigin.h" | |
11 #include "public/platform/Platform.h" | 14 #include "public/platform/Platform.h" |
12 | 15 |
13 using namespace device::mojom::blink; | 16 using namespace device::mojom::blink; |
14 | 17 |
15 namespace blink { | 18 namespace blink { |
16 | 19 |
20 using mojom::blink::PermissionName; | |
21 using mojom::blink::PermissionService; | |
22 using mojom::blink::PermissionStatus; | |
23 | |
17 SensorProxy::SensorProxy(SensorType sensorType, | 24 SensorProxy::SensorProxy(SensorType sensorType, |
25 PermissionService* permissionService, | |
26 RefPtr<SecurityOrigin> origin, | |
18 SensorProviderProxy* provider, | 27 SensorProviderProxy* provider, |
19 std::unique_ptr<SensorReadingFactory> readingFactory) | 28 std::unique_ptr<SensorReadingFactory> readingFactory) |
20 : m_type(sensorType), | 29 : m_type(sensorType), |
21 m_mode(ReportingMode::CONTINUOUS), | 30 m_mode(ReportingMode::CONTINUOUS), |
22 m_provider(provider), | 31 m_provider(provider), |
23 m_clientBinding(this), | 32 m_clientBinding(this), |
24 m_state(SensorProxy::Uninitialized), | 33 m_state(SensorProxy::Uninitialized), |
25 m_suspended(false), | 34 m_suspended(false), |
26 m_readingFactory(std::move(readingFactory)), | 35 m_readingFactory(std::move(readingFactory)), |
27 m_maximumFrequency(0.0) {} | 36 m_maximumFrequency(0.0), |
37 m_permissionStatus(PermissionStatus::ASK), | |
38 m_permissionService(permissionService), | |
39 m_securityOrigin(std::move(origin)) {} | |
40 | |
41 void SensorProxy::onPermissionUpdate(PermissionStatus status) { | |
42 if (m_permissionStatus != status) { | |
43 m_permissionStatus = status; | |
44 SensorPermissionChanged(); | |
45 } | |
46 | |
47 // Keep listening to changes. | |
48 m_permissionService->GetNextPermissionChange( | |
49 createPermissionDescriptor(PermissionName::SENSORS), m_securityOrigin, | |
50 m_permissionStatus, | |
51 convertToBaseCallback( | |
52 WTF::bind(&SensorProxy::onPermissionUpdate, wrapPersistent(this)))); | |
Mikhail
2016/11/16 13:04:28
wrapPersistent will hold SensorProxy from deletion
riju_
2016/11/18 09:29:35
Done.
| |
53 } | |
28 | 54 |
29 SensorProxy::~SensorProxy() {} | 55 SensorProxy::~SensorProxy() {} |
30 | 56 |
31 void SensorProxy::dispose() { | 57 void SensorProxy::dispose() { |
32 m_clientBinding.Close(); | 58 m_clientBinding.Close(); |
33 } | 59 } |
34 | 60 |
35 DEFINE_TRACE(SensorProxy) { | 61 DEFINE_TRACE(SensorProxy) { |
36 visitor->trace(m_reading); | 62 visitor->trace(m_reading); |
37 visitor->trace(m_observers); | 63 visitor->trace(m_observers); |
(...skipping 11 matching lines...) Expand all Loading... | |
49 | 75 |
50 void SensorProxy::initialize() { | 76 void SensorProxy::initialize() { |
51 if (m_state != Uninitialized) | 77 if (m_state != Uninitialized) |
52 return; | 78 return; |
53 | 79 |
54 if (!m_provider->sensorProvider()) { | 80 if (!m_provider->sensorProvider()) { |
55 handleSensorError(); | 81 handleSensorError(); |
56 return; | 82 return; |
57 } | 83 } |
58 | 84 |
59 m_state = Initializing; | 85 // Request permission. |
60 auto callback = convertToBaseCallback( | 86 if (m_permissionStatus == PermissionStatus::ASK) { |
Mikhail
2016/11/16 13:04:28
nit: switch .. case .. would look better
riju_
2016/11/18 09:29:35
Done.
| |
61 WTF::bind(&SensorProxy::onSensorCreated, wrapWeakPersistent(this))); | 87 m_permissionService->RequestPermission( |
62 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor), | 88 createPermissionDescriptor(PermissionName::SENSORS), m_securityOrigin, |
63 callback); | 89 UserGestureIndicator::processingUserGesture(), |
90 convertToBaseCallback( | |
91 WTF::bind(&SensorProxy::onPermissionUpdate, wrapPersistent(this)))); | |
92 } else if (m_permissionStatus == PermissionStatus::DENIED) { | |
93 handleSensorError(SecurityError, "Permission not granted."); | |
Mikhail
2016/11/16 13:04:28
NotAllowedError
riju_
2016/11/18 09:29:35
Done.
| |
94 } | |
Mikhail
2016/11/16 13:04:28
case for granted permissions?
riju_
2016/11/18 09:29:35
Done.
| |
64 } | 95 } |
65 | 96 |
66 void SensorProxy::addConfiguration( | 97 void SensorProxy::addConfiguration( |
67 SensorConfigurationPtr configuration, | 98 SensorConfigurationPtr configuration, |
68 std::unique_ptr<Function<void(bool)>> callback) { | 99 std::unique_ptr<Function<void(bool)>> callback) { |
69 DCHECK(isInitialized()); | 100 DCHECK(isInitialized()); |
70 m_sensor->AddConfiguration(std::move(configuration), | 101 m_sensor->AddConfiguration(std::move(configuration), |
71 convertToBaseCallback(std::move(callback))); | 102 convertToBaseCallback(std::move(callback))); |
72 } | 103 } |
73 | 104 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 | 152 |
122 void SensorProxy::RaiseError() { | 153 void SensorProxy::RaiseError() { |
123 handleSensorError(); | 154 handleSensorError(); |
124 } | 155 } |
125 | 156 |
126 void SensorProxy::SensorReadingChanged() { | 157 void SensorProxy::SensorReadingChanged() { |
127 for (Observer* observer : m_observers) | 158 for (Observer* observer : m_observers) |
128 observer->onSensorReadingChanged(); | 159 observer->onSensorReadingChanged(); |
129 } | 160 } |
130 | 161 |
162 void SensorProxy::SensorPermissionChanged() { | |
Mikhail
2016/11/16 13:04:28
if proxy is not initialized, it should ignore this
riju_
2016/11/18 09:29:35
Done.
| |
163 // If Permission is revoked throw error. | |
164 if (m_permissionStatus != PermissionStatus::GRANTED) { | |
165 handleSensorError(SecurityError, "Permission not granted."); | |
166 } else { | |
167 if (!isInitialized()) { | |
168 m_state = Initializing; | |
Mikhail
2016/11/16 13:04:28
why? 'Initializing' state should be cause *only* f
riju_
2016/11/18 09:29:35
Done.
| |
169 auto callback = convertToBaseCallback( | |
170 WTF::bind(&SensorProxy::onSensorCreated, wrapWeakPersistent(this))); | |
171 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor), | |
172 callback); | |
173 } | |
174 } | |
175 } | |
176 | |
131 void SensorProxy::handleSensorError(ExceptionCode code, | 177 void SensorProxy::handleSensorError(ExceptionCode code, |
132 String sanitizedMessage, | 178 String sanitizedMessage, |
133 String unsanitizedMessage) { | 179 String unsanitizedMessage) { |
134 if (!Platform::current()) { | 180 if (!Platform::current()) { |
135 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. | 181 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. |
136 return; | 182 return; |
137 } | 183 } |
138 | 184 |
139 m_state = Uninitialized; | 185 m_state = Uninitialized; |
140 // The m_sensor.reset() will release all callbacks and its bound parameters, | 186 // The m_sensor.reset() will release all callbacks and its bound parameters, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); | 248 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); |
203 auto version = seqlock.ReadBegin(); | 249 auto version = seqlock.ReadBegin(); |
204 auto readingData = buffer->reading; | 250 auto readingData = buffer->reading; |
205 if (seqlock.ReadRetry(version)) | 251 if (seqlock.ReadRetry(version)) |
206 return false; | 252 return false; |
207 result = readingData; | 253 result = readingData; |
208 return true; | 254 return true; |
209 } | 255 } |
210 | 256 |
211 } // namespace blink | 257 } // namespace blink |
OLD | NEW |