From 338877b3cb544b528e98e089d48b0f1851d1a6cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20C=C3=A1ceres?= Date: Wed, 17 Jun 2020 10:18:41 +0000 Subject: [PATCH] Bug 1643205 - Navigator's share() must consume user activation r=edgar navigator.share() must consume the user activation. Differential Revision: https://phabricator.services.mozilla.com/D79640 --- dom/base/Navigator.cpp | 24 +++++------ dom/moz.build | 1 + dom/webshare/moz.build | 11 +++++ dom/webshare/test/mochitest/mochitest.ini | 5 +++ ...vigator_share_consume_user_activation.html | 43 +++++++++++++++++++ .../share-consume-activation.https.html | 33 ++++++++++++++ .../tests/web-share/share-empty.https.html | 41 ++++++++++++------ .../web-share/share-url-invalid.https.html | 23 ++++++---- 8 files changed, 147 insertions(+), 34 deletions(-) create mode 100644 dom/webshare/moz.build create mode 100644 dom/webshare/test/mochitest/mochitest.ini create mode 100644 dom/webshare/test/mochitest/test_navigator_share_consume_user_activation.html create mode 100644 testing/web-platform/tests/web-share/share-consume-activation.https.html diff --git a/dom/base/Navigator.cpp b/dom/base/Navigator.cpp index aa3fa6719f05..d8600ea437f4 100644 --- a/dom/base/Navigator.cpp +++ b/dom/base/Navigator.cpp @@ -1374,6 +1374,17 @@ Promise* Navigator::Share(const ShareData& aData, ErrorResult& aRv) { return nullptr; } + // null checked above + auto* doc = mWindow->GetExtantDoc(); + + if (StaticPrefs::dom_webshare_requireinteraction() && + !doc->ConsumeTransientUserGestureActivation()) { + aRv.ThrowNotAllowedError( + "User activation was already consumed " + "or share() was not activated by a user gesture."); + return nullptr; + } + // If none of data's members title, text, or url are present, reject p with // TypeError, and abort these steps. bool someMemberPassed = aData.mTitle.WasPassed() || aData.mText.WasPassed() || @@ -1384,9 +1395,6 @@ Promise* Navigator::Share(const ShareData& aData, ErrorResult& aRv) { return nullptr; } - // null checked above - auto doc = mWindow->GetExtantDoc(); - // If data's url member is present, try to resolve it... nsCOMPtr url; if (aData.mUrl.WasPassed()) { @@ -1415,16 +1423,6 @@ Promise* Navigator::Share(const ShareData& aData, ErrorResult& aRv) { text.SetIsVoid(true); } - // The spec does the "triggered by user activation" after the data checks. - // Unfortunately, both Chrome and Safari behave this way, so interop wins. - // https://github.com/w3c/web-share/pull/118 - if (StaticPrefs::dom_webshare_requireinteraction() && - !UserActivation::IsHandlingUserInput()) { - NS_WARNING("Attempt to share not triggered by user activation"); - aRv.Throw(NS_ERROR_DOM_NOT_ALLOWED_ERR); - return nullptr; - } - // Let mSharePromise be a new promise. mSharePromise = Promise::Create(mWindow->AsGlobal(), aRv); if (aRv.Failed()) { diff --git a/dom/moz.build b/dom/moz.build index c4c446fefb69..d34d8a7f7562 100644 --- a/dom/moz.build +++ b/dom/moz.build @@ -86,6 +86,7 @@ DIRS += [ 'url', 'webauthn', 'webidl', + 'webshare', 'xml', 'xslt', 'xul', diff --git a/dom/webshare/moz.build b/dom/webshare/moz.build new file mode 100644 index 000000000000..42720eb7fde7 --- /dev/null +++ b/dom/webshare/moz.build @@ -0,0 +1,11 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("Core", "DOM: Core & HTML") + +MOCHITEST_MANIFESTS += ['test/mochitest/mochitest.ini'] + diff --git a/dom/webshare/test/mochitest/mochitest.ini b/dom/webshare/test/mochitest/mochitest.ini new file mode 100644 index 000000000000..295815d492fa --- /dev/null +++ b/dom/webshare/test/mochitest/mochitest.ini @@ -0,0 +1,5 @@ +[DEFAULT] +prefs = + dom.webshare.enabled=true +scheme = https +[test_navigator_share_consume_user_activation.html] diff --git a/dom/webshare/test/mochitest/test_navigator_share_consume_user_activation.html b/dom/webshare/test/mochitest/test_navigator_share_consume_user_activation.html new file mode 100644 index 000000000000..9cb3f04d96a6 --- /dev/null +++ b/dom/webshare/test/mochitest/test_navigator_share_consume_user_activation.html @@ -0,0 +1,43 @@ + +Web Share: consume transient activation + + + + + + + diff --git a/testing/web-platform/tests/web-share/share-consume-activation.https.html b/testing/web-platform/tests/web-share/share-consume-activation.https.html new file mode 100644 index 000000000000..ba14117c1d95 --- /dev/null +++ b/testing/web-platform/tests/web-share/share-consume-activation.https.html @@ -0,0 +1,33 @@ + + + + + WebShare Test: consume user activation + + + + + + + + + + diff --git a/testing/web-platform/tests/web-share/share-empty.https.html b/testing/web-platform/tests/web-share/share-empty.https.html index ce7e2fefbd47..d0e0241ae44e 100644 --- a/testing/web-platform/tests/web-share/share-empty.https.html +++ b/testing/web-platform/tests/web-share/share-empty.https.html @@ -1,34 +1,49 @@ - + WebShare Test: Share no known fields + + diff --git a/testing/web-platform/tests/web-share/share-url-invalid.https.html b/testing/web-platform/tests/web-share/share-url-invalid.https.html index ca20cf0c70cc..7c79d9e841e2 100644 --- a/testing/web-platform/tests/web-share/share-url-invalid.https.html +++ b/testing/web-platform/tests/web-share/share-url-invalid.https.html @@ -1,20 +1,27 @@ - + WebShare Test: Share with an invalid URL + +