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

Unified Diff: third_party/WebKit/Source/modules/sensor/Sensor.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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/sensor/Sensor.cpp
diff --git a/third_party/WebKit/Source/modules/sensor/Sensor.cpp b/third_party/WebKit/Source/modules/sensor/Sensor.cpp
index c9f902d42ad470d7f9d58be39494524c9fa69d63..845587369ec27ee0952d5d02ec0b7aa69fee1fd1 100644
--- a/third_party/WebKit/Source/modules/sensor/Sensor.cpp
+++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp
@@ -13,11 +13,14 @@
#include "modules/sensor/SensorPollingStrategy.h"
#include "modules/sensor/SensorProviderProxy.h"
#include "modules/sensor/SensorReading.h"
+#include "platform/UserGestureIndicator.h"
using namespace device::mojom::blink;
namespace blink {
+using mojom::blink::PermissionStatus;
+
Sensor::Sensor(ScriptState* scriptState,
const SensorOptions& sensorOptions,
ExceptionState& exceptionState,
@@ -72,15 +75,47 @@ void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) {
return;
}
- initSensorProxyIfNeeded();
+ // If the algorithm is not allowed to show a popup, throw SecurityError.
+ if (!UserGestureIndicator::consumeUserGesture()) {
+ exceptionState.throwDOMException(
+ SecurityError,
+ "Must be handling a user gesture to show a permission request.");
+ return;
+ }
+ initSensorProxyIfNeeded();
if (!m_sensorProxy) {
exceptionState.throwDOMException(
InvalidStateError, "The Sensor is no longer associated to a frame.");
return;
}
- startListening();
+ m_sensorProxy->addObserver(this);
+ if (!m_sensorProxy->isInitialized()) {
+ m_sensorProxy->initialize();
+ return;
+ }
+
+ // Check the permission status.
+ auto permissionStatus = m_sensorProxy->getPermissionStatus();
+
+ if (permissionStatus == PermissionStatus::ASK) {
+ m_sensorProxy->setStartPendingPermission(true);
+ m_sensorProxy->requestPermission(scriptState->getExecutionContext(),
+ permissionStatus);
+ return;
+ }
+ if (permissionStatus == PermissionStatus::GRANTED) {
+ startListening();
+ } else if (permissionStatus == PermissionStatus::DENIED) {
+ stopListening();
Mikhail 2016/11/10 09:30:48 how can be sensor listening in 'idle' or 'errored'
riju_ 2016/11/14 14:00:06 Done.
+
+ ConsoleMessage* consoleMessage = ConsoleMessage::create(
+ JSMessageSource, InfoMessageLevel, "Permission Denied.");
+ scriptState->getExecutionContext()->addConsoleMessage(consoleMessage);
+
+ exceptionState.throwDOMException(NotAllowedError, "Permission Denied.");
+ }
}
void Sensor::stop(ScriptState*, ExceptionState& exceptionState) {
@@ -150,8 +185,8 @@ void Sensor::initSensorProxyIfNeeded() {
m_sensorProxy = provider->getSensor(m_type);
if (!m_sensorProxy) {
- m_sensorProxy =
- provider->createSensor(m_type, createSensorReadingFactory());
+ m_sensorProxy = provider->createSensor(m_type, getExecutionContext(),
+ createSensorReadingFactory());
}
}
@@ -159,6 +194,7 @@ void Sensor::contextDestroyed() {
if (m_state == Sensor::SensorState::ACTIVE ||
m_state == Sensor::SensorState::ACTIVATING)
stopListening();
+ m_sensorProxy->resetPermissionService();
Mikhail 2016/11/10 09:30:48 should be handled by proxy itself
riju_ 2016/11/14 14:00:06 Done.
}
void Sensor::onSensorInitialized() {
@@ -173,6 +209,22 @@ void Sensor::onSensorReadingChanged() {
m_polling->onSensorReadingChanged();
}
+void Sensor::onSensorPermissionChanged() {
Mikhail 2016/11/10 09:30:48 all this logic should be inside proxy itself
riju_ 2016/11/14 14:00:06 Done.
+ // Check the permission status.
+ auto permissionStatus = m_sensorProxy->getPermissionStatus();
+
+ if (permissionStatus != PermissionStatus::GRANTED) {
+ stopListening();
+ reportError(NotAllowedError,
+ "start/stop() call has failed as permission was denied.");
+ } else {
+ if (m_sensorProxy->getStartPendingPermission()) {
+ startListening();
+ m_sensorProxy->setStartPendingPermission(false);
+ }
+ }
+}
+
void Sensor::onSensorError(ExceptionCode code,
const String& sanitizedMessage,
const String& unsanitizedMessage) {
@@ -228,12 +280,6 @@ void Sensor::startListening() {
DCHECK(m_sensorProxy);
updateState(Sensor::SensorState::ACTIVATING);
- m_sensorProxy->addObserver(this);
- if (!m_sensorProxy->isInitialized()) {
- m_sensorProxy->initialize();
- return;
- }
-
if (!m_configuration) {
m_configuration =
createSensorConfig(m_sensorOptions, *m_sensorProxy->defaultConfig());

Powered by Google App Engine
This is Rietveld 408576698