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

Side by Side Diff: third_party/WebKit/Source/modules/sensor/SensorProxy.cpp

Issue 2458453002: [sensors] Add Permission guard to the generic sensor apis.
Patch Set: Fix mikhail's comments Created 4 years, 1 month 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
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"
15 #include "wtf/RefPtr.h"
12 16
13 using namespace device::mojom::blink; 17 using namespace device::mojom::blink;
14 18
15 namespace blink { 19 namespace blink {
16 20
21 using mojom::blink::PermissionName;
22 using mojom::blink::PermissionService;
23 using mojom::blink::PermissionStatus;
24
17 SensorProxy::SensorProxy(SensorType sensorType, 25 SensorProxy::SensorProxy(SensorType sensorType,
26 PermissionService* permissionService,
27 PassRefPtr<SecurityOrigin> origin,
18 SensorProviderProxy* provider, 28 SensorProviderProxy* provider,
19 std::unique_ptr<SensorReadingFactory> readingFactory) 29 std::unique_ptr<SensorReadingFactory> readingFactory)
20 : m_type(sensorType), 30 : m_type(sensorType),
21 m_mode(ReportingMode::CONTINUOUS), 31 m_mode(ReportingMode::CONTINUOUS),
22 m_provider(provider), 32 m_provider(provider),
23 m_clientBinding(this), 33 m_clientBinding(this),
24 m_state(SensorProxy::Uninitialized), 34 m_state(SensorProxy::Uninitialized),
25 m_suspended(false), 35 m_suspended(false),
26 m_readingFactory(std::move(readingFactory)), 36 m_readingFactory(std::move(readingFactory)),
27 m_maximumFrequency(0.0) {} 37 m_maximumFrequency(0.0),
38 m_permissionStatus(PermissionStatus::ASK),
39 m_permissionService(permissionService),
40 m_securityOrigin(std::move(origin)) {}
Mikhail 2016/11/15 14:03:18 no need to move PassRefPtr, I think PassRefPtr its
riju_ 2016/11/16 12:38:25 Done.
41
42 void SensorProxy::onPermissionUpdate(PermissionStatus status) {
43 if (m_permissionStatus != status) {
44 m_permissionStatus = status;
45 SensorPermissionChanged();
46 }
47
48 // Keep listening to changes.
49 m_permissionService->GetNextPermissionChange(
50 createPermissionDescriptor(PermissionName::SENSORS), m_securityOrigin,
51 m_permissionStatus,
52 convertToBaseCallback(
53 WTF::bind(&SensorProxy::onPermissionUpdate, wrapPersistent(this))));
54 }
28 55
29 SensorProxy::~SensorProxy() {} 56 SensorProxy::~SensorProxy() {}
30 57
31 void SensorProxy::dispose() { 58 void SensorProxy::dispose() {
32 m_clientBinding.Close(); 59 m_clientBinding.Close();
33 } 60 }
34 61
35 DEFINE_TRACE(SensorProxy) { 62 DEFINE_TRACE(SensorProxy) {
36 visitor->trace(m_reading); 63 visitor->trace(m_reading);
37 visitor->trace(m_observers); 64 visitor->trace(m_observers);
(...skipping 11 matching lines...) Expand all
49 76
50 void SensorProxy::initialize() { 77 void SensorProxy::initialize() {
51 if (m_state != Uninitialized) 78 if (m_state != Uninitialized)
52 return; 79 return;
53 80
54 if (!m_provider->sensorProvider()) { 81 if (!m_provider->sensorProvider()) {
55 handleSensorError(); 82 handleSensorError();
56 return; 83 return;
57 } 84 }
58 85
59 m_state = Initializing; 86 // Request permission.
60 auto callback = convertToBaseCallback( 87 if (m_permissionStatus == PermissionStatus::ASK) {
Mikhail 2016/11/15 14:03:18 what if (m_permissionStatus != PermissionStatus::A
riju_ 2016/11/16 12:38:25 Done.
61 WTF::bind(&SensorProxy::onSensorCreated, wrapWeakPersistent(this))); 88 m_permissionService->RequestPermission(
shalamov 2016/11/14 14:40:06 Can be garbage, if invalidated.
riju_ 2016/11/16 12:38:25 Acknowledged.
62 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor), 89 createPermissionDescriptor(PermissionName::SENSORS), m_securityOrigin,
63 callback); 90 UserGestureIndicator::processingUserGesture(),
91 convertToBaseCallback(
92 WTF::bind(&SensorProxy::onPermissionUpdate, wrapPersistent(this))));
93 }
64 } 94 }
65 95
66 void SensorProxy::addConfiguration( 96 void SensorProxy::addConfiguration(
67 SensorConfigurationPtr configuration, 97 SensorConfigurationPtr configuration,
68 std::unique_ptr<Function<void(bool)>> callback) { 98 std::unique_ptr<Function<void(bool)>> callback) {
69 DCHECK(isInitialized()); 99 DCHECK(isInitialized());
70 m_sensor->AddConfiguration(std::move(configuration), 100 m_sensor->AddConfiguration(std::move(configuration),
71 convertToBaseCallback(std::move(callback))); 101 convertToBaseCallback(std::move(callback)));
72 } 102 }
73 103
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 151
122 void SensorProxy::RaiseError() { 152 void SensorProxy::RaiseError() {
123 handleSensorError(); 153 handleSensorError();
124 } 154 }
125 155
126 void SensorProxy::SensorReadingChanged() { 156 void SensorProxy::SensorReadingChanged() {
127 for (Observer* observer : m_observers) 157 for (Observer* observer : m_observers)
128 observer->onSensorReadingChanged(); 158 observer->onSensorReadingChanged();
129 } 159 }
130 160
161 void SensorProxy::SensorPermissionChanged() {
162 // If Permission is revoked throw error and reset connection.
163 if (m_permissionStatus != PermissionStatus::GRANTED) {
164 m_provider->onPermissionServiceConnectionError();
shalamov 2016/11/14 14:40:06 This will invalidate permission proxy when permiss
Mikhail 2016/11/15 14:03:18 we should prepare implementation for more granular
riju_ 2016/11/16 12:38:25 Acknowledged.
riju_ 2016/11/16 12:38:25 My bad, handleSensorError() is called now
165 } else {
166 m_state = Initializing;
shalamov 2016/11/14 14:40:07 Can there be a case when we get "GRANTED" and prox
Mikhail 2016/11/15 14:03:18 we should not assign m_state here. What if SensorP
riju_ 2016/11/16 12:38:26 Added a check now, so that we don't call GetProxy
167 auto callback = convertToBaseCallback(
168 WTF::bind(&SensorProxy::onSensorCreated, wrapWeakPersistent(this)));
169 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor),
170 callback);
171 }
172 }
173
131 void SensorProxy::handleSensorError(ExceptionCode code, 174 void SensorProxy::handleSensorError(ExceptionCode code,
132 String sanitizedMessage, 175 String sanitizedMessage,
133 String unsanitizedMessage) { 176 String unsanitizedMessage) {
134 if (!Platform::current()) { 177 if (!Platform::current()) {
135 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. 178 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed.
136 return; 179 return;
137 } 180 }
138 181
139 m_state = Uninitialized; 182 m_state = Uninitialized;
140 // The m_sensor.reset() will release all callbacks and its bound parameters, 183 // The m_sensor.reset() will release all callbacks and its bound parameters,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); 245 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value();
203 auto version = seqlock.ReadBegin(); 246 auto version = seqlock.ReadBegin();
204 auto readingData = buffer->reading; 247 auto readingData = buffer->reading;
205 if (seqlock.ReadRetry(version)) 248 if (seqlock.ReadRetry(version))
206 return false; 249 return false;
207 result = readingData; 250 result = readingData;
208 return true; 251 return true;
209 } 252 }
210 253
211 } // namespace blink 254 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698