Bug 232584 - Web Inspector: REGRESSION(?): Emulate User Gesture doesn't work
Summary: Web Inspector: REGRESSION(?): Emulate User Gesture doesn't work
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Inspector (show other bugs)
Version: WebKit Nightly Build
Hardware: All All
: P2 Normal
Assignee: Devin Rousso
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-11-01 14:31 PDT by Devin Rousso
Modified: 2022-03-18 18:04 PDT (History)
11 users (show)

See Also:


Attachments
Patch v1.0 (13.45 KB, patch)
2021-11-01 20:14 PDT, Patrick Angle
no flags Details | Formatted Diff | Diff
Patch v1.1 - Fix additional user gesture emulation cases (16.79 KB, patch)
2021-11-01 20:35 PDT, Patrick Angle
hi: review-
Details | Formatted Diff | Diff
[fast-cq] Patch (47.80 KB, patch)
2022-03-16 11:43 PDT, Devin Rousso
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
[fast-cq] Patch (49.21 KB, patch)
2022-03-16 13:23 PDT, Devin Rousso
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Devin Rousso 2021-11-01 14:31:29 PDT
try evaluating the following on any page
```
     navigator.share({ text: "test" })
```

this used to work

now it errors with
> Unhandled Promise Rejection: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.

T.T
Comment 1 Radar WebKit Bug Importer 2021-11-01 14:32:26 PDT
<rdar://problem/84899576>
Comment 2 Patrick Angle 2021-11-01 20:14:38 PDT
Created attachment 443053 [details]
Patch v1.0
Comment 3 Patrick Angle 2021-11-01 20:35:27 PDT
Created attachment 443057 [details]
Patch v1.1 - Fix additional user gesture emulation cases
Comment 4 Devin Rousso 2021-11-01 22:41:26 PDT
Comment on attachment 443057 [details]
Patch v1.1 - Fix additional user gesture emulation cases

View in context: https://bugs.webkit.org/attachment.cgi?id=443057&action=review

r- as this definitely needs some tests, but otherwise the logic is sound :)

One thing worth noting is that the underlying concept we're trying to emulate with this is a "activation" <https://html.spec.whatwg.org/multipage/interaction.html#activation-notification>, and in the case of `navigator.share` specifically it's a "transient activation" that's consumed on the first attempt.  So we may wanna make a test that tries to call `navigator.share` twice in the same `Runtime.evaluate` and confirm that the second one fails because the first consumed the emulated "transient activation".

> Source/WebCore/ChangeLog:8
> +        In some code paths, including navigator.share require that we emulate user gestures for the specific Document

NIT: awkward sentence, please rephrase :)

> Source/WebCore/inspector/InspectorFrontendHost.cpp:113
> +                document = downcast<Document>(globalObject->scriptExecutionContext());

Can we actually assume for sure that this is a `Document`?  I think yes (since this is about contextmenus), but just wanna make sure :)

> Source/WebCore/inspector/agents/page/PageDebuggerAgent.cpp:70
> +Document* PageDebuggerAgent::documentForInjectedScript(InjectedScript injectedScript)

`const InjectedScript& injectedScript`

NIT: I kinda think this would be better on `PageRuntimeAgent` since "debugging" is kinda a subset of "regular evaluation" 😅

> Source/WebCore/inspector/agents/page/PageDebuggerAgent.cpp:84
> +    auto injectedScript = injectedScriptManager().injectedScriptForObjectId(callFrameId);

So this means that we have two calls to `injectedScriptForObjectId` in the same callstack, since `InspectorDebuggerAgent::evaluateOnCallFrame` also does it.  That's not the worst thing, but it's not super ideal either.

Rather than have that, perhaps we could remove the override `PageDebuggerAgent::evaluateOnCallFrame` and introduce a new `virtual UserGestureEmulationScope InspectorDebuggerAgent::emulateUserGesture(bool, const InjectedScript&) { }` that'd be overridden by `PageDebuggerAgent` and is called inside `InspectorDebuggerAgent::evaluateOnCallFrame` that'd do the `UserGestureEmulationScope` logic instead (tho this may require that we create a base `UserGestureEmulationScope` inside JSC that's overridden in WebCore)?

> Source/WebCore/inspector/agents/page/PageDebuggerAgent.cpp:140
> +        Document* document = nullptr;
> +        if (auto* domGlobalObject = JSC::jsCast<JSDOMGlobalObject*>(globalObject))
> +            document = downcast<Document>(domGlobalObject->scriptExecutionContext());

Could we also create a `static Document* PageDebuggerAgent::documentForGlobalObject(JSC::JSGlobalObject*)` so that this code isn't repeated?
Comment 5 Devin Rousso 2022-03-16 11:43:23 PDT
Created attachment 454869 [details]
[fast-cq] Patch

(apologies to @Patrick Angle, I totally forgot that I'd filed this bug AND that you'd already written a patch 😅 thanks for letting me take it)
Comment 6 Devin Rousso 2022-03-16 13:23:22 PDT
Created attachment 454888 [details]
[fast-cq] Patch
Comment 7 Patrick Angle 2022-03-18 16:21:09 PDT
Comment on attachment 454888 [details]
[fast-cq] Patch

rs=me, Nice tests!
Comment 8 EWS 2022-03-18 18:04:32 PDT
Committed r291517 (248623@main): <https://commits.webkit.org/248623@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 454888 [details].