Bug 236714

Summary: Add -[WKWebProcessPlugInFrame lookUpFrameFromJSValue:]
Product: WebKit Reporter: Timothy Hatcher <timothy>
Component: WebKit APIAssignee: Timothy Hatcher <timothy>
Status: RESOLVED FIXED    
Severity: Normal CC: cdumez, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Local Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch none

Description Timothy Hatcher 2022-02-16 09:37:20 PST
Safari needs a way to convert a JS window object or frame DOM element into a WKWebProcessPlugInFrame in the injected bundle for web extensions.
Comment 1 Radar WebKit Bug Importer 2022-02-16 09:37:35 PST
<rdar://problem/89030846>
Comment 2 Timothy Hatcher 2022-02-16 09:44:10 PST
Created attachment 452203 [details]
Patch
Comment 3 Chris Dumez 2022-02-16 09:51:36 PST
Comment on attachment 452203 [details]
Patch

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

> Source/WebCore/page/Frame.cpp:1146
> +    JSC::JSGlobalObject* globalObject = toJS(context);

I would think you should be able to do:
return globalObject  ? activeDOMWindow(*globalObject).frame() : nullptr;

No?
Comment 4 Chris Dumez 2022-02-16 09:52:24 PST
Comment on attachment 452203 [details]
Patch

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

>> Source/WebCore/page/Frame.cpp:1146
>> +    JSC::JSGlobalObject* globalObject = toJS(context);
> 
> I would think you should be able to do:
> return globalObject  ? activeDOMWindow(*globalObject).frame() : nullptr;
> 
> No?

And if so, we probably don't need this new function on the Frame class at all.
Comment 5 Timothy Hatcher 2022-02-16 09:57:03 PST
Comment on attachment 452203 [details]
Patch

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

>>> Source/WebCore/page/Frame.cpp:1146
>>> +    JSC::JSGlobalObject* globalObject = toJS(context);
>> 
>> I would think you should be able to do:
>> return globalObject  ? activeDOMWindow(*globalObject).frame() : nullptr;
>> 
>> No?
> 
> And if so, we probably don't need this new function on the Frame class at all.

We need to be able to get the Frame from a JSHTMLIFrameElement, JSHTMLFrameElement or JSDOMWindow.
Comment 6 Chris Dumez 2022-02-16 10:28:26 PST
Comment on attachment 452203 [details]
Patch

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

> Source/WebCore/page/Frame.cpp:1141
> +Frame* Frame::fromJSValue(JSContextRef context, JSValueRef valueRef)

I think we should rename to make it clear we want a *content* frame, as suggested below.

> Source/WebCore/page/Frame.cpp:1149
> +

We can simplify a bit here:
```
if (auto* window = JSDOMWindow::toWrapped(vm, value))
    return window->frame();
```

> Source/WebCore/page/Frame.cpp:1159
> +    if (auto* frameElement = JSC::jsDynamicCast<JSHTMLIFrameElement*>(vm, value))

I think this may look a bit better:
```
auto* jsNode = JSC::jsDynamicCast<JSNode*>(vm, value);
if (!jsNode || !is<HTMLFrameOwnerElement>(jsNode->wrapped()))
    return nullptr;
return downcast<HTMLFrameOwnerElement>(*jsNode->wrapped()).contentFrame();
```

> Source/WebKit/WebProcess/InjectedBundle/API/Cocoa/WKWebProcessPlugInFramePrivate.h:36
> ++ (instancetype)lookUpFrameFromJSValue:(JSValue *)value;

I think we may want to rename this to lookUpContentFrameFromJSValue given the behavior. You don't really want the frame, you want the "content" frame in the case the value is a <frame> / <iframe>.
We may also want to convey the fact that this only works for window and frame elements, maybe something like: lookUpContentFrameForWindowOrFrameElement:
Comment 7 Timothy Hatcher 2022-02-16 13:45:54 PST
Created attachment 452246 [details]
Patch
Comment 8 Chris Dumez 2022-02-16 13:51:45 PST
Comment on attachment 452246 [details]
Patch

r=me
Comment 9 EWS 2022-02-17 00:01:21 PST
Committed r290001 (247387@main): <https://commits.webkit.org/247387@main>

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