Add WebsitePolicy for the client to specify the device orientation & motion access...
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 14 Mar 2019 20:40:09 +0000 (20:40 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 14 Mar 2019 20:40:09 +0000 (20:40 +0000)
https://bugs.webkit.org/show_bug.cgi?id=195750

Reviewed by Geoffrey Garen.

Source/WebCore:

Add WebsitePolicy for the client to specify the device orientation & motion access policy. If
the client already knows access to the device motion & orientation API will be granted / denied,
it can let WebKit know via WebsitePolicies so that WebKit will not ask the client when the
permission is requested by JS.

* dom/DeviceOrientationAndMotionAccessController.cpp:
(WebCore::DeviceOrientationAndMotionAccessController::shouldAllowAccess):
(WebCore::DeviceOrientationAndMotionAccessController::setAccessState):
(WebCore::DeviceOrientationAndMotionAccessController::accessState const):
* dom/DeviceOrientationAndMotionAccessController.h:
* loader/DocumentLoader.h:
(WebCore::DocumentLoader::deviceOrientationAndMotionAccessState const):
(WebCore::DocumentLoader::setDeviceOrientationAndMotionAccessState):
* page/DOMWindow.cpp:
(WebCore::DOMWindow::isAllowedToUseDeviceMotionOrientation const):

Source/WebKit:

Add WebsitePolicy for the client to specify the device orientation & motion access policy. If
the client already knows access to the device motion & orientation API will be granted / denied,
it can let WebKit know via WebsitePolicies so that WebKit will not ask the client when the
permission is requested by JS.

* Shared/WebsitePoliciesData.cpp:
(WebKit::WebsitePoliciesData::encode const):
(WebKit::WebsitePoliciesData::decode):
(WebKit::WebsitePoliciesData::applyToDocumentLoader):
* Shared/WebsitePoliciesData.h:
* UIProcess/API/APIWebsitePolicies.cpp:
(API::WebsitePolicies::data):
* UIProcess/API/APIWebsitePolicies.h:
* UIProcess/API/Cocoa/_WKWebsitePolicies.h:
* UIProcess/API/Cocoa/_WKWebsitePolicies.mm:
(-[_WKWebsitePolicies setDeviceOrientationAndMotionAccessPolicy:]):
(-[_WKWebsitePolicies deviceOrientationAndMotionAccessPolicy]):

Tools:

Add API test coverage.

* TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm:
(-[WebsitePoliciesDeviceOrientationDelegate initWithDeviceOrientationAccessPolicy:]):
(-[WebsitePoliciesDeviceOrientationDelegate _webView:decidePolicyForNavigationAction:userInfo:decisionHandler:]):
(-[WebsitePoliciesDeviceOrientationUIDelegate _webView:shouldAllowDeviceOrientationAndMotionAccessRequestedByFrame:decisionHandler:]):

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

14 files changed:
Source/WebCore/ChangeLog
Source/WebCore/dom/DeviceOrientationAndMotionAccessController.cpp
Source/WebCore/dom/DeviceOrientationAndMotionAccessController.h
Source/WebCore/loader/DocumentLoader.h
Source/WebCore/page/DOMWindow.cpp
Source/WebKit/ChangeLog
Source/WebKit/Shared/WebsitePoliciesData.cpp
Source/WebKit/Shared/WebsitePoliciesData.h
Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp
Source/WebKit/UIProcess/API/APIWebsitePolicies.h
Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.h
Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.mm
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm

index 5c5a794..8bff583 100644 (file)
@@ -1,3 +1,26 @@
+2019-03-14  Chris Dumez  <cdumez@apple.com>
+
+        Add WebsitePolicy for the client to specify the device orientation & motion access policy
+        https://bugs.webkit.org/show_bug.cgi?id=195750
+
+        Reviewed by Geoffrey Garen.
+
+        Add WebsitePolicy for the client to specify the device orientation & motion access policy. If
+        the client already knows access to the device motion & orientation API will be granted / denied,
+        it can let WebKit know via WebsitePolicies so that WebKit will not ask the client when the
+        permission is requested by JS.
+
+        * dom/DeviceOrientationAndMotionAccessController.cpp:
+        (WebCore::DeviceOrientationAndMotionAccessController::shouldAllowAccess):
+        (WebCore::DeviceOrientationAndMotionAccessController::setAccessState):
+        (WebCore::DeviceOrientationAndMotionAccessController::accessState const):
+        * dom/DeviceOrientationAndMotionAccessController.h:
+        * loader/DocumentLoader.h:
+        (WebCore::DocumentLoader::deviceOrientationAndMotionAccessState const):
+        (WebCore::DocumentLoader::setDeviceOrientationAndMotionAccessState):
+        * page/DOMWindow.cpp:
+        (WebCore::DOMWindow::isAllowedToUseDeviceMotionOrientation const):
+
 2019-03-14  Shawn Roberts  <sroberts@apple.com>
 
         Unreviewed, rolling out r242931.
index 2b69849..d86c160 100644 (file)
@@ -32,6 +32,7 @@
 #include "ChromeClient.h"
 #include "DOMWindow.h"
 #include "Document.h"
+#include "DocumentLoader.h"
 #include "Frame.h"
 #include "Page.h"
 #include "UserGestureIndicator.h"
@@ -46,8 +47,9 @@ DeviceOrientationAndMotionAccessController::DeviceOrientationAndMotionAccessCont
 
 void DeviceOrientationAndMotionAccessController::shouldAllowAccess(Function<void(ExceptionOr<bool> granted)>&& callback)
 {
-    if (m_accessState)
-        return callback(*m_accessState);
+    if (auto accessState = this->accessState())
+        return callback(*accessState);
+    ASSERT(m_document.frame());
 
     if (!UserGestureIndicator::processingUserGesture())
         return callback(Exception { NotAllowedError, "Requesting device orientation or motion access requires a user gesture"_s });
@@ -56,15 +58,11 @@ void DeviceOrientationAndMotionAccessController::shouldAllowAccess(Function<void
     if (!page)
         return callback(false);
 
-    auto* frame = m_document.frame();
-    if (!frame)
-        return callback(false);
-
     m_pendingRequests.append(WTFMove(callback));
     if (m_pendingRequests.size() > 1)
         return;
 
-    page->chrome().client().shouldAllowDeviceOrientationAndMotionAccess(*frame, [this, weakThis = makeWeakPtr(*this)](bool granted) mutable {
+    page->chrome().client().shouldAllowDeviceOrientationAndMotionAccess(*m_document.frame(), [this, weakThis = makeWeakPtr(*this)](bool granted) mutable {
         if (weakThis)
             setAccessState(granted);
     });
@@ -72,9 +70,9 @@ void DeviceOrientationAndMotionAccessController::shouldAllowAccess(Function<void
 
 void DeviceOrientationAndMotionAccessController::setAccessState(bool granted)
 {
-    ASSERT(!m_accessState);
-
-    m_accessState = granted;
+    auto* frame = m_document.frame();
+    if (frame && frame->loader().documentLoader())
+        frame->loader().documentLoader()->setDeviceOrientationAndMotionAccessState(granted);
 
     auto pendingRequests = WTFMove(m_pendingRequests);
     for (auto& request : pendingRequests)
@@ -89,6 +87,12 @@ void DeviceOrientationAndMotionAccessController::setAccessState(bool granted)
     }
 }
 
+Optional<bool> DeviceOrientationAndMotionAccessController::accessState() const
+{
+    auto* frame = m_document.frame();
+    return frame && frame->loader().documentLoader() ? frame->loader().documentLoader()->deviceOrientationAndMotionAccessState() : false;
+}
+
 } // namespace WebCore
 
 #endif // ENABLE(DEVICE_ORIENTATION)
index 4a1de91..e599cad 100644 (file)
@@ -41,14 +41,13 @@ class DeviceOrientationAndMotionAccessController : public CanMakeWeakPtr<DeviceO
 public:
     explicit DeviceOrientationAndMotionAccessController(Document&);
 
-    const Optional<bool>& accessState() const { return m_accessState; }
+    Optional<bool> accessState() const;
     void shouldAllowAccess(Function<void(ExceptionOr<bool> granted)>&&);
 
 private:
     void setAccessState(bool);
 
     Document& m_document;
-    Optional<bool> m_accessState;
     Vector<Function<void(bool)>> m_pendingRequests;
 };
 
index 81c6048..019d1b5 100644 (file)
@@ -262,8 +262,8 @@ public:
     bool userContentExtensionsEnabled() const { return m_userContentExtensionsEnabled; }
     void setUserContentExtensionsEnabled(bool enabled) { m_userContentExtensionsEnabled = enabled; }
 
-    bool deviceOrientationEventEnabled() const { return m_deviceOrientationEventEnabled; }
-    void setDeviceOrientationEventEnabled(bool enabled) { m_deviceOrientationEventEnabled = enabled; }
+    const Optional<bool>& deviceOrientationAndMotionAccessState() const { return m_deviceOrientationAndMotionAccessState; }
+    void setDeviceOrientationAndMotionAccessState(const Optional<bool>& state) { m_deviceOrientationAndMotionAccessState = state; }
 
     AutoplayPolicy autoplayPolicy() const { return m_autoplayPolicy; }
     void setAutoplayPolicy(AutoplayPolicy policy) { m_autoplayPolicy = policy; }
@@ -552,7 +552,7 @@ private:
     String m_customJavaScriptUserAgentAsSiteSpecificQuirks;
     String m_customNavigatorPlatform;
     bool m_userContentExtensionsEnabled { true };
-    bool m_deviceOrientationEventEnabled { true };
+    Optional<bool> m_deviceOrientationAndMotionAccessState;
     AutoplayPolicy m_autoplayPolicy { AutoplayPolicy::Default };
     OptionSet<AutoplayQuirk> m_allowedAutoplayQuirks;
     PopUpPolicy m_popUpPolicy { PopUpPolicy::Default };
index 8c31dbd..4d7b3af 100644 (file)
@@ -1840,7 +1840,7 @@ DeviceMotionController* DOMWindow::deviceMotionController() const
 
 bool DOMWindow::isAllowedToUseDeviceMotionOrientation(String& message) const
 {
-    if (!frame() || !frame()->settings().deviceOrientationEventEnabled() || !document() || !document()->loader() || !document()->loader()->deviceOrientationEventEnabled()) {
+    if (!frame() || !frame()->settings().deviceOrientationEventEnabled()) {
         message = "API is disabled"_s;
         return false;
     }
index c7b272c..17b4fed 100644 (file)
@@ -1,3 +1,28 @@
+2019-03-14  Chris Dumez  <cdumez@apple.com>
+
+        Add WebsitePolicy for the client to specify the device orientation & motion access policy
+        https://bugs.webkit.org/show_bug.cgi?id=195750
+
+        Reviewed by Geoffrey Garen.
+
+        Add WebsitePolicy for the client to specify the device orientation & motion access policy. If
+        the client already knows access to the device motion & orientation API will be granted / denied,
+        it can let WebKit know via WebsitePolicies so that WebKit will not ask the client when the
+        permission is requested by JS.
+
+        * Shared/WebsitePoliciesData.cpp:
+        (WebKit::WebsitePoliciesData::encode const):
+        (WebKit::WebsitePoliciesData::decode):
+        (WebKit::WebsitePoliciesData::applyToDocumentLoader):
+        * Shared/WebsitePoliciesData.h:
+        * UIProcess/API/APIWebsitePolicies.cpp:
+        (API::WebsitePolicies::data):
+        * UIProcess/API/APIWebsitePolicies.h:
+        * UIProcess/API/Cocoa/_WKWebsitePolicies.h:
+        * UIProcess/API/Cocoa/_WKWebsitePolicies.mm:
+        (-[_WKWebsitePolicies setDeviceOrientationAndMotionAccessPolicy:]):
+        (-[_WKWebsitePolicies deviceOrientationAndMotionAccessPolicy]):
+
 2019-03-14  Timothy Hatcher  <timothy@apple.com>
 
         REGRESSION (r242908): TestWebKitAPI.WebKit.AddAndRemoveDataDetectors Crashed
index 6b3b02d..a885289 100644 (file)
@@ -37,8 +37,8 @@ namespace WebKit {
 void WebsitePoliciesData::encode(IPC::Encoder& encoder) const
 {
     encoder << contentBlockersEnabled;
-    encoder << deviceOrientationEventEnabled;
     encoder << autoplayPolicy;
+    encoder << deviceOrientationAndMotionAccessState;
     encoder << allowedAutoplayQuirks;
     encoder << customHeaderFields;
     encoder << popUpPolicy;
@@ -54,16 +54,16 @@ Optional<WebsitePoliciesData> WebsitePoliciesData::decode(IPC::Decoder& decoder)
     decoder >> contentBlockersEnabled;
     if (!contentBlockersEnabled)
         return WTF::nullopt;
-
-    Optional<bool> deviceOrientationEventEnabled;
-    decoder >> deviceOrientationEventEnabled;
-    if (!deviceOrientationEventEnabled)
-        return WTF::nullopt;
     
     Optional<WebsiteAutoplayPolicy> autoplayPolicy;
     decoder >> autoplayPolicy;
     if (!autoplayPolicy)
         return WTF::nullopt;
+
+    Optional<Optional<bool>> deviceOrientationAndMotionAccessState;
+    decoder >> deviceOrientationAndMotionAccessState;
+    if (!deviceOrientationAndMotionAccessState)
+        return WTF::nullopt;
     
     Optional<OptionSet<WebsiteAutoplayQuirk>> allowedAutoplayQuirks;
     decoder >> allowedAutoplayQuirks;
@@ -102,9 +102,9 @@ Optional<WebsitePoliciesData> WebsitePoliciesData::decode(IPC::Decoder& decoder)
     
     return { {
         WTFMove(*contentBlockersEnabled),
-        WTFMove(*deviceOrientationEventEnabled),
         WTFMove(*allowedAutoplayQuirks),
         WTFMove(*autoplayPolicy),
+        WTFMove(*deviceOrientationAndMotionAccessState),
         WTFMove(*customHeaderFields),
         WTFMove(*popUpPolicy),
         WTFMove(*websiteDataStoreParameters),
@@ -120,7 +120,7 @@ void WebsitePoliciesData::applyToDocumentLoader(WebsitePoliciesData&& websitePol
     documentLoader.setCustomUserAgent(websitePolicies.customUserAgent);
     documentLoader.setCustomJavaScriptUserAgentAsSiteSpecificQuirks(websitePolicies.customJavaScriptUserAgentAsSiteSpecificQuirks);
     documentLoader.setCustomNavigatorPlatform(websitePolicies.customNavigatorPlatform);
-    documentLoader.setDeviceOrientationEventEnabled(websitePolicies.deviceOrientationEventEnabled);
+    documentLoader.setDeviceOrientationAndMotionAccessState(websitePolicies.deviceOrientationAndMotionAccessState);
     
     // Only setUserContentExtensionsEnabled if it hasn't already been disabled by reloading without content blockers.
     if (documentLoader.userContentExtensionsEnabled())
index c2711a9..d3dd005 100644 (file)
@@ -47,9 +47,9 @@ struct WebsitePoliciesData {
     static void applyToDocumentLoader(WebsitePoliciesData&&, WebCore::DocumentLoader&);
 
     bool contentBlockersEnabled { true };
-    bool deviceOrientationEventEnabled { true };
     OptionSet<WebsiteAutoplayQuirk> allowedAutoplayQuirks;
     WebsiteAutoplayPolicy autoplayPolicy { WebsiteAutoplayPolicy::Default };
+    Optional<bool> deviceOrientationAndMotionAccessState;
     Vector<WebCore::HTTPHeaderField> customHeaderFields;
     WebsitePopUpPolicy popUpPolicy { WebsitePopUpPolicy::Default };
     Optional<WebsiteDataStoreParameters> websiteDataStoreParameters;
index c63ce06..86fcac0 100644 (file)
@@ -56,7 +56,7 @@ WebKit::WebsitePoliciesData WebsitePolicies::data()
     Optional<WebKit::WebsiteDataStoreParameters> parameters;
     if (m_websiteDataStore)
         parameters = m_websiteDataStore->websiteDataStore().parameters();
-    return { contentBlockersEnabled(), deviceOrientationEventEnabled(), allowedAutoplayQuirks(), autoplayPolicy(),
+    return { contentBlockersEnabled(), allowedAutoplayQuirks(), autoplayPolicy(), deviceOrientationAndMotionAccessState(),
         customHeaderFields(), popUpPolicy(), WTFMove(parameters), m_customUserAgent, m_customJavaScriptUserAgentAsSiteSpecificQuirks, m_customNavigatorPlatform };
 }
 
index ea98d25..7eda19d 100644 (file)
@@ -49,15 +49,15 @@ public:
 
     bool contentBlockersEnabled() const { return m_contentBlockersEnabled; }
     void setContentBlockersEnabled(bool enabled) { m_contentBlockersEnabled = enabled; }
-
-    bool deviceOrientationEventEnabled() const { return m_deviceOrientationEventEnabled; }
-    void setDeviceOrientationEventEnabled(bool enabled) { m_deviceOrientationEventEnabled = enabled; }
     
     OptionSet<WebKit::WebsiteAutoplayQuirk> allowedAutoplayQuirks() const { return m_allowedAutoplayQuirks; }
     void setAllowedAutoplayQuirks(OptionSet<WebKit::WebsiteAutoplayQuirk> quirks) { m_allowedAutoplayQuirks = quirks; }
     
     WebKit::WebsiteAutoplayPolicy autoplayPolicy() const { return m_autoplayPolicy; }
     void setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy policy) { m_autoplayPolicy = policy; }
+
+    const Optional<bool>& deviceOrientationAndMotionAccessState() const { return m_deviceOrientationAndMotionAccessState; }
+    void setDeviceOrientationAndMotionAccessState(Optional<bool> state) { m_deviceOrientationAndMotionAccessState = state; }
     
     const Vector<WebCore::HTTPHeaderField>& customHeaderFields() const { return m_customHeaderFields; }
     Vector<WebCore::HTTPHeaderField>&& takeCustomHeaderFields() { return WTFMove(m_customHeaderFields); }
@@ -84,9 +84,9 @@ private:
     WebsitePolicies(bool contentBlockersEnabled, OptionSet<WebKit::WebsiteAutoplayQuirk>, WebKit::WebsiteAutoplayPolicy, Vector<WebCore::HTTPHeaderField>&&, WebKit::WebsitePopUpPolicy, RefPtr<WebsiteDataStore>&&);
 
     bool m_contentBlockersEnabled { true };
-    bool m_deviceOrientationEventEnabled { true };
     OptionSet<WebKit::WebsiteAutoplayQuirk> m_allowedAutoplayQuirks;
     WebKit::WebsiteAutoplayPolicy m_autoplayPolicy { WebKit::WebsiteAutoplayPolicy::Default };
+    Optional<bool> m_deviceOrientationAndMotionAccessState;
     Vector<WebCore::HTTPHeaderField> m_customHeaderFields;
     WebKit::WebsitePopUpPolicy m_popUpPolicy { WebKit::WebsitePopUpPolicy::Default };
     RefPtr<WebsiteDataStore> m_websiteDataStore;
index bab68b2..34a3069 100644 (file)
@@ -45,6 +45,12 @@ typedef NS_OPTIONS(NSUInteger, _WKWebsitePopUpPolicy) {
     _WKWebsitePopUpPolicyBlock,
 } WK_API_AVAILABLE(macosx(10.14), ios(12.0));
 
+typedef NS_OPTIONS(NSUInteger, _WKWebsiteDeviceOrientationAndMotionAccessPolicy) {
+    _WKWebsiteDeviceOrientationAndMotionAccessPolicyAsk,
+    _WKWebsiteDeviceOrientationAndMotionAccessPolicyGrant,
+    _WKWebsiteDeviceOrientationAndMotionAccessPolicyDeny,
+} WK_API_AVAILABLE(macosx(10.14), ios(12.0));
+
 @class WKWebsiteDataStore;
 
 WK_CLASS_AVAILABLE(macosx(10.12.3), ios(10.3))
@@ -59,6 +65,6 @@ WK_CLASS_AVAILABLE(macosx(10.12.3), ios(10.3))
 @property (nonatomic, copy) NSString *customUserAgent WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
 @property (nonatomic, copy) NSString *customJavaScriptUserAgentAsSiteSpecificQuirks WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
 @property (nonatomic, copy) NSString *customNavigatorPlatform WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
-@property (nonatomic) BOOL deviceOrientationEventEnabled WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
+@property (nonatomic) _WKWebsiteDeviceOrientationAndMotionAccessPolicy deviceOrientationAndMotionAccessPolicy WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
 
 @end
index 6b780e3..d5e9c91 100644 (file)
     return _websitePolicies->contentBlockersEnabled();
 }
 
-- (void)setDeviceOrientationEventEnabled:(BOOL)deviceOrientationEventEnabled
-{
-    _websitePolicies->setDeviceOrientationEventEnabled(deviceOrientationEventEnabled);
-}
-
-- (BOOL)deviceOrientationEventEnabled
-{
-    return _websitePolicies->deviceOrientationEventEnabled();
-}
-
 - (void)setAllowedAutoplayQuirks:(_WKWebsiteAutoplayQuirk)allowedQuirks
 {
     OptionSet<WebKit::WebsiteAutoplayQuirk> quirks;
     }
 }
 
+- (void)setDeviceOrientationAndMotionAccessPolicy:(_WKWebsiteDeviceOrientationAndMotionAccessPolicy)policy
+{
+    switch (policy) {
+    case _WKWebsiteDeviceOrientationAndMotionAccessPolicyAsk:
+        _websitePolicies->setDeviceOrientationAndMotionAccessState(WTF::nullopt);
+        break;
+    case _WKWebsiteDeviceOrientationAndMotionAccessPolicyGrant:
+        _websitePolicies->setDeviceOrientationAndMotionAccessState(true);
+        break;
+    case _WKWebsiteDeviceOrientationAndMotionAccessPolicyDeny:
+        _websitePolicies->setDeviceOrientationAndMotionAccessState(false);
+        break;
+    }
+}
+
+- (_WKWebsiteDeviceOrientationAndMotionAccessPolicy)deviceOrientationAndMotionAccessPolicy
+{
+    if (!_websitePolicies->deviceOrientationAndMotionAccessState())
+        return _WKWebsiteDeviceOrientationAndMotionAccessPolicyAsk;
+    return *_websitePolicies->deviceOrientationAndMotionAccessState() ? _WKWebsiteDeviceOrientationAndMotionAccessPolicyGrant : _WKWebsiteDeviceOrientationAndMotionAccessPolicyDeny;
+}
+
 - (void)setPopUpPolicy:(_WKWebsitePopUpPolicy)policy
 {
     switch (policy) {
index b1ebde3..de71327 100644 (file)
@@ -1,5 +1,19 @@
 2019-03-14  Chris Dumez  <cdumez@apple.com>
 
+        Add WebsitePolicy for the client to specify the device orientation & motion access policy
+        https://bugs.webkit.org/show_bug.cgi?id=195750
+
+        Reviewed by Geoffrey Garen.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm:
+        (-[WebsitePoliciesDeviceOrientationDelegate initWithDeviceOrientationAccessPolicy:]):
+        (-[WebsitePoliciesDeviceOrientationDelegate _webView:decidePolicyForNavigationAction:userInfo:decisionHandler:]):
+        (-[WebsitePoliciesDeviceOrientationUIDelegate _webView:shouldAllowDeviceOrientationAndMotionAccessRequestedByFrame:decisionHandler:]):
+
+2019-03-14  Chris Dumez  <cdumez@apple.com>
+
         [PSON] Make sure the WebProcessCache is leverage when relaunching a process after termination
         https://bugs.webkit.org/show_bug.cgi?id=195747
 
index f14293b..2a476cf 100644 (file)
@@ -1246,24 +1246,24 @@ addEventListener("deviceorientation", (event) => {
 )TESTRESOURCE";
 
 @interface WebsitePoliciesDeviceOrientationDelegate : NSObject <WKNavigationDelegate> {
-    BOOL _deviceOrientationEventEnabled;
+    _WKWebsiteDeviceOrientationAndMotionAccessPolicy _accessPolicy;
 }
-- (instancetype)initWithDeviceOrientationEventEnabled:(BOOL)enabled;
+- (instancetype)initWithDeviceOrientationAccessPolicy:(_WKWebsiteDeviceOrientationAndMotionAccessPolicy)accessPolicy;
 @end
 
 @implementation WebsitePoliciesDeviceOrientationDelegate
 
-- (instancetype)initWithDeviceOrientationEventEnabled:(BOOL)enabled
+- (instancetype)initWithDeviceOrientationAccessPolicy:(_WKWebsiteDeviceOrientationAndMotionAccessPolicy)accessPolicy
 {
     self = [super init];
-    _deviceOrientationEventEnabled = enabled;
+    _accessPolicy = accessPolicy;
     return self;
 }
 
 - (void)_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction userInfo:(id <NSSecureCoding>)userInfo decisionHandler:(void (^)(WKNavigationActionPolicy, _WKWebsitePolicies *))decisionHandler
 {
     _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease];
-    [websitePolicies setDeviceOrientationEventEnabled:_deviceOrientationEventEnabled];
+    [websitePolicies setDeviceOrientationAndMotionAccessPolicy:_accessPolicy];
 
     decisionHandler(WKNavigationActionPolicyAllow, websitePolicies);
 }
@@ -1275,6 +1275,8 @@ addEventListener("deviceorientation", (event) => {
 
 @end
 
+static bool calledShouldAllowDeviceOrientationAndMotionAccessDelegate = false;
+
 @interface WebsitePoliciesDeviceOrientationUIDelegate : NSObject <WKUIDelegatePrivate> {
 }
 @end
@@ -1283,12 +1285,13 @@ addEventListener("deviceorientation", (event) => {
 
 - (void)_webView:(WKWebView *)webView shouldAllowDeviceOrientationAndMotionAccessRequestedByFrame:(WKFrameInfo *)requestingFrame decisionHandler:(void (^)(BOOL))decisionHandler
 {
+    calledShouldAllowDeviceOrientationAndMotionAccessDelegate = true;
     decisionHandler(YES);
 }
 
 @end
 
-static void runWebsitePoliciesDeviceOrientationEventTest(bool websitePolicyValue)
+static void runWebsitePoliciesDeviceOrientationEventTest(_WKWebsiteDeviceOrientationAndMotionAccessPolicy accessPolicy)
 {
     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
 
@@ -1298,7 +1301,7 @@ static void runWebsitePoliciesDeviceOrientationEventTest(bool websitePolicyValue
 
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
 
-    auto delegate = adoptNS([[WebsitePoliciesDeviceOrientationDelegate alloc] initWithDeviceOrientationEventEnabled:websitePolicyValue]);
+    auto delegate = adoptNS([[WebsitePoliciesDeviceOrientationDelegate alloc] initWithDeviceOrientationAccessPolicy:accessPolicy]);
     [webView setNavigationDelegate:delegate.get()];
     auto uiDelegate = adoptNS([[WebsitePoliciesDeviceOrientationUIDelegate alloc] init]);
     [webView setUIDelegate:uiDelegate.get()];
@@ -1321,7 +1324,12 @@ static void runWebsitePoliciesDeviceOrientationEventTest(bool websitePolicyValue
 
     [webView _simulateDeviceOrientationChangeWithAlpha:1.0 beta:2.0 gamma:3.0];
 
-    if (websitePolicyValue)
+    if (accessPolicy == _WKWebsiteDeviceOrientationAndMotionAccessPolicyAsk)
+        EXPECT_TRUE(calledShouldAllowDeviceOrientationAndMotionAccessDelegate);
+    else
+        EXPECT_FALSE(calledShouldAllowDeviceOrientationAndMotionAccessDelegate);
+
+    if (accessPolicy != _WKWebsiteDeviceOrientationAndMotionAccessPolicyDeny)
         TestWebKitAPI::Util::run(&didReceiveMessage);
     else {
         TestWebKitAPI::Util::sleep(0.1);
@@ -1329,14 +1337,19 @@ static void runWebsitePoliciesDeviceOrientationEventTest(bool websitePolicyValue
     }
 }
 
-TEST(WebKit, WebsitePoliciesDeviceOrientationEventEnabled)
+TEST(WebKit, WebsitePoliciesDeviceOrientationGrantAccess)
+{
+    runWebsitePoliciesDeviceOrientationEventTest(_WKWebsiteDeviceOrientationAndMotionAccessPolicyGrant);
+}
+
+TEST(WebKit, WebsitePoliciesDeviceOrientationDenyAccess)
 {
-    runWebsitePoliciesDeviceOrientationEventTest(true);
+    runWebsitePoliciesDeviceOrientationEventTest(_WKWebsiteDeviceOrientationAndMotionAccessPolicyDeny);
 }
 
-TEST(WebKit, WebsitePoliciesDeviceOrientationEventDisabled)
+TEST(WebKit, WebsitePoliciesDeviceOrientationAskAccess)
 {
-    runWebsitePoliciesDeviceOrientationEventTest(false);
+    runWebsitePoliciesDeviceOrientationEventTest(_WKWebsiteDeviceOrientationAndMotionAccessPolicyAsk);
 }
 
 #endif // PLATFORM(IOS_FAMILY)