Skip to content

Commit

Permalink
WebShare: Introduce web-share feature policy
Browse files Browse the repository at this point in the history
share() requests fail with a rejected promise if the 'web-share'
feature is not enabled.

We define 'self' as the default feature policy for web-share.

The spec has been updated
w3c/web-share#166

Intent to Ship
https://groups.google.com/a/chromium.org/g/blink-dev/c/fgme9KOd8CU/m/TCYPKQAXAwAJ

Bug: 1079104
Change-Id: Id4030448a54589eddb45185cbd6fd8970aee34c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2299791
Commit-Queue: Eric Willigers <ericwilligers@chromium.org>
Reviewed-by: Ian Clelland <iclelland@chromium.org>
Reviewed-by: Daniel Murphy <dmurph@chromium.org>
Reviewed-by: Dominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#789872}
  • Loading branch information
ericwilligers authored and Commit Bot committed Jul 20, 2020
1 parent 5f75772 commit e41da73
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ enum FeaturePolicyFeature {
// Controls use of Clipboard API Write.
kClipboardWrite = 77,

// Controls use of Web Share API.
kWebShare = 78,

// Don't change assigned numbers of any item, and don't reuse removed slots.
// Add new features at the end of the enum.
// Also, run update_feature_policy_enum.py in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"usb"
"vertical-scroll"
"vr"
"web-share"
"\"https://example.com/\""
"*"
"'self'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@
feature_default: "EnableForAll",
depends_on: ["ExperimentalProductivityFeatures"],
},
{
name: "WebShare",
feature_policy_name: "web-share",
depends_on: ["WebShare"],
},
{
name: "WebXr",
feature_policy_name: "xr-spatial-tracking",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <utility>

#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
#include "third_party/blink/public/mojom/feature_policy/feature_policy_feature.mojom-blink.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_share_data.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
Expand Down Expand Up @@ -202,6 +203,13 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state,
return ScriptPromise();
}

if (!ExecutionContext::From(script_state)
->IsFeatureEnabled(mojom::blink::FeaturePolicyFeature::kWebShare)) {
exception_state.ThrowDOMException(DOMExceptionCode::kNotAllowedError,
"Permission denied");
return ScriptPromise();
}

LocalDOMWindow* window = LocalDOMWindow::From(script_state);
if (!LocalFrame::ConsumeTransientUserActivation(window->GetFrame())) {
exception_state.ThrowDOMException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>WebShare Test: Can be disabled by feature policy</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<script>
promise_test(async t => {
await test_driver.bless("web share");

await promise_rejects_dom(t, "NotAllowedError", navigator.share({}));
}, "share can be disabled by feature policy");
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Feature-Policy: web-share 'none'
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ top-navigation
trust-token-redemption
usb
vertical-scroll
web-share
xr-spatial-tracking

1 change: 1 addition & 0 deletions tools/metrics/histograms/enums.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28608,6 +28608,7 @@ Called by update_feature_policy_enum.py.-->
<int value="75" label="CrossOriginIsolated"/>
<int value="76" label="ClipboardRead"/>
<int value="77" label="ClipboardWrite"/>
<int value="78" label="WebShare"/>
</enum>

<enum name="FeaturePolicyImageCompressionFormat">
Expand Down

0 comments on commit e41da73

Please sign in to comment.