Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scrolling options to PDFJS context menus. #2417

Conversation

sammygill
Copy link
Contributor

@sammygill sammygill commented Jul 14, 2022

d50f30c

Add scrolling options to PDFJS context menus.
https://bugs.webkit.org/show_bug.cgi?id=237059
rdar://89315125

Reviewed by Aditya Keerthi.

This patch adds scrolling options to the context menu when right
clicking on a PDF that is being rendered in PDFJS. It is important to
note that this patch only adds the options to the context menu in
appearance only. Linking the options to the actual implementation in
PDFJS will be done in another patch.

By taking advantage of the current context menu code, such as in
ContextMenuController, we can try to keep the PDF code as close to the
rest of the context menu code. For example, we would have have to go
through WebPageProxy::showPDFContextMenu inside WebPageProxyMac, which
the current PDFPlugin code does.

The only special consideration that needs to be made is within
ContextMenuController::populate. We need to be able to determine that
the PDF context menu items must be added to the context menu. We can
do this by determining that the current frame is an iframe
(!frame->isMainFrame) and that the owning document is a PDFDocument
(frame->ownerElement()->document().isPDFDocument()). Similarly, in
already existing cases where the code checks for frame->page(), we must
add an extra condition that the owning document is NOT a PDFDocument so
that we do not add any extra context menu items to PDF context menus and
remain consistent with the current PDF context menu.

* Source/WebCore/en.lproj/Localizable.strings:
* Source/WebCore/page/ContextMenuController.cpp:
(WebCore::ContextMenuController::checkOrEnableIfNeeded const):
(WebCore::ContextMenuController::populate):
Goes through and determines where to add the PDF context menu items. We
need to make sure that the normal context menu remain the same (when
not viewing a PDF) and also to make sure that the PDF context menu
remains consistent with the current implementation.

* Source/WebCore/platform/ContextMenuItem.cpp:
(WebCore::isValidContextMenuAction):
* Source/WebCore/platform/ContextMenuItem.h:
Adds the new context menu scrolling items to the ContextMenuAction
enum.

* Source/WebCore/platform/LocalizedStrings.cpp:
(WebCore::contextMenuItemPDFSinglePage):
(WebCore::contextMenuItemPDFSinglePageContinuous):
(WebCore::contextMenuItemPDFTwoPages):
(WebCore::contextMenuItemPDFTwoPagesContinuous):
* Source/WebCore/platform/LocalizedStrings.h:
* Source/WebKitLegacy/mac/WebView/WebHTMLView.mm:
(toAction):
(toTag):

Canonical link: https://commits.webkit.org/253130@main

@sammygill sammygill self-assigned this Jul 14, 2022
@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from b55bd47 to 1ff6586 Compare July 14, 2022 17:56
@sammygill sammygill changed the title Need a short description (OOPS!). Create context menu with scrolling options. Jul 14, 2022
@sammygill sammygill added PDF For bugs in WebKit's built-in PDF support. WebKit Nightly Build labels Jul 14, 2022
@sammygill sammygill marked this pull request as draft July 14, 2022 17:57
Copy link
Member

@nt1m nt1m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is specific to PDF.js, it should probably not touch PDFPlugin code

@sammygill
Copy link
Contributor Author

If this is specific to PDF.js, it should probably not touch PDFPlugin code

It looks like to get outside of PDFPlugin we will want to register some event handler outside of it. WebCore::callDefaultEventHandlersInBubblingOrder eventually calls HTMLPluginElement::defaultEventHandler(). Maybe this could be done inside WebCore::PDFDocument or is there perhaps a better area?

@sammygill
Copy link
Contributor Author

If this is specific to PDF.js, it should probably not touch PDFPlugin code

It looks like to get outside of PDFPlugin we will want to register some event handler outside of it. WebCore::callDefaultEventHandlersInBubblingOrder eventually calls HTMLPluginElement::defaultEventHandler(). Maybe this could be done inside WebCore::PDFDocument or is there perhaps a better area?

Actually I don't think PDFDocument is the right place to do this. I'll need to look around a bit

@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from 1ff6586 to c72b4aa Compare July 15, 2022 23:15
@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label Jul 15, 2022
@sammygill sammygill removed the merging-blocked Applied to prevent a change from being merged label Jul 19, 2022
@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from c72b4aa to d670863 Compare July 19, 2022 00:16
@sammygill sammygill changed the title Create context menu with scrolling options. Need a short description (OOPS!). Jul 19, 2022
@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label Jul 19, 2022
@sammygill sammygill removed the merging-blocked Applied to prevent a change from being merged label Jul 21, 2022
@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from d670863 to c005256 Compare July 21, 2022 17:56
@sammygill sammygill changed the title Need a short description (OOPS!). Create context menu with scrolling options. Jul 21, 2022
@sammygill sammygill marked this pull request as ready for review July 21, 2022 17:57
@sammygill sammygill requested a review from cdumez as a code owner July 21, 2022 17:57
@cdumez
Copy link
Contributor

cdumez commented Jul 21, 2022

This PR title seems overly generic. It should probably say something about PDFjs or PDF.

@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from c005256 to 5b20186 Compare July 21, 2022 18:11
@sammygill sammygill changed the title Create context menu with scrolling options. Add scrolling options to PDFJS context menus. Jul 21, 2022
@sammygill
Copy link
Contributor Author

This PR title seems overly generic. It should probably say something about PDFjs or PDF.

Updated!

@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from 5b20186 to dc8d657 Compare July 21, 2022 18:40
appendItem(StopItem, m_contextMenu.get());
else
appendItem(ReloadItem, m_contextMenu.get());
if (frame->isMainFrame() || (frame->page() && !frame->ownerElement()->document().isPDFDocument())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to null check frame->ownerElement() instead of frame->page()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the null pointer check here. Also removed the frame->page() check since the more specific if branches are checking for them along with other checks below.

#endif
}

if (frame->page() && !frame->isMainFrame())
if (frame->page() && !frame->isMainFrame() && !frame->ownerElement()->document().isPDFDocument())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What guarantees that frame->ownerElement() is non null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I had some misunderstandings between frames and pages. I will definitely need to add a null check for that. If we got rid of the frame->page() check here would that be ok? Since a frame could get detached from a page (although I don't entirely know what that scenario would be), would we want to check that or do you not think it would matter in this case? Also, it looks like I might want to add a null check for the document as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to add a null check for the document as well?
No, document() is a reference, not a pointer so it cannot be null.

But when you dereference this pointer (frame->ownerElement()) without a null check, this looks dangerous.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the frame->ownerElement null pointer check here

@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from dc8d657 to f86f94d Compare July 21, 2022 21:37
@nt1m
Copy link
Member

nt1m commented Jul 22, 2022

Can you guard the new code behind ENABLE(PDFJS) ?

@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from f86f94d to 3e8dad2 Compare July 22, 2022 21:09
Source/WebCore/page/ContextMenuController.cpp Outdated Show resolved Hide resolved
Source/WebCore/page/ContextMenuController.cpp Outdated Show resolved Hide resolved
Source/WebCore/page/ContextMenuController.cpp Outdated Show resolved Hide resolved
Source/WebCore/platform/ContextMenuItem.cpp Outdated Show resolved Hide resolved
@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from 2732914 to 3deaa11 Compare August 2, 2022 22:07
@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch 2 times, most recently from 6adc716 to 704f9bd Compare August 3, 2022 16:57
@pxlcoder pxlcoder added the merge-queue Applied to send a pull request to merge-queue label Aug 4, 2022
Comment on lines 268 to 271
case ContextMenuAction::ContextMenuItemPDFSinglePageContinuous:
case ContextMenuAction::ContextMenuItemPDFTwoPages:
case ContextMenuAction::ContextMenuItemPDFTwoPagesContinuous:
return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sgill26 It would be good to move these up and share the return true; with the other cases in a follow-up patch (maybe the patch where you hook up the context menu items into PDF.js).

@webkit-commit-queue
Copy link
Collaborator

Commit message contains (OOPS!), blocking PR #2417

@webkit-commit-queue webkit-commit-queue added merging-blocked Applied to prevent a change from being merged and removed merge-queue Applied to send a pull request to merge-queue labels Aug 4, 2022
@aj062 aj062 added merge-queue Applied to send a pull request to merge-queue and removed merging-blocked Applied to prevent a change from being merged labels Aug 4, 2022
@webkit-commit-queue
Copy link
Collaborator

Commit message contains (OOPS!) and no reviewer found, blocking PR #2417

@webkit-commit-queue webkit-commit-queue added merging-blocked Applied to prevent a change from being merged and removed merge-queue Applied to send a pull request to merge-queue labels Aug 4, 2022
@sammygill sammygill removed the merging-blocked Applied to prevent a change from being merged label Aug 4, 2022
@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch 2 times, most recently from 941bdbd to a9d682f Compare August 4, 2022 17:14
@cdumez cdumez added the merge-queue Applied to send a pull request to merge-queue label Aug 4, 2022
Comment on lines 816 to 819
ContextMenuItem SinglePageItem(ActionType, ContextMenuItemPDFSinglePage, contextMenuItemPDFSinglePage());
ContextMenuItem SinglePageContinuousItem(ActionType, ContextMenuItemPDFSinglePageContinuous, contextMenuItemPDFSinglePageContinuous());
ContextMenuItem TwoPagesItem(ActionType, ContextMenuItemPDFTwoPages, contextMenuItemPDFTwoPages());
ContextMenuItem TwoPagesContinuousItem(ActionType, ContextMenuItemPDFTwoPagesContinuous, contextMenuItemPDFTwoPagesContinuous());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Indentation.

@cdumez cdumez removed the merge-queue Applied to send a pull request to merge-queue label Aug 4, 2022
@sammygill sammygill force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from a9d682f to 13e2ff7 Compare August 4, 2022 18:58
@pxlcoder pxlcoder added the merge-queue Applied to send a pull request to merge-queue label Aug 4, 2022
https://bugs.webkit.org/show_bug.cgi?id=237059
rdar://89315125

Reviewed by Aditya Keerthi.

This patch adds scrolling options to the context menu when right
clicking on a PDF that is being rendered in PDFJS. It is important to
note that this patch only adds the options to the context menu in
appearance only. Linking the options to the actual implementation in
PDFJS will be done in another patch.

By taking advantage of the current context menu code, such as in
ContextMenuController, we can try to keep the PDF code as close to the
rest of the context menu code. For example, we would have have to go
through WebPageProxy::showPDFContextMenu inside WebPageProxyMac, which
the current PDFPlugin code does.

The only special consideration that needs to be made is within
ContextMenuController::populate. We need to be able to determine that
the PDF context menu items must be added to the context menu. We can
do this by determining that the current frame is an iframe
(!frame->isMainFrame) and that the owning document is a PDFDocument
(frame->ownerElement()->document().isPDFDocument()). Similarly, in
already existing cases where the code checks for frame->page(), we must
add an extra condition that the owning document is NOT a PDFDocument so
that we do not add any extra context menu items to PDF context menus and
remain consistent with the current PDF context menu.

* Source/WebCore/en.lproj/Localizable.strings:
* Source/WebCore/page/ContextMenuController.cpp:
(WebCore::ContextMenuController::checkOrEnableIfNeeded const):
(WebCore::ContextMenuController::populate):
Goes through and determines where to add the PDF context menu items. We
need to make sure that the normal context menu remain the same (when
not viewing a PDF) and also to make sure that the PDF context menu
remains consistent with the current implementation.

* Source/WebCore/platform/ContextMenuItem.cpp:
(WebCore::isValidContextMenuAction):
* Source/WebCore/platform/ContextMenuItem.h:
Adds the new context menu scrolling items to the ContextMenuAction
enum.

* Source/WebCore/platform/LocalizedStrings.cpp:
(WebCore::contextMenuItemPDFSinglePage):
(WebCore::contextMenuItemPDFSinglePageContinuous):
(WebCore::contextMenuItemPDFTwoPages):
(WebCore::contextMenuItemPDFTwoPagesContinuous):
* Source/WebCore/platform/LocalizedStrings.h:
* Source/WebKitLegacy/mac/WebView/WebHTMLView.mm:
(toAction):
(toTag):

Canonical link: https://commits.webkit.org/253130@main
@webkit-early-warning-system webkit-early-warning-system force-pushed the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch from 13e2ff7 to d50f30c Compare August 4, 2022 20:47
@webkit-commit-queue
Copy link
Collaborator

Committed 253130@main (d50f30c): https://commits.webkit.org/253130@main

Reviewed commits have been landed. Closing PR #2417 and removing active labels.

@webkit-early-warning-system webkit-early-warning-system merged commit d50f30c into WebKit:main Aug 4, 2022
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Aug 4, 2022
@sammygill sammygill deleted the eng/PDF-js-context-menu-should-match-PDFKits-context-menu branch April 19, 2024 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PDF For bugs in WebKit's built-in PDF support.
Projects
None yet
8 participants