Device orientation's permission should only require a user gesture to prompt the...
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 14 Mar 2019 18:57:58 +0000 (18:57 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 14 Mar 2019 18:57:58 +0000 (18:57 +0000)
https://bugs.webkit.org/show_bug.cgi?id=195731

Reviewed by Geoffrey Garen.

Source/WebCore:

Device orientation's permission should only require a user gesture to prompt the user. If the
user already made a decision, we should resolve the promise with this decision, even without
user gesture.

This is useful for JS to figure out if they are access to device orientation or not because
showing UI for the user to give permission.

No new tests, updated existing tests.

* dom/DeviceOrientationAndMotionAccessController.cpp:
(WebCore::DeviceOrientationAndMotionAccessController::shouldAllowAccess):
* dom/DeviceOrientationAndMotionAccessController.h:
* dom/DeviceOrientationOrMotionEvent.cpp:
(WebCore::DeviceOrientationOrMotionEvent::requestPermission):

LayoutTests:

Update layout tests accordingly.

* fast/device-orientation/device-motion-request-permission-denied.html:
* fast/device-orientation/device-motion-request-permission-granted.html:
* fast/device-orientation/device-motion-request-permission-user-gesture-expected.txt:
* fast/device-orientation/device-orientation-request-permission-denied.html:
* fast/device-orientation/device-orientation-request-permission-granted.html:
* fast/device-orientation/device-orientation-request-permission-user-gesture-expected.txt:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242951 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/fast/device-orientation/device-motion-request-permission-denied.html
LayoutTests/fast/device-orientation/device-motion-request-permission-granted.html
LayoutTests/fast/device-orientation/device-motion-request-permission-user-gesture-expected.txt
LayoutTests/fast/device-orientation/device-orientation-request-permission-denied.html
LayoutTests/fast/device-orientation/device-orientation-request-permission-granted.html
LayoutTests/fast/device-orientation/device-orientation-request-permission-user-gesture-expected.txt
Source/WebCore/ChangeLog
Source/WebCore/dom/DeviceOrientationAndMotionAccessController.cpp
Source/WebCore/dom/DeviceOrientationAndMotionAccessController.h
Source/WebCore/dom/DeviceOrientationOrMotionEvent.cpp

index 5343056..9cf8f5f 100644 (file)
@@ -1,3 +1,19 @@
+2019-03-14  Chris Dumez  <cdumez@apple.com>
+
+        Device orientation's permission should only require a user gesture to prompt the user
+        https://bugs.webkit.org/show_bug.cgi?id=195731
+
+        Reviewed by Geoffrey Garen.
+
+        Update layout tests accordingly.
+
+        * fast/device-orientation/device-motion-request-permission-denied.html:
+        * fast/device-orientation/device-motion-request-permission-granted.html:
+        * fast/device-orientation/device-motion-request-permission-user-gesture-expected.txt:
+        * fast/device-orientation/device-orientation-request-permission-denied.html:
+        * fast/device-orientation/device-orientation-request-permission-granted.html:
+        * fast/device-orientation/device-orientation-request-permission-user-gesture-expected.txt:
+
 2019-03-14  Dean Jackson  <dino@apple.com>
 
         Block all plugins smaller than 5x5px
index 30cd0a2..fb358d6 100644 (file)
@@ -18,12 +18,10 @@ internals.withUserGesture(() => {
 
         addEventListener("devicemotion", () => {});
 
-        internals.withUserGesture(() => {
-            DeviceMotionEvent.requestPermission().then((_result) => {
-                result = _result;
-                shouldBeEqualToString("result", "denied");
-                finishJSTest();
-            });
+        DeviceMotionEvent.requestPermission().then((_result) => {
+            result = _result;
+            shouldBeEqualToString("result", "denied");
+            finishJSTest();
         });
     });
 });
index cdc11e9..ccd9b78 100644 (file)
@@ -18,12 +18,10 @@ internals.withUserGesture(() => {
 
         addEventListener("devicemotion", () => {});
 
-        internals.withUserGesture(() => {
-            DeviceMotionEvent.requestPermission().then((_result) => {
-                result = _result;
-                shouldBeEqualToString("result", "granted");
-                finishJSTest();
-            });
+        DeviceMotionEvent.requestPermission().then((_result) => {
+            result = _result;
+            shouldBeEqualToString("result", "granted");
+            finishJSTest();
         });
     });
 });
index 90cf576..89ce6b5 100644 (file)
@@ -1,4 +1,4 @@
-CONSOLE MESSAGE: line 14: NotAllowedError: Calling requestPermission() requires a user gesture
+CONSOLE MESSAGE: line 14: NotAllowedError: Requesting device orientation or motion access requires a user gesture
 Tests that DeviceMotionEvent.requestPermission() requires a user gesture.
 
 On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
index 938d73c..4969fbc 100644 (file)
@@ -18,12 +18,10 @@ internals.withUserGesture(() => {
 
         addEventListener("deviceorientation", () => {});
 
-        internals.withUserGesture(() => {
-            DeviceOrientationEvent.requestPermission().then((_result) => {
-                result = _result;
-                shouldBeEqualToString("result", "denied");
-                finishJSTest();
-            });
+        DeviceOrientationEvent.requestPermission().then((_result) => {
+            result = _result;
+            shouldBeEqualToString("result", "denied");
+            finishJSTest();
         });
     });
 });
index c41badb..8bb6fc2 100644 (file)
@@ -18,12 +18,10 @@ internals.withUserGesture(() => {
 
         addEventListener("deviceorientation", () => {});
 
-        internals.withUserGesture(() => {
-            DeviceOrientationEvent.requestPermission().then((_result) => {
-                result = _result;
-                shouldBeEqualToString("result", "granted");
-                finishJSTest();
-            });
+        DeviceOrientationEvent.requestPermission().then((_result) => {
+            result = _result;
+            shouldBeEqualToString("result", "granted");
+            finishJSTest();
         });
     });
 });
index bab426a..6093b24 100644 (file)
@@ -1,4 +1,4 @@
-CONSOLE MESSAGE: line 14: NotAllowedError: Calling requestPermission() requires a user gesture
+CONSOLE MESSAGE: line 14: NotAllowedError: Requesting device orientation or motion access requires a user gesture
 Tests that DeviceOrientationEvent.requestPermission() requires a user gesture.
 
 On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
index 74540ad..954ad4d 100644 (file)
@@ -1,3 +1,25 @@
+2019-03-14  Chris Dumez  <cdumez@apple.com>
+
+        Device orientation's permission should only require a user gesture to prompt the user
+        https://bugs.webkit.org/show_bug.cgi?id=195731
+
+        Reviewed by Geoffrey Garen.
+
+        Device orientation's permission should only require a user gesture to prompt the user. If the
+        user already made a decision, we should resolve the promise with this decision, even without
+        user gesture.
+
+        This is useful for JS to figure out if they are access to device orientation or not because
+        showing UI for the user to give permission.
+
+        No new tests, updated existing tests.
+
+        * dom/DeviceOrientationAndMotionAccessController.cpp:
+        (WebCore::DeviceOrientationAndMotionAccessController::shouldAllowAccess):
+        * dom/DeviceOrientationAndMotionAccessController.h:
+        * dom/DeviceOrientationOrMotionEvent.cpp:
+        (WebCore::DeviceOrientationOrMotionEvent::requestPermission):
+
 2019-03-14  Youenn Fablet  <youenn@apple.com>
 
         Reset storage quota when clearing IDB/Cache API entries
index bddfdbb..2b69849 100644 (file)
@@ -34,6 +34,7 @@
 #include "Document.h"
 #include "Frame.h"
 #include "Page.h"
+#include "UserGestureIndicator.h"
 
 namespace WebCore {
 
@@ -43,11 +44,14 @@ DeviceOrientationAndMotionAccessController::DeviceOrientationAndMotionAccessCont
     ASSERT(&m_document.topDocument() == &m_document);
 }
 
-void DeviceOrientationAndMotionAccessController::shouldAllowAccess(Function<void(bool granted)>&& callback)
+void DeviceOrientationAndMotionAccessController::shouldAllowAccess(Function<void(ExceptionOr<bool> granted)>&& callback)
 {
     if (m_accessState)
         return callback(*m_accessState);
 
+    if (!UserGestureIndicator::processingUserGesture())
+        return callback(Exception { NotAllowedError, "Requesting device orientation or motion access requires a user gesture"_s });
+
     auto* page = m_document.page();
     if (!page)
         return callback(false);
index 71fc792..4a1de91 100644 (file)
@@ -27,6 +27,7 @@
 
 #if ENABLE(DEVICE_ORIENTATION)
 
+#include "ExceptionOr.h"
 #include <wtf/Function.h>
 #include <wtf/Vector.h>
 #include <wtf/WeakPtr.h>
@@ -41,7 +42,7 @@ public:
     explicit DeviceOrientationAndMotionAccessController(Document&);
 
     const Optional<bool>& accessState() const { return m_accessState; }
-    void shouldAllowAccess(Function<void(bool granted)>&&);
+    void shouldAllowAccess(Function<void(ExceptionOr<bool> granted)>&&);
 
 private:
     void setAccessState(bool);
index ae09616..41cf6fe 100644 (file)
@@ -27,7 +27,6 @@
 #include "DeviceOrientationOrMotionEvent.h"
 
 #include "Document.h"
-#include "UserGestureIndicator.h"
 
 namespace WebCore {
 
@@ -38,17 +37,16 @@ void DeviceOrientationOrMotionEvent::requestPermission(Document& document, Permi
     if (!window)
         return promise.reject(Exception { InvalidStateError, "No browsing context"_s });
 
-    if (!UserGestureIndicator::processingUserGesture())
-        return promise.reject(Exception { NotAllowedError, "Calling requestPermission() requires a user gesture"_s });
-
     String errorMessage;
     if (!window->isAllowedToUseDeviceMotionOrientation(errorMessage)) {
         document.addConsoleMessage(MessageSource::JS, MessageLevel::Warning, makeString("Call to requestPermission() failed, reason: ", errorMessage, "."));
         return promise.resolve(PermissionState::Denied);
     }
 
-    document.deviceOrientationAndMotionAccessController().shouldAllowAccess([promise = WTFMove(promise)](bool granted) mutable {
-        promise.resolve(granted ? PermissionState::Granted : PermissionState::Denied);
+    document.deviceOrientationAndMotionAccessController().shouldAllowAccess([promise = WTFMove(promise)](ExceptionOr<bool> granted) mutable {
+        if (granted.hasException())
+            return promise.reject(granted.releaseException());
+        promise.resolve(granted.returnValue() ? PermissionState::Granted : PermissionState::Denied);
     });
 }
 #endif