⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 276181 in webkit


Ignore:
Timestamp:
Apr 16, 2021, 4:53:19 PM (5 years ago)
Author:
Wenson Hsieh
Message:

[macOS] Refactor some webpage translation code
https://bugs.webkit.org/show_bug.cgi?id=224680
Work towards <rdar://75641882>

Reviewed by Tim Horton.

Source/WebCore:

In preparation for fixing https://webkit.org/b/224683, refactor some codepaths for handling webpage translation
via the context menu on macOS, in WebKit2.

Currently, the context menu action for ContextMenuItemTagTranslate is fully handled in the client layer in
WebKit2, using state in m_activeContextMenuContextData. However, to make this action work in WebKitLegacy as
well, we need the ability to call back into the ContextMenuController to handle the action, since context menu
actions in WebKitLegacy only target the shared WebMenuTarget instance, which only knows about the context menu
controller rather than the WebHTMLView that vended the menu item.

Instead of adding logic in WebMenuTarget to dig the WebHTMLView corresponding to the focused frame out of
the context menu controller, it makes more sense to plumb this call through the (already-established)
ContextMenuClient. While this has the disadvantage of requiring an extra IPC hop on the WebKit2 case, it also
has the advantage that we can lazily compute the selection bounds and menu location in root view coordinates
only if the user has selected this menu action, which makes context menu data (slightly) cheaper to compute.

No change in behavior.

  • loader/EmptyClients.cpp:
  • page/ContextMenuClient.h:
  • page/ContextMenuContext.h:

(WebCore::ContextMenuContext::setSelectionBounds): Deleted.
(WebCore::ContextMenuContext::selectionBounds const): Deleted.

Remove code for computing and setting selection bounds. This was only added in support of webpage translation;
instead of computing this up front, we can instead send this information only when the action is invoked.

  • page/ContextMenuController.cpp:

(WebCore::ContextMenuController::contextMenuItemSelected):

Move logic for computing the selection bounds and menu location in root view coordinates out of populate and
into contextMenuItemSelected, only in the case where the action is ContextMenuItemTagTranslate.

(WebCore::ContextMenuController::populate):

Source/WebKit:

See WebCore ChangeLog for more details.

  • Shared/ContextMenuContextData.cpp:

(WebKit::ContextMenuContextData::ContextMenuContextData):
(WebKit::ContextMenuContextData::encode const):
(WebKit::ContextMenuContextData::decode):

  • Shared/ContextMenuContextData.h:

Remove selectionBounds. We don't need this anymore, because we'll instead compute the selection bounds only
when the Translate menu item is selected, instead of relying on m_activeContextMenuContextData being up to
date.

(WebKit::ContextMenuContextData::selectedText const):
(WebKit::ContextMenuContextData::selectionBounds const): Deleted.

  • UIProcess/Cocoa/WebPageProxyCocoa.mm:

(WebKit::WebPageProxy::handleContextMenuTranslation):

  • UIProcess/WebPageProxy.cpp:

(WebKit::WebPageProxy::contextMenuItemSelected):

  • UIProcess/WebPageProxy.h:
  • UIProcess/WebPageProxy.messages.in:
  • WebProcess/WebCoreSupport/WebContextMenuClient.h:
  • WebProcess/WebCoreSupport/mac/WebContextMenuClientMac.mm:

(WebKit::WebContextMenuClient::handleTranslation):

  • WebProcess/WebPage/WebPage.cpp:

(WebKit::WebPage::handleContextMenuTranslation):

  • WebProcess/WebPage/WebPage.h:

Source/WebKitLegacy/mac:

See WebCore ChangeLog for more details.

  • WebCoreSupport/WebContextMenuClient.h:
  • WebCoreSupport/WebContextMenuClient.mm:

(WebContextMenuClient::handleTranslation):

  • WebView/WebView.mm:

(-[WebView _handleContextMenuTranslation:selectionBounds:menuLocation:]):

Add an empty stub with a FIXME for the time being.

  • WebView/WebViewInternal.h:
Location:
trunk/Source
Files:
21 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r276180 r276181  
     12021-04-16  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [macOS] Refactor some webpage translation code
     4        https://bugs.webkit.org/show_bug.cgi?id=224680
     5        Work towards <rdar://75641882>
     6
     7        Reviewed by Tim Horton.
     8
     9        In preparation for fixing https://webkit.org/b/224683, refactor some codepaths for handling webpage translation
     10        via the context menu on macOS, in WebKit2.
     11
     12        Currently, the context menu action for `ContextMenuItemTagTranslate` is fully handled in the client layer in
     13        WebKit2, using state in `m_activeContextMenuContextData`. However, to make this action work in WebKitLegacy as
     14        well, we need the ability to call back into the `ContextMenuController` to handle the action, since context menu
     15        actions in WebKitLegacy only target the shared `WebMenuTarget` instance, which only knows about the context menu
     16        controller rather than the `WebHTMLView` that vended the menu item.
     17
     18        Instead of adding logic in `WebMenuTarget` to dig the `WebHTMLView` corresponding to the focused frame out of
     19        the context menu controller, it makes more sense to plumb this call through the (already-established)
     20        `ContextMenuClient`. While this has the disadvantage of requiring an extra IPC hop on the WebKit2 case, it also
     21        has the advantage that we can lazily compute the selection bounds and menu location in root view coordinates
     22        only if the user has selected this menu action, which makes context menu data (slightly) cheaper to compute.
     23
     24        No change in behavior.
     25
     26        * loader/EmptyClients.cpp:
     27        * page/ContextMenuClient.h:
     28        * page/ContextMenuContext.h:
     29        (WebCore::ContextMenuContext::setSelectionBounds): Deleted.
     30        (WebCore::ContextMenuContext::selectionBounds const): Deleted.
     31
     32        Remove code for computing and setting selection bounds. This was only added in support of webpage translation;
     33        instead of computing this up front, we can instead send this information only when the action is invoked.
     34
     35        * page/ContextMenuController.cpp:
     36        (WebCore::ContextMenuController::contextMenuItemSelected):
     37
     38        Move logic for computing the selection bounds and menu location in root view coordinates out of `populate` and
     39        into `contextMenuItemSelected`, only in the case where the action is `ContextMenuItemTagTranslate`.
     40
     41        (WebCore::ContextMenuController::populate):
     42
    1432021-04-16  Jiewen Tan  <jiewen_tan@apple.com>
    244
  • trunk/Source/WebCore/loader/EmptyClients.cpp

    r275163 r276181  
    116116#endif
    117117
     118#if HAVE(TRANSLATION_UI_SERVICES)
     119    void handleTranslation(const String&, const IntRect&, const IntPoint&) final { }
     120#endif
     121
    118122#if PLATFORM(GTK)
    119123    void insertEmoji(Frame&) final { }
  • trunk/Source/WebCore/page/ContextMenuClient.h

    r245460 r276181  
    4646    virtual void stopSpeaking() = 0;
    4747
     48#if HAVE(TRANSLATION_UI_SERVICES)
     49    virtual void handleTranslation(const String&, const IntRect&, const IntPoint&) = 0;
     50#endif
     51
    4852#if PLATFORM(COCOA)
    4953    virtual void searchWithSpotlight() = 0;
  • trunk/Source/WebCore/page/ContextMenuContext.h

    r274521 r276181  
    5656    const String& selectedText() const { return m_selectedText; }
    5757
    58     void setSelectionBounds(const IntRect& bounds) { m_selectionBounds = bounds; }
    59     const IntRect& selectionBounds() const { return m_selectionBounds; }
    60 
    6158#if ENABLE(SERVICE_CONTROLS)
    6259    void setControlledImage(Image* controlledImage) { m_controlledImage = controlledImage; }
     
    6865    HitTestResult m_hitTestResult;
    6966    String m_selectedText;
    70     IntRect m_selectionBounds;
    7167
    7268#if ENABLE(SERVICE_CONTROLS)
  • trunk/Source/WebCore/page/ContextMenuController.cpp

    r276084 r276181  
    524524        break;
    525525    case ContextMenuItemTagRevealImage:
    526     case ContextMenuItemTagTranslate:
    527526        // This should be handled at the client layer.
    528527        ASSERT_NOT_REACHED();
     528        break;
     529    case ContextMenuItemTagTranslate:
     530#if HAVE(TRANSLATION_UI_SERVICES)
     531        if (auto view = makeRefPtr(frame->view())) {
     532            auto selectionBounds = view->contentsToRootView(enclosingIntRect(frame->selection().selectionBounds()));
     533            auto location = view->contentsToRootView(m_context.hitTestResult().roundedPointInInnerNodeFrame());
     534            m_client.handleTranslation(m_context.hitTestResult().selectedText(), selectionBounds, location);
     535        }
     536#endif
    529537        break;
    530538    default:
     
    869877
    870878    auto selectedText = m_context.hitTestResult().selectedText();
    871     if (!selectedText.isEmpty()) {
    872         m_context.setSelectedText(selectedText);
    873         if (auto view = makeRefPtr(frame->view())) {
    874             auto selectionBoundsInContentCoordinates = enclosingIntRect(frame->selection().selectionBounds());
    875             if (!selectionBoundsInContentCoordinates.isEmpty())
    876                 m_context.setSelectionBounds(view->contentsToRootView(selectionBoundsInContentCoordinates));
    877         }
    878     }
     879    m_context.setSelectedText(selectedText);
    879880
    880881    if (!m_context.hitTestResult().isContentEditable()) {
  • trunk/Source/WebKit/ChangeLog

    r276180 r276181  
     12021-04-16  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [macOS] Refactor some webpage translation code
     4        https://bugs.webkit.org/show_bug.cgi?id=224680
     5        Work towards <rdar://75641882>
     6
     7        Reviewed by Tim Horton.
     8
     9        See WebCore ChangeLog for more details.
     10
     11        * Shared/ContextMenuContextData.cpp:
     12        (WebKit::ContextMenuContextData::ContextMenuContextData):
     13        (WebKit::ContextMenuContextData::encode const):
     14        (WebKit::ContextMenuContextData::decode):
     15        * Shared/ContextMenuContextData.h:
     16
     17        Remove `selectionBounds`. We don't need this anymore, because we'll instead compute the selection bounds only
     18        when the Translate menu item is selected, instead of relying on `m_activeContextMenuContextData` being up to
     19        date.
     20
     21        (WebKit::ContextMenuContextData::selectedText const):
     22        (WebKit::ContextMenuContextData::selectionBounds const): Deleted.
     23        * UIProcess/Cocoa/WebPageProxyCocoa.mm:
     24        (WebKit::WebPageProxy::handleContextMenuTranslation):
     25        * UIProcess/WebPageProxy.cpp:
     26        (WebKit::WebPageProxy::contextMenuItemSelected):
     27        * UIProcess/WebPageProxy.h:
     28        * UIProcess/WebPageProxy.messages.in:
     29        * WebProcess/WebCoreSupport/WebContextMenuClient.h:
     30        * WebProcess/WebCoreSupport/mac/WebContextMenuClientMac.mm:
     31        (WebKit::WebContextMenuClient::handleTranslation):
     32        * WebProcess/WebPage/WebPage.cpp:
     33        (WebKit::WebPage::handleContextMenuTranslation):
     34        * WebProcess/WebPage/WebPage.h:
     35
    1362021-04-16  Jiewen Tan  <jiewen_tan@apple.com>
    237
  • trunk/Source/WebKit/Shared/ContextMenuContextData.cpp

    r274521 r276181  
    5454    , m_webHitTestResultData(context.hitTestResult(), true)
    5555    , m_selectedText(context.selectedText())
    56     , m_selectionBounds(context.selectionBounds())
    5756#if ENABLE(SERVICE_CONTROLS)
    5857    , m_selectionIsEditable(false)
     
    8079    encoder << m_webHitTestResultData;
    8180    encoder << m_selectedText;
    82     encoder << m_selectionBounds;
    8381
    8482#if ENABLE(SERVICE_CONTROLS)
     
    108106
    109107    if (!decoder.decode(result.m_selectedText))
    110         return false;
    111 
    112     if (!decoder.decode(result.m_selectionBounds))
    113108        return false;
    114109
  • trunk/Source/WebKit/Shared/ContextMenuContextData.h

    r274521 r276181  
    5555    const WebHitTestResultData& webHitTestResultData() const { return m_webHitTestResultData; }
    5656    const String& selectedText() const { return m_selectedText; }
    57     const WebCore::IntRect& selectionBounds() const { return m_selectionBounds; }
    5857
    5958#if ENABLE(SERVICE_CONTROLS)
     
    8685    WebHitTestResultData m_webHitTestResultData;
    8786    String m_selectedText;
    88     WebCore::IntRect m_selectionBounds;
    8987
    9088#if ENABLE(SERVICE_CONTROLS)
  • trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm

    r275768 r276181  
    616616}
    617617
     618void WebPageProxy::handleContextMenuTranslation(const String& text, const WebCore::IntRect& boundsInView, const WebCore::IntPoint& locationInView)
     619{
     620    return pageClient().handleContextMenuTranslation(text, boundsInView, locationInView);
     621}
     622
    618623#endif // HAVE(TRANSLATION_UI_SERVICES)
    619624#endif // ENABLE(CONTEXT_MENUS)
  • trunk/Source/WebKit/UIProcess/WebPageProxy.cpp

    r276177 r276181  
    67476747        return;
    67486748
    6749     case ContextMenuItemTagTranslate:
    6750 #if HAVE(TRANSLATION_UI_SERVICES)
    6751         pageClient().handleContextMenuTranslation(m_activeContextMenuContextData.selectedText(), m_activeContextMenuContextData.selectionBounds(), m_activeContextMenuContextData.menuLocation());
    6752 #endif
    6753         return;
    6754 
    67556749    default:
    67566750        break;
  • trunk/Source/WebKit/UIProcess/WebPageProxy.h

    r276177 r276181  
    19051905#endif
    19061906
    1907 #if HAVE(TRANSLATION_UI_SERVICES)
     1907#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
    19081908    bool canHandleContextMenuTranslation() const;
     1909    void handleContextMenuTranslation(const String& text, const WebCore::IntRect& boundsInView, const WebCore::IntPoint& locationInView);
    19091910#endif
    19101911
  • trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in

    r276177 r276181  
    226226#endif
    227227
     228#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     229    HandleContextMenuTranslation(String text, WebCore::IntRect boundsInView, WebCore::IntPoint locationInView)
     230#endif
     231
    228232#if ENABLE(MEDIA_CONTROLS_CONTEXT_MENUS) && USE(UICONTEXTMENU)
    229233    ShowMediaControlsContextMenu(WebCore::FloatRect targetFrame, Vector<WebCore::MediaControlsContextMenuItem> items) -> (WebCore::MediaControlsContextMenuItem::ID selectedItemID) Async
  • trunk/Source/WebKit/WebProcess/WebCoreSupport/WebContextMenuClient.h

    r248762 r276181  
    5757#endif
    5858
     59#if HAVE(TRANSLATION_UI_SERVICES)
     60    void handleTranslation(const String&, const WebCore::IntRect& selectionBoundsInRootView, const WebCore::IntPoint& locationInRootView) final;
     61#endif
     62
    5963#if PLATFORM(GTK)
    6064    void insertEmoji(WebCore::Frame&) override;
  • trunk/Source/WebKit/WebProcess/WebCoreSupport/mac/WebContextMenuClientMac.mm

    r235205 r276181  
    9595}
    9696
     97#if HAVE(TRANSLATION_UI_SERVICES)
     98
     99void WebContextMenuClient::handleTranslation(const String& text, const IntRect& bounds, const IntPoint& location)
     100{
     101    m_page->send(Messages::WebPageProxy::HandleContextMenuTranslation(text, bounds, location));
     102}
     103
     104#endif // HAVE(TRANSLATION_UI_SERVICES)
     105
    97106} // namespace WebKit
    98107
  • trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp

    r275921 r276181  
    75297529}
    75307530
     7531#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     7532
     7533void WebPage::handleContextMenuTranslation(const String& text, const IntRect& boundsInView, const IntPoint& locationInView)
     7534{
     7535    send(Messages::WebPageProxy::HandleContextMenuTranslation(text, boundsInView, locationInView));
     7536}
     7537
     7538#endif
     7539
    75317540} // namespace WebKit
    75327541
  • trunk/Source/WebKit/WebProcess/WebPage/WebPage.h

    r276177 r276181  
    13921392#endif
    13931393
     1394#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     1395    void handleContextMenuTranslation(const String& text, const WebCore::IntRect& selectionBoundsInView, const WebCore::IntPoint& menuLocationInView);
     1396#endif
     1397
    13941398#if ENABLE(MEDIA_CONTROLS_CONTEXT_MENUS) && USE(UICONTEXTMENU)
    13951399    void showMediaControlsContextMenu(WebCore::FloatRect&&, Vector<WebCore::MediaControlsContextMenuItem>&&, CompletionHandler<void(WebCore::MediaControlsContextMenuItem::ID)>&&);
  • trunk/Source/WebKitLegacy/mac/ChangeLog

    r276177 r276181  
     12021-04-16  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [macOS] Refactor some webpage translation code
     4        https://bugs.webkit.org/show_bug.cgi?id=224680
     5        Work towards <rdar://75641882>
     6
     7        Reviewed by Tim Horton.
     8
     9        See WebCore ChangeLog for more details.
     10
     11        * WebCoreSupport/WebContextMenuClient.h:
     12        * WebCoreSupport/WebContextMenuClient.mm:
     13        (WebContextMenuClient::handleTranslation):
     14        * WebView/WebView.mm:
     15        (-[WebView _handleContextMenuTranslation:selectionBounds:menuLocation:]):
     16
     17        Add an empty stub with a `FIXME` for the time being.
     18
     19        * WebView/WebViewInternal.h:
     20
    1212021-04-16  Peng Liu  <peng.liu6@apple.com>
    222
  • trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.h

    r248762 r276181  
    6868#endif
    6969
     70#if HAVE(TRANSLATION_UI_SERVICES)
     71    void handleTranslation(const String&, const WebCore::IntRect& selectionBoundsInRootView, const WebCore::IntPoint& locationInRootView) final;
     72#endif
     73
    7074private:
    7175    NSMenu *contextMenuForEvent(NSEvent *, NSView *, bool& isServicesMenu);
  • trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.mm

    r260753 r276181  
    148148}
    149149
     150#if HAVE(TRANSLATION_UI_SERVICES)
     151
     152void WebContextMenuClient::handleTranslation(const String& text, const IntRect& selectionBoundsInRootView, const IntPoint& locationInRootView)
     153{
     154    [m_webView _handleContextMenuTranslation:text selectionBounds:selectionBoundsInRootView menuLocation:locationInRootView];
     155}
     156
     157#endif
     158
    150159#if ENABLE(SERVICE_CONTROLS)
    151160
  • trunk/Source/WebKitLegacy/mac/WebView/WebView.mm

    r276177 r276181  
    96289628}
    96299629
     9630#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     9631
     9632- (void)_handleContextMenuTranslation:(const String&)text selectionBounds:(const WebCore::IntRect&)selectionBoundsInRootView menuLocation:(const WebCore::IntPoint&)locationInRootView
     9633{
     9634    // FIXME (224683): Not implemented yet.
     9635    UNUSED_PARAM(text);
     9636    UNUSED_PARAM(selectionBoundsInRootView);
     9637    UNUSED_PARAM(locationInRootView);
     9638}
     9639
     9640#endif // HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     9641
    96309642@end
    96319643
  • trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h

    r276177 r276181  
    158158#endif
    159159
     160#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     161- (void)_handleContextMenuTranslation:(const String&)text selectionBounds:(const WebCore::IntRect&)boundsInView menuLocation:(const WebCore::IntPoint&)menuLocation;
     162#endif
     163
    160164- (void)_windowVisibilityChanged:(NSNotification *)notification;
    161165
Note: See TracChangeset for help on using the changeset viewer.