Bug 236510

Summary: Inject custom styles into PDF.js to make it look like PDFKit
Product: WebKit Reporter: Tim Nguyen (:ntim) <ntim>
Component: PDFAssignee: Tim Nguyen (:ntim) <ntim>
Status: RESOLVED FIXED    
Severity: Normal CC: cdumez, changseok, esprehn+autocc, ews-watchlist, gyuyoung.kim, joepeck, kangil.han, thorton, webkit-bug-importer, youennf
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on:    
Bug Blocks: 235969    
Attachments:
Description Flags
Patch
none
Patch
none
Patch
thorton: review+
Patch
none
[fast-cq] Patch for landing
none
[fast-cq] Patch for landing none

Description Tim Nguyen (:ntim) 2022-02-11 07:49:54 PST
<rdar://problem/88457996>
Comment 1 Tim Nguyen (:ntim) 2022-02-11 07:50:01 PST
rdar://88457996
Comment 2 Tim Nguyen (:ntim) 2022-02-11 07:57:12 PST
Created attachment 451696 [details]
Patch
Comment 3 Tim Nguyen (:ntim) 2022-02-11 08:02:34 PST
Created attachment 451698 [details]
Patch
Comment 4 youenn fablet 2022-02-11 08:31:25 PST
Comment on attachment 451698 [details]
Patch

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

> Source/WebCore/ChangeLog:12
> +        (e.g. loading a blob, hooking with find-in-page messages, etc.).

There are other changes in this patch, is it expected?

> Source/WebCore/html/PDFDocument.cpp:58
> +    PDFDocumentEventListener(PDFDocument& document)

explicit.

> Source/WebCore/html/PDFDocument.cpp:67
> +    PDFDocument& m_document;

This is not safe, there is no guarantee m_document is alive during PDFDocumentEventListener lifetime
Comment 5 Tim Nguyen (:ntim) 2022-02-11 08:51:29 PST
Created attachment 451703 [details]
Patch
Comment 6 Tim Horton 2022-02-11 09:07:04 PST
Comment on attachment 451703 [details]
Patch

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

> Source/WebCore/html/PDFDocument.h:51
> +    RefPtr<HTMLIFrameElement> m_iframe;

“m_iFrame” I would imagine given the type’s capitalization, but check what we use elsewhere.
Comment 7 Chris Dumez 2022-02-11 09:14:31 PST
(In reply to Tim Horton from comment #6)
> Comment on attachment 451703 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=451703&action=review
> 
> > Source/WebCore/html/PDFDocument.h:51
> > +    RefPtr<HTMLIFrameElement> m_iframe;
> 
> “m_iFrame” I would imagine given the type’s capitalization, but check what
> we use elsewhere.

I have never seen m_iFrame in our code base. I think we consistently use "iframe".
Comment 8 youenn fablet 2022-02-11 09:15:54 PST
Comment on attachment 451703 [details]
Patch

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

> Source/WebCore/html/PDFDocument.cpp:153
> +    m_iframe = iframe.ptr();

m_iframe = WTFMove(iframe)

> Source/WebCore/html/PDFDocument.cpp:170
> +    auto script = HTMLScriptElement::create(scriptTag, *this, false, false);

It is a bit weird to create an element with *this while it will be added to another document.
You can probably pass a Document& to injectContentScript from the event handler, in which case no need to store m_frame at all.
Comment 9 Tim Nguyen (:ntim) 2022-02-11 09:19:39 PST
Created attachment 451705 [details]
Patch
Comment 10 Tim Nguyen (:ntim) 2022-02-11 09:25:39 PST
Created attachment 451707 [details]
[fast-cq] Patch for landing
Comment 11 Tim Nguyen (:ntim) 2022-02-11 09:28:08 PST
(In reply to youenn fablet from comment #8)
> Comment on attachment 451703 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=451703&action=review
> 
> > Source/WebCore/html/PDFDocument.cpp:153
> > +    m_iframe = iframe.ptr();
> 
> m_iframe = WTFMove(iframe)
> 
> > Source/WebCore/html/PDFDocument.cpp:170
> > +    auto script = HTMLScriptElement::create(scriptTag, *this, false, false);
> 
> It is a bit weird to create an element with *this while it will be added to
> another document.
> You can probably pass a Document& to injectContentScript from the event
> handler, in which case no need to store m_frame at all.

Done, I've used the contentDocument instead of *this. The m_iframe can't be removed since injectContentScript is called from the event listener. 

Besides that, it's useful to have it to send messages to the iframe if we need to in the future (for find in page for instance)
Comment 12 Tim Nguyen (:ntim) 2022-02-11 09:30:21 PST
Comment on attachment 451707 [details]
[fast-cq] Patch for landing

whoops not up to date
Comment 13 Tim Nguyen (:ntim) 2022-02-11 09:31:08 PST
Created attachment 451708 [details]
[fast-cq] Patch for landing
Comment 14 EWS 2022-02-11 09:32:02 PST
Committed r289627 (247137@main): <https://commits.webkit.org/247137@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 451707 [details].
Comment 15 Radar WebKit Bug Importer 2022-02-11 09:33:18 PST
<rdar://problem/88820270>
Comment 16 Tim Nguyen (:ntim) 2022-02-11 09:33:38 PST
Comment on attachment 451708 [details]
[fast-cq] Patch for landing

Welp, fast-cq was faster than me, will address comments manually
Comment 17 youenn fablet 2022-02-11 09:34:42 PST
> Done, I've used the contentDocument instead of *this. The m_iframe can't be
> removed since injectContentScript is called from the event listener. 

The event listener has a ScriptExecutionContext& which is probably the Document& you want to use.
In that case, why would we need m_iframe?
Comment 18 Tim Nguyen (:ntim) 2022-02-11 09:54:05 PST
(In reply to youenn fablet from comment #17)
> > Done, I've used the contentDocument instead of *this. The m_iframe can't be
> > removed since injectContentScript is called from the event listener. 
> 
> The event listener has a ScriptExecutionContext& which is probably the
> Document& you want to use.
> In that case, why would we need m_iframe?

Addressed in https://commits.webkit.org/r289630