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

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 comments + rebase 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"
11 #include "public/platform/Platform.h" 13 #include "public/platform/Platform.h"
14 #include "wtf/RefPtr.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 ExecutionContext* executionContext,
18 SensorProviderProxy* provider, 26 SensorProviderProxy* provider,
19 std::unique_ptr<SensorReadingFactory> readingFactory) 27 std::unique_ptr<SensorReadingFactory> readingFactory)
20 : m_type(sensorType), 28 : m_type(sensorType),
21 m_mode(ReportingMode::CONTINUOUS), 29 m_mode(ReportingMode::CONTINUOUS),
22 m_provider(provider), 30 m_provider(provider),
23 m_clientBinding(this), 31 m_clientBinding(this),
24 m_state(SensorProxy::Uninitialized), 32 m_state(SensorProxy::Uninitialized),
25 m_suspended(false), 33 m_suspended(false),
26 m_readingFactory(std::move(readingFactory)) {} 34 m_readingFactory(std::move(readingFactory)),
35 m_sensorPermission(PermissionStatus::ASK),
36 m_executionContext(executionContext),
Mikhail 2016/11/10 09:30:48 can we pass the "prepared" permissionService here
riju_ 2016/11/14 14:00:06 Yes, done now.
37 m_startPendingPermission(false) {}
38
39 void SensorProxy::getNextPermissionChange(ExecutionContext* executionContext,
Mikhail 2016/11/10 09:30:48 we don't need this wrapper, it's called only once
riju_ 2016/11/14 14:00:06 Done. removed.
40 PermissionStatus status) {
41 m_permissionService->GetNextPermissionChange(
42 createPermissionDescriptor(PermissionName::SENSORS),
43 executionContext->getSecurityOrigin(), m_sensorPermission,
44 convertToBaseCallback(
45 WTF::bind(&SensorProxy::onPermissionUpdate, wrapPersistent(this))));
46 }
47
48 void SensorProxy::requestPermission(ExecutionContext* executionContext,
49 PermissionStatus status) {
50 m_permissionService->RequestPermission(
51 createPermissionDescriptor(PermissionName::SENSORS),
52 m_executionContext->getSecurityOrigin(),
53 UserGestureIndicator::processingUserGesture(),
54 convertToBaseCallback(
55 WTF::bind(&SensorProxy::onPermissionUpdate, wrapPersistent(this))));
56 }
57
58 void SensorProxy::onPermissionUpdate(PermissionStatus status) {
59 if (m_sensorPermission != status) {
60 m_sensorPermission = status;
61 SensorPermissionChanged();
62 }
63 // Keep listening to changes.
64 getNextPermissionChange(m_executionContext, m_sensorPermission);
65 }
27 66
28 SensorProxy::~SensorProxy() {} 67 SensorProxy::~SensorProxy() {}
29 68
69 void SensorProxy::resetPermissionService() {
70 m_permissionService.reset();
71 }
72
73 PermissionService* SensorProxy::getPermissionService(
74 ExecutionContext* executionContext) {
75 if (!m_permissionService &&
Mikhail 2016/11/10 09:30:48 think we should not init it lazily, see my comment
riju_ 2016/11/14 14:00:07 Done.
76 connectToPermissionService(executionContext,
77 mojo::GetProxy(&m_permissionService))) {
78 m_permissionService.set_connection_error_handler(convertToBaseCallback(
79 WTF::bind(&SensorProxy::permissionServiceConnectionError,
80 wrapWeakPersistent(this))));
81 }
82 return m_permissionService.get();
83 }
84
85 void SensorProxy::permissionServiceConnectionError() {
86 m_permissionService.reset();
Mikhail 2016/11/10 09:30:48 this looks like a fatal error, so service reset is
riju_ 2016/11/14 14:00:06 Done.
87 }
88
30 void SensorProxy::dispose() { 89 void SensorProxy::dispose() {
31 m_clientBinding.Close(); 90 m_clientBinding.Close();
32 } 91 }
33 92
34 DEFINE_TRACE(SensorProxy) { 93 DEFINE_TRACE(SensorProxy) {
35 visitor->trace(m_reading); 94 visitor->trace(m_reading);
36 visitor->trace(m_observers); 95 visitor->trace(m_observers);
37 visitor->trace(m_provider); 96 visitor->trace(m_provider);
97 visitor->trace(m_executionContext);
38 } 98 }
39 99
40 void SensorProxy::addObserver(Observer* observer) { 100 void SensorProxy::addObserver(Observer* observer) {
41 if (!m_observers.contains(observer)) 101 if (!m_observers.contains(observer))
42 m_observers.add(observer); 102 m_observers.add(observer);
43 } 103 }
44 104
45 void SensorProxy::removeObserver(Observer* observer) { 105 void SensorProxy::removeObserver(Observer* observer) {
46 m_observers.remove(observer); 106 m_observers.remove(observer);
47 } 107 }
48 108
49 void SensorProxy::initialize() { 109 void SensorProxy::initialize() {
50 if (m_state != Uninitialized) 110 if (m_state != Uninitialized)
51 return; 111 return;
52 112
53 if (!m_provider->sensorProvider()) { 113 if (!m_provider->sensorProvider()) {
54 handleSensorError(); 114 handleSensorError();
55 return; 115 return;
56 } 116 }
117 // Get permission service.
118 PermissionService* permissionService =
119 getPermissionService(m_executionContext);
120
121 if (!permissionService) {
122 handleSensorError(
123 InvalidStateError,
124 "In its current state, the global scope can't request permissions");
125 }
126
127 // Request permission.
128 if (m_sensorPermission == PermissionStatus::ASK) {
129 setStartPendingPermission(true);
Mikhail 2016/11/10 09:30:48 why split 'setStartPendingPermission' and 'request
riju_ 2016/11/14 14:00:06 Done.
130 requestPermission(m_executionContext, m_sensorPermission);
Mikhail 2016/11/10 09:30:48 here we should interrupt and wait for permissions
riju_ 2016/11/14 14:00:06 Acknowledged.
131 }
57 132
58 m_state = Initializing; 133 m_state = Initializing;
59 auto callback = convertToBaseCallback( 134 auto callback = convertToBaseCallback(
60 WTF::bind(&SensorProxy::onSensorCreated, wrapWeakPersistent(this))); 135 WTF::bind(&SensorProxy::onSensorCreated, wrapWeakPersistent(this)));
61 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor), 136 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor),
Mikhail 2016/11/10 09:30:48 this method should be called *AFTER* permission is
riju_ 2016/11/14 14:00:07 Done.
62 callback); 137 callback);
63 } 138 }
64 139
65 void SensorProxy::addConfiguration( 140 void SensorProxy::addConfiguration(
66 SensorConfigurationPtr configuration, 141 SensorConfigurationPtr configuration,
67 std::unique_ptr<Function<void(bool)>> callback) { 142 std::unique_ptr<Function<void(bool)>> callback) {
68 DCHECK(isInitialized()); 143 DCHECK(isInitialized());
69 m_sensor->AddConfiguration(std::move(configuration), 144 m_sensor->AddConfiguration(std::move(configuration),
70 convertToBaseCallback(std::move(callback))); 145 convertToBaseCallback(std::move(callback)));
71 } 146 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 195
121 void SensorProxy::RaiseError() { 196 void SensorProxy::RaiseError() {
122 handleSensorError(); 197 handleSensorError();
123 } 198 }
124 199
125 void SensorProxy::SensorReadingChanged() { 200 void SensorProxy::SensorReadingChanged() {
126 for (Observer* observer : m_observers) 201 for (Observer* observer : m_observers)
127 observer->onSensorReadingChanged(); 202 observer->onSensorReadingChanged();
128 } 203 }
129 204
205 void SensorProxy::SensorPermissionChanged() {
206 for (Observer* observer : m_observers)
207 observer->onSensorPermissionChanged();
208 }
209
130 void SensorProxy::handleSensorError(ExceptionCode code, 210 void SensorProxy::handleSensorError(ExceptionCode code,
131 String sanitizedMessage, 211 String sanitizedMessage,
132 String unsanitizedMessage) { 212 String unsanitizedMessage) {
133 if (!Platform::current()) { 213 if (!Platform::current()) {
134 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. 214 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed.
135 return; 215 return;
136 } 216 }
137 217
138 m_state = Uninitialized; 218 m_state = Uninitialized;
139 // The m_sensor.reset() will release all callbacks and its bound parameters, 219 // The m_sensor.reset() will release all callbacks and its bound parameters,
140 // therefore, handleSensorError accepts messages by value. 220 // therefore, handleSensorError accepts messages by value.
141 m_sensor.reset(); 221 m_sensor.reset();
142 m_sharedBuffer.reset(); 222 m_sharedBuffer.reset();
143 m_sharedBufferHandle.reset(); 223 m_sharedBufferHandle.reset();
144 m_defaultConfig.reset(); 224 m_defaultConfig.reset();
145 m_clientBinding.Close(); 225 m_clientBinding.Close();
146 m_reading = nullptr; 226 m_reading = nullptr;
227 m_permissionService.reset();
147 228
148 for (Observer* observer : m_observers) 229 for (Observer* observer : m_observers)
149 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage); 230 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage);
150 } 231 }
151 232
152 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, 233 void SensorProxy::onSensorCreated(SensorInitParamsPtr params,
153 SensorClientRequest clientRequest) { 234 SensorClientRequest clientRequest) {
154 DCHECK_EQ(Initializing, m_state); 235 DCHECK_EQ(Initializing, m_state);
155 if (!params) { 236 if (!params) {
156 handleSensorError(NotFoundError, "Sensor is not present on the platform."); 237 handleSensorError(NotFoundError, "Sensor is not present on the platform.");
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); 279 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value();
199 auto version = seqlock.ReadBegin(); 280 auto version = seqlock.ReadBegin();
200 auto readingData = buffer->reading; 281 auto readingData = buffer->reading;
201 if (seqlock.ReadRetry(version)) 282 if (seqlock.ReadRetry(version))
202 return false; 283 return false;
203 result = readingData; 284 result = readingData;
204 return true; 285 return true;
205 } 286 }
206 287
207 } // namespace blink 288 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698